Measure length of all curves in a component

I need some help getting started with this. I want to create a Journal that will return the sum of all curve lengths in a selected component in the Assembly Navigator. The component will contain only curves, but they may or may not be connected.

I have done a little Journaling and have used Visual Studio to generate a dll for a command button in NX, so I have some knowlegde but not enough to get me going on this.

A part has a "Curves" collection that will give you access to all the curves in the part. If you are starting with a component reference, first you will need to get to the parent "part"; you can use componentReference.Prototype.OwningPart to get a reference to the part file.

I'll try to post some example code when I have access to NX tomorrow...


Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Sub Main()

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

Dim myComponent As Assemblies.Component

If SelectComponent("select component", myComponent) = Selection.Response.Cancel Then
Return
End If

'get the part from the component
Dim myPart As Part = myComponent.Prototype.OwningPart
Dim totalCurveLength As Double

'iterate through the curve collection, add curve length to the running total
For Each aCurve As Curve In myPart.Curves
totalCurveLength += aCurve.GetLength
Next

'report the component name, part file name, and total curve length
lw.WriteLine("Component: " & myComponent.DisplayName)
lw.WriteLine("Component Part: " & myPart.FullPath)
lw.WriteLine("Total curve length: " & totalCurveLength.ToString)

lw.Close()

End Sub

Function SelectComponent(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select a component"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_component_type
.Subtype = UFConstants.UF_component_subtype
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

End Module

This is excellent, thank you! I am running into a problem though. I select the component within the assembly and the output window shows the same name for myComponent and myPart, and zero curves.

I've loaded the module into VS and am going to try to use intellisense to figure out what I can do with it, but any additional help would be greatly appreciated. Thanks again!

RVogel

I neglected to point out the name for myPart and myComponent that outputs is the parent part of the assembly, and not the component I select. And when I select the component, the entire parent assembly highlights.

RVogel

Okay, I did some testing and found that if I create a part from scratch with the desired components, etc., it worked perfectly.
I then did some testing on the real files that I want to work with and found that I needed to make the immediate parent of the component I wanted to measure the displayed part. Then all worked as needed. I have multiple assemblies and components nested within a file and that was confusing the Journal.

Thank you.

RVogel