Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Looping through a tool list in vb.net


Recommended Posts

I am not a Mastercam user but there are others here who are and I am trying to help them out by extracting cutting tooling information (type of tool, length of cut, how may flutes, etc) and dump that information into an Excel sheet that we use to generate setup sheets.

 

I have been struggling for way to loop through all of the tools in a file; can anyone help me out with an example of the code in vb.net?

 

Thank,

 

Daryl

Link to comment
Share on other sites

Well, first,, there are two ways I know of to access the tool info -

1) from the tool library inside the MCam document, which would be all tools, regardless if they are used or not

2) by parsing thru all the (selected) operations, and getting only the tools used, but you have to filter out the duplicates.

 

 

Second, you didnt specify what version of MasterCam you are coding for. The code below works in MCam X6, but SHOULD work as legacy in X7. (using the supplied Nethook 2 lib)

 

Third, if you have the choice, GO WITH X7. The nethook library has been drastically updated, and you can access all Tool Params, where in X6, it is limited.

 

 

The first sub below will run just by calling it. It gives you all the tools in the MCam document.

 

The second requires you to select some operations before you call it. It will give you a list of only the tools used, with duplicates filtered out. This list is not sorted (because your head may explode) but when you are ready for sorting, gimme a whistle.

 

The MCam Excel lib is very stable; however, if you need more functionality, you'll need to roll your own, and Late Binding has proved for me to be the most stable between Office versions.

 

Cheers.

 

Sub GetLibraryTools()
 Dim AllTools() As Mastercam.Database.Tool = Mastercam.Support.SearchManager.GetTools()
 'GC.Collect()	 ' uncomment this line if you are getting strange crashes in X6
 Dim ToolNumber As Integer = 0
 Dim ToolDia As Double = 0.0
 Dim FluteLength As Double = 0.0
 Dim Msg As String = String.Empty

 For Each SingleTool As Mastercam.Database.Tool In AllTools
	 ToolNumber = SingleTool.Number
	 ToolDia = SingleTool.Diameter
	 FluteLength = SingleTool.FluteLength
	 Msg = Msg & ToolNumber.ToString & vbTab & Format(ToolDia, "0.000") & vbTab & Format(FluteLength, "0.000") & vbCrLf
 Next
 MsgBox(Msg, MsgBoxStyle.Information, "Complete Tool List")
End Sub

Sub GetOperationTools()
 ' parse down selected ops and get tools associated with those SELECTED ops
 Dim op As Mastercam.Database.Operation
 Dim OpList() As Mastercam.Database.Operation = Mastercam.Support.SearchManager.GetOperations(True)
 Dim intOuterLoop As Integer = -1, intInnerLoop As Integer = -1
 Dim ToolIsDuplicate As Boolean = False
 Dim ToolList() As Mastercam.Database.Tool = {}
 Dim ToolListCount As Long = -1
 Dim ToolNumber As Integer = 0
 Dim ToolDia As Double = 0.0
 Dim FluteLength As Double = 0.0
 Dim Msg As String = String.Empty

 For Each op In OpList
	 Select Case op.Type
		 Case 17, 4, -1 ' MANUAL, tRANSORM, OR UNKNOWN - no tool or cant extract tool
		 Case Else
			 Dim SingleTool As Mastercam.Database.Tool = op.OperationTool
			 ToolNumber = SingleTool.Number
			 ToolDia = SingleTool.Diameter
			 FluteLength = SingleTool.FluteLength
			 ToolIsDuplicate = False
			 If ToolListCount = -1 Then
				 ToolIsDuplicate = False
			 Else ' check the list
				 For intInnerLoop = 0 To ToolListCount
					 If ToolNumber = ToolList(intInnerLoop).Number Then
						 ToolIsDuplicate = True
						 Exit For
					 End If
				 Next
			 End If
			 ' If our Tool is not flagged as a duplicate, add it to the array
			 If ToolIsDuplicate = False Then
				 ToolListCount = ToolListCount + 1
				 ReDim Preserve ToolList(ToolListCount)
				 ToolList(ToolListCount) = SingleTool
				 Msg = Msg & ToolNumber.ToString & vbTab & Format(ToolDia, "0.000") & vbTab & Format(FluteLength, "0.000") & vbCrLf
			 End If
	 End Select
 Next op

 MsgBox(Msg, MsgBoxStyle.Information, "Operation Tool List")

End Sub

 

FYI: check out this link: http://www.emastercam.com/board/index.php?showtopic=65946

Link to comment
Share on other sites

Thank you so much; this is exactly what I needed to get over the hump.

 

Real world examples like you provided would be absolutely invaluable if they were included in the help file so those like me who know enough about this stuff to stumble around in it but are missing a few key elements.

 

BTW, I am using X7 so that should help.

 

Daryl

Link to comment
Share on other sites

FWIW, there are some example VBScript files located in your C:\Users\Public\Documents\shared mcamx8\vb folder that I wrote a few years ago that may also help, GetJobInformation.vbs and MS Excel Data.vbs for example .

 

and those are awesome examples. I'd bet those two snippets, along with my fav, "Toolpath Door CHook.vbs", are plagiarized more than any other code except the very first "Hello World" program. (at least after I was done with them) :p If you want a little more functionality than they give, its a LOT more work, and less speedy code (CHooks excluded, natch)

 

and as you can see from my Last Post , me and my old friend VBScript still spend some quality time :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...