Author Topic: Fast Open/Convert script  (Read 6323 times)

Offline Jeff_Birt

  • CNC Jedi
  • *****
  • Posts: 821
    • View Profile
Fast Open/Convert script
« on: April 16, 2015, 15:26:03 pm »
When working with Gerber files each layer has its own file and since you can't open a file in CamBam without loosing the existing file you have to open each Gerber file individually which is a pain. I tried to convert Andy's File Merge script to automatically open each file and save it as a CB file and then merge, while it did open each file and prompt you to save them the merge would not work.

What I did was create a separate Fast Open/Convert script that will open all the select files types CamBam supports and prompt you to save the file (CamBam won't let you do a silent save). Then you can use the File Merge script: http://www.cambam.co.uk/forum/index.php?topic=4206.0 to merge them.

You can have both scripts open at the same time and run the fastcon.py first and then run the merge.py and it works fine. I'm not sure why it does not work when both functions are in the same script. Python is one of the most obtuse languages I have ever tried to learn though :)

Code: [Select]
"""

fastconv.py

quickly convert file to .CB

Version: 1.0, [2015-04-16]
Author: Jeff_Birt

"""

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()         
           
# loop through cambam files
for cbfile in cbfiles:
CamBamUI.MainUI.OpenFile(cbfile,1)
CamBamUI.MainUI.FileSave();



Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1345
    • View Profile
Re: Fast Open/Convert script
« Reply #1 on: April 16, 2015, 21:17:57 pm »
HI Jeff
Thanks for contributing, I don't know if It's any help, But you can open more than one instance of CB open
at a time and copy and past the files between them. I do this to merge .dxf files together.
Dave.

Offline Jeff_Birt

  • CNC Jedi
  • *****
  • Posts: 821
    • View Profile
Re: Fast Open/Convert script
« Reply #2 on: April 17, 2015, 12:36:16 pm »
Hi Dave, I do the same thing as you for most things. With Gerber files (for PCB work) you can have 3-5 files and it gets very time consuming and cumbersome to have to open each one separately to combine them. Ideally, a tool like this would recognize when they were Gerber files and what layer they were and apply the correct template for each layer!

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1345
    • View Profile
Re: Fast Open/Convert script
« Reply #3 on: April 17, 2015, 13:25:59 pm »
HI Jeff
I'm working on getting my plugin to recognise when a new fie has been opened or closed, Right now
I need to associate the layers in the plugin with the "new layers" that are present  in the newly opened file.
What's happening now is that even if the new layers (in the newly opened file) have the same name they are given a new id in the cadfile. I've been going through the API looking at Layers and Layers collection classes and methods and have found some methods that are "likely suspects"  and just need to find out which one I need to use. If I find out how to do this this would enable you to run a plugin and then load up one or many
files and copy the layers (in the background without you having to load the files manualy) to the plugin cadfile and then save the pluging cadfile as a cb file and have all the layers merged into one file.
Dave

 

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6133
    • View Profile
    • Cambam V1.0 French Doc
Re: Fast Open/Convert script
« Reply #4 on: April 17, 2015, 14:04:14 pm »
Hello Dave

bool CadFile.HasLayer(string layername) to know if a layer already exists.

Quote
What's happening now is that even if the new layers (in the newly opened file) have the same name they are given a new id in the cadfile.

Layers have no ID unlike "drawing objects", they are identified by their name only.

++
David

« Last Edit: April 17, 2015, 14:12:42 pm by dh42 »

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1345
    • View Profile
Re: Fast Open/Convert script
« Reply #5 on: April 17, 2015, 23:19:01 pm »
HI David
In C# You do this to work with Layers

 // If layers do not exist then create them
 if (file.HasLayer("Unmachined Inside Areas") == false) { newLayer = file.CreateLayer("Unmachined Inside Areas"); }
 // set layer colours to same as button colours (i.e. colour picker colour)
  newLayer.Color = Change_Unmachined_Inside_Layer_button.BackColor;

It's "newLayer" in the piece of code above you use to work with the layer properties.
"newLayer" Has an id and on the first startup of the plugin is given an ID.
However if you then close the file and reload it, the "Unmachined Inside Areas" Layer when loaded up the second time is given a new ID and your "Layer identifier-->  newLayer" no longer points to it.
So what I need to do reassociate the Layer identifier with the layer "Unmachined Inside Areas".
Dave.

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 4463
    • View Profile
Re: Fast Open/Convert script
« Reply #6 on: April 18, 2015, 09:44:26 am »
Dave,

Try this;

Code: [Select]
public class Demo2
    {
        public static ICADView view = CamBamUI.MainUI.ActiveView;
        public CADFile file = view.CADFile;
        Layer newLayer;
 
        public void MyMain()
        {
         if (file.HasLayer("Test_Layer1") == true)
            {
             foreach (Layer exists_layer in file.Layers)
              {
               if (exists_layer.Name == "Test_Layer1")
                  {
                   newLayer = exists_layer;
                   CamBam.ThisApplication.AddLogMessage("instance n of newLayer = " + "Name " + newLayer.Name + " Hash " + newLayer.GetHashCode());
                  }
               }
            }
            else
            {
             newLayer = file.CreateLayer("Test_Layer1");
             CamBam.ThisApplication.AddLogMessage("instance 1 of newLayer = " + "Name " + newLayer.Name + " Hash " + newLayer.GetHashCode());
            }
        }
Made in England

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1345
    • View Profile
Re: Fast Open/Convert script
« Reply #7 on: April 18, 2015, 10:05:20 am »
Thanks Eddy
I'll run the compiler over it in the morning when I get home, I think you have got it, It just looks right!
Thanks again.
Dave.