Forums:
This is my code and I want to rename the the assembly and then each child in the assembly but it's not working for me.
'Journal to recursively walk through the assembly structure' will run on assemblies or piece parts' will step through all components of the displayed part'NX 7.5, native'NXJournaling.com February 24, 2012 Option Strict Off Imports System Imports NXOpen Imports NXOpen.UFImports NXOpen.AssembliesImports NXOpenUI Imports System.Collections.GenericImports System.Windows.Forms Module NXJournal Public theSession As Session = Session.GetSession()Public ufs As UFSession = UFSession.GetUFSession()Public lw As ListingWindow = theSession.ListingWindow Public workPart As NXOpen.Part= theSession.Parts.WorkPublic dispPart as NXOpen.Part= theSession.Parts.DisplayPublic fileNameNoExt AsStringPublic basePart1 As NXOpen.BasePart=NothingPublic partLoadStatus1 As NXOpen.PartLoadStatus=NothingPublic childpart as NXOpen.PartPublic childrenpart as NXOpen.Part Public PartNames(0)AsStringPublic ArrayIndex asinteger=0Public i asinteger=0Public j asintegerPublic match asinteger=0Public theUISession As UI = UI.GetUIPublic c As ComponentAssembly = dispPart.ComponentAssembly Public errorMessageHandler4 As NXOpen.PDM.ErrorMessageHandler=NothingPublic selectedparts1(0)As NXOpen.BasePartPublic failedparts1()As NXOpen.BasePartPublic logicalobjects1()As NXOpen.PDM.LogicalObjectPublic sourceobjects1()As NXOpen.NXObjectPublic partOperationCopyBuilder1 As NXOpen.PDM.PartOperationCopyBuilder=NothingPublic readOnly1 AsBoolean=NothingPublic part1 As NXOpen.Part=NothingPublic sourceobjects2()As NXOpen.NXObjectPublic sourceobjects3()As NXOpen.NXObjectPublic sourceobjects4()As NXOpen.NXObjectPublic sourceobjects5()As NXOpen.NXObjectPublic sourceobjects6()As NXOpen.NXObjectPublic selectedparts2(0)As NXOpen.BasePartPublic failedparts2()As NXOpen.BasePartPublic logicalobjects2()As NXOpen.PDM.LogicalObjectPublic sourceobjects7()As NXOpen.NXObjectPublic sourceobjects8()As NXOpen.NXObjectPublic sourceobjects9()As NXOpen.NXObjectPublic sourceobjects10()As NXOpen.NXObjectPublic nullNXOpen_BasePart As NXOpen.BasePart=NothingPublic objects1(-1)As NXOpen.NXObjectPublic attributePropertiesBuilder1 As NXOpen.AttributePropertiesBuilder=NothingPublic objects2(0)As NXOpen.NXObjectPublic changed1 AsBoolean=NothingPublic sourceobjects11()As NXOpen.NXObjectPublic attributetitles1(-1)AsStringPublic titlepatterns1(-1)AsStringPublic nXObject1 As NXOpen.NXObject=NothingPublic objects3(0)As NXOpen.NXObjectPublic properties1(0)As NXOpen.NXObjectPublic errorList1 As NXOpen.ErrorList=NothingPublic errorMessageHandler1 As NXOpen.PDM.ErrorMessageHandler=NothingPublic markId2 As NXOpen.Session.UndoMarkId=NothingPublic markId3 As NXOpen.Session.UndoMarkId=NothingPublic errorMessageHandler2 As NXOpen.PDM.ErrorMessageHandler=NothingPublic errorMessageHandler3 As NXOpen.PDM.ErrorMessageHandler=NothingPublic nXObject2 As NXOpen.NXObject=NothingPublic partstocheckin1(0)As NXOpen.BasePartPublic checkininput1 As NXOpen.PDM.PdmPart.CheckinInput=New NXOpen.PDM.PdmPart.CheckinInput(False, True, False)Public operationErrors1 As NXOpen.PDM.OperationErrors=NothingPublic row AsLong=1Public column AsLong=1Public index asinteger=0Public lg As LogFile = theSession.LogFilePublic intchecker asintegerPublic savename asstringPublic PartNames2DArray(0,0)AsString Sub Main() Dim excelBom AsString= ChooseBomFile()If excelBom =""Then'USER PRESSED CANCEL theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "User did not select a file, journal exiting")ExitSubEndIf 'CREATE EXCEL OBJECTDim objExcel =CreateObject("Excel.Application")If objExcel IsNothingThen'USER PRESSED CANCEL theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not start Excel, journal exiting")ExitSubEndIf 'OPEN EXCEL FILEDim objWorkbook = objExcel.Workbooks.Open(excelBom) objExcel.visible=True objWorkbook.activesheet.cells(row, column).select() Dim lastRow AsLong Const xlUp AsInteger=-4162Const xlNone AsInteger=-4142 lastRow = objWorkbook.ActiveSheet.Cells(objWorkbook.activesheet.Rows.Count, column).End(xlUp).Row Dim TempArray(lastRow,1)asstring PartNames2DArray = TempArray lw.WriteLine("Excel File Name: "& excelBom) lw.WriteLine("Last Row: "& lastRow) Do Until row > lastRow objWorkbook.ActiveSheet.Cells(row,1).value=Replace(objWorkbook.ActiveSheet.Cells(row,1).value, vbCr, " ") objWorkbook.ActiveSheet.Cells(row,1).value=Replace(objWorkbook.ActiveSheet.Cells(row,1).value, vbLf, " ") objWorkbook.ActiveSheet.Cells(row,1).value=Replace(objWorkbook.ActiveSheet.Cells(row,1).value, vbCrLf, " ") objWorkbook.ActiveSheet.Cells(row,1).value=Trim(objWorkbook.ActiveSheet.Cells(row,1).value) objWorkbook.ActiveSheet.Cells(row,2).value=Replace(objWorkbook.ActiveSheet.Cells(row,2).value, vbCr, " ") objWorkbook.ActiveSheet.Cells(row,2).value=Replace(objWorkbook.ActiveSheet.Cells(row,2).value, vbLf, " ") objWorkbook.ActiveSheet.Cells(row,2).value=Replace(objWorkbook.ActiveSheet.Cells(row,2).value, vbCrLf, " ") objWorkbook.ActiveSheet.Cells(row,2).value=Trim(objWorkbook.ActiveSheet.Cells(row,2).value) PartNames2DArray(index,0)= objWorkbook.ActiveSheet.Cells(row,1).value PartNames2DArray(index,1)= objWorkbook.ActiveSheet.Cells(row,2).value row = row +1 index = index +1Loop objWorkbook.Saved=True objExcel.Quit() objWorkbook =Nothing objExcel =Nothing lw.Open for i =0to4if PartNames2DArray(i,0)<>""thenTry 'TRIES TO OPEN THE PART SPECIFIED IN THE ARRAY basePart1 = theSession.Parts.OpenActiveDisplay("@DB/"& PartNames2DArray(i,0)&"/"&1, NXOpen.DisplayPartOption.AllowAdditional, partLoadStatus1) workPart = theSession.parts.work dispPart = thesession.parts.display c = dispPart.ComponentAssembly Catch ex As Exception lw.WriteLine("Error Trying to Open Part: "& PartNames2DArray(i,0))goto skipforward EndTryelsegoto skipforward endif ifnot IsNothing(c.RootComponent)then if ArrayIndex =0then lw.writeline("Is first part and is an assembly") fileNameNoExt = c.RootComponent.DisplayName fileNameNoExt = fileNameNoExt.Substring(0, Len(fileNameNoExt)-2)'GETS THE DISPLAYED PART NAME AND TRIMS OFF TWO LETTERS FROM THE STRING IN ORDER TO REMOVE THE FORWARD SLASH AND REVISION NUMBER PartNames(ArrayIndex)= FileNameNoExt lw.WriteLine("Assembly: "& PartNames(ArrayIndex))for intchecker =0toUBound(PartNames2DArray)if PartNames(ArrayIndex)= PartNames2DArray(intchecker, 0)then savename = PartNames2DArray(intchecker, 1) part1 = dispPart saveas exitforendifnext ArrayIndex = ArrayIndex +1else lw.writeline("Not first part and is an assembly")Redim Preserve PartNames(ArrayIndex) fileNameNoExt = c.RootComponent.DisplayName fileNameNoExt = fileNameNoExt.Substring(0, Len(fileNameNoExt)-2)'GETS THE DISPLAYED PART NAME AND TRIMS OFF TWO LETTERS FROM THE STRING IN ORDER TO REMOVE THE FORWARD SLASH AND REVISION NUMBERfor j =0toUBound(PartNames)if fileNameNoExt = PartNames(j)then match =1endifnextif match <>1then PartNames(ArrayIndex)= fileNameNoExt for intchecker =0toUBound(PartNames2DArray)if PartNames(ArrayIndex)= PartNames2DArray(intchecker, 0)then savename = PartNames2DArray(intchecker, 1) part1 = dispPart saveas exitforendifnext ArrayIndex = ArrayIndex +1endif match =0 endif '*** insert code to process 'root component' (assembly file)'*** end of code to process root component ReportComponentChildren(c.RootComponent, 0) else'*** insert code to process piece part lw.writeline("Piece Part")if ArrayIndex =0then lw.writeline("First Part is a Piece Part") dispPart = theSession.Parts.Display fileNameNoExt = dispPart.FullPath fileNameNoExt = fileNameNoExt.Substring(0, Len(fileNameNoExt)-2)'GETS THE DISPLAYED PART NAME AND TRIMS OFF TWO LETTERS FROM THE STRING IN ORDER TO REMOVE THE FORWARD SLASH AND REVISION NUMBER lw.writeline("FileNameNoExt: "& fileNameNoExt)'WRITES THE NAME OF THE OBJECT TO THE LISTING WINDOW PartNames(ArrayIndex)= fileNameNoExt for intchecker =0toUBound(PartNames2DArray)if PartNames(ArrayIndex)= PartNames2DArray(intchecker, 0)then savename = PartNames2DArray(intchecker, 1) part1 = dispPart saveas exitforendifnext ArrayIndex = ArrayIndex +1 else lw.writeline("Not First part and is Piece Part") dispPart = theSession.Parts.DisplayRedim Preserve PartNames(ArrayIndex) fileNameNoExt = dispPart.FullPath fileNameNoExt = fileNameNoExt.Substring(0, Len(fileNameNoExt)-2)'GETS THE DISPLAYED PART NAME AND TRIMS OFF TWO LETTERS FROM THE STRING IN ORDER TO REMOVE THE FORWARD SLASH AND REVISION NUMBERfor j =0toUBound(PartNames)if fileNameNoExt = PartNames(j)then match =1endifnextif match <>1then PartNames(ArrayIndex)= fileNameNoExt for intchecker =0toUBound(PartNames2DArray)if PartNames(ArrayIndex)= PartNames2DArray(intchecker, 0)then savename = PartNames2DArray(intchecker, 1) part1 = dispPart saveas exitforendifnext ArrayIndex = ArrayIndex +1endif match =0 endif endif theSession.CleanUpFacetedFacesAndEdges() theSession.Parts.Display.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing) skipforward: next for j =0toUBound(PartNames) lw.writeline(PartNames(j))next lw.Close EndSub '**********************************************************Sub reportComponentChildren(ByVal comp As Component, _ ByVal indent AsInteger) ForEach child As Component In comp.GetChildren()'*** insert code to process component or subassembly childpart = child.prototype.owningpart fileNameNoExt = Child.DisplayName fileNameNoExt = fileNameNoExt.Substring(0, Len(fileNameNoExt)-2)'GETS THE DISPLAYED PART NAME AND TRIMS OFF TWO LETTERS FROM THE STRING IN ORDER TO REMOVE THE FORWARD SLASH AND REVISION NUMBERfor j =0toUBound(PartNames)if fileNameNoExt = PartNames(j)then match =1endifnextif match <>1thenRedim Preserve PartNames(ArrayIndex) PartNames(ArrayIndex)= fileNameNoExt lw.writeline("I added at the 3rd bottom most level")for intchecker =0toUBound(PartNames2DArray)if PartNames(ArrayIndex)= PartNames2DArray(intchecker, 0)then savename = PartNames2DArray(intchecker, 1) part1 = childpart saveas exitforendifnext ArrayIndex = ArrayIndex +1endif match =0'*** end of code to process component or subassemblyif child.GetChildren.Length<>0thenforeach children as component in child.getchildren()'*** this is a subassembly, add code specific to subassemblies childrenpart = children.prototype.owningpart fileNameNoExt = children.DisplayName fileNameNoExt = fileNameNoExt.Substring(0, Len(fileNameNoExt)-2)'GETS THE DISPLAYED PART NAME AND TRIMS OFF TWO LETTERS FROM THE STRING IN ORDER TO REMOVE THE FORWARD SLASH AND REVISION NUMBERfor j =0toUBound(PartNames)if fileNameNoExt = PartNames(j)then match =1endifnextif match <>1thenRedim Preserve PartNames(ArrayIndex) PartNames(ArrayIndex)= fileNameNoExt lw.writeline("I added at the 2nd bottom most level")for intchecker =0toUBound(PartNames2DArray)if PartNames(ArrayIndex)= PartNames2DArray(intchecker, 0)then savename = PartNames2DArray(intchecker, 1) part1 = childrenpart saveas exitforendifnext ArrayIndex = ArrayIndex +1endif match =0next'*** end of code to process subassemblyelse'this component has no children (it is a leaf node) fileNameNoExt = Child.DisplayName fileNameNoExt = fileNameNoExt.Substring(0, Len(fileNameNoExt)-2)'GETS THE DISPLAYED PART NAME AND TRIMS OFF TWO LETTERS FROM THE STRING IN ORDER TO REMOVE THE FORWARD SLASH AND REVISION NUMBERfor j =0toUBound(PartNames)if fileNameNoExt = PartNames(j)then match =1endifnextif match <>1thenRedim Preserve PartNames(ArrayIndex) PartNames(ArrayIndex)= fileNameNoExt lw.writeline("I added at the bottom most level")for intchecker =0toUBound(PartNames2DArray)if PartNames(ArrayIndex)= PartNames2DArray(intchecker, 0)then savename = PartNames2DArray(intchecker, 1) part1 = childpart saveas exitforendifnext ArrayIndex = ArrayIndex +1endif match =0endif reportComponentChildren(child, indent +1)Next EndSub'**********************************************************PublicFunction GetUnloadOption(ByVal dummy AsString)AsIntegerReturn Session.LibraryUnloadOption.ImmediatelyEndFunction'********************************************************** sub SaveAs() if savename =""then theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "The Save-As name is blank or an error")exitsubendif readOnly1 = dispPart.IsReadOnly partOperationCopyBuilder1 = theSession.PdmSession.CreateCopyOperationBuilder(NXOpen.PDM.PartOperationBuilder.OperationType.SaveAs) partOperationCopyBuilder1.SetOperationSubType(NXOpen.PDM.PartOperationCopyBuilder.OperationSubType.Default) partOperationCopyBuilder1.DefaultDestinationFolder=":Newstuff" partOperationCopyBuilder1.DependentFilesToCopyOption= NXOpen.PDM.PartOperationCopyBuilder.CopyDependentFiles.All partOperationCopyBuilder1.ReplaceAllComponentsInSession=True partOperationCopyBuilder1.SetDialogOperation(NXOpen.PDM.PartOperationBuilder.OperationType.Revise) selectedparts1(0)= dispPart partOperationCopyBuilder1.SetSelectedPartsToCopy(selectedparts1, failedparts1) partOperationCopyBuilder1.CreateLogicalObjects(logicalobjects1) sourceobjects1 = logicalobjects1(0).GetUserAttributeSourceObjects() sourceobjects2 = logicalobjects1(0).GetUserAttributeSourceObjects() sourceobjects3 = logicalobjects1(0).GetUserAttributeSourceObjects() sourceobjects4 = logicalobjects1(0).GetUserAttributeSourceObjects() sourceobjects5 = logicalobjects1(0).GetUserAttributeSourceObjects() partOperationCopyBuilder1.SetDialogOperation(NXOpen.PDM.PartOperationBuilder.OperationType.SaveAs) sourceobjects6 = logicalobjects1(0).GetUserAttributeSourceObjects() selectedparts2(0)= dispPart partOperationCopyBuilder1.SetSelectedPartsToCopy(selectedparts2, failedparts2) partOperationCopyBuilder1.CreateLogicalObjects(logicalobjects2) sourceobjects7 = logicalobjects2(0).GetUserAttributeSourceObjects() sourceobjects8 = logicalobjects2(0).GetUserAttributeSourceObjects() sourceobjects9 = logicalobjects2(0).GetUserAttributeSourceObjects() sourceobjects10 = logicalobjects2(0).GetUserAttributeSourceObjects() attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(nullNXOpen_BasePart, objects1, NXOpen.AttributePropertiesBuilder.OperationType.None) objects2(0)= sourceobjects7(0) attributePropertiesBuilder1.SetAttributeObjects(objects2) attributePropertiesBuilder1.Title="DB_PART_NO" attributePropertiesBuilder1.Category="N4_NonDesignPart" attributePropertiesBuilder1.StringValue= savename changed1 = attributePropertiesBuilder1.CreateAttribute() sourceobjects11 = logicalobjects2(0).GetUserAttributeSourceObjects() nXObject1 = partOperationCopyBuilder1.CreateAttributeTitleToNamingPatternMap(attributetitles1, titlepatterns1) objects3(0)= logicalobjects2(0) properties1(0)= nXObject1 errorList1 = partOperationCopyBuilder1.AutoAssignAttributesWithNamingPattern(objects3, properties1) errorList1.Dispose() errorMessageHandler1 = partOperationCopyBuilder1.GetErrorMessageHandler(True) markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Save Parts As") theSession.DeleteUndoMark(markId2, Nothing) markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Save Parts As") partOperationCopyBuilder1.ValidateLogicalObjectsToCommit() errorMessageHandler2 = partOperationCopyBuilder1.GetErrorMessageHandler(True) errorMessageHandler3 = partOperationCopyBuilder1.GetErrorMessageHandler(True) nXObject2 = partOperationCopyBuilder1.Commit() errorMessageHandler4 = partOperationCopyBuilder1.GetErrorMessageHandler(True) theSession.DeleteUndoMark(markId3, Nothing) partOperationCopyBuilder1.Destroy() attributePropertiesBuilder1.Destroy() partstocheckin1(0)= dispPart operationErrors1 = dispPart.PDMPart.CheckinParts(partstocheckin1, checkininput1) operationErrors1.Dispose() part1 =nothing savename ="" endsub Function ChooseBomFile()AsString Dim fdlg As OpenFileDialog =New OpenFileDialog() fdlg.Title="Select Excel BOM file"Dim dir AsString dir = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) fdlg.InitialDirectory= dir fdlg.Filter="Excel Files(*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm" fdlg.FilterIndex=2 fdlg.RestoreDirectory=TrueIf fdlg.ShowDialog()= DialogResult.OKThenReturn fdlg.FileNameElseReturn""EndIf EndFunction EndModule