×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Selecting objects by name in NX Open

Selecting objects by name in NX Open

Selecting objects by name in NX Open

(OP)
Hello everyone!

I can’t understand how to select an object in NX using its name. For example, I have a CSYS named ‘Ref’ (not the CSYS feature, but the CSYS itself). How can I make a selection in NX Open if I know that the CSYS I need is named ‘Ref’?

www.cadroad.com

RE: Selecting objects by name in NX Open

Cycle through the coordinate systems in the part looking for the one named "REF". Save a reference to the one that you find.

CODE

Option Strict Off
Imports System
Imports NXOpen

Module Module1

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim refCsys As CoordinateSystem = Nothing

        'find coordinate system named "REF"
        For Each temp As CoordinateSystem In workPart.CoordinateSystems
            If temp.Name = "REF" Then
                refCsys = temp
            End If
        Next

        If Not IsNothing(refCsys) Then
            lw.WriteLine("REF csys found")
        Else
            lw.WriteLine("REF csys not found")
            Return
        End If

        'do something with it
        refCsys.Highlight()
        'use part cleanup -> remove extraneous highlighting to get it back to normal

        lw.Close()

    End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function

End Module 

www.nxjournaling.com

RE: Selecting objects by name in NX Open

(OP)
Thank you! I will now research this example and get back to you in case I have any questions.

www.cadroad.com

RE: Selecting objects by name in NX Open

(OP)
I'm not so familiar with VB, maybe you have C# version of this example?

www.cadroad.com

RE: Selecting objects by name in NX Open

CODE

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using NXOpen;

static class Module1
{


	public static void Main()
	{
		Session theSession = Session.GetSession();
		if ((theSession.Parts.BaseWork == null)) {
			//active part required
			return;
		}

		Part workPart = theSession.Parts.Work;
		ListingWindow lw = theSession.ListingWindow;
		lw.Open();

		CoordinateSystem refCsys = null;

		//find coordinate system named "REF"
		foreach (CoordinateSystem temp in workPart.CoordinateSystems) {
			if (temp.Name == "REF") {
				refCsys = temp;
			}
		}

		if ((refCsys != null)) {
			lw.WriteLine("REF csys found");
		} else {
			lw.WriteLine("REF csys not found");
			return;
		}

		//do something with it
		refCsys.Highlight();
		//use part cleanup -> remove extraneous highlighting to get it back to normal

		lw.Close();

	}


	public static Session.LibraryUnloadOption GetUnloadOption(string dummy)
	{

		//Unloads the image immediately after execution within NX
		return NXOpen.Session.LibraryUnloadOption.Immediately;

	}

} 

www.nxjournaling.com

RE: Selecting objects by name in NX Open

(OP)
Thank you! I'll dig into it right now!

www.cadroad.com

RE: Selecting objects by name in NX Open

(OP)
As far as I understand, we do not actually 'select' objects, we're sifting through all objects and picking those with matching name? Can this method be slow when we have many objects?
In interactive mode, we can use Class Selection window, enter name there and get a selection. Is it possible to utilise this functionality programmatically? Or are we actually doing it in the above code?

www.cadroad.com

RE: Selecting objects by name in NX Open

In the code above, the csys object is not added to any type of selection, but we do have a reference to it so that we can use it in some way. The code above starts by looking through only the csys objects (not all of the objects in the file). It examines each, looking for the one named "REF". While it is true that the more objects you have, the slower the search will be; the file would have to contain a LOT of such objects before you notice a slowdown.

The code above could be improved slightly (with respect to speed of execution) by exiting the loop when the named csys is found. As it is, even when the named csys is found, we continue looking at the rest of the csys objects.

www.nxjournaling.com

RE: Selecting objects by name in NX Open

(OP)
Anyway, I was able to put this example to use and it works for me. It's more than enough for my current needs. Thank you!

www.cadroad.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources