4 Replies Latest reply on Jan 26, 2017 10:15 AM by Jana Stahn

    check if Component2 is invalid

    Jana Stahn

      I noticed that it is possible to retrieve a Component2 and make it invalid.

      I will shortly explain how I did it.


      This is my assembly:



      1. I retrieved all components via AssemblyDoc.GetComponents(false). This gave me two components: the top level component and its child component.

      2. I replaced the top level component by a part file. Thus, after the replace, the top level component does not have a child any more.


      I still have the child component, even though it has no counterpart in the feature tree any more. I call it "invalid" for the purpose of this question. (I don't know if this is the official nomenclature.)


      My question is: How do I find out if a given Component2 is invalid?


      I noticed that before replacing, the component in question had a Name2: "subLvlComp-1".

      After replacing, its Name2 is "". So the Name2 is an indicator. But I don't know if this always works.

      Does anyone have experience with this? Is there a method that was designed to return the validity state?


      I attached the assembly and part files as well as a macro that does the component retrieving, replacing and writing the names to the console.

      If you want to try, just open assembly.SLDASM and run the macro (with output console visible).

        • Re: check if Component2 is invalid
          Amen Allah Jlili

          Only the API support team can answer that Jana!
          To me, it looks like the .Name swallows any exceptions and returns "" if an exceptions occurs. Evidently, all components must have a name so you can assume if that the return is an empty string that there's something wrong that your component object.


          Off the top of my head, best approach is to construct a tree class of SelectionDataString strings and component2 objects that includes a method to validate. This will preserve the structure of the components tree even if after you replace a component. You can try to select the said component with its SelectionDataString (from the tree class, not from the object), if the selection's return is false then something's wrong. You can then delete the component2 object and refresh the tree.

          • Re: check if Component2 is invalid
            Raghvendra Bhargava

            Hi Jana,

            you can use AssemblyDoc.GetComponents(false) after replacing the component. It will give you latest info for components.

            Or could you please elaborate the application workflow?

            • Re: check if Component2 is invalid
              Nilesh Patel

              Hi Jana,


              'subLvlComp-1' is the second element of the component array 'IAssemblyDoc.GetComponents(false)' retrieved before replacing the top level component 'topLvlComp-1'  but  after replacement of the top level component, the child component is not in the final assembly and this is why .Name2 of the component 'subLvlComp-1' is " " because it cannot access that component. It should throw an error when you try to use IComponent2.Name2 property, but some reason it doesn't.


              You need to use IAssemblyDoc.GetComponents(false) method again as explained by Raghvendra Bhargava  to get the latest array of components, once you delete, add or replace any component. You could use 'GOTO' to jump back to 'swComps = swAssy.GetComponents(False)' in your macro every time one of the these happens to make sure that you get the latest component array.


              Amen Allah Jlili has mentioned one work around. Other option if to use 'IComponent2.Select4'. It will return 'False' if the component is not selected.


              Hope this helps.

              • Re: check if Component2 is invalid
                Jana Stahn

                Component2.GetID() returns -1 if the component is invalid. But it also returns -1 for the root component. So I guess I will just stick with the Name2 == "" check (the root component has a name other than "").