Showing results for 
Search instead for 
Do you mean 
Reply
Solved! Go to solution

Obtaining assembly component files

Hello everyone!

 

I need to go through the assembly and perform action on each component. Could you please advice me how can this be done? I found some samples in the Solution Center, but they all aim at obtaining occurences, rather than actual parts. In other words, if somepart.prt is added twice into assembly, the code obtains them both, where I need to access the actual somepart.prt to perform action on its feature three. If you know relevant Solution Center example, please point me to it. Thanks!

8 REPLIES

Re: Obtaining assembly component files

Keyword is "prototype".

 

Search for nx_api3112 in SolutionCenter.

 

Regards

Jan

Jan Böttcher

Re: Obtaining assembly component files

Just get all the parts in the partCollection, and iterate through the array, like this:

 

Option Strict Off
Imports System
Imports NXOpen

Module process_every_part_in_session

    Dim theSession As Session = Session.GetSession()

    Sub Main()

        Dim allParts() As Part = theSession.Parts.ToArray()

        For Each thisPart In allParts

            Echo("Part: " & thisPart.Leaf())
            ' perform your desired action on the part here
        Next

    End Sub

    Sub Echo(ByVal output As String)

        theSession.ListingWindow.Open()
        theSession.ListingWindow.WriteLine(output)
        theSession.LogFile.WriteLine(output)

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function

End Module

Re: Obtaining assembly component files

If you have a reference to a component, you can get the part with something like this:

 

{comp}.Prototype.OwningPart

 

This assumes that the component is loaded in memory; if it is not loaded, you may have to load it first.

Re: Obtaining assembly component files

So basically I can iterate through all instances and just check if the actual component has been already attended to?

Re: Obtaining assembly component files

 

Yes, but based on your initial description, I thought you only wanted to handle each part once.  To do that, you can use the PartCollection, and you will skip all of the multiples.

 

Re: Obtaining assembly component files

The downside of the PartCollection is that it may contain parts that don't belong to the assembly of interest.

 

You can process the assembly components and keep track of the ones that you have already processed, or process the part collection ensuring that each part is used in the assembly of interest before modifying it.

Re: Obtaining assembly component files

I found a neat example nx_api3552. I believe it can be modified to add components to a list and just check each obtained component against it. This way I should be able to end up with a list of assembly files.

Solution
Solution
Accepted by topic author Scaffold
‎02-11-2016 01:06 PM

Re: Obtaining assembly component files

Thanks everyone for the suggestions! They helped me to do the job.

For anyone interested, I eventually found an excellent C# example: nx_api3405.