Last active
April 4, 2019 17:42
-
-
Save lava/84a58f84f811fbdab063d7cb5ea5148b to your computer and use it in GitHub Desktop.
Postprocessing script to make FreeCAD paths importible by Easel (WIP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# *************************************************************************** | |
# * (c) Benno Evers ([email protected]) 2019 * | |
# * * | |
# * This file is part of the FreeCAD CAx development system. * | |
# * * | |
# * This program is free software; you can redistribute it and/or modify * | |
# * it under the terms of the GNU Lesser General Public License (LGPL) * | |
# * as published by the Free Software Foundation; either version 2 of * | |
# * the License, or (at your option) any later version. * | |
# * for detail see the LICENCE text file. * | |
# * * | |
# * FreeCAD is distributed in the hope that it will be useful, * | |
# * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
# * GNU Lesser General Public License for more details. * | |
# * * | |
# * You should have received a copy of the GNU Library General Public * | |
# * License along with FreeCAD; if not, write to the Free Software * | |
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * | |
# * USA * | |
# * * | |
# ***************************************************************************/ | |
from __future__ import print_function | |
TOOLTIP=''' | |
This is a post-processor for the Easel WebUI which can control various | |
models of CNC mills, i.e. Carvey, X-Carve and ShapeOko 1/2. | |
Note that it has only been tested with the Carvey. | |
Its main task is to linearize any arcs, since Easel only | |
supports linear movements. | |
''' | |
import datetime | |
import FreeCAD | |
import Part | |
import PathScripts.PostUtils as PostUtils | |
def export(objectslist, filename, argstring): | |
"Called when freecad exports a list of objects" | |
# TODO(bevers): Support multiple paths by concatenating them together. | |
if len(objectslist) > 1: | |
print("This script is unable to write more than one Path object") | |
return | |
obj = objectslist[0] | |
if not hasattr(obj, "Path"): | |
print("the given object is not a path") | |
return | |
gcode = postprocess(obj.Path.Commands) | |
# Debugging info | |
dia = PostUtils.GCodeEditorDialog() | |
dia.editor.setText(gcode) | |
result = dia.exec_() | |
gfile = open(filename, "w") | |
gfile.write(gcode) | |
gfile.close() | |
return result | |
def linearize_arc(start, end, center, normal, clockwise): | |
result = "; {} arc from {} to {}\n; around center point {}\n".format( | |
"Clockwise" if clockwise else "Counter-clockwise", | |
start, | |
end, | |
center) | |
radius = (center - start).Length | |
# Center everything at the origin for the following steps | |
start0 = start - center | |
end0 = end - center | |
EPSILON = 1e-7 | |
if (start0 + end0).Length < EPSILON: | |
# 180 degree angle | |
mid0 = normal.cross(start0) | |
elif (start0 - end0).Length < EPSILON: | |
# 360 degree angle | |
mid0 = -start0 | |
else: | |
# "Normal" angle | |
mid0 = radius*(start0 + end0).normalize() | |
# Check if mid-point is clockwise or counter-clockwise from the start. | |
mid_clockwise = normal.dot(start0.cross(mid0)) | |
# If it doesn't match the requested direction, choose the point on | |
# the other side of the circle. | |
if clockwise != mid_clockwise: | |
mid0 = -mid0 | |
mid = center + mid0 | |
arc = Part.ArcOfCircle(start, mid, end) | |
steps = 64 # TODO: make number of steps configurable | |
points = arc.discretize(steps) | |
c = [] | |
for p in points: | |
c.append("G1 X{} Y{} Z{}\n".format(p.x, p.y, p.z)) | |
return result + "".join(c) + "\n" | |
def postprocess(commands): | |
output = "; Generated by FreeCAD Easel post-processor.\n" | |
output += "G21 ; Metric units\n" # FreeCAD internal values are always metric. | |
# Currently supported internal commands: | |
# G0, G1, G2, G3 (Movement) | |
# G17, G18, G19 (Plane selection for G2/G3 arcs) | |
# G81, G82, G83 (Drilling) | |
# G90, G91 (Absolute/relative coordinates) | |
# Carvey starts at (20mm, 20mm, 20mm) after smart-clamp calibration. | |
# TODO: Check if this is different for the other easel-supported machines. | |
# TODO: Turn `state` into a FreeCAD.Vector ? | |
state = {'X': 20., 'Y': 20., 'Z': 20.} | |
normal = FreeCAD.Vector(0, 0, 1) | |
for cmd in commands: | |
if cmd.Name[0] == "(": | |
output += "; " + cmd.Name + "\n" | |
elif cmd.Name == "G0" or cmd.Name == "G1": | |
argnames = cmd.Parameters.keys() | |
if 'A' in argnames or 'B' in argnames or 'C' in argnames: | |
raise RuntimeError("A,B,C rotational axes currently not supported.") | |
if 'X' in argnames: | |
state['X'] = float(cmd.Parameters['X']) | |
if 'Y' in argnames: | |
state['Y'] = float(cmd.Parameters['Y']) | |
if 'Z' in argnames: | |
state['Z'] = float(cmd.Parameters['Z']) | |
# We could make generated file sizes slightly smaller by omitting | |
# unchanged coordinates, but so far we haven't hit a size limit in | |
# Easel. | |
output += "{} X{} Y{} Z{}\n".format(cmd.Name, state['X'], state['Y'], state['Z']) | |
elif cmd.Name == "G2" or cmd.Name == "G3": | |
# We are guaranteed that the arc described by these commands always | |
# lies in one of the three coordinate planes, and that the correct | |
# plane has already been set through the G17/G18/G19 commands. | |
# For arcs that are not aligned to a plane, FreeCAD will linearize | |
# them internally and generate a sequence of G1 movements itself. | |
argnames = cmd.Parameters.keys() | |
if 'A' in argnames or 'B' in argnames or 'C' in argnames: | |
raise RuntimeError("A,B,C rotational axes currently not supported.") | |
# Update final position | |
start = FreeCAD.Vector(state['X'], state['Y'], state['Z']) | |
end = FreeCAD.Vector(state['X'], state['Y'], state['Z']) | |
center = FreeCAD.Vector(state['X'], state['Y'], state['Z']) | |
if 'X' in argnames: | |
end.x = float(cmd.Parameters['X']) | |
if 'Y' in argnames: | |
end.y = float(cmd.Parameters['Y']) | |
if 'Z' in argnames: | |
end.z = float(cmd.Parameters['Z']) | |
if 'I' in argnames: | |
center.x += float(cmd.Parameters['I']) | |
if 'J' in argnames: | |
center.y += float(cmd.Parameters['J']) | |
if 'K' in argnames: | |
center.z += float(cmd.Parameters['K']) | |
clockwise = cmd.Name == "G2" | |
output += linearize_arc(start, end, center, normal, clockwise) | |
state = end_position | |
elif cmd.Name == "G81" or cmd.Name == "G82" or cmd.Name == "G83": | |
raise RuntimeError("Drilling commands (G8x) currently not supported.") | |
elif cmd.Name == "G90": | |
output += "G90 ; Absolute coordinate mode\n" | |
elif cmd.Name == "G91": | |
raise RuntimeError("Relative coordinate mode (G91) currently not supported.") | |
elif cmd.Name == "G17": # Use the xy-plane for arc movements. | |
normal = FreeCAD.Vector(0, 0, 1) | |
elif cmd.Name == "G18": # Use the zx-plane for arc movements. | |
normal = FreeCAD.Vector(0, 1, 0) | |
elif cmd.Name == "G19": # Use the yz-plane for arc movements. | |
normal = FreeCAD.Vector(1, 0, 0) | |
else: | |
# TODO: Maybe just output unknown commands unchanged? | |
raise RuntimeError("Unexpected internal G-Code command {}".format(cmd.Name)) | |
return output | |
print(__name__ + " gcode postprocessor loaded.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment