2 Replies Latest reply on May 29, 2015 3:13 PM by Michael Ferraro

    Check if custom property exists and if it doesnt add it

    Michael Ferraro

      Hello All,

       

      I am looking to write a macro to check if my assembly has 2 custom properties. I dont care what the value is, I just want to see if it has that custom property or not. The rest of the macro fills the property out however if the property doesn't exist it obviously cant fill it out If the assembly does not have one or both of them then I want to add each of the custom properties.

       

      I am looking to make the loop and then just duplicate the loop for the other custom property. I dont need anything that is fancy that will do both at the same time. The two custom properties are called "Published As" and "Revision".

       

      I am confusing myself a little bit as to where I should do this in the code/loop.

      ~~~~~~~~~~~~~~~

      Dim swApp As Object
      Dim swModel As SldWorks.ModelDoc2
      Dim swExt As SldWorks.ModelDocExtension
      Dim swCustPropMgr As SldWorks.CustomPropertyManager
      Dim PublishedAs As String
      Dim PropertyValue As String
      Dim ResolvedPropertyValue As String
      Dim ReturnValue As Integer
      Dim CustomInfoName As Variant
      Dim CustomInfoNameArray As Variant

       

      Sub main()

      Set swApp = Application.SldWorks
      Set swModel = swApp.ActiveDoc
      Set swExt = swModel.Extension
      Set swCustPropMgr = swExt.CustomPropertyManager("")

      CustomInfoNameArray = swModel.GetCustomInfoNames2("")
      For Each CustomInfoName In CustomInfoNameArray
          If CustomInfoName <> "Published As" Then
          End If
      Next

      End Sub

      ~~~~~~~~~~~~~~~~

       

      So if I put the add command inside the if statement wouldn't that not work? it needs to run through all the properties first and see if one is called "Published As" and if none are then create the property.

       

      Thanks,

       

      Pencil

        • Re: Check if custom property exists and if it doesnt add it
          Keith Rice

          If you're using SolidWorks 2014 then you can simply use ICustomPropertyManager::Add3 which, in the last argument, gives these options:

           

          swCustomPropertyDeleteAndAdd

          swCustomPropertyOnlyIfNew

          swCustomPropertyReplaceValue

           

          Hence you can choose to add the property only if it doesn't exist, in which case if it already existed, those original values will be kept. Or you can choose to delete and add, which means that post-running the property will have whatever new value you desired. You might play around with them and see what works best for you.

           

          Const PROP_NAME_1 As String = "Published As"
          Const PROP_NAME_2 As String = "Revision"
          Const PROP_VAL_1 As String = Empty
          Const PROP_VAL_2 As String = Empty
          
          Sub main()
              Dim swApp As SldWorks.SldWorks
              Dim swModel As SldWorks.ModelDoc2
              Dim swCustPropMgr As SldWorks.CustomPropertyManager
              
              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)
              swCustPropMgr.Add3 PROP_NAME_1, swCustomInfoText, PROP_VAL_1, swCustomPropertyOnlyIfNew
              swCustPropMgr.Add3 PROP_NAME_2, swCustomInfoText, PROP_VAL_2, swCustomPropertyOnlyIfNew
          End Sub
          

           

          You can mimic all of this in 2013 and earlier you just need to use ICustomPropertyManager::GetX, AddX, and/or SetX in the right order depending on what you want.

           

          Keith

          SolidWorks API Training