mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-01 18:25:32 -06:00
PLINE: the JSON form starts at PLINEWID too
A JSON pline without a width now takes PLINEWID, as the command does, unless its vertices give their own widths; an explicit width, 0 included, is kept. FILLET and CHAMFER's JSON forms already fall back on their variables the same way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
45f9b5653e
commit
d56ead7ca4
@@ -45,7 +45,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
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"])
|
||||
|
||||
@@ -41,15 +41,21 @@ fn vertex_value(v: &Value) -> Option<PolyVertex> {
|
||||
|
||||
pub(super) fn run_pline(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
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<PolyVertex> = 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;
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user