Placing an ipt file in an iam file with Inventor api

hayattangercekler2
Advocate
Advocate

Placing an ipt file in an iam file with Inventor api

hayattangercekler2
Advocate
Advocate

I want to place my sample ipt file at the point in the coordinates I want in an iam file that I open with the Inventor api. My iam file is opening but I am getting this error when trying to add my ipt file. How can I fix this?

 

 

 

 

 Application oApp = _application;
            Application application = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
            string fullpath = "C:\\Users\\user\\Desktop\\part1.iam";


            var prt = application.Documents.Open(fullpath);
            prt.UnitsOfMeasure.LengthUnits = UnitsTypeEnum.kMillimeterLengthUnits;
            PartDocument prt2 = (PartDocument)application.Documents.Add(DocumentTypeEnum.kPartDocumentObject, application.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject), true);
            PartComponentDefinition cdef = prt2.ComponentDefinition;
            // Set a reference to the assembly component definition.


            // Set a reference to the transient geometry object.
            TransientGeometry oTG = oApp.TransientGeometry;

            // Create a matrix.  A new matrix is initialized with an identity matrix.
            Matrix oMatrix = oTG.CreateMatrix();
            // Set a reference to the transient geometry object.

            // Create a matrix. A new matrix is initialized with an identity matrix.

            oMatrix.SetTranslation(oTG.CreateVector(30, 0, 0));
            // Add the first occurrence.
            ComponentOccurrence oOcc1;
            oOcc1 = cdef.Occurrences.Add(@"C:\Users\user\Desktop\door.ipt", oMatrix);

 

 

 

Error : "System.NotImplementedException: 'Unrealized (Exception from HRESULT: 0x80004001 (E_NOTIMPL))'"

 

0 Likes
Reply
Accepted solutions (1)
335 Views
4 Replies
Replies (4)

Zach.Stauffer
Advocate
Advocate

You're trying to place a part inside of a part. I think this line is wrong:

PartComponentDefinition cdef = prt2.ComponentDefinition;
//should be:
PartComponentDefinition cdef = prt.ComponentDefinition;

 

Also I would suggest you use better variable names, you are loading your assembly into a variable called "prt" instead of "mainAssembly" or something similar. I assume you're using Visual Studio to program C#, the auto complete handles the typing of long names so it's much better to use a variable name like "assemblyComponentDef" than "cdef", etc. etc. And the old hungarian notation way of adding an "o" to variable names is outdated and can probably be phased out of future code.

 

I also don't think you need "prt2", unless you use it further down in the code that isn't shown.

 

Similarly, you define oApp and then immediately define application, but you already have _application defined somewhere and can just use that, no need to make new variables here. Just inefficient code that will lead to more errors in the future.

0 Likes

hayattangercekler2
Advocate
Advocate

This is how I fixed my code. In this case "application.ActiveDocument.ComponentDefinition" cannot be found, but that's how it was in most of the sample codes. I also get an object assignment error when I try to do this from the "PartDocument" interface. How can I do that ?

            Application application = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");

            string fullpath = "C:\\Users\\user\\Desktop\\part1.iam";

            var documentOpen =application.Documents.Open(fullpath, true);

            documentOpen.UnitsOfMeasure.LengthUnits = UnitsTypeEnum.kMillimeterLengthUnits;

            PartDocument partDocument = documentOpen as PartDocument;
            

            var componentDefinition = application.ActiveDocument.ComponentDefinition;
          


            TransientGeometry transientGeometry = application.TransientGeometry;


            Matrix matrix = transientGeometry.CreateMatrix();
    

            matrix.SetTranslation(transientGeometry.CreateVector(90, 0, 0));

            ComponentOccurrence oOcc1;
            oOcc1 = componentDefinition.Occurrences.Add(@"C:\Users\user\Desktop\part1.ipt", matrix);

    
0 Likes

Zach.Stauffer
Advocate
Advocate
Accepted solution

application.ActiveDocument is a Document, not an AssemblyDocument, and doesn't have a Component Definition. You should be able to cast the ActiveDocument to an AssemblyDocument and then access the ComponentDefinition:

if (application.ActiveDocument.DocumentType != DocumentTypeEnum.kAssemblyDocumentObject) return;
AssemblyDocument assemblyDoc = (AssemblyDocument)application.ActiveDocument;
ComponentDefinition assemblyComponentDef = assemblyDoc.ComponentDefinition;

 I added a check to make sure that the active document is an assembly, otherwise you'd get errors if the code was ran in a part document.

0 Likes

hayattangercekler2
Advocate
Advocate
Thank you for the answer. I would be very happy if you could help me with this problem.

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/working-in-a-ready-made-iam-with-inv...
0 Likes