Journal to Create components(layer wise) from Assembly bodies after reading File names from an Excel

We normally Model/mock stuff as bodies at assembly level and then move those bodies into components. I tried automating this , so that the new part names are read from an excel file, and the solid bodies(which are segregated by layer no's) are then Cut and pasted into the new part. Everything works fine, the only problem I am facing now is that the new parts also have duplicate dumb solids of the bodies which were cut-pasted into them, I am manually deleting those.
Can someone Please look at the below code and let me know where the problem is?
PS: This is my first Post here :)

' NX 11.0.2.7
' Journal created by lavjit jain on Thu Aug 13 21:44:23 2020 India Standard Time
'
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module CreateDetails

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

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

Structure GMDetail
Dim PartName As String
Dim LayerNo As Integer
Dim DBPartName As String
End Structure

Private excelFileName As String = "C:\Users\lavjitja\Desktop\macros\test template\Create Details.xlsx"
Dim displayPart As NXOpen.Part = theSession.Parts.Display
Dim workPart As NXOpen.Part = theSession.Parts.Work

Sub Main()

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

'save the assembly location
Dim parentFolder As String = IO.Path.GetDirectoryName(theSession.Parts.Display.FullPath)

' Try to access Excel
Dim objExcel = CreateObject("Excel.Application")

' Open Excel workbook (filename, confirm conversion = false, open read-only = true)
Dim objWorkbook = objExcel.Workbooks.Open (excelFileName, false, true)

Dim row As Integer
row = 2 ' we skip the first row, as we assume this contains the column headings.

Dim rowData As GMDetail

' Loop over all data in the spreadsheet

Do
' Read data from spreadsheet
rowData.LayerNo = objExcel.Cells(row, 2).Value
rowData.PartName = objExcel.Cells(row, 3).Value
rowData.DBPartName = objExcel.Cells(row, 4).Value
theSession.ListingWindow().WriteLine("Object: " + rowData.PartName + " read data.")

' If the objType is not empty, call function to assign the attributes for the object
If Not String.IsNullOrWhiteSpace(rowData.LayerNo)

'code for getting all objects on a particular layer
Dim objs() As NXObject = displayPart.Layers.GetAllObjectsOnLayer(rowData.LayerNo)

'create new components
CreateNewComponent(objs, IO.Path.Combine(parentFolder, rowData.PartName & ".f01.0010.prt"), rowData.LayerNo, rowData.DBPartName)

End If

row = row + 1 ' next row until we find an empty one

Loop Until String.IsNullOrWhiteSpace(rowData.LayerNo)

' Close Excel
objExcel.Quit

'save the display part (the assembly)
Dim theSaveStatus As PartSaveStatus
theSaveStatus = theSession.Parts.Display.Save(BasePart.SaveComponents.True, False)

End Sub

'code for adding component:

Sub CreateNewComponent(ByVal objects() As NXObject, ByVal fileName As String, ByVal layernum As String,ByVal DBPart As String)

Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Add Component")

Dim basePart1 As NXOpen.BasePart = Nothing
Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
basePart1 = theSession.Parts.OpenBase("C:\Users\lavjitja\Desktop\macros\test template\mbXXXXXXl.f01.0010_Part.prt", partLoadStatus1)

partLoadStatus1.Dispose()

Dim basePoint1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
Dim orientation1 As NXOpen.Matrix3x3 = Nothing
orientation1.Xx = 1.0
orientation1.Xy = 0.0
orientation1.Xz = 0.0
orientation1.Yx = 0.0
orientation1.Yy = 1.0
orientation1.Yz = 0.0
orientation1.Zx = 0.0
orientation1.Zy = 0.0
orientation1.Zz = 1.0
Dim partLoadStatus2 As NXOpen.PartLoadStatus = Nothing
Dim component1 As NXOpen.Assemblies.Component = Nothing
component1 = workPart.ComponentAssembly.AddComponent("C:\Users\lavjitja\Desktop\macros\test template\mbXXXXXXl.f01.0010_Part.prt", "PART", "MBXXXXXXL.F01.0010_PART", basePoint1, orientation1, layernum , partLoadStatus2, True)

partLoadStatus2.Dispose()

Dim partLoadStatus3 As NXOpen.PartLoadStatus = Nothing
theSession.Parts.SetWorkComponent(component1, NXOpen.PartCollection.RefsetOption.Current, NXOpen.PartCollection.WorkComponentOption.Visible, partLoadStatus3)

workPart = theSession.Parts.Work ' mbXXXXXXl.f01.0010_Part
partLoadStatus3.Dispose()
WorkPart.SetAttribute ("DB_PART_NAME", DBPart )
WorkPart.SetAttribute ("DB_PART_DESC", DBPart )

' ----------------------------------------------
' Menu: File->Save As...
' ----------------------------------------------
Dim partSaveStatus1 As NXOpen.PartSaveStatus = Nothing
partSaveStatus1 = workPart.SaveAs(filename)

partSaveStatus1.Dispose()

Dim nullNXOpen_Assemblies_Component As NXOpen.Assemblies.Component = Nothing

Dim partLoadStatus4 As NXOpen.PartLoadStatus = Nothing
theSession.Parts.SetWorkComponent(nullNXOpen_Assemblies_Component, NXOpen.PartCollection.RefsetOption.Current, NXOpen.PartCollection.WorkComponentOption.Visible, partLoadStatus4)

workPart = theSession.Parts.Work ' TEST DATA
partLoadStatus4.Dispose()
' ----------------------------------------------
' Menu: Edit->Copy
' ----------------------------------------------
workPart.PmiManager.RestoreUnpastedObjects()

Dim markId8 As NXOpen.Session.UndoMarkId = Nothing
markId8 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Copy")

Dim copyCutBuilder1 As NXOpen.Gateway.CopyCutBuilder = Nothing
copyCutBuilder1 = workPart.ClipboardOperationsManager.CreateCopyCutBuilder()

copyCutBuilder1.CanCopyAsSketch = False

copyCutBuilder1.IsCut = True

copyCutBuilder1.ToClipboard = True

copyCutBuilder1.DestinationFilename = Nothing

copyCutBuilder1.SetObjects(objects)

Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = copyCutBuilder1.Commit()

copyCutBuilder1.Destroy()

theSession.DeleteUndoMark(markId8, Nothing)

Dim markId9 As NXOpen.Session.UndoMarkId = Nothing
markId9 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Make Work Part")

Dim partLoadStatus5 As NXOpen.PartLoadStatus = Nothing
theSession.Parts.SetWorkComponent(component1, NXOpen.PartCollection.RefsetOption.Current, NXOpen.PartCollection.WorkComponentOption.Visible, partLoadStatus5)

workPart = theSession.Parts.Work ' asdasd
partLoadStatus5.Dispose()
theSession.SetUndoMarkName(markId9, "Make Work Part")

' ----------------------------------------------
' Menu: Edit->Paste
' ----------------------------------------------
Dim markId10 As NXOpen.Session.UndoMarkId = Nothing
markId10 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Paste")

Dim pasteBuilder1 As NXOpen.Gateway.PasteBuilder = Nothing
pasteBuilder1 = workPart.ClipboardOperationsManager.CreatePasteBuilder()

Dim nXObject2 As NXOpen.NXObject = Nothing
nXObject2 = pasteBuilder1.Commit()

pasteBuilder1.Destroy()

Dim markId11 As NXOpen.Session.UndoMarkId = Nothing
markId11 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Make Work Part")

Dim partLoadStatus6 As NXOpen.PartLoadStatus = Nothing
theSession.Parts.SetWorkComponent(nullNXOpen_Assemblies_Component, NXOpen.PartCollection.RefsetOption.Current, NXOpen.PartCollection.WorkComponentOption.Visible, partLoadStatus6)

workPart = theSession.Parts.Work ' TEST DATA
partLoadStatus6.Dispose()
theSession.SetUndoMarkName(markId11, "Make Work Part")

End Sub

End Module

On the assembly tab, there is a command called "create new"; this command will allow you to select geometry from the assembly, and it will create a new component, move the geometry to the new file, and add the component to the assembly. You might have better luck with this command rather than using the copy/paste method.

I made a code for that too, but i kept on getting an error telling that the file that I am trying to use isnt a template file(even though enforce as pierce part wasnt switched on for this file)
below is the Code for that

Sub CreateNewComponent(ByVal objects() As NXObject, ByVal fileName As String, ByVal layernum As String)

Dim fileNew1 As FileNew
fileNew1 = theSession.Parts.FileNew()

'if using your template, change to False
fileNew1.UseBlankTemplate = False

fileNew1.TemplateFileName = "V:\Work\MISL\Lavjit\SEED-FILES\mbXXXXXXl.f01.0010_Part.prt"
fileNew1.Units = Part.Units.Millimeters

fileNew1.TemplateFileName = "Blank"

fileNew1.ApplicationName = "GatewayTemplate"
'fileNew1.ApplicationName = "ModelTemplate"

fileNew1.RelationType = ""

fileNew1.UsesMasterModel = "No"

fileNew1.TemplateType = FileNewTemplateType.Item

fileNew1.NewFileName = fileName

fileNew1.MasterFileName = ""

fileNew1.MakeDisplayedPart = False

Dim createNewComponentBuilder1 As Assemblies.CreateNewComponentBuilder
createNewComponentBuilder1 = theSession.Parts.Work.AssemblyManager.CreateNewComponentBuilder()

createNewComponentBuilder1.LayerOption = Assemblies.CreateNewComponentBuilder.ComponentLayerOptionType.AsSpecified

createNewComponentBuilder1.DefiningObjectsAdded = False

'code for adding solid bodies:

For Each obj As NXObject In objects

If TypeOf (obj) Is NXOpen.Body Then
Dim added1 As Boolean = Nothing
added1 = createNewComponentBuilder1.ObjectForNewComponent.Add(obj)
End If
Next

createNewComponentBuilder1.ComponentOrigin = Assemblies.CreateNewComponentBuilder.ComponentOriginType.Absolute

createNewComponentBuilder1.NewFile = fileNew1

createNewComponentBuilder1.ReferenceSet = createNewComponentBuilder1.ComponentReferenceSetType.EntirePartOnly
createNewComponentBuilder1.ReferenceSetName = "Part"

Dim nXObject2 As NXObject
nXObject2 = createNewComponentBuilder1.Commit()

createNewComponentBuilder1.Destroy()

End Sub

Learn Learn Learn

Nevertheless, I still dont Understand what is wrong with the Cut/Paste method. The logic seems right, is it an NX bug?

Learn Learn Learn

If you suspect that it is a bug, I'd suggest that you contact GTAC and report your findings. I've not run your code as I have not recreated all the necessary supporting files, so I can't really comment on if it is a bug or intended functionality.