Selecting groups without stickiness

Hi,
I am new to NXOpen, VB and Journaling. I am working on a project that converts a 3D model to 2D by taking cross-sections at various angles and merges them into one. I am able to create the required cross sections which are saved as curves in a group. However, I am unable to move them as I am unable to select the required group within the journal.

I am unable to create the required string for different groups within the loop and hence unable to select the required group. The error I receive with the created string is no object found with this name.
*Code from Journal*
Dim group1 As Group = CType(workPart.FindObject("ENTITY 15 11 1"), Group)

*Required code within for loop*
Dim groupx As Group = CType(workPart.FindObject("ENTITY 15 (13-2x) 1"), Group)

Is there anyway to generate the required string or select the group directly? Any help is appreciated.
I have attached the code to move the cross-section below.

Thanks


Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String)

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim nullFeatures_MoveObject As Features.MoveObject = Nothing

If Not workPart.Preferences.Modeling.GetHistoryMode Then
Throw(New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
End If
'--------------------------------
'Declaring variables
Dim x As Integer
'Defines axis
Dim origin1 As Point3d = New Point3d(0.0, 0.0, 0.0)
Dim vector1 As Vector3d = New Vector3d(0.0, 0.0, 1.0)
Dim direction1 As Direction
direction1 = workPart.Directions.CreateDirection(origin1, vector1, SmartObject.UpdateOption.WithinModeling)
Dim nullPoint As Point = Nothing
Dim axis1 As Axis
axis1 = workPart.Axes.CreateAxis(nullPoint, direction1, SmartObject.UpdateOption.WithinModeling)

'Points
Dim Origin As New Point3d(0, 0, 0)
Dim point1 As Point
point1 = workPart.Points.CreatePoint(Origin)
Dim point2 As Point
point2 = workPart.Points.CreatePoint(Origin)
Dim point3 As Point
point3 = workPart.Points.CreatePoint(Origin)

Dim nullUnit As Unit = Nothing
'----------------------------------
Dim n As Integer = 6
Dim answer As Integer = 30
'----------------------------------
For x = 1 to n
Dim expression1 As Expression
Dim expression2 As Expression
Dim expression3 As Expression
Dim expression4 As Expression
Dim expression5 As Expression
Dim expression6 As Expression
Dim expression7 As Expression
Dim expression8 As Expression
Dim expression9 As Expression
Dim expression10 As Expression
Dim expression11 As Expression
Dim expression12 As Expression
Dim expression13 As Expression
Dim expression14 As Expression
Dim expression15 As Expression
Dim expression16 As Expression
Dim expression17 As Expression
'Actual moving
Dim moveObjectBuilderx As Features.MoveObjectBuilder
moveObjectBuilderx = workPart.BaseFeatures.CreateMoveObjectBuilder(nullFeatures_MoveObject)
moveObjectBuilderx.TransformMotion.DistanceAngle.OrientXpress.AxisOption = GeometricUtilities.OrientXpressBuilder.Axis.Passive
moveObjectBuilderx.TransformMotion.DistanceAngle.OrientXpress.PlaneOption = GeometricUtilities.OrientXpressBuilder.Plane.Passive
moveObjectBuilderx.TransformMotion.AlongCurveAngle.AlongCurve.IsPercentUsed = True
moveObjectBuilderx.TransformMotion.AlongCurveAngle.AlongCurve.Expression.RightHandSide = "0"
moveObjectBuilderx.TransformMotion.AlongCurveAngle.AlongCurve.Expression.RightHandSide = "0"
moveObjectBuilderx.TransformMotion.OrientXpress.AxisOption = GeometricUtilities.OrientXpressBuilder.Axis.Passive
moveObjectBuilderx.TransformMotion.OrientXpress.PlaneOption = GeometricUtilities.OrientXpressBuilder.Plane.Passive
moveObjectBuilderx.TransformMotion.Option = GeometricUtilities.ModlMotion.Options.Angle
moveObjectBuilderx.TransformMotion.DistanceValue.RightHandSide = "0"
moveObjectBuilderx.TransformMotion.DistanceBetweenPointsDistance.RightHandSide = "0"
moveObjectBuilderx.TransformMotion.RadialDistance.RightHandSide = "0"
moveObjectBuilderx.TransformMotion.Angle.RightHandSide = "30"
moveObjectBuilderx.TransformMotion.DistanceAngle.Distance.RightHandSide = "0"
moveObjectBuilderx.TransformMotion.DistanceAngle.Angle.RightHandSide = "0"
moveObjectBuilderx.TransformMotion.DeltaEnum = GeometricUtilities.ModlMotion.Delta.ReferenceWcsWorkPart
moveObjectBuilderx.TransformMotion.DeltaXc.RightHandSide = "0"
moveObjectBuilderx.TransformMotion.DeltaYc.RightHandSide = "0"
moveObjectBuilderx.TransformMotion.DeltaZc.RightHandSide = "0"
moveObjectBuilderx.TransformMotion.AlongCurveAngle.AlongCurve.Expression.RightHandSide = "0"
moveObjectBuilderx.TransformMotion.AlongCurveAngle.AlongCurveAngle.RightHandSide = "0"
theSession.SetUndoMarkName(markId1, "Move Object Dialog")

moveObjectBuilderx.TransformMotion.AngularAxis = axis1
moveObjectBuilderx.TransformMotion.AngularAxis = axis1

'Defining objects: Replaced by group
Dim groupx As Group = CType(workPart.FindObject("ENTITY 15 11 1"), Group)
Dim addedx As Boolean
addedx = moveObjectBuilderx.ObjectToMoveObject.Add(groupx)

'Definition of angle
moveObjectBuilderx.TransformMotion.Angle.RightHandSide = x*answer

'Expressions
Dim unit1 As Unit
unit1 = moveObjectBuilderx.TransformMotion.RadialOriginDistance.Units
expression1 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1)

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")
expression2 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1)
expression3 = workPart.Expressions.CreateSystemExpressionWithUnits("p17_x=0.00000000000", unit1)
expression4 = workPart.Expressions.CreateSystemExpressionWithUnits("p18_y=0.00000000000", unit1)
expression5 = workPart.Expressions.CreateSystemExpressionWithUnits("p19_z=0.00000000000", unit1)
expression6 = workPart.Expressions.CreateSystemExpressionWithUnits("p20_xdelta=0.00000000000", unit1)
expression7 = workPart.Expressions.CreateSystemExpressionWithUnits("p21_ydelta=0.00000000000", unit1)
expression8 = workPart.Expressions.CreateSystemExpressionWithUnits("p22_zdelta=0.00000000000", unit1)
expression9 = workPart.Expressions.CreateSystemExpressionWithUnits("p23_radius=0.00000000000", unit1)

Dim unit2 As Unit
unit2 = moveObjectBuilderx.TransformMotion.DistanceAngle.Angle.Units
expression10 = workPart.Expressions.CreateSystemExpressionWithUnits("p24_angle=0.00000000000", unit2)
expression11 = workPart.Expressions.CreateSystemExpressionWithUnits("p25_zdelta=0.00000000000", unit1)
expression12 = workPart.Expressions.CreateSystemExpressionWithUnits("p26_radius=0.00000000000", unit1)
expression13 = workPart.Expressions.CreateSystemExpressionWithUnits("p27_angle1=0.00000000000", unit2)
expression14 = workPart.Expressions.CreateSystemExpressionWithUnits("p28_angle2=0.00000000000", unit2)
expression15 = workPart.Expressions.CreateSystemExpressionWithUnits("p29_distance=0", unit1)
expression16 = workPart.Expressions.CreateSystemExpressionWithUnits("p30_arclen=0", unit1)
expression17 = workPart.Expressions.CreateSystemExpressionWithUnits("p31_percent=0", nullUnit)

theSession.SetUndoMarkName(markId2, "Point Dialog")

'Expression Values
expression3.RightHandSide = "0.00000000000"
expression4.RightHandSide = "0.00000000000"
expression5.RightHandSide = "0.00000000000"
expression6.RightHandSide = "0.00000000000"
expression7.RightHandSide = "0.00000000000"
expression8.RightHandSide = "0.00000000000"
expression9.RightHandSide = "0.00000000000"
expression10.RightHandSide = "0.00000000000"
expression11.RightHandSide = "0.00000000000"
expression12.RightHandSide = "0.00000000000"
expression13.RightHandSide = "0.00000000000"
expression14.RightHandSide = "0.00000000000"
expression17.RightHandSide = "100.00000000000"

theSession.DeleteUndoMark(markId2, Nothing)

axis1.Point = point2
moveObjectBuilderx.TransformMotion.AngularAxis = axis1

Dim markId5 As Session.UndoMarkId
markId5 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Move Object")
theSession.DeleteUndoMark(markId5, Nothing)
Dim markId6 As Session.UndoMarkId
markId6 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Move Object")
Dim nXObject1 As NXObject
nXObject1 = moveObjectBuilderx.Commit()
Dim objects2() As NXObject
objects2 = moveObjectBuilderx.GetCommittedObjects()

theSession.DeleteUndoMark(markId6, Nothing)

theSession.SetUndoMarkName(markId1, "Move Object")

moveObjectBuilderx.Destroy()

workPart.Expressions.Delete(expression17)
workPart.Expressions.Delete(expression16)
workPart.Expressions.Delete(expression15)
workPart.Expressions.Delete(expression14)
workPart.Expressions.Delete(expression13)
workPart.Expressions.Delete(expression12)
workPart.Expressions.Delete(expression11)
workPart.Expressions.Delete(expression10)
workPart.Expressions.Delete(expression9)
workPart.Expressions.Delete(expression8)
workPart.Expressions.Delete(expression7)
workPart.Expressions.Delete(expression6)
workPart.Expressions.Delete(expression5)
workPart.Expressions.Delete(expression4)
workPart.Expressions.Delete(expression3)
workPart.Expressions.Delete(expression2)
workPart.Expressions.Delete(expression1)
Next
workPart.Points.DeletePoint(point3)
End Sub
End Module

There is some code in the following link that shows how to get the groups in the part file:
http://nxjournaling.com/comment/3601#comment-3601

If you are looking for a group with a particular name, you could cycle through the groups until you find it.

Thanks for the help. Based on the reply and link, I am able to cycle through the groups and select the group I want. But I am having trouble saving this group for use in later parts.

How exactly should I do this? Am I doing something wrong?
the edited code is attached below.
Thanks again.


Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
Dim workPart As Part = theSession.Parts.Work

Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim tmpGrp As NXOpen.Tag = NXOpen.Tag.Null
Dim myGroup As Group
Dim x As Integer

Do
theUfSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_group_type, tmpGrp)
'skip the initial null tag
If tmpGrp = NXOpen.Tag.Null Then
Continue Do
End If

myGroup = Utilities.NXObjectManager.Get(tmpGrp)
For x = 1 to 6
If myGroup.Name = "Section "&x Then
Dim groupx As Group = myGroup
lw.WriteLine(myGroup.Name)
End If
Next
Loop Until tmpGrp = NXOpen.Tag.Null

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

Based on the posted code, it appears that you are looking for groups named: "Section 1", "Section 2", ... , and "Section 6" and you want to save them in variables named "group1", "group2", ... "group6".

The variable declaration "Dim groupx as Group" will result in a single variable named "groupx". The "x" here will be interpreted as a literal character in the variable name, it will not resolve to the value of the "x" variable. If you want to save various values, the number of which you do not know in advance, you can use an array or a list object. Of these 2 methods, I prefer the list as it is easy to work with.

The code below is a variation of the code that you posted; it uses a list to hold any and all of the group objects found. After cycling through all groups looking for those named "Section 1" -> "Section 6", it uses a "For Each" loop to report the names of the groups it found. If you would like to access a certain group within the list, you can use the .Item property. For instance, if you would like to work with the third group in the list, you could use:
myGroups.Item(2)
The items are zero based so the first group would be .Item(0), the second .Item(1), etc.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module Module74

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
Dim workPart As Part = theSession.Parts.Work

Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim tmpGrpTag As NXOpen.Tag = NXOpen.Tag.Null
Dim tmpGroup As Group
Dim myGroups As New List(Of Group)

Do
theUfSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_group_type, tmpGrpTag)
'skip the initial null tag
If tmpGrpTag = NXOpen.Tag.Null Then
Continue Do
End If

tmpGroup = Utilities.NXObjectManager.Get(tmpGrpTag)
For x As Integer = 1 To 6
If tmpGroup.Name = "Section " & x Then
myGroups.Add(tmpGroup)
End If
Next
Loop Until tmpGrpTag = NXOpen.Tag.Null

'Do something with the groups we found
lw.WriteLine(myGroups.Count.ToString & " Groups found:")
For Each temp As Group In myGroups
lw.WriteLine(" " & temp.Name)
Next

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

Thanks for all the help!

Hi,
As I mentioned before I am working on a project to convert a 3D model to a 2D cross-section by taking cross sections at various angles and merging them together. My code is working fine but I realised that as the cross-sections are merged there are a lot of duplicate line segments created.
Is it possible to delete the duplicates? NX doesn't really have a tool that can be used in a journal and it seems impossible to write a generic code that can be used for different models.

What version of NX are you using? NX 10 and above has an "optimize 2D curve" command that might help to clean up the duplicates. I have not used the command in a journal, so I'm unsure of the required syntax. If you are using NX 10 or above, try recording a journal while using the command.

I am using NX9.

I suggest upgrading your NX if possible. Otherwise, you can write your own code to find and remove duplicate curves.

"it seems impossible to write a generic code that can be used for different models"

Difficult - yes, but take heart; it is not impossible.

Hi,
While I wasn't able to come up with a generic code, I realized that projecting the selected curves creates a single merged curve. This is a good enough result for my project. However I am facing a slight issue when it comes to selecting the curves. I figured the solution for this would be selecting the previously created groups but I'm not sure how to integrate the two.
Any advice would be appreciated.
Attached below is the journal used to move a single arc.

Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String)

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Insert->Derived Curve->Project...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim nullFeatures_Feature As Features.Feature = Nothing

If Not workPart.Preferences.Modeling.GetHistoryMode Then
Throw(New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
End If

Dim projectCurveBuilder1 As Features.ProjectCurveBuilder
projectCurveBuilder1 = workPart.Features.CreateProjectCurveBuilder(nullFeatures_Feature)

Dim origin1 As Point3d = New Point3d(0.0, 0.0, 0.0)
Dim normal1 As Vector3d = New Vector3d(0.0, 0.0, 1.0)
Dim plane1 As Plane
plane1 = workPart.Planes.CreatePlane(origin1, normal1, SmartObject.UpdateOption.WithinModeling)

Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("Inch"), Unit)

Dim expression1 As Expression
expression1 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1)

Dim expression2 As Expression
expression2 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1)

projectCurveBuilder1.CurveFitData.Tolerance = 1e-005

projectCurveBuilder1.CurveFitData.AngleTolerance = 0.5

projectCurveBuilder1.BridgedGapSize = 0.0393700787401575

projectCurveBuilder1.AngleToProjectionVector.RightHandSide = "0"

projectCurveBuilder1.BridgedGapSize = 0.0393700787401575

theSession.SetUndoMarkName(markId1, "Project Curve Dialog")

plane1.SetMethod(PlaneTypes.MethodType.FixedZ)

Dim geom1(-1) As NXObject
plane1.SetGeometry(geom1)

Dim origin2 As Point3d = New Point3d(2.3935356072252, 0.0, 0.0)
plane1.Origin = origin2

Dim matrix1 As Matrix3x3
matrix1.Xx = 1.0
matrix1.Xy = 0.0
matrix1.Xz = 0.0
matrix1.Yx = 0.0
matrix1.Yy = 1.0
matrix1.Yz = 0.0
matrix1.Zx = 0.0
matrix1.Zy = 0.0
matrix1.Zz = 1.0
plane1.Matrix = matrix1

plane1.SetAlternate(PlaneTypes.AlternateType.One)

plane1.Evaluate()

plane1.SetMethod(PlaneTypes.MethodType.FixedZ)

projectCurveBuilder1.SectionToProject.DistanceTolerance = 1e-005

projectCurveBuilder1.SectionToProject.ChainingTolerance = 9.5e-006

projectCurveBuilder1.SectionToProject.AngleTolerance = 0.5

projectCurveBuilder1.PlaneToProjectTo = plane1

projectCurveBuilder1.SectionToProject.SetAllowedEntityTypes(Section.AllowTypes.CurvesAndPoints)

workPart.ModelingViews.WorkView.FitAfterShowOrHide(View.ShowOrHideType.HideOnly)

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "section mark")

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, Nothing)
Dim curves1(0) As IBaseCurve
Dim arc227 As Arc = CType(workPart.Arcs.FindObject("ENTITY 5 368 1"), Arc)

curves1(0) = arc227
Dim curveDumbRule1 As CurveDumbRule
curveDumbRule1 = workPart.ScRuleFactory.CreateRuleBaseCurveDumb(curves1)

projectCurveBuilder1.SectionToProject.AllowSelfIntersection(True)

Dim rules1(0) As SelectionIntentRule
rules1(0) = curveDumbRule1
Dim nullNXObject As NXObject = Nothing

Dim helpPoint1 As Point3d = New Point3d(2.9990229769739, -3.28717888387713, 1.83637194466023e-016)
projectCurveBuilder1.SectionToProject.AddToSection(rules1, arc227, nullNXObject, nullNXObject, helpPoint1, Section.Mode.Create, False)

theSession.DeleteUndoMark(markId3, Nothing)

theSession.DeleteUndoMark(markId2, Nothing)

Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Project Curve")

theSession.DeleteUndoMark(markId4, Nothing)

Dim markId5 As Session.UndoMarkId
markId5 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Project Curve")

Dim nXObject1 As NXObject
nXObject1 = projectCurveBuilder1.Commit()

theSession.DeleteUndoMark(markId5, Nothing)

theSession.SetUndoMarkName(markId1, "Project Curve")

projectCurveBuilder1.SectionToProject.CleanMappingData()

projectCurveBuilder1.SectionToProject.CleanMappingData()

projectCurveBuilder1.Destroy()

Try
' Expression is still in use.
workPart.Expressions.Delete(expression2)
Catch ex As NXException
ex.AssertErrorCode(1050029)
End Try

Try
' Expression is still in use.
workPart.Expressions.Delete(expression1)
Catch ex As NXException
ex.AssertErrorCode(1050029)
End Try

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

Try recording another journal while projecting the curves, but this time use the "curves in group" selection method. You will end up with code similar to what you have above, except it will take a group as the input.

Dim groups1(0) As Group
Dim group1 As Group = CType(workPart.FindObject("ENTITY 15 1 1"), Group)

groups1(0) = group1
Dim curveGroupRule1 As CurveGroupRule
curveGroupRule1 = workPart.ScRuleFactory.CreateRuleCurveGroup(groups1)

Since you already have a reference to the curve group, use it instead of the call to the .FindObject method.

Just got a chance to try what you suggested. There doesn't seem to be a "curves in group" selection method. Is it a feature in NX 9?

Edit: I integrated the code you posted into the projected curves journal and with a bit of tweaking, it seems to be working fine. Thanks for all the help.

Yes, "curves in group" is available in NX 9. It is a curve selection rule similar to "single curve", "tangent curves", "connected curves", etc.