File size: 9,971 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | # SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * Copyright (c) 2017 sliptonic <shopinthewoods@gmail.com> *
# * *
# * 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 *
# * *
# ***************************************************************************
import os
import socket
import sys
from typing import Any, Dict, Optional
from Path.Post.Processor import PostProcessor
import Path
import FreeCAD
translate = FreeCAD.Qt.translate
DEBUG = False
if DEBUG:
Path.Log.setLevel(Path.Log.Level.DEBUG, Path.Log.thisModule())
Path.Log.trackModule(Path.Log.thisModule())
else:
Path.Log.setLevel(Path.Log.Level.INFO, Path.Log.thisModule())
#
# Define some types that are used throughout this file.
#
Values = Dict[str, Any]
class Smoothie(PostProcessor):
"""
The SmoothieBoard post processor class.
This postprocessor outputs G-code suitable for SmoothieBoard controllers.
It supports direct network upload to the SmoothieBoard via TCP/IP.
"""
def __init__(
self,
job,
tooltip=translate("CAM", "Refactored SmoothieBoard post processor"),
tooltipargs=["ip-addr", "verbose"],
units="Metric",
) -> None:
super().__init__(
job=job,
tooltip=tooltip,
tooltipargs=tooltipargs,
units=units,
)
Path.Log.debug("Refactored SmoothieBoard post processor initialized.")
self.ip_addr: Optional[str] = None
self.verbose: bool = False
def init_values(self, values: Values) -> None:
"""Initialize values that are used throughout the postprocessor."""
#
super().init_values(values)
#
# Set any values here that need to override the default values set
# in the parent routine.
#
# The order of parameters.
# SmoothieBoard doesn't want K properties on XY plane (like LinuxCNC).
#
values["PARAMETER_ORDER"] = [
"X",
"Y",
"Z",
"A",
"B",
"I",
"J",
"F",
"S",
"T",
"Q",
"R",
"L",
]
#
# Used in the argparser code as the "name" of the postprocessor program.
#
values["MACHINE_NAME"] = "SmoothieBoard"
#
# Any commands in this value will be output as the last commands
# in the G-code file.
#
values[
"POSTAMBLE"
] = """M05
G17 G90
M2"""
values["POSTPROCESSOR_FILE_NAME"] = __name__
#
# Any commands in this value will be output after the header and
# safety block at the beginning of the G-code file.
#
values["PREAMBLE"] = """G17 G90"""
def init_arguments(self, values, argument_defaults, arguments_visible):
"""Initialize command-line arguments, including SmoothieBoard-specific options."""
parser = super().init_arguments(values, argument_defaults, arguments_visible)
# Add SmoothieBoard-specific argument group
smoothie_group = parser.add_argument_group("SmoothieBoard-specific arguments")
smoothie_group.add_argument(
"--ip-addr", help="IP address for direct upload to SmoothieBoard (e.g., 192.168.1.100)"
)
smoothie_group.add_argument(
"--verbose",
action="store_true",
help="Enable verbose output for network transfer debugging",
)
return parser
def process_arguments(self):
"""Process arguments and update values, including SmoothieBoard-specific settings."""
flag, args = super().process_arguments()
if flag and args:
# Update SmoothieBoard-specific values from parsed arguments
if hasattr(args, "ip_addr") and args.ip_addr:
self.ip_addr = args.ip_addr
Path.Log.info(f"SmoothieBoard IP address set to: {self.ip_addr}")
if hasattr(args, "verbose"):
self.verbose = args.verbose
if self.verbose:
Path.Log.info("Verbose mode enabled")
return flag, args
def export(self):
"""Override export to handle network upload to SmoothieBoard."""
# First, do the standard export processing
gcode_sections = super().export()
if gcode_sections is None:
return None
# If IP address is specified, send to SmoothieBoard instead of writing to file
if self.ip_addr:
# Combine all G-code sections
gcode = ""
for section_name, section_gcode in gcode_sections:
if section_gcode:
gcode += section_gcode
# Get the output filename from the job
filename = self._job.PostProcessorOutputFile
if not filename or filename == "-":
filename = "output.nc"
self._send_to_smoothie(self.ip_addr, gcode, filename)
# Return the gcode for display/editor
return gcode_sections
# Normal file-based export
return gcode_sections
def _send_to_smoothie(self, ip: str, gcode: str, fname: str) -> None:
"""
Send G-code directly to SmoothieBoard via network.
Args:
ip: IP address of the SmoothieBoard
gcode: G-code string to send
fname: Filename to use on the SmoothieBoard SD card
"""
fname = os.path.basename(fname)
FreeCAD.Console.PrintMessage(f"Sending to SmoothieBoard: {fname}\n")
gcode = gcode.rstrip()
filesize = len(gcode)
try:
# Make connection to SmoothieBoard SFTP server (port 115)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(4.0)
s.connect((ip, 115))
tn = s.makefile(mode="rw")
# Read startup prompt
ln = tn.readline()
if not ln.startswith("+"):
FreeCAD.Console.PrintError(f"Failed to connect with SFTP: {ln}\n")
return
if self.verbose:
print("RSP: " + ln.strip())
# Issue initial store command
tn.write(f"STOR OLD /sd/{fname}\n")
tn.flush()
ln = tn.readline()
if not ln.startswith("+"):
FreeCAD.Console.PrintError(f"Failed to create file: {ln}\n")
return
if self.verbose:
print("RSP: " + ln.strip())
# Send size of file
tn.write(f"SIZE {filesize}\n")
tn.flush()
ln = tn.readline()
if not ln.startswith("+"):
FreeCAD.Console.PrintError(f"Failed: {ln}\n")
return
if self.verbose:
print("RSP: " + ln.strip())
# Now send file
cnt = 0
for line in gcode.splitlines(True):
tn.write(line)
if self.verbose:
cnt += len(line)
print("SND: " + line.strip())
print(f"{cnt}/{filesize}\r", end="")
tn.flush()
ln = tn.readline()
if not ln.startswith("+"):
FreeCAD.Console.PrintError(f"Failed to save file: {ln}\n")
return
if self.verbose:
print("RSP: " + ln.strip())
# Exit
tn.write("DONE\n")
tn.flush()
tn.close()
FreeCAD.Console.PrintMessage("Upload complete\n")
except socket.timeout:
FreeCAD.Console.PrintError(f"Connection timeout while connecting to {ip}:115\n")
except ConnectionRefusedError:
FreeCAD.Console.PrintError(
f"Connection refused by {ip}:115. Is the SmoothieBoard running?\n"
)
except Exception as e:
FreeCAD.Console.PrintError(f"Error sending to SmoothieBoard: {str(e)}\n")
@property
def tooltip(self):
tooltip: str = """
This is a postprocessor file for the CAM workbench.
It is used to take a pseudo-gcode fragment from a CAM object
and output 'real' GCode suitable for a SmoothieBoard controller.
This postprocessor supports direct network upload to SmoothieBoard
via the --ip-addr argument.
"""
return tooltip
|