Author Topic: CamBam File Merge  (Read 10788 times)

Offline 10bulls

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 2164
  • Coding Jedi
    • View Profile
    • www.cambam.info
CamBam File Merge
« on: June 03, 2014, 09:47:03 am »
I have just added a new Python script to the reference section which can be used to merge multiple CamBam files into the current drawing...

http://www.cambam.info/ref/script.cbfilemerge

The script can merge files selected with a file open dialog, directory listings or hard coded lists.

All the primitive ID remapping is taken care of, so machining operations will still be linked to their correct drawing objects.

I hope someone finds this script useful!  :)

Offline lloydsp

  • CNC Jedi
  • *****
  • Posts: 8331
    • View Profile
Re: CamBam File Merge
« Reply #1 on: June 03, 2014, 11:14:09 am »
Oh, boy, oh boy, oh BOY will I find that useful, Andy!

Finally, I can split my projects into smaller files, and not battle getting them back together for the archive version.

And welcome back! (???)

Are you stopping by this neck of the woods for long? <G>

LLoyd
"Pyro for Fun and Profit for More Than Fifty Years"

Offline pixelmaker

  • CNC Jedi
  • *****
  • Posts: 1777
    • View Profile
    • pixelmaker
Re: CamBam File Merge
« Reply #2 on: June 03, 2014, 11:14:51 am »
!! HE IS STILL ALIVE !!   ;D

Nice to hear from you. This is, for me, more important then every script.

But the script I will test soon.


ralf

Offline Bubba

  • CNC Jedi
  • *****
  • Posts: 2984
    • View Profile
Re: CamBam File Merge
« Reply #3 on: June 03, 2014, 12:48:55 pm »
Andy, you've been gone for awhile, is everything alright?

I'm hoping that it was looonnng well deserved vacation. ;) :D
My 2¢

Win 10 64 bit, CB [1.0} rc 1 64 bit, Mach3, ESS, G540

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6133
    • View Profile
    • Cambam V1.0 French Doc
Re: CamBam File Merge
« Reply #4 on: June 03, 2014, 13:39:20 pm »
hello Andy,

Happy to hear about you, and welcome back  :D

++
David

Offline 10bulls

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 2164
  • Coding Jedi
    • View Profile
    • www.cambam.info
Re: CamBam File Merge
« Reply #5 on: June 03, 2014, 14:47:59 pm »
It's nice to be missed!  ;D
Everything is fine this end, thanks everyone and sorry once again for the radio silence! 

There's certainly lots going on here.  Development of CamBam V1.0 (the next biggy) is plodding along slowly but surely.  I've been mucking about with 4th axis a bit, so there will definitely be a bit of that in 1.0.

CamBam might be getting a new physical home too, which will be exciting!  Hopefully more about that soon.

(at least no skeleton cartoons this time!)  :P

Regards

Andy

Offline Bubba

  • CNC Jedi
  • *****
  • Posts: 2984
    • View Profile
Re: CamBam File Merge
« Reply #6 on: June 03, 2014, 18:01:59 pm »
I've been mucking about with 4th axis a bit, so there will definitely be a bit of that in 1.0.
================

Hell YES! ;D

I'm tired of cnc Wrapper.. :(
My 2¢

Win 10 64 bit, CB [1.0} rc 1 64 bit, Mach3, ESS, G540

Offline Dragonfly

  • CNC Jedi
  • *****
  • Posts: 2370
    • View Profile
Re: CamBam File Merge
« Reply #7 on: June 04, 2014, 08:11:30 am »
Glad to hear from you, Andy!

Offline Bubba

  • CNC Jedi
  • *****
  • Posts: 2984
    • View Profile
Re: CamBam File Merge
« Reply #8 on: June 04, 2014, 23:46:49 pm »
Ok, So where is the script. Went to the reference library in the scripts section noticed the "CamBam File Merge"
script. Where the actual file located? How do I go about installing it? Thanks.
My 2¢

Win 10 64 bit, CB [1.0} rc 1 64 bit, Mach3, ESS, G540

Offline lloydsp

  • CNC Jedi
  • *****
  • Posts: 8331
    • View Profile
Re: CamBam File Merge
« Reply #9 on: June 05, 2014, 00:10:51 am »
It's right there ON the screen, Bubba.  Copy/paste it to a text editor, and save it in the CB system/scripts directory with the filename extension of ".py".

Lloyd
"Pyro for Fun and Profit for More Than Fifty Years"

Offline Bubba

  • CNC Jedi
  • *****
  • Posts: 2984
    • View Profile
Re: CamBam File Merge
« Reply #10 on: June 05, 2014, 17:09:55 pm »
It's right there ON the screen,
===============

Oh, OK. Thanks Lloyd.

I noticed it,but I was expecting something like plugin, zipped file of some sort..

Thank again for that.. ;) :D ;D
My 2¢

Win 10 64 bit, CB [1.0} rc 1 64 bit, Mach3, ESS, G540

Offline Jeff_Birt

  • CNC Jedi
  • *****
  • Posts: 821
    • View Profile
Re: CamBam File Merge
« Reply #11 on: April 15, 2015, 13:52:57 pm »
I had an idea to modify this script so that you could merge any support CamBam file types. The script below first opens each file and saves it. If it was not a .cb file you will get prompted via a save dialog and if it is a .cb file you should get the 'OK to overwrite existing file' dialog. The dialogs can't be avoided as CamBam does not have a save method that you can supply a path to.

It opens each file fine and saves them as a CamBam file fine but the merge function no longer works. The file names passed to the merge have the original extension type removed and the .cb added so it should be loading the correct files. If I run the original merge script on the files this script opened and save it merges fine. It seems to me that the script does not like the fact that there are CAD entities already existing. I was wondering if anyone else had an idea of why it will not merge the files?

Code: [Select]
"""

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 = "All (*.*)|*.*"
    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()         
           
newfiles = []
# loop through cambam files
for cbfile in cbfiles:
CamBamUI.MainUI.OpenFile(cbfile,1)
CamBamUI.MainUI.FileSave();
index = len(cbfile) - cbfile.rfind(".")
cbfile = cbfile[:-index]
cbfile = cbfile + ".cb"
print "new path ", cbfile
newfiles.append(cbfile)

CamBamUI.MainUI.FileNew(1, 0, 1)
doc.Layers.Clear() 
doc.Parts.Clear()
print "cleared"

# loop through cambam files
for newfile in newfiles:

    print "Merging ", cbfile

    cb = CADFile()
    cb.Open(newfile)

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

    # loop through layers
    for layer in cb.Layers:
print "layer"
        # 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"


Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 4463
    • View Profile
Re: CamBam File Merge
« Reply #12 on: April 15, 2015, 15:01:21 pm »
Jeff,
Maybe you need a pause to let the files write to disc before trying to read them again ?
Made in England

Offline Jeff_Birt

  • CNC Jedi
  • *****
  • Posts: 821
    • View Profile
Re: CamBam File Merge
« Reply #13 on: April 15, 2015, 15:16:33 pm »
It seems to find and load the files. I put a print statement in the section that loops through the layers and I can tell it is looking through each file. I even tried creating a new file hoping that would clear everything out but that did not work either.

I find scripts a bit frustrating as you can set break points and find out what is really happening.

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6133
    • View Profile
    • Cambam V1.0 French Doc
Re: CamBam File Merge
« Reply #14 on: April 15, 2015, 16:01:41 pm »
Hello,

Quote
I find scripts a bit frustrating as you can set break points and find out what is really happening.

Yes, that need a lot of app.log() or msgbox() to follow what happens ... a step by step and an indication of the line number would be a good thing .... but not sure we can add that with plugin ..

++
David