3 June 2014 CamBam File Merge Python scripts

COMMENTS

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...

Python Script:

"""

CBFileMerge.py

Merge multiple CamBam (*.cb) files into the current drawing.

Version: 1.0, [2014-06-03]
Author: 10bulls

"""

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

# switch to toggle between a file open dialog or directory 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:
    # Merge all .cb files in a directory...
    cbfiles = Directory.GetFiles("C:\\CamBam\\cbfiles\\misc", "*.cb")
    # or declare an array of files...
    # cbfiles = [ "C:\\CamBam\\cbfiles\\0.9.8\\ball.cb",
    #            "C:\\CamBam\\cbfiles\\0.9.8\\continuous-spiral-lead.cb" ]

doc.Layers.Clear()  
doc.Parts.Clear()          
            
# loop through cambam files
for cbfile in cbfiles:

    print "Merging ", cbfile
    
    cb = CADFile()
    cb.Open(cbfile)

    # make a hashtable to map primitive IDs
    idmap = {}

    # loop through layers
    for layer in cb.Layers:
        # copy the layer
        newlayer = layer.Clone()
        # Clone makes a shallow copy of Entities, so need to clear this
        newlayer.Entities.Clear()
        newlayer.Parent = None
        
        # get a unique name for the copied layer
        i = 1
        while doc.HasLayer(newlayer.Name):
            newlayer.Name = layer.Name.rstrip("0123456789") + str(i)
            i = i + 1
    
        doc.Layers.Add(newlayer)

        # loop through drawing objects
        for o in layer.Entities:
            
            # copy drawing object
            o2 = o.Clone()
            newlayer.Entities.Add(o2)
            
            # maintain a map of old ID -> new ID
            idmap[o.ID] = o2.ID
            
    # loop through parts
    for part in cb.Parts:
        # copy the part
        newpart = part.Clone()
        # Clone makes a shallow copy of MachineOps, so need to clear this
        newpart.MachineOps.Clear()
        newpart.CADFile = None
        
        # get a unique name for the copied part
        i = 1
        while doc.HasPart(newpart.Name):
            newpart.Name = part.Name.rstrip("0123456789") + str(i)
            i = i + 1

        # Remap nesting point list IDs
        if (not String.IsNullOrEmpty(newpart.Nesting.PointListID)):
            newpart.Nesting.PointListID = str(idmap[int.Parse(newpart.Nesting.PointListID)])
            
        doc.Parts.Add(newpart)
        
        # loop through machining operations
        for mop in part.MachineOps:
            # copy machining operation
            m = mop.Clone()
            
            # Remap primitive IDs for MOPFromGeometry
            if isinstance(mop,MOPFromGeometry):
                newPrimIDs = List[int]()
                for ID in mop.PrimitiveIds:
                    newPrimIDs.Add( idmap[ID] )
                m.PrimitiveIds = newPrimIDs.ToArray()
                
                # For 3D Surface, remap Boundary shape IDs
                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"

Comments

<< 1 >> 
#  Larry Mcclendon 28/06/2014 21:34:39
I having trouble with cam bambecause the one Ipurchased was on xp computer that crashed widos 7 cam bam fails to find mach3 I need the key so cam bam will work right .if I donot here from you I am going to purchase a 3d printer and use that to make parts cause 3d print comes with a cad program.
Reply Quote   4  
#  Rob 24/08/2016 05:06:50
Pasted this right into CAMBAM Scipt New Window, selected compile and it ran no problems. I merged three files into one sucessfully on the first attempt. Coordinates were spot on. Great addition to the tool kit.
Reply Quote   5  
#  anon 05/10/2016 20:41:37
Will this script work if using cambam that has used all 40 instances?
Reply Quote   8  

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