1

这是我的第一篇文章,所以我希望我设置正确。

基本情况:我正在尝试使用后处理 Perl 脚本来更改我的自定义粘贴 3D 打印机的 G 代码行。为防止糊剂渗出,我通过手动更改 G 代码以在一层完成后缩回取得了不错的成功,回到原位并等待,然后继续下一层,然后挤出。然而,对于更大的 G 代码,这将是一场噩梦……

我正在努力克服 3 个主要障碍。

  1. 我如何“查找/索引到最接近的 E##(从 Z),读取值,减去回缩值,然后打印该新值?”
  2. 如何确保我没有阅读以注释/(;)开头的行并意外更改它们?
  3. 我不确定这是否是实现上述内容的最合乎逻辑的方法,或者是否还有其他用于粘贴挤出的切片机?这里的任何信息都会有所帮助!

到目前为止,我真正拥有的后处理脚本是这样的:

use strict;
use warnings;

# Go through the input file, line by line
while ( <> ) {
    # Print the existing line to the output
    print;
    if (my $match = m/Z(\d+\.?\d*)/) {
        printf ";There is a Z"
        
        #now go up a couple lines to find E#?
    }
    
}

输出是:

; generated by Slic3r 1.3.0 on 2021-03-14 at 12:34:30

; external perimeters extrusion width = 0.66mm (0.62mm^3/s)
; perimeters extrusion width = 0.84mm (0.80mm^3/s)
; infill extrusion width = 0.75mm (0.71mm^3/s)
; solid infill extrusion width = 0.84mm (0.80mm^3/s)
; top infill extrusion width = 0.84mm (0.80mm^3/s)

M104 S180 ; set temperature
G28 ; home all axes
G1 Z5 F5000 ; lift nozzle
There is a Z
; Filament gcode

M109 S180 ; set temperature and wait for it to be reached
G21 ; set units to millimeters
G90 ; use absolute coordinates
M82 ; use absolute distances for extrusion
G92 E0
;-----------------
;Before Layer - - Add Extrusion below

G28 X0 ; home X axis
G28 Y0 ; home X axis
G4 P10000 ; Delay for miliseconds
;-----------------
G1 Z0.200 F1800.000
There is a Z;-----------------
;After Layer - - Add Extrusion below

;-----------------
G1 E-2.00000 F2400.00000
G92 E0
G1 X90.681 Y91.354 F1800.000
G1 E2.00000 F2400.00000
G1 F300
G1 X92.423 Y89.937 E2.05106
G1 X95.500 Y89.143 E2.12329
G1 X104.500 Y89.143 E2.32788
G1 X106.711 Y89.540 E2.37895
G1 X108.646 Y90.681 E2.43001
G1 X110.063 Y92.423 E2.48107
G1 X110.857 Y95.500 E2.55330
G1 X110.857 Y104.500 E2.75789
G1 X110.460 Y106.711 E2.80895
G1 X109.319 Y108.646 E2.86002
G1 X107.577 Y110.063 E2.91108
G1 X104.500 Y110.857 E2.98331
G1 X95.500 Y110.857 E3.18790
G1 X93.289 Y110.460 E3.23896
G1 X91.354 Y109.319 E3.29002
G1 X89.937 Y107.577 E3.34109
G1 X89.143 Y104.500 E3.41331
G1 X89.143 Y95.500 E3.61791
G1 X89.540 Y93.289 E3.66897
G1 X90.635 Y91.432 E3.71799
G1 E1.71799 F2400.00000
G92 E0
G1 X103.586 Y103.586 F1800.000
G1 E2.00000 F2400.00000
G1 F300
G1 X96.414 Y103.586 E2.16303
G1 X96.414 Y96.414 E2.32606
G1 X103.586 Y96.414 E2.48909
G1 X103.586 Y103.496 E2.65007
G1 X103.943 Y103.943 F1800.000
G1 F300
G1 X96.057 Y103.943 E2.82934
G1 X96.057 Y96.057 E3.00860
G1 X103.943 Y96.057 E3.18786
G1 X103.943 Y103.853 E3.36508
G1 X104.300 Y104.300 F1800.000
G1 F300
G1 X95.700 Y104.300 E3.56058
G1 X95.700 Y95.700 E3.75608
G1 X104.300 Y95.700 E3.95158
G1 X104.300 Y104.210 E4.14503
G1 X103.720 Y104.145 F1800.000
G1 X103.090 Y103.368 F1800.000
G1 F300
G1 X103.368 Y103.090 E4.15417
G1 X103.368 Y102.573 E4.16618
G1 X102.573 Y103.368 E4.19231
G1 X102.057 Y103.368 E4.20433
G1 X103.368 Y102.057 E4.24744
G1 X103.368 Y101.540 E4.25946
G1 X101.540 Y103.368 E4.31956
G1 X101.023 Y103.368 E4.33158
G1 X103.368 Y101.023 E4.40867
G1 X103.368 Y100.507 E4.42069
G1 X100.507 Y103.368 E4.51477
G1 X99.990 Y103.368 E4.52679
G1 X103.368 Y99.990 E4.63786
G1 X103.368 Y99.474 E4.64987
G1 X99.474 Y103.368 E4.77794
G1 X98.957 Y103.368 E4.78995
G1 X103.368 Y98.957 E4.93501
G1 X103.368 Y98.440 E4.94702
G1 X98.440 Y103.368 E5.10906
G1 X97.924 Y103.368 E5.12108
G1 X103.368 Y97.924 E5.30011
G1 X103.368 Y97.407 E5.31212
G1 X97.407 Y103.368 E5.50815
G1 X96.891 Y103.368 E5.52016
G1 X103.368 Y96.891 E5.73317
G1 X103.368 Y96.632 E5.73918
G1 X103.109 Y96.632 E5.74518
G1 X96.632 Y103.109 E5.95820
G1 X96.632 Y102.593 E5.97021
G1 X102.593 Y96.632 E6.16623
G1 X102.076 Y96.632 E6.17825
G1 X96.632 Y102.076 E6.35728
G1 X96.632 Y101.560 E6.36929
G1 X101.560 Y96.632 E6.53134
G1 X101.043 Y96.632 E6.54335
G1 X96.632 Y101.043 E6.68840
G1 X96.632 Y100.526 E6.70042
G1 X100.526 Y96.632 E6.82848
G1 X100.010 Y96.632 E6.84050
G1 X96.632 Y100.010 E6.95157
G1 X96.632 Y99.493 E6.96358
G1 X99.493 Y96.632 E7.05767
G1 X98.977 Y96.632 E7.06968
G1 X96.632 Y98.977 E7.14678
G1 X96.632 Y98.460 E7.15879
G1 X98.460 Y96.632 E7.21890
G1 X97.943 Y96.632 E7.23091
G1 X96.632 Y97.943 E7.27403
G1 X96.632 Y97.427 E7.28605
G1 X97.427 Y96.632 E7.31217
G1 X96.910 Y96.632 E7.32419
G1 X96.632 Y96.910 E7.33332
;-----------------
;Before Layer - - Add Extrusion below

G28 X0 ; home X axis
G28 Y0 ; home X axis
G4 P10000 ; Delay for miliseconds
;-----------------
G1 Z0.400 F1800.000
There is a Z;-----------------
;After Layer - - Add Extrusion below

;-----------------
G1 X102.666 Y102.666 F1800.000
G1 F300
G1 X97.334 Y102.666 E7.60387
G1 X97.334 Y97.334 E7.87442
G1 X102.666 Y97.334 E8.14497
G1 X102.666 Y102.576 E8.41095
G1 X103.463 Y103.463 F1800.000
G1 F300
G1 X96.537 Y103.463 E8.76239
G1 X96.537 Y96.537 E9.11384
G1 X103.463 Y96.537 E9.46528
G1 X103.463 Y103.373 E9.81216
G1 X104.170 Y104.170 F1800.000
G1 F300
G1 X95.830 Y104.170 E10.13979
G1 X95.830 Y95.830 E10.46742
G1 X104.170 Y95.830 E10.79505
G1 X104.170 Y104.080 E11.11915
G1 X103.590 Y104.015 F1800.000
G1 X102.179 Y98.441 F1800.000
G1 F300
G1 X101.559 Y97.821 E11.16833
G1 X100.313 Y97.821 E11.23820
G1 X102.179 Y99.687 E11.38619
G1 X102.179 Y100.933 E11.45605
G1 X99.067 Y97.821 E11.70285
G1 X97.821 Y97.821 E11.77272
G1 X102.179 Y102.179 E12.11832
G1 X100.933 Y102.179 E12.18818
G1 X97.821 Y99.067 E12.43498
G1 X97.821 Y100.313 E12.50484
G1 X99.687 Y102.179 E12.65283
G1 X98.441 Y102.179 E12.72270
G1 X97.821 Y101.559 E12.77188
;-----------------
;Before Layer - - Add Extrusion below

G28 X0 ; home X axis
G28 Y0 ; home X axis
G4 P10000 ; Delay for miliseconds
;-----------------
G1 Z0.600 F1800.000
There is a Z;-----------------
;After Layer - - Add Extrusion below

;-----------------
G1 X102.666 Y102.666 F1800.000
G1 F300
G1 X97.334 Y102.666 E13.04243
G1 X97.334 Y97.334 E13.31298
G1 X102.666 Y97.334 E13.58353
G1 X102.666 Y102.576 E13.84951
G1 X103.463 Y103.463 F1800.000
G1 F300
G1 X96.537 Y103.463 E14.20095
G1 X96.537 Y96.537 E14.55240
G1 X103.463 Y96.537 E14.90384
G1 X103.463 Y103.373 E15.25071
G1 X104.170 Y104.170 F1800.000
G1 F300
G1 X95.830 Y104.170 E15.57835
G1 X95.830 Y95.830 E15.90598
G1 X104.170 Y95.830 E16.23361
G1 X104.170 Y104.080 E16.55771
G1 X103.590 Y104.015 F1800.000
G1 X101.559 Y102.179 F1800.000
G1 F300
G1 X102.179 Y101.559 E16.60689
G1 X102.179 Y100.313 E16.67676
G1 X100.313 Y102.179 E16.82475
G1 X99.067 Y102.179 E16.89461
G1 X102.179 Y99.067 E17.14141
G1 X102.179 Y97.821 E17.21127
G1 X97.821 Y102.179 E17.55687
G1 X97.821 Y100.933 E17.62674
G1 X100.933 Y97.821 E17.87353
G1 X99.687 Y97.821 E17.94340
G1 X97.821 Y99.687 E18.09139
G1 X97.821 Y98.441 E18.16125
G1 X98.441 Y97.821 E18.21044
;-----------------
;Before Layer - - Add Extrusion below

G28 X0 ; home X axis
G28 Y0 ; home X axis
G4 P10000 ; Delay for miliseconds
;-----------------
G1 Z0.800 F1800.000
There is a Z;-----------------
;After Layer - - Add Extrusion below

;-----------------
G1 X102.666 Y102.666 F1800.000
G1 F300
G1 X97.334 Y102.666 E18.48099
G1 X97.334 Y97.334 E18.75154
G1 X102.666 Y97.334 E19.02208
G1 X102.666 Y102.576 E19.28807
G1 X103.463 Y103.463 F1800.000
G1 F300
G1 X96.537 Y103.463 E19.63951
G1 X96.537 Y96.537 E19.99095
G1 X103.463 Y96.537 E20.34239
G1 X103.463 Y103.373 E20.68927
G1 X104.170 Y104.170 F1800.000
G1 F300
G1 X95.830 Y104.170 E21.01690
G1 X95.830 Y95.830 E21.34454
G1 X104.170 Y95.830 E21.67217
G1 X104.170 Y104.080 E21.99627
G1 X103.590 Y104.015 F1800.000
G1 X102.179 Y98.441 F1800.000
G1 F300
G1 X101.559 Y97.821 E22.04545
G1 X100.313 Y97.821 E22.11532
G1 X102.179 Y99.687 E22.26330
G1 X102.179 Y100.933 E22.33317
G1 X99.067 Y97.821 E22.57996
G1 X97.821 Y97.821 E22.64983
G1 X102.179 Y102.179 E22.99543
G1 X100.933 Y102.179 E23.06530
G1 X97.821 Y99.067 E23.31209
G1 X97.821 Y100.313 E23.38196
G1 X99.687 Y102.179 E23.52995
G1 X98.441 Y102.179 E23.59981
G1 X97.821 Y101.559 E23.64899
;-----------------
;Before Layer - - Add Extrusion below

G28 X0 ; home X axis
G28 Y0 ; home X axis
G4 P10000 ; Delay for miliseconds
;-----------------
G1 Z1.000 F1800.000
There is a Z;-----------------
;After Layer - - Add Extrusion below

;-----------------
G1 E21.64899 F2400.00000
G92 E0
G1 X102.666 Y102.666 F1800.000
G1 E2.00000 F2400.00000
G1 F300
G1 X97.334 Y102.666 E2.27055
G1 X97.334 Y97.334 E2.54110
G1 X102.666 Y97.334 E2.81165
G1 X102.666 Y102.576 E3.07763
G1 X103.463 Y103.463 F1800.000
G1 F300
G1 X96.537 Y103.463 E3.42907
G1 X96.537 Y96.537 E3.78052
G1 X103.463 Y96.537 E4.13196
G1 X103.463 Y103.373 E4.47883
G1 X104.170 Y104.170 F1800.000
G1 F300
G1 X95.830 Y104.170 E4.80647
G1 X95.830 Y95.830 E5.13410
G1 X104.170 Y95.830 E5.46173
G1 X104.170 Y104.080 E5.78583
G1 X103.590 Y104.015 F1800.000
G1 E3.78583 F2400.00000
G92 E0
G1 X101.559 Y102.179 F1800.000
G1 E2.00000 F2400.00000
G1 F300
G1 X102.179 Y101.559 E2.04918
G1 X102.179 Y100.313 E2.11905
G1 X100.313 Y102.179 E2.26704
G1 X99.067 Y102.179 E2.33690
G1 X102.179 Y99.067 E2.58370
G1 X102.179 Y97.821 E2.65356
G1 X97.821 Y102.179 E2.99916
G1 X97.821 Y100.933 E3.06903
G1 X100.933 Y97.821 E3.31582
G1 X99.687 Y97.821 E3.38569
G1 X97.821 Y99.687 E3.53368
G1 X97.821 Y98.441 E3.60355
G1 X98.441 Y97.821 E3.65273
G1 E1.65273 F2400.00000
G92 E0
; Filament-specific end gcode 

;END gcode for filament

M104 S0 ; turn off temperature
G28 X0  ; home X axis
M84     ; disable motors

M140 S0 ; set bed temperature
; filament used = 28.8mm (0.1cm3)
; total filament cost = 0.0

; avoid_crossing_perimeters = 0
; bed_shape = 0x0,200x0,200x200,0x200
; bed_temperature = 0
; before_layer_gcode = ;-----------------\n;Before Layer - - Add Extrusion below\n\nG28 X0 ; home X axis\nG28 Y0 ; home X axis\nG4 P10000 ; Delay for miliseconds\n;-----------------
; between_objects_gcode = 
; bridge_acceleration = 0
; bridge_fan_speed = 100
; brim_connections_width = 0
; brim_width = 0
; complete_objects = 0
; cooling = 0
; default_acceleration = 0
; disable_fan_first_layers = 3
; duplicate_distance = 6
; end_filament_gcode = "; Filament-specific end gcode \n\n;END gcode for filament\n"
; end_gcode = M104 S0 ; turn off temperature\nG28 X0  ; home X axis\nM84     ; disable motors\n
; extruder_clearance_height = 20
; extruder_clearance_radius = 20
; extruder_offset = 0x0
; extrusion_axis = E
; extrusion_multiplier = 1
; fan_always_on = 0
; fan_below_layer_time = 60
; filament_colour = #FFFFFF
; filament_cost = 0
; filament_density = 0
; filament_diameter = 2
; filament_max_volumetric_speed = 0
; filament_notes = ""
; first_layer_acceleration = 0
; first_layer_bed_temperature = 0
; first_layer_extrusion_width = 200%
; first_layer_speed = 5
; first_layer_temperature = 180
; gcode_arcs = 0
; gcode_comments = 0
; gcode_flavor = reprap
; has_heatbed = 1
; infill_acceleration = 0
; infill_first = 0
; interior_brim_width = 0
; layer_gcode = ;-----------------\n;After Layer - - Add Extrusion below\n\n;-----------------
; max_fan_speed = 100
; max_layer_height = 0.3
; max_print_speed = 80
; max_volumetric_speed = 0
; min_fan_speed = 35
; min_layer_height = 0.15
; min_print_speed = 10
; min_skirt_length = 0
; notes = 
; nozzle_diameter = 0.6
; only_retract_when_crossing_perimeters = 1
; ooze_prevention = 0
; output_filename_format = [input_filename_base].gcode
; perimeter_acceleration = 0
; post_process = "C:\\Users\\matth\\Desktop\\Slic3r-1.3.0.64bit\\script\\MassLayerZ1.pl"
There is a Z; pressure_advance = 0
; printer_notes = 
; resolution = 0
; retract_before_travel = 2
; retract_layer_change = 0
; retract_length = 2
; retract_length_toolchange = 10
; retract_lift = 0
; retract_lift_above = 0
; retract_lift_below = 0
; retract_restart_extra = 0
; retract_restart_extra_toolchange = 0
; retract_speed = 40
; skirt_distance = 6
; skirt_height = 1
; skirts = 1
; slowdown_below_layer_time = 5
; spiral_vase = 0
; standby_temperature_delta = -5
; start_filament_gcode = "; Filament gcode\n"
; start_gcode = G28 ; home all axes\nG1 Z5 F5000 ; lift nozzle\n
There is a Z; temperature = 180
; threads = 4
; toolchange_gcode = 
; travel_speed = 30
; use_firmware_retraction = 0
; use_relative_e_distances = 0
; use_set_and_wait_bed = 0
; use_set_and_wait_extruder = 0
; use_volumetric_e = 0
; vibration_limit = 0
; wipe = 0
; z_offset = 0
; z_steps_per_mm = 0
; adaptive_slicing = 0
; adaptive_slicing_quality = 75%
; dont_support_bridges = 1
; extrusion_width = 0
; first_layer_height = 0.2
; infill_only_where_needed = 0
; interface_shells = 0
; layer_height = 0.2
; match_horizontal_surfaces = 0
; raft_layers = 0
; regions_overlap = 0
; seam_position = aligned
; sequential_print_priority = 0
; support_material = 0
; support_material_angle = 0
; support_material_buildplate_only = 0
; support_material_contact_distance = 0.2
; support_material_enforce_layers = 0
; support_material_extruder = 1
; support_material_extrusion_width = 0
; support_material_interface_extruder = 1
; support_material_interface_extrusion_width = 0
; support_material_interface_layers = 3
; support_material_interface_spacing = 0
; support_material_interface_speed = 100%
; support_material_max_layers = 0
; support_material_pattern = pillars
; support_material_spacing = 2.5
; support_material_speed = 5
; support_material_threshold = 60%
; xy_size_compensation = 0
; bottom_infill_pattern = rectilinear
; bottom_solid_layers = 3
; bridge_flow_ratio = 1
; bridge_speed = 5
; external_perimeter_extrusion_width = 0
; external_perimeter_speed = 5
; external_perimeters_first = 0
; extra_perimeters = 1
; fill_angle = 45
; fill_density = 100%
; fill_gaps = 1
; fill_pattern = rectilinear
; gap_fill_speed = 5
; infill_every_layers = 1
; infill_extruder = 1
; infill_extrusion_width = 0
; infill_overlap = 55%
; infill_speed = 5
; overhangs = 1
; perimeter_extruder = 1
; perimeter_extrusion_width = 0
; perimeter_speed = 5
; perimeters = 3
; small_perimeter_speed = 5
; solid_infill_below_area = 70
; solid_infill_every_layers = 0
; solid_infill_extruder = 1
; solid_infill_extrusion_width = 0
; solid_infill_speed = 5
; thin_walls = 1
; top_infill_extrusion_width = 0
; top_infill_pattern = rectilinear
; top_solid_infill_speed = 5
; top_solid_layers = 3

我想重新创建的内容的简短摘录:我从之前的挤出中减去 8 来创建这条线 G1 E-0.66668 F1800。然后,在换层之后基本上只是打印了之前的挤压。G1 E7.33332 F1800.000。

G1 X97.943 Y96.632 E7.23091
G1 X96.632 Y97.943 E7.27403
G1 X96.632 Y97.427 E7.28605
G1 X97.427 Y96.632 E7.31217
G1 X96.910 Y96.632 E7.32419
G1 X96.632 Y96.910 E7.33332

;-----------------
G1 E-0.66668 F1800
G28 X0 ; home X axis
G28 Y0 ; home X axis
G4 P10000 ; Delay for miliseconds

;-----------------
G1 Z0.400 F1800.000

;-----------------------------
G1 E7.33332 F1800.000

G1 X102.666 Y102.666 F1800.000
G1 F300
G1 X97.334 Y102.666 E7.60387
G1 X97.334 Y97.334 E7.87442
G1 X102.666 Y97.334 E8.14497
G1 X102.666 Y102.576 E8.41095
G1 X103.463 Y103.463 F1800.000
4

1 回答 1

1

您必须阅读这些行并维护一个缓冲区以使用收回和恢复行包装“Z”块:

#!/usr/bin/perl -i 
use strict;
use warnings;
my @buffer;
my $e_value;
while (<>){
    if (m/E(-?\d+\.\d+)/){
        $e_value = $1;
        # if buffer holds lines but we haven't seen a Z
        # we can print and reset
        if (@buffer){
            print @buffer;
            @buffer = ();
        }
        print;
    }
    else{
        # no E- keep this line, but do not print it yet
        push @buffer, $_;
    }
    if (m/Z\d+\.\d+/){
        # found Z add the modified lines around the buffer
        print "\n;---found a Z entry, retract;\n";
        printf "G1 E%.5f F1800.000",$e_value -8;
        print @buffer;
        @buffer = ();
        print "\n;---After Z entry, reset position:\n";
        printf "G1 E%.5f F1800.000",$e_value ;

    }
}

在您写的评论中,您需要一个-i开关才能使脚本为您工作。阅读perlrun中的开关。如果您perldoc perlrun在 shell 中键入,也可以使用。
如果没有-i开关,这将打印到STDOUT并可以重定向到文件script inputfile > outputfile。使用-i开关 perl 将打印到具有原始名称的文件。

于 2021-03-15T17:10:45.890 回答