Custom Properties Macro
Custom Properties Macro
(OP)
First off, I am not familiar with API or macros.
Getting that off my chest;
Ultimately I would like a macro that will add a custom property to all the files in a directory (the directory will only have parts and assemblies). I would also like the macro to supply the value for that property. I want the value to be the model file name. This task seems relatively simple from a programming perspective, and probably is in the hands of someone familiar with macros in general. Unfortunately I am not.
I have searched previous threads and the web. The macros that seem to offer a good starting point are FixProperties and PropertyEditorSpec from Lenny Kikstra.
I haven't had any success getting the FixProperties macro to work, however from its description, it will do the global creation of the property. Has anyone used/modified this macro?
Once I get the FixProperties macro to work, does anyone have any input on how to link the model file name to the property. It cannot be done solely through the solidworks interface. I hoped it would be listed in the "link to value" drop down, but that is only for mass properties.
Thanks in advance,
Shaggy
Getting that off my chest;
Ultimately I would like a macro that will add a custom property to all the files in a directory (the directory will only have parts and assemblies). I would also like the macro to supply the value for that property. I want the value to be the model file name. This task seems relatively simple from a programming perspective, and probably is in the hands of someone familiar with macros in general. Unfortunately I am not.
I have searched previous threads and the web. The macros that seem to offer a good starting point are FixProperties and PropertyEditorSpec from Lenny Kikstra.
I haven't had any success getting the FixProperties macro to work, however from its description, it will do the global creation of the property. Has anyone used/modified this macro?
Once I get the FixProperties macro to work, does anyone have any input on how to link the model file name to the property. It cannot be done solely through the solidworks interface. I hoped it would be listed in the "link to value" drop down, but that is only for mass properties.
Thanks in advance,
Shaggy






RE: Custom Properties Macro
RE: Custom Properties Macro
To help everyone understand my problem, here is the situation I am dealing with. We are in the process of mirgating a lot of SolidWorks models into our new pdm system. For this mirgation to be aoutmatic, the pdm system we are using (Windchill by PTC... not my recommendation, but the decision was made prior to my employment at the company) requires a custom property called SP:PART_NR to be created. The value of this property needs to match the number assigned to the PDM part entry. It just so happens that we already name our files with that number. With that property correctly filled, all we have to do is check in the model files and they will be properly linked to their respective entries in pdm.
So back to TheTick's recommendation, I am not able to get the pdm system to look at the SW-FileName property. It only references the SP:PART_NR property. So I need to add this property to 1000+ files and give the value of the file name to that property.
Thanks again to everyone.
RE: Custom Properties Macro
(CAUTION AIR CODE)
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim longstatus As Long
Dim longwarnings As Long
Dim Part As Object
Dim strFpath As String
Dim strFName As String
Set swApp = Application.SldWorks
Do Until strFName = ""
Set Part = swApp.OpenDoc6(strFpath & strFName, swDocDRAWING, swOpenDocOptions_Silent, "", longstatus, longwarnings)
Set Part = swApp.ActivateDoc2(strFName, False, longstatus)
Set swModel = swApp.ActiveDoc
swModel.CustomInfo2("", "SP:PART_NR") = $PRP:"SW-File Name"
Part.Save2 False
Set Part = Nothing
swApp.CloseDoc strFName
strFName = Dir
Loop
End Sub
This doesn't work?
RE: Custom Properties Macro
To get access to a list of files in a directory, you need the Microsoft Scripting object.
See "Copy Document and its Dependencies Example (VB)" in SW API help.
See also <h
RE: Custom Properties Macro
Just create a property called "Name" or whatever you want it to be and dump the value $PRP:"SW-File Name" into it.
It's a little slow as SW will be opened and the change will be made for every document. But you can schedule it to run while you are sleeping.
RE: Custom Properties Macro
I just want to reiterate that I am a total novice when it comes to macros and API.
alexit: I attempted to run the macro you listed above. I got an error message : <macro_name> has wrong format and cannot be converted to VBA macro file.
More than likely I did something wrong. I created a new macro and deleted the "stuff" that automatically appears in the code. I pasted your code in place and saved the file. When I attempted to run the macro, I got the message.
TheTick: I haven't had the opportunity to investigate your provided link, but I will shortly.
jmongan: Unfortunately I am using SW 2004. So I don't have access to task scheduler. As you (and alexit) have mentioned, assigning the value of $prp:"sw-file name" will work. That was my original plan, however when I tested it in the custom property I didn't realize that I needed the $prp:.
I have had a little interaction with Leonard Kikstra ht
and it appears his fixproperties macro will be a good base for the macro that I need to create. His macro will automatically create the custom property. I just need to modify it to add the value of the property as $prp:"sw-file name". I believe I can use a portion of the code that alexit provided. Is that correct?
Thanks again everyone for the help.
RE: Custom Properties Macro
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim longstatus As Long
Dim longwarnings As Long
Dim Part As Object
Dim strFpath As String
Dim strFName As String
strFpathPrt = "c:\temp\" 'Full path to parts here
strFNamePrt = Dir(strFpathPrt & "*.sldprt")
Set swApp = Application.SldWorks
Do Until strFName = ""
Set Part = swApp.OpenDoc6(strFpath & strFName, swDocDRAWING, swOpenDocOptions_Silent, "", longstatus, longwarnings)
Set Part = swApp.ActivateDoc2(strFName, False, longstatus)
Set swModel = swApp.ActiveDoc
swModel.CustomInfo2("", "SP:PART_NR") = $PRP:"SW-File Name"
Part.Save2 False
Set Part = Nothing
swApp.CloseDoc strFName
strFName = Dir
Loop
End Sub
Shaggy, is not VB, but SW macro code. Make new macro using Tools/Macro/New in blank SW window (no files open) then cut paste from here. Make sure the "automatic" entries from "New" are erase first.
Good luck
RE: Custom Properties Macro
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim longstatus As Long
Dim longwarnings As Long
Dim Part As Object
Dim strFpath As String
Dim strFName As String
strFpath = "c:\temp\" 'Full path to parts here
strFName = Dir(strFpath & "*.sldprt")
Set swApp = Application.SldWorks
Do Until strFName = ""
Set Part = swApp.OpenDoc6(strFpath & strFName, swDocPART, swOpenDocOptions_Silent, "", longstatus, longwarnings)
Set Part = swApp.ActivateDoc2(strFName, False, longstatus)
Set swModel = swApp.ActiveDoc
swModel.CustomInfo2("", "SP:PART_NR") = $PRP:"SW-File Name"
Part.Save2 False
Set Part = Nothing
swApp.CloseDoc strFName
strFName = Dir
Loop
strFName = Dir(strFpath & "*.sldasm")
Set swApp = Application.SldWorks
Do Until strFName = ""
Set Part = swApp.OpenDoc6(strFpath & strFName, swDocASSEMBLY, swOpenDocOptions_Silent, "", longstatus, longwarnings)
Set Part = swApp.ActivateDoc2(strFName, False, longstatus)
Set swModel = swApp.ActiveDoc
swModel.CustomInfo2("", "SP:PART_NR") = $PRP:"SW-File Name"
Part.SaveAs2 False
Set Part = Nothing
swApp.CloseDoc strFName
strFName = Dir
Loop
End Sub
More luck...
RE: Custom Properties Macro
I am still getting the error message that I mentioned in the earlier post. Within Solidworks, I go to tools>Macro>new and delete the "automatic" stuff. I past in your code. When I run the macro (with no SW files open)I get an error that says "...has wrong format and cannot be converted to VBA macro file". I am using SW 2004 if that may be of use.
Also, I did some searching on the Solidworks website and found an exe that seems to do just what I want. The exe is called ChangeCustomProperties11.exe and can be found at:
http://www
I am still interested in the method that alexit has described. Also, for the sake of learning, I would like to find out why I am unable to run alexit's macro.
Thanks agin for the help.