C# Apprentice Iterate through SubAssembly

C# Apprentice Iterate through SubAssembly

Anonymous
Not applicable
1,583 Views
10 Replies
Message 1 of 11

C# Apprentice Iterate through SubAssembly

Anonymous
Not applicable

Hallo,

 

i need to iterate through the SubAssemblies or SubOccurrences of Main Assembly, i know it possible in VB.Net but i want to do the same in C#.net.

In VB.Net as Following :

Public Sub TraverseAssemblySample()
    ' Get the active assembly.
    Dim oAsmDoc As AssemblyDocument
    Set oAsmDoc = ThisApplication.ActiveDocument
    Debug.Print oAsmDoc.DisplayName

    ' Call the function that does the recursion.
    Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)
End Sub

Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, _
                             Level As Integer)
    ' Iterate through all of the occurrence in this collection.  This
    ' represents the occurrences at the top level of an assembly.
    Dim oOcc As ComponentOccurrence
    For Each oOcc In Occurrences
        ' Print the name of the current occurrence.
        Debug.Print Space(Level * 3) & oOcc.Name

        ' Check to see if this occurrence represents a subassembly
        ' and recursively call this function to traverse through it.
        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
            Call TraverseAssembly(oOcc.SubOccurrences, Level + 1)
        End If
    Next
End Sub

 

Anyone can help ?

Thanks

The result should be like that

screenshot.png

0 Likes
1,584 Views
10 Replies
Replies (10)
Message 2 of 11

ianteneth
Advocate
Advocate

Hi @Anonymous,

 

Here is your code converted to C#. Also, remember that there is an Inventor forum dedicated to programming and customization like this. Here is the link to it.

https://forums.autodesk.com/t5/inventor-customization/bd-p/120 

 

Output:

Annotation 2020-04-27 123834.png

 

Converted Code:

// Get instance of Inventor
            Application application = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") as Application;

            // Get the active assembly.
            AssemblyDocument assembly = application.ActiveDocument as AssemblyDocument;
            Console.WriteLine(assembly.DisplayName);

            // Call the function that does the recursion
            TraverseAssembly(assembly.ComponentDefinition.Occurrences, 1);

private static void TraverseAssembly(ComponentOccurrences occurrences, int level)
        {
            // Iterate through all of the occurrences in this collection.
            // This represents the occurrences at the top level of an assembly.
            foreach(ComponentOccurrence occurrence in occurrences)
            {
                // Print the name of the occurrence.
                Console.WriteLine(new String(' ', level) + occurrence.Name);

                // Check to see if this occurrence represenst a subassembly.
                // Recursively call this function to traverse through it.
                if(occurrence.DefinitionDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                {
                    TraverseSubAssembly(occurrence.SubOccurrences, level + 1);
                }
            }
        }

        private static void TraverseSubAssembly(ComponentOccurrencesEnumerator occurrences, int level)
        {
            // Iterate through all of the occurrences in this collection.
            // This represents the occurrences at the top level of an assembly.
            foreach (ComponentOccurrence occurrence in occurrences)
            {
                // Print the name of the occurrence.
                Console.WriteLine(new String(' ', level) + occurrence.Name);

                // Check to see if this occurrence represenst a subassembly.
                // Recursively call this function to traverse through it.
                if (occurrence.DefinitionDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                {
                    TraverseSubAssembly(occurrence.SubOccurrences, level + 1);
                }
            }
        }

 

 

 

Message 3 of 11

Anonymous
Not applicable

Hi @ianteneth 

 

Thank you very much.

0 Likes
Message 4 of 11

Anonymous
Not applicable

Hi,

@ianteneth 
unfortunately it doesn't work in Assembly with multisub Assembly.
I got this Error Message.

 

Error: 0x80004005 (E_FAIL)

 

static void Main(string[] args)
        {
            Inventor.ApprenticeServerDocument assembly;
            Inventor.ApprenticeServerComponent oSvr = new Inventor.ApprenticeServerComponent();
            assembly = oSvr.Open(@"C:\This_Inventor_Project\Parts\Baugruppe2.iam");

            Console.WriteLine(assembly.DisplayName);

            // Call the function that does the recursion
            TraverseAssembly(assembly.ComponentDefinition.Occurrences, 1);
        }

        private static void TraverseAssembly(Inventor.ComponentOccurrences occurrences, int level)
        {
            // Iterate through all of the occurrences in this collection.
            // This represents the occurrences at the top level of an assembly.
            foreach (Inventor.ComponentOccurrence occurrence in occurrences)
            {
                // Print the name of the occurrence.
                Console.WriteLine(new String(' ', level) + occurrence.Name);

                // Check to see if this occurrence represenst a subassembly.
                // Recursively call this function to traverse through it.
                if (occurrence.DefinitionDocumentType == Inventor.DocumentTypeEnum.kAssemblyDocumentObject)
                {
                    TraverseSubAssembly(occurrence.SubOccurrences, level + 1);
                }
            }
        }

        private static void TraverseSubAssembly(Inventor.ComponentOccurrencesEnumerator occurrences, int level)
        {
            // Iterate through all of the occurrences in this collection.
            // This represents the occurrences at the top level of an assembly.
            foreach (Inventor.ComponentOccurrence occurrence in occurrences)
            {
                // Print the name of the occurrence.
                Console.WriteLine(new String(' ', level) + occurrence.Name);

                // Check to see if this occurrence represenst a subassembly.
                // Recursively call this function to traverse through it.
                if (occurrence.DefinitionDocumentType == Inventor.DocumentTypeEnum.kAssemblyDocumentObject)
                {
                    TraverseSubAssembly(occurrence.SubOccurrences, level + 1);
                }
            }
        }

 

 

2020-04-30 09_09_26-Window.png

0 Likes
Message 5 of 11

ianteneth
Advocate
Advocate

Are there by chance suppressed parts in this assembly? I tested this on my computer with a suppressed subassembly and I received the same error. Below is the updated code to skip over suppressed components. See if that fixes the error.

 

static void Main(string[] args)
        {

            // Get instance of Inventor
            Application application = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") as Application;

            // Get the active assembly.
            AssemblyDocument assembly = application.ActiveDocument as AssemblyDocument;
            Console.WriteLine(assembly.DisplayName);

            // Call the function that does the recursion
            TraverseAssembly(assembly.ComponentDefinition.Occurrences, 1);

            Console.ReadKey();
        }


private static void TraverseAssembly(ComponentOccurrences occurrences, int level)
        {
            // Iterate through all of the occurrences in this collection.
            // This represents the occurrences at the top level of an assembly.
            foreach(ComponentOccurrence occurrence in occurrences)
            {
                // Skip if occurrence is suppressed
                if (!occurrence.Suppressed)
                {
                    // Print the name of the occurrence.
                    Console.WriteLine(new String(' ', level) + occurrence.Name);

                    // Check to see if this occurrence represenst a subassembly.
                    // Recursively call this function to traverse through it.
                    if (occurrence.DefinitionDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                    {
                        TraverseSubAssembly(occurrence.SubOccurrences, level + 4);
                    }
                }
            }
        }



private static void TraverseSubAssembly(ComponentOccurrencesEnumerator occurrences, int level)
        {
            // Iterate through all of the occurrences in this collection.
            // This represents the occurrences at the top level of an assembly.
            foreach (ComponentOccurrence occurrence in occurrences)
            {
                // Skip if occurrence is suppressed
                if (!occurrence.Suppressed)
                {
                    // Print the name of the occurrence.
                    Console.WriteLine(new String(' ', level) + occurrence.Name);

                    // Check to see if this occurrence represenst a subassembly.
                    // Recursively call this function to traverse through it.
                    if (occurrence.DefinitionDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                    {
                        TraverseSubAssembly(occurrence.SubOccurrences, level + 4);
                    }
                }
            }
        }

 

0 Likes
Message 6 of 11

Anonymous
Not applicable

Hi @ianteneth ,

Thank you so much for the speedy reply.

I dont have suppressed parts in this assembly, I got the same Error with your updated code. 

 

error.png

0 Likes
Message 7 of 11

ianteneth
Advocate
Advocate

Hmm. I just successfully tested this code on a decently big nested assembly. Below is some output from that. Can you click the "View Details" link in the error and expand a few sections and post a screenshot? And I am guessing you cannot share the files?

 

Annotation 2020-04-30 151230.png

 

0 Likes
Message 8 of 11

Anonymous
Not applicable

u use the API in you code, i tested this code with API it worked fine, but with Apprentice Server i get Error, could you please test it with Apprentice Server (same as mycode) you will get the same Error.

See please the Screenshot in attached.

Thanks.

 

2.png3.png1.png

0 Likes
Message 9 of 11

ianteneth
Advocate
Advocate

I am just noticing that you are asking about Apprentice in the title... Sorry about that! I am not familiar with Apprentice Server. Maybe we can find someone on here that is familiar with that. Maybe @WCrihfield can help

Message 10 of 11

WCrihfield
Mentor
Mentor

Nope.  I haven't worked with Apprentice either.  Though, I could be wrong, but I think those who don't work with Apprentice, can still access and use its functionality, but those whose default is Apprentice, can only use Apprentice.    I've heard it is faster to use for certain things, too, but as I said, I don't really know that much about the differences, to give much advice about the specifics.  Sorry.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 11 of 11

ianteneth
Advocate
Advocate

@Anonymoussince I didn't address the Apprentice Server aspect of your question I might help get others to answer if you uncheck my earlier answer as the solution. I think the 3 dots to the top right of my post will let you mark it as "Not Solution"?