Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Properties Macro 3

Status
Not open for further replies.

ShaggyPE

Mechanical
Sep 8, 2003
1,127
US
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
 
Replies continue below

Recommended for you

What about using the built-in property "SW-File Name"?
 
That was my hope to somehow link my new custom property to that standard property.

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.
 
Maybe I not understanding but why not just:

(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?
 
Why not use the SW Task Scheduler. It is already set up so that you can add a property to the custom properties of every part in a directory.

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.
 
Thank you everyone so far.

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 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.
 
Tick, sorry for air code, appended as shown, good catch:

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
 
Oops, missed all assembly files too, try this:

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...
 
Thanks Alexit,
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:


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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top