Hello NX Enthusiasts!
I'm excited to share a series of NX journals that were developed with a little help from my AI assistant. Over the past month, we've put a lot of effort into creating and refining these tools, aiming to enhance our workflow efficiency in Siemens NX as a Joinery Designer.
These five journals, along with their versions, are designed to address specific challenges in Material Weight Management and streamline processes in ways not previously shared in the community. I hope they will not only serve as practical tools for projects but also as learning resources to inspire further innovation. All of them work in a single-threaded environment, as I have neither an Author license nor a Material license.
I want to express my heartfelt gratitude to everyone here who has contributed their sample codes and insights. Your valuable input served as the fundamental building blocks for the success of this little project. I sincerely hope that others will find these resources as useful as I have.
Happy coding, and let's keep pushing the boundaries of what's possible with NX!
1. Weight Calculation in a Multi-body environment for solid bodies
I created several versions of the Material Journal and placed them on the ribbon with colored icons. Icons are included in the zip file and can be modified with Krita or other free open-source image editors.
Original thread used as a base code and guide for my first journal:
https://nxjournaling.com/content/random-appearance-color-selected-compon...
- Material Journal - EW_Material_17mmGRP: Changes body color, moves it to a layer, sets a density value, calculates weight, and attributes it: EW_Body_Weight.
Added built in Material: As an addition, the built-in material added to the solid body. You have to create your own Material Library to use this. See code for further details.
- Face Material Journal - EW_Material_FACE_INSIDE: Alters the color of selected faces. Has priority over the main Material Journal. Used to distinguish the inside/outside of the body.
- Raw Body Journal - EW_Material_RAW BODY: By selecting the original body and the raw body, this calculates the weight difference and adds a new attribute: EW_Raw_Body_Diff_Weight. Crucial to us for handling this on a custom level.
- Delete Attributes Journal - EW_Material_DELETE ATTRIBUTES: Keeps the body unchanged but removes any weight-related attributes. Created so these bodies won’t be included in the weight calculation on the drawing.
2. Solid Body Material Filter Tool
Using this tool, you can control the visibility of specific solid bodies on your screen using the attributes assigned before. When creating components, it simplifies the process of organizing them. This tool automatically adjusts visibility based on the chosen materials. If no attribute is found, it hides them among the others. Without Weight option at the bottom displays all bodies without any weight information, so you can double check your work.
https://www.nxjournaling.com/content/easy-material-weight-management-sol...
3. Component Creator
This tool enables you to automatically create parts in Teamcenter by requesting you a main component number. For example, "X17068219-01" creates: X17068219-01-101, X17068219-01-102, etc. Select solid bodies to create components for.
To utilize this, I recommend recording a journal in your environment that solely focuses on creating a component using 'Assemblies / New Component.' See code for further details.
The tool comes in two versions:
Smart Sorting: This version sorts the selected bodies by material name in descending order (The first 2 digits in the assigned material name, if there’s none, it will do alphabetically). If multiple bodies have the same material, it will then sort them by weight, also in descending order.
Simple Select: This version won't sort your selection. Instead, it will preserve the order in which you initially clicked on the bodies for selection.
Features Common to Both Versions:
To set the Teamcenter’s ID number, the tool searches for the first sequential component, labeled "101." If none exists, it will create one and save it. Next, the tool employs a 10* to automatically generate the next available component number. To avoid duplication, the solid bodies are marked with an attribute. These components aren't saved; they are created for you to save if you are satisfied with the outcome. The names for the components will be derived from the names of the solid bodies. If a solid body does not have an assigned name, a default name, “Body” will be used.
https://www.nxjournaling.com/content/easy-material-weight-management-com...
4. Component Weight Transfer
In the Modeling environment/Main Assembly, this journal transfers weight information (weight attribute - EW_Body_Weight) from solid bodies to components. Summarizes all component weights to assign a Total Assembly Weight attribute to the Main Assembly, excluding weights of underlying components.
https://www.nxjournaling.com/content/easy-material-weight-management-com...
5. Total Weight to Drawings
In the Drafting environment, sums all solid body weights for a Total Built-in Weight and adds Raw body differences for a Total Environmental Weight in the title block. Does not require EW_Component Weight Transfer Journal.
https://www.nxjournaling.com/content/easy-material-weight-management-tot...
Important to know:
Weight is calculated during the Material Journal to a solid body. Journals are not associative. Any geometry changes require Journal reapplication.
Material Journal:
Imports System Imports NXOpen Imports System.Collections.Generic Imports NXOpen.UF Module NXJournal Dim theSession As Session Dim theUFSession As UFSession Dim workPart As Part Dim displayPart As Part Dim mySelectedObjects As New List(Of DisplayableObject) Dim theUI As UI Sub Main(ByVal args() As String) Try theSession = Session.GetSession() theUFSession = UFSession.GetUFSession() workPart = theSession.Parts.Work displayPart = theSession.Parts.Display theUI = UI.GetUI() Dim markId1 As Session.UndoMarkId = Nothing markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Material Journal") If SelectObjects("Hey, select multiple somethings", mySelectedObjects) = Selection.Response.Ok Then For Each tempComp As Body In mySelectedObjects Dim density As Double = 1017 ' Kg/m3 - Change this value to your own specific density Dim displayModification1 As DisplayModification displayModification1 = theSession.DisplayManager.NewDisplayModification() With displayModification1 .ApplyToAllFaces = False .ApplyToOwningParts = True .NewColor = 117 ' Set the solid body color to 117 .NewLayer = 1 ' Set the solid body to layer 1 .Apply({tempComp}) End With displayModification1.Dispose() DeleteAllAttributes(tempComp) AddBodyAttribute(tempComp, "EW_Material", "17mm GRP") ' Material name - Change this name to your own material Dim attributePropertiesBuilder1 As AttributePropertiesBuilder attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, {tempComp}, AttributePropertiesBuilder.OperationType.None) attributePropertiesBuilder1.Title = "EW_Material_Density" attributePropertiesBuilder1.DataType = NXOpen.AttributePropertiesBaseBuilder.DataTypeOptions.Number attributePropertiesBuilder1.Units = "MilliMeter" attributePropertiesBuilder1.Units = "KilogramPerCubicMeter" attributePropertiesBuilder1.NumberValue = density Dim nXObject1 As NXObject nXObject1 = attributePropertiesBuilder1.Commit() attributePropertiesBuilder1.Destroy() ' Calculate volume using mass properties Dim myMeasure As MeasureManager = workPart.MeasureManager() Dim massUnits(4) As Unit massUnits(1) = workPart.UnitCollection.GetBase("Volume") Dim mb As MeasureBodies = myMeasure.NewMassProperties(massUnits, 0.99, {tempComp}) mb.InformationUnit = MeasureBodies.AnalysisUnit.KilogramMeter ' Extract volume and multiply it by density to get weight Dim bodyVolume As Double = mb.Volume Dim bodyWeight As Double = bodyVolume * density ' Create or update an attribute named 'EW_Body_Weight' and assign the weight value to it Dim attributePropertiesBuilderForWeight As AttributePropertiesBuilder attributePropertiesBuilderForWeight = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, {tempComp}, AttributePropertiesBuilder.OperationType.Create) attributePropertiesBuilderForWeight.Title = "EW_Body_Weight" attributePropertiesBuilderForWeight.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Number attributePropertiesBuilderForWeight.Units = "Kilogram" attributePropertiesBuilderForWeight.NumberValue = bodyWeight Dim attributeObjectForWeight As NXObject = attributePropertiesBuilderForWeight.Commit() attributePropertiesBuilderForWeight.Destroy() Next End If Catch ex As Exception ' Handle exceptions here Console.WriteLine("Houston, we have a situation... an error occurred: " & ex.Message) End Try End Sub Function SelectObjects(prompt As String, ByRef dispObj As List(Of DisplayableObject)) As Selection.Response Dim selObj As NXObject() Dim title As String = "Select solid bodies - 17mm GRP" Dim includeFeatures As Boolean = False Dim keepHighlighted As Boolean = False Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart Dim selectionMask_array(0) As Selection.MaskTriple With selectionMask_array(0) .Type = UFConstants.UF_solid_type .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY End With Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj) If resp = Selection.Response.ObjectSelected Or resp = Selection.Response.ObjectSelectedByName Or resp = Selection.Response.Ok Then For Each item As NXObject In selObj dispObj.Add(CType(item, DisplayableObject)) Next Return Selection.Response.Ok Else Return Selection.Response.Cancel End If End Function Sub AddBodyAttribute(ByVal theBody As Body, ByVal attTitle As String, ByVal attValue As String) Dim attributePropertiesBuilder1 As AttributePropertiesBuilder attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, {theBody}, AttributePropertiesBuilder.OperationType.None) attributePropertiesBuilder1.IsArray = False attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String attributePropertiesBuilder1.Title = attTitle attributePropertiesBuilder1.StringValue = attValue Dim nXObject1 As NXObject nXObject1 = attributePropertiesBuilder1.Commit() attributePropertiesBuilder1.Destroy() End Sub Sub DeleteAllAttributes(ByVal theObject As NXObject) Dim attributeInfo() As NXObject.AttributeInformation = theObject.GetUserAttributes() For Each temp As NXObject.AttributeInformation In attributeInfo theObject.DeleteUserAttributes(temp.Type, Update.Option.Now) Next End Sub Public Function GetUnloadOption(ByVal dummy As String) As Integer GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately End Function End Module
Material Journal - Added built in Material:
Imports System Imports NXOpen Imports System.Collections.Generic Imports NXOpen.UF Module NXJournal Dim theSession As Session Dim theUFSession As UFSession Dim workPart As Part Dim displayPart As Part Dim mySelectedObjects As New List(Of DisplayableObject) Dim physicalMaterial1 As PhysicalMaterial = Nothing Dim theUI As UI Sub Main(ByVal args() As String) Try theSession = Session.GetSession() theUFSession = UFSession.GetUFSession() workPart = theSession.Parts.Work displayPart = theSession.Parts.Display theUI = UI.GetUI() Dim lw As ListingWindow = theSession.ListingWindow lw.Open() Dim markId1 As Session.UndoMarkId = Nothing markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Material Journal") ' Initialize the builders Dim physicalMaterialListBuilder1 As NXOpen.PhysMat.PhysicalMaterialListBuilder = Nothing Dim physicalMaterialAssignBuilder1 As NXOpen.PhysMat.PhysicalMaterialAssignBuilder = Nothing physicalMaterialListBuilder1 = workPart.MaterialManager.PhysicalMaterials.CreateListBlockBuilder() physicalMaterialAssignBuilder1 = workPart.MaterialManager.PhysicalMaterials.CreateMaterialAssignBuilder() Dim materialLibraryPath As String = "C:\Your Folder\To your material library\physicalmateriallibrary.xml" Dim materialName As String = "GRP" ' Make sure this name is matches with your created material in your library Dim materialLibraryLoaded As Boolean = False Try physicalMaterial1 = workPart.MaterialManager.PhysicalMaterials.LoadFromMatmlLibrary(materialLibraryPath, materialName) materialLibraryLoaded = True Catch ex As Exception lw.WriteLine("Failed to load material library: " & ex.Message) End Try If SelectObjects("Hey, select multiple somethings", mySelectedObjects) = Selection.Response.Ok Then For Each tempComp As Body In mySelectedObjects Try Dim physicalMaterial1 As NXOpen.PhysicalMaterial = CType(workPart.MaterialManager.PhysicalMaterials.FindObject("PhysicalMaterial[GRP]"), NXOpen.PhysicalMaterial) If physicalMaterial1 Is Nothing Then lw.WriteLine("Error: Material 'GRP' not found.") Return End If 'lw.WriteLine("Processing body: " & tempComp.JournalIdentifier) Dim density As Double = 1017 ' Kg/m3 - Change this value if you need your own specific density Dim displayModification1 As DisplayModification displayModification1 = theSession.DisplayManager.NewDisplayModification() With displayModification1 .ApplyToAllFaces = False .ApplyToOwningParts = True .NewColor = 117 ' Set the solid body color to 117 .NewLayer = 1 ' Set the solid body to layer 1 .Apply({tempComp}) End With displayModification1.Dispose() DeleteUserAttribute(tempComp, "EW_Material") DeleteUserAttribute(tempComp, "EW_Body_Weight") DeleteUserAttribute(tempComp, "EW_Material_Density") AddBodyAttribute(tempComp, "EW_Material", "17mm GRP") ' Material name - Change this name to your own material Dim attributePropertiesBuilder1 As AttributePropertiesBuilder attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, {tempComp}, AttributePropertiesBuilder.OperationType.None) attributePropertiesBuilder1.Title = "EW_Material_Density" attributePropertiesBuilder1.DataType = NXOpen.AttributePropertiesBaseBuilder.DataTypeOptions.Number attributePropertiesBuilder1.Units = "MilliMeter" attributePropertiesBuilder1.Units = "KilogramPerCubicMeter" attributePropertiesBuilder1.NumberValue = density Dim nXObject1 As NXObject nXObject1 = attributePropertiesBuilder1.Commit() 'lw.WriteLine("Assigned 'SS_Material' attribute with value '17mm GRP' to body: " & tempComp.JournalIdentifier) attributePropertiesBuilder1.Destroy() ' Calculate volume using mass properties Dim myMeasure As MeasureManager = workPart.MeasureManager() Dim massUnits(4) As Unit massUnits(1) = workPart.UnitCollection.GetBase("Volume") Dim mb As MeasureBodies = myMeasure.NewMassProperties(massUnits, 0.99, {tempComp}) mb.InformationUnit = MeasureBodies.AnalysisUnit.KilogramMeter ' Extract volume and multiply it by density to get weight Dim bodyVolume As Double = mb.Volume Dim bodyWeight As Double = bodyVolume * density 'lw.WriteLine("Calculated volume: " & bodyVolume & " and weight: " & bodyWeight & " for body: " & tempComp.JournalIdentifier) ' Create or update an attribute named 'EW_Body_Weight' and assign the weight value to it Dim attributePropertiesBuilderForWeight As AttributePropertiesBuilder attributePropertiesBuilderForWeight = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, {tempComp}, AttributePropertiesBuilder.OperationType.Create) attributePropertiesBuilderForWeight.Title = "EW_Body_Weight" attributePropertiesBuilderForWeight.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Number attributePropertiesBuilderForWeight.Units = "Kilogram" attributePropertiesBuilderForWeight.NumberValue = bodyWeight Dim attributeObjectForWeight As NXObject = attributePropertiesBuilderForWeight.Commit() attributePropertiesBuilderForWeight.Destroy() If physicalMaterial1 IsNot Nothing Then physicalMaterial1.AssignObjects(New NXOpen.NXObject() {tempComp}) 'lw.WriteLine("Material 'GRP' successfully assigned to body: " & tempComp.JournalIdentifier) Else lw.WriteLine("Error: Material 'GRP' not found in the material library.") End If Catch ex As Exception lw.WriteLine("Error processing body: " & tempComp.JournalIdentifier & " - " & ex.Message) lw.WriteLine("Exception occurred: " & ex.ToString()) End Try Next theSession.UpdateManager.DoUpdate(markId1) Else lw.WriteLine("No objects were selected.") End If If physicalMaterialAssignBuilder1 IsNot Nothing Then physicalMaterialAssignBuilder1.Destroy() physicalMaterialAssignBuilder1 = Nothing ' Set the builder to Nothing after destruction End If If physicalMaterialListBuilder1 IsNot Nothing Then physicalMaterialListBuilder1.Destroy() physicalMaterialListBuilder1 = Nothing ' Set the builder to Nothing after destruction End If Catch ex As Exception ' Handle exceptions here Console.WriteLine("Houston, we have a situation... an error occurred: " & ex.Message) Finally If physicalMaterial1 IsNot Nothing Then physicalMaterial1 = Nothing End If End Try End Sub Function SelectObjects(prompt As String, ByRef dispObj As List(Of DisplayableObject)) As Selection.Response Dim selObj As NXObject() Dim title As String = "Select solid bodies - 17mm GRP" Dim includeFeatures As Boolean = False Dim keepHighlighted As Boolean = False Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart Dim selectionMask_array(0) As Selection.MaskTriple With selectionMask_array(0) .Type = UFConstants.UF_solid_type .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY End With Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj) If resp = Selection.Response.ObjectSelected Or resp = Selection.Response.ObjectSelectedByName Or resp = Selection.Response.Ok Then For Each item As NXObject In selObj dispObj.Add(CType(item, DisplayableObject)) Next Return Selection.Response.Ok Else Return Selection.Response.Cancel End If End Function Sub AddBodyAttribute(ByVal theBody As Body, ByVal attTitle As String, ByVal attValue As String) Dim attributePropertiesBuilder1 As AttributePropertiesBuilder attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, {theBody}, AttributePropertiesBuilder.OperationType.None) attributePropertiesBuilder1.IsArray = False attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String attributePropertiesBuilder1.Title = attTitle attributePropertiesBuilder1.StringValue = attValue Dim nXObject1 As NXObject nXObject1 = attributePropertiesBuilder1.Commit() attributePropertiesBuilder1.Destroy() End Sub Sub DeleteAllAttributes(ByVal theObject As NXObject) Dim attributeInfo() As NXObject.AttributeInformation = theObject.GetUserAttributes() For Each temp As NXObject.AttributeInformation In attributeInfo theObject.DeleteUserAttributes(temp.Type, Update.Option.Now) Next End Sub Sub DeleteUserAttribute(ByVal theObject As NXObject, ByVal attributeName As String) Dim attributeInfo() As NXObject.AttributeInformation = CType(theObject, Body).GetUserAttributes() For Each temp As NXObject.AttributeInformation In attributeInfo If temp.Title = attributeName Then theObject.DeleteUserAttribute(temp.Type, temp.Title, False, Update.Option.Now) Exit For End If Next End Sub Public Function GetUnloadOption(ByVal dummy As String) As Integer GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately End Function End Module
Face Material Journal:
Option Strict Off Imports System Imports System.Collections.Generic 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, "Face attributes") lw.Open() Dim selobj As NXObject Dim type As Integer Dim subtype As Integer Dim theFaces As New List(Of Face) Dim theUI As UI = UI.GetUI Dim numsel As Integer = theUI.SelectionManager.GetNumSelectedObjects() ' Process the preselected Faces If numsel > 0 Then For inx As Integer = 0 To numsel - 1 selobj = theUI.SelectionManager.GetSelectedTaggedObject(inx) theUfSession.Obj.AskTypeAndSubtype(selobj.Tag, type, subtype) If type = UFConstants.UF_solid_type Then theFaces.Add(selobj) End If Next Else ' Prompt to select Faces If SelectFaces("Select Faces to reset attributes", theFaces) = Selection.Response.Cancel Then Return End If End If For Each temp As Face In theFaces Dim displayModification As DisplayModification = theSession.DisplayManager.NewDisplayModification() With displayModification ' .ApplyToAllFaces = False .ApplyToOwningParts = True .NewColor = 17 ' Set color to 17 (you can change this to your color) .Apply({temp}) End With displayModification.Dispose() Next lw.Close() End Sub Function SelectFaces(ByVal prompt As String, ByRef selFace As List(Of Face)) As Selection.Response Dim theUI As UI = UI.GetUI Dim title As String = "Select Faces - Inside" Dim includeFeatures As Boolean = False Dim keepHighlighted As Boolean = False Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart Dim selectionMask_array(0) As Selection.MaskTriple Dim selObj() As TaggedObject With selectionMask_array(0) .Type = UFConstants.UF_solid_type .Subtype = 0 .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE End With Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj) If resp = Selection.Response.Ok Then For Each temp As TaggedObject In selObj selFace.Add(temp) Next Return Selection.Response.Ok Else Return Selection.Response.Cancel End If End Function Public Function GetUnloadOption(ByVal dummy As String) As Integer GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately End Function End Module
Raw Body Journal:
Imports System Imports NXOpen Imports System.Collections.Generic Imports NXOpen.UF Module NXJournal Dim theSession As Session = Session.GetSession() Dim theUFSession As UFSession Dim workPart As Part Dim displayPart As Part Dim mySelectedObjects As New List(Of DisplayableObject) Dim theUI As UI Dim lw As ListingWindow = theSession.ListingWindow Sub Main(ByVal args() As String) Dim theSession As Session = Session.GetSession() Dim markId1 As NXOpen.Session.UndoMarkId markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Raw Body Journal") lw.Open() Try theSession = Session.GetSession() theUFSession = UFSession.GetUFSession() workPart = theSession.Parts.Work displayPart = theSession.Parts.Display theUI = UI.GetUI() ' Variables to store attributes from the first selected body (original body) Dim origmat_density As Double = 0.0 Dim orig_weight As Double = 0.0 ' First selection for the original body Try If SelectObjects("Select the Original body", mySelectedObjects) = Selection.Response.Ok Then For Each tempComp As Body In mySelectedObjects Dim attributes = tempComp.GetUserAttributes() For Each attr1 As NXObject.AttributeInformation In attributes If attr1.Title = "EW_Material_Density" Then origmat_density = CDbl(attr1.StringValue) ElseIf attr1.Title = "EW_Body_Weight" Then orig_weight = CDbl(attr1.StringValue) End If Next Next End If If origmat_density = 0.0 Or orig_weight = 0.0 Then 'Throw New Exception("Failed to decipher the weight value for this body! Please apply the appropriate Material Journal before proceeding") End If ' Clear previous selections mySelectedObjects.Clear() ' Second selection for the Raw body If SelectObjects("Select the Raw body", mySelectedObjects) = Selection.Response.Ok Then For Each tempComp As Body In mySelectedObjects ' Measure Raw body volume Dim myMeasure As MeasureManager = workPart.MeasureManager() Dim massUnits(4) As Unit massUnits(1) = workPart.UnitCollection.GetBase("Volume") Dim mb As MeasureBodies = myMeasure.NewMassProperties(massUnits, 0.99, {tempComp}) mb.InformationUnit = MeasureBodies.AnalysisUnit.KilogramMeter Dim bodyVolume As Double = mb.Volume If bodyVolume = 0.0 Then Throw New Exception("Invalid body volume.") End If ' Calculate the Raw weight based on Raw volume and original material density Dim scrib_weight As Double = origmat_density * bodyVolume - orig_weight If Double.IsNaN(scrib_weight) Or Double.IsInfinity(scrib_weight) Then Throw New Exception("Invalid Raw weight calculated.") End If ' Add new attribute Dim attributePropertiesBuilderForWeight As AttributePropertiesBuilder attributePropertiesBuilderForWeight = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, {tempComp}, AttributePropertiesBuilder.OperationType.Create) attributePropertiesBuilderForWeight.Title = "EW_Raw_Body_Diff_Weight" attributePropertiesBuilderForWeight.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Number attributePropertiesBuilderForWeight.Units = "Kilogram" attributePropertiesBuilderForWeight.NumberValue = scrib_weight Dim attributeObjectForWeight As NXObject = attributePropertiesBuilderForWeight.Commit() attributePropertiesBuilderForWeight.Destroy() ' Delete old attribute DeleteUserAttribute(tempComp, "EW_Body_Weight") ' Move body to Layer 170 Dim displayModification1 As DisplayModification displayModification1 = theSession.DisplayManager.NewDisplayModification() With displayModification1 .ApplyToAllFaces = False .ApplyToOwningParts = True .NewLayer = 170 ' Set the raw solid body to layer 170 .Apply({tempComp}) End With displayModification1.Dispose() Next End If Catch ex As Exception lw.WriteLine("An error occurred: " & ex.Message) Finally lw.Close() End Try Catch ex As Exception Console.WriteLine("An error occurred: " & ex.Message) End Try End Sub Function SelectObjects(prompt As String, ByRef dispObj As List(Of DisplayableObject)) As Selection.Response Dim selObj As NXObject() = Nothing Dim title As String = prompt Dim includeFeatures As Boolean = False Dim keepHighlighted As Boolean = False Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart Dim selectionMask_array(0) As Selection.MaskTriple With selectionMask_array(0) .Type = UFConstants.UF_solid_type .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY End With Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj) If resp = Selection.Response.ObjectSelected Or resp = Selection.Response.ObjectSelectedByName Or resp = Selection.Response.Ok Then For Each item As NXObject In selObj dispObj.Add(CType(item, DisplayableObject)) Next Return Selection.Response.Ok Else Return Selection.Response.Cancel End If End Function Sub AddBodyAttribute(ByVal theBody As Body, ByVal attTitle As String, ByVal attValue As String) Dim attributePropertiesBuilder1 As AttributePropertiesBuilder attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, {theBody}, AttributePropertiesBuilder.OperationType.None) attributePropertiesBuilder1.IsArray = False attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String attributePropertiesBuilder1.Title = attTitle attributePropertiesBuilder1.StringValue = attValue Dim nXObject1 As NXObject nXObject1 = attributePropertiesBuilder1.Commit() attributePropertiesBuilder1.Destroy() End Sub Sub DeleteUserAttribute(ByVal theObject As NXObject, ByVal attributeName As String) Dim attributeInfo() As NXObject.AttributeInformation = CType(theObject, Body).GetUserAttributes() For Each temp As NXObject.AttributeInformation In attributeInfo If temp.Title = attributeName Then theObject.DeleteUserAttribute(temp.Type, temp.Title, False, Update.Option.Now) Exit For End If Next End Sub Public Function GetUnloadOption(ByVal dummy As String) As Integer GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately End Function End Module
Delete Attributes Journal:
Imports System Imports NXOpen Imports System.Collections.Generic Imports NXOpen.UF Module NXJournal Dim theSession As Session Dim theUFSession As UFSession Dim workPart As Part Dim displayPart As Part Dim mySelectedObjects As New List(Of DisplayableObject) Dim theUI As UI Sub Main(ByVal args() As String) Try theSession = Session.GetSession() theUFSession = UFSession.GetUFSession() workPart = theSession.Parts.Work displayPart = theSession.Parts.Display theUI = UI.GetUI() Dim markId1 As Session.UndoMarkId = Nothing markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Delete Attributes") If SelectObjects("Hey, select multiple somethings", mySelectedObjects) = Selection.Response.Ok Then For Each tempComp As Body In mySelectedObjects Dim displayModification1 As DisplayModification displayModification1 = theSession.DisplayManager.NewDisplayModification() With displayModification1 .ApplyToAllFaces = False .ApplyToOwningParts = True .Apply({tempComp}) End With DeleteAllAttributes(tempComp) Next End If Catch ex As Exception ' Handle exceptions here Console.WriteLine("An error occurred: " & ex.Message) End Try End Sub Function SelectObjects(prompt As String, ByRef dispObj As List(Of DisplayableObject)) As Selection.Response Dim selObj As NXObject() Dim title As String = "Select solid bodies to DELETE their Weight Data" Dim includeFeatures As Boolean = False Dim keepHighlighted As Boolean = False Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart Dim selectionMask_array(0) As Selection.MaskTriple With selectionMask_array(0) .Type = UFConstants.UF_solid_type .SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY End With Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj) If resp = Selection.Response.ObjectSelected Or resp = Selection.Response.ObjectSelectedByName Or resp = Selection.Response.Ok Then For Each item As NXObject In selObj dispObj.Add(CType(item, DisplayableObject)) Next Return Selection.Response.Ok Else Return Selection.Response.Cancel End If End Function Sub DeleteAllAttributes(ByVal theObject As NXObject) Dim attributeInfo() As NXObject.AttributeInformation = theObject.GetUserAttributes() For Each temp As NXObject.AttributeInformation In attributeInfo theObject.DeleteUserAttributes(temp.Type, Update.Option.Now) Next End Sub Public Function GetUnloadOption(ByVal dummy As String) As Integer GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately End Function End Module