Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations MintJulep on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get all components in an assembly

Status
Not open for further replies.

claforet

Mechanical
Apr 8, 2010
54
Does anyone know an easy way using NXOpen to get all the components in an assembly (including subassemblies) saved to an array? I like to sort the assembly tree in descending order and then export it to a spreadsheet. I would ideally like to get that same structure/order, but I need it in NXOPen variables so that I can manipulate the data.

Thanks
 
Replies continue below

Recommended for you

There's an example of what you want to do in the GTAC API library...
 
In NXOpen, let's say you have a component 'c' which is part of a multi-level assembly. When using the assembly navigator, you can 'Pack All' and it will group identical components together in each sub-assembly and put a 'x 2', or whatever, indicating how many of those components are in a particular sub-assembly. How can I get that number for component 'c' with NXOpen?
 
The journal below shows one method to get this info. It is probably not the recommended method, and it only works on loaded components.
Code:
[COLOR=blue]Option[/color] [COLOR=blue]Strict[/color] [COLOR=blue]Off[/color]  

[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] NXOpen  
[COLOR=blue]Imports[/color] NXOpen.UF  
[COLOR=blue]Imports[/color] NXOpen.Assemblies  

[COLOR=blue]Module[/color] NXJournal  

    [COLOR=blue]Public[/color] theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
    [COLOR=blue]Public[/color] ufs [COLOR=blue]As[/color] UFSession [COLOR=blue]=[/color] UFSession.GetUFSession()  
    [COLOR=blue]Public[/color] lw [COLOR=blue]As[/color] ListingWindow [COLOR=blue]=[/color] theSession.ListingWindow  

    [COLOR=blue]Sub[/color] Main()  
        [COLOR=blue]Dim[/color] workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work  
        [COLOR=blue]Dim[/color] dispPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Display  

        lw.Open()  
        [COLOR=blue]Try[/color]  
            [COLOR=blue]Dim[/color] c [COLOR=blue]As[/color] ComponentAssembly [COLOR=blue]=[/color] dispPart.ComponentAssembly  
 [COLOR=green]'to process the work part rather than the display part,[/color]
 [COLOR=green]'  comment the previous line and uncomment the following line[/color]
 [COLOR=green]'Dim c As ComponentAssembly = workPart.ComponentAssembly[/color]
            [COLOR=blue]If[/color] [COLOR=blue]Not[/color] IsNothing(c.RootComponent) [COLOR=blue]Then[/color]  
 [COLOR=green]'*** insert code to process 'root component' (assembly file)[/color]
                lw.WriteLine("Assembly: " [COLOR=blue]&[/color] c.RootComponent.DisplayName)  
                lw.WriteLine(" [COLOR=blue]+[/color] Active Arrangement: " [COLOR=blue]&[/color] c.ActiveArrangement.Name)  
 [COLOR=green]'*** end of code to process root component[/color]
                reportComponentChildren(c.RootComponent, 1)  
            [COLOR=blue]Else[/color]  
 [COLOR=green]'*** insert code to process piece part[/color]
                lw.WriteLine("Part has no components")  
            End [COLOR=blue]If[/color]  
        [COLOR=blue]Catch[/color] e [COLOR=blue]As[/color] Exception  
            theSession.ListingWindow.WriteLine("Failed: " [COLOR=blue]&[/color] e.ToString)  
        End [COLOR=blue]Try[/color]  
        lw.Close()  

    End [COLOR=blue]Sub[/color]  

 [COLOR=green]'**********************************************************[/color]
    [COLOR=blue]Sub[/color] reportComponentChildren(ByVal comp [COLOR=blue]As[/color] Component, [COLOR=blue]ByVal[/color] indent [COLOR=blue]As[/color] [COLOR=blue]Integer[/color])  

        [COLOR=blue]Dim[/color] numOccs(-1) [COLOR=blue]As[/color] Tag  

        [COLOR=blue]For[/color] [COLOR=blue]Each[/color] child [COLOR=blue]As[/color] Component [COLOR=blue]In[/color] comp.GetChildren()  
 [COLOR=green]'*** insert code to process component or subassembly[/color]
            lw.WriteLine(New String(" ", indent * 2) & child.DisplayName & vbTab & child.Name)  
            [COLOR=blue]Try[/color]  
                ufs.Assem.AskOccsOfPart(comp.Prototype.OwningPart.Tag, child.Prototype.OwningPart.Tag, numOccs)  
                lw.WriteLine(New String(" ", indent * 2) & "Quantity: " [COLOR=blue]&[/color] numOccs.GetLength(0))  
            [COLOR=blue]Catch[/color] ex [COLOR=blue]As[/color] System.NullReferenceException  
                lw.WriteLine(New String(" ", indent * 2) & "*** Component quantity information unavailable (component not loaded)")  
            [COLOR=blue]Catch[/color] ex [COLOR=blue]As[/color] ApplicationException  

            End [COLOR=blue]Try[/color]  

 [COLOR=green]'*** end of code to process component or subassembly[/color]
            [COLOR=blue]If[/color] child.GetChildren.Length <> 0 [COLOR=blue]Then[/color]  
 [COLOR=green]'*** this is a subassembly, add code specific to subassemblies[/color]
                lw.WriteLine(New String(" ", indent * 2) & _  
                 "* subassembly with " [COLOR=blue]&[/color] _  
                 child.GetChildren.Length [COLOR=blue]&[/color] " components")  
                lw.WriteLine(New String(" ", indent * 2) & _  
                 " [COLOR=blue]+[/color] Active Arrangement: " [COLOR=blue]&[/color] _  
                 child.OwningPart.ComponentAssembly.ActiveArrangement.Name)  
 [COLOR=green]'*** end of code to process subassembly[/color]
            [COLOR=blue]Else[/color]  
 [COLOR=green]'this component has no children (it is a leaf node)[/color]
 [COLOR=green]'add any code specific to bottom level components[/color]
            End [COLOR=blue]If[/color]  
            lw.WriteLine("")  
            reportComponentChildren(child, indent [COLOR=blue]+[/color] 1)  

        [COLOR=blue]Next[/color]  

    End [COLOR=blue]Sub[/color]  
 [COLOR=green]'**********************************************************[/color]
    [COLOR=blue]Public[/color] [COLOR=blue]Function[/color] GetUnloadOption(ByVal dummy [COLOR=blue]As[/color] [COLOR=blue]String[/color]) [COLOR=blue]As[/color] [COLOR=blue]Integer[/color]  
        [COLOR=blue]Return[/color] Session.LibraryUnloadOption.Immediately  
    End [COLOR=blue]Function[/color]  
 [COLOR=green]'**********************************************************[/color]

End [COLOR=blue]Module[/color]


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor