How do I create an if statement macro that can add a new custom property?

How do I create an if statement macro that would take my custom property material and add a new custom property line named R/M with a material code.

Ex:

If material =16 CRS

Then R/M = 010132000

I would like the property R/M to be added by the Macro.

SolidworksApi macros
Answers 15
DD
DD 2016-09-12 Best Answer

​, I don't have a macro answer for you, but this would be very easy to do in Excel.  You could make the configuration controlled by a Design Table and then do your if statement there.

LL
LL 2016-09-12

Hey Dennis thanks for the timely response

How would configurations work with EPDM?

Do you have an example?

CC
CC 2016-09-12
<p>Dim cpm As SldWorks.CustomPropertyManager</p><p>    Set cpm = swModelDoc.Extension.CustomPropertyManager("")</p><p>If cpm.Get("material") = "16 crs"  Then        </p><p>        cpm.Add2 "rm", swCustomInfoText, "1234"</p><p>End If</p>
LL
LL 2016-09-12

When I debug the code I get an error at the (Set cpm = swModelDoc.Extension.CustomPropertyManager(""))

Do you know what I am doing wrong?

comment imagecomment image
CS
CS 2016-09-12

If you're using ePDM you could do a list with alias and then populate the data card variables with $token in the input formula.

CC
CC 2016-09-12

you need to declare (set) the active document

<p>Set swApp = Application.SldWorks</p><p>    Set swModelDoc = swApp.ActiveDoc</p>
LL
LL 2016-09-12

Thanks so much for all the help. It is still getting hung up in the same spot and this is exactly what I have.

I'm not sure what is going on.

comment imagecomment image
DG
DG 2016-09-12

Here are the codes

<p>Option Explicit</p><p>Sub main()</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></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 "Material", False, ValOut, ResolvedValOut, wasResolved</p><p>If ResolvedValOut = "16 CRS" Then</p><p>    swCustProp.Add3 "R/M", swCustomInfoText, "010132000", 1</p><p>End If</p><p></p><p>End Sub</p>
CC
CC 2016-09-12

I just tested and it works - see the full macro

<p>Dim swApp As Object</p><p>Dim swModelDoc As SldWorks.ModelDoc2</p><p>Sub main()</p><p></p><p></p><p>Set swApp = Application.SldWorks</p><p>Set swModelDoc = swApp.ActiveDoc</p><p>Dim cpm As SldWorks.CustomPropertyManager</p><p>    Set cpm = swModelDoc.Extension.CustomPropertyManager("")</p><p>If cpm.Get("Revision") = "B-01+" Then</p><p>        cpm.Add2 "Test", swCustomInfoText, "1234"</p><p>End If</p><p>End Sub</p>
CC
CC 2016-09-12

Deepak

You're quick !

DG
DG 2016-09-12

I've updated the codes removing Obsolete variable and added updated one.

DG
DG 2016-09-12

These codes may be fail for material as material is set to part material and your codes may not get the resolved values.

CC
CC 2016-09-12

cool - Thx

LL
LL 2016-09-12

it works thank you so much do you think i could use this macro on many different parts with different materials? so more if then statments in the same macro?

DG
DG 2016-09-12

Yes. You can use something like this (changes as required, just added random values)

<p>If ResolvedValOut = "16 CRS" Then</p><p>    swCustProp.Add3 "R/M", swCustomInfoText, "010132000", 1</p><p>ElseIf ResolvedValOut = "17 CRS" Then</p><p>    swCustProp.Add3 "R/M", swCustomInfoText, "010133000", 1</p><p>End If</p>