Hello all,
How Can I select multiple objects but having a same name. for example, point, line and arc are having the same name.
so currently I am doing this.....
For Each tempObj As DisplayableObject In workPart.Curves
Dim mycase As String = tempObj.Name
Select Case mycase
Case "B200_50_05_01_G"
myobjects.Add(tempObj)
Case "B200_50_05_01_EG"
myobjects.Add(tempObj)
Case "B200_50_05_01_EN"
myobjects.Add(tempObj)
End Select
Next
For Each tempObj As DisplayableObject In workPart.Arcs
Dim mycase As String = tempObj.Name
Select Case mycase
Case "B200_50_05_01_G"
myobjects.Add(tempObj)
' lwd.WriteLine(tempObj.Name)
Case "B200_50_05_01_EG"
myobjects.Add(tempObj)
' lwd.WriteLine(tempObj.Name)
Case "B200_50_05_01_EN"
myobjects.Add(tempObj)
End Select
Next
For Each tempObj As DisplayableObject In workPart.Points
Dim mycase As String = tempObj.Name
Select Case mycase
Case "B200_50_05_01_G"
myobjects.Add(tempObj)
'lwd.WriteLine(tempObj.Name)
Case "B200_50_05_01_EG"
myobjects.Add(tempObj)
'lwd.WriteLine(tempObj.Name)
Case "B200_50_05_01_EN"
myobjects.Add(tempObj)
End Select
and so on, is there a better way, I don't want the user to select the objects, I am doing specific operations on a collection of objects, so I need those objects only.
Please help.
re: find named object
I would suggest creating a list (of string) to hold the object names of interest; then as you loop through the collections you can do something like:
for each temp as displayableobject in someCollection
if nameList.Contains(temp.name) then
'add object to list
end if
next
It should be noted that the .Curves collection contains all the curve types (lines, arcs, splines, etc); you don't need to iterate through the .Curves and the .Arcs collections separately.
Also, if you want to find objects by name regardless of type, you can make use of the .CycleByName method.
Thank you, I will try that.
Thank you, I will try that.
Regards
Pradeep
#If it works, it works.
Can you send some sample
Can you send some sample snippet using
Public Sub CycleByName(name As String, ByRef _object As NXOpen.Tag)
Please.
Regards
Pradeep
#If it works, it works.
re: CycleByName
The code below will cycle through the objects looking for ones named "REF". Change the value of the "nameToFind" variable before running the code.
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Module Module1
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
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 nameToFind As String = "REF"
Dim theTag As Tag = Tag.Null
Dim foundObjects As New List(Of NXObject)
theUfSession.Obj.CycleByName(nameToFind, theTag)
Do Until theTag = Tag.Null
Dim temp As NXObject = Utilities.NXObjectManager.Get(theTag)
foundObjects.Add(temp)
theUfSession.Obj.CycleByName(nameToFind, theTag)
Loop
If foundObjects.Count > 0 Then
lw.WriteLine(foundObjects.Count.ToString & " objects found with the name: " & nameToFind)
For Each temp As NXObject In foundObjects
lw.WriteLine(" " & temp.GetType.ToString)
Next
Else
lw.WriteLine("No objects found with name: " & nameToFind)
End If
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
re: CycleByName
I found this post quite useful but had a question regarding the posted code (I'm using NX7.5.5). If I give REF names to (non feature) curves and also to some features (such as cylinder, extrude, points or feature curves) then the program will only find the non feature curves
2 objects found with the name: REF
NXOpen.Line
NXOpen.Arc
If I run the program without the REF name on those 2 non feature curves then I get the following:
4 objects found with the name: REF
NXOpen.Features.Cylinder
NXOpen.Features.PointFeature
NXOpen.Features.AssociativeLine
NXOpen.Features.AssociativeArc
Any ideas on this?
Also - I don't really understand why the "theUfSession.Obj.CycleByName(nameToFind, theTag)" line appears both before and inside the Do loop. Sure this is just my limited understanding, but if you care to explain that would be much appreciated.
Thanks,
Geoff
re: CycleByName
In the C API reference, there is a note that ".CycleByName will only return features if there is no other object with the given name in the file. If the file has both objects and features with the same name, you can get the features by using the .CycleByNameAndType (using the UF_feature_type) method."
JXB is correct about why the function call appears twice in the code that I posted. The .CycleByName method takes two parameters: a string representing the name, and a tag (in our case, the tag parameter is named "theTag"). The tag parameter is used as both input and output for the method. Imagine that NX keeps a list of all the objects named "REF"; by passing in a null tag, we are saying "give me the first object named REF". After the method executes, "theTag" is no longer Null, but contains the tag of the first object. If we call .CycleByName again, this time passing in the tag of the first object, "theTag" will be set to the tag of the next named object in the list. When we run out of named objects, .CycleByName returns a null tag.
The code as posted calls the .CycleByName method with a null tag to "prime the pump" as it were. Then the "Do Until" checks to see if anything interesting was returned. If a null tag is returned at this point, no objects have the given name and the loop exits.
If you have used NX for any length of time, you've probably noticed that there are multiple ways to get the desired result. The same is true about programming. Below is one alternative to the code posted previously:
Do
theUfSession.Obj.CycleByName(nameToFind, theTag)
if theTag = Tag.Null then
Exit Do
end if
Dim temp As NXObject = Utilities.NXObjectManager.Get(theTag)
foundObjects.Add(temp)
Loop
In this case .CycleByName only appears once in the code; after we call it we check the output tag. If the tag is null, we exit the loop, otherwise we get the object and keep looping.
re: CycleByName
Thanks for taking the time to explain this. You're so right about the many ways of achieving the same thing (which while this is good, like most other things where there are multiple valid appraoches it makes it a little trickier to learn). Your second example made more sense to me as I guess it was more like other approaches I'd seen.
Print partial match?
Is there a way to get the program to cycle through all the names and print to the screen any partial matches?
If a part has REF_Sketch(1) and REF_Extrude(16) I would like it if by searching for REF it could get the screen to print out REF_Sketch(1) and the REF_Extrude(16).
re: partial name match
You could use the .CycleAll method to cycle through all the objects in a part file and check their name for partial matches. However, if you know the type of object that you are looking for, you can narrow down the search considerably. In your example, it appears that both REF_Sketch(1) and REF_Extrude(16) are named features. If you are only looking for named features, you can iterate through the part's .Features collection and query the name of each as needed.
I know of no API function that will take a "partial name" as input and return the objects that match the criteria. But that is not to say that you could not write such a function for your own use...
re: CycleByName
I think the theUfSession.Obj.CycleByName(nameToFind, theTag) appears twice because the 1st time it is used to initialised the variable 'theTag'. Otherwise this variable is Null and the Do Until .. Loop is not executed
Thanks
Regards