I am trying to add the custom property “Finished Length” to a batch of files. Some files have the custom property already and some do not. Some of the files that have the custom property have a value for that custom property. I want to add “Finished Length” to all the parts that don’t already have it, but I don’t want to remove the value present in the files that already have “Finished Length”
The example below shows what I am trying to achieve. The top section is what we have currently have and the bottom section is what we are aiming for.
Part Name – Custom Property – Value
Part A – BLANK - BLANK
Part B – “Finished Length” - BLANK
Part C – “Finished Length” – 123
Part Name – Custom Property – Value
Part A – “Finished Length” - BLANK
Part B – “Finished Length” - BLANK
Part C – “Finished Length” – 123
I have found a couple of macros that do something similar but they all overwrite the existing values with a blank string. I know that if I use the Custom property tab builder and apply the form that I create to a part, it adds the custom property if it doesn’t exist but does not overwrite existing ones. I just need to do this on a mass scale. Hopefully I can replicate this functionality?
SolidworksApi macros
Have you taken a look at the #Task tool? The below screen is what I could find regarding batch adding a custom property.
Hello Alex,
I have tried #Task. Its an incredible tool and has done a lot of really useful things for us, but the macros for adding custom properties seem to overwrite exisiting ones with a blank value. Unless of course Im not using it right? Is there any syntax that I can use that would ignore properties that are already present?
I haven't actually used it for that yet so it's hard to say. The only other thing that I can think of is to write something with the API to access the properties of the file and check if the property exists before adding it. I think the Document Manager can be used to change attributes without opening the file in SW but I believe it requires a serial number that you can request through your VAR.
Again, I haven't used these tools for that specifically so there may be something I am missing.
You can use following codes and batch run it via #TASK.
<p>Option Explicit</p><p>Dim swApp As SldWorks.SldWorks</p><p>Dim swModel As SldWorks.ModelDoc2</p><p>Dim swCustProp As CustomPropertyManager</p><p>Dim ValOut As String</p><p>Dim ResolvedValOut As String</p><p>Dim wasResolved As Boolean</p><p>Dim resolved As Variant</p><p>Dim nErrors As Long</p><p>Dim nWarnings As Long</p><p></p><p>Sub main()</p><p></p><p>Set swApp = Application.SldWorks</p><p>Set swModel = swApp.ActiveDoc</p><p>Set swCustProp = swModel.Extension.CustomPropertyManager("")</p><p></p><p>swCustProp.Get5 "Finished Length", False, ValOut, ResolvedValOut, wasResolved</p><p></p><p>If ValOut = "" Then</p><p>swCustProp.Add3 "Finished Length", 30, "Some value here", 1</p><p>End If</p><p></p><p>swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings</p><p> </p><p>End Sub</p>Thanks so much Deppak,
But unfortunately that code isn't doing what I was wanting. There are 3 scenarios I need it to handle.
If a part does NOT have property "finished length" then to add it with no value
If a part has the property "finished length" and it has a value already, then do nothing and do not overwrite
If a part has the property "finished length" and the value is populated then do nothing and do not overwrite
...and do not to delete or change any other properties.
Luke.
Luke, sorry as this line is not correct (have fixed it in above post also)
It should be
Perfect! Thanks a lot Deepak. I have been looking for this soilution for a while now!
Just had to remove the text "Some value here" to keep the value blank.
Thnaks again.
i didn't understand the answer could you please clarify ?