Message 1 of 4
Automatic view scale and position rule from center point
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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