Partial File Path

Use this space to ask how to do whatever you're trying to use SolidWorks to do.
TTevolve
Posts: 253
Joined: Wed Jan 05, 2022 10:15 am
Answers: 3
x 86
x 159

Partial File Path

Unread post by TTevolve »

On our drawing templates we have a file path shown (Using the custom properties SW-Folder Name). So recently we moved to a OneDrive setup for our files and the path added extra folder levels, so now it goes all they way across the template. Is there a way to parse out only part of the path to show?

For example our old file path was
X:\Data\Job Type\1000-2000\J1000\Filename

Now it looks like this
D:\OneDrive - Company Name\Documents - Communication site\Data\Job Type\1000-2000\J1000\Filename

Basically I would like to show just Data\Job Type\1000-2000\J1000\Filename on the boarder, is that possible to do?
by JSculley » Thu Apr 14, 2022 10:58 am
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):
image.png
Here is the Main module:

Code: Select all

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:

Code: Select all

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.
Go to full post
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 600

Re: Partial File Path

Unread post by zwei »

What SW version are you using?

SW21 allow equation to be used in custom property which might fit your need (I dont have 21 at the moment so i am not sure whether will it work)
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
Frank_Oostendorp
Posts: 221
Joined: Tue Mar 09, 2021 7:25 am
Answers: 3
Location: Netherlands
x 184
x 229

Re: Partial File Path

Unread post by Frank_Oostendorp »

@TTevolve just curious, what is the purpose to have the path shown on the drawing? Do you use it to open the part/assembly that is on the drawing? 😉
User avatar
gupta9665
Posts: 412
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 434
x 450

Re: Partial File Path

Unread post by gupta9665 »

The only way I can think of is via macro.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
SPerman
Posts: 2056
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2227
x 1878
Contact:

Re: Partial File Path

Unread post by SPerman »

You can map a drive to your onedrive folder so that the path is much shorter. This is what I did, before we adopted PDM.

https://www.computerworld.com/article/2 ... ndows.html

(I love the fact that the OS that will tell you the path is too long burns up 40+ characters creating absurdly long and unnecessary paths.)
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
Glenn Schroeder
Posts: 1522
Joined: Mon Mar 08, 2021 11:43 am
Answers: 23
Location: southeast Texas
x 1759
x 2132

Re: Partial File Path

Unread post by Glenn Schroeder »

Frank_Oostendorp wrote: Thu Apr 14, 2022 5:55 am @TTevolve just curious, what is the purpose to have the path shown on the drawing? Do you use it to open the part/assembly that is on the drawing? 😉
I can't answer for @TTevolve, but I also have the file path in the margin of my Drawings. It's helpful when I have a pdf from several years ago in front of me, but not the SW files, and I want to find them.
"On the days when I keep my gratitude higher than my expectations, well, I have really good days."

Ray Wylie Hubbard in his song "Mother Blues"
Frank_Oostendorp
Posts: 221
Joined: Tue Mar 09, 2021 7:25 am
Answers: 3
Location: Netherlands
x 184
x 229

Re: Partial File Path

Unread post by Frank_Oostendorp »

Glenn Schroeder wrote: Thu Apr 14, 2022 8:37 am I can't answer for @TTevolve, but I also have the file part in the margin of my Drawings. It's helpful when I have a pdf from several years ago in front of me, but not the SW files, and I want to find them.
Right. We use unique numbers for each part/assembly, drawings have the same file name as the items shown on it. So a search in file explorer will lead us to the documents. But we purchase items according our drawings all over the world, so we do not like to show our file paths. :roll:
User avatar
JSculley
Posts: 646
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 878

Re: Partial File Path

Unread post by JSculley »

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):
image.png
image.png (4.51 KiB) Viewed 1094 times
Here is the Main module:

Code: Select all

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:

Code: Select all

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.
TTevolve
Posts: 253
Joined: Wed Jan 05, 2022 10:15 am
Answers: 3
x 86
x 159

Re: Partial File Path

Unread post by TTevolve »

Like Glenn said above, we use PDF's to share our drawings with the shop, so it's a quick way to know where to look for models/parts/drawings when you created them a while ago and can't remember where you saved them. I know you can search by part number, but we have years of data so searches can take 2 or 3 minutes vs just going to the the folders in the path. I will try the macro JSculley put up.

It wasn't an issue until someone (an IT guy) decided to use really long folder names for the base folders and didn't consult anyone that it might be an issue. Thanks for the help guys!
Post Reply