3 June 2014
CamBam File Merge Python scripts
This script merges all the drawing objects and machining operations from a list of CamBam (.cb) files
into the current drawing.
References to drawing object IDs, such as machining operation source objects, are remapped to new, unique object IDs
of the merged file.
This script demonstrates:
- opening CamBam (.cb) files.
- iterating through layers, drawing objects, parts and machining operations
- using Windows forms OpenFileDialog
- using Python hash tables for ID remapping
Python version...
import sys
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import OpenFileDialog
from System.Windows.Forms import DialogResult
from System.IO import Directory
from System.Collections.Generic import List
filebrowse = True
cbfiles = []
if (filebrowse):
dlg = OpenFileDialog()
dlg.Filter = doc.OpenFileFilter
dlg.DefaultExt = doc.DefaultExtension
dlg.Multiselect = True
ret = dlg.ShowDialog()
if (not ret == DialogResult.OK): sys.exit()
cbfiles = dlg.FileNames
else:
cbfiles = Directory.GetFiles("C:\\CamBam\\cbfiles\\misc", "*.cb")
doc.Layers.Clear()
doc.Parts.Clear()
for cbfile in cbfiles:
print "Merging ", cbfile
cb = CADFile()
cb.Open(cbfile)
idmap = {}
for layer in cb.Layers:
newlayer = layer.Clone()
newlayer.Entities.Clear()
newlayer.Parent = None
i = 1
while doc.HasLayer(newlayer.Name):
newlayer.Name = layer.Name.rstrip("0123456789") + str(i)
i = i + 1
doc.Layers.Add(newlayer)
for o in layer.Entities:
o2 = o.Clone()
newlayer.Entities.Add(o2)
idmap[o.ID] = o2.ID
for part in cb.Parts:
newpart = part.Clone()
newpart.MachineOps.Clear()
newpart.CADFile = None
i = 1
while doc.HasPart(newpart.Name):
newpart.Name = part.Name.rstrip("0123456789") + str(i)
i = i + 1
if (not String.IsNullOrEmpty(newpart.Nesting.PointListID)):
newpart.Nesting.PointListID = str(idmap[int.Parse(newpart.Nesting.PointListID)])
doc.Parts.Add(newpart)
for mop in part.MachineOps:
m = mop.Clone()
if isinstance(mop,MOPFromGeometry):
newPrimIDs = List[int]()
for ID in mop.PrimitiveIds:
newPrimIDs.Add( idmap[ID] )
m.PrimitiveIds = newPrimIDs.ToArray()
if (isinstance(mop,MOP3DSurface)):
if (mop.BoundaryShapeIds is not None):
newBSPrimIDs = List[int]()
for ID in mop.BoundaryShapeIds:
newBSPrimIDs.Add( idmap[ID] )
m.BoundaryShapeIds = newBSPrimIDs.ToArray()
newpart.MachineOps.Add(m)
print "Merge Finished"