Searching Subassemblies Via iLogic

Searching Subassemblies Via iLogic

tdant
Collaborator Collaborator
2,607 Views
8 Replies
Message 1 of 9

Searching Subassemblies Via iLogic

tdant
Collaborator
Collaborator

Hey all,

 

I'm a code rookie who's learning iLogic, so bear with me. Despite all my googling efforts, I'm stuck.

 

I'm trying to write an assembly rule that will find and hide all parts that contain a certain string in their name. I need to learn how to 

search an assembly for part names that contain the target string. There must be some sort of loop mechanism that can make it happen, but I don't know enough about iLogic to figure it out.

 

I need a way to get the component names from the assembly file, and also a way to see whether those components are parts or assemblies. Can anyone help?

0 Likes
Accepted solutions (1)
2,608 Views
8 Replies
Replies (8)
Message 2 of 9

MechMachineMan
Advisor
Advisor

See the linked resources.

 

http://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html

 

http://help.autodesk.com/view/INVNTOR/2018/ENU/?guid=GUID-DE229A99-9860-4501-AF4C-C671D120A2DB


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 9

tdant
Collaborator
Collaborator

Thanks, this is helping a ton. Question: Is ComponentDefinition an inherent property of the Document object? I don't see it in the API documentation. I'm looking at this line:

 

SyntaxEditor Code Snippet

TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)

which passes the first argument into the TraverseAssembly() and runs it through a loop. The .ComponentDefinition.Occurrences part is building a list of occurrences in the oAsmDoc document, right? 

0 Likes
Message 4 of 9

MechMachineMan
Advisor
Advisor

Yes and No.

 

You should be able to access the ComponentDefinition method of the generic Document object (except for DrawingDocument), HOWEVER, there is documented issues of it not functioning properly in all cases.

 

The "proper" use is to call the ComponentDefinition from the typed object of either PartDocument or AssemblyDocument.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 5 of 9

tdant
Collaborator
Collaborator

So then, If I replace that line with:

 

SyntaxEditor Code Snippet

TraverseAssembly(ThisDoc.Document.ComponentOccurrences, 1)

I get the same effect with a little less work, right? 

 

Edit: Nevermind. That doesn't work. If I understand correctly, ThisDoc.Document.ComponentDefinition is itself an AssemblyComponentDefinition object, and then you can call the .Occurrences method on it. 

0 Likes
Message 6 of 9

MechMachineMan
Advisor
Advisor

It CAN be. It probably actually resolves to a GENERIC ComponentDefinition object because nothing about it is strongly typed as AssemblyDocument/ AssemblyComponentDefinition

 

To strongly type, you could do:

 

Dim oACD As AssemblyComponentDefinition

On Error Resum Next
   oACD = ThisDoc.Document.ComponentDefinition
   If Err.Number <> 0 Then
       MsgBox("AssemblyComponentDefinition Not accessible from this document")
       Exit Sub
   End if
On Error GoTo 0

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 7 of 9

tdant
Collaborator
Collaborator

Boom. Got it. Apparently Leaf Occurrences are a more elegant solution, but I'm making myself learn the hard way first as a matter of principle.

 

SyntaxEditor Code Snippet

Sub Main()
HideBreakReps(ThisDoc.Document.ComponentDefinition.Occurrences)
End Sub

Sub HideBreakReps(Occurrences As ComponentOccurrences)
For Each oOcc in Occurrences
    If oOcc.DefinitionDocumentType = kAssemblyDocumentObject
    HideBreakReps(oOcc.SubOccurrences)
    Else If oOcc.DefinitionDocumentType = kPartDocumentObject
        If InStr(LCase(oOcc.Name), "break rep")
        oOcc.Visible = False
        End If
    Else
    MessageBox.Show("No sub-assemblies or parts found")
    End If
Next
End Sub

 

Message 8 of 9

MechMachineMan
Advisor
Advisor
Accepted solution

FTFY.

    - Proper nesting.

    - Included 'then' following if statements so if you ever need to convert it to vba, you can do so much faster.

    - For same reason as above, included call on function/sub calls that only contain 1 argument wrapped in brackets.

   

- Coded with these changes, you actually should be able to paste it in the VBA Macro editor and run it without any tweaks, and also have it work in the rule environment.

 

Sub Main()
    Call HideBreakReps(ThisDoc.Document.ComponentDefinition.Occurrences)
End Sub

Sub HideBreakReps(Occurrences As ComponentOccurrences)
    For Each oOcc in Occurrences
        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
            Call HideBreakReps(oOcc.SubOccurrences)
        Elseif oOcc.DefinitionDocumentType = kPartDocumentObject Then
            If InStr(LCase(oOcc.Name), "break rep")
                oOcc.Visible = False
            End If
        Else
            Call Msgbox("No sub-assemblies or parts found")
        End If
    Next
End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 9 of 9

tdant
Collaborator
Collaborator

Well this has been a most educational exercise. Thanks for the help.

0 Likes