Quantcast
Channel: NX Journaling - Journaling / NXOpen API
Viewing all articles
Browse latest Browse all 787

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

$
0
0

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.GenericImports 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    AsStringDim LayerNo   AsIntegerDim DBPartName    AsStringEndStructure 
	Private excelFileName AsString="C:\Users\lavjitja\Desktop\macros\test template\Create Details.xlsx"Dim displayPart As NXOpen.Part= theSession.Parts.DisplayDim 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 locationDim parentFolder AsString= IO.Path.GetDirectoryName(theSession.Parts.Display.FullPath) 
	' Try to access ExcelDim 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 AsInteger
         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 objectIfNotString.IsNullOrWhiteSpace(rowData.LayerNo) 
				'code for getting all objects on a particular layerDim 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) 
 
                EndIf 
                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) 
 
	EndSub 
 
	'code for adding component: 
	Sub CreateNewComponent(ByVal objects()As NXObject, ByVal fileName AsString, ByVal layernum AsString,ByVal DBPart AsString) 
        	Dim markId1 As NXOpen.Session.UndoMarkId=Nothing
		markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Add Component") 
		Dim basePart1 As NXOpen.BasePart=NothingDim 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.0Dim partLoadStatus2 As NXOpen.PartLoadStatus=NothingDim 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") 
 
 
 
 
    	EndSub 
EndModule

Viewing all articles
Browse latest Browse all 787

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>