Automatic view scale and position rule from center point

Automatic view scale and position rule from center point

Anthony5375F
Contributor Contributor
429 Views
3 Replies
Message 1 of 4

Automatic view scale and position rule from center point

Anthony5375F
Contributor
Contributor

Good afternoon, 

 

I have two codes, one that re-scales and the other that re-positions the drawing view. 
Currently if i use the drawing as a template, the drawings vary in size, therefore position and scale sometimes does not work. Could i merge these two codes and rather than position from bottom left corner, position from center point? 

Alternatively, look at a better solution? 

Here are the two codes along with an attached spreadsheet (Some cells aren't complete yet) as wanted to create a  sheet that controls multiple rules. 

Sub Main
	Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
	
	''' --------------------------MAGIC DICTIONARY CODE----------------------------------- '''
	
	' Everything in a Comma Separated Value (.csv) is separated, funnily enough, by a comma
	
	' FILE CAN'T BE OPEN!

	
	' Opening CSV file
	' Read lines separately


	' A typical variable line is going to look like this: (10-10, 20-20, 30-30, 10 etc )
	
	Dim splitChar As String = ","
        Dim csvData As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)()

        Using reader As New IO.StreamReader("C:\Users\anthony\Desktop\scale_mapping.csv", Text.Encoding.Default)
            Dim line As String = reader.ReadLine
			Dim Index As Integer
			Index = 1
            Do While (Not line Is Nothing) And (Index < oDoc.Sheets.Count)
                Try
                    csvData.Add(Index, line)
                Catch ex As Exception
                    MsgBox("Could not add data from line: " & line)
                End Try
                line = reader.ReadLine
				Index = Index+1
            Loop
        End Using
	
	''' -------------------------------------------------- '''
	

	For i As Integer = 1 To oDoc.Sheets.Count ' Go through every sheet in document
		oDoc.Sheets(i).Activate ' Activate the sheet
		Dim oSheet As Sheet = oDoc.ActiveSheet ' Reference active sheet
		
		
		Dim index As Integer = 4 ' Way to map what view you're on vs the list above
		For Each oView In oSheet.DrawingViews
			' Load the point from our dictionary we made with the csv file
			Dim rowData As String()
			rowData = csvData.Item(i+1).Split(",")
			' This means we get an array of variables like '10-10' etc. We need to split this later using the "-" character	
				
				
			Dim oScale As Double
			oScale = rowData(index) ' Map current view in loop onto list of numbers above
			Try
				ReScale_View(oView, oScale, oScale) ' Scale accordingly
			Catch
				Logger.Error(oSheet.Name & " has a problematic drawing view.")	
			End Try
			index = index + 1 ' Add to the index to go to next number
		Next
'		Try
'			Call RepositionDrawingViewLabels
'		Catch
'			Logger.Error("Error in reposition on " & oDoc.ActiveSheet.Name)
'		End Try
	Next
End Sub

Public Sub ReScale_View (aView As DrawingView, X As Double, Y As Double)

Dim XOrg As Double = aView.Width / aView.Scale

Dim YOrg As Double = aView.Height / aView.Scale

Dim XScale As Double = X / XOrg
Dim YScale As Double = Y / YOrg
aView.Scale = Math.Min(XScale, YScale)

End Sub

 

Sub Main
	Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
	
	''' --------------------------MAGIC DICTIONARY CODE----------------------------------- '''
	
	' Everything in a Comma Separated Value (.csv) is separated, funnily enough, by a comma

	
	' Opening CSV file
	' Read lines separately


	' A typical variable line is going to look like this: (10-10, 20-20, 30-30, 10 etc )
	
	Dim splitChar As String = ","
        Dim csvData As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)()

        Using reader As New IO.StreamReader("C:\Users\anthony\Desktop\scale_mapping.csv", Text.Encoding.Default)
            Dim line As String = reader.ReadLine
			Dim Index As Integer
			Index = 1
            Do While (Not line Is Nothing) And (Index < oDoc.Sheets.Count)
                Try
                    csvData.Add(Index, line)
                Catch ex As Exception
                    MsgBox("Could not add data from line: " & line)
                End Try
                line = reader.ReadLine
				Index = Index+1
            Loop
        End Using


	
	''' -------------------------------------------------- '''
	
	For i As Integer = 1 To oDoc.Sheets.Count ' Go through every sheet in document
		oDoc.Sheets(i).Activate ' Activate the sheet
		Dim oSheet As Sheet = oDoc.ActiveSheet ' Reference active sheet
		
		
		Dim index As Integer = 1 ' Way to map what view you're on vs the list above
		For Each oView In oSheet.DrawingViews
			'Try					
				oBasePosition = ThisApplication.TransientGeometry.CreatePoint2d() ' Created a point (nominally a Vector) e.g. Vector2d(14,40) 
				
				' Load the point from our dictionary we made with the csv file
				Dim rowData As String()
				rowData = csvData.Item(i+1).Split(",")
				' This means we get an array of variables like '10-10' etc. We need to split this later using the "-" character
				
				
				' Cursed string coercion magic, abandon hope all ye who enter here
				Dim xPos As Integer
				Dim yPos As Integer				
				xPos = CInt(rowData(index).Split("|")(0))
				yPos = CInt(rowData(index).Split("|")(1))
				' Terrible code ends... for now
				
				
				
				oBasePosition.X = (xPos/10) + (oView.Width/2)
			    oBasePosition.Y = (yPos/10) + (oView.Height/2)
				Reposition_View(oView, oBasePosition) ' Scale accordingly - passing oView and oBasePosition to the function below
				
			'Catch
			'	Logger.Error("Cannot place drawing " & oView.ToString & " on Sheet " & oSheet.Name)	
			'End Try
			index = index + 1 ' Add to the index to go to next number
		Next
'		Try
'			Call RepositionDrawingViewLabels
'		Catch
'			Logger.Error("Error in reposition on " & oDoc.ActiveSheet.Name)
'		End Try
	Next
End Sub

Public Sub Reposition_View (aView As DrawingView, aPos As Point2d)

    'Move the baseview to the new position

	'Logger.Info("Moving " & aView.Name & "  from " & aView.Position.ToString & " to " & aPos.ToString)
    aView.Position = aPos
   

    ' Ensure that the geometry in this view will not move if new geometry is added (and the view size changes as a result)

    ' Note this doesn't prevent a user dragging the view around!

	Try
   		aView.ViewJustification = ViewJustificationEnum.kFixedViewJustification
	Catch
		Logger.Error("Cannot place " & aView.Name)
	End Try


End Sub
0 Likes
430 Views
3 Replies
Replies (3)
Message 2 of 4

JelteDeJong
Mentor
Mentor

try it like this:

Sub Main()

	Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument

	Dim csvData As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)()

	Using reader As New IO.StreamReader("D:\Downloads\scale_mapping.csv", Text.Encoding.Default)
		Dim line As String = reader.ReadLine
		Dim Index As Integer
		Index = 1
		Do While (Not line Is Nothing)
			Try
				csvData.Add(Index, line)
			Catch ex As Exception
				MsgBox("Could not add data from line: " & line)
			End Try
			line = reader.ReadLine
			Index = Index + 1
		Loop
	End Using

	''' -------------------------------------------------- '''


	For i As Integer = 1 To oDoc.Sheets.Count ' Go through every sheet in document
		Dim oSheet As Sheet = oDoc.Sheets(i)
		oSheet.Activate() ' Activate the sheet

		Dim rowData As String() = csvData.Item(i + 1).Split(",")


		For j = 1 To 3
			Dim oView As DrawingView = oSheet.DrawingViews.Item(j)

			' --- Set position ---
			Dim cellPos = rowData(j)

			Dim xPos As Integer = CInt(cellPos.Split("|")(0))
			Dim yPos As Integer = CInt(cellPos.Split("|")(1))

			Dim oBasePosition = ThisApplication.TransientGeometry.CreatePoint2d() ' Created a point (nominally a Vector) e.g. Vector2d(14,40)
			oBasePosition.X = (xPos / 10) + (oView.Width / 2)
			oBasePosition.Y = (yPos / 10) + (oView.Height / 2)
			Reposition_View(oView, oBasePosition) ' Scale accordingly - passing oView and oBasePosition to the function below



			' --- Set scale ---
			Dim cellScale = rowData(j + 3)

			ReScale_View(oView, cellScale, cellScale) ' Scale accordingly

		Next

	Next
End Sub

Public Sub ReScale_View(aView As DrawingView, X As Double, Y As Double)

	If (aView.ScaleFromBase) Then Return

	Dim XOrg As Double = aView.Width / aView.Scale
	Dim YOrg As Double = aView.Height / aView.Scale

	Dim XScale As Double = X / XOrg
	Dim YScale As Double = Y / YOrg

	aView.Scale = Math.Min(XScale, YScale)

End Sub


Public Sub Reposition_View(aView As DrawingView, aPos As Point2d)

	aView.Position = aPos

	Try
		aView.ViewJustification = ViewJustificationEnum.kFixedViewJustification
	Catch
		Logger.Info("Cannot place " & aView.Name)
	End Try

End Sub

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 4

Anthony5375F
Contributor
Contributor

Thank you for replying.

 

It seems to stop on the second page (there are 17) altering page 1 scale

 

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.DrawingViews.get_Item(Int32 Index)
at ThisRule.Main() in rule: Combined filter/scale, in document Wine cellar.dwg:line 33
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Works perfectly on the first page though! 

0 Likes
Message 4 of 4

Anthony5375F
Contributor
Contributor

Error on line 33 in rule: Combined filter/scale, in document: Wine cellar.dwg

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

0 Likes