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