View.SetCurrentAsFront - Parts in an Assembly

View.SetCurrentAsFront - Parts in an Assembly

mjordan3
Advocate Advocate
689 Views
6 Replies
Message 1 of 7

View.SetCurrentAsFront - Parts in an Assembly

mjordan3
Advocate
Advocate

I'm trying to write an ilogic routine that will let me do a View.SetCurrentAsFront for all parts in an assembly.  I'd like to "push" the current assembly view to the parts, but can't seem to figure it out.  I've tried

  1. calling ActiveDocument.View.SetCurrentAsFront when the part is active in the assembly (which only seems to work for the assemblyDocument),
  2. ThisApplication.ActiveView.SetCurrentAsFront when the part is active, and
  3. iterating through each partDocument and adding a view, then using SetCurrentAsFront.  This doesn't work as it will open a new windowed view of each part and use the default view (usually the home isometric).

This is my code so far (option 3 is currently in place):

 

Sub Main()

    If Not ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then
        MsgBox("Please run this test code from an assembly file.")
        Exit Sub
    End If

    Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
    Dim docFile As Document
    Dim oViews As Views
    Dim oView As View

    'Uncomment below to change the current view in the assembly document to Front
    'ThisApplication.ActiveView.SetCurrentAsFront

    'get each part document in the assembly
    For Each docFile In oDoc.ReferencedDocuments

        oViews = docFile.Views
 		If oViews.Count = 0 Then
 			oView = oViews.Add
            oView.SetCurrentAsFront
            docFile.Save
            docFile.Close
        Else
            oView = docFile.Views.Item(1)
            oView.SetCurrentAsFront
	   End If
    Next

End Sub

 

In case anyone is wondering why I'm doing this, I'm trying to speedup my part plating.  I tend to use multibody parts a lot and they usually are not oriented to show the proper face to Front after making components; since the Inventor baseView command defaults to Front, this would make life easier.

 

Any help would be greatly appreciated.  Thanks!

0 Likes
690 Views
6 Replies
Replies (6)
Message 2 of 7

YuhanZhang
Autodesk
Autodesk

You can try below VBA code if it is what you want:

 

Sub SetComponentFrontViewSample()
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    TransformOccu oDoc
End Sub

Function TransformOccu(oDoc As AssemblyDocument)
    Dim oMatrix As Matrix
    Set oMatrix = ThisApplication.TransientGeometry.CreateMatrix
    
    Dim oTransform As Matrix
    Dim oTranslation As Vector
    
    Dim oOccu As ComponentOccurrence
    For Each oOccu In oDoc.ComponentDefinition.Occurrences
        Set oTransform = oOccu.Transformation
        
        Set oTranslation = ThisApplication.TransientGeometry.CreateVector(oTransform.Cell(1, 4), oTransform.Cell(2, 4), oTransform.Cell(3, 4))
        
        oMatrix.SetTranslation oTranslation, True
        oOccu.Transformation = oMatrix
        
        If oOccu.SubOccurrences.Count <> 0 Then
            TransformOccu oOccu.Definition.Document
        End If
    Next
End Function


If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 3 of 7

mjordan3
Advocate
Advocate

It looks like you're creating a new matrix that describes the componentOccurrence's position relative to the Assembly's space, I'm not sure what that does though.  I've run this in an assembly and it does not seem to have an effect on the components.  Isn't the ComponentOccurence.Transformation a way to manipulate the componentOccurrence's position in assembly space?

 

I guess the root of my question is: what happens when you run the View.SetCurrentAsFront method on a document?  I want to do the same to componentOccurrences while in an assembly, using the assembly's current view.  Where in the API is the part or assembly's Front "view" defined?

0 Likes
Message 4 of 7

YuhanZhang
Autodesk
Autodesk

The View.SetCurrentAsFront is the same function as the UI function below(Set Current View as->Front):

 

SetFrontView.png

 

 

If you can do it via UI workflow then we can check how we can do it via API.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 5 of 7

mjordan3
Advocate
Advocate

@YuhanZhang wrote:

The View.SetCurrentAsFront is the same function as the UI function below(Set Current View as->Front):

 

Correct.  My issue is that the View.SetCurrentAsFront does not seem to affect componentOccurrence files when it is called from within a parent assembly, even when accessing the componentOccurrence documents or double-clicking thecomponentOccurrence to edit them.

 

While in an assembly, if I run

 

msgBox(ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.Item(1).Definition.Document.Views.Count)

it returns "0".  Since I can't call View.SetCurentAsFront without a view object, I try at add a view to the componentOccurrence.  However if I run

ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.Item(1).Definition.Document.Views.Add

it will open the componentOccurrence file in a new window, at that point the camera is no longer pointing where it was in the assembly.

 

 

  1. Is there another way to access the underlying mechanism that controls the Front/Top/Home View through the API?

 Am I correct in that:

 

    2. the View.SetCurrentAsFront only works in the currently open file, and

    3. there is not a way to push this into componentOccurrences from an assembly view?

 

 

0 Likes
Message 6 of 7

YuhanZhang
Autodesk
Autodesk

When you in-place edit an assembly the View is still related with the top assembly but not the part/sub-assembly, so View.SetCurrentAsFront still sets it for top assembly.  But here I still can't catch you, what's your purpose? Why can't you open the occurrence in its own View to set the view cube orientation?



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 7 of 7

mjordan3
Advocate
Advocate

Thank you for your help on this.

 

I am currently opening each part and setting the "Front" manually.  This takes a long time when the assembly is made up of a lot of parts, especially when the components are generated from multibody parts.  The example I use in the attached screencast is simple, as most of the parts can use the same view.  But often my work will have parts that cannot share a common view.

 

For example, my last project had 63 cnc router parts - all generated from multibody files.  If we can edit the part views from an assembly (all at once), it saves our designers a lot of time.

 

Ultimately I am writing a routine to generate CNC drawing views automatically, and being able to control the "Front View" in the parts allows me to code that script consistently.  I've attached a screencast that details how I want to use this.

 

 

0 Likes