Selecting and Deleting Small Solid Bodies

I have a problem where an imported Printed Circuit Board includes thousands of small bodies that need to be deleted.

Does anyone have any ideas about how to do that?

Best Regards

Jon Schmidt

The main issue here is devising a test that differentiates between a body that should be deleted and one that should be kept. If all the bodies are in the same part file (not an assembly), you can iterate through the .Bodies collection and test each body. Perhaps you measure each body and if the volume is under a certain value, it is marked for deletion. Alternately, you could look at the bounding box of the body and if one or more dimensions are below your threshold, it would be considered "small" and get marked for deletion.

Once you define "small" vs. "not small", the journal implementation should be fairly easy.

Thanks I found journal on another forum, I guess that you made it for selecting curves shorter than a given length and then deleting them.

I am thinking that I will try to find similar functions for solids as were used for curves and then substitute them.

Never having done this type of journal (where API is called) I am that I will find the functions in an API reference file and try to rework this example.

The idea of using a bounding box and then getting the volume of it sounds like a good plan.

I will try to do it this weekend.

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

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim smallCurves As New List(Of Curve)

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'%% curves less than or equal to the following length will be deleted
'%% change this value to your requirement
Const smallCurveCutoff As Double = 0.025
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

For Each tempCurve As Curve In workPart.Curves

If tempCurve.GetLength <= smallCurveCutoff Then
smallCurves.Add(tempCurve)
End If

Next

If smallCurves.Count > 0 Then

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Delete small curves")

Dim notifyOnDelete1 As Boolean
notifyOnDelete1 = theSession.Preferences.Modeling.NotifyOnDelete

theSession.UpdateManager.ClearErrorList()

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(smallCurves.ToArray)

Dim notifyOnDelete2 As Boolean
notifyOnDelete2 = theSession.Preferences.Modeling.NotifyOnDelete

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId1)

End If

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

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

'----Other unload options-------
'Unloads the image immediately after execution within NX
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'-------------------------------

End Function

End Module

Best Regards

Jon Schmidt

I made a "Hail Marry" attempt to convert the journal above to work on Solid Bodies.
I got the following (not surprising)

'GetVolume" is not a member of 'NXOpenBody'

I am trying to find a source for the functions that are available for NXOpenBody that I can use to measure the volume.

I am looking here, but not finding what I am looking for, is this the right place to look? I am working in NX12.

https://docs.plm.automation.siemens.com/data_services/resources/nx/12/nx...

Here is what I have tried so far:

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

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim smallBodies As New List(Of Body)

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'%% Bodies less than or equal to the following volume will be deleted
'%% change this value to your requirement
Const smallVolumeCutoff As Double = 0.002
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

For Each tempBody As Body In workPart.Bodies

If tempBody.GetVolume <= smallVolumeCutoff Then
smallBodies.Add(tempBody)
End If

Next

If smallBodies.Count > 0 Then

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Delete small bodies")

Dim notifyOnDelete1 As Boolean
notifyOnDelete1 = theSession.Preferences.Modeling.NotifyOnDelete

theSession.UpdateManager.ClearErrorList()

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(smallBodies.ToArray)

Dim notifyOnDelete2 As Boolean
notifyOnDelete2 = theSession.Preferences.Modeling.NotifyOnDelete

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId1)

End If

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

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

'----Other unload options-------
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'-------------------------------

End Function

End Module

Best Regards

Jon Schmidt

There is a journal here that shows how to take some measurements of a body and find the bounding box dimensions.

http://nxjournaling.com/comment/5288#comment-5288

Here is a working journal

'Search the part for some bodoes and delete the ones smaller than an entered bounding box volume.

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

Module Module1

Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession

Sub Main()
Dim theUISession As UI = UI.GetUI
Dim smallVolumeCutoffString As String = ""
Dim smallVolumeCutoffStringConst As String

'input box: prompt and title
Dim smallVolumeCutoff = CDbl(NXInputBox.GetInputString("0.002 Might ber a good size to begin with", "Enter Maximum Bounding Volume"))

Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim workPart As Part = theSession.Parts.Work
Dim theBodies As New List(Of Body)
Dim smallBodies As New List(Of Body)

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'%% Bodies less than or equal to the following volume will be deleted
'%% change this value to your requirement
'Const smallVolumeCutoff As Double = 0.002
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

'gather solid and sheet bodies into a list
For Each temp As Body In workPart.Bodies
theBodies.Add(temp)
Next

'trim the list to
For Each temp As Body In theBodies
If CDbl(BoundingBoxVolume(temp)) <= smallVolumeCutoff Then
smallBodies.Add(temp)
End If
Next

'count the bodies and delete them
If smallBodies.Count > 0 Then
lw.WriteLine("smallBodies.Count = " & smallBodies.Count)

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Delete small bodies")

Dim notifyOnDelete1 As Boolean
notifyOnDelete1 = theSession.Preferences.Modeling.NotifyOnDelete

theSession.UpdateManager.ClearErrorList()

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(smallBodies.ToArray)

Dim notifyOnDelete2 As Boolean
notifyOnDelete2 = theSession.Preferences.Modeling.NotifyOnDelete

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId1)

End If

End Sub

Public Function BoundingBoxVolume(ByVal tempObj As NXObject) As Double

Dim bbox(5) As Double

theUFSession.Modl.AskBoundingBox(tempObj.Tag, bbox)
Return (bbox(3) - bbox(0)) * (bbox(4) - bbox(1)) * (bbox(5) - bbox(2))

End Function

End Module

Best Regards

Jon Schmidt