AnsweredAssumed Answered

Retrieve component from part document

Question asked by Jana Stahn on Nov 15, 2016
Latest reply on Nov 19, 2016 by Ivana Kolin

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...