Add the lengths of curves in a Drive Curve Method

I am working on an NXOpen project and I need some help. I've taken some examples and some code that has kindly been given to me and have hacked it together to almost get what I want. Here is what I have so far:

1) user selects the desired Operations in the Operation Navigator. These will be operations that use the Curve/Point Drive Method. This drive method is a collection of curves seperated into sets. Each set is its own collection of curves.

2) user clicks a command button which is linked to an NXOpen dll of the code I am working on (see below)

3) the dll looks at each selection and counts the number of SETS in each operation.

4) a listing window opens and outputs the operation name and the number of sets in the operation.

This is not the final output I want. I want the dll to iterate the curves in each set and total the sum of the curve lengths. The final output I want is the sum of all of the curve lengths in all of the sets of all of the selected operations. The output will just be a decimal number of the final sum. I need help with the iteration of the curves and totaling the sum. Any help would be greatly appreciated!

My code:

Option Strict Off
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.CAM
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpenUI

Module countDriveCurveSets

Dim theSession As Session
Dim theUfSession As UFSession
Dim WorkPart As Part
Dim OPERATIONGROUP As String

Dim m_OperationList() As String

Sub Main()

theSession = Session.GetSession()
theUfSession = UFSession.GetUFSession()
WorkPart = theSession.Parts.Work

Dim setupTag As Tag
Dim selectedTags() As NXOpen.Tag
Dim selectedCount As Integer
Dim lw As ListingWindow = theSession.ListingWindow

' If there is a work part only then we can go further
If WorkPart IsNot Nothing Then

theUfSession.Cam.InitSession()
theUfSession.Setup.AskSetup(setupTag)

' If there is a setup only then we go further
If setupTag <> 0 Then
' Get the selected nodes from the Operation Navigator
theUfSession.UiOnt.AskSelectedNodes(selectedCount, selectedTags)

If selectedCount = 0 Then
MsgBox("No operations selected.", MsgBoxStyle.Exclamation, "Count Drive Curves")
Exit Sub
End If

Dim ptr As IntPtr = New System.IntPtr
Dim cycle_cb_fn As UFNcgroup.CycleCbFT = New UFNcgroup.CycleCbFT(AddressOf cycle_cb)

' 'WRITE RESULT DATA TO FILE & SCREEEN
lw.SelectDevice(ListingWindow.DeviceType.Window, "")
lw.Open()

Dim i As Integer
'Loop over the selected nodes to take action
For i = 0 To selectedCount - 1

Dim opName As NXObject = NXObjectManager.Get(selectedTags(i))

lw.WriteLine(opName.Name & " has " & action(selectedTags(i)) & " Drive Curve sets.")
lw.WriteLine("")

' Now if the selected item is a Group object then we need to cycle objects inside it
theUfSession.Ncgroup.CycleMembers(selectedTags(i), cycle_cb_fn, ptr)
Next i

lw.Close()
lw.SelectDevice(ListingWindow.DeviceType.Window, "")
End If

End If
End Sub

Function cycle_cb(ByVal camObjectTag As Tag, ByVal ptr As IntPtr) As Boolean

Dim answer As Boolean
' Every item needs to be checked to take action
answer = action(camObjectTag)
Return answer

End Function

Public Function action(ByVal camObjectTag As Tag) As Integer

Dim camObject As NXObject = NXObjectManager.Get(camObjectTag)
Dim operation As CAM.Operation
Dim surfaceContourBuilder As CAM.SurfaceContourBuilder
Dim driveMeth As CAM.DmCurveBuilder
Dim count As Integer

operation = CType(WorkPart.CAMSetup.CAMOperationCollection.FindObject(camObject.Name), CAM.Operation)
surfaceContourBuilder = WorkPart.CAMSetup.CAMOperationCollection.CreateSurfaceContourBuilder(operation)

driveMeth = surfaceContourBuilder.DmcurveBuilder

' Counts the number of Drive Curve sets in a Curve/Point Drive Method of the operation
count = driveMeth.DpmcvDriveCurves.Length

surfaceContourBuilder.Destroy()

Return count
End Function

End Module

Here is the code in a format a little easier to read:

Option Strict Off
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.CAM
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpenUI

Module countDriveCurveSets

Dim theSession As Session
Dim theUfSession As UFSession
Dim WorkPart As Part
Dim OPERATIONGROUP As String

Dim m_OperationList() As String

Sub Main()

theSession = Session.GetSession()
theUfSession = UFSession.GetUFSession()
WorkPart = theSession.Parts.Work

Dim setupTag As Tag
Dim selectedTags() As NXOpen.Tag
Dim selectedCount As Integer
Dim lw As ListingWindow = theSession.ListingWindow

' If there is a work part only then we can go further
If WorkPart IsNot Nothing Then

theUfSession.Cam.InitSession()
theUfSession.Setup.AskSetup(setupTag)

' If there is a setup only then we go further
If setupTag <> 0 Then
' Get the selected nodes from the Operation Navigator
theUfSession.UiOnt.AskSelectedNodes(selectedCount, selectedTags)

If selectedCount = 0 Then
MsgBox("No operations selected.", MsgBoxStyle.Exclamation, "Count Drive Curves")
Exit Sub
End If

Dim ptr As IntPtr = New System.IntPtr
Dim cycle_cb_fn As UFNcgroup.CycleCbFT = New UFNcgroup.CycleCbFT(AddressOf cycle_cb)

' 'WRITE RESULT DATA TO FILE & SCREEEN
lw.SelectDevice(ListingWindow.DeviceType.Window, "")
lw.Open()

Dim i As Integer
'Loop over the selected nodes to take action
For i = 0 To selectedCount - 1

Dim opName As NXObject = NXObjectManager.Get(selectedTags(i))

lw.WriteLine(opName.Name & " has " & action(selectedTags(i)) & " Drive Curve sets.")
lw.WriteLine("")

' Now if the selected item is a Group object then we need to cycle objects inside it
theUfSession.Ncgroup.CycleMembers(selectedTags(i), cycle_cb_fn, ptr)
Next i

lw.Close()
lw.SelectDevice(ListingWindow.DeviceType.Window, "")
End If

End If
End Sub

Function cycle_cb(ByVal camObjectTag As Tag, ByVal ptr As IntPtr) As Boolean

Dim answer As Boolean
' Every item needs to be checked to take action
answer = action(camObjectTag)
Return answer

End Function

Public Function action(ByVal camObjectTag As Tag) As Integer

Dim camObject As NXObject = NXObjectManager.Get(camObjectTag)
Dim operation As CAM.Operation
Dim surfaceContourBuilder As CAM.SurfaceContourBuilder
Dim driveMeth As CAM.DmCurveBuilder
Dim count As Integer

operation = CType(WorkPart.CAMSetup.CAMOperationCollection.FindObject(camObject.Name), CAM.Operation)
surfaceContourBuilder = WorkPart.CAMSetup.CAMOperationCollection.CreateSurfaceContourBuilder(operation)

driveMeth = surfaceContourBuilder.DmcurveBuilder

' Counts the number of Drive Curve sets in a Curve/Point Drive Method of the operation
count = driveMeth.DpmcvDriveCurves.Length

surfaceContourBuilder.Destroy()

Return count
End Function

End Module

RVogel

I finally figured out how to get this to work. This adds up all of the Sections (sets) in each selected opeation using the Curve/Point Drive Method. I would appreciate any advice on additions to the code for catching errors and exceptions, like operations selected that don't have the correct drive method or a group selected instead of operation(s).

Option Strict Off
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.CAM
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpenUI

Module countDriveCurveSets

Dim theSession As Session
Dim theUfSession As UFSession
Dim WorkPart As Part
Dim OPERATIONGROUP As String

Dim m_OperationList() As String

Sub Main()

theSession = Session.GetSession()
theUfSession = UFSession.GetUFSession()
WorkPart = theSession.Parts.Work

Dim setupTag As Tag
Dim selectedTags() As NXOpen.Tag
Dim selectedCount As Integer
Dim lw As ListingWindow = theSession.ListingWindow

' If there is a work part only then we can go further
If WorkPart IsNot Nothing Then

theUfSession.Cam.InitSession()
theUfSession.Setup.AskSetup(setupTag)

' If there is a setup only then we go further
If setupTag <> 0 Then
' Get the selected nodes from the Operation Navigator
theUfSession.UiOnt.AskSelectedNodes(selectedCount, selectedTags)

If selectedCount = 0 Then
MsgBox("No operations selected.", MsgBoxStyle.Exclamation, "Add Drive Curves")
Exit Sub
End If

Dim ptr As IntPtr = New System.IntPtr
Dim cycle_cb_fn As UFNcgroup.CycleCbFT = New UFNcgroup.CycleCbFT(AddressOf cycle_cb)

' 'WRITE RESULT DATA TO FILE & SCREEEN
lw.SelectDevice(ListingWindow.DeviceType.Window, "")
lw.Open()

Dim finalSum As Double

Dim i As Integer
'Loop over the selected nodes to take action
For i = 0 To selectedCount - 1

Dim opName As NXObject = NXObjectManager.Get(selectedTags(i))

lw.WriteLine(opName.Name & " has " & action(selectedTags(i)) & " inches selected")
lw.WriteLine("")

finalSum += action(selectedTags(i))

' Now if the selected item is a Group object then we need to cycle objects inside it
theUfSession.Ncgroup.CycleMembers(selectedTags(i), cycle_cb_fn, ptr)
Next i
lw.WriteLine("Final sum of all operations is: " & finalSum & " inches")
lw.Close()
lw.SelectDevice(ListingWindow.DeviceType.Window, "")
End If

End If
End Sub

Function cycle_cb(ByVal camObjectTag As Tag, ByVal ptr As IntPtr) As Boolean

Dim answer As Boolean
' Every item needs to be checked to take action
answer = action(camObjectTag)
Return answer

End Function

Public Function action(ByVal camObjectTag As Tag) As Double

Dim camObject As NXObject = NXObjectManager.Get(camObjectTag)

Dim operation As CAM.Operation
Dim surfaceContourBuilder1 As CAM.SurfaceContourBuilder
Dim totalLength As Double
Dim driveChainItemBuilder1() As CAM.DriveChainItemBuilder
Dim curveObjects(0) As NXOpen.NXObject

totalLength = 0.0

operation = CType(WorkPart.CAMSetup.CAMOperationCollection.FindObject(camObject.Name), CAM.Operation)
surfaceContourBuilder1 = WorkPart.CAMSetup.CAMOperationCollection.CreateSurfaceContourBuilder(operation)
driveChainItemBuilder1 = surfaceContourBuilder1.DmcurveBuilder.DpmcvDriveCurves.GetContents

For i = 0 To driveChainItemBuilder1.Count - 1
Dim gSection As Section = driveChainItemBuilder1.ElementAt(i).CurveItem
gSection.GetOutputCurves(curveObjects)
For Each aCurve As Curve In curveObjects
totalLength += aCurve.GetLength
Next
Next
surfaceContourBuilder1.Destroy()

Return totalLength

End Function

End Module

RVogel