CoG Journal - how to check if the CoG already exist

Hello,

I have Journal for creating CoG Point into NX Modelling.
Everything works well, but I would like to check if the point was created. If the point already exists, I would like to override it by new dimensions (if the geometry change).

Can anyone help me with this?

Thank you in advance for any help


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

Module Module1

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

Sub Main()

Dim workPart As Part = theSession.Parts.Work

lw.Open()
Dim max As Integer
If workPart.PartUnits = Part.Units.Inches Then
max = 10
Else 'metric file
max = 250
End If
Const undoMarkName As String = "report assembly mass props"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

Dim bodyTags As New List(Of Tag)
bodyTags = AskAllBodyTags(theSession.Parts.Display)

Dim acc_value(10) As Double
acc_value(0) = 0.999
Dim mass_props(46) As Double
Dim stats(12) As Double

Dim acc_value2(10) As Double
acc_value2(0) = 0.999
Dim mass_props2(46) As Double
Dim stats2(12) As Double
If workPart.PartUnits = Part.Units.Inches Then
lw.writeline("inches")
theUfSession.Modl.AskMassProps3d(bodyTags.ToArray, bodyTags.Count, 1, 1, 0.03, 1, acc_value, mass_props, stats)
Else
lw.writeline("milimetres")
theUfSession.Modl.AskMassProps3d(bodyTags.ToArray, bodyTags.Count, 1, 4, 0.03, 1, acc_value, mass_props, stats)
mass_props(3)=mass_props(3)*1000
mass_props(4)=mass_props(4)*1000
mass_props(5)=mass_props(5)*1000
mass_props(2)=mass_props(2)/.45
End If

lw.WriteLine("Mass in lbm: " & mass_props(2).ToString & " lbm")
lw.WriteLine("Mass in kg: " & (mass_props(2)*0.45).ToString & " kg")
lw.WriteLine("Center of Mass:")
lw.writeline("X: " & mass_props(3).ToString)
lw.writeline("Y: " & mass_props(4).ToString)
lw.writeline("Z: " & mass_props(5).ToString)

Dim p1 = NXOpen.Session.GetSession.Parts.Work.Points.CreatePoint(New Point3d(mass_props(3), mass_props(4), mass_props(5)))
p1.SetVisibility(SmartObject.VisibilityOption.Visible)

Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature = Nothing

Dim pointFeatureBuilder1 As NXOpen.Features.PointFeatureBuilder = Nothing
pointFeatureBuilder1 = workPart.BaseFeatures.CreatePointFeatureBuilder(nullNXOpen_Features_Feature)

pointFeatureBuilder1.Point = p1

Const sketchLayer As Integer = 110

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.NewLayer = sketchLayer

displayModification1.Apply(workPart.Points.ToArray)

displayModification1.Dispose()

Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = pointFeatureBuilder1.Commit()
nXObject1.SetName("CoG")

pointFeatureBuilder1.Destroy()
lw.Close()

End Sub

Function AskAllBodyTags(ByVal thePart As Part) As List(Of Tag)

Dim theBodyTags As New List(Of Tag)
Dim aBodyTag As Tag = Tag.Null
Do
theUfSession.Obj.CycleObjsInPart(thePart.Tag,
UFConstants.UF_solid_type, aBodyTag)
If aBodyTag = Tag.Null Then
Exit Do
End If

Dim theType As Integer, theSubtype As Integer
theUfSession.Obj.AskTypeAndSubtype(aBodyTag, theType, theSubtype)
If theSubtype = UFConstants.UF_solid_body_subtype Then

Dim bodyType As Integer = 0
theUfSession.Modl.AskBodyType(aBodyTag, bodyType)
If bodyType = UFConstants.UF_MODL_SOLID_BODY Then
theBodyTags.Add(aBodyTag)
End If

End If

Loop While True

Return theBodyTags

End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

End Function

End Module

It appears that your code creates a point feature named "CoG"; you could add code at the beginning to search for this named point feature and take action accordingly.

Alternatively, depending on the NX version you are using, you could create an associative measurement with the "timestamp" option turned off. This would force the measurement to remain at the end of the feature tree and update after any part changes; just be sure to add any new components to the measurement feature as they are added to the assembly.

Hello,

Thanks for the response, I managed to do it as you said :)

Best Regards