Making an extrude in ActiveDocument with Inventor api c#

Making an extrude in ActiveDocument with Inventor api c#

hayattangercekler2
Advocate Advocate
490 Views
3 Replies
Message 1 of 4

Making an extrude in ActiveDocument with Inventor api c#

hayattangercekler2
Advocate
Advocate

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);

 

 

0 Likes
Accepted solutions (1)
491 Views
3 Replies
Replies (3)
Message 2 of 4

CattabianiI
Collaborator
Collaborator

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)

0 Likes
Message 3 of 4

hayattangercekler2
Advocate
Advocate
Accepted solution

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);
0 Likes
Message 4 of 4

CattabianiI
Collaborator
Collaborator

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