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.UF
Imports NXOpen.Assemblies
Imports NXOpenUI
Imports System.Collections.Generic
Imports 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.Work
Public dispPart as NXOpen.Part = theSession.Parts.Display
Public fileNameNoExt As String
Public basePart1 As NXOpen.BasePart = Nothing
Public partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
Public childpart as NXOpen.Part
Public childrenpart as NXOpen.Part
Public PartNames(0) As String
Public ArrayIndex as integer = 0
Public i as integer = 0
Public j as integer
Public match as integer = 0
Public theUISession As UI = UI.GetUI
Public c As ComponentAssembly = dispPart.ComponentAssembly
Public errorMessageHandler4 As NXOpen.PDM.ErrorMessageHandler = Nothing
Public selectedparts1(0) As NXOpen.BasePart
Public failedparts1() As NXOpen.BasePart
Public logicalobjects1() As NXOpen.PDM.LogicalObject
Public sourceobjects1() As NXOpen.NXObject
Public partOperationCopyBuilder1 As NXOpen.PDM.PartOperationCopyBuilder = Nothing
Public readOnly1 As Boolean = Nothing
Public part1 As NXOpen.Part = Nothing
Public sourceobjects2() As NXOpen.NXObject
Public sourceobjects3() As NXOpen.NXObject
Public sourceobjects4() As NXOpen.NXObject
Public sourceobjects5() As NXOpen.NXObject
Public sourceobjects6() As NXOpen.NXObject
Public selectedparts2(0) As NXOpen.BasePart
Public failedparts2() As NXOpen.BasePart
Public logicalobjects2() As NXOpen.PDM.LogicalObject
Public sourceobjects7() As NXOpen.NXObject
Public sourceobjects8() As NXOpen.NXObject
Public sourceobjects9() As NXOpen.NXObject
Public sourceobjects10() As NXOpen.NXObject
Public nullNXOpen_BasePart As NXOpen.BasePart = Nothing
Public objects1(-1) As NXOpen.NXObject
Public attributePropertiesBuilder1 As NXOpen.AttributePropertiesBuilder = Nothing
Public objects2(0) As NXOpen.NXObject
Public changed1 As Boolean = Nothing
Public sourceobjects11() As NXOpen.NXObject
Public attributetitles1(-1) As String
Public titlepatterns1(-1) As String
Public nXObject1 As NXOpen.NXObject = Nothing
Public objects3(0) As NXOpen.NXObject
Public properties1(0) As NXOpen.NXObject
Public errorList1 As NXOpen.ErrorList = Nothing
Public errorMessageHandler1 As NXOpen.PDM.ErrorMessageHandler = Nothing
Public markId2 As NXOpen.Session.UndoMarkId = Nothing
Public markId3 As NXOpen.Session.UndoMarkId = Nothing
Public errorMessageHandler2 As NXOpen.PDM.ErrorMessageHandler = Nothing
Public errorMessageHandler3 As NXOpen.PDM.ErrorMessageHandler = Nothing
Public nXObject2 As NXOpen.NXObject = Nothing
Public partstocheckin1(0) As NXOpen.BasePart
Public checkininput1 As NXOpen.PDM.PdmPart.CheckinInput = New NXOpen.PDM.PdmPart.CheckinInput(False, True, False)
Public operationErrors1 As NXOpen.PDM.OperationErrors = Nothing
Public row As Long = 1
Public column As Long = 1
Public index as integer = 0
Public lg As LogFile = theSession.LogFile
Public intchecker as integer
Public savename as string
Public PartNames2DArray(0,0) As String
Sub Main()
Dim excelBom As String = ChooseBomFile()
If excelBom = "" Then
'USER PRESSED CANCEL
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "User did not select a file, journal exiting")
Exit Sub
End If
'CREATE EXCEL OBJECT
Dim objExcel = CreateObject("Excel.Application")
If objExcel Is Nothing Then
'USER PRESSED CANCEL
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not start Excel, journal exiting")
Exit Sub
End If
'OPEN EXCEL FILE
Dim objWorkbook = objExcel.Workbooks.Open(excelBom)
objExcel.visible = True
objWorkbook.activesheet.cells(row, column).select()
Dim lastRow As Long
Const xlUp As Integer = -4162
Const xlNone As Integer = -4142
lastRow = objWorkbook.ActiveSheet.Cells(objWorkbook.activesheet.Rows.Count, column).End(xlUp).Row
Dim TempArray(lastRow,1) as string
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 + 1
Loop
objWorkbook.Saved = True
objExcel.Quit()
objWorkbook = Nothing
objExcel = Nothing
lw.Open
for i = 0 to 4
if PartNames2DArray(i,0) <> "" then
Try
'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
End Try
else
goto skipforward
end if
if not IsNothing(c.RootComponent) then
if ArrayIndex = 0 then
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 = 0 to UBound(PartNames2DArray)
if PartNames(ArrayIndex) = PartNames2DArray(intchecker, 0) then
savename = PartNames2DArray(intchecker, 1)
part1 = dispPart
saveas
exit for
end if
next
ArrayIndex = ArrayIndex + 1
else
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 NUMBER
for j = 0 to UBound(PartNames)
if fileNameNoExt = PartNames(j) then
match = 1
end if
next
if match <> 1 then
PartNames(ArrayIndex) = fileNameNoExt
for intchecker = 0 to UBound(PartNames2DArray)
if PartNames(ArrayIndex) = PartNames2DArray(intchecker, 0) then
savename = PartNames2DArray(intchecker, 1)
part1 = dispPart
saveas
exit for
end if
next
ArrayIndex = ArrayIndex + 1
end if
match = 0
end if
'*** 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 = 0 then
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 = 0 to UBound(PartNames2DArray)
if PartNames(ArrayIndex) = PartNames2DArray(intchecker, 0) then
savename = PartNames2DArray(intchecker, 1)
part1 = dispPart
saveas
exit for
end if
next
ArrayIndex = ArrayIndex + 1
else
lw.writeline("Not First part and is Piece Part")
dispPart = theSession.Parts.Display
Redim 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 NUMBER
for j = 0 to UBound(PartNames)
if fileNameNoExt = PartNames(j) then
match = 1
end if
next
if match <> 1 then
PartNames(ArrayIndex) = fileNameNoExt
for intchecker = 0 to UBound(PartNames2DArray)
if PartNames(ArrayIndex) = PartNames2DArray(intchecker, 0) then
savename = PartNames2DArray(intchecker, 1)
part1 = dispPart
saveas
exit for
end if
next
ArrayIndex = ArrayIndex + 1
end if
match = 0
end if
end if
theSession.CleanUpFacetedFacesAndEdges()
theSession.Parts.Display.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing)
skipforward:
next
for j = 0 to UBound(PartNames)
lw.writeline(PartNames(j))
next
lw.Close
End Sub
'**********************************************************
Sub reportComponentChildren( ByVal comp As Component, _
ByVal indent As Integer)
For Each 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 NUMBER
for j = 0 to UBound(PartNames)
if fileNameNoExt = PartNames(j) then
match = 1
end if
next
if match <> 1 then
Redim Preserve PartNames(ArrayIndex)
PartNames(ArrayIndex) = fileNameNoExt
lw.writeline("I added at the 3rd bottom most level")
for intchecker = 0 to UBound(PartNames2DArray)
if PartNames(ArrayIndex) = PartNames2DArray(intchecker, 0) then
savename = PartNames2DArray(intchecker, 1)
part1 = childpart
saveas
exit for
end if
next
ArrayIndex = ArrayIndex + 1
end if
match = 0
'*** end of code to process component or subassembly
if child.GetChildren.Length <> 0 then
for each 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 NUMBER
for j = 0 to UBound(PartNames)
if fileNameNoExt = PartNames(j) then
match = 1
end if
next
if match <> 1 then
Redim Preserve PartNames(ArrayIndex)
PartNames(ArrayIndex) = fileNameNoExt
lw.writeline("I added at the 2nd bottom most level")
for intchecker = 0 to UBound(PartNames2DArray)
if PartNames(ArrayIndex) = PartNames2DArray(intchecker, 0) then
savename = PartNames2DArray(intchecker, 1)
part1 = childrenpart
saveas
exit for
end if
next
ArrayIndex = ArrayIndex + 1
end if
match = 0
next
'*** end of code to process subassembly
else
'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 NUMBER
for j = 0 to UBound(PartNames)
if fileNameNoExt = PartNames(j) then
match = 1
end if
next
if match <> 1 then
Redim Preserve PartNames(ArrayIndex)
PartNames(ArrayIndex) = fileNameNoExt
lw.writeline("I added at the bottom most level")
for intchecker = 0 to UBound(PartNames2DArray)
if PartNames(ArrayIndex) = PartNames2DArray(intchecker, 0) then
savename = PartNames2DArray(intchecker, 1)
part1 = childpart
saveas
exit for
end if
next
ArrayIndex = ArrayIndex + 1
end if
match = 0
end if
reportComponentChildren(child, indent + 1)
Next
End Sub
'**********************************************************
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
'**********************************************************
sub SaveAs()
if savename = "" then
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "The Save-As name is blank or an error")
exit sub
end if
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 = ""
end sub
Function ChooseBomFile() As String
Dim fdlg As OpenFileDialog = New OpenFileDialog()
fdlg.Title = "Select Excel BOM file"
Dim dir As String
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 = True
If fdlg.ShowDialog() = DialogResult.OK Then
Return fdlg.FileName
Else
Return ""
End If
End Function
End Module
re: save-as
It sounds like what you really want is to clone your assembly. Unfortunately, I don't have any example code for it.
The next best thing might be a save-as on your component files and assembly. There is some code in the link below (by Steve Labout) that might help or give you ideas.
http://www.nxjournaling.com/comment/289#comment-289
Thanks,
Thanks,
This is in PLM and it basically works right now if im just saving-as the top level assembly or the part is a piece part but for whatever reason it doesn't successfully save-as the children in assemblies.