VBA Assembly Baseview on specific plane of part

VBA Assembly Baseview on specific plane of part

sam
Advocate Advocate
710 Views
6 Replies
Message 1 of 7

VBA Assembly Baseview on specific plane of part

sam
Advocate
Advocate

Hi, 


I am trying to create base views of an assembly through VBA and the views needs to be looking on work planes of one of the part of the assembly. Those planes are on different angles from origin planes, depending on angle entries on configurator. I have tried searching forum but not with much luck. Any help in this regards in highly appreciated. 

Similar question has been asked before as well but not with any solution. 


https://forums.autodesk.com/t5/inventor-customization/change-baseview-with-custom-view-orientation/m...

Thanks in advance, 
regards, 
sam

0 Likes
Accepted solutions (2)
711 Views
6 Replies
Replies (6)
Message 2 of 7

aelqabbany
Advocate
Advocate

Hi @sam, I am currently working on a similar script to that which you mentioned.

 

The way I do it is by finding the largest face of a part (as my reference), then rotating the part by specific angles, setting those views as my front view, and placing that front view on my drawing.

 

@Ralf_Krieg's code here is a brilliant starting point.

 

If you can upload your test part, and a picture of the views that you want to be shown, that will help me (and others) help you.

0 Likes
Message 3 of 7

sam
Advocate
Advocate

Thanks for quick reply, Though in my case the parts are circular tubes with few holes so I am forced to follow the work planes. 

0 Likes
Message 4 of 7

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

So we'll try to fit your needs. 🙂

To test:

- SAVE your work

- open your assembly, your drawing and close all other unused files

- run the AddOrientedBaseViews Sub

- select the part occurrence in your assembly when prompted

- wait until finished

 

 

The WorkPlanes must be named "ViewPlane1", "ViewPlane2", "ViewPlane3" and so on. The macro looks for the first 9 chars in name. We need this to filter out work planes used for other things. I've marked the position in code if you would like to use another name. Also adapt the number of chars in the same line to your name.

 

 

Option Explicit

Private myAssDoc As AssemblyDocument
Private myDrawDoc As DrawingDocument
Private oAssView As View
Private oDrawView As View

Public Sub AddOrientedBaseViews()

    Dim oApp As Inventor.Application
    Set oApp = ThisApplication

    If oApp.Documents.Count = 0 Then
        Call MsgBox("Need a drawing and the assembly open.", vbCritical)
        Exit Sub
    End If
    
    If oApp.Views.Count > 2 Then
        Call MsgBox("Please have only your drawing and the assembly open.", vbCritical)
        Exit Sub
    End If
    
    If Not oApp.ActiveDocument.DocumentType = kDrawingDocumentObject Then
        Call MsgBox("Need a drawing document active.", vbCritical)
        Exit Sub
    End If
    
    Dim oView As View
    For Each oView In oApp.Views
        If oView.Document.DocumentType = kAssemblyDocumentObject Then
            Set myAssDoc = oView.Document
            Set oAssView = oView
            Call oAssView.Activate
        ElseIf oView.Document.DocumentType = kDrawingDocumentObject Then
            Set oDrawView = oView
            Set myDrawDoc = oView.Document
        End If
    Next
    
    Call RotateAssy
    
    MsgBox "Done"
    
End Sub
Private Sub RotateAssy()
    
    Dim oAssDoc As AssemblyDocument
    Set oAssDoc = myAssDoc
    
    Dim oCompOcc As ComponentOccurrence
    Set oCompOcc = ThisApplication.CommandManager.Pick(kAssemblyLeafOccurrenceFilter, "Select the Part with ViewPlanes.")
    If oCompOcc.DefinitionDocumentType = kPartDocumentObject Then
        Call oDrawView.Activate
        Call ProcessOccs(oCompOcc)
    End If
    
End Sub

Private Sub ProcessOccs(ByVal oCompOcc As ComponentOccurrence)
    'Ignore pattern elements
    If oCompOcc.IsPatternElement = True Then Exit Sub
    
    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oCompOcc.Definition
    
    Dim iPosIndex As Integer
    Dim oWorkPlane As WorkPlane
    
    For Each oWorkPlane In oCompDef.WorkPlanes
        If Left(oWorkPlane.Name, 9) = "ViewPlane" Then '<---------------------------------process only named work planes where first 9 chars are "ViewPlane"
            Dim oWorkPlaneProxy As WorkPlaneProxy
            Call oCompOcc.CreateGeometryProxy(oWorkPlane, oWorkPlaneProxy)
            
            If Not oWorkPlaneProxy Is Nothing Then
                Dim oCamera As camera
                Set oCamera = PlaneToFront(oWorkPlaneProxy)
                If Not oCamera Is Nothing Then
                    Call AddOrientedBaseView(oCamera, oWorkPlane.Name, iPosIndex)
                    If iPosIndex < 5 Then
                        iPosIndex = iPosIndex + 1
                    End If
                End If
            End If
        End If
    Next
    
End Sub


Private Function PlaneToFront(ByVal oWorkPlaneProxy As WorkPlaneProxy) As camera
    
    Dim oWorkPlaneNormal As UnitVector
    Set oWorkPlaneNormal = oWorkPlaneProxy.Plane.Normal
    
    Dim oOrigin As Point
    Dim oXAxis As UnitVector
    Dim oYAxis As UnitVector
    
    Call oWorkPlaneProxy.GetPosition(oOrigin, oXAxis, oYAxis)
    
    Dim oCamera As camera
    Set oCamera = oAssView.camera
    
    oCamera.Target = oOrigin
    
    Dim oEye As Point
    Set oEye = oCamera.Target
    
    Call oEye.TranslateBy(oWorkPlaneNormal.AsVector)
    oCamera.Eye = oEye
    
    Call oCamera.ApplyWithoutTransition
    
    'Dim i As Integer
    'For i = 1 To 100
    '    Sleep 10
    '    DoEvents
    'Next
    
    Set PlaneToFront = oCamera
End Function

Private Sub AddOrientedBaseView(ByVal oCamera As camera, ByVal sWorkPlaneName As String, ByVal iPosIndex As Integer)
    
    Dim oApp As Inventor.Application
    Set oApp = ThisApplication
    
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = myDrawDoc
    
    Dim oAssDoc As AssemblyDocument
    Set oAssDoc = myAssDoc
    
    Dim oSheet As Sheet
    Set oSheet = oDrawDoc.ActiveSheet
    
    Dim aPos(5, 1) As Integer
    aPos(0, 0) = 1
    aPos(0, 1) = 3
    aPos(1, 0) = 3
    aPos(1, 1) = 3
    aPos(2, 0) = 5
    aPos(2, 1) = 3
    aPos(3, 0) = 1
    aPos(3, 1) = 1
    aPos(4, 0) = 3
    aPos(4, 1) = 1
    aPos(5, 0) = 5
    aPos(5, 1) = 1
    
    Dim oPos As Point2d
    Set oPos = oApp.TransientGeometry.CreatePoint2d(oSheet.Width / 6 * aPos(iPosIndex, 0), oSheet.Height / 4 * aPos(iPosIndex, 1))
    
    Dim oScale As Double
    oScale = 0.2 '<---------------------------------------------------------------------------Scale factor 1:5
    
    Dim oDrawView As DrawingView
    Set oDrawView = oSheet.DrawingViews.AddBaseView(oAssDoc, oPos, oScale, kArbitraryViewOrientation, kHiddenLineRemovedDrawingViewStyle, , oCamera)
    
    oDrawView.Label.FormattedText = sWorkPlaneName
    oDrawView.ShowLabel = True
End Sub

 

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 5 of 7

sam
Advocate
Advocate

Hi @Ralf_Krieg thank you so much. This worked really as intended though there are some rotation issues which I will try to sort myself first while understanding the code completely and I might ask you some question later. Thanks again. 

0 Likes
Message 6 of 7

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

You can add in Sub PlaneToFront a defined upVector for the Camera object to rotate the view. For example to use the x-Axis vector of the WorkPlaneProxy add

oCamera.upVector = oXAxis

before the line

Call oCamera.ApplyWithoutTransition

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 7 of 7

sam
Advocate
Advocate

@Ralf_Krieg Thank you.

0 Likes