×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Custom Properties Macro
3

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

RE: Custom Properties Macro

What about using the built-in property "SW-File Name"?

RE: Custom Properties Macro

(OP)
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.

RE: Custom Properties Macro

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?

RE: Custom Properties Macro

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.

RE: Custom Properties Macro

(OP)
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 http://webpages.charter.net/mkikstra/SolidWorks.html
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

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

RE: Custom Properties Macro

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...

RE: Custom Properties Macro

(OP)
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:

http://www.solidworks.com/pages/services/subscription/modellibrary/listmodels.html?C1=API&amp;C2=Miscellaneous

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources