Creating a drawing from scratch with the Inventor API

By Philippe Leefsma

Q:

Is it possible to create Drawing Dimensions and other annotation such as a text box with a leader using the Inventor API?

A:

The following VB.NET example show how to create a new drawing and add views. Dimensions in a view are created along with a text box and a leader.

The attached ipt file provided as link below has attribues attached to the edges that are used in the CreateDrawing function. Notice that the location of this file is hard coded to C:\Temp\Part1.ipt in the code. Change this to the location where you put the file.

  

    ‘ Demonstrates the ability to automatically create a drawing. 

    ‘ It’s hard coded to look for edges

    ‘ with specific attributes attached. The supplied Part1.ipt

    ‘ has the attributes attached to edges..

    Public Sub CreateDrawing(ByVal app As Inventor.Application)

 

        ‘ Create a new drawing document using the default template.

        Dim oDrawDoc As DrawingDocument

        oDrawDoc = m_inventorApp.Documents.Add(

            DocumentTypeEnum.kDrawingDocumentObject, _

            m_inventorApp.FileManager.GetTemplateFile(

                DocumentTypeEnum.kDrawingDocumentObject))

 

        Dim oSheet As Sheet

        oSheet = oDrawDoc.ActiveSheet

 

        ‘ Open the part to be inserted into the drawing.

        Dim oPartDoc As PartDocument

        oPartDoc = m_inventorApp.Documents.Open("c:\temp\Part1.ipt", False)

 

        Dim oTG As TransientGeometry

        oTG = m_inventorApp.TransientGeometry

 

        MsgBox("Create base and orthographic views.")

 

        ‘ Place the base front view.

        Dim oFrontView As DrawingView

        oFrontView = oSheet.DrawingViews.AddBaseView(

            oPartDoc,

            oTG.CreatePoint2d(15, 12),

            3,

            ViewOrientationTypeEnum.kFrontViewOrientation,

            DrawingViewStyleEnum.kHiddenLineDrawingViewStyle)

 

        ‘ Create the top, right and iso views.

        Dim oTopView As DrawingView

        oTopView = oSheet.DrawingViews.AddProjectedView(

            oFrontView,

            oTG.CreatePoint2d(15, 30),

            DrawingViewStyleEnum.kFromBaseDrawingViewStyle)

 

        Dim oIsoView As DrawingView

        oIsoView = oSheet.DrawingViews.AddProjectedView(

            oFrontView,

            oTG.CreatePoint2d(40, 30),

            DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle)

 

        MsgBox("Create section view.")

 

        ‘ Create a front section view by

        ‘ defining a section line in the front view.

        Dim oSectionSketch As DrawingSketch

        oSectionSketch = oFrontView.Sketches.Add

 

        oSectionSketch.Edit()

 

        ‘ Get the circular edge of the top of the part.

        Dim oObjs As ObjectCollection

        oObjs = oPartDoc.AttributeManager.FindObjects("Name", "Name", "Edge13")

        Dim oCircularEdge As Edge

        oCircularEdge = oObjs.Item(1)

 

        ‘ Get the associated drawingcurve.

        Dim oDrawViewCurves As DrawingCurvesEnumerator

        oDrawViewCurves = oFrontView.DrawingCurves(oCircularEdge)

        Dim oCircularCurve As DrawingCurve

        oCircularCurve = oDrawViewCurves.Item(1)

 

        Dim oCircularEntity As SketchEntity

        oCircularEntity = oSectionSketch.AddByProjectingEntity(oCircularCurve)

 

        ‘ Draw the section line.

        Dim oSectionLine As SketchLine

        oSectionLine = oSectionSketch.SketchLines.AddByTwoPoints(

            oTG.CreatePoint2d(0, 0.9),

            oTG.CreatePoint2d(0, -0.9))

 

        ‘ Create a constraint to the projected circle center point and the line.

        Call oSectionSketch.GeometricConstraints.AddCoincident(

            oSectionLine,

            oCircularEntity.CenterSketchPoint)

 

        oSectionSketch.ExitEdit()

 

        ‘ Create the section view.

        Dim oSectionView As SectionDrawingView

        oSectionView = oSheet.DrawingViews.AddSectionView(

            oFrontView,

            oSectionSketch,

            oTG.CreatePoint2d(34, 10),

            DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle,

            Nothing,

            False)

 

        MsgBox("Create detail view.")

 

        ‘ Create the detail view.

        Dim oDetailView As DetailDrawingView

        oDetailView = oSheet.DrawingViews.AddDetailView(

            oSectionView,

            oTG.CreatePoint2d(35, 15),

            DrawingViewStyleEnum.kFromBaseDrawingViewStyle,

            False,

            oTG.CreatePoint2d(21, 16),

            oTG.CreatePoint2d(24, 18), , 10, , "A")

 

        ‘ Get the various edges of the model.

        Dim aoEdges(0 To 13) As Edge

        Dim i As Integer

        For i = 1 To 13

            oObjs = oPartDoc.AttributeManager.FindObjects(

                "Name", "Name", "Edge" & i)

            aoEdges(i) = oObjs.Item(1)

        Next

 

        ‘ Get the equivalent drawing curves.

        Dim aoDrawCurves(0 To 13) As DrawingCurve

        For i = 1 To 13

            oDrawViewCurves = oFrontView.DrawingCurves(aoEdges(i))

            aoDrawCurves(i) = oDrawViewCurves.Item(1)

        Next

 

        MsgBox("Create dimensions to the curves in the view.")

 

        ‘ Create some dimensions

        Dim oGeneralDims As GeneralDimensions

        oGeneralDims = oSheet.DrawingDimensions.GeneralDimensions

 

        Dim oDim As GeneralDimension

 

        oDim = oGeneralDims.AddLinear(

            oTG.CreatePoint2d(15.5, 13),

            oSheet.CreateGeometryIntent(aoDrawCurves(1)),

            oSheet.CreateGeometryIntent(aoDrawCurves(3)),

            DimensionTypeEnum.kVerticalDimensionType)

 

        oDim = oGeneralDims.AddLinear(

            oTG.CreatePoint2d(16, 9),

            oSheet.CreateGeometryIntent(aoDrawCurves(2)),

            oSheet.CreateGeometryIntent(aoDrawCurves(4)),

            DimensionTypeEnum.kHorizontalDimensionType)

 

        oDim = oGeneralDims.AddRadius(

            oTG.CreatePoint2d(17, 19),

            oSheet.CreateGeometryIntent(aoDrawCurves(13)))

 

        oDim = oGeneralDims.AddDiameter(

            oTG.CreatePoint2d(8, 20),

            oSheet.CreateGeometryIntent(aoDrawCurves(13)),

            True, False)

 

        MsgBox("Create a text box with a leader.")

 

        ‘ Place a text box with a leader.

        Dim oObjColl As ObjectCollection

        oObjColl = m_inventorApp.TransientObjects.CreateObjectCollection

        Call oObjColl.Add(oTG.CreatePoint2d(17.5, 11))

        Dim oEval As Curve2dEvaluator

        oEval = aoDrawCurves(4).Evaluator2D

        Dim adParams(0) As Double

        adParams(0) = 0.6

        Dim adPoints(0 To 1) As Double

        Call oEval.GetPointAtParam(adParams, adPoints)

 

        Call oObjColl.Add(oTG.CreatePoint2d(adPoints(0), adPoints(1)))

        Dim oLeaderNote As LeaderNote

        oLeaderNote = oSheet.DrawingNotes.LeaderNotes.Add(

            oObjColl, "Text with a leader")

        oLeaderNote.DimensionStyle = oLeaderNote.DimensionStyle

 

    End Sub

   

<

p style=”line-height: normal;margin: 0in 0in 0pt” class=”MsoNormal”> 


Comments

7 responses to “Creating a drawing from scratch with the Inventor API”

  1. Michel van den Maagdenberg Avatar
    Michel van den Maagdenberg

    Hi Philippe,
    I am really new to Inventor API programming and am tring to find my way through sample code. This topic was very interesting to try.
    If I am running the code it seems to fail on this part:
    Create section view Area:
    oFrontView.Sketches.Add
    oSectionSketch.Edit()
    If I type a “.” after Sketches I am not able to select the Add.0Section.Edit()
    The error that I am getting states there has been a change to the class library?
    Can you pls help me resolving this?
    Thanks and regards,
    Michel

  2. Philippe Avatar
    Philippe

    Hi Michel,
    Sorry I’m not getting what you are talking about… Which version of Inventor interop are you using? Which language, is is VB.Net? Which version? This code is VB.Net 4.0
    The following instructions need to be on separate lines
    oFrontView.Sketches.Add
    oSectionSketch.Edit()

  3. info@shapotools.com Avatar
    info@shapotools.com

    Hi,
    The above example is for creating a drawing for a part. Is the process the same while creating a drawing for an assembly? The attributes are contained in the parts / subassemblies and do not show up at assembly level. Do you have sample code for the same?
    Thanks & Regards,
    Shapoor

  4. This VB.net is it to create a DLL or EXE? How does it trigger the new drawing?
    Thank’s!

  5. Alain Avatar
    Alain

    Is it possible to pick a part or assembly (Prompt to select a file) in a directory instead of giving a path where is the file in the vb.net?
    Thank’s!
    Alain

  6. oPartDoc.AttributeManager.FindObjects(“Name”, “Name”, “Edge13”) error

  7. franklinadams38@gmail.com Avatar
    franklinadams38@gmail.com

    Could someone please explain how to run this code?

Leave a Reply to franklinadams38@gmail.comCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading