用于解释 GCODE 的 PERL 脚本

PERL script to interpret GCODE

我一直在研究 PERL 脚本,这是我在 3D 打印论坛上从另一个人那里收到的。

它的作用是找到如下所示的一行: G1 F1800.000 E0.15799 成这样一行: M104 S0

还有这一行: G1 E5.00000 F1800.000 进入这个: M104 S100

脚本应该可以工作,但是,运行无论是通过命令行,还是通过切片 (Slic3r) 软件,都不会产生任何结果.我也可以使用 python 脚本,但是,我根本不知道如何使用 python 编写脚本。

这是脚本:

#!/usr/bin/perl -i.before_postproc
#
# Author  : Guy Poizat
# Version : 1.2
# Copyright : none.
#
# Change from previous version : added handling of slic3r's retraction speed setting,
#  so that this script is not disrupted by a setting change.

use strict;
use warnings;

# what will replace a retraction line with : gcode to cut the laser power off.
my $powerOffLine = "M104 S0\n";

# what will replace an "unretraction" line with : gcode to switch the laser power on.
my $powerOnLine = "M104 S100\n";

# Extract the speed setting from slic3r's environment variables (or use the default 30mm/s).
#  Unit is millimeters per minute in gcode, millimeters per second in slic3r configuration.
my $defaultRetractSpeed = 1800;
my $envRetractSpeed = $ENV{"SLIC3R_RETRACT_SPEED"};
my $retractSpeed = defined( $envRetractSpeed ) ? $envRetractSpeed * 60 : $defaultRetractSpeed;

# pattern of a retraction line - would make the filament back-off before a non printing move
#  on a FDM 3d printer, to avoid ooze.
my $retractLine = "G1 F$retractSpeed.* E";

# pattern of an unretraction line - would make the filament forward back to its position before starting extruding again.
my $unretractLine = "G1 E.* F$retractSpeed";

# read stdin and any/all files passed as parameters one line at a time
while (< > ) {
    if (/$retractLine/) {
        # if we found a retraction line, replace it with laser power off
        print "$powerOffLine";
    }
    else {
        # nothing to change, print line as-is
        print or die $!;
    }

    if (/$unretractLine/) {
        # if we have an un-retraction line, append laser power on right after it
        print "$powerOnLine";
    }
}

该脚本由用于 3D 打印的 gcode 生成器 Slic3r 调用。然后,它更改了整个文件中的几行代码,以使其与我自己构建的激光切割机一起正常工作。

我将提供一个 gcode 示例文件,这样人们就可以看到我正在尝试更改的代码。

实在是查不出是什么问题!应该 运行 就好了。 感谢您的宝贵时间,如果此脚本能够运行,您将帮助很多人。 - 马里努斯

可在此处找到 GCODE 示例:

G21 ; set units to millimeters
M104 S0 (turns off laser)
G21
G28 X0 Y0 Z0
G91
G1 Z+6 F4000 (Sends Z to focal length)
G90
G92 X0 Y0 Z0 E0 (sets logical zero)
M400
M104 S100 (turns on laser)
G90 ; use absolute coordinates
G92 E0
M82 ; use absolute distances for extrusion
M204 S2200
G1 Z0.100 F14400.000
G1 F1800.000 E-5.00000
G92 E0
G1 X25.383 Y62.928 F14400.000
G1 E5.00000 F1800.000
G1 X24.893 Y61.890 E5.00162 F480.000
G1 X24.552 Y60.811 E5.00322
G1 X24.475 Y59.685 E5.00482
G1 X29.714 Y62.560 E5.15186
G1 X27.126 Y62.850 E5.15554
G1 X25.398 Y62.927 E5.15799
G1 F1800.000 E0.15799
G92 E0
G1 X45.931 Y77.187 F14400.000
G1 E5.00000 F1800.000
G1 X45.115 Y81.009 E5.00553 F480.000
G1 X44.514 Y85.098 E5.01138
G1 X36.645 Y81.972 E5.08052
G1 X39.298 Y81.849 E5.08428
G1 X41.096 Y80.860 E5.08718
G1 X45.919 Y77.196 E5.09575
G1 F1800.000 E0.09575
G92 E0
G1 X35.177 Y48.889 F14400.000
G1 E5.00000 F1800.000
G1 X35.280 Y48.648 E5.00037 F480.000
G1 X35.441 Y48.437 E5.00075
G1 X35.714 Y48.389 E5.00114
G1 X38.692 Y48.215 E5.00536
G1 X40.179 Y47.884 E5.00751
G1 X40.964 Y47.413 E5.00881
G1 X41.626 Y46.859 E5.01003

考虑一下 B::Deparse 说的

$ perl -MO=Deparse -le " while(< >){ print } "
BEGIN { $/ = "\n"; $\ = "\n"; }
use File::Glob ();
while (defined($_ = < >)) {
    print $_;
}
-e syntax OK

$ perl -MO=Deparse -le " while(<>){ print } "
BEGIN { $/ = "\n"; $\ = "\n"; }
while (defined($_ = <ARGV>)) {
    print $_;
}
-e syntax OK

<>< >不一样,第一个是<ARGV>,第二个是glob(" ")

这里这一行有什么问题:

# read stdin and any/all files passed as parameters one line at a time
while (< > ) {

因为它没有按照评论所说的那样做。

改为:

while ( <STDIN> ) {

(假设你想从 STDIN 读取 - 如果你想从命令行读取文件名,你需要明确地打开它们)