4 December 2012
Tool Library Automation Python scripts
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)
from CamBam.Library import *
tool_library_name = "My Tools"
new_tool_library_name = "Tool Test"
filename = 'C:\\dump\\tool-test.txt'
for tl in CamBamConfig.Defaults.ToolLibraries:
app.log(tl.Name)
tool_library = CamBamConfig.Defaults.ToolLibraries.Find(tool_library_name)
for tool in tool_library:
app.log(tool.Name)
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)
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")
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)
tool6 = new_tool_library.Find(6)
if tool6 is not None:
new_tool_library.Remove(tool6)
app.log("Tool 6 removed")
tool_new = ToolDefinition()
tool_new.Name = new_tool_library.FormatToolName(tool_new)
tool_new.Index = 66
tool_new.Diameter = 6
tool_new.ToolProfile = ToolProfiles.EndMill
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
tool_new.MaxRampAngle = 30
tool_new.ToothLoad = 0.2
tool_new.AxialDepthOfCut = 2
tool_new.RadialDepthOfCut = 1.5
new_tool_library.Add(tool_new)
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')
with open(filename, 'r') as f:
while 1:
s = f.readline()
if s == "": break
s = s.rstrip()
app.log(s)