setting Solid Density to Zero for all Extracted Bodies

Hi!

I am trying to set the Solid Density to Zero (0.0) for ALL (!) Extracted Bodies in the Work Part.

There is no Selection Filter available for the Feature "Extracted Body".

Is there a way to manage this by journal?


Thanks for help, Martin

Try this code:





Option Strict Off
Imports System
Imports NXOpen

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const undoMarkName As String = "extract body zero density"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

For Each myFeature As Features.Feature In workPart.Features
If TypeOf (myFeature) Is Features.ExtractFace Then
Dim extFeature As Features.ExtractFace = myFeature
extFeature.MakeCurrentFeature()
For Each theBody As Body In extFeature.GetBodies
theBody.Density = 0
Next

End If
Next

'make last feature current
Dim lastNum As Integer = workPart.Features.ToArray.GetUpperBound(0)
Dim lastFeature As Features.Feature = workPart.Features.ToArray(lastNum)
lastFeature.MakeCurrentFeature()

lw.Close()

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

Hello!

Thanks for the code.

But the density of bodies from features "Extracted Body" AND "Linked Body" is set to "0".

Can the code be changed that bodies ONLY from features "Extracted Body" will be set to "0" ?

Many thanks for your help!

Kind regards, Martin

Try changing line 24 to:



If TypeOf (myFeature) Is Features.ExtractFace And myFeature.FeatureType.Contains("EXTRACT") Then

Hello!

That works perfect!

I´m grateful for the possibility to post questions and receiving helpful answers.

Thanks a lot.

Kind regards, Martin

Would it be possible to change the density of a body selected inside a different part (in the context of assembly)? It would need to be picked by user.
For UG it looks like the body selected in "Entire Part" differs from the same body selected using "Within Workpart" filter.

I experience problems when a body is selected
In scope of
Selection.SelectionScope.AnyInAssembly
and not
Selection.SelectionScope.WorkPart

because the .Tag is different. Is it possible to "translate" body_Tag from assembly to workpart level.

Once you have a reference to a body object, you can check the .IsOccurrence property; if .IsOccurrence returns "True", then you have a reference to a "component" body that is controlled by the assembly. You can then use the .Prototype property to get the prototype body (the one defined in the part file). The part file that defines the geometry will need to be fully loaded and you will need write access to the file to change and save the part.

The problem with the prototype object is that mybody.Prototype it is not a Body object any more, so the mybody.Prototype.Tag doesnt work. I was able to track back the Prototype body Tag using:
Dim nxBody_tag As Tag = theUfSession.Assem.AskPrototypeOfOcc(myBody.Tag)

Then I change the workpart before setting the density, but you may be right that the only thing I need is to have the part fully loaded.

I forget that many problems I encounter is because parts are not loaded fully.

The code below will allow you to select a body from the assembly and will report some information on the body and the prototype body (if an occurrence was selected).

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

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

Dim theUI As UI = UI.GetUI()
Dim lw As ListingWindow = theSession.ListingWindow

Sub Main()

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJ")

lw.Open()

Dim theBody As Body = Nothing
If SelectBody("select a body", theBody) = Selection.Response.Cancel Then
Return
End If

lw.WriteLine("selected body tag: " & theBody.Tag.ToString)
lw.WriteLine(".OwningPart: " & theBody.OwningPart.Leaf)
lw.WriteLine(".IsOccurrence: " & theBody.IsOccurrence.ToString)
Dim protoBody As Body = Nothing
If theBody.IsOccurrence Then
protoBody = theBody.Prototype
End If

If IsNothing(protoBody) Then
Return
End If

lw.WriteLine(".OwningComponent: " & theBody.OwningComponent.DisplayName)
lw.WriteLine("prototype body tag: " & protoBody.Tag.ToString)
lw.WriteLine("type: " & protoBody.GetType.ToString)
lw.WriteLine("proto body owning part: " & protoBody.OwningPart.Leaf)

lw.Close()

End Sub

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

Dim theUI As UI = UI.GetUI
Dim title As String = "Select a body"
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.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
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

Public Function GetUnloadOption(ByVal dummy As String) As Integer

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

End Function

End Module

Thanks,

in documentation the prototype is INXObject
But I did not try to change type to Body before I wanted to get the Tag from it.
In your code it looks like it is forced as you define

Dim protoBody As Body
and then
protoBody = theBody.Prototype

right?

You will notice that the first line of my journal is:
Option Strict Off
This allows the compiler to do implicit conversions. The code lines that you have pointed out tells the compiler to cast the prototype to a body object. In this case, since the selection function only allows the user to select a body, we know the conversion to a body type will be successful.

Had we used
Option Strict On
the code as written would not even run; the compiler would complain that it cannot convert the object. We would need to add code to explicitly convert the object types as needed.