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

Making an extrude in ActiveDocument with Inventor api c#

I need to make an extrude in ActiveDocument with Inventor api c#. I try a lot, but it doesn't work. What is the reason for this? I want to cut an iam file with extrude that I have opened.

 

 

 

  Application application1 = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
            string openIam = "C:\\Users\\User\\Documents\\Assembly1.iam";




            string fullpath1 = "C:\\Users\\USER\\DesktoP\\part1.iam";
            string fullpath2 = "C:\\Servers\\part.iam";

            var documentOpen = _application.Documents.Open(openIam, true);

            documentOpen.UnitsOfMeasure.LengthUnits = UnitsTypeEnum.kMillimeterLengthUnits;
            var StartDoc = _application.ActiveDocument as AssemblyDocument ;


   
            AssemblyComponentDefinition oAsmCompDef = StartDoc.ComponentDefinition;




            TransientGeometry transientGeometry = _application.TransientGeometry;

            int inputIpt1X = Convert.ToInt16(kapi1xkonum.Text);

            int inputIpt2X = int.Parse(kapi2xkonum.Text);

            int kapi1X = inputIpt1X + 75;
            int kapi2X = inputIpt2X + 75;

            Matrix matrix1 = transientGeometry.CreateMatrix();

            
            matrix1.SetTranslation(transientGeometry.CreateVector(kapi1X, 0, 0));



            string kapi1 = openFileDialog1.FileName;
            ComponentOccurrence oOcc1;
            oOcc1 = oAsmCompDef.Occurrences.Add(kapi1, matrix1);

            Matrix matrix2 = transientGeometry.CreateMatrix();


            matrix1.SetTranslation(transientGeometry.CreateVector(kapi2X, 0, 0));

            string kapi2 = openFileDialog2.FileName;
            ComponentOccurrence oOcc2;
            oOcc2 = oAsmCompDef.Occurrences.Add(kapi2, matrix1);

 
            Matrix matrix3 = transientGeometry.CreateMatrix();


            matrix3.SetTranslation(transientGeometry.CreateVector(0, 0, 0));
            ComponentOccurrence oOcc3;
            oOcc3 = oAsmCompDef.Occurrences.Add(fullpath1, matrix2);

            PlanarSketch sktch = oAsmCompDef.Sketches.Add(oAsmCompDef.WorkPlanes["XY Plane"], true);

            Profile oProfile = sktch.Profiles.AddForSolid();

            var cornerPointOne = transientGeometry.CreatePoint2d(20, 10);
            var cornerPointTwo = transientGeometry.
                CreatePoint2d(90, 10);

            _currentSketch.SketchLines.
                AddAsTwoPointRectangle(cornerPointOne, cornerPointTwo);


            var extrudeDef = oAsmCompDef.Features.ExtrudeFeatures.
                CreateExtrudeDefinition(oProfile,
                PartFeatureOperationEnum.kCutOperation);
            extrudeDef.SetDistanceExtent(42, PartFeatureExtentDirectionEnum.kPositiveExtentDirection);
            oAsmCompDef.Features.ExtrudeFeatures.Add(extrudeDef);


            StartDoc.SaveAs(fullpath2, false);

 

 

Tags (1)
Labels (2)

The code you posted lacks of initialization of some fields and variables, maybe due to copy and paste from a bigger project from which this code was taken. Try to post  cleaner and easy to run code next time.
The issue could be the rectangle you're creating in the sketch: you cannot make a rectangle given two points on the same line (20, 10), (90, 10)

Thank you very much, you are the best. After looking at the codes again and changing their order and structure, I got the result I wanted. I had been working on it for a long time and this really pissed me off. There was no correct result from the answers of those who returned from the forum.
After all, I am sharing my correct working codes here.

 

            string openIam = "C:\\Users\\username\\Documents\\Assembly1.iam";

            string fullpath1 = "C:\\Users\\username\\Desktop\\sampleIam\\partiam.iam";

            string fullpath2 = "C:\\Servers\\partiam.iam";

            var documentOpen = _application.Documents.Open(openIam, true);

  
            if (_application.ActiveDocument.DocumentType != DocumentTypeEnum.kAssemblyDocumentObject) return;

            documentOpen.UnitsOfMeasure.LengthUnits = UnitsTypeEnum.kMillimeterLengthUnits;

            AssemblyDocument StartDoc = _application.ActiveDocument as AssemblyDocument  ;

            var oSketch = _application.ActiveEditObject;

            var oAsmCompDef = StartDoc.ComponentDefinition;


            TransientGeometry transientGeometry = _application.TransientGeometry;



            Matrix matrix3 = transientGeometry.CreateMatrix();


            matrix3.SetTranslation(transientGeometry.CreateVector(0, 0, 0));

            ComponentOccurrence oOcc3;

            oOcc3 = oAsmCompDef.Occurrences.Add(fullpath1, matrix3);


            PlanarSketch osketch = oAsmCompDef.Sketches.Add(oAsmCompDef.WorkPlanes[3]);

            TransientGeometry transientgeo = _application.TransientGeometry;

            SketchEntitiesEnumerator objects = osketch.SketchLines.AddAsTwoPointRectangle(transientgeo.CreatePoint2d(20, 0), transientgeo.CreatePoint2d(95, 10));

            SketchLine sketchLine = objects[1] as SketchLine;

            double oneLine = sketchLine.Length;

            double copyLine = oneLine / 2;


            Profile oProfile = osketch.Profiles.AddForSolid();


            ExtrudeDefinition extrudeDefinition = oAsmCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, PartFeatureOperationEnum.kCutOperation);

            extrudeDefinition.SetDistanceExtent(42, PartFeatureExtentDirectionEnum.kSymmetricExtentDirection);

            var oExtrude = oAsmCompDef.Features.ExtrudeFeatures.Add(extrudeDefinition);


            int ipt1Xinput = Convert.ToInt16(ipt1xkonum.Text);

            int ipt2Xinput = int.Parse(ipt2xkonum.Text);

            int ipt1X = ipt1Xinput + 75;
            int ipt2X = ipt2Xinput + 75;

            Matrix matrix1 = transientGeometry.CreateMatrix();


            matrix1.SetTranslation(transientGeometry.CreateVector(ipt1X , 0, 0));



            string ipt1file = openFileDialog1.FileName;
            ComponentOccurrence oOcc1;
            oOcc1 = oAsmCompDef.Occurrences.Add(ipt1file , matrix1);

            Matrix matrix2 = transientGeometry.CreateMatrix();


            matrix1.SetTranslation(transientGeometry.CreateVector(ipt2X , 0, 0));

            string ipt2file = openFileDialog2.FileName;
            ComponentOccurrence oOcc2;
            oOcc2 = oAsmCompDef.Occurrences.Add(ipt2file , matrix2);


            

            StartDoc.SaveAs(fullpath2, false);

Next time post the exception and the line throwing the exception you get running the code, too.