Friday, 21 July 2017

Updated - Align Drawing Views

Hi

So I was actually using the code, and I discovered a small error.
The Horizontal "Top" and "Bottom" alignments were swapped around.

This has been fixed below. (Note, I have also amended the previous post as well.)

I have also added the ability to align the drawing view labels. There is one small addition though, there is a prompt to change the default location of the labels, the values concerned are really small, so work carefully. Naturally you can remove it from the code if it bugs you. (I personally have a need to move the labels around a bit)

Anyway the updated code:



'Reg Hasell
'18/07/2017
'This will align the selected views on the drawings sheet
'A minimum of two views must be selected.
'The first view selected will be the Anchor. (Base View)
'A section of the code was taken from the forum
'Thanks to Adrian Salariu, I stole the First view Co-Ordinates from him.
'I then changed everything to suit my personal needs.
'Revisions:
'Rev 1.1
'20/07/2017
'The Horizontal alignement was reversed
'Added the ability to align the view labels.


Sub Main()
Dim oSSet As SelectSet = ThisDoc.Document.SelectSet
If oSSet.count <2 Then
    MessageBox.Show("A Minimum of two views must be seleted first", "iLogic")
    Return
End If
oMenu()
End Sub

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

Sub oMenu()
Dim oResu1t As String="Result"
Dim oLst As New ArrayList
oLst.Add("Vert - Left")
oLst.Add("Vert - Right")
oLst.Add("Vert - CL")
oLst.Add("")
oLst.Add("Hor - Top")
oLst.Add("Hor - Bottom")
oLst.Add("Hor - CL")
oLst.Add("")
oLst.Add("Align Labels")

'Display table and get the selection
oResu1t=InputListBox("Select Alignment", oLst, oResu1t, _
Title := "View Alignment", ListName := "View Alignment")

'Set iProperties based on the selection.
If oResu1t="Vert - Left" Then
Call oVert_Left()
ElseIf oResu1t="Vert - Right" Then
Call oVert_Right()
ElseIf oResu1t="Vert - CL" Then
Call oVert_CL()
ElseIf oResu1t="Hor - Top" Then
Call oHor_Top()
ElseIf oResu1t="Hor - Bottom" Then
Call oHor_Bott()
ElseIf oResu1t="Hor - CL" Then
Call oHor_CL()
ElseIf oResu1t="Align Labels" Then
Call Align_Labels()
End If
End Sub

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

Sub oVert_Left()
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oView As DrawingView
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
Dim trans As Transaction
trans = ThisApplication.TransactionManager.StartTransaction(oDrawDoc, "Align Views")
Dim oSSet As SelectSet = oDrawDoc.SelectSet
'For Each oView In oSSet
        Dim oFirstView As DrawingView = TryCast(oSSet.item(1), DrawingView)
       'views are processed by center so we need to get center X and substract half view
        Dim XPos As Double
        XPos = oFirstView.Position.X - (oFirstView.Width/2)
        Dim oPoint2D As Inventor.Point2D
        'view position Is XPos + half Of Each view
 For Each oView In oSSet
    'For Each oView In oSheet.DrawingViews
           oPoint2D = ThisApplication.TransientGeometry.CreatePoint2D(XPos + (oView.Width/2),oView.Position.Y)
           oView.Position = oPoint2D
        Next
       
'finish the transaction
trans.End
End Sub

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

Sub oVert_Right()
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oView As DrawingView
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
Dim trans As Transaction
trans = ThisApplication.TransactionManager.StartTransaction(oDrawDoc, "Align Views")
Dim oSSet As SelectSet = oDrawDoc.SelectSet
'For Each oView In oSSet
        Dim oFirstView As DrawingView = TryCast(oSSet.item(1), DrawingView)
        'views are processed by center so we need to get center X and substract half view
        Dim XPos As Double
        XPos = oFirstView.Position.X + (oFirstView.Width/2)
        Dim oPoint2D As Inventor.Point2D
        'view position Is XPos + half Of Each view
 For Each oView In oSSet
    'For Each oView In oSheet.DrawingViews
        oPoint2D = ThisApplication.TransientGeometry.CreatePoint2D(XPos - (oView.Width/2),oView.Position.Y)
           oView.Position = oPoint2D
        Next
       
'finish the transaction
trans.End
End Sub

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

Sub oVert_CL()
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oView As DrawingView
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
Dim trans As Transaction
trans = ThisApplication.TransactionManager.StartTransaction(oDrawDoc, "Align Views")
Dim oSSet As SelectSet = oDrawDoc.SelectSet
'For Each oView In oSSet
        Dim oFirstView As DrawingView = TryCast(oSSet.item(1), DrawingView)
        'views are processed by center so we need to get center X and substract half view
        Dim XPos As Double
        XPos = oFirstView.Position.X
        Dim oPoint2D As Inventor.Point2D

         For Each oView In oSSet
    'For Each oView In oSheet.DrawingViews
            oPoint2D = ThisApplication.TransientGeometry.CreatePoint2D(XPos,oView.Position.Y)
            oView.Position = oPoint2D
        Next
       
'finish the transaction
trans.End
End Sub

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

Sub oHor_Top()
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oView As DrawingView
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
Dim trans As Transaction
trans = ThisApplication.TransactionManager.StartTransaction(oDrawDoc, "Align Views")
Dim oSSet As SelectSet = oDrawDoc.SelectSet
'For Each oView In oSSet
        Dim oFirstView As DrawingView = TryCast(oSSet.item(1), DrawingView)
        'views are processed by center so we need to get center X and substract half view
        'Dim XPos As Double
        Dim YPos As Double
        'XPos = oFirstView.Position.X + (oFirstView.Width/2)
        YPos = oFirstView.Position.Y + (oFirstView.Height/2)
        Dim oPoint2D As Inventor.Point2D

         For Each oView In oSSet
    'For Each oView In oSheet.DrawingViews
            oPoint2D = ThisApplication.TransientGeometry.CreatePoint2D(oView.Position.X,YPos - (oView.Height/2))
            oView.Position = oPoint2D
        Next
       
'finish the transaction
trans.End
End Sub

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

Sub oHor_Bott()
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oView As DrawingView
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
Dim trans As Transaction
trans = ThisApplication.TransactionManager.StartTransaction(oDrawDoc, "Align Views")
Dim oSSet As SelectSet = oDrawDoc.SelectSet
'For Each oView In oSSet
        Dim oFirstView As DrawingView = TryCast(oSSet.item(1), DrawingView)
       'views are processed by center so we need to get center X and substract half view

        'Dim XPos As Double
        Dim YPos As Double
        'XPos = oFirstView.Position.X - (oFirstView.Width/2)
        YPos = oFirstView.Position.Y - (oFirstView.Height/2)
        Dim oPoint2D As Inventor.Point2D

         For Each oView In oSSet
    'For Each oView In oSheet.DrawingViews
            oPoint2D = ThisApplication.TransientGeometry.CreatePoint2D(oView.Position.X,YPos + (oView.Height/2))
           oView.Position = oPoint2D
        Next
       
'finish the transaction
trans.End
End Sub

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

Sub oHor_CL()
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oView As DrawingView
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
Dim trans As Transaction
trans = ThisApplication.TransactionManager.StartTransaction(oDrawDoc, "Align Views")
Dim oSSet As SelectSet = oDrawDoc.SelectSet
'For Each oView In oSSet
        Dim oFirstView As DrawingView = TryCast(oSSet.item(1), DrawingView)
        'views are processed by center so we need to get center X and substract half view
        Dim XPos As Double
        Dim YPos As Double
            XPos = oFirstView.Position.X + (oFirstView.Width)
            YPos = oFirstView.Position.Y
        Dim oPoint2D As Inventor.Point2D

         For Each oView In oSSet
    'For Each oView In oSheet.DrawingViews
            oPoint2D = ThisApplication.TransientGeometry.CreatePoint2D(oView.Position.X,YPos)
           oView.Position = oPoint2D
        Next
       
'finish the transaction
trans.End
End Sub

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

Sub Align_Labels()
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oView As DrawingView
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
Dim trans As Transaction
trans = ThisApplication.TransactionManager.StartTransaction(oDrawDoc, "Align Views")
Dim oSSet As SelectSet = oDrawDoc.SelectSet
'
        Dim oNewPosition As Point2d
        oNewPOS = InputBox("Add Offset?", "Title", 1)
        oNewPOS /= 10

         For Each oView In oSSet
          'oNewPosition = oView.Label.Position
          oNewPosition = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X, oView.Center.Y - (oView.Height / 2)-( 1+oNewPOS))
          ' oNewPosition = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X, oView.Center.Y - (oView.Height / 2)-1)
           oView.Label.Position = oNewPosition
        Next
       
'finish the transaction
trans.End
End Sub