Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic to make a part in a subassembly visible

6 REPLIES 6
Reply
Message 1 of 7
dan_inv09
1661 Views, 6 Replies

iLogic to make a part in a subassembly visible

I've got this bit of iLogic that someone was kind enough to post here, I'm not sure who but I'd like to thank them a lot.

 

It goes through an assembly and creates a representation for each unique part. I'm using it in an assembly with subassemblies and while it is able to dig down into the subassembly to create a view named after each part it is not able to make the part visible.

 

I think somewhere in this bit I need to do something to tell it (or let it?) dig down into the subassembly:

 

'set visible if name matches first occurence
If oCompOcc.Name = ShortName & ":1" Then
oCompOcc.Visible = True
ThisApplication.ActiveView.Update()
Else
oCompOcc.Visible = False
ThisApplication.ActiveView.Update()
End If

 

But I'm not sure. (The most iLogic I've ever done is copying this bit out of an old post and pasting it, and even that wasn't that easy.)

 

Here's the whole thing (if you know where it came from thank the author for me):

 

Spoiler
Spoiler
'create a design view representation for each unique part in the assembly 
'define current documentDim openDoc As Document
openDoc = ThisDoc.Document

'set a reference to the assembly component definintion.
'this assumes an assembly document is open.Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'look at all of the components in the assemblyDim oCompDef As Inventor.ComponentDefinition = openDoc.ComponentDefinition

'define the first level components collectionDim oCompOcc As Inventor.ComponentOccurrence 

'define view rep Dim oViewRep As DesignViewRepresentation

'define an arraylist to hold the list of  view rep namesDim NameList As New ArrayList()

'Look at the view reps in the assemblyFor Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations
'set the list of names to the array listNameList.add(oViewRep.Name)
Next

'check for a Default view rep and create it if not foundIf Not NameList.Contains("Default") Then
    'create Default view rep     oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add("Default") 
    oViewRep.ShowAll
    oViewRep.Activate
End If

'zoom allThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute

'look at all of the unique parts in the assemblyFor Each docFile In openDoc.AllReferencedDocuments
    If docFile.DocumentType = 12290 Then '12290 is the part document enumurator    'locate the last backslash position in the full file name    Dim FNamePos As Long
    FNamePos = InStrRev(docFile.FullFileName, "\", -1) 
    'remove path from part file name         Dim docFName As String
    docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)         
    'remove extension from part file name     ShortName = Left(docFName,  Len(docFName) - 4)   
        'check to see if the arraylist contains the desired view rep        If Not NameList.Contains(ShortName) Then
        'create new View Rep         oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add(ShortName)
        oViewRep.Activate
        oViewRep.Locked = False    
        Else If NameList.Contains(ShortName) Then
        'reference existing View Rep         oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item(ShortName) 
        oViewRep.Activate
        oViewRep.Locked = False
        End If
        'look at all of the occurences        For Each oCompOcc in oCompDef.Occurrences
        'locate the colon position in the occurence name        oCompOccPos = InStrRev(oCompOcc.Name, ":") 
        'set occurence name to everything left of the colon        oOccName = Left(oCompOcc.Name, oCompOccPos -1) 
            'set visible if name matches first occurence              If oCompOcc.Name = ShortName & ":1"  Then
            oCompOcc.Visible = True
            ThisApplication.ActiveView.Update()
            Else
            oCompOcc.Visible = False
            ThisApplication.ActiveView.Update()
            End If
        Next
    End If
    'lock view rep    oViewRep.Locked = True    
Next

'set Default View Rep activeoAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("Default").activate
6 REPLIES 6
Message 2 of 7

The following iLogic rule iterates assembly tree at all levels and hide those components which names contain substring TargetSubstring.

Hope this code could be customized to suit your needs.

 

Sub Main()

    'string to search in component's names
    Dim TargetSubstring As String = "SomeSubName"

    '=======================================================
    ' Set reference to active document.
    ' This assumes the active document is an assembly
    Dim oAsmDoc As Inventor.AssemblyDocument = ThisApplication.ActiveDocument
    ' Get assembly component definition
    Dim oAssCompDef As Inventor.ComponentDefinition = oAsmDoc.ComponentDefinition
	'check if suitable design view represantation is active
    Dim oManager As RepresentationsManager = oAssCompDef.RepresentationsManager
    Dim oRep As DesignViewRepresentation = oManager.ActiveDesignViewRepresentation
    If oRep.DesignViewType <> DesignViewTypeEnum.kPublicDesignViewType Then
        MsgBox("Activate Default or another custom view representation")
        Exit Sub
    End If

    'create object collection for the components found
    Dim oColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection

    ' Get all occurrences from component definition for Assembly document
    Call ProcessOccurrences(oAssCompDef.Occurrences, oColl, TargetSubstring)

	'uncomment these lines for debug purposes
'    If oColl.Count > 0 Then
'        Dim st As String = ""
'        For Each oOcc As ComponentOccurrence In oColl
'            st = st & oOcc.Name & vbNewLine
'        Next
'        st = st & vbNewLine & "Total: " & oColl.Count
'        MsgBox(st)
'    End If

    'change visibility for all components in the collection
    If oColl.Count > 0 Then
        oRep.SetVisibilityOfOccurrences(oColl, False)
    End If

End Sub

' This function is called for processing collection of occurrences.
' It is called recursively to iterate
' through the entire assembly tree.
' Adds components to the collection oColl
Private Sub ProcessOccurrences(ByVal oOccs As ComponentOccurrences, _
                               ByVal oColl As ObjectCollection, _
                               ByRef St As String)
    For Each oOcc As ComponentOccurrence In oOccs
        If oOcc.IsSubstituteOccurrence Then Continue For
        If TypeOf oOcc.Definition Is VirtualComponentDefinition Then Continue For

        'process current component oOcc
        If oOcc.Name.IndexOf(St) >= 0 Then oColl.Add(oOcc)

        ' process subcomponents if they exist
        If oOcc.SubOccurrences.Count > 0 Then
            Call ProcessOccurrences(oOcc.SubOccurrences, oColl, St)
        End If
    Next
End Sub

 cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Message 3 of 7

Thank you.

 

As I said before, the extent of my knowledge of iLogic is I copied some code out of a forum post so I'm not really sure what I'm looking at or what I'm supposed to do with it.

 

But from what I can figure out it looks like it's hiding things. I have no problem hiding things - I don't need to dig down into the sub assemblies to hide things, everything is already hidden.

Somewhere in the code I put up it tries to find the parts that it named views after and have the visibility on for only that one for that view. It has no problem digging down into the subassemblies to find their names, but when selecting which to leave on it isn't digging.

 

This bit

 

'look at all of the components in the assembly
Dim oCompDef As Inventor.ComponentDefinition = openDoc.ComponentDefinition

 

I think looks at everything: parts, subassemblies, parts in the subassemblies.

But I think this bit

 

'define the first level components collection

Dim oCompOcc As Inventor.ComponentOccurrence

 

only looks at the top level.

 

So I think I need to change oCompOcc so that it also looks at everything?

 

Or is that not going to work?

Message 4 of 7
dan_inv09
in reply to: dan_inv09

Or does it look at everything?

 

Maybe we need to make it not look at assemblies?

Is it turning off all the parts except the one the view is named after (which it turns on if it's not) and then turning off the whole subassembly?

 

Or maybe I could just take this:

 

        If oCompOcc.Name = ShortName & ":1"  Then
             oCompOcc.Visible = True
             ThisApplication.ActiveView.Update()
             Else
             oCompOcc.Visible = False
             ThisApplication.ActiveView.Update()
             End If

 

and put a line before it that turns off the visibility at the top level and get rid of the "else"?

That way nothing is visible to start and it just finds the first part that matches the view to turn it on and nothing else.

(So what's the way to turn off the visibility for the whole assembly? "something.Visible=False"?)

Message 5 of 7

Attached rule (iLogic viewReps.txt) creates design view representations
in the top assembly for each its unique part.  Then this rule associate 
newly created DesignViewRepresentation objects with the corresponding 
components visibility but does this at the level of the top assembly only.

Am I right that your intent is to associate every design view representation 
in top assembly with all component occurrences that could represent each
parent unique part at different assembly levels? 


Screencast video of your workflow in the UI could greatly simplify description.

https://screencast.autodesk.com/

Cheers


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Message 6 of 7


Vladimir.Ananyev wrote:

Am I right that your intent is to associate every design view representation 
in top assembly with all component occurrences that could represent each
parent unique part at different assembly levels? 


Yes.

It's creating views named after all the parts at whatever level, but it only seems to be able to make the part visible if it is in the top level.

 

I have a welded part that is two halves in subassemblies but I want to make a drawing for all the pieces. (Ideally it might place views on the drawing as well, but for now I just want to get the visibility right.)

Message 7 of 7

 

Your code should complete several steps for every view representation you need:

1) create the new DesignViewRepresantation

2) Find and adjust the visibility of leaf components (parts)

3) save top assembly

 

The following rule illustrates this approach. Change RepName and CompName values.

 

Sub Main()
 
    Dim RepName As String = "Hidden_Riegel"
    Dim CompName As String = "Riegel"
 
    oAsmDoc = ThisDoc.Document
    If oAsmDoc Is Nothing Then
        MsgBox("Please open an assembly document")
        Exit Sub
    End If
 
    Dim oAssCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
    Dim oManager As RepresentationsManager = oAssCompDef.RepresentationsManager
    Dim oRep As DesignViewRepresentation = Nothing
    Try
        For Each oRep In oManager.DesignViewRepresentations
            If oRep.Name = RepName Then Exit For
        Next
        If oRep Is Nothing Then
            oRep = oManager.DesignViewRepresentations.Add(RepName)
        End If
 
        If oRep.Name <> RepName Then
            oRep = oManager.DesignViewRepresentations.Add(RepName)
        End If
    Catch ex As Exception
    End Try
 
    'hide all leaf components which names contain CompName substring
 
    Dim oColl As ComponentOccurrencesEnumerator = oAssCompDef.Occurrences.AllLeafOccurrences()
 
    For Each oOcc As ComponentOccurrence In oColl
        If oOcc.Suppressed Then Continue For
        If oOcc.IsSubstituteOccurrence Then Continue For
        If TypeOf oOcc.Definition Is VirtualComponentDefinition Then Continue For
        If oOcc.Name.IndexOf(CompName) >= 0 Then
            oOcc.Visible = False
        End If
    Next
 
    oAsmDoc.Save()  ' save top assembly
         Beep
End Sub

Fill free to modify this sample to suit your specific needs.

Cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report