Scan directory for SW files - Find/replace and create custom properties

Hi,

I want to add multi-language descriptions to my SW weldment profiles. I am looking for a macro to scan a folder and add custom properties to all the .SLDLFP.

For exemple :

The ANSI Inch / Tube Square folder :

The first file is TS1.5x1.5 x 0.125.SLDLFP

By default there is a custom properties named "Description" and value "TS1,5x1,5 x 0,125"

I would love to create two more custom properties called Description_french and Description_english. I would just use a common Find / Replace windows function and replace the "TS" in the original Description properties by "Tube carré "+ "1,5x1,5 x 0,125" (leave everything already in the original description exept for "TS") for Description_french and "Tube Square "+"1,5x1,5 x 0,125" for the Description_english.

Origial TS1.5x1.5 x 0.125.SLDLFP file :

Custom properties                Value

Description                             TS1.5x1.5 x 0.125

New file :

Custom properties                Value

Description                             TS1.5x1.5 x 0.125

Description_french                 Tube carré 1.5x1.5 x 0.125

Description_english               Tube square 1.5x1.5 x 0.125

Can you help me ?

Thanks !

SolidworksApi macros
Answers 5
KR
KR 2012-05-10 Best Answer

Etienne,

This can certainly be done.  One option might be to use the Document Manager, which allows you to modify custom properties in a document without actually opening the document in SolidWorks.  However I am not certain that this works with weldment files.

Your other option would be to open each part in a folder in SolidWorks, modify the custom properties, save the part, close it, etc.

To loop through files in a folder you will need to use the Dir() function (assuming you are using VBA).  You can also look up code samples in the API Help.  To open documents, use ISldWorks::OpenDoc6.  To save documents, use ISldWorks::Save3.  To work with custom properties use the ICustomPropertyManager interface.  To replace data in string, you can use the built-in VBA Replace() function.

You can google for more info on these methods, use the API Help (Help-->API Help), or use the videos at my web site.  If you decide to use my videos, Lesson 1.10 covers the Dir() function, Lesson 3.1 covers opening and saving documents, Lesson 3.3 covers custom properties, and Lesson 1.6 covers built-in VBA functions like the ones you need.  Also, if you simply want to learn how to use the API Help, check out Lesson 2.2.

Keith

Video Tutorials for the SolidWorks API

SF
SF 2012-05-14

Ok I started looking around and gathering different macros to combine.

First of all, I need to scan the files in the desired folder and open them. I'm looking for something really basic. I would specify the directory and file type directly in the script.

I took Dan's script from https://forum.solidworks.com/message/97101#97101 and I changed the extension so it searches for .SLDLFP files.

Dim Fold As String

Dim FileType As String

Sub main()

'Folder Name. Change to folder that has SolidWorks files

Fold = "C:\test"

FileType = "*.sldlfp*"

'This first time prints all files

GetFiles

Debug.Print "*******************"

'Prints all files that end with prt

FileType = "*.sldlfp"

GetFiles

End Sub

Sub GetFiles()

Dim FileName As String

FileName = Dir(Fold & FileType)

Do While FileName <> ""

Debug.Print FileName

FileName = Dir

Loop

End Sub

The Dir() function does not seem to work as FileName never changes for the files names in the directory C:\test

Can you help me out with that ?

DG
DG 2012-05-14

Use

Fold = "C:\test\"

instead of

Fold = "C:\test"

SF
SF 2012-05-14

Hi Etienne.

To read from the custom properties, just enter an empty "" configuration name

SF
SF 2012-05-14

Thank you all for your help (Andries, Deepak and Keith). My Macro is now fully operational. For those interested, here it is :

Option Explicit

Dim swApp As SldWorks.SldWorks

Dim swModel As ModelDoc2

Dim swModelDocExt As ModelDocExtension

Dim swCustProp As CustomPropertyManager

Dim val As String

Dim val_francais As String

Dim val_anglais As String

Dim valout As String

Dim bool As Boolean

Dim sFileName       As String

Dim silent          As Boolean

Dim Path            As String

Dim swConfig        As String

Dim nErrors         As Long

Dim nWarnings       As Long

Dim Fold As String

Dim FileName As String

Dim FileType As String

Dim Nom_Remplacer As String

Dim Nom_Francais As String

Dim Nom_Anglais As String

Sub main()

Set swApp = Application.SldWorks

Fold = "C:\test\"               'Directory in which the weldment profiles are (in this exemple it is the Tube Square (ANSI) directory)

FileType = "*.sldlfp*"          'File extension of weldment profiles

Nom_Remplacer = "TS"            'Expression to replace in the defaut Description of the profile (From exemple TS1.5x1.5 x 0.125.SLDLFP)

Nom_Francais = "Tube carré "   'Replace it with.. in french...

Nom_Anglais = "Tube square "         'Replace it with.. in english...

FileName = Dir(Fold & FileType)

Do While FileName <> ""

Debug.Print FileName

    Set swModel = swApp.OpenDoc6(Fold + FileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)

    Set swModel = swApp.ActiveDoc

    Set swModelDocExt = swModel.Extension

    Set swCustProp = swModelDocExt.CustomPropertyManager("")

    bool = swCustProp.Get4("Description", False, val, valout)

    val_francais = Replace(val, Nom_Remplacer, Nom_Francais)

    val_anglais = Replace(val, Nom_Remplacer, Nom_Anglais)

    swModel.AddCustomInfo3 "", "DESCRIPTION_FRANCAIS", swCustomInfoText, val_francais

    swModel.AddCustomInfo3 "", "DESCRIPTION_ANGLAIS", swCustomInfoText, val_anglais

    swModel.ForceRebuild3 False

    swModel.Save2 silent

    swApp.CloseDoc swModel.GetTitle

    Set swModel = Nothing

    FileName = Dir

Loop

End Sub