NX Sample CAM Visual Basic Program: cycle tools in all parts in folder

2019-09-04T12:59:57Z
NX for Manufacturing

Summary


Details

--------------- 

Sample CAM Visual Basic Program: cycle tools in all parts in folder

Solution

 ' This program will cycle through all the part files in a selected folder. 

' For each part, it will cycle through all tools in the tool view. 

' For each tool, it will write the tool name and number in the listing 

' window and the syslog.

Below is the program :

'============================================================================= 

' Copyright 2015 Siemens Product Lifecycle Management Software Inc. All 

Rights Reserved. 

'============================================================================= 

' @<DEL>@ TEXT ENCLOSED within delete markers will be REMOVED 

' REVISIONS 

' 25-may-2010 Mark Rief Adapt from uganswer example to change tolerances 

' 02-jun-2010 Mark Rief Put in uganswer 

' 27-jun02015 Mark Rief Cycle tools in all parts in selected directory 

' TEXT ENCLOSED within delete markers will be REMOVED @<DEL>@ 

' =========================================================================== 

' DESCRIPTION 

' Boiler plate for cycling all the tools in all the parts in a folder. 

' This program will cycle through all the part files in a selected folder. 

' For each part, it will cycle through all tools in the tool view. 

' For each tool, it will write the tool name and number number in the 

listing 

' window and the syslog. 

' Rename the file from .txt to .vb 

' To change the default directory, go to the bottom of this file, look for 

' fbd.SelectedPath, and uncomment and edit one of the lines. 

' ============================================================================

Option Strict Off

Imports NXOpen 

Imports NXOpen.Preferences 

Imports NXOpen.UI 

Imports NXOpen.Utilities 

Imports NXOpen.CAM 

Imports NXOpen.UF 

Imports System 

Imports System.io 

Imports System.Environment 

Imports System.Windows.Forms

Module cycle_all_parts_in_dir 

 Sub Main()

 Dim theSession As Session = Session.GetSession() 

 Dim foldername As String = ""

 ' Open the listing window 

 theSession.ListingWindow.Open()

 ' Select the directory 

 If (select_directory(foldername) <> DialogResult.OK) Then 

 theSession.ListingWindow.WriteLine("Input canceled...exit") 

 Return 

 Else 

 theSession.ListingWindow.WriteLine("Selected directory " & 

foldername) 

 theSession.LogFile.WriteLine("MY JOURNAL MESSAGE - " & 

"Selected directory 

" & foldername)

 End If

 Dim dir As DirectoryInfo = New DirectoryInfo(foldername) 

 Dim fsi As FileSystemInfo

 ' Cycle through all the parts in the directory

 For Each fsi In dir.GetFileSystemInfos() 

 If (TypeOf fsi Is FileInfo And fsi.Extension.Contains("prt")) Then 

 Dim f As FileInfo = CType(fsi, FileInfo) 

 Dim pspec As String = f.FullName 

 theSession.ListingWindow.WriteLine(pspec & " found") 

 theSession.LogFile.WriteLine("MY JOURNAL MESSAGE - 

" & pspec & " found") 

 Do_each_part(pspec) 

 End If 

 Next fsi 

 

 theSession.ListingWindow.WriteLine("End of Parts") 

 theSession.LogFile.WriteLine("MY JOURNAL MESSAGE - " & "End of 

Parts")

 End Sub

 Public Function Do_each_part(ByVal pspec As String) As Boolean 

 ' This is done for every part file 

 Dim theSession As Session = Session.GetSession() 

 Dim theUfSession As UFSession = UFSession.GetUFSession() 

 Dim partLoadStatus As PartLoadStatus = Nothing 

 Dim partSaveStatus As PartSaveStatus 

 Dim WorkPart As Part

 ' Open the listing window 

 theSession.ListingWindow.Open()

 theSession.Parts.LoadOptions.ComponentsToLoad = 

LoadOptions.LoadComponents.None 

 theSession.Parts.OpenBaseDisplay(pspec, partLoadStatus) 

 WorkPart = theSession.Parts.Work 

 Dim setupTag As Tag = NXOpen.Tag.Null 

 Dim camObjectTag As Tag = NXOpen.Tag.Null 

 Dim RootTag As Tag 

 Dim answer As Boolean

 theSession.ListingWindow.WriteLine(pspec & " opened") 

 theSession.LogFile.WriteLine("MY JOURNAL MESSAGE - " & pspec & " 

opened")

 theUfSession.Cam.IsSessionInitialized(answer) 

 If answer = False Then 

 theUfSession.Cam.InitSession() 

 End If 

 theUfSession.Setup.AskSetup(setupTag)

 ' If there is a setup only then we go further 

 If setupTag <> 0 Then 

 Dim ptr As IntPtr = New System.IntPtr 

 Dim cycle_cb_fn As UFNcgroup.CycleCbFT = New 

UFNcgroup.CycleCbFT(AddressOf cycle_cb) 

 theSession.ListingWindow.WriteLine(pspec & " has a CAM setup") 

 theSession.LogFile.WriteLine("MY JOURNAL MESSAGE - " & 

pspec & " has a 

CAM setup")

 'TOOL view 

 theUfSession.Setup.AskMctRoot(setupTag, RootTag) 

 If RootTag <> 0 Then 

 'Cycle through the view and find every object 

 theUfSession.Ncgroup.CycleMembers(RootTag, cycle_cb_fn, ptr) 

 End If 

 

 End If

 ' Close the part 

 WorkPart.Close(NXOpen.BasePart.CloseWholeTree.False, 

NXOpen.BasePart.CloseModified.UseResponses, Nothing) 

 theSession.ListingWindow.WriteLine(pspec & " closed") 

 theSession.LogFile.WriteLine("MY JOURNAL MESSAGE - " & pspec & " 

closed")

 Return True 

 End Function 

 

 Function cycle_cb(ByVal camObjectTag As Tag, ByVal ptr As IntPtr) As 

Boolean 

 'Function called on every object encountered in the CAM cycling

 Dim answer As Boolean 

 Dim theSession = Session.GetSession() 

 Dim theUfSession = UFSession.GetUFSession() 

 Dim WorkPart As Part = TheSession.Parts.Work 

 theSession.ListingWindow.Open()

 ' When cycling check if the object is a Group 

 theUfSession.Ncgroup.IsGroup(camObjectTag, answer)

 ' Only then we need to do something 

 If answer = True Then 

 

 ' Find the cam object from the Tag 

 Dim camObject As NXObject = NXObjectManager.Get(camObjectTag)

 'Check if the object is a Tool 

 If TypeOf camObject Is CAM.Tool Then

theSession.ListingWindow.WriteLine(camObject.Name() & " being cycled") 

 theSession.LogFile.WriteLine("MY JOURNAL 

MESSAGE - " & camObject.Name() & 

" being cycled")

 'Create a tool builder 

 Dim cutterType As Integer 

 Dim cutterSubtype As Integer 

 

theUFSession.Cutter.AskTypeAndSubtype(camObjectTag, cutterType, 

cutterSubtype)

 Dim ToolBuilder = Nothing 

 If cutterType = 1 Then ' Mill 

 ToolBuilder = 

WorkPart.CamSetup.CAMGroupCollection.CreateMillToolBuilder(camObject) 

 ElseIf cutterType = 2 Then 

' Drill 

 ToolBuilder = 

WorkPart.CamSetup.CAMGroupCollection.CreateDrillStdToolBuilder(camObject) 

 ElseIf cutterType = 3 Then 

' Turn 

 ToolBuilder = 

WorkPart.CamSetup.CAMGroupCollection.CreateTurnToolBuilder(camObject) 

 ElseIf cutterType = 4 Then 

' Groove 

 ToolBuilder = 

WorkPart.CamSetup.CAMGroupCollection.CreateGrooveToolBuilder(camObject) 

 ElseIf cutterType = 5 Then 

' Thread 

 ToolBuilder = 

WorkPart.CamSetup.CAMGroupCollection.CreateThreadToolBuilder(camObject) 

 ElseIf cutterType = 7 Then 

' Barrel 

 ToolBuilder = 

WorkPart.CamSetup.CAMGroupCollection.CreateBarrelToolBuilder(camObject) 

 ElseIf cutterType = 8 Then 

' T 

 ToolBuilder = 

WorkPart.CamSetup.CAMGroupCollection.CreateTToolBuilder(camObject) 

 ElseIf cutterType = 9 Then 

' Form 

 ToolBuilder = 

WorkPart.CamSetup.CAMGroupCollection.CreateFormToolBuilder(camObject) 

' ElseIf cutterType = 11 Then 

' Solid 

' ToolBuilder = 

WorkPart.CamSetup.CAMGroupCollection.CreateSolidToolBuilder(camObject) 

 End If 

 

 If ToolBuilder IsNot Nothing Then

 ' Get the tool number 

 

theSession.ListingWindow.WriteLine(camObject.Name() & " Tool Number is " 

& ToolBuilder.TlNumberBuilder.Value ) 

 theSession.LogFile.WriteLine("MY 

JOURNAL MESSAGE - " & camObject.Name() & 

" Tool Number is " & ToolBuilder.TlNumberBuilder.Value )

 'Commit the change (if editing and 

saving) 

 ' millToolBuilder.Commit()

 'Destroy the builder 

 ToolBuilder.Destroy() 

 

 End If 

 

 End If 

 

 End If

 Return True 

 End Function

 Public Function select_directory(ByRef foldername) As 

System.Windows.Forms.DialogResult 

 Dim fbd As FolderBrowserDialog 

 Dim result As System.Windows.Forms.DialogResult

 fbd = New System.Windows.Forms.FolderBrowserDialog() 

 fbd.Description = "Select directory to update ALL parts" 

 fbd.ShowNewFolderButton = False

 ' start browsing at Desktop folder, uncomment for any other default 

directory 

 'fbd.SelectedPath = GetEnvironmentVariable("UGII_BASE_DIR") 

 'fbd.SelectedPath = GetEnvironmentVariable("UGII_BASE_DIR") + 

"\Moldwizard" 

 'fbd.SelectedPath = "c:\mypath1\mypath2" 

 fbd.SelectedPath = "d:\users\rief\nx10\junk"

 result = fbd.ShowDialog() 

 foldername = fbd.SelectedPath 

 fbd.Dispose() 

 Return result

 End Function

End Module

Notes

KB Article ID# PL8009980

Contents

SummaryDetails

Associated Components

Manufacturing General