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.
Main thread:
https://www.nxjournaling.com/content/easy-material-weight-management-part-1
Component Creator - Simple Select Version:
Imports System Imports NXOpen Imports System.Collections.Generic Imports NXOpen.UF Module NXJournal Dim theSession As NXOpen.Session = NXOpen.Session.GetSession() Dim theUFSession As NXOpen.UF.UFSession = NXOpen.UF.UFSession.GetUFSession() Dim workPart As NXOpen.Part = theSession.Parts.Work Dim displayPart As NXOpen.Part = theSession.Parts.Display Dim theUI As NXOpen.UI = NXOpen.UI.GetUI() Dim logicalobjects1() As NXOpen.PDM.LogicalObject = Nothing Dim logicalobjects2() As NXOpen.PDM.LogicalObject = Nothing Dim sourceobjects1() As NXOpen.NXObject Dim selectedObjectName As String = "BODY" DIM defaultsolidbodyname As String = selectedObjectName Dim mySelectedObjects As New List(Of DisplayableObject) Dim lw As ListingWindow = theSession.ListingWindow Dim nXObject2 As NXOpen.NXObject = Nothing Function IsComponentCreated(ByVal body As Body) As Boolean Try Dim attrValue As String = body.GetStringAttribute("Component_created") Return Boolean.Parse(attrValue) Catch ex As NXOpen.NXException ' If any exception is caught, we assume the attribute does not exist Return False End Try End Function ' Subroutine to set the 'Component_created' attribute on a body Sub SetComponentCreated(ByVal body As Body, ByVal created As Boolean) Try body.SetAttribute("Component_created", created.ToString()) Catch ex As NXOpen.NXException ' Handle any exceptions related to setting the attribute lw.WriteLine("Error setting attribute: " & ex.Message) End Try End Sub Sub Main(ByVal args() As String) lw.Open() theSession = Session.GetSession() theUFSession = UFSession.GetUFSession() workPart = theSession.Parts.Work displayPart = theSession.Parts.Display theUI = UI.GetUI() Dim isFirstSave As Boolean = True ' Step 1: Prompt for User Input Dim assemblyid As String = InputBox("Please enter the Main Components Number - X17068219-01", "Component Creator - Simple Select Version") If String.IsNullOrEmpty(assemblyid) Then Exit Sub End If ' Use SelectObjects function to get user-selected bodies selectedObjectName = SelectObjects("Hey, select multiple somethings", mySelectedObjects) Dim markId1 As NXOpen.Session.UndoMarkId markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Component Creator") For Each tempComp As DisplayableObject In mySelectedObjects ' Declare the variables here to make them accessible in Finally block Dim attributePropertiesBuilder1 As NXOpen.AttributePropertiesBuilder = Nothing Dim createNewComponentBuilder1 As NXOpen.Assemblies.CreateNewComponentBuilder = Nothing ' Step 2: Concatenate the String Dim AssemblyidString As String = assemblyid & "-101" Dim body As Body = CType(tempComp, Body) selectedObjectName = body.Name ' Update name for this iteration ' Skip the body if the 'Component_created' attribute is True If IsComponentCreated(body) Then lw.WriteLine("This solid body already has a component in existence, thus we shall proceed to ") lw.WriteLine("elegantly leap over this step: " & selectedObjectName) Continue For End If If String.IsNullOrEmpty(selectedObjectName) Then Continue For End If Try ' Create new file Dim fileNew1 As NXOpen.FileNew = theSession.Parts.FileNew() Dim partOperationCreateBuilder1 As NXOpen.PDM.PartOperationCreateBuilder = Nothing partOperationCreateBuilder1 = theSession.PdmSession.CreateCreateOperationBuilder(NXOpen.PDM.PartOperationBuilder.OperationType.Create) fileNew1.SetPartOperationCreateBuilder(partOperationCreateBuilder1) partOperationCreateBuilder1.SetOperationSubType(NXOpen.PDM.PartOperationCreateBuilder.OperationSubType.FromTemplate) partOperationCreateBuilder1.SetModelType("master") partOperationCreateBuilder1.SetItemType("Item") partOperationCreateBuilder1.CreateLogicalObjects(logicalobjects1) sourceobjects1 = logicalobjects1(0).GetUserAttributeSourceObjects() partOperationCreateBuilder1.DefaultDestinationFolder = ":Newfolder" ' you need to change this to your environment fileNew1.TemplateFileName = "Here comes your Template name from your recorded journal" ' you need to change this to your environment fileNew1.UseBlankTemplate = False fileNew1.ApplicationName = "ModelTemplate" fileNew1.Units = NXOpen.Part.Units.Millimeters fileNew1.RelationType = "master" fileNew1.UsesMasterModel = "No" fileNew1.TemplateType = NXOpen.FileNewTemplateType.Item fileNew1.TemplatePresentationName = "Model" fileNew1.ItemType = "Item" fileNew1.Specialization = "" fileNew1.SetCanCreateAltrep(False) partOperationCreateBuilder1.SetAddMaster(False) partOperationCreateBuilder1.CreateLogicalObjects(logicalobjects2) partOperationCreateBuilder1.SetAddMaster(False) Dim nullNXOpen_BasePart As NXOpen.BasePart = Nothing ' Create attributes Dim objects1(-1) As NXOpen.NXObject attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(nullNXOpen_BasePart, objects1, NXOpen.AttributePropertiesBuilder.OperationType.Create) Dim objects2(-1) As NXOpen.NXObject attributePropertiesBuilder1.SetAttributeObjects(objects2) Dim objects3(0) As NXOpen.NXObject objects3(0) = sourceobjects1(0) attributePropertiesBuilder1.SetAttributeObjects(objects3) attributePropertiesBuilder1.Title = "DB_PART_NO" attributePropertiesBuilder1.Category = "Item" attributePropertiesBuilder1.StringValue = AssemblyidString attributePropertiesBuilder1.Category = "Item" Dim changed1 As Boolean = Nothing changed1 = attributePropertiesBuilder1.CreateAttribute() Dim attributetitles1(-1) As String Dim titlepatterns1(-1) As String Dim nXObject1 As NXOpen.NXObject = Nothing nXObject1 = partOperationCreateBuilder1.CreateAttributeTitleToNamingPatternMap(attributetitles1, titlepatterns1) Dim objects4(0) As NXOpen.NXObject objects4(0) = logicalobjects1(0) Dim properties1(0) As NXOpen.NXObject properties1(0) = nXObject1 Dim errorList1 As NXOpen.ErrorList = Nothing errorList1 = partOperationCreateBuilder1.AutoAssignAttributesWithNamingPattern(objects4, properties1) errorList1.Dispose() attributePropertiesBuilder1.Title = "DB_PART_NAME" attributePropertiesBuilder1.StringValue = selectedObjectName attributePropertiesBuilder1.Category = "Item" Dim changed2 As Boolean = Nothing changed2 = attributePropertiesBuilder1.CreateAttribute() fileNew1.MasterFileName = "" fileNew1.MakeDisplayedPart = False fileNew1.DisplayPartOption = NXOpen.DisplayPartOption.AllowAdditional partOperationCreateBuilder1.ValidateLogicalObjectsToCommit() Dim logicalobjects4(0) As NXOpen.PDM.LogicalObject logicalobjects4(0) = logicalobjects1(0) partOperationCreateBuilder1.CreateSpecificationsForLogicalObjects(logicalobjects4) ' Create new component createNewComponentBuilder1 = workPart.AssemblyManager.CreateNewComponentBuilder() createNewComponentBuilder1.ReferenceSetName = "MODEL" createNewComponentBuilder1.ComponentOrigin = NXOpen.Assemblies.CreateNewComponentBuilder.ComponentOriginType.Absolute createNewComponentBuilder1.OriginalObjectsDeleted = False createNewComponentBuilder1.ObjectForNewComponent.Clear() createNewComponentBuilder1.ObjectForNewComponent.Add(body) createNewComponentBuilder1.NewFile = fileNew1 Dim nXObject2 As NXOpen.NXObject = Nothing nXObject2 = createNewComponentBuilder1.Commit() lw.WriteLine("") lw.WriteLine("Great news my friend,") lw.WriteLine("Our first component - 101 created") If isFirstSave Then Dim partSaveStatus As NXOpen.PartSaveStatus = Nothing Dim newComponent As NXOpen.Assemblies.Component = CType(nXObject2, NXOpen.Assemblies.Component) Dim newPart As NXOpen.Part = CType(newComponent.Prototype, NXOpen.Part) Try partSaveStatus = newPart.Save(NXOpen.BasePart.SaveComponents.False, NXOpen.BasePart.CloseAfterSave.False) Catch ex As NXOpen.NXException ' Handle NX-specific exceptions here Catch ex As Exception ' Handle general exceptions here End Try If partSaveStatus IsNot Nothing Then partSaveStatus.Dispose() End If lw.WriteLine("And saved to Teamcenter: " & AssemblyidString ) lw.WriteLine("") lw.WriteLine("A friendly nudge: the remaining components are still drifting in the digital") lw.WriteLine("ether, unsaved. Do cast an eye, delete, if the stars are out of alignment,") lw.WriteLine("and proceed as the universe dictates.") lw.WriteLine("") isFirstSave = False End If Dim markId2 As NXOpen.Session.UndoMarkId markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Component Creator") Catch ex As Exception When ex.Message.Contains("The new filename is not a valid file specification") ' Modify assembly ID and try again AssemblyidString = assemblyid & "-10*" ' Create new file Dim fileNew1 As NXOpen.FileNew = theSession.Parts.FileNew() Dim partOperationCreateBuilder1 As NXOpen.PDM.PartOperationCreateBuilder = Nothing partOperationCreateBuilder1 = theSession.PdmSession.CreateCreateOperationBuilder(NXOpen.PDM.PartOperationBuilder.OperationType.Create) fileNew1.SetPartOperationCreateBuilder(partOperationCreateBuilder1) partOperationCreateBuilder1.SetOperationSubType(NXOpen.PDM.PartOperationCreateBuilder.OperationSubType.FromTemplate) partOperationCreateBuilder1.SetModelType("master") partOperationCreateBuilder1.SetItemType("Item") partOperationCreateBuilder1.CreateLogicalObjects(logicalobjects1) sourceobjects1 = logicalobjects1(0).GetUserAttributeSourceObjects() partOperationCreateBuilder1.DefaultDestinationFolder = ":Newfolder" ' you need to change this to your environment fileNew1.TemplateFileName = "Here comes your Template name from your recorded journal" ' you need to change this to your environment fileNew1.UseBlankTemplate = False fileNew1.ApplicationName = "ModelTemplate" fileNew1.Units = NXOpen.Part.Units.Millimeters fileNew1.RelationType = "master" fileNew1.UsesMasterModel = "No" fileNew1.TemplateType = NXOpen.FileNewTemplateType.Item fileNew1.TemplatePresentationName = "Model" fileNew1.ItemType = "Item" fileNew1.Specialization = "" fileNew1.SetCanCreateAltrep(False) partOperationCreateBuilder1.SetAddMaster(False) partOperationCreateBuilder1.CreateLogicalObjects(logicalobjects2) partOperationCreateBuilder1.SetAddMaster(False) Dim nullNXOpen_BasePart As NXOpen.BasePart = Nothing ' Create attributes Dim objects1(-1) As NXOpen.NXObject attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(nullNXOpen_BasePart, objects1, NXOpen.AttributePropertiesBuilder.OperationType.Create) Dim objects2(-1) As NXOpen.NXObject attributePropertiesBuilder1.SetAttributeObjects(objects2) Dim objects3(0) As NXOpen.NXObject objects3(0) = sourceobjects1(0) attributePropertiesBuilder1.SetAttributeObjects(objects3) attributePropertiesBuilder1.Title = "DB_PART_NO" attributePropertiesBuilder1.Category = "Item" attributePropertiesBuilder1.StringValue = AssemblyidString attributePropertiesBuilder1.Category = "Item" Dim changed1 As Boolean = Nothing changed1 = attributePropertiesBuilder1.CreateAttribute() Dim attributetitles1(-1) As String Dim titlepatterns1(-1) As String Dim nXObject1 As NXOpen.NXObject = Nothing nXObject1 = partOperationCreateBuilder1.CreateAttributeTitleToNamingPatternMap(attributetitles1, titlepatterns1) Dim objects4(0) As NXOpen.NXObject objects4(0) = logicalobjects1(0) Dim properties1(0) As NXOpen.NXObject properties1(0) = nXObject1 Dim errorList1 As NXOpen.ErrorList = Nothing errorList1 = partOperationCreateBuilder1.AutoAssignAttributesWithNamingPattern(objects4, properties1) errorList1.Dispose() attributePropertiesBuilder1.Title = "DB_PART_NAME" attributePropertiesBuilder1.StringValue = selectedObjectName attributePropertiesBuilder1.Category = "Item" Dim changed2 As Boolean = Nothing changed2 = attributePropertiesBuilder1.CreateAttribute() fileNew1.MasterFileName = "" fileNew1.MakeDisplayedPart = False fileNew1.DisplayPartOption = NXOpen.DisplayPartOption.AllowAdditional partOperationCreateBuilder1.ValidateLogicalObjectsToCommit() Dim logicalobjects4(0) As NXOpen.PDM.LogicalObject logicalobjects4(0) = logicalobjects1(0) partOperationCreateBuilder1.CreateSpecificationsForLogicalObjects(logicalobjects4) ' Create new component createNewComponentBuilder1 = workPart.AssemblyManager.CreateNewComponentBuilder() createNewComponentBuilder1.ReferenceSetName = "MODEL" createNewComponentBuilder1.ComponentOrigin = NXOpen.Assemblies.CreateNewComponentBuilder.ComponentOriginType.Absolute createNewComponentBuilder1.OriginalObjectsDeleted = False createNewComponentBuilder1.ObjectForNewComponent.Clear() createNewComponentBuilder1.ObjectForNewComponent.Add(body) createNewComponentBuilder1.NewFile = fileNew1 Dim nXObject2 As NXOpen.NXObject = Nothing Try nXObject2 = createNewComponentBuilder1.Commit() Catch ex1 As Exception lw.WriteLine("Second attempt failed: " & ex1.Message) ' Handle second failure End Try Catch ex As Exception lw.WriteLine("Houston, we have a situation... an error occurred: " & ex.Message) lw.WriteLine("Stack Trace: " & ex.StackTrace) Finally If createNewComponentBuilder1 IsNot Nothing Then createNewComponentBuilder1.Destroy() End If If attributePropertiesBuilder1 IsNot Nothing Then attributePropertiesBuilder1.Destroy() End If End Try theSession.CleanUpFacetedFacesAndEdges() SetComponentCreated(body, True) Next End Sub Function SelectObjects(prompt As String, ByRef dispObj As List(Of DisplayableObject)) As Boolean Dim selObj As NXObject() Dim title As String = "Select solid bodies" 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 OrElse resp = Selection.Response.ObjectSelectedByName OrElse resp = Selection.Response.Ok Then If selObj IsNot Nothing AndAlso selObj.Length > 0 Then For Each item As NXObject In selObj If String.IsNullOrEmpty(item.Name) Then item.SetName(defaultsolidbodyname) End If dispObj.Add(CType(item, DisplayableObject)) Next Return True ' Successfully selected objects Else ' Handle the case where no objects are selected lw.WriteLine("No objects were selected.") Return False End If Else ' Handle the case where the user cancels the selection or some other unexpected response occurs lw.WriteLine("Unexpected response during object selection: " & resp.ToString()) Return False End If End Function End Module
Component Creator - Smart Sorting Version:
Imports System Imports NXOpen Imports System.Collections.Generic Imports NXOpen.UF Module NXJournal Dim theSession As NXOpen.Session = NXOpen.Session.GetSession() Dim theUFSession As NXOpen.UF.UFSession = NXOpen.UF.UFSession.GetUFSession() Dim workPart As NXOpen.Part = theSession.Parts.Work Dim displayPart As NXOpen.Part = theSession.Parts.Display Dim theUI As NXOpen.UI = NXOpen.UI.GetUI() Dim logicalobjects1() As NXOpen.PDM.LogicalObject = Nothing Dim logicalobjects2() As NXOpen.PDM.LogicalObject = Nothing Dim sourceobjects1() As NXOpen.NXObject Dim selectedObjectName As String = "BODY" DIM defaultsolidbodyname As String = selectedObjectName Dim mySelectedObjects As New List(Of DisplayableObject) Dim lw As ListingWindow = theSession.ListingWindow Dim nXObject2 As NXOpen.NXObject = Nothing Function IsComponentCreated(ByVal body As Body) As Boolean Try Dim attrValue As String = body.GetStringAttribute("Component_created") Return Boolean.Parse(attrValue) Catch ex As NXOpen.NXException ' If any exception is caught, we assume the attribute does not exist Return False End Try End Function ' Subroutine to set the 'Component_created' attribute on a body Sub SetComponentCreated(ByVal body As Body, ByVal created As Boolean) Try body.SetAttribute("Component_created", created.ToString()) Catch ex As NXOpen.NXException ' Handle any exceptions related to setting the attribute lw.WriteLine("Error setting attribute: " & ex.Message) End Try End Sub Sub Main(ByVal args() As String) lw.Open() theSession = Session.GetSession() theUFSession = UFSession.GetUFSession() workPart = theSession.Parts.Work displayPart = theSession.Parts.Display theUI = UI.GetUI() Dim isFirstSave As Boolean = True ' Step 1: Prompt for User Input Dim assemblyid As String = InputBox("Please enter the Main Components Number - X17068219-01", "Component Creator - Smart Sorting Version") If String.IsNullOrEmpty(assemblyid) Then Exit Sub End If ' Use SelectObjects function to get user-selected bodies selectedObjectName = SelectObjects("Hey, select multiple somethings", mySelectedObjects) Dim markId1 As NXOpen.Session.UndoMarkId markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Component Creator") For Each tempComp As DisplayableObject In mySelectedObjects ' Declare the variables here to make them accessible in Finally block Dim attributePropertiesBuilder1 As NXOpen.AttributePropertiesBuilder = Nothing Dim createNewComponentBuilder1 As NXOpen.Assemblies.CreateNewComponentBuilder = Nothing ' Step 2: Concatenate the String Dim AssemblyidString As String = assemblyid & "-101" Dim body As Body = CType(tempComp, Body) selectedObjectName = body.Name ' Update name for this iteration ' Skip the body if the 'Component_created' attribute is True If IsComponentCreated(body) Then lw.WriteLine("This solid body already has a component in existence, thus we shall proceed to ") lw.WriteLine("elegantly leap over this step: " & selectedObjectName) Continue For End If If String.IsNullOrEmpty(selectedObjectName) Then Continue For End If Try ' Create new file Dim fileNew1 As NXOpen.FileNew = theSession.Parts.FileNew() Dim partOperationCreateBuilder1 As NXOpen.PDM.PartOperationCreateBuilder = Nothing partOperationCreateBuilder1 = theSession.PdmSession.CreateCreateOperationBuilder(NXOpen.PDM.PartOperationBuilder.OperationType.Create) fileNew1.SetPartOperationCreateBuilder(partOperationCreateBuilder1) partOperationCreateBuilder1.SetOperationSubType(NXOpen.PDM.PartOperationCreateBuilder.OperationSubType.FromTemplate) partOperationCreateBuilder1.SetModelType("master") partOperationCreateBuilder1.SetItemType("Item") partOperationCreateBuilder1.CreateLogicalObjects(logicalobjects1) sourceobjects1 = logicalobjects1(0).GetUserAttributeSourceObjects() partOperationCreateBuilder1.DefaultDestinationFolder = ":Newfolder" ' you need to change this to your environment fileNew1.TemplateFileName = "Here comes your Template name from your recorded journal" ' you need to change this to your environment fileNew1.UseBlankTemplate = False fileNew1.ApplicationName = "ModelTemplate" fileNew1.Units = NXOpen.Part.Units.Millimeters fileNew1.RelationType = "master" fileNew1.UsesMasterModel = "No" fileNew1.TemplateType = NXOpen.FileNewTemplateType.Item fileNew1.TemplatePresentationName = "Model" fileNew1.ItemType = "Item" fileNew1.Specialization = "" fileNew1.SetCanCreateAltrep(False) partOperationCreateBuilder1.SetAddMaster(False) partOperationCreateBuilder1.CreateLogicalObjects(logicalobjects2) partOperationCreateBuilder1.SetAddMaster(False) Dim nullNXOpen_BasePart As NXOpen.BasePart = Nothing ' Create attributes Dim objects1(-1) As NXOpen.NXObject attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(nullNXOpen_BasePart, objects1, NXOpen.AttributePropertiesBuilder.OperationType.Create) Dim objects2(-1) As NXOpen.NXObject attributePropertiesBuilder1.SetAttributeObjects(objects2) Dim objects3(0) As NXOpen.NXObject objects3(0) = sourceobjects1(0) attributePropertiesBuilder1.SetAttributeObjects(objects3) attributePropertiesBuilder1.Title = "DB_PART_NO" attributePropertiesBuilder1.Category = "Item" attributePropertiesBuilder1.StringValue = AssemblyidString attributePropertiesBuilder1.Category = "Item" Dim changed1 As Boolean = Nothing changed1 = attributePropertiesBuilder1.CreateAttribute() Dim attributetitles1(-1) As String Dim titlepatterns1(-1) As String Dim nXObject1 As NXOpen.NXObject = Nothing nXObject1 = partOperationCreateBuilder1.CreateAttributeTitleToNamingPatternMap(attributetitles1, titlepatterns1) Dim objects4(0) As NXOpen.NXObject objects4(0) = logicalobjects1(0) Dim properties1(0) As NXOpen.NXObject properties1(0) = nXObject1 Dim errorList1 As NXOpen.ErrorList = Nothing errorList1 = partOperationCreateBuilder1.AutoAssignAttributesWithNamingPattern(objects4, properties1) errorList1.Dispose() attributePropertiesBuilder1.Title = "DB_PART_NAME" attributePropertiesBuilder1.StringValue = selectedObjectName attributePropertiesBuilder1.Category = "Item" Dim changed2 As Boolean = Nothing changed2 = attributePropertiesBuilder1.CreateAttribute() fileNew1.MasterFileName = "" fileNew1.MakeDisplayedPart = False fileNew1.DisplayPartOption = NXOpen.DisplayPartOption.AllowAdditional partOperationCreateBuilder1.ValidateLogicalObjectsToCommit() Dim logicalobjects4(0) As NXOpen.PDM.LogicalObject logicalobjects4(0) = logicalobjects1(0) partOperationCreateBuilder1.CreateSpecificationsForLogicalObjects(logicalobjects4) ' Create new component createNewComponentBuilder1 = workPart.AssemblyManager.CreateNewComponentBuilder() createNewComponentBuilder1.ReferenceSetName = "MODEL" createNewComponentBuilder1.ComponentOrigin = NXOpen.Assemblies.CreateNewComponentBuilder.ComponentOriginType.Absolute createNewComponentBuilder1.OriginalObjectsDeleted = False createNewComponentBuilder1.ObjectForNewComponent.Clear() createNewComponentBuilder1.ObjectForNewComponent.Add(body) createNewComponentBuilder1.NewFile = fileNew1 Dim nXObject2 As NXOpen.NXObject = Nothing nXObject2 = createNewComponentBuilder1.Commit() lw.WriteLine("") lw.WriteLine("Great news my friend,") lw.WriteLine("Our first component - 101 created") If isFirstSave Then Dim partSaveStatus As NXOpen.PartSaveStatus = Nothing Dim newComponent As NXOpen.Assemblies.Component = CType(nXObject2, NXOpen.Assemblies.Component) Dim newPart As NXOpen.Part = CType(newComponent.Prototype, NXOpen.Part) Try partSaveStatus = newPart.Save(NXOpen.BasePart.SaveComponents.False, NXOpen.BasePart.CloseAfterSave.False) Catch ex As NXOpen.NXException ' Handle NX-specific exceptions here Catch ex As Exception ' Handle general exceptions here End Try If partSaveStatus IsNot Nothing Then partSaveStatus.Dispose() End If lw.WriteLine("And saved to Teamcenter: " & AssemblyidString ) lw.WriteLine("") lw.WriteLine("A friendly nudge: the remaining components are still drifting in the digital") lw.WriteLine("ether, unsaved. Do cast an eye, delete, if the stars are out of alignment,") lw.WriteLine("and proceed as the universe dictates.") lw.WriteLine("") isFirstSave = False End If Dim markId2 As NXOpen.Session.UndoMarkId markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Component Creator") Catch ex As Exception When ex.Message.Contains("The new filename is not a valid file specification") ' Modify assembly ID and try again AssemblyidString = assemblyid & "-10*" ' Create new file Dim fileNew1 As NXOpen.FileNew = theSession.Parts.FileNew() Dim partOperationCreateBuilder1 As NXOpen.PDM.PartOperationCreateBuilder = Nothing partOperationCreateBuilder1 = theSession.PdmSession.CreateCreateOperationBuilder(NXOpen.PDM.PartOperationBuilder.OperationType.Create) fileNew1.SetPartOperationCreateBuilder(partOperationCreateBuilder1) partOperationCreateBuilder1.SetOperationSubType(NXOpen.PDM.PartOperationCreateBuilder.OperationSubType.FromTemplate) partOperationCreateBuilder1.SetModelType("master") partOperationCreateBuilder1.SetItemType("Item") partOperationCreateBuilder1.CreateLogicalObjects(logicalobjects1) sourceobjects1 = logicalobjects1(0).GetUserAttributeSourceObjects() partOperationCreateBuilder1.DefaultDestinationFolder = ":Newfolder" ' you need to change this to your environment fileNew1.TemplateFileName = "Here comes your Template name from your recorded journal" ' you need to change this to your environment fileNew1.UseBlankTemplate = False fileNew1.ApplicationName = "ModelTemplate" fileNew1.Units = NXOpen.Part.Units.Millimeters fileNew1.RelationType = "master" fileNew1.UsesMasterModel = "No" fileNew1.TemplateType = NXOpen.FileNewTemplateType.Item fileNew1.TemplatePresentationName = "Model" fileNew1.ItemType = "Item" fileNew1.Specialization = "" fileNew1.SetCanCreateAltrep(False) partOperationCreateBuilder1.SetAddMaster(False) partOperationCreateBuilder1.CreateLogicalObjects(logicalobjects2) partOperationCreateBuilder1.SetAddMaster(False) Dim nullNXOpen_BasePart As NXOpen.BasePart = Nothing ' Create attributes Dim objects1(-1) As NXOpen.NXObject attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(nullNXOpen_BasePart, objects1, NXOpen.AttributePropertiesBuilder.OperationType.Create) Dim objects2(-1) As NXOpen.NXObject attributePropertiesBuilder1.SetAttributeObjects(objects2) Dim objects3(0) As NXOpen.NXObject objects3(0) = sourceobjects1(0) attributePropertiesBuilder1.SetAttributeObjects(objects3) attributePropertiesBuilder1.Title = "DB_PART_NO" attributePropertiesBuilder1.Category = "Item" attributePropertiesBuilder1.StringValue = AssemblyidString attributePropertiesBuilder1.Category = "Item" Dim changed1 As Boolean = Nothing changed1 = attributePropertiesBuilder1.CreateAttribute() Dim attributetitles1(-1) As String Dim titlepatterns1(-1) As String Dim nXObject1 As NXOpen.NXObject = Nothing nXObject1 = partOperationCreateBuilder1.CreateAttributeTitleToNamingPatternMap(attributetitles1, titlepatterns1) Dim objects4(0) As NXOpen.NXObject objects4(0) = logicalobjects1(0) Dim properties1(0) As NXOpen.NXObject properties1(0) = nXObject1 Dim errorList1 As NXOpen.ErrorList = Nothing errorList1 = partOperationCreateBuilder1.AutoAssignAttributesWithNamingPattern(objects4, properties1) errorList1.Dispose() attributePropertiesBuilder1.Title = "DB_PART_NAME" attributePropertiesBuilder1.StringValue = selectedObjectName attributePropertiesBuilder1.Category = "Item" Dim changed2 As Boolean = Nothing changed2 = attributePropertiesBuilder1.CreateAttribute() fileNew1.MasterFileName = "" fileNew1.MakeDisplayedPart = False fileNew1.DisplayPartOption = NXOpen.DisplayPartOption.AllowAdditional partOperationCreateBuilder1.ValidateLogicalObjectsToCommit() Dim logicalobjects4(0) As NXOpen.PDM.LogicalObject logicalobjects4(0) = logicalobjects1(0) partOperationCreateBuilder1.CreateSpecificationsForLogicalObjects(logicalobjects4) ' Create new component createNewComponentBuilder1 = workPart.AssemblyManager.CreateNewComponentBuilder() createNewComponentBuilder1.ReferenceSetName = "MODEL" createNewComponentBuilder1.ComponentOrigin = NXOpen.Assemblies.CreateNewComponentBuilder.ComponentOriginType.Absolute createNewComponentBuilder1.OriginalObjectsDeleted = False createNewComponentBuilder1.ObjectForNewComponent.Clear() createNewComponentBuilder1.ObjectForNewComponent.Add(body) createNewComponentBuilder1.NewFile = fileNew1 Dim nXObject2 As NXOpen.NXObject = Nothing Try nXObject2 = createNewComponentBuilder1.Commit() Catch ex1 As Exception lw.WriteLine("Second attempt failed: " & ex1.Message) ' Handle second failure End Try Catch ex As Exception lw.WriteLine("Houston, we have a situation... an error occurred: " & ex.Message) lw.WriteLine("Stack Trace: " & ex.StackTrace) Finally If createNewComponentBuilder1 IsNot Nothing Then createNewComponentBuilder1.Destroy() End If If attributePropertiesBuilder1 IsNot Nothing Then attributePropertiesBuilder1.Destroy() End If End Try theSession.CleanUpFacetedFacesAndEdges() SetComponentCreated(body, True) Next End Sub Function SelectObjects(prompt As String, ByRef dispObj As List(Of DisplayableObject)) As Boolean Dim selObj As NXObject() Dim title As String = "Select solid bodies" 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 OrElse resp = Selection.Response.ObjectSelectedByName OrElse resp = Selection.Response.Ok Then If selObj IsNot Nothing AndAlso selObj.Length > 0 Then For Each item As NXObject In selObj If String.IsNullOrEmpty(item.Name) Then item.SetName(defaultsolidbodyname) End If dispObj.Add(CType(item, DisplayableObject)) Next ' Sort objects Try dispObj.Sort(Function(a, b) Dim aMat As String = Nothing Dim bMat As String = Nothing Dim aWeight As Double = 0 Dim bWeight As Double = 0 Dim primaryResult As Integer = 0 Try aMat = a.GetStringAttribute("EW_Material") Catch ex As Exception ' Handle or log the exception if necessary End Try Try bMat = b.GetStringAttribute("EW_Material") Catch ex As Exception ' Handle or log the exception if necessary End Try Try aWeight = a.GetRealAttribute("EW_Body_Weight") Catch ex As Exception ' Handle or log the exception if necessary End Try Try bWeight = b.GetRealAttribute("EW_Body_Weight") Catch ex As Exception ' Handle or log the exception if necessary End Try ' Handling primary sort based on EW_Material attribute If aMat IsNot Nothing AndAlso bMat IsNot Nothing Then Try Dim aNum As Integer = Integer.Parse(aMat.Substring(0, aMat.IndexOf("mm")).Trim()) Dim bNum As Integer = Integer.Parse(bMat.Substring(0, bMat.IndexOf("mm")).Trim()) primaryResult = bNum.CompareTo(aNum) ' Sort in descending order Catch ex As Exception ' Handle or log the exception if the substring can't be parsed to an integer primaryResult = String.Compare(aMat, bMat) ' Sort alphabetically in that case End Try ElseIf aMat IsNot Nothing Then primaryResult = -1 ' a comes before b ElseIf bMat IsNot Nothing Then primaryResult = 1 ' b comes before a Else primaryResult = 0 ' Both are null; they're equal in terms of sorting End If ' Handling secondary sort based on EW_Body_Weight attribute If primaryResult = 0 Then Return bWeight.CompareTo(aWeight) ' Sort in descending order based on weight Else Return primaryResult ' Otherwise, return the result of the primary comparison End If End Function) Catch ex As Exception lw.WriteLine("An error occurred during sorting: " & ex.Message) ' Handle the exception as appropriate for your application End Try Return True ' Successfully selected and sorted objects Else ' Handle the case where no objects are selected lw.WriteLine("No objects were selected.") Return False End If Else ' Handle the case where the user cancels the selection or some other unexpected response occurs lw.WriteLine("Unexpected response during object selection: " & resp.ToString()) Return False End If End Function End Module