How to create a script that will count features in a model

I haven't written a real script in NX in a very long time and I'm hoping someone can share some knowledge. Thanks for your time.

I have a model that has thousands of holes and sometimes thousands of slots. I want to create a script that will allow me to easily count the number of holes or slots.

More than likely there will be other holes in the model, so it's important that the user be able to refine their search criteria, by referring to a specific feature or a specific diameter or range of diameters (diam 2.0 - 2.2).

Please Help. :)

Are these models parameterized, do these holes and slots show up in the part navigator as features?

Also, what version of NX are you running?

Running Version 8.5.3.3

These are features that are created using holes, slots or sketches. Most times these features are then multiplied by creating arrays through feature array or ,sometimes, synchronous modeling.

Hi,
recently I have made a journal that finds all bodies's features and goes on analysing them, looking for attributes.
I guess you might use some bits and pieces for you own purpose?

'report all features Attributes
'
'Based on two sample scripts made by GTAC
'07.07.2016 / UM

Imports System
Imports System.Collections 'für ArrayLists
Imports NXOpen
Imports NXOpen.Features
Imports NXOpen.UF

Public Class report_all_hole_features

' class members
Private Shared theSession As Session
Private Shared theUFSession As UFSession
Private Shared theUI As UI
Private Shared lw As ListingWindow
Private Shared workPart As Part

Public Shared Function Main(ByVal args() As String) As Integer
theSession = Session.GetSession
theUFSession = UFSession.GetUFSession
theUI = UI.GetUI
workPart = theSession.Parts.Work
lw = theSession.ListingWindow
lw.Open

' Undo-Marke setzen
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Change Translucency")

' Alle Features erhalten
Dim feat As Feature
Dim featColl As FeatureCollection = workPart.Features
Dim classType As String = nothing
Dim featType As String = nothing

' Alle Features durchlaufen
For Each feat in featColl
featType = feat.FeatureType
classType = feat.GetType().FullName

If featType = "HOLE PACKAGE" AND _
classType = "NXOpen.Features.HolePackage" Then
report_feature_info(feat)
report_holepackage_feature(feat)
Else If featType = "HOLE ORCHESTRATION" Then
report_feature_info(feat)
report_holeseries_feature(feat)
Else If featType = "SIMPLE HOLE" AND _
classType = "NXOpen.Features.Hole" Then
report_feature_info(feat)
report_hole_feature(feat)
End If

lw.Writeline("")
lw.WriteLine(CStr(feat.GetFeatureName) & " - Custom Name: " & Cstr(feat.Name) &" - Timestamp: (" & CStr(feat.timestamp) & ")")
lw.WriteLine(" Feature Type: " & CStr(featType) & " - Class Type:" & CStr(classType))
LW.Writeline(" Anzahl erzeugte Entities: " & feat.GetEntities.Length)
'Problem: Anzahl Entities = 0

'Alle Attribute des aktuellen Features erhalten
Dim theAttributes() As NXObject.AttributeInformation
theAttributes = feat.GetUserAttributes(True)

Dim title As String
Dim value As String
Dim attType As String
Dim attReadOnly As String
Dim attInherited As String

' Alle Attribute des aktuellen Features (feat) durchlaufen
For Each temp As NXObject.AttributeInformation In theAttributes
title = temp.Title
value = ""
'lw.WriteLine(" title: " & title)

Select Case temp.Type
Case Is = NXObject.AttributeType.Boolean
value = temp.BooleanValue.ToString
attType = "Boolean"
Case Is = NXObject.AttributeType.Integer
value = temp.IntegerValue.ToString
attType = "Integer"
Case Is = NXObject.AttributeType.Real
value = temp.RealValue.ToString
attType = "Real"
Case Is = NXObject.AttributeType.String
value = temp.StringValue
attType = "String"
Case Is = NXObject.AttributeType.Time
value = temp.TimeValue.ToString
attType = "Time"
Case Else
value = "value type not supported by this journal"
attType = "N/A"
End Select

'lw.WriteLine(" value: " & value)
lw.WriteLine(" Attribut: [" & title & "]-[" & value & "]")
'lw.WriteLine("")
Next
Next
Return 0

End Function

Public Shared Function report_feature_info(ByRef feat As Feature)
lw.WriteLine("")
lw.WriteLine("Feature: " & feat.ToString _
& " name: " + feat.GetFeatureName() _
& " type: " & feat.FeatureType _
& " Class type: " & feat.GetType().FullName)
End Function

Public Shared Function report_holepackage_feature(ByRef hpFeat As HolePackage) As Integer

Dim hpBuilder As HolePackageBuilder = workPart.Features.CreateHolePackageBuilder(hpFeat)
Dim hType As HolePackageBuilder.Types = hpBuilder.Type
lw.WriteLine(" Type: " & hType.ToString())
If hType = HolePackageBuilder.Types.GeneralHole Then
Dim hForm As HolePackageBuilder.HoleForms = hpBuilder.GeneralHoleForm
lw.WriteLine(" Form: " & hForm.ToString())
End If
hpBuilder.Destroy()

Dim bodies() As Body = hpFeat.GetBodies()
For Each bd As Body in bodies
lw.WriteLine(" Body: " & bd.ToString())
Next

Dim origins() As Point3d
hpFeat.GetOrigins(origins)
For Each pt As Point3d in origins
lw.WriteLine(" Origin: " & pt.ToString())
Next

Dim directions() As Vector3d
hpFeat.GetDirections(directions)
For Each dir As Vector3d in directions
lw.WriteLine(" Direction: " & dir.ToString())
Next

' To extract specific values, use NXOpen.Features.HolePackage
Dim exprs() As Expression = hpFeat.GetExpressions()
For Each exp As Expression in exprs
lw.WriteLine(" Expression: " & exp.Equation)
Next

End Function

Public Shared Function report_holeseries_feature(ByRef hpFeat As HolePackage) As Integer

Dim shf As Feature = nothing
shf = hpFeat.GetHoleSeriesStartHoleFeature()
If shf IsNot nothing Then
lw.WriteLine(" Start Feature :" & shf.ToString())
report_holepackage_feature(shf)
End If

Dim mhfs() As Feature = hpFeat.GetHoleSeriesMiddleHoleFeatures()
For Each mhf As Feature in mhfs
lw.WriteLine(" Middle Feature :" & mhf.ToString())
report_holepackage_feature(mhf)
Next

Dim ehf As Feature = nothing
ehf = hpFeat.GetHoleSeriesEndHoleFeature()
If ehf IsNot nothing Then
lw.WriteLine(" End Feature :" & ehf.ToString())
report_holepackage_feature(ehf)
End If

End Function

Public Shared Function report_hole_feature(ByRef hFeat As Hole) As Integer

lw.WriteLine(" Subtype: " & hFeat.GetSubtype().ToString())

Dim bodies() As Body = hFeat.GetBodies()
For Each bd As Body in bodies
lw.WriteLine(" Body: " & bd.ToString())
Next

lw.WriteLine(" Origin: " & hFeat.Location.ToString)

Dim dir_x(2),dir_y(2) As Double
theUFSession.Modl.AskFeatDirection(hFeat.Tag, dir_x, dir_y)
lw.WriteLine( String.Format(" Direction: [X={0}, Y={1}, Z={2}]", dir_x(0), dir_x(1), dir_x(2)))

lw.WriteLine(" Diameter: " & hFeat.Diameter.Equation)
If Not hFeat.Depth Is Nothing Then
lw.WriteLine(" Depth: " & hFeat.Depth.Equation)
Else
lw.WriteLine(" Depth: Through Hole")
End If

End Function

Public Shared Function select_feature(ByRef feat As Feature) As Selection.Response
Dim selobj As NXObject
Dim cursor As Point3d
Dim typeArray() As Selection.SelectionType = _
{ Selection.SelectionType.Features }

Dim resp As Selection.Response = theUI.SelectionManager.SelectObject("Select feature", _
"Select feature", Selection.SelectionScope.WorkPart, false, typeArray, selobj, cursor)
If ((resp = Selection.Response.ObjectSelected) _
OrElse (resp = Selection.Response.ObjectSelectedByName)) Then
feat = CType(selobj,Feature)
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If
End Function

Public Shared Function GetUnloadOption(ByVal arg As String) As Integer
Return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately)
End Function

End Class

/UdoMM

Thank you for the info. I was hoping to create a script that would prompt the user to select a feature/s that they would like counted. Any ideas on how to incorporate that?

Hi,

to let the user select a feature, you may use something like this:

'select feature

Option Strict Off

Imports NXOpen
 
Module SelectFeature

Sub Main()
Dim prompt As String
SelectAFeature(prompt)
End Sub

Function SelectAFeature(ByRef prompt As String) As Features.Feature

Dim mask() As Selection.SelectionType = {Selection.SelectionType.Features}
Dim cursor As Point3d = Nothing
Dim sel_obj As TaggedObject = Nothing

UI.GetUI.SelectionManager.SelectTaggedObject(prompt,
"Select a Feature", Selection.SelectionScope.WorkPart, False,
mask, sel_obj, cursor)

Return CType(sel_obj, Features.Feature)

End Function
End Module

/UdoMM