A macro feature can be used to do this. You can even add the macro feature to your drawing template so that any future drawings made with the template will have the macro feature. Every rebuild the macro feature will check the current drawing path, trim off the part you don’t want and store the result in a custom property that can be linked to a note on your sheet format.
The macro also listens for file save events so that it can update the property. The macro consists of one regular module and one class module (for the event handling):
Here is the Main module:
Dim swApp As SldWorks.SldWorks
Dim mDoc As ModelDoc2
Dim fMgr As FeatureManager
Dim macroMethods(8) As String
Dim macroPath As String
Dim f As feature
Dim handler As New EventHandler
Const CUSTOM_PROPERTY_NAME = "Path"
Const START_OF_TRIMMED_PATH = "Desktop"
Option Explicit
Sub Main()
Set swApp = Application.SldWorks
Set mDoc = swApp.ActiveDoc
Set fMgr = mDoc.FeatureManager
macroPath = swApp.GetCurrentMacroPathName
macroMethods(0) = macroPath: macroMethods(1) = "Main": macroMethods(2) = "swmRebuild"
macroMethods(3) = macroPath: macroMethods(4) = "Main": macroMethods(5) = "swmEdit"
macroMethods(6) = macroPath: macroMethods(7) = "Main": macroMethods(8) = "swmSecurity"
Dim dimTypes As Variant
Dim dimValue As Variant
Dim vEditBodies As Variant
fMgr.InsertMacroFeature3 "StorePath", "", (macroMethods), Empty, Empty, Empty, dimTypes, dimValue, vEditBodies, Empty, swMacroFeatureEmbedMacroFile
End Sub
Function swmRebuild(vSW As Variant, vMDoc As Variant, feature As Variant) As Boolean
Dim mDoc As ModelDoc2
Set mDoc = vMDoc
handler.init mDoc
If Not hasProperty(mDoc) Then
createProperty mDoc
End If
updatePath mDoc
swmRebuild = True
End Function
Function swmEdit(swApp As Variant, mDoc As Variant, feature As Variant) As Boolean
swmEdit = True
End Function
Function swmSecurity(vSW As Variant, vDoc As Variant, vFeat As Variant) As Boolean
swmSecurity = SwConst.swMacroFeatureSecurityOptions_e.swMacroFeatureSecurityByDefault
End Function
Sub updatePath(mDoc As ModelDoc2)
Dim path As String
path = mDoc.GetPathName
If path <> "" Then
Dim mExt As ModelDocExtension
Set mExt = mDoc.Extension
Dim cMgr As CustomPropertyManager
Set cMgr = mExt.CustomPropertyManager("")
path = Mid(path, InStr(1, path, START_OF_TRIMMED_PATH , vbTextCompare))
cMgr.Set2 CUSTOM_PROPERTY_NAME, path
End If
End Sub
Sub createProperty(mDoc As ModelDoc2)
Dim mExt As ModelDocExtension
Set mExt = mDoc.Extension
Dim cMgr As CustomPropertyManager
Set cMgr = mExt.CustomPropertyManager("")
cMgr.Add3 CUSTOM_PROPERTY_NAME, swCustomInfoType_e.swCustomInfoText, "", swCustomPropertyOnlyIfNew
End Sub
Function hasProperty(mDoc As ModelDoc2) As Boolean
Dim mExt As ModelDocExtension
Set mExt = mDoc.Extension
Dim cMgr As CustomPropertyManager
Set cMgr = mExt.CustomPropertyManager("")
Dim val As String
Dim resolvedVal As String
Dim wasResolved As Boolean
If cMgr.Get5(CUSTOM_PROPERTY_NAME, False, val, resolvedVal, wasResolved) = swCustomInfoGetResult_NotPresent Then
hasProperty = False
Else
hasProperty = True
End If
End Function
Here is the class module:
Option Explicit
Public WithEvents swDrawing As SldWorks.DrawingDoc
Private Function swDrawing_FileSaveAsNotify2(ByVal filename As String) As Long
updatePath swDrawing
swDrawing_FileSaveAsNotify2 = 0
End Function
Public Function init(ByRef aDoc As ModelDoc2)
Set swDrawing = aDoc
End Function
The macro feature is set up so that it stores the macro code in the file itself. The two constants (CUSTOM_PROPERTY_NAME and START_OF_TRIMMED_PATH) at the start of the module control the name of the custom property and the text to search for in the file path. The START_OF_TRIMMED_PATH constant should be set to the text that always represents the start of the string before which everything should be dropped. As written, it is looking for “Desktop” and storing in a custom property named “Path”, which I used for testing purposes. So, 'C:\Users\jsculley\Desktop\SOLIDWORKS Files\somedrawing.slddrw" would be trimmed to “Desktop\SOLIDWORKS Files\somedrawing.slddrw” and stored in the Path custom property.
This is sample code only, not necessarily suitable for production.