MichaelRKessler commited on
Commit
d48e528
·
1 Parent(s): b48633f

Add color dropdowns for Travel and Print lines

Browse files
Files changed (2) hide show
  1. app.py +48 -2
  2. gcode_viewer.py +4 -2
app.py CHANGED
@@ -339,6 +339,8 @@ def render_toolpath(
339
  shape1_path: str | None,
340
  travel_opacity: float = 0.55,
341
  print_opacity: float = 1.0,
 
 
342
  ) -> tuple[Any, str, dict]:
343
  if source == GCODE_SOURCE_UPLOAD:
344
  path = uploaded_path
@@ -358,7 +360,7 @@ def render_toolpath(
358
  if parsed["point_count"] == 0:
359
  return None, "No G0/G1 movement lines found in the file.", {}
360
 
361
- figure = build_toolpath_figure(parsed, travel_opacity=travel_opacity, print_opacity=print_opacity)
362
  (x_min, y_min, z_min), (x_max, y_max, z_max) = parsed["bounds"]
363
  summary = (
364
  f"**{parsed['point_count']} moves parsed** — "
@@ -689,6 +691,12 @@ def build_demo() -> gr.Blocks:
689
  value=0.55,
690
  step=0.05,
691
  )
 
 
 
 
 
 
692
  print_opacity_slider = gr.Slider(
693
  label="Print (G1) opacity",
694
  minimum=0.0,
@@ -696,6 +704,12 @@ def build_demo() -> gr.Blocks:
696
  value=1.0,
697
  step=0.05,
698
  )
 
 
 
 
 
 
699
  toolpath_plot = gr.Plot(label="Tool Path", elem_id="toolpath_plot")
700
  toolpath_status = gr.Markdown("")
701
  parsed_state = gr.State({})
@@ -708,7 +722,7 @@ def build_demo() -> gr.Blocks:
708
  )
709
  render_button.click(
710
  fn=render_toolpath,
711
- inputs=[gcode_source, gcode_upload, gcode_file_1, travel_opacity_slider, print_opacity_slider],
712
  outputs=[toolpath_plot, toolpath_status, parsed_state],
713
  )
714
  travel_opacity_slider.release(
@@ -743,6 +757,38 @@ def build_demo() -> gr.Blocks:
743
  return [];
744
  }"""
745
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
746
 
747
  return demo
748
 
 
339
  shape1_path: str | None,
340
  travel_opacity: float = 0.55,
341
  print_opacity: float = 1.0,
342
+ travel_color: str = "#969696",
343
+ print_color: str = "#1f77b4",
344
  ) -> tuple[Any, str, dict]:
345
  if source == GCODE_SOURCE_UPLOAD:
346
  path = uploaded_path
 
360
  if parsed["point_count"] == 0:
361
  return None, "No G0/G1 movement lines found in the file.", {}
362
 
363
+ figure = build_toolpath_figure(parsed, travel_opacity=travel_opacity, print_opacity=print_opacity, travel_color=travel_color, print_color=print_color)
364
  (x_min, y_min, z_min), (x_max, y_max, z_max) = parsed["bounds"]
365
  summary = (
366
  f"**{parsed['point_count']} moves parsed** — "
 
691
  value=0.55,
692
  step=0.05,
693
  )
694
+ travel_color_picker = gr.Dropdown(
695
+ label="Travel (G0) color",
696
+ choices=[("Grey", "#969696"), ("Orange", "#ff7f0e"), ("Green", "#2ca02c"), ("Red", "#d62728"), ("Purple", "#9467bd"), ("Pink", "#e377c2"), ("Black", "#000000"), ("White", "#ffffff")],
697
+ value="#969696",
698
+ allow_custom_value=False,
699
+ )
700
  print_opacity_slider = gr.Slider(
701
  label="Print (G1) opacity",
702
  minimum=0.0,
 
704
  value=1.0,
705
  step=0.05,
706
  )
707
+ print_color_picker = gr.Dropdown(
708
+ label="Print (G1) color",
709
+ choices=[("Blue", "#1f77b4"), ("Orange", "#ff7f0e"), ("Green", "#2ca02c"), ("Red", "#d62728"), ("Purple", "#9467bd"), ("Pink", "#e377c2"), ("Black", "#000000"), ("White", "#ffffff")],
710
+ value="#1f77b4",
711
+ allow_custom_value=False,
712
+ )
713
  toolpath_plot = gr.Plot(label="Tool Path", elem_id="toolpath_plot")
714
  toolpath_status = gr.Markdown("")
715
  parsed_state = gr.State({})
 
722
  )
723
  render_button.click(
724
  fn=render_toolpath,
725
+ inputs=[gcode_source, gcode_upload, gcode_file_1, travel_opacity_slider, print_opacity_slider, travel_color_picker, print_color_picker],
726
  outputs=[toolpath_plot, toolpath_status, parsed_state],
727
  )
728
  travel_opacity_slider.release(
 
757
  return [];
758
  }"""
759
  )
760
+ travel_color_picker.change(
761
+ fn=None,
762
+ inputs=[travel_color_picker],
763
+ outputs=[],
764
+ js="""(color) => {
765
+ const container = document.getElementById("toolpath_plot");
766
+ if (!container) return [];
767
+ const plotDiv = container.querySelector(".js-plotly-plot");
768
+ if (!plotDiv || !plotDiv.data) return [];
769
+ const indices = plotDiv.data
770
+ .map((t, i) => t.name === "Travel (G0)" ? i : -1)
771
+ .filter(i => i >= 0);
772
+ if (indices.length > 0) Plotly.restyle(plotDiv, {"line.color": color}, indices);
773
+ return [];
774
+ }"""
775
+ )
776
+ print_color_picker.change(
777
+ fn=None,
778
+ inputs=[print_color_picker],
779
+ outputs=[],
780
+ js="""(color) => {
781
+ const container = document.getElementById("toolpath_plot");
782
+ if (!container) return [];
783
+ const plotDiv = container.querySelector(".js-plotly-plot");
784
+ if (!plotDiv || !plotDiv.data) return [];
785
+ const indices = plotDiv.data
786
+ .map((t, i) => t.name === "Print (G1)" ? i : -1)
787
+ .filter(i => i >= 0);
788
+ if (indices.length > 0) Plotly.restyle(plotDiv, {"line.color": color}, indices);
789
+ return [];
790
+ }"""
791
+ )
792
 
793
  return demo
794
 
gcode_viewer.py CHANGED
@@ -121,6 +121,8 @@ def build_toolpath_figure(
121
  parsed: dict,
122
  travel_opacity: float = 0.55,
123
  print_opacity: float = 1.0,
 
 
124
  ) -> go.Figure:
125
  print_xs, print_ys, print_zs = _segments_to_xyz(parsed["print_segments"])
126
  travel_xs, travel_ys, travel_zs = _segments_to_xyz(parsed["travel_segments"])
@@ -136,7 +138,7 @@ def build_toolpath_figure(
136
  mode="lines",
137
  name="Travel (G0)",
138
  opacity=travel_opacity,
139
- line=dict(color="rgb(150,150,150)", width=2, dash="dot"),
140
  hoverinfo="skip",
141
  )
142
  )
@@ -150,7 +152,7 @@ def build_toolpath_figure(
150
  mode="lines",
151
  name="Print (G1)",
152
  opacity=print_opacity,
153
- line=dict(color="#1f77b4", width=4),
154
  hovertemplate="X=%{x:.2f}<br>Y=%{y:.2f}<br>Z=%{z:.2f}<extra></extra>",
155
  )
156
  )
 
121
  parsed: dict,
122
  travel_opacity: float = 0.55,
123
  print_opacity: float = 1.0,
124
+ travel_color: str = "#969696",
125
+ print_color: str = "#1f77b4",
126
  ) -> go.Figure:
127
  print_xs, print_ys, print_zs = _segments_to_xyz(parsed["print_segments"])
128
  travel_xs, travel_ys, travel_zs = _segments_to_xyz(parsed["travel_segments"])
 
138
  mode="lines",
139
  name="Travel (G0)",
140
  opacity=travel_opacity,
141
+ line=dict(color=travel_color, width=2, dash="dot"),
142
  hoverinfo="skip",
143
  )
144
  )
 
152
  mode="lines",
153
  name="Print (G1)",
154
  opacity=print_opacity,
155
+ line=dict(color=print_color, width=4),
156
  hovertemplate="X=%{x:.2f}<br>Y=%{y:.2f}<br>Z=%{z:.2f}<extra></extra>",
157
  )
158
  )