With an increase in the number of CAD applications, the number of CAD files specific to applications has increased a lot.
Users are now interested to see designing a CAD file in one application and import in other application. Make some modifications and re-import it back,
Thanks to interoperability without which it would not have been possible.

Autodesk Inventor is one of the promising, prominent and giant application in CAD industry. Autodesk Inventor has exposed its application programming interface(API).

Believe us, almost all that user can do directly using graphical designing, it can be done using APIs.

Inventor API directly supports few of the standard geometries like Cylinder, Cone etc. But for customized geometries, there are a large number of options to draw them. For example to draw a cube a rectangle is required to be drawn and then just extrude it to a height as per required. This will output a cube. In blog ahead, we will have a detailed description on this.

Advantages:

  • Using Inventor API, we can draw any custom geometry.
  • We can import/read any custom CAD file in Inventor and draw geometries in Inventor.
  • Create custom UI as per requirements.
  • Handle user interaction for e.g. click events, selection events etc with model.

I’m assuming everyone is good enough to create a plugin application and get it running.

The code is completely done in C#. So let’s get started.

1. Creating a new Part Document:
Autodesk Inventor has two types of documents i.e. Part Document and Assembly Document.
Part document is like individual entity where all the graphical designing can be done.
Assembly document is like collection of part documents and assemblies.
Just imagine a case where you are designing a car. You have already designed a wheel. Save it as part. And just import it 4 times in an assembly document at four different locations. You are done. This is like reusability.


PartDocument oPartDoc = default(PartDocument);
oPartDoc = (PartDocument)ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, ThisApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject), true);

This creates a new part file in graphics. The last Boolean represent whether to create part document in silent mode or active i.e. actually create part document in graphics view.

To change the name of the part document in the model hierarchy and title bar:


oPartDoc.DisplayName = “Test Document”

To save the thumbnail of part document:


oPartDoc.SetThumbnailSaveOption(ThumbnailSaveOptionEnum.kActiveComponentIsoViewOnSave);

Few other thumbnail options are:


kNoThumbnail, kActiveComponentIsoViewOnSave, kActiveWindowOnSave, kActiveWindow, and kImportFromFile

Note: Above API works in Inventor 2017 and above only.

To save the part document at provided path:


oPartDoc.SaveAs(, true);

To close opened part document:

	
oPartDoc.Close(true);

This closes part document in silent mode i.e. does not ask user whether he wants tosave it or not.

 

2.Draw Cylinder:


TransientBRep tBrep = ThisApplication.TransientBRep;
oCompDef = oPartDoc.ComponentDefinition;

            Inventor.Point top = ThisApplication.TransientGeometry.CreatePoint(0, 0, 0);
            Inventor.Point bottomT = ThisApplication.TransientGeometry.CreatePoint(0, 5, 0);
            SurfaceBody body = tBrep.CreateSolidCylinderCone(top, bottomT, 0.5, 0.5, 0.5);
            oCompDef.Features.NonParametricBaseFeatures.Add(body);

3.Draw Cone:


Inventor.Point coneTop = ThisApplication.TransientGeometry.CreatePoint(1, 1, 0);         
Inventor.Point coneBottom = ThisApplication.TransientGeometry.CreatePoint(5, 1, 0);           
SurfaceBody conebody = tBrep.CreateSolidCylinderCone(coneTop, coneBottom, 0.5, 0.5, 0.7);
 oCompDef.Features.NonParametricBaseFeatures.Add(conebody);

 

4. Extrude Operation:
Extrude is to lengthen any profile i.e. closed 2d face for e.g. triangle, rectangle, circle to some height. Extrude can be in positive, negative as well as bidirectional.

Below example shows how a rectangle is extruded to form a cube.
There are two ways:

    1. By drawing the rectangle using Lines:


    SketchPoints oSkPnts = oSketch.SketchPoints;
            oSkPnts.Add(tGeom.CreatePoint2d(0, 0), false);
            oSkPnts.Add(tGeom.CreatePoint2d(1, 0), false);
            oSkPnts.Add(tGeom.CreatePoint2d(1, 1), false);
            oSkPnts.Add(tGeom.CreatePoint2d(0, 1), false);
            SketchLines oLines = oSketch.SketchLines;
            SketchLine[] oLine = new SketchLine[5];
            oLine[1] = oLines.AddByTwoPoints(oSkPnts[1], oSkPnts[2]);
            oLine[2] = oLines.AddByTwoPoints(oSkPnts[2], oSkPnts[3]);
            oLine[3] = oLines.AddByTwoPoints(oSkPnts[3], oSkPnts[4]);
            oLine[4] = oLines.AddByTwoPoints(oSkPnts[4], oSkPnts[1]);

            oProfile = oSketch.Profiles.AddForSolid();
 oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, PartFeatureOperationEnum.kJoinOperation);
oExtrudeDef.SetDistanceExtent(1,PartFeatureExtentDirectionEnum.kSymmetricExtentDirection);
            feature = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef); 
    

   2. By Drawing Rectangle directly:

 
PlanarSketch rectSketch = default(PlanarSketch);
            rectSketch = oCompDef.Sketches.Add (oCompDef.WorkPlanes[3]);
SketchEntitiesEnumerator oRectangleLines = rectSketch.SketchLines.AddAsTwoPointRectangle(tGeom.CreatePoint2d(6, 6), tGeom.CreatePoint2d(7, 5));									ObjectCollection oPathSegments = ThisApplication.TransientObjects.CreateObjectCollection();
            foreach (SketchEntity oentity in oRectangleLines)
            {
                oPathSegments.Add(oentity);
            }
            Profile rectProfile = rectSketch.Profiles.AddForSolid(false, oPathSegments);
            oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(rectProfile, PartFeatureOperationEnum.kJoinOperation);
            oExtrudeDef.SetDistanceExtent(1,PartFeatureExtentDirectionEnum.kPositiveExtentDirection);
            feature = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef);

 

5. Revolve Feature:
Revolve is one of the promising features where we draw a sketch, provide an axis for revolve and the angle for revolve.


PlanarSketch edSketch = default(PlanarSketch);
edSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes[3]);
SketchPoint pt = dSketch.SketchPoints.Add(tGeom.CreatePoint2d(5, 25)); 	           
SketchPoint pt1 = edSketch.SketchPoints.Add(tGeom.CreatePoint2d(1, 25));           
 SketchPoint pt2 = edSketch.SketchPoints.Add(tGeom.CreatePoint2d(9, 25));
SketchArc arc = edSketch.SketchArcs.AddByCenterStartEndPoint(pt, pt1, pt2);           
SketchLine line = edSketch.SketchLines.AddByTwoPoints(pt2, pt1);            
Profile profile = edSketch.Profiles.AddForSurface();
PartFeatures fe = oCompDef.Features;
RevolveFeatures rf = fe.RevolveFeatures;
rf.AddByAngle(profile, line, Math.PI, PartFeatureExtentDirectionEnum.kPositiveExtentDirection, PartFeatureOperationEnum.kSurfaceOperation);

 

6. iProperties:
iPropeties dialog is the place where you can add extra properties/attributes related to the part file. Property cay be anything like Color, Dimensions, Materials etc.


Document activeDoc = ThisApplication.ActiveDocument;
PropertySets pSets = activeDoc.PropertySets;
            foreach (PropertySet set1 in pSets)
            {
                String name = set1.Name;
                if (name.Equals("Inventor User Defined Properties"))
               {
                   set1.Add("This is iProperty","TestString");	//Text type
                   set1.Add( 20.0,"Double Type");              //Double type
                   set1.Add(true,"BooleanType");	       //Boolean Type
                 }
            }

 

There are a lot of more things which are interesting and can be done using Inventor programming API for e.g. Loft, Emboss, geometry intersection & booleans etc. But a blog is not a place where we can cover everything and show our technical skills. So get in touch with us on this.

For now, enjoy programming with Autodesk Inventor API’s and get back to us.

Author:Rahul K.
Contact us:
info@prototechsolutions.com
ProtoTech Solutions Pvt Ltd.