4 December 2012 Tool Library Automation Python scripts

COMMENTS

Maintaining large lists of tools in different libraries and across different applications can be a tedious business.

This script is intended to demonstrate various methods to aid tool library maintenance, including:

  • Finding tool libraries by name
  • Adding new or removing existing tool libraries
  • Enumerating tools within a library
  • Copying tools from one library to another
  • Finding tools in a library by tool number
  • Removing and creating new tool definitions
  • Writing tools to external files (such as a CSV text file)

Python Script:

#
# tool-library.py
#

from CamBam.Library import *

tool_library_name = "My Tools"
new_tool_library_name = "Tool Test"
filename = 'C:\\dump\\tool-test.txt'

# List tool libraries...
for tl in CamBamConfig.Defaults.ToolLibraries:
	app.log(tl.Name)

# find a tool library by name...
tool_library = CamBamConfig.Defaults.ToolLibraries.Find(tool_library_name)

# List tools in library...
for tool in tool_library:
	app.log(tool.Name)

# Remove a tool library if it exists...
new_tool_library = CamBamConfig.Defaults.ToolLibraries.Find(new_tool_library_name)
if new_tool_library is not None:
	CamBamConfig.Defaults.ToolLibraries.Remove(new_tool_library)

# create a new tool library
new_tool_library = ToolLibrary()
new_tool_library.Name = new_tool_library_name
new_tool_library.ToolNameFormat = "{$diameter}mm {$flutes} flute {$profile}"
CamBamConfig.Defaults.ToolLibraries.Add( new_tool_library )
app.log("Tool library '" + new_tool_library.Name + "' created")

# copy all tools from tool_library into new_tool_library:
for tool in tool_library:
	tool_copy = tool.Clone()
	tool_copy.Notes = String.Format("Copied from {0}", tool_library.Name )
	tool_copy.PartCode = String.Format("{0}({1})", tool_library.Name, tool.Index )
	new_tool_library.Add(tool_copy)

# find tool by tool index...
tool6 = new_tool_library.Find(6)

# remove a tool from a library...
if tool6 is not None:
	new_tool_library.Remove(tool6)
	app.log("Tool 6 removed")

# create a new tool definition...
tool_new = ToolDefinition()

# set tool name
# tool_new.Name = "?" 
# or...
# generate a tool name from the library's tool format...
tool_new.Name = new_tool_library.FormatToolName(tool_new)

# if index is not set, one will be automatically generated
tool_new.Index = 66
tool_new.Diameter = 6
tool_new.ToolProfile = ToolProfiles.EndMill
# Unspecified
# EndMill
# BallNose 
# BullNose
# VCutter
# Drill
# Lathe

tool_new.Flutes = 3
tool_new.FluteLength = 15
tool_new.Length = 40
tool_new.Notes = "Tool created by script"
tool_new.PartCode = "123456"
tool_new.Material = "HSS"
tool_new.Coating = "TiN"
tool_new.ShankDiameter = 6.35
tool_new.HelixAngle = 45
tool_new.VeeAngle = 0
# uncomment to override the post processor tool change command
# tool_new.ToolChange = "T{$tool.index} M6"
#
# NOTE: The following fields are currently informational only
# They are not yet used by CamBam for toolpath creation.
#
tool_new.MaxRampAngle = 30
tool_new.ToothLoad = 0.2
tool_new.AxialDepthOfCut = 2
tool_new.RadialDepthOfCut = 1.5

new_tool_library.Add(tool_new)

# write all the tools to a CSV text file

with open(filename, 'w') as f:
	f.write("Index,Name,Diameter,Profile\n")
	for tool in new_tool_library:
		f.write(tool.Index.ToString())
		f.write(',\"')
		f.write(tool.Name) 
		f.write('\",')
		f.write(tool.Diameter.ToString())
		f.write(',\"')
		f.write(tool.ToolProfile.ToString())
		f.write('\"\n')

# read back tool information
with open(filename, 'r') as f:
	while 1:
		s = f.readline()
		if s == "": break
		s = s.rstrip()
		# parse tool information from text but for now, just display text
		app.log(s)

Comments

<< >> 
name: Rate this article

url:
e-mail:
Name, url and e-mail are optional. E-mail addresses will NOT be displayed, but please provide one if you would like a reply.
If your comment or url is not displayed immediately, it may be awaiting approval.
The following BBCode formatting codes are supported : [b], [i], [u], [code], [quote], [list], [*]
Your comments...


Submit Comment