我一直在处理我在3D打印论坛上从另一个人那里收到的PERL脚本。
它的作用是找到一条如下所示的行: 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";
}
}
脚本由Slic3r调用,Slic3r是用于3D打印的gcode生成器。然后它会在整个文件中更改几行代码,使其与我自己构建的激光切割器一起正常工作。
我将提供一个gcode示例文件,以便可以看到我想要更改的代码。
真的无法找出问题所在!它应该运行得很好。 感谢您的时间,如果这个脚本有效,您将帮助很多人。 - Marinus
可在此处找到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
答案 0 :(得分:1)
考虑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(" ")
答案 1 :(得分:1)
这一行是错误的:
# read stdin and any/all files passed as parameters one line at a time
while (< > ) {
因为它没有做评论所说的那样做。
将其更改为:
while ( <STDIN> ) {
(假设你想从STDIN读取 - 如果你想从命令行读取文件名,你需要明确地打开它们)