2 Replies Latest reply on Apr 10, 2014 10:17 PM by Jeroen Janssen Andeweg

    Change custom property of a part in an assembly

    Jeroen Janssen Andeweg

      This challenge is going on for days now and I can't get it the way I would like to see it.


      I have an assembly. In a 'For - Next' I get the 'Childeren' as Component2. They can be parts or subassemblies.

      I can determine what type of document the 'Child' is.


      In these documents I have Custom Properties that I would like to change to a new value.


      And this is where I get stuck. I have tried different things but am unsuccessful in 1 thing; I have to open the file to change the value of the custom properties and then close it.

      Having an assembly of about 1000 parts makes it a disco on my screen and that is not what I want (I have past the disco stage for many years now).


      I have tried my logic (which did not work obviously)

      With Component2 I am unable to find something that allows me to change the custom property so I have to go to CustomPropertyManager


      Dim FullFileNameofChild as String

      FullFileNameofChild = "Child'.GetPathName

      Dim MyActiveDoc as ModelDoc2

      MyActiveDoc = swApp.ActivateDoc2(FullFileNameofChild, (Silent) True, (Error), DOC_ERROR (as Long))


      Dim MyCustProp as CustomPropertyManager

      MyCustProp = MyActiveDoc.Extension.CustomPropertyManager("") <<<<< this is where it goes wrong.


      I do not have a clue why. In VB.NET I can select it and no errors occur untill debugging. I am thinking that ActivateDoc2 is not doing what I think it should do.


      Can someone point me in the right direction please.


      Much appreciated.

        • Re: Change custom property of a part in an assembly
          Artem Taturevych

          You can modify the custom properties of assembly components while they are invisible (not opened it their own windows) as long as the component has a pointer to IModelDoc2. There are only several situations where the pointer is NULL:


          • Component is Suppressed or Lightweight
          • Component is missing the files (in fact it is also suppressed)


          So check this code:

          Dim swApp As SldWorks.SldWorks

          Dim swAssy As SldWorks.AssemblyDoc


          Sub main()


              Set swApp = Application.SldWorks


              Set swAssy = swApp.ActiveDoc


              Dim swComp As SldWorks.Component2

              Set swComp = swAssy.GetComponents(False)(0)


              Dim swRefModel As SldWorks.ModelDoc2

              Set swRefModel = swComp.GetModelDoc2


              If Not swRefModel Is Nothing Then


                  Dim swCustPrpMgr As SldWorks.CustomPropertyManager

                  Set swCustPrpMgr = swRefModel.Extension.CustomPropertyManager("")

          swCustPrpMgr.Add2 "Test Field", swCustomInfoType_e.swCustomInfoText, "Test"

          swCustPrpMgr.Set2 "Test Field", "Test"



                  'TODO: Open Part or Resolve Component

              End If


          End Sub


          Regards, Artem Taturevych | Snr. Developer | IC3D ANZ


          IC3DSteel – New Steel Solution for SolidWorks

          translationXpert – SolidWorks files language translator

          LinkedIn - SolidWorks API Group