Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Inventor API - C# - ComponentOccurences one level lower

christoph.steidle
Participant

Inventor API - C# - ComponentOccurences one level lower

christoph.steidle
Participant
Participant

Hello all,

I have a question regarding access via the INventor API.

I use VisualStudio Class Library with .NET 6.0 to access the API.

If I read here in the forum, you always access ComponentDefinition this way.

 

' Get the active assembly.
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.ActiveDocument

' Get the definition of the assembly.
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = oAsmDoc.ComponentDefinition

For me in VS, it would look like this.

 

var _inventor = (Inventor.Application)obj;
var _document = _inventor.ActiveDocument;
var _componentDefinition = _document.CompnentDefinition;

 

 

Unfortunately, this does not work for me so to access it I have to go one level higher which I do not understand and therefore does not work as intended?

 

 

var _inventor = (Inventor.Application)obj;
var _document = _inventor.ActiveDocument;

foreach (Document refDoc in _document.AllReferencedDocuments)
{   
if (refDoc.DocumentType == DocumentTypeEnum.kPartDocumentObject)
{
       var doc = (PartDocument)refDoc;
       var _doc.Add((doc )refDoc);
}

foreach (ComponentOccurrence occurence in _doc[i].ComponentDefinition.Occurrences)

 

I would be very grateful for your help or an explanation.

 

Greetings

0 Likes
Reply
Accepted solutions (1)
574 Views
3 Replies
Replies (3)

Ralf_Krieg
Advisor
Advisor

Hello

 

Just a typo?

var _componentDefinition = _document.CompnentDefinition;

vs.

var _componentDefinition = _document.ComponentDefinition;

R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes

christoph.steidle
Participant
Participant

Hello,

 

thanks for your answer but I don't exactly understand what you mean?

 

var _componentDefinition = _document.CompnentDefinition;
var _componentDefinition1 = _document.ComponentDefinition;
var _componentDefinition2 = _doc[0].ComponentDefinition;

 

As you could see in the picture only the third line is an accessible method?

image.png

0 Likes

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

Meant that in the line I posted, an "o" in Component was missing.

Anyway, your problem is, that _document is a generic document type. This includes drawings too and drawings don't have a ComponentDefinition. Cast _document as AssemblyDocument (if prechecked it is an assembly) and the error should disappear.

_doc[0] is a referenced document (part or subassembly) in your assembly. As all docs in _doc are casted as PartDocuments, you don't get an error of the missing Componentdefinition object. PartDocument always have a ComponentDefinition. But, a referenced document could also be an assembly, so this way the next error is just one step away.


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes