There are a couple of posts here about it....
here is the code i use.
You might have to do some adjustment for position and sizes depending on the sheet sizes set in your template. It works well for me on A3.
make sure the part or assembly file is saved before running it.
Also orientate the model wisely.
Make sure the template path is right in the rule.
Sub Main()
Dim oDrawingDoc As DrawingDocument
Dim oSheets As Sheets
Dim oView As DrawingView
'capture the current active sheet
Dim oCurrentNumber As Sheet
Dim oPartDoc As Document
Dim oSheet As Sheet
Dim oTG As TransientGeometry
Dim oView1 As DrawingView
Dim oView2 As DrawingView
Dim oView3 As DrawingView
Dim oView4 As DrawingView
Dim oView5 As DrawingView
Dim oView6 As DrawingView
Dim oViews As DrawingViews
Dim TextSize_TB As Decimal
Dim oScale As String
Dim oPartsList1 As PartsList
Dim View(0 To 5) As String
'GET SHEET METAL FLAT PATTERN EXTENTS LENGTH TO CALCULATE VIEW SCALES
'MessageBox.Show(iProperties.Value("Custom","FlatExtentsLength"), "Title")
'MessageBox.Show(iProperties.Value("Custom","FlatExtentsWidth"), "Title")
iProperties.Value("Custom", "Extents Length") = 1
iProperties.Value("Custom","Extents Width") = 1
extents_length = SheetMetal.FlatExtentsLength
extents_width = SheetMetal.FlatExtentsWidth
FLATPATTERNLENGTH = iProperties.Value("Custom","Extents Length")
FLATPATTERNWIDTH = iProperties.Value("Custom","Extents Width")
Dim sheetsize
sheetsize = MaxOfMany(FLATPATTERNLENGTH, FLATPATTERNWIDTH)
'Ask to create drawing?
AskCreateDrawing=MsgBox("Would you like to Create a drawing for this MODEL?"& Chr(10)&Chr(10)&"Ensure the front view is set", vbYesNo,"Drawing Selection")
If AskCreateDrawing = vbYes Then
oPartDoc = ThisDoc.Document
End If
'Rotate Model to correct view
' Dim oCamera As Camera
' oCamera = ThisApplication.ActiveView.Camera
' oCamera.ViewOrientationType = 10764 'Front View
' oCamera.Apply
'Define IDW Template File Location
oDrawingDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, "C:\Users\RomPoteaux\...\Templates\Template.idw", True)
oSheet = oDrawingDoc.Sheets.Item(1)
oSheet = oDrawingDoc.ActiveSheet
oViews = oSheet.DrawingViews
For Each oView In oViews
If oView.ScaleFromBase = False Then
oView.ScaleString = oScale
End If
Next
'Limits of Drawing Border in cm
X_Pos_Min = 4'0.6
X_Pos_Max = 30'35.46
Y_Pos_Min = 6
Y_Pos_Max = 30'27.58
vert_off = 1
TextSize_TB = 0.2
'Define 2d views
oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min+((X_Pos_Max-X_Pos_Min)*0.25), Y_Pos_Min+((Y_Pos_Max-Y_Pos_Min)*0.25)+vert_off)' Bottom Left
oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min+((X_Pos_Max-X_Pos_Min)*0.75), Y_Pos_Min+((Y_Pos_Max-Y_Pos_Min)*0.25)+vert_off)' Bottom Right
oPoint3 = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min+((X_Pos_Max-X_Pos_Min)*0.25), Y_Pos_Min+((Y_Pos_Max-Y_Pos_Min)*0.75)+vert_off)' Top Left
oPoint4 = ThisApplication.TransientGeometry.CreatePoint2d(35#, 12#)
oPoint5 = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min+((X_Pos_Max-X_Pos_Min)*0.75), Y_Pos_Min+((Y_Pos_Max-Y_Pos_Min)*0.75)+vert_off)' Top Right
oPointTLBL = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.125), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.625)+vert_off)'
'oPointTLTL = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.125), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.875)+vert_off)'
oPointTLBR = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.375), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.625) + vert_off)'
oPointTLTR = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.375), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.875)+vert_off)'
oPointTRBL = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.625), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.625)+vert_off)'
'oPointTRTL = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.625), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.875) + vert_off)'
oPointTRBR = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.875), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.625) + vert_off)'
oPointTRTR = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.875), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.875)+vert_off)'
oPointBLBL = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.125), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.125)+vert_off)'
'oPointBLTL = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.125), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.375)+vert_off)'
oPointBLBR = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.375), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.125) + vert_off)'
oPointBLTR = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.375), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.375)+vert_off)'
oPointBRBL = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.625), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.125)+vert_off)'
'oPointBRTL = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.625), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.375)+vert_off)'
oPointBRBR = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.875), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.125) + vert_off)'
oPointBRTR = ThisApplication.TransientGeometry.CreatePoint2d(X_Pos_Min + ((X_Pos_Max - X_Pos_Min) * 0.875), Y_Pos_Min + ((Y_Pos_Max - Y_Pos_Min) * 0.375)+vert_off)'
'-----------------------Flat Pattern View Section-------------------------------------------------------------------------
'This is the code which adds the sheetmetal flat pattern default view:
' Create a new NameValueMap object
Dim oBaseViewOptions As NameValueMap
oBaseViewOptions = ThisApplication.TransientObjects.CreateNameValueMap
'True = folded view
'False = flat pattern view
'oBaseViewOptions.Add("SheetMetalFoldedModel", False)
oBaseViewOptions.Add("SheetMetalFoldedModel", False)
'Set Base View
If oPartDoc.DocumentType = kPartDocumentObject Then
scale1 = get_size(oPartDoc, X_Pos_Max, X_Pos_Min, Y_Pos_Max, Y_Pos_Min)*3
ElseIf oPartDoc.DocumentType = kAssemblyDocumentObject Then
scale1 = get_sizeassy(oPartDoc, X_Pos_Max, X_Pos_Min, Y_Pos_Max, Y_Pos_Min)*3
Else
scale1 = 1 / 50
End If
' kDefaultViewOrientation = 10753
' kTopViewOrientation = 10754
' kRightViewOrientation = 10755
' kBackViewOrientation = 10756
' kBottomViewOrientation = 10757
' kLeftViewOrientation = 10758
' kIsoTopRightViewOrientation = 10759
' kIsoTopLeftViewOrientation = 10760
' kIsoBottomRightViewOrientation = 10761
' kIsoBottomLeftViewOrientation = 10762
' kArbitraryViewOrientation = 10763
' kFrontViewOrientation = 10764
' kCurrentViewOrientation = 10765
' kSavedCameraViewOrientation = 10766
' kFlatPivotRightViewOrientation = 10767
' kFlatPivotLeftViewOrientation = 10768
' kFlatPivot180ViewOrientation = 10769
' kFlatBacksideViewOrientation = 10770
' kFlatBacksidePivotRightViewOrientation = 10771
' kFlatBacksidePivotLeftViewOrientation = 10772
' kFlatBacksidePivot180ViewOrientation = 10773
oBaseView = oSheet.DrawingViews.AddBaseView(oPartDoc, oPoint1, scale1, kCurrentViewOrientation, kHiddenLineDrawingViewStyle)
'oSheet.DrawingDimensions.GeneralDimensions.Retrieve(oBaseView)
'Bottom Right View
oView3 = oSheet.DrawingViews.AddProjectedView(oBaseView, oPoint2, 32260)
' Dims only placed if Left Hand View NOT active
'oSheet.DrawingDimensions.GeneralDimensions.Retrieve(oView3)
'Top Left View
oView4 = oSheet.DrawingViews.AddProjectedView(oBaseView, oPoint3, 32260)
'oSheet.DrawingDimensions.GeneralDimensions.Retrieve(oView4)
'oView5 = oSheet.DrawingViews.AddBaseView(oPartDoc, oPoint5, scale1, kFlatBacksideViewOrientation, kHiddenLineDrawingViewStyle), , , oBaseViewOptions)
'oView5 = oSheet.DrawingViews.AddBaseView(oPartDoc, oPoint5, scale1, kFlatBackViewOrientation, kHiddenLineDrawingViewStyle, , , oBaseViewOptions)
''Top Right Hand ISO View
oView6 = oSheet.DrawingViews.AddBaseView(oPartDoc, oPoint4, scale1 * 0.75, kIsoTopLeftViewOrientation, kHiddenLineDrawingViewStyle)
''*** NO Dims on this View ***
'Created by Ben Smeaton 16/05/2023, bensmeaton@gmail.com.
'On Error GoTo ClintBrown3D
''Code to Centre Dimensions - Adapted from the Inevntor API Sample by @ClintBrown3D
' ' a reference to the active drawing document
' Dim oDoc As DrawingDocument
' oDoc = ThisApplication.ActiveDocument
' ' a reference to the active sheet & dimension
' 'Dim oSheet As Sheet
' oSheet = oDoc.ActiveSheet
' Dim oDrawingDim As DrawingDimension
' ' Iterate over all dimensions in the drawing and center them if they are linear or angular.
' For Each oDrawingDim In oSheet.DrawingDimensions
' If TypeOf oDrawingDim Is LinearGeneralDimension Or TypeOf oDrawingDim Is AngularGeneralDimension Then
' Call oDrawingDim.CenterText
' End If
' Next
''--------------------------------------------------------------------------------------------------------------------
''Code to Centre Dimensions - Adapted from https://modthemachine.typepad.com/my_weblog/2009/03/running-commands-using-the-api.html
'' Get the active document, assuming it is a drawing.
' Dim oDrawDoc As DrawingDocument
' oDrawDoc = ThisApplication.ActiveDocument
' ' Get the collection of dimensions on the active sheet.
' Dim oDimensions As DrawingDimensions
' oDimensions = oDrawDoc.ActiveSheet.DrawingDimensions
' ' Get a reference to the select set and clear it.
' Dim oSelectSet As SelectSet
' oSelectSet = oDrawDoc.SelectSet
' oSelectSet.Clear
' ' Add each dimension to the select set to select them.
' Dim oDrawDim As DrawingDimension
' For Each oDrawDim In oDimensions
' oSelectSet.Select(oDrawDim)
' Next
' Call ThisApplication.CommandManager.ControlDefinitions.Item("DrawingArrangeDimensionsCmd").Execute
'Return
'ClintBrown3D :
'MessageBox.Show("We've encountered a mystery", "Unofficial Inventor", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
' Run the external iLogic rule
'iLogicVb.RunRule("R:\Engineering\Autodesk\Customisation\iLogic\3-03AddOverallDims")
End Sub
Sub RunExternalRule(rulePath As String)
Dim oPartDoc As PartDocument = ThisDoc.Document
Try
iLogicVb.RunRule("R:\Engineering\Autodesk\Customisation\iLogic\3-03AddOverallDims")
Catch ex As Exception
' Handle any errors that might occur
MessageBox.Show("Error running the external iLogic rule: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Function New_Sheet (SheetName As Integer)
Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument
oSheet = oDoc.Sheets(1)
oSheet.Activate
Dim oCommandMgr As CommandManager
oCommandMgr = ThisApplication.CommandManager
' Get control definition for the line command.
Dim oControlDef As ControlDefinition
oControlDef = oCommandMgr.ControlDefinitions.Item("DrawingNewSheetCtxCmd")
' Execute the command.
Try
oControlDef.Execute
Dim oSheet2 As Sheet
oSheet2 = ThisDoc.Document.ActiveSheet
oSheet2.Name ="Sheet:" & SheetName
Catch
End Try
End Function
Function get_size(oRefDoc As PartDocument, X_Pos_Max As Decimal, X_Pos_Min As Decimal, Y_Pos_Max As Decimal, Y_Pos_Min As Decimal)
Try
' Get the TransientBRep and TransientGeometry objects.
Dim transBRep As TransientBRep = ThisApplication.TransientBRep
Dim transGeom As TransientGeometry = ThisApplication.TransientGeometry
' Combine all bodies in Part into a single transient Surface Body.
Dim combinedBodies As SurfaceBody = Nothing
For Each surfBody As SurfaceBody In oRefDoc.ComponentDefinition.SurfaceBodies
If combinedBodies Is Nothing Then
combinedBodies = transBRep.Copy(surfBody)
Else
transBRep.DoBoolean(combinedBodies, surfBody, BooleanTypeEnum.kBooleanTypeUnion)
End If
Next
' Get the oriented mininum range box of all bodies in Part.
' NOTE: "OrientedMinimumRangeBox" was added in Inventor 2020.3/2021.
Dim minBox As OrientedBox = combinedBodies.OrientedMinimumRangeBox
' Get length of each side of mininum range box.
Dim dir1 As Double = minBox.DirectionOne.Length
Dim dir2 As Double = minBox.DirectionTwo.Length
Dim dir3 As Double = minBox.DirectionThree.Length
' Sort lengths from smallest to largest.
Dim lengths As New List(Of Double) From {dir1, dir2, dir3 }
lengths.Sort
Dim minLength As Double = lengths(0)
Dim midLength As Double = lengths(1)
Dim maxLength As Double = lengths(2)
scale1 = (((Min(X_Pos_Max - X_Pos_Min, Y_Pos_Max - Y_Pos_Min)) / 2) * 0.33) / maxLength
Return scale1
Catch
scale1 = 1 / 50
Return scale1
End Try
End Function
Function get_sizeassy(oRefDoc As AssemblyDocument, X_Pos_Max As Decimal, X_Pos_Min As Decimal, Y_Pos_Max As Decimal, Y_Pos_Min As Decimal)
Dim oADoc As AssemblyDocument = oRefDoc
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oBox As Box = oADef.RangeBox
Dim oMinP As Point = oBox.MinPoint
Dim oMaxP As Point = oBox.MaxPoint
Dim dX As Double = oMaxP.X - oMinP.X
Dim dY As Double = oMaxP.Y - oMinP.Y
Dim dZ As Double = oMaxP.Z - oMinP.Z
Dim lengths As New List(Of Double) From {dX, dY, dZ}
lengths.Sort
Dim minLength As Double = lengths(0)
Dim midLength As Double = lengths(1)
Dim maxLength As Double = lengths(2)
scale1 = (((Min(X_Pos_Max - X_Pos_Min, Y_Pos_Max - Y_Pos_Min)) / 2) * 0.33) / maxLength
Return scale1
End Function
Function view_label(oView As DrawingView)
oView.ShowLabel = True
oDescription = "<StyleOverride FontSize='0.35'><StyleOverride Underline='True'><Property Document='model' PropertySet='Design Tracking Properties' Property='Description' FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' PropertyID='29'>DESCRIPTION</Property></StyleOverride></StyleOverride>"
oPartNumber = "<Br/><StyleOverride FontSize='0.35'><StyleOverride Underline='True'><Property Document='model' PropertySet='Design Tracking Properties' Property='Part Number' FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' PropertyID='5'>PART NUMBER</Property></StyleOverride></StyleOverride>"
oStringScale = "<Br/><StyleOverride FontSize='0.35'>(Scale <DrawingViewScale/>)</StyleOverride>"
' oView.Label.FormattedText = oDescription & oPartNumber & oStringScale
End Function