Getting started with jython scripting is very easy as the complete scripting environment is bundled with BoardCAD and no extra installation is needed.
To get started simply open BoardCAD and click on Jython console tab in the bottom.
from javax.swing import * JOptionPane.showMessageDialog(None, "Hello world")
This will bring up a dialog window with the text Hello world. While this is very cool it doesn't really have anything to do with BoardCAD. To interact with BoardCAD, we first need to import the BoardCAD GUI in order to get access to our current board models. In the following example we import the BoardCAD GUI and read the length of the current board.
import boardcad.gui.jdk.BoardCAD from javax.swing import * brd=boardcad.gui.jdk.BoardCAD.getInstance().getCurrentBrd() JOptionPane.showMessageDialog(None, "Board length = %f " % brd.getLength())
Writing commands directly in the jython console is great for testing, but for normal purposes it is more practical to save the commands in a file and call it something like myscript.py. You can then run the commands by choosing Scripts - Run script in the menu and open myscript.py.
Now, it would be even more cool if you could get a specific menu option for your new script, instead of having to browse for the file each time you want to run your script. Luckily, you can. The following script adds the menu option Show length under the Scripts menu.
import boardcad.gui.jdk.BoardCAD from javax.swing import * import java.awt.event.KeyEvent as KeyEvent def runMyScript(event): execfile('/Users/hornstein/Desktop/myscript.py') jythonmenu=boardcad.gui.jdk.BoardCAD.getInstance().scriptMenu mitem=JMenuItem('Show length', actionPerformed=runMyScript) jythonmenu.add(mitem) ~~~~~ This is great. Now you only have to run the script above each time that BoardCAD starts and you'll have your new great functionality ready directly from the BoardCAD menu. Could it be any better? Well, maybe if we didn't have to run any script manually at all. Fortunately the BoardCAD team has thought about this and actually added a special script that is executed automatically every time that BoardCAD starts. The script is called boardcad_init.py and is located at root directory of your BoardCAD installation. In Windows this is typically "c:\Program Files (x86)\BoardCAD" and in Mac OS "/Applications/BoardCAD.app/Contents/java/app". Below you can see the default content of that file:
import boardcad.gui.jdk.BoardCAD
import boardcam.BoardMachine
from javax.swing import JScrollPane
from java.awt import *
from javax.swing import *
from java.io import *
import java.awt.event.KeyEvent as KeyEvent
import console
def gcodeDeck(event):
fc = JFileChooser()
fc.setCurrentDirectory(File(boardcad.gui.jdk.BoardCAD.getInstance().defaultDirectory))
returnVal = fc.showSaveDialog(boardcad.gui.jdk.BoardCAD.getInstance().getFrame())
if (returnVal != JFileChooser.APPROVE_OPTION):
return
mfile = fc.getSelectedFile()
filename = mfile.getPath()
if(filename == None):
return
boardcad.gui.jdk.BoardCAD.getInstance().defaultDirectory = mfile.getPath()
boardcam.BoardMachine.deckFileName=filename
boardcam.BoardMachine.read_machine_data()
filename=boardcam.BoardMachine.deckScript
myconsole.insertText("execfile('" + filename + "')")
myconsole.enter()
board_draw=boardcad.gui.jdk.BoardCAD.getInstance().getBoardHandler().board_draw
board_draw.deck_cut=boardcam.BoardMachine.deck_cut;
board_draw.nr_of_cuts_deck=boardcam.BoardMachine.nr_of_cuts_deck;
board_draw.deck_collision=boardcam.BoardMachine.deck_collision
def gcodeBottom(event):
fc = JFileChooser()
fc.setCurrentDirectory(File(boardcad.gui.jdk.BoardCAD.getInstance().defaultDirectory))
returnVal = fc.showSaveDialog(boardcad.gui.jdk.BoardCAD.getInstance().getFrame())
if (returnVal != JFileChooser.APPROVE_OPTION):
return
mfile = fc.getSelectedFile()
filename = mfile.getPath()
if(filename == None):
return
boardcad.gui.jdk.BoardCAD.getInstance().defaultDirectory = mfile.getPath()
boardcam.BoardMachine.bottomFileName=filename
boardcam.BoardMachine.read_machine_data()
filename=boardcam.BoardMachine.bottomScript
myconsole.insertText("execfile('" + filename + "')")
myconsole.enter()
board_draw=boardcad.gui.jdk.BoardCAD.getInstance().getBoardHandler().board_draw
board_draw.bottom_cut=boardcam.BoardMachine.bottom_cut;
board_draw.nr_of_cuts_bottom=boardcam.BoardMachine.nr_of_cuts_bottom;
board_draw.bottom_collision=boardcam.BoardMachine.bottom_collision
def runScript(event):
fc = JFileChooser()
fc.setCurrentDirectory(File(boardcad.gui.jdk.BoardCAD.getInstance().defaultDirectory))
returnVal = fc.showOpenDialog(boardcad.gui.jdk.BoardCAD.getInstance().getFrame())
if (returnVal != JFileChooser.APPROVE_OPTION):
return
mfile = fc.getSelectedFile()
filename = mfile.getPath()
myconsole.insertText("execfile('" + filename + "')")
myconsole.enter()
frame=boardcad.gui.jdk.BoardCAD.getInstance().getFrame()
mytab=boardcad.gui.jdk.BoardCAD.getInstance().mTabbedPane2
myconsole = console.Console()
mytab.addTab("Jython console", JScrollPane(myconsole.text_pane))
mbar=frame.getJMenuBar()
jythonmenu=boardcad.gui.jdk.BoardCAD.getInstance().scriptMenu
mitem=JMenuItem('G-code deck', actionPerformed=gcodeDeck)
jythonmenu.add(mitem)
mitem=JMenuItem('G-code bottom', actionPerformed=gcodeBottom)
jythonmenu.add(mitem)
jythonmenu.addSeparator()
mitem=JMenuItem('Run script', actionPerformed=runScript)
jythonmenu.add(mitem)
mbar.revalidate()
~~~~
As you can see, it is also this script that adds the gcode scripts to the Script menu. The gcode scripts are also located at the root directory of your BoardCAD installation and having a look at those is a good way to get a better understanding of how BoardCAD scripting works.