Inventor model tree structure

Inventor model tree structure

dhanshri_gavas
Contributor Contributor
239 Views
1 Reply
Message 1 of 2

Inventor model tree structure

dhanshri_gavas
Contributor
Contributor

Hi,
I wanted to create a multilevel tree structure for the model using an API. For creating the structure of the subassembly, I added the default template. and in those added parts. but in that subassembly, I wanted assembly again, but I am not able to insert the same template again, getting the error.

dhanshri_gavas_0-1717129843912.png

Document _document = inventorApp.ActiveDocument;
            AssemblyDocument pDoc = _document as AssemblyDocument;
            AssemblyComponentDefinition oPartCompDef = pDoc.ComponentDefinition;
            inventorApp.PartOptions.EnableConstructionEnvironment = false;
            pDoc.ObjectVisibility.Sketches = true;
            ObjectCollection oBodies = inventorApp.TransientObjects.CreateObjectCollection();
 
 
            TransientGeometry transientGeometry = inventorApp.TransientGeometry;
            Matrix matrix = transientGeometry.CreateMatrix();
 
            ComponentOccurrence derivedAssemblyOccurrence = oPartCompDef.Occurrences.Add("D:\\DemoImp\\DemoImp\\Standard.iam", matrix);
            derivedAssemblyOccurrence.Name = "SubAssembly1";
 
            ComponentOccurrence derivedAssemblyOccurrence_new1 = derivedAssemblyOccurrence.Definition.Occurrences.Add("D:\\DemoImp\\DemoImp\\Standard.iam", matrix);
            derivedAssemblyOccurrence_new1.Name = "SubAssembly2";
 
            ComponentOccurrence derivedAssemblyOccurrence_new2 = derivedAssemblyOccurrence_new1.Definition.Occurrences.Add("D:\\DemoImp\\DemoImp\\Standard.iam", matrix);
            derivedAssemblyOccurrence_new2.Name = "SubAssembly3";

 

 

0 Likes
240 Views
1 Reply
Reply (1)
Message 2 of 2

Michael.Navara
Advisor
Advisor

The issue is in bad usage of Occurrences.Add method. The first argument of this method requires file name of existing file, not a template. In your case you try to insert the document to itself.

You need to create document from template first and then you can place the newly created document to the assembly.

 

Try this modified code

Document _document = inventorApp.ActiveDocument;
AssemblyDocument asmDoc = _document as AssemblyDocument;
AssemblyComponentDefinition asmCompDef = asmDoc.ComponentDefinition;
inventorApp.PartOptions.EnableConstructionEnvironment = false;
asmDoc.ObjectVisibility.Sketches = true;
ObjectCollection oBodies = inventorApp.TransientObjects.CreateObjectCollection();


TransientGeometry transientGeometry = inventorApp.TransientGeometry;
Matrix matrix = transientGeometry.CreateMatrix();

var asmTemplate = "D:\\DemoImp\\DemoImp\\Standard.iam";
var subAssy1 = inventorApp.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, asmTemplate, false) as AssemblyDocument;
ComponentOccurrence derivedAssemblyOccurrence = asmCompDef.Occurrences.AddByComponentDefinition(subAssy1.ComponentDefinition as ComponentDefinition, matrix);
derivedAssemblyOccurrence.Name = "SubAssembly1";

var subAssy2 = inventorApp.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, asmTemplate, false) as AssemblyDocument;
ComponentOccurrence derivedAssemblyOccurrence_new1 = derivedAssemblyOccurrence.Definition.Occurrences.AddByComponentDefinition(subAssy2.ComponentDefinition as ComponentDefinition, matrix);
derivedAssemblyOccurrence_new1.Name = "SubAssembly2";

var subAssy3 = inventorApp.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, asmTemplate, false) as AssemblyDocument;
ComponentOccurrence derivedAssemblyOccurrence_new2 = derivedAssemblyOccurrence_new1.Definition.Occurrences.AddByComponentDefinition(subAssy3.ComponentDefinition as ComponentDefinition, matrix);
derivedAssemblyOccurrence_new2.Name = "SubAssembly3";
0 Likes