diff --git a/crates/engine/src/cmd/draw.rs b/crates/engine/src/cmd/draw.rs index 52bf23ace..71e627e49 100644 --- a/crates/engine/src/cmd/draw.rs +++ b/crates/engine/src/cmd/draw.rs @@ -45,7 +45,7 @@ pub fn specs() -> Vec { CommandSpec::new("pline", "Polyline", run_pline) .menu(&["Draw", "Polyline"]) .alias(&["pl"]) - .params("{vertices: [[x,y] | {p:[x,y], bulge, startWidth, endWidth}], closed?, width?}") + .params("{vertices: [[x,y] | {p:[x,y], bulge, startWidth, endWidth}], closed?, width? (default PLINEWID unless vertices give widths)}") .interactive(|_| Ok(Box::new(PlineM::default()))), CommandSpec::new("circle", "Circle", run_circle) .menu(&["Draw", "Circle", "Center, Radius"]) diff --git a/crates/engine/src/cmd/draw/line_pline.rs b/crates/engine/src/cmd/draw/line_pline.rs index bfca3bc8f..62a4aac02 100644 --- a/crates/engine/src/cmd/draw/line_pline.rs +++ b/crates/engine/src/cmd/draw/line_pline.rs @@ -41,15 +41,21 @@ fn vertex_value(v: &Value) -> Option { pub(super) fn run_pline(s: &mut Session, p: &Value) -> Result { let raw = p.get("vertices").or_else(|| p.get("points")).and_then(Value::as_array); + let mut vertex_widths = false; for v in raw.into_iter().flatten() { - size_param("pline", v, "startWidth", true)?; - size_param("pline", v, "endWidth", true)?; + vertex_widths |= size_param("pline", v, "startWidth", true)?.is_some() | size_param("pline", v, "endWidth", true)?.is_some(); } let vs: Vec = raw.map(|a| a.iter().filter_map(vertex_value).collect()).unwrap_or_default(); if vs.len() < 2 { return Err(bad("pline", "`vertices` needs at least 2 points")); } - let width = size_param("pline", p, "width", true)?.unwrap_or(0.0); + // Without a width, the polyline starts at PLINEWID as the command does (unless its vertices + // give their own widths). + let width = match size_param("pline", p, "width", true)? { + Some(w) => w, + None if vertex_widths => 0.0, + None => Some(s.doc()?.header.f64("PLINEWID", 0.0)).filter(|w| w.is_finite() && *w > 0.0).unwrap_or(0.0), + }; let mut k = lwpoly(vs, bool_or(p, "closed", false)); if let EntityKind::LwPolyline(pl) = &mut k { pl.const_width = width; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index b72588f46..e64037b2e 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1291,6 +1291,17 @@ fn pline_undo_mode_and_plinewid_match_autocad() { s.cmdline("pline 0,0 10,0").unwrap(); s.cmdline("").unwrap(); assert!(matches!(&s.doc().unwrap().model.last().unwrap().kind, EntityKind::LwPolyline(p) if p.const_width == 0.5)); + // The JSON form starts there too, unless it gives a width or its vertices give theirs. + let width = |s: &mut Session, p: serde_json::Value| { + s.execute("pline", &p).unwrap(); + match &s.doc().unwrap().model.last().unwrap().kind { + EntityKind::LwPolyline(p) => p.const_width, + k => panic!("{k:?}"), + } + }; + assert_eq!(width(&mut s, json!({"vertices": [[0, 0], [10, 0]]})), 0.5); + assert_eq!(width(&mut s, json!({"vertices": [[0, 0], [10, 0]], "width": 0})), 0.0); + assert_eq!(width(&mut s, json!({"vertices": [{"p": [0, 0], "startWidth": 1, "endWidth": 2}, [10, 0]]})), 0.0); } #[test]