4 Replies Latest reply on Nov 19, 2016 1:58 PM by Ivana Kolin

    Retrieve component from part document

    Jana Stahn

      I have written an add-in that does stuff with sketches. Currently it only works for assemblies, and much of the code is written such that it is required to know the owning component of each sketch. For assembly documents I know how to do this: select the sketch and use GetSelectedObjectsComponent or cast a sketch as IEntity and call GetComponent.


      But now I want to extend the add-in to work on part documents, too. The problem is that in order to avoid rewriting a hell lot of code, I need to retrieve an IComponent2 from a part document (but it must not be null!). Is this possible?


      As far as I tested, an IFeature retrieved by IPartDoc.FirstFeature() or IFeature.NextFeature() always returns null when cast as IEntity and called IEntity.GetComponent(). Selecting some face/edge/vertex and calling GetSelectedObjectsComponent returns null as well. This does not help...


      If I can't retrieve an IComponent2 from the IPartDoc, is there any other way to get an IComponent2? I would be happy with any kind of dummy component. The only requirement is that the component is not null. This is because I use a lot of C# Dictionaries with IComponent2 as keys, and the dictionaries do not allow null keys.


      Or is it maybe possible to create a new IComponent2 via API (without changing the ModelDoc/PartDoc)?

      I already tried this (in C#):

           Component2 foo = new Component2();

      But I got a COMException ("class not registered")...


      As a last resort, I could create my own class implementing the IComponent2 interface. But I'd like to know if you have any better ideas before I do that...

        • Re: Retrieve component from part document
          Ivana Kolin
          Sub main()
              Dim swApp                       As SldWorks.SldWorks
              Dim swModel                     As SldWorks.ModelDoc2
              Dim swConfMgr                   As SldWorks.ConfigurationManager
              Dim swConf                      As SldWorks.Configuration
              Dim swRootComp                  As SldWorks.Component2
              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swConfMgr = swModel.ConfigurationManager
              Set swConf = swConfMgr.ActiveConfiguration
              Set swRootComp = swConf.GetRootComponent3(True)
          End Sub
          • Re: Retrieve component from part document
            Nilesh Patel

            Hi Jana,


            The reason why IEntity.GetComponent() return NULL because it works for an Assembly only not for a Part. Below is the screenshot of API help remark for IEntity.GetComponent().

            One way you can get the IComponent2 from IPartDoc is:

            1. Create a dummy assembly using ISldWorks.NewDocument method. You may not have to save the assembly.

            2. Use IAssemblyDoc.AddComponent5 method to add your part into the assembly. This method automatically returns IComponent2 object.

            3. Destroy or not save the dummy assembly.


            There may be a better solution but I am sure it will work for you now.