Automation for sheet usage

Automation for sheet usage

bogholderiE4HL4
Explorer Explorer
71 Views
1 Reply
Message 1 of 2

Automation for sheet usage

bogholderiE4HL4
Explorer
Explorer

Hello

I'm looking for a way to add 3 assemblies onto a sheet, and 1 representation file - .ipn.
The 3 assemblies, would be seperated by two different representations, "No Walls" and "Open".
I would like to be able to rotate them aswell and add a scaling to them.
This kinda works fine with some help from ChatGPT and Claude, but im having trouble placing them correctly and dynamically on the sheet.

I would like the same margins/spacing always, eventho im changing the scaling.

So my question is, how can i place them correctly on the sheet?
On top of that, i would like to rotate the views, so its the correct view - when placed.

It would be superb, if i would be able to make custom view orientations aswell, but this i can do manually - this is recommended by chatGPT.

The code:

Some helplines - written in danish, my native language (From ChatGPT)

Sub Main()
' ---------------------------
' Aktiv tegning
' ---------------------------
Dim oDrawDoc As DrawingDocument = ThisDoc.Document
Dim oSheet As Sheet = oDrawDoc.Sheets(1)

' ---------------------------
' Mappe og filnavn
' ---------------------------
Dim basePath As String = "C:\Users\OneDrive\Documents"
Dim fileName As String = "4195-3 Toilet"

Dim asmPath As String = basePath & fileName & ".iam"
Dim ipnPath As String = basePath & fileName & ".ipn"

' ---------------------------
' Vælg skala (fri input)
' ---------------------------
Dim userScale As String
userScale = InputBox("Indtast skala (fx 1:5, 1:10, 1:20, 1:26):", "Skala", "1:20")

Dim scale As Double
Try
scale = 1 / CDbl(userScale.Replace("1:", "").Trim())
Catch ex As Exception
MsgBox("Ugyldig skala – standard = 1:20")
scale = 0.05
End Try

' ---------------------------
' Vælg orientering eller Custom View
' ---------------------------
Dim userView As String
userView = InputBox("Vælg orientering (Front, Right, Left, Iso eller Custom):", "View Selection", "Iso")

Dim viewType As ViewOrientationTypeEnum
Dim customViewName As String = ""

Select Case LCase(userView)
Case "front": viewType = ViewOrientationTypeEnum.kFrontViewOrientation
Case "right": viewType = ViewOrientationTypeEnum.kRightViewOrientation
Case "left": viewType = ViewOrientationTypeEnum.kLeftViewOrientation
Case "iso": viewType = ViewOrientationTypeEnum.kIsoTopRightViewOrientation
Case "custom" Case Else
MsgBox("Ugyldigt valg – standard = Iso")
viewType = ViewOrientationTypeEnum.kIsoTopRightViewOrientation
End Select

' ---------------------------
' Åbn assembly dokument
' ---------------------------
Dim oAsmDoc As Document = ThisApplication.Documents.Open(asmPath, False)

' ---------------------------
' Tilføj IAM view "No Walls" - ØVERST VENSTRE
' ---------------------------
Dim oAsmView As DrawingView
oAsmView = oSheet.DrawingViews.AddBaseView(oAsmDoc, ThisApplication.TransientGeometry.CreatePoint2d(10, 20), scale, viewType, DrawingViewStyleEnum.kShadedDrawingViewStyle, "No Walls")
If customViewName <> "" Then oAsmView.ReferencedDocumentDescriptor.RepresentationViewName = customViewName

' Rotation IAM
Dim userRotationIAM As String
userRotationIAM = InputBox("Rotation for IAM 'No Walls' view (0, 90L, 90R, 180, None):", "Rotation IAM", "0")
Dim rotIAM As Double = 0
Select Case LCase(userRotationIAM)
Case "0": rotIAM = 0
Case "90l": rotIAM = -Math.PI / 2
Case "90r": rotIAM = Math.PI / 2
Case "180": rotIAM = Math.PI
Case "none": rotIAM = 0
Case Else: rotIAM = 0
End Select
oAsmView.RotateByAngle(rotIAM)

' ---------------------------
' Tilføj IAM view "Open" - ØVERST HØJRE
' ---------------------------
Dim oOpenView As DrawingView
oOpenView = oSheet.DrawingViews.AddBaseView(oAsmDoc, ThisApplication.TransientGeometry.CreatePoint2d(30, 20), scale, viewType, DrawingViewStyleEnum.kShadedDrawingViewStyle, "Open")
If customViewName <> "" Then oOpenView.ReferencedDocumentDescriptor.RepresentationViewName = customViewName

userRotationIAM = InputBox("Rotation for IAM 'Open' view (0, 90L, 90R, 180, None):", "Rotation IAM", "0")
Select Case LCase(userRotationIAM)
Case "0": rotIAM = 0
Case "90l": rotIAM = -Math.PI / 2
Case "90r": rotIAM = Math.PI / 2
Case "180": rotIAM = Math.PI
Case "none": rotIAM = 0
Case Else: rotIAM = 0
End Select
oOpenView.RotateByAngle(rotIAM)

' ---------------------------
' Åbn presentation dokument
' ---------------------------
Dim oIpnDoc As Document = ThisApplication.Documents.Open(ipnPath, False)

' Tilføj IPN view - NEDERST HØJRE
Dim oIpnView As DrawingView
oIpnView = oSheet.DrawingViews.AddBaseView(oIpnDoc, ThisApplication.TransientGeometry.CreatePoint2d(30, 10), scale, viewType, DrawingViewStyleEnum.kShadedDrawingViewStyle)
If customViewName <> "" Then oIpnView.ReferencedDocumentDescriptor.RepresentationViewName = customViewName

' Rotation IPN
Dim userRotationIPN As String
userRotationIPN = InputBox("Rotation for IPN view (0, 90L, 90R, 180, None):", "Rotation IPN", "0")
Dim rotIPN As Double = 0
Select Case LCase(userRotationIPN)
Case "0": rotIPN = 0
Case "90l": rotIPN = -Math.PI / 2
Case "90r": rotIPN = Math.PI / 2
Case "180": rotIPN = Math.PI
Case "none": rotIPN = 0
Case Else: rotIPN = 0
End Select
oIpnView.RotateByAngle(rotIPN)

' ---------------------------
' Opdater tegningen
' ---------------------------
oDrawDoc.Update()
MsgBox("Views indsat med Custom View og rotation!")
End Sub

 

 

0 Likes
72 Views
1 Reply
Reply (1)
Message 2 of 2

J_Pfeifer_
Advocate
Advocate

When it comes to assigning assemblies or whatnot as a view on any one of the pages. You can create a template that when opened. Asks the users to supply an assembly or part document. This then would create a standard view at that scale for that item. Example, you have 3 sheets with one view each. If you specify the view when creating the template. When you save that template, then create a new part. You can specify the three views by selecting the proper document manually. 

 

This is how I have my templates built. Particularly the first page. We have 4 isometric views on the first page of our drawing documents. When a template is opened, it allows the users to specify the main assembly. From here it does a bunch of stuff, but the one pertaining to you is the positioning and scaling of these four views. We can have items 36" dia @ 96" high. Then have things 14 ft in diameter, at over 30 ft tall. Scaling these views was critical. This is an old code Piece that I have in the program that I have not yet rewritten (Looks like crap) (See below for a hard coded example)

Sub ModifyPageScale()
		
		Dim Version As String = iProperties.Value("Project", "Revision Number")
		Dim JobNo As String = iProperties.Value("Project", "Project")
	
	Try	
		Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
		Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
		Dim oScale As Double = .25 / 12
		
		Dim oPage1 As Sheet = oDoc.Sheets.Item(1)
	
		Dim oPageOneView1 As DrawingView
		Dim oPageOneView2 As DrawingView
		Dim oPageOneView3 As DrawingView 
		Dim oPageOneView4 As DrawingView
	
		Try
			oPageOneView1 = oPage1.DrawingViews.Item(4) 'VIEW AT 135 DEG - View 1
			oPageOneView2 = oPage1.DrawingViews.Item(3) 'VIEW AT 45 DEG - View 2
			oPageOneView3 = oPage1.DrawingViews.Item(2) 'VIEW AT 315 DEG - View 3
			oPageOneView4 = oPage1.DrawingViews.Item(1) 'VIEW AT 225 DEG - View 4
		Catch ex As Exception
			MessageBox.Show("Problem at document creation, assembly, part, or otherwise was not selected correctly at start up." * vbCrLf & "Please close this document and restart" & vbCrLf & "Exitting startup program.")
			Exit Sub
		End Try
		
		oPageOneView1.Scale = oScale
		oPageOneView2.Scale = oScale
		oPageOneView3.Scale = oScale
		oPageOneView4.Scale = oScale
		
		'MessageBox.Show("Reset scale for tesing.")
		
		Dim ViewStyle As DrawingViewStyleEnum = DrawingViewStyleEnum.kHiddenLineDrawingViewStyle
		
		Dim Box1Point As Point2d = oTG.CreatePoint2d(.635, .635)
		Dim Box2Point As Point2d = oTG.CreatePoint2d(33, 13)
		
		Dim oPage1Box As Box2d = oTG.CreateBox2d()
		oPage1Box.MinPoint = Box1Point
		oPage1Box.MaxPoint = Box2Point
		
		Dim oPage1BoxWidth As Double = Box2Point.X - Box1Point.X
		Dim oPage1BoxHeight As Double = Box2Point.Y - Box1Point.Y
		
		'MessageBox.Show(oPage1BoxWidth.ToString & " " & oPage1BoxHeight.ToString) 'This checks the size of the x and y of the box object
		
		Dim oBox1YHeight As Double = oPage1BoxHeight / 2 + .635 'This variable represents the distance from center to bottom of page for page one view placement
		Dim oBox1XWidthStart As Double = (oPage1BoxWidth / 4) / 2 + .635 'This variable represents the distance from left edge of sheet to center start point for first page 1st view placement
		Dim oBox1XWidthCenter As Double = (oPage1BoxWidth / 4) / 2 'This value represents the distance on the x axis for view spacing. 
		
		'Messagebox.Show("The first value should match 6.945 " & oBox1Yheight.ToString & " This second value should match 4.68 " & oBox1XWidthStart.ToString & " The thrid value should match 4.0456 " & oBox1XWidthCenter.ToString) 'This verifies the correct distance values mathmatically. 
		
		'Below establishes the point objects that each view on page one will be attached to upon runtime. 
		Dim PageOneViewOne As Point2d = oTG.CreatePoint2d(oBox1XWidthStart, oBox1YHeight)
		Dim PageOneViewTwo As Point2d = oTG.CreatePoint2d(oBox1XWidthStart + oBox1XWidthCenter * 2, oBox1YHeight)
		Dim PageOneViewThree As Point2d = oTG.CreatePoint2d(oBox1XWidthStart + oBox1XWidthCenter * 4, oBox1YHeight)
		Dim PageOneviewFour As Point2d = oTG.CreatePoint2d(oBox1XWidthStart + oBox1XWidthCenter * 6, oBox1YHeight)
		
		'Start the scaling of each view in sheet one to be smaller than the quadrant of the bounding box. 
		'Using height, and width of the views, we can compare that to a variable established to represent the size of the quadrant they're within. 
		Dim Page1MaxViewWidth As Double = oPage1BoxWidth / 4 ' Should equal 8.091
		Dim Page1MaxViewHeight As Double = oPage1BoxHeight 'Should be the same as the total height of the box, no reason to equate it. 
		
		i = 0
		oScale = oPageOneView1.Scale
		
		'The section below is checking the view size against the created box2d zones in the program. If the height or the width are too large, we scale by 94% and check the values again until the view size is lower than the box2d object
		Do While oPageOneView1.Width > Page1MaxViewWidth Or oPageOneView1.Height > Page1MaxViewHeight
			'
			'MessageBox.Show("Page one width: " & oPageOneView1.Width.ToString & " Page one height: " & oPageOneView1.Height.ToString & vbCrLf & " Page 1 width maximum: " & Page1MaxViewWidth & " Page 1 height maximum: " & Page1MaxViewHeight)
			oScale = oScale * .96
			oPageOneView1.Scale = oScale
			oPageOneView2.Scale = oScale
			oPageOneView3.Scale = oScale
			oPageOneView4.Scale = oScale
		
			'Logger.Info("Loop " & i & " - oScale:" & oScale & " Viewscale: " & oPageOneView1.Scale)
			'Logger.Info("   " & oPageOneView1.Width)
			'Logger.Info("   " & oPageOneView1.Height)
			'Logger.Info("   View Width: " & oPageOneView1.Width)
			'Logger.Info("   View Height: " & oPageOneView1.Height)
		
			'ActiveSheet.NativeEntity.Update
			'ThisDoc.Document.Update
		
			'endless loop killer just in case. This is a file within the C drive that exists to create a stop barrior between an endless loop and restarting the program. 
			'If the file is not found, or has it's name changed. It will enter the if block and exit the sub immediately before the need to crash the software. 
			'oFile = "C:\Temp\Endless Loop Saftey File.txt"
			'If IO.File.Exists("C:\Temp\Endless Loop Saftey File.txt") = False Then
			'	MsgBox("The rule called '" & iLogicVb.RuleName & "' stopped because text file doesn't exist:" & vbLf & oFile, , "iLogic")
			'	Exit Sub
			'End If
			
			
			i = i + 1
		Loop
	Catch ex As Exception
		MessageBox.Show("Please copy the following message into ""Bug reporting"" on teams:" & vbCrLf & vbCrLf & "Drawing template - Updating symbol library." & vbCrLf & _
		"Version: " & Version & vbCrLf & "Job no: " & JobNo & vbCrLf & "Unable to scale page 1 views, please check placement and size. ") 
	End Try
		
End Sub

 

I could write up a whole something to take care of your needs. However, I think the consideration of CAD tools before programing would be greatly beneficial. If you then run into limitations, then program around them. 

 

For you if you want to program this, I would work on an empty drawing and create a rule that can import and place a top view. From here, learn how to change the view or even access other functions. Then you can create a sub that also moves the views across the sheet using a step by step process. 

 

The entire idea I'm passing here, it would be better for you to play around with something smaller in focus and learn what needs to be accessed in order to create the script yourself. Asking Chat gpt to create something vague and large, it will typically lead to more issues than solutions. 

0 Likes