12

How do I get a list of all open parts in Siemens NX using NX Open?

I've tried to use Session.Parts, but I'm having trouble using it, and I have a feeling it's pretty simple. I tried

for (auto part in session->Parts())
{
    // ...
}

but the compiler doesn't like it.

richardec
  • 14,202
  • 6
  • 23
  • 49

1 Answers1

25

C++

NXOpen::Session::Parts() returns an NXOpen::PartCollection, which, like all NX Open collections, can be iterated over, and the iterator can be dereferenced to access the actual Part object:

#include <NXOpen/Session.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/BasePart.hxx>

NXOpen::Session *session = NXOpen::Session::GetSession();
for (auto it = session->Parts()->begin(); it != session->Parts()->end(); it++)
{
    NXOpen::Part *part = *it;
    std::string name = part->Name().GetText();
    std::string fullPath = part->FullPath().GetText();
    // Do something with name or fullPath...
}

C#

Simply looping over NXOpen.Session.Parts will do the trick:

using NXOpen.Session;
using NXOpen.Part;

Session session = Session.GetSession();
foreach (Part part in session.Parts)
{
    string name = part.Name;
    string fullPath = part.FullPath;
    // Do something with name or fullPath...
}

Java

import nxopen.Session;
import nxopen.Part;
import java.util.Iterator;

Session session = (Session)SessionFactory.get("Session");
for (Iterator it = session.parts().iterator(); it.hasNext();) {
    Part part = (Part)it.next();
    String name = part.name();
    String fullPath = part.fullPath();
    // Do something with name or fullPath...
}
richardec
  • 14,202
  • 6
  • 23
  • 49