28 November 2012 MOP Automate VB and Python scripts

COMMENTS

This script will automate the process of opening a source file, insert machining operations, set various properties and produce g-code.

These scripts demonstrate:

  • opening a CAD file (.dxf in this example)
  • drawing some extra shapes into the drawing
  • setting machining options
  • Call asynchronous CAD functions such as
    Edit -> Join and Edit -> Convert to polyline
  • creating a machining operation from objects in a drawing layer
  • setting machining operation properties
  • saving a drawing and creating g-code

Python version...
VBScript version...

Python Script:

#
#	mop-automate.py
#
from CamBam.CAM import *
from CamBam.UI import CamBamUI
from CamBam.Values import *

# TO CHANGE...

source_file = "C:\\CamBam\\cbfiles\\aluskids.dxf"
out_filename = "C:\\dump\\test.cb"

# Open a source file
# note: the second parameter '1' opens the file synchronously rather than in a worker thread
CamBamUI.MainUI.OpenFile(source_file,1)

# The 'doc' global variable is always the CADFile before any file opens,
# so we create a new varialbe pointing to the newly opened active file...
newdoc = CamBamUI.MainUI.ActiveView.CADFile

# draw some shapes...
poly=Polyline()
poly.Add(8,5,0)
poly.Add(22,5,0)
poly.Add(25,15,0)
poly.Add(15,25,0)
poly.Add(5,15,0)
poly.Closed=1
newdoc.Add(poly)

circle=Circle()
circle.Center=Point3F(-30,15,0)
circle.Diameter=15
newdoc.Add(circle)

# set any machining options required...
newdoc.MachiningOptions.PostProcessor = "Mach3-CutViewer"
newdoc.MachiningOptions.FastPlungeHeight = 0.1

# Select All...
CamBamUI.MainUI.ActiveView.SelectAllVisibleGeometry()

# Convert to polylines...
PolylineUtils.PolyLinesConvert(CamBamUI.MainUI.ActiveView)
# The gui operation runs in a worker process, so wait for the
# thinking message to disapear...
while CamBamUI.MainUI.ActiveView.CurrentEditMode is not None:
	app.Sleep(1)

# Edit - Join
PolylineUtils.JoinPolyLines(CamBamUI.MainUI.ActiveView,0.001)
# wait for the thinking message to disapear...
while CamBamUI.MainUI.ActiveView.CurrentEditMode is not None:
	app.Sleep(1)

# create a profile mop
# Use all the drawing objects from the first layer...
profile=MOPProfile(newdoc,newdoc.Layers[0].Entities.ToArray())
# Use all the drawing objects from the last layer...
# profile=MOPProfile(newdoc,newdoc.Layers[newdoc.Layers.Count-1].Entities.ToArray())
# Use all the drawing objects from a layer with a specific name...
# profile=MOPProfile(newdoc,newdoc.Layers["LAYER_03"].Entities.ToArray())

# Set the profile style property...
profile.Style="cutout"

# Other properties are based on the generic CBValue class
# Some examples of how these properties should be set...
profile.ToolDiameter = CBValue[float](0.8)
profile.InsideOutside = CBValue[InsideOutsideOptions](InsideOutsideOptions.Inside)

# add the machine op to the drawing...
CamBamUI.MainUI.InsertMOP(profile)

# save the current drawing...
newdoc.Save(out_filename)

# create g-code output
CAMUtils.GenerateGCodeOutput(view)

VBScript:

'
'	mop-automate.vbs
'
sub main

	' TO CHANGE...
	dim source_file = "C:\CamBam\cbfiles\aluskids.dxf"
	dim out_filename = "C:\dump\test.cb"

	' Open a source file
	' note: the second parameter '1' opens the file synchronously rather than in a worker thread
	CamBamUI.MainUI.OpenFile(source_file,1)

	' The 'doc' global variable is always the CADFile before any file opens,
	' so we create a new varialbe pointing to the newly opened active file...
	dim newdoc = CamBamUI.MainUI.ActiveView.CADFile
	
	' draw some shapes...
	dim poly = new Polyline()
	poly.Add(8,5,0)
	poly.Add(22,5,0)
	poly.Add(25,15,0)
	poly.Add(15,25,0)
	poly.Add(5,15,0)
	poly.Closed=True
	newdoc.Add(poly)
	
	dim circle = new Circle()
	circle.Center = new Point3F(-30,15,0)
	circle.Diameter=15
	newdoc.Add(circle)

	' set any machining options required...
	newdoc.MachiningOptions.PostProcessor = "Mach3-CutViewer"
	newdoc.MachiningOptions.FastPlungeHeight = 0.1

	' Select All...
	CamBamUI.MainUI.ActiveView.SelectAllVisibleGeometry()
	
	' Convert to polylines...
	PolylineUtils.PolyLinesConvert(CamBamUI.MainUI.ActiveView)
	' The gui operation runs in a worker process, so wait for the
	' thinking message to disapear...
	while not CamBamUI.MainUI.ActiveView.CurrentEditMode is nothing 
		app.Sleep(1)
	end while
	
	' Edit - Join
	PolylineUtils.JoinPolyLines(CamBamUI.MainUI.ActiveView,0.001)
	' wait for the thinking message to disapear...
	while not CamBamUI.MainUI.ActiveView.CurrentEditMode is nothing
		app.Sleep(1)
	end while

	' create a profile mop
	' Use all the drawing objects from the first layer...
	' dim profile = new CamBam.CAM.MOPProfile(newdoc,newdoc.Layers(0).Entities.ToArray())
	' Use all the drawing objects from the last layer...
	' dim profile = new CamBam.CAM.MOPProfile(newdoc,newdoc.Layers(newdoc.Layers.Count-1).Entities.ToArray())
	' Use all the drawing objects from a layer with a specific name...
	dim profile = new CamBam.CAM.MOPProfile(newdoc,newdoc.Layers("LAYER_03").Entities.ToArray())

	' Set the profile style property...
	profile.Style = "cutout"

	' Other properties are based on the generic CBValue class
	' Some examples of how these properties should be set...
	' Note: CBValue(of type) syntax not working in current release 
	' as extra .dlls need to be referenced in the VB script host.
	' user alternative syntax...

	dim td = profile.ToolDiameter
	td.SetValue(0.8)
	profile.ToolDiameter = td

	dim io = profile.InsideOutside
	io.SetValue(CamBam.CAM.InsideOutsideOptions.Inside)
	profile.InsideOutside = io

	' add the machine op to the drawing...
	CamBamUI.MainUI.InsertMOP(profile)

	' save the current drawing...
	newdoc.Save(out_filename)

	' create g-code output
	CamBam.CAM.CAMUtils.GenerateGCodeOutput(view)

end sub

Comments

<< 1 >> 
#  10Bulls
25/02/2013 15:08:42
First Post!
Reply Quote   9  
#  Doug 25/02/2013 15:57:15
Dang, you are always first!!!
Reply Quote   7  
#  Dean Elliott 02/12/2015 21:33:46
Just bought CamBam Plus. Still waiting on license. Very new to this. How do I utilize this script?
Reply Quote   7  
#  anon 03/12/2015 09:21:33
From the Script menu, select New, then VBScript or Python Script.
Paste the script text from above into the editor and change any variables.
With the cursor anywhere in the script text, press F5 to run the script.
Watch for error and diagnostic messages at the bottom of the main CamBam window.
Reply Quote   7  
#  Naelyan 11/05/2016 20:46:58
Is there a way to generate GCode only for the MOP that was just created by a similar script ?
CamBam.CAM.CAMUtils.GenerateGCodeOutput(view) will generate a GCode containing all the MOPs for all active parts (if I am right). Practically, I am looking for a script instruction equivalent to "right-click on a MOP and select Produce GCode".
Can any one help ?
Reply Quote   9  

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