Imports System Imports NXOpen Imports NXOpenUI Imports NXOpen.UF Imports NXOpen.Utilities Imports System.Threading Imports System.Threading.Tasks Imports NXOpen.Assemblies Imports Excel = Microsoft.Office.Interop.Excel Module featureIdentification Public ufs As UFSession = UFSession.GetUFSession Public thesession As Session = Session.GetSession Public lw As ListingWindow = thesession.ListingWindow Public workpart As Part = thesession.Parts.Work Public wp = workpart Public theUI As UI = UI.GetUI Public basePart1 As NXOpen.BasePart = Nothing Public partLoadStatus1 As NXOpen.PartLoadStatus = Nothing Public partstouse1(0) As NXOpen.BasePart Public partstouse2(0) As NXOpen.BasePart '################# LOAD TOOLS ############################## Public philips(0) As NXOpen.BasePart Public allenm5(0) As NXOpen.BasePart Public allenm6(0) As NXOpen.BasePart Public allenm8(0) As NXOpen.BasePart Public allenm10(0) As NXOpen.BasePart Public allenm12(0) As NXOpen.BasePart Public allenm14(0) As NXOpen.BasePart Public hex8(0) As NXOpen.BasePart Public hex10(0) As NXOpen.BasePart Public hex12(0) As NXOpen.BasePart Public hex14(0) As NXOpen.BasePart Public hex17(0) As NXOpen.BasePart Public hex19(0) As NXOpen.BasePart Public hex24(0) As NXOpen.BasePart '################# LOAD TOOLS ############################## Const xlDown As Long = -4121 Const xlRight As Long = -4161 Public app As New Excel.Application() Public workBooks As Excel.Workbooks = app.Workbooks Public workBook As Excel.Workbook = app.Workbooks.Add() Public sheet As Excel.Worksheet = workBook.ActiveSheet Public cells As Excel.Range = sheet.Cells Public cell_index = 2 Sub Main() 'Dim thevectorselection As vectorselection = Nothing Dim dispPart As Part = thesession.Parts.Display Dim markId1 As NXOpen.Session.UndoMarkId = Nothing markId1 = thesession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start") Try lw.Open() lw.WriteLine("working") Dim green As Integer = 101 Dim blue As Integer = 175 Dim pink As Integer = 150 Dim grey As Integer = 170 Dim brown As Integer = 190 Dim red As Integer = 186 Dim yellow As Integer = 6 partstouse1(0) = load_tool("D:\toll_access\tool1.prt") partstouse2(0) = load_tool("D:\toll_access\tool2.prt") philips(0) = load_tool("D:\toll_access\Phillips\Phillips.prt") allenm5(0) = load_tool("D:\toll_access\Allen bit\Allen_4.prt") allenm6(0) = load_tool("D:\toll_access\Allen bit\Allen_5.prt") allenm8(0) = load_tool("D:\toll_access\Allen bit\Allen_6.prt") allenm10(0) = load_tool("D:\toll_access\Allen bit\Allen_8.prt") allenm12(0) = load_tool("D:\toll_access\Allen bit\Allen_10.prt") allenm14(0) = load_tool("D:\toll_access\Allen bit\Allen_12.prt") hex8(0) = load_tool("D:\toll_access\Hex Socket\HEX_8.prt") hex10(0) = load_tool("D:\toll_access\Hex Socket\HEX_10.prt") hex12(0) = load_tool("D:\toll_access\Hex Socket\HEX_12.prt") hex14(0) = load_tool("D:\toll_access\Hex Socket\HEX_14.prt") hex17(0) = load_tool("D:\toll_access\Hex Socket\HEX_17.prt") hex19(0) = load_tool("D:\toll_access\Hex Socket\HEX_19.prt") hex24(0) = load_tool("D:\toll_access\Hex Socket\HEX_24.prt") partLoadStatus1.Dispose() ufs.Ui.SetStatus("Tools loaded from library") Dim c As ComponentAssembly = dispPart.ComponentAssembly 'to process the work part rather than the display part, comment the previous line and uncomment the following line 'Dim c As ComponentAssembly = workPart.ComponentAssembly ufs.Ui.SetStatus("creating tool access paths") If Not IsNothing(c.RootComponent) Then lw.WriteLine("Assembly: " & c.RootComponent.DisplayName) 'lw.WriteLine(" + Active Arrangement: " & c.ActiveArrangement.Name) cycle_through_components(c.RootComponent, 0) Else lw.WriteLine("Part has no components") End If cells(1, 1).columnwidth = 15 cells(1, 1).value() = "PART NO" cells(1, 2).columnwidth = 40 cells(1, 2).value() = "DESCRIPTION" cells(1, 3).columnwidth = 30 cells(1, 3).value() = "BELONGING ASSEMBLY" cells(1, 4).columnwidth = 65 cells(1, 4).value() = "COORDINATES" cells(1, 5).columnwidth = 17 cells(1, 5).value() = "STANDARD(Y/N?)" cells(1, 6).columnwidth = 10 cells(1, 6).value() = "THREAD" cells(1, 7).columnwidth = 16 cells(1, 7).value() = "BOLT/NUT TYPE" cells(1, 8).columnwidth = 17 cells(1, 8).value() = "TOOL ADDED?" workBook.SaveAs("D:\tool_access.xlsx") workBook.Save() workBook.Close() app.Quit() ufs.Ui.SetStatus("end") thesession.SetUndoMarkName(markId1, " ") Catch ex As Exception lw.WriteLine("error" & ex.ToString) End Try End Sub Function load_tool(t_path As String) As NXOpen.Part thesession.PdmSession.SetNativeMode(True, False) basePart1 = thesession.Parts.OpenBase(t_path, partLoadStatus1) thesession.PdmSession.SetNativeMode(False, False) Dim part1 As NXOpen.Part = CType(basePart1, NXOpen.Part) Return part1 End Function Function add_tool(point6 As Point3d, orientation1 As NXOpen.Matrix3x3, c As String, part_add() As NXOpen.BasePart) '################## COMPONENT BUILDER Dim addComponentBuilder1 As NXOpen.Assemblies.AddComponentBuilder = Nothing addComponentBuilder1 = workpart.AssemblyManager.CreateAddComponentBuilder() '################## COMPONENT POSITIONER Dim componentPositioner1 As NXOpen.Positioning.ComponentPositioner = Nothing componentPositioner1 = workpart.ComponentAssembly.Positioner componentPositioner1.ClearNetwork() Dim arrangement1 As NXOpen.Assemblies.Arrangement = CType(workpart.ComponentAssembly.Arrangements.FindObject("Arrangement 1"), NXOpen.Assemblies.Arrangement) componentPositioner1.PrimaryArrangement = arrangement1 componentPositioner1.BeginAssemblyConstraints() Dim allowInterpartPositioning1 As Boolean = Nothing allowInterpartPositioning1 = thesession.Preferences.Assemblies.InterpartPositioning 'Dim nullNXOpen_Unit As NXOpen.Unit = Nothing '################## UNIT DEFINATION Dim unit1 As NXOpen.Unit = CType(workpart.UnitCollection.FindObject("MilliMeter"), NXOpen.Unit) Dim unit2 As NXOpen.Unit = CType(workpart.UnitCollection.FindObject("Degrees"), NXOpen.Unit) '################## COMPONENT NETWOK Dim network1 As NXOpen.Positioning.Network = Nothing network1 = componentPositioner1.EstablishNetwork() Dim componentNetwork1 As NXOpen.Positioning.ComponentNetwork = CType(network1, NXOpen.Positioning.ComponentNetwork) componentNetwork1.MoveObjectsState = True Dim nullNXOpen_Assemblies_Component As NXOpen.Assemblies.Component = Nothing componentNetwork1.DisplayComponent = nullNXOpen_Assemblies_Component componentNetwork1.MoveObjectsState = True Dim markId2 As NXOpen.Session.UndoMarkId = Nothing markId2 = thesession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Assembly Constraints Update") Dim nullNXOpen_Assemblies_ProductInterface_InterfaceObject As NXOpen.Assemblies.ProductInterface.InterfaceObject = Nothing '##################################################################################################################################################### addComponentBuilder1.ReferenceSet = "Use Model" addComponentBuilder1.Layer = -1 addComponentBuilder1.SetPartsToAdd(part_add) '##################################################################################################################################################### Dim markId3 As NXOpen.Session.UndoMarkId = Nothing markId3 = thesession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Start") thesession.SetUndoMarkName(markId3, "Point") workpart.MeasureManager.SetPartTransientModification() workpart.MeasureManager.ClearPartTransientModification() thesession.DeleteUndoMark(markId3, Nothing) '##################################################################################################################################################### '##################################################################################################################################################### addComponentBuilder1.SetInitialLocationAndOrientation(point6, orientation1) '##################################################################################################################################################### componentNetwork1.Solve() componentPositioner1.ClearNetwork() Dim nErrs1 As Integer = Nothing nErrs1 = thesession.UpdateManager.AddToDeleteList(componentNetwork1) Dim nErrs2 As Integer = Nothing nErrs2 = thesession.UpdateManager.DoUpdate(markId2) componentPositioner1.EndAssemblyConstraints() Dim logicalobjects1() As NXOpen.PDM.LogicalObject addComponentBuilder1.GetLogicalObjectsHavingUnassignedRequiredAttributes(logicalobjects1) Dim t = "TOOL1" + c lw.WriteLine(t) addComponentBuilder1.ComponentName = t Dim nXObject1 As NXOpen.NXObject = Nothing nXObject1 = addComponentBuilder1.Commit() Dim errorList1 As NXOpen.ErrorList = Nothing errorList1 = addComponentBuilder1.GetOperationFailures() errorList1.Dispose() ' addComponentBuilder1.Destroy() Dim nullNXOpen_Assemblies_Arrangement As NXOpen.Assemblies.Arrangement = Nothing componentPositioner1.PrimaryArrangement = nullNXOpen_Assemblies_Arrangement thesession.DeleteUndoMark(markId2, Nothing) thesession.CleanUpFacetedFacesAndEdges() End Function Function cycle_through_components(ByVal comp As Component, ByVal indent As Integer) Dim c As String Dim y1 As String = Nothing Dim thread As String = Nothing Dim btype As String = Nothing Dim tools As String = Nothing Dim aflat As String = Nothing Dim flag3 As Boolean = False For Each child As Component In comp.GetChildren() c = child.DisplayName.ToString If Not child.ReferenceSet.ToString = "Empty" Then lw.WriteLine(New String(" ", indent * 2) & child.DisplayName()) Try If child.GetUserAttribute("CATEGORY", NXObject.AttributeType.String, -1).StringValue = "HW (Hardware includes nuts,washers)" Then lw.WriteLine("hardware") If Not child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("WASHER") And Not child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("DOWEL") And Not child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("CLAMP") And Not child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("CIRCLIP") And Not child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("PIN") And child.Parent.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("INSTALL") Then lw.WriteLine(child.Parent.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue) lw.WriteLine("added") Dim ref1 As NXOpen.Point3d = New NXOpen.Point3d(0, 0, 0) Dim ref2 As NXOpen.Matrix3x3 = Nothing child.GetPosition(ref1, ref2) lw.WriteLine(ref1.ToString) lw.WriteLine(ref2.ToString) If child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("NUT") Then btype = "FLANGED_NUT" ElseIf child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("BUTTON") Or child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("SOCKET") Then btype = "ALEN" ElseIf child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("BOLT") Then btype = "HEX_BOLT" ElseIf child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("SCREW") Then btype = "SCREW" add_tool(ref1, ref2, c, philips) Else btype = "NA" add_tool(ref1, ref2, c, partstouse1) End If Dim z As String = child.DisplayName.ToString If z(0) = "K" Then y1 = "Y" thread = "{}{}".Format(z(4) + z(5)) tools = btype + thread.ToString If btype = "HEX_BOLT" Or btype = "FLANGED_NUT" Then If z.Contains("KAE") Or z.Contains("KC") Then aflat = thread + 4 Else aflat = thread + 2 End If If aflat = 8 Or aflat = 7 Then add_tool(ref1, ref2, c, hex8) ElseIf aflat = 10 Then add_tool(ref1, ref2, c, hex10) ElseIf aflat = 12 Then add_tool(ref1, ref2, c, hex12) ElseIf aflat = 14 Then add_tool(ref1, ref2, c, hex14) ElseIf aflat = 16 Then add_tool(ref1, ref2, c, hex17) ElseIf aflat = 18 Then add_tool(ref1, ref2, c, hex19) ElseIf aflat > 18 Then add_tool(ref1, ref2, c, hex24) Else add_tool(ref1, ref2, c, partstouse1) End If End If If btype = "ALEN" Then If thread = 5 Then add_tool(ref1, ref2, c, allenm5) ElseIf thread = 6 Then add_tool(ref1, ref2, c, allenm6) ElseIf thread = 8 Then add_tool(ref1, ref2, c, allenm8) ElseIf thread = 10 Then add_tool(ref1, ref2, c, allenm10) ElseIf thread = 12 Then add_tool(ref1, ref2, c, allenm12) ElseIf thread > 12 Then add_tool(ref1, ref2, c, allenm14) Else add_tool(ref1, ref2, c, partstouse1) End If End If Else y1 = "N" If child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("M5") Then thread = 5 tools = btype + thread.ToString ElseIf child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("M6") Then thread = 6 tools = btype + thread.ToString ElseIf child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("M8") Then thread = 8 tools = btype + thread.ToString ElseIf child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("M10") Then thread = 10 tools = btype + thread.ToString ElseIf child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("M12") Then thread = 12 tools = btype + thread.ToString ElseIf child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("M14") Then thread = 14 tools = btype + thread.ToString ElseIf child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("M16") Then thread = 16 tools = btype + thread.ToString ElseIf child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("M18") Then thread = 18 tools = btype + thread.ToString ElseIf child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue.Contains("M24") Then thread = 24 tools = btype + thread.ToString Else thread = 0 tools = "NA" + thread.ToString End If If btype = "HEX_BOLT" Or btype = "FLANGED_NUT" Then flag3 = True aflat = thread + 4 If aflat > 6 And aflat < 9 Then add_tool(ref1, ref2, c, hex8) ElseIf aflat = 10 Then add_tool(ref1, ref2, c, hex10) ElseIf aflat = 12 Then add_tool(ref1, ref2, c, hex12) ElseIf aflat = 14 Then add_tool(ref1, ref2, c, hex14) ElseIf aflat = 16 Then add_tool(ref1, ref2, c, hex17) ElseIf aflat = 18 Then add_tool(ref1, ref2, c, hex19) ElseIf aflat > 18 Then add_tool(ref1, ref2, c, hex24) Else add_tool(ref1, ref2, c, partstouse1) End If End If If btype = "ALEN" Then If thread = 5 Then add_tool(ref1, ref2, c, allenm5) ElseIf thread = 6 Then add_tool(ref1, ref2, c, allenm6) ElseIf thread = 8 Then add_tool(ref1, ref2, c, allenm8) ElseIf thread = 10 Then add_tool(ref1, ref2, c, allenm10) ElseIf thread = 12 Then add_tool(ref1, ref2, c, allenm12) ElseIf thread > 12 Then add_tool(ref1, ref2, c, allenm14) Else add_tool(ref1, ref2, c, partstouse1) End If End If End If cells(cell_index, 1).value() = child.DisplayName.ToString cells(cell_index, 2).value() = child.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue cells(cell_index, 3).value() = child.Parent.GetUserAttribute("DB_PART_NAME", NXObject.AttributeType.String, -1).StringValue cells(cell_index, 4).value() = ref1.ToString cells(cell_index, 5).value() = y1 cells(cell_index, 6).value() = thread cells(cell_index, 7).value() = btype If flag3 Then cells(cell_index, 8).value() = "Tool Added, User to confirm A/F" flag3 = False Else cells(cell_index, 8).value() = "Tool Added" End If cell_index += 1 End If End If Catch lw.WriteLine("ddddddd") GoTo NextIteration End Try NextIteration: End If cycle_through_components(child, indent + 1) Next End Function Public Function GetUnloadOption(ByVal dummy As String) As Integer Return CType(NXOpen.Session.LibraryUnloadOption.Immediately, Integer) End Function End Module