File size: 3,884 Bytes
0664784
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# settings_report_details.py

import re
from decimal import Decimal

def analyze_settings(gcode):
    # Initialize settings with lists to track multiple entries
    settings = {
        "Unit System": set(),
        "Mode": set(),
        "Selected Plane": set(),
        "Work Coordinates": set(),
        "Spindle Speed": set(),
        "Spindle Rotation": set(),
        "Feed Rate": set(),
        "Maximum Depth": None,  # Track only the maximum depth
    }

    lines = gcode.splitlines()
    max_depth = None
    unit_system = "mm/min"  # Default to metric if not specified

    for line in lines:
        if "G20" in line:
            settings["Unit System"].add("Imperial Units")
            unit_system = "in/min"
        elif "G21" in line:
            settings["Unit System"].add("Metric Units")
            unit_system = "mm/min"
        
        if "G90" in line:
            settings["Mode"].add("Absolute Programming")
        elif "G91" in line:
            settings["Mode"].add("Incremental Programming")
        
        if "G17" in line:
            settings["Selected Plane"].add("X-Y Plane")
        elif "G18" in line:
            settings["Selected Plane"].add("Z-X Plane")
        elif "G19" in line:
            settings["Selected Plane"].add("Z-Y Plane")
        
        work_coords = {"G54", "G55", "G56", "G57", "G58", "G59"}
        for coord in work_coords:
            if coord in line:
                settings["Work Coordinates"].add(coord)
        
        if "M03" in line:
            settings["Spindle Rotation"].add("Clockwise")
        elif "M04" in line:
            settings["Spindle Rotation"].add("Counterclockwise")
        
        # Track spindle speeds, feed rates, and Z depths
        spindle_speed = re.search(r"S(\d+)", line)
        if spindle_speed:
            settings["Spindle Speed"].add(spindle_speed.group(1) + " RPM")
        
        feed_rate = re.search(r"F(\d+\.?\d*)", line)
        if feed_rate:
            settings["Feed Rate"].add(feed_rate.group(1) + f" {unit_system}")
        
        z_value = re.search(r"Z(-?\d+\.?\d*)", line)
        if z_value:
            try:
                depth = Decimal(z_value.group(1))
                if max_depth is None or depth < max_depth:
                    max_depth = depth
            except Exception as e:
                print(f"Error processing Z depth in line: {line}, error: {e}")

    # Assign the maximum depth after checking all lines
    if max_depth is not None:
        unit = "mm" if "Metric Units" in settings["Unit System"] else "in"
        settings["Maximum Depth"] = f"{abs(max_depth)} {unit}"
    
    # Convert all sets to sorted lists (to handle multiple entries)
    for key, value in settings.items():
        if isinstance(value, set):
            settings[key] = sorted(value)

    return settings

def generate_detailed_report(gcode):
    settings = analyze_settings(gcode)
    report = []
    for key in ["Unit System", "Mode", "Selected Plane", "Work Coordinates", "Spindle Speed", "Spindle Rotation", "Feed Rate", "Maximum Depth"]:
        if isinstance(settings[key], list):
            # Join multiple values with commas
            report.append(f"{key}: {', '.join(settings[key])}")
        else:
            report.append(f"{key}: {settings[key]}")
    return "\n".join(report)

# Testing block to run if the script is executed directly
if __name__ == "__main__":
    # Example G-code sample for testing with multiple values
    gcode_sample = """
    G21 G90 G54 G17
    G0 X0 Y0 Z5.0
    M03 S1000
    G1 Z-0.1 F100
    G1 X10 Y10 Z-0.1
    M04 S1200
    G91
    G1 X20 Y20 F150
    G1 Z-0.2
    G90
    G1 Z5.0
    M05
    M30
    %
    """

    # Generate and print the detailed report for the test G-code
    report = generate_detailed_report(gcode_sample)
    print("G-code Detailed Report")
    print("=" * 40)
    print(report)