Automaticly create a drawing from a part

jeroenbrouwers8C4TM
Explorer
Explorer

Automaticly create a drawing from a part

jeroenbrouwers8C4TM
Explorer
Explorer

Hi All,

 

im trying to get a code to create automaticly a drawing from a part.

i want to have automaticly the wright scale and the dimensions.

im trying all kinds off coding but not one is working on my end.

in the future i want to get more out of this. but im new in the coding 

 

Can someone help me out. we use inventor 2021.

 

the code what comes nearby is this one. but it doestnt wok completly (i copy paste it its to complex for me)

we have parts sometimes of 1 meter and other times of 12 meter that doesnt work in this code.

 

f (ActiveSheet.Name <> "Sheet:2") Then Return
SheetPlayX = 250
SheetPlayY = 250 

deviation_X = -10
deviation_Y = 40
play_between_views = -20 


Dim View(0 To 6) As String
Dim CenterX(0 To 6) As Single
Dim CenterY(0 To 6) As Single
Dim ViewHeight(0 To 6) As Single
Dim ViewWidth(0 To 6) As Single
Dim PlayX(0 To 3) As Single
Dim PlayY(0 To 3) As Single
ViewQtX = 0
ViewQtY = 0


SheetWidth = ActiveSheet.Width - SheetPlayX - play_between_views
SheetHeight = ActiveSheet.Height - SheetPlayY - play_between_views

View(1)="VIEW1"
View(2)="VIEW2"
View(3)="VIEW3"
View(4)="VIEW4"
View(5)="VIEW5"
View(6)="VIEW6"

CurrentScale = ActiveSheet.View(View(1)).Scale

For i=1 To 6
Try
ViewHeight(i) = ActiveSheet.View(View(i)).Height * (1 / CurrentScale)
ViewWidth(i) = ActiveSheet.View(View(i)).Width * (1 / CurrentScale)
Catch
ViewHeight(i)=0
ViewWidth(i)=0
End Try
Next

MaxWidth = ViewWidth(5) + ViewWidth(1) + ViewWidth(3)
MaxHeight = ViewHeight(4) + ViewHeight(1) + ViewHeight(2)


ScaleX =  SheetWidth / MaxWidth 
ScaleY =  SheetHeight / MaxHeight

FinalScale = Min(ScaleY,ScaleX)

If FinalScale <1 Then
FinalScale = FinalScale^-1
mm = 1
FinalScale = Round(Round(FinalScale,2) / mm) * mm
ActiveSheet.View(View(1)).ScaleString = "1/" & FinalScale
ActiveSheet.View(View(6)).ScaleString="1/"&FinalScale
Else
mm = 1
FinalScale = Round(Round(FinalScale,2) / mm) * mm
ActiveSheet.View(View(1)).Scale = FinalScale
End If


iProperties.Value("Custom", "Scale") = ActiveSheet.View(View(1)).ScaleString
iProperties.Value("Custom", "Scale")=ActiveSheet.View(View(6)).ScaleString


For i=1 To 6
Try
ViewHeight(i)=ActiveSheet.View(View(i)).Height
ViewWidth(i)=ActiveSheet.View(View(i)).Width
Catch
ViewHeight(i)=0
ViewWidth(i)=0
End Try
Next


MaxWidth = ViewWidth(5) + ViewWidth(1) + ViewWidth(3)
MaxHeight = ViewHeight(4) + ViewHeight(1) + ViewHeight(2)


If ViewWidth(5)<>0 Then
NumberViewsX = NumberViewsX+1
End If
If ViewWidth(1)<>0 Then
NumberViewsX = NumberViewsX+1
End If
If ViewWidth(3)<>0 Then
NumberViewsX = NumberViewsX+1
End If

If ViewHeight(4)<>0 Then
NumberViewsY = NumberViewsY+1
End If
If ViewHeight(1)<>0 Then
NumberViewsY = NumberViewsY+1
End If
If ViewHeight(2)<>0 Then
NumberViewsY = NumberViewsY+1
End If



PlayX(0) = (ActiveSheet.Width - MaxWidth) /(NumberViewsX+1)
PlayY(0) = (ActiveSheet.Height - MaxHeight) /(NumberViewsY+1)

If ViewWidth(5)<>0 Then
PlayX(1) = PlayX(0) + play_between_views
End If
If ViewWidth(1)<>0 Then
PlayX(2) = PlayX(0) + play_between_views
End If

If ViewHeight(2)<>0 Then
PlayY(1)=PlayY(0) + play_between_views
End If
If ViewHeight(1)<>0 Then
PlayY(2)=PlayY(0) + play_between_views
End If


CenterX(5)= PlayX(0) + deviation_X + ViewWidth(5)/2
CenterX(1)= CenterX(5) + ViewWidth(5)/2 + PlayX(1) + ViewWidth(1)/2
CenterX(3) = CenterX(1) + ViewWidth(1) / 2 + PlayX(2) + ViewWidth(3) / 2


CenterX(2)= CenterX(1)
CenterX(4) = CenterX(1)
CenterX(6) = CenterX(3)-60

CenterY(2)= PlayY(0) + deviation_Y + ViewHeight(2)/2
CenterY(1)= CenterY(2) + ViewHeight(2)/2 + PlayY(1) + ViewHeight(1)/2-70
CenterY(4)= CenterY(1) + ViewHeight(1)/2 + PlayY(2) + ViewHeight(4)/2+20

CenterY(5)=CenterY(1)
CenterY(3) = CenterY(1)
CenterY(6) = CenterY(4) -60



For i=1 To 6
Try
ActiveSheet.View(View(i)).SetCenter(CenterX(i),CenterY(i))
Catch
End Try
Next
Dim oDoc As DrawingDocument
    oDoc = ThisApplication.ActiveDocument


   

scaleview6 =CurrentScale

InventorVb.DocumentUpdate()
0 Likes
Reply
Accepted solutions (2)
1,586 Views
8 Replies
Replies (8)

WCrihfield
Mentor
Mentor
Accepted solution

It looks like all this code is doing is dealing with scales & positions of existing views of an existing drawing, with no mention of a part document for a source model.

Here is some code I put together about a year or so ago.  It starts from the open (active) part document, then creates a drawing, then inspects the models size, and uses that information to figure the correct scale and orientation to place four views of it onto the new drawing.  It creates a front, top, right, and iso view (4 total).  It takes into account having a border and title block within the drawing, and avoids it (not in a 'smart' way, just using offset numbers to define what area of the sheet that is available.)

It doesn't go into placing dimensions on it, though, because that's a whole other complex task that is highly customized to specific model geometry, and I wanted this to be a more universally used tool.

There are some comments within, but isn't to difficult to follow.

 

Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition

If System.IO.File.Exists(ThisDoc.PathAndFileName(False) & ".idw") Then
	oOverWrite = MsgBox("A Drawing for this model already exists." & vbNewLine &
	"Do you want to over-write it?", vbYesNo + vbQuestion, "DRAWING EXISTS!")
	If oOverWrite = vbNo Then
		Return
	End If
End If

'The following line sends a full file name to Inventor's Clipboard, this can be used to automatically
'fill in a dialog that would normally pop-up asking for what model you want to place into your drawing.
'ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent,oFileName)


Dim oTPath As String = ThisApplication.FileOptions.TemplatesPath
Dim oTName As String = "B-SIZE STANDARD DRAWING TEMPLATE.idw"
Dim oDDoc As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, oTPath & "\" & oTName, True)
'oDDoc.SheetFormats.Item(1)

'Using Measure (instead of RangeBox)
Dim oXDim As Long = Measure.ExtentsLength
Dim oYDim As Long = Measure.ExtentsWidth
Dim oZDim As Long = Measure.ExtentsHeight

''Using RangeBox for size
'Dim oRangeBox As Box = oPDef.RangeBox
'Dim oXDim As Long = (oRangeBox.MaxPoint.X - oRangeBox.MinPoint.X)
'Dim oYDim As Long = (oRangeBox.MaxPoint.Y - oRangeBox.MinPoint.Y)
'Dim oZDim As Long = (oRangeBox.MaxPoint.Z - oRangeBox.MinPoint.Z)

'MsgBox("RangeBox Size of Model = " & vbNewLine &
'oXDim & " along X Axis" & vbNewLine &
'oYDim & " along Y Axis" & vbNewLine &
'oZDim & " along Z Axis")

Dim oModelWidth As Long = MaxOfMany(oXDim, oYDim, oZDim)
Dim oModelHeight As Long = MinOfMany(oXDim, oYDim, oZDim)

'MsgBox("Longest Dim = " & oModelWidth & vbCrLf &
'"This will be used to calculate scale & view width in drawing.")

Dim oViewRotation As Double
Dim oViewOrientation As ViewOrientationTypeEnum
If oModelWidth = oXDim And oModelHeight = oYDim Then
	oViewOrientation = ViewOrientationTypeEnum.kFrontViewOrientation
	oViewRotation = 0
ElseIf oModelWidth = oXDim And oModelHeight = oZDim Then
	oViewOrientation = ViewOrientationTypeEnum.kTopViewOrientation
	oViewRotation = 0
ElseIf oModelWidth = oYDim And oModelHeight = oXDim Then
	oViewOrientation = ViewOrientationTypeEnum.kFrontViewOrientation
	oViewRotation = (PI/2)
ElseIf oModelWidth = oYDim And oModelHeight = oZDim Then
	oViewOrientation = ViewOrientationTypeEnum.kRightViewOrientation
	oViewRotation = (PI/2)
ElseIf oModelWidth = oZDim And oModelHeight = oXDim Then
	oViewOrientation = ViewOrientationTypeEnum.kTopViewOrientation
	oViewRotation = (PI/2)
ElseIf oModelWidth = oZDim And oModelHeight = oYDim Then
	oViewOrientation = ViewOrientationTypeEnum.kRightViewOrientation
	oViewRotation = 0
End If

'Get the size of the Active Sheet
Dim oSheetWidth As Double = oDDoc.ActiveSheet.Width
Dim oSheetHeight As Double = oDDoc.ActiveSheet.Height

'Get size of area within border available for views (used for scaling views)
Dim oSideBorderOffset As Double = (.5 * 2.54)
Dim oTopBottomBorderOffset As Double = (.5 * 2.54)
Dim oTitleBlockHeight As Double = (2.5 * 2.54)
Dim oBorderWidth As Double = oSheetWidth-(oSideBorderOffset*2)
Dim oBorderHeight As Double = oSheetHeight - (oTopBottomBorderOffset * 2)
Dim oAreaAboveTitleBlock As Double = (oBorderHeight - oTopBottomBorderOffset) - oTitleBlockHeight

'Set view scale, based on model width
Dim oBViewScale As Double
If oModelWidth > (oBorderWidth/2) Then
	oBViewScale = ((oBorderWidth/2) / oModelWidth)
ElseIf oModelWidth < (oBorderWidth/2) Then
	oBViewScale = (oModelWidth / (oBorderWidth/2))
End If

inc = 1/32
oBViewScale = Round(Round((oBViewScale/2.54),5) / inc) * inc

'Try creating & placing each view at 1:1 scale, then scale to needed factor.
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oViews As DrawingViews = oDDoc.ActiveSheet.DrawingViews

Dim oFullScale As Double = 1
Dim oOriginPoint As Point2d = oTG.CreatePoint2d(0, 0)
Dim oBaseView As DrawingView = oViews.AddBaseView(oPDoc, oOriginPoint, oFullScale, _
oViewOrientation, DrawingViewStyleEnum.kHiddenLineDrawingViewStyle)

'Locations of view centerpoint columns & rows
Dim o1stColumn As Double = oSideBorderOffset + (oBorderWidth / 4)
Dim o2ndColumn As Double = oSideBorderOffset + ((oBorderWidth / 4) * 3)
Dim o1stRow As Double = oTopBottomBorderOffset + oTitleBlockHeight + (oAreaAboveTitleBlock/4)
Dim o2ndRow As Double = oTopBottomBorderOffset + oTitleBlockHeight + ((oAreaAboveTitleBlock/4)*3)

'Base View center point location
Dim oBaseViewLocation As Point2d = oTG.CreatePoint2d(o1stColumn, o1stRow)

'Dim oBaseView As DrawingView = oViews.AddBaseView(oPDoc, oBaseViewLocation, oBViewScale, _
'oViewOrientation, DrawingViewStyleEnum.kHiddenLineDrawingViewStyle)
oBaseView.Rotation = oViewRotation
oBaseView.Position = oBaseViewLocation
oBaseView.Scale = oBViewScale
oBaseView.ScaleString = RoundToFraction(oBViewScale,1/32,RoundingMethod.Round)
'oBaseView.Center = oBaseViewLocation
'oBaseView.RotateByAngle = 
oBaseView.IncludeMeshBodies = True
oBaseView.IncludeSurfaceBodies = True
'oBaseView.Top
'oBaseView.Height
'oBaseView.Left
'oBaseView.Width
oBaseView.Name = "BASE VIEW"
InventorVb.DocumentUpdate()
'MsgBox("Center Of View Is At:  " & oBaseView.Center.X & " , " & oBaseView.Center.Y)

'MsgBox("oBViewScale = " & oBaseView.ScaleString)


Dim oUpperViewInsPoint As Point2d = oTG.CreatePoint2d(o1stColumn, o2ndRow)
Dim oUpperView As DrawingView = oViews.AddProjectedView(oBaseView, oUpperViewInsPoint, _
DrawingViewStyleEnum.kFromBaseDrawingViewStyle)
oUpperView.Name = "UPPER VIEW"
oUpperView.IncludeMeshBodies = True
oUpperView.IncludeSurfaceBodies = True

Dim oRightViewInsPoint As Point2d = oTG.CreatePoint2d(o2ndColumn, o1stRow)
Dim oRightView As DrawingView = oViews.AddProjectedView(oBaseView, oRightViewInsPoint, _
DrawingViewStyleEnum.kFromBaseDrawingViewStyle)
oRightView.Name = "RIGHT VIEW"
oRightView.IncludeMeshBodies = True
oRightView.IncludeSurfaceBodies = True

Dim oISOViewInsPoint As Point2d = oTG.CreatePoint2d(o2ndColumn, o2ndRow)
Dim oISOView As DrawingView = oViews.AddProjectedView(oBaseView, oISOViewInsPoint, _
DrawingViewStyleEnum.kShadedDrawingViewStyle)
oISOView.Name = "ISO VIEW"
oISOView.IncludeMeshBodies = True
oISOView.IncludeSurfaceBodies = True

 

If you have trouble understanding something in there, just let me know.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

jeroenbrouwers8C4TM
Explorer
Explorer
Accepted solution

thank you for your reply.

it doesnt seems to work on my end. i get a  error :

 

 

Error in rule: Rule0, in document: S03-M002.ipt

De parameter is onjuist. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

System.ArgumentException: De parameter is onjuist. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.Documents.Add(DocumentTypeEnum DocumentType, String TemplateFileName, Boolean CreateVisible)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

0 Likes

WCrihfield
Mentor
Mentor

I suppose I should have explained further.

There are some things within this code that you will have to customize to your situation.

It looks like it is having trouble creating the drawing file, near the top of the code.

  • You will need to change the value of the "oTPath" variable to the name of your drawing template file.  In my case I have several drawing template files, depending on what I'm going to be using the drawing for, so I specified the file name of the one I wanted to use here.
  • You will likely need to change the values of the following variables to fit your drawing layout, border offset from the edges of your sheet, and title block size/position.
    • oSideBorderOffset - This specifies how far the border geometry is from the edges of my sheet (on right and left sides of the sheet), to make sure my views are inside it.
    • oTopBottomBorderOffset - This specifies how far the border geometry is from the edges of my sheet (on top & bottom of the sheet)
    • oTitleBlockHeight - This specifies how tall my title block is above the bottom of my border.

There may be others too, just read through it if you can, and see if anything looks like it may need to be updated to suit your needs.  I was converting the numbers from the default centimeters to inches, too, which you most likely won't need to.  So anywhere you see me multiplying or dividing by 2.54 or similar, you can probably get rid of that math.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

jeroenbrouwers8C4TM
Explorer
Explorer

Hi WCrihfield,

 

it works thank you for your answer.

now ive got to get the scale wright because it scales it to small.

 

 

maybe im going to take a course for ilogic because i think we can get allot more out of it.

 

 

0 Likes

G.Binl
Advocate
Advocate

@WCrihfield  Hello hope you are well,

Thanks for the code.

I have most of what i want to do and added the scaling as shown (1:12) which will be adjusted to just a select few scales. next is to set-up the next 3 sheet with set views and representations.

My question is my model front view is shown with the cube stating front, but when the base view is dropped into drawing it uses the right view i tried to adjust some parameters to rotate it but to no avail. any help would be great. 

GBinl_0-1683121732669.pngGBinl_3-1683122029890.png

to get the Scale to look like (1:12) I used ElseIF, and added a new variable oGBScale

ElseIf oBViewScale >.083 And oBViewScale < .1 Then
		 oBViewScale = 1 / 12
		 oGBScale = "1:12"
oBaseView.ScaleString = oGBScale
 

 

 

0 Likes

WCrihfield
Mentor
Mentor

Hi @G.Binl.  I think I understand the situation, but I am not 100% sure.  Dealing with how the ViewCube is set-up in relation to the model's origin WorkAxes is somewhat critical to how that part of my code worked for me.  We have that orientation set-up the way we like it in our model template files, so we know what to expect, but there are some older model files where that orientation was messed around with, and would throw things off.  If the ViewCube orientation to your model origin axes are not the same as what is portrayed in this code, then you may have to have a closer look at that association, then modify the block of code which sets the values of the 'oViewRotation' & 'oViewOrientation' variables, to suit your specific situation.  But if that association varies from one model to the next, I am not sure what you may have to do to correct the orientation.  I believe my goal at the time I wrote that code was to make sure my 'base' view was both the widest one (left to right) primarily, and the tallest one secondarily, because I always use landscape sheet orientation, and the base view usually contains the most details, dimensions, & notes.  If you have not already looked into SheetFormats, that is another very useful tool in these types of situations.

 

Also, I don't know why I was using 'Long' as the Data Type for the model size...it seems like that should have been 'Double' instead, because all of those methods return Double data type.  I think when using Long, it just drops the portion after the decimal place, making it less accurate.  Plus, for determining the model width, height, & depth, I could have used a List(Of Double), then used its Sort method, instead of MaxOfMany & MinOfMany.  I'm sure there are lots of ways of making it more efficient or doing things differently.  I don't really do a ton of complete drawing automation, because I deal with far too much variety in what we create drawings for, and having multiple engineers/designers (both past & present) complicates things a bit too.  It takes a fairly high level of uniformity & preparation for full drawing automation to be most effective, efficient, & useful.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Zain_Zarar
Explorer
Explorer

Zain_Zarar_0-1716292510908.png

I'm getting this error message, any solution?

0 Likes

WCrihfield
Mentor
Mentor

Hi @Zain_Zarar.  That is not much information.  Can you please be more specific, and include more details about your situation?  If you are talking about trying to use an exact copy of the code I posted nearly 4 years ago, in 'Message 2' above, then that would mean that Line 19 contained the following code:

Dim oDDoc As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, oTPath & "\" & oTName, True)

Within that line of code, a full path and file name are being supplied, telling it the exact file location, and exact file name, for the template drawing file on my computer at that time.  The previous two lines of code specified that values for those two variables being used there.

Dim oTPath As String = ThisApplication.FileOptions.TemplatesPath
Dim oTName As String = "B-SIZE STANDARD DRAWING TEMPLATE.idw"

If that exact file does not exist on your computer at the same file path, and with the same file name, then that line of code will cause an error.  You must either not specify a full file name for the template file, or change it to the one on your own file system that you want to use.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)