9 Replies Latest reply on Mar 16, 2016 1:41 PM by Don Michaels

    How to use swCustProp.Add ?

    Don Michaels

      I am trying to use macros to get, add, and delete custom properties. The code I have does not add a custom property. There is no error, it just doesn't add the custom property. The 'get' code does work.

      Here is the macro code:

       

      Option Explicit

      Dim swApp As SldWorks.SldWorks

      Dim swModel As ModelDoc2

      Dim swModelDocExt As ModelDocExtension

      Dim swCustProp As CustomPropertyManager

      Dim val As String

      Dim valout As String

      Dim bool As Boolean

      Dim swSelMgr            As SldWorks.SelectionMgr

      Dim swFeat              As SldWorks.Feature

      Dim swCustPropMgr       As SldWorks.CustomPropertyManager

      Dim nRetVal             As Long

      Dim vNameArr            As Variant

      Dim vName               As Variant

      Dim bRet                As Boolean

      Dim TestPropVar        As String

      Dim TestFieldName      As String

      Dim TestFieldValue     As String

       

      Sub main()

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

      Set swModelDocExt = swModel.Extension

      If swModel.GetType = swDocPART Then

          Set swCustProp = swModelDocExt.CustomPropertyManager("")

          'Get a Custom Property value

          bool = swCustProp.Get4("Part_Number", False, val, valout) 'This WORKS

           'Add a Custom Property

          TestPropVar = "TestVar"

          TestFieldName = "Test_Cust_Prop"

          TestFieldValue = "TestIT"

          bool = swCustProp.Add(TestFieldName, TestPropVar, TestFieldValue) 'This DOES NOT WORK

       

      End If 'is part

      End Sub

       

      I appreciate any suggestions.

      Don Michaels

      Belcan Engineering, Solon Ohio

        • Re: How to use swCustProp.Add ?
          Artem Taturevych

          The second parameter for this method should be a type of the field (integer). Something like the following:

          bool = swCustProp.Add(TestFieldName, swCustomInfoType_e.swCustomInfoText, TestFieldValue)

            • Re: How to use swCustProp.Add ?
              Don Michaels

              Thank you Artem, but it does not work. I have tried additional syntax, I can run them all at the same time and none of them add the custom property. There must be something more fundamental that is wrong. Here is a snippet of what I am trying:

               

              bool = swCustProp.Add(TestFieldName, TestPropVar, TestFieldValue) 'This DOES NOT WORK

              bool = swCustProp.Add(TestFieldName, swCustomInfoType_e.swCustomInfoText, TestFieldValue) 'This DOES NOT WORK

              bool = swCustProp.Add(TestFieldName, swCustomInfoType_e.swCustomInfoNumber, TestFieldValue) 'This DOES NOT WORK

              bool = swCustProp.Add3(TestFieldName, nRetVal, TestFieldValue, True) 'This DOES NOT WORK

              bool = swCustProp.Set(TestFieldName, TestFieldValue) 'This DOES NOT WORK

              swCustProp.Set2 "Test_Cust_Prop", TestFieldValue 'This DOES NOT WORK

               

              It appears that the second parameter needs to be a string, not an integer, so I tried both

              swCustomInfoType_e.swCustomInfoText and swCustomInfoType_e.swCustomInfoNumber

               

              • Re: How to use swCustProp.Add ?
                Don Michaels

                Artem, thank you very much for your help. Your advice was important to the solution. I needed this in addition to using add2 instead of add. I put my working code snippet in the reply to John.

                Thanks again,

                Don Michaels

              • Re: How to use swCustProp.Add ?
                John Alexander

                Looks like the latest method is Add2:

                 

                2012 SOLIDWORKS API Help - Add2 Method (ICustomPropertyManager)

                Option Explicit

                Sub main()

                    Dim swApp   As SldWorks.SldWorks

                    Dim swModel As SldWorks.ModelDoc2

                   

                    Dim dummyString As String

                    Dim outString   As String

                   

                    Dim lRet    As Long

                    Dim bRet    As Boolean

                   

                   

                    Set swApp = Application.SldWorks

                    Set swModel = swApp.ActiveDoc

                   

                    lRet = swModel.Extension.CustomPropertyManager("").Add2("NewFieldName", swCustomInfoType_e.swCustomInfoText, "This is a new field")

                    If lRet = 1 Then '1 if custom property is added

                         Debug.Print "Custom property is added"

                         bRet = swModel.Extension.CustomPropertyManager("").Get4("NewFieldName", False, outString, dummyString)

                         Debug.Print outString 'Should print "This is a new field"

                    ElseIf lRet = 0 Then '0 if custom property is not added

                         Debug.Print "Custom property is not added"

                    Else '-1 if the custom property already exists

                         Debug.Print "Custom property already exists"

                    End If

                End Sub

                 

                Running this once produces the output:

                     Custom property is added

                     This is a new field

                Running this a second time produces the output:

                     Custom property is not added

                Therefore, the returned long from Add2 doesn't appear to be correct (it will not warn you if the custom property exists already).

                 

                Let me know if this is helpful

                  • Re: How to use swCustProp.Add ?
                    Don Michaels

                    John, Thank you very much for your help. It appears that add2 was the key, (as well as swCustomInfoType_e.swCustomInfoText)  although add3 did work for a while. Add3 overwrites an existing value, it worked for a dozen tests, then while tweaking things it stopped working. So, Add2 is fine as long as I delete2 first. Here is the code that worked:

                     

                    Set swApp = Application.SldWorks
                    Set swModel = swApp.ActiveDoc
                    Set swModelDocExt = swModel.Extension
                    Set swCustProp = swModelDocExt.CustomPropertyManager("")

                    'Add, Replace, Get a Custom Property
                    sw_InfoText = swCustomInfoType_e.swCustomInfoText
                    TestFieldName = "Test Cust Prop"
                    TestFieldValue = "Test IT"

                    lRet = swCustProp.Delete2(TestFieldName) 'This DOES WORK
                    lRet = swCustProp.Add2(TestFieldName, sw_InfoText, TestFieldValue) 'This DOES WORK, but use Delete2 first to replace value
                    lRet = swCustProp.Add3(TestFieldName, sw_InfoText, TestFieldValue, True) 'This DID Work to overwrite values, but stopped working
                    bRet = swCustProp.Get4(TestFieldName, False, val, valout) 'This DOES WORK

                     

                     

                    Thanks again,

                    Don Michaels