4c1dee13f3
4 commits, squashed; the originals stay reachable from tag archive/merge-main-develop. - b16e8a542 jyviko: Sketch: a control spline can carry its own knots and weights - e6abea809 jyviko: Sketch projection: a free-form edge comes in as the one exact spline it is - f61f25c7f jyviko: Fillet rig: blends past an outer rim of their faces build, on SMLib smlib-blends-past-rims - 09bc14dbd jyviko: Naming: test that a round of a round's end has no seam with that round's faces Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
405 lines
20 KiB
Rust
405 lines
20 KiB
Rust
//! Sketch drawing exchange: Insert DXF, Insert SVG and sketch DXF export.
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use serde_json::{Value, json};
|
|
use solidcraft_geom::Vec2;
|
|
use solidcraft_io::{Geom2, MAX_DRAWING_BYTES};
|
|
use solidcraft_sketch::{CurveKind, LinkKind, LinkSource, Sketch};
|
|
|
|
use super::CommandSpec;
|
|
use super::sketch::{edit, ids_of};
|
|
use crate::params::{bad, num, str_, vec2};
|
|
use crate::{EngineError, Result, Session};
|
|
|
|
pub static COMMANDS: &[CommandSpec] = &[
|
|
CommandSpec::new("sketch.insert_dxf", "Insert DXF", insert_dxf)
|
|
.at("SKETCH", "INSERT")
|
|
.icon("sketch_dxf")
|
|
.params(concat!("path | text: ASCII DXF (blocks expanded); at?: [x,y] offset, scale? | width? | height?: mm, the drawing's size before rotation, aspect kept (win over scale); angle?: degrees, about the drawing's origin (or its centre); align?: \"origin\" (default: `at` is where the file's origin goes) | \"center\" (`at` is where the drawing's middle goes and it turns and scales about it); tolerance?: mm within which ends join (default 0.0001); plane?: (when no sketch is active, a new sketch on it; default XY)", created_doc!())),
|
|
CommandSpec::new("sketch.insert_svg", "Insert SVG", insert_svg)
|
|
.at("SKETCH", "INSERT")
|
|
.icon("svg")
|
|
.params(concat!("path | text: SVG; at?: [x,y] offset, scale? | width? | height?: mm, the drawing's size before rotation, aspect kept (win over scale); angle?: degrees, about the drawing's origin (or its centre); align?: \"origin\" (default: `at` is where the file's origin goes) | \"center\" (`at` is where the drawing's middle goes and it turns and scales about it); tolerance?: mm within which ends join (default 0.0001); plane?: (when no sketch is active, a new sketch on it; default XY)", created_doc!())),
|
|
CommandSpec::new("sketch.drawing_bounds", "Drawing Size", drawing_bounds)
|
|
.icon("svg")
|
|
.noundo()
|
|
.params("path | text: SVG or DXF (by `format`: \"svg\" | \"dxf\", default by the file name, else SVG); returns min, max, width and height in the file's own size"),
|
|
CommandSpec::new("sketch.export_dxf", "Save As DXF", export_dxf).icon("sketch_dxf")
|
|
.noundo()
|
|
.params("sketch?: id|name (default active); path?: file to write (else the DXF text is returned)"),
|
|
];
|
|
|
|
/// The sketch import command for a drawing file name, regardless of extension case.
|
|
pub fn drawing_import_command(path: &str) -> Option<&'static str> {
|
|
let ext = std::path::Path::new(path).extension()?.to_str()?;
|
|
if ext.eq_ignore_ascii_case("dxf") {
|
|
Some("sketch.insert_dxf")
|
|
} else if ext.eq_ignore_ascii_case("svg") {
|
|
Some("sketch.insert_svg")
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn source_text(p: &Value, cmd: &str) -> Result<String> {
|
|
if let Some(t) = str_(p, "text") {
|
|
if t.len() > MAX_DRAWING_BYTES {
|
|
return Err(bad(cmd, "the drawing is too large"));
|
|
}
|
|
return Ok(t.to_string());
|
|
}
|
|
let path = str_(p, "path").filter(|x| !x.trim().is_empty() && x.len() < 4096).ok_or_else(|| bad(cmd, "give `path` or `text`"))?;
|
|
let bytes = solidcraft_io::vfs::read(path, MAX_DRAWING_BYTES).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
|
Ok(String::from_utf8_lossy(&bytes).to_string())
|
|
}
|
|
|
|
/// Add imported geometry to a sketch, sharing end points that meet.
|
|
/// Adds the geometry; end points closer than `q` (mm) become one point, so drawings whose
|
|
/// ends almost meet still make closed profiles.
|
|
pub(super) fn add_geometry(sk: &mut Sketch, geom: &[Geom2], f: &dyn Fn(Vec2) -> Vec2, k: f64, q: f64) -> Result<(Vec<usize>, Vec<Geom2>)> {
|
|
let mut map: HashMap<(i64, i64), Vec<(Vec2, usize)>> = HashMap::new();
|
|
let mut pt = |sk: &mut Sketch, p: Vec2| -> Result<usize> {
|
|
let p = f(p);
|
|
let key = ((p.x / q).floor() as i64, (p.y / q).floor() as i64);
|
|
for dx in -1..=1 {
|
|
for dy in -1..=1 {
|
|
if let Some(v) = map.get(&(key.0 + dx, key.1 + dy))
|
|
&& let Some((_, i)) = v.iter().find(|(o, _)| o.dist(p) <= q)
|
|
{
|
|
return Ok(*i);
|
|
}
|
|
}
|
|
}
|
|
let i = sk.add_point(p, None)?;
|
|
map.entry(key).or_default().push((p, i));
|
|
Ok(i)
|
|
};
|
|
let mut curves = Vec::new();
|
|
let mut texts = Vec::new();
|
|
for g in geom {
|
|
let made = match g {
|
|
Geom2::Point(p) => {
|
|
pt(sk, *p)?;
|
|
None
|
|
}
|
|
Geom2::Line(a, b) => {
|
|
let (a, b) = (pt(sk, *a)?, pt(sk, *b)?);
|
|
(a != b).then_some(CurveKind::Line { a, b })
|
|
}
|
|
Geom2::Circle(c, r) => Some(CurveKind::Circle { c: pt(sk, *c)?, r: r * k }),
|
|
Geom2::Arc { c, a, b } => {
|
|
let (c, a, b) = (pt(sk, *c)?, pt(sk, *a)?, pt(sk, *b)?);
|
|
(a != b && c != a).then_some(CurveKind::Arc { c, a, b })
|
|
}
|
|
Geom2::Ellipse { c, major, minor } => Some(CurveKind::Ellipse { c: pt(sk, *c)?, m: pt(sk, *major)?, r: minor * k }),
|
|
Geom2::Spline { pts, control, degree } => {
|
|
let mut idx = Vec::new();
|
|
for p in pts.iter().take(solidcraft_sketch::MAX_SPLINE_POINTS) {
|
|
let i = pt(sk, *p)?;
|
|
if idx.last() != Some(&i) {
|
|
idx.push(i);
|
|
}
|
|
}
|
|
(idx.len() >= 2).then_some(CurveKind::Spline { pts: idx, control: *control, degree: *degree, periodic: false, nurbs: None })
|
|
}
|
|
Geom2::Conic { a, apex, b, rho } => {
|
|
let (a, x, b) = (pt(sk, *a)?, pt(sk, *apex)?, pt(sk, *b)?);
|
|
(a != b && x != a && x != b).then_some(CurveKind::Conic { a, b, apex: x, rho: *rho })
|
|
}
|
|
Geom2::Text { text, at, height, angle } => {
|
|
texts.push(Geom2::Text { text: text.clone(), at: f(*at), height: height * k, angle: *angle });
|
|
None
|
|
}
|
|
};
|
|
if let Some(kind) = made
|
|
&& let Ok(c) = sk.add_curve(kind, None)
|
|
{
|
|
curves.push(c);
|
|
}
|
|
}
|
|
Ok((curves, texts))
|
|
}
|
|
|
|
/// The box around a drawing's geometry (arcs, circles and curves by their extent).
|
|
fn bounds_of(geom: &[Geom2]) -> Option<(Vec2, Vec2)> {
|
|
let mut pts: Vec<Vec2> = Vec::new();
|
|
for g in geom {
|
|
match g {
|
|
Geom2::Point(p) => pts.push(*p),
|
|
Geom2::Line(a, b) => pts.extend([*a, *b]),
|
|
Geom2::Circle(c, r) => pts.extend([*c + Vec2::new(*r, *r), *c - Vec2::new(*r, *r)]),
|
|
Geom2::Arc { c, a, b } => {
|
|
let r = c.dist(*a);
|
|
let (a0, mut a1) = ((*a - *c).angle(), (*b - *c).angle());
|
|
while a1 <= a0 {
|
|
a1 += std::f64::consts::TAU;
|
|
}
|
|
pts.extend([*a, *b]);
|
|
// The four extreme directions that lie on the arc.
|
|
for q in 0..4 {
|
|
let mut d = f64::from(q) * std::f64::consts::FRAC_PI_2;
|
|
while d < a0 {
|
|
d += std::f64::consts::TAU;
|
|
}
|
|
if d < a1 {
|
|
pts.push(*c + Vec2::from_angle(d) * r);
|
|
}
|
|
}
|
|
}
|
|
Geom2::Ellipse { c, major, minor } => {
|
|
let r = c.dist(*major).max(*minor);
|
|
pts.extend([*c + Vec2::new(r, r), *c - Vec2::new(r, r)]);
|
|
}
|
|
Geom2::Spline { pts: sp, .. } => pts.extend(sp.iter().copied()),
|
|
Geom2::Conic { a, apex, b, .. } => pts.extend([*a, *apex, *b]),
|
|
Geom2::Text { at, .. } => pts.push(*at),
|
|
}
|
|
}
|
|
let first = *pts.first()?;
|
|
let (lo, hi) = pts.iter().fold((first, first), |(lo, hi), p| (Vec2::new(lo.x.min(p.x), lo.y.min(p.y)), Vec2::new(hi.x.max(p.x), hi.y.max(p.y))));
|
|
(lo.x.is_finite() && lo.y.is_finite() && hi.x.is_finite() && hi.y.is_finite()).then_some((lo, hi))
|
|
}
|
|
|
|
fn read_drawing(p: &Value, cmd: &str, svg: bool) -> Result<Vec<Geom2>> {
|
|
let text = source_text(p, cmd)?;
|
|
if svg { solidcraft_io::read_svg(&text) } else { solidcraft_io::read_dxf(&text) }.map_err(|e| bad(cmd, e.to_string()))
|
|
}
|
|
|
|
/// How big a drawing is in its own file, to size it before it is inserted.
|
|
fn drawing_bounds(_s: &mut Session, p: &Value) -> Result<Value> {
|
|
let cmd = "sketch.drawing_bounds";
|
|
let name_is_dxf = str_(p, "path").is_some_and(|x| x.to_ascii_lowercase().ends_with(".dxf"));
|
|
let svg = match str_(p, "format") {
|
|
Some(f) => !f.eq_ignore_ascii_case("dxf"),
|
|
None => !name_is_dxf,
|
|
};
|
|
let geom = read_drawing(p, cmd, svg)?;
|
|
let (lo, hi) = bounds_of(&geom).ok_or_else(|| bad(cmd, "the drawing has no geometry SolidCraft reads"))?;
|
|
Ok(json!({"min": [lo.x, lo.y], "max": [hi.x, hi.y], "width": hi.x - lo.x, "height": hi.y - lo.y}))
|
|
}
|
|
|
|
fn insert(s: &mut Session, p: &Value, cmd: &str, svg: bool) -> Result<Value> {
|
|
let geom = read_drawing(p, cmd, svg)?;
|
|
if geom.is_empty() {
|
|
return Err(bad(cmd, "the drawing has no geometry SolidCraft reads"));
|
|
}
|
|
// A size in millimetres wins over a factor; the aspect ratio is always kept.
|
|
let natural = bounds_of(&geom).map(|(lo, hi)| (hi.x - lo.x, hi.y - lo.y));
|
|
let sized = |key: &str, of: fn(&(f64, f64)) -> f64| -> Result<Option<f64>> {
|
|
let Some(v) = p.get(key) else { return Ok(None) };
|
|
let want = v.as_f64().filter(|w| w.is_finite() && *w > 0.0).ok_or_else(|| bad(cmd, format!("`{key}` must be a positive number (mm)")))?;
|
|
let have = natural.as_ref().map(of).filter(|h| *h > 1e-9).ok_or_else(|| bad(cmd, format!("the drawing has no {key} to size")))?;
|
|
Ok(Some(want / have))
|
|
};
|
|
let from_size = match sized("width", |n| n.0)? {
|
|
Some(k) => Some(k),
|
|
None => sized("height", |n| n.1)?,
|
|
};
|
|
let k = from_size.or_else(|| num(p, "scale")).filter(|k| *k > 1e-9 && *k < 1e6).unwrap_or(1.0);
|
|
let angle = match p.get("angle") {
|
|
Some(v) => v.as_f64().filter(|a| a.is_finite()).ok_or_else(|| bad(cmd, "`angle` must be a number (degrees)"))?.to_radians(),
|
|
None => 0.0,
|
|
};
|
|
let (sin, cos) = angle.sin_cos();
|
|
let pivot = match str_(p, "align") {
|
|
None | Some("origin") => Vec2::ZERO,
|
|
Some("center") => bounds_of(&geom).map(|(lo, hi)| (lo + hi) * 0.5).unwrap_or(Vec2::ZERO),
|
|
Some(_) => return Err(bad(cmd, "`align` must be \"origin\" or \"center\"")),
|
|
};
|
|
let off = p.get("at").and_then(vec2).unwrap_or(Vec2::ZERO);
|
|
let mut created = None;
|
|
if s.active_sketch.is_none() {
|
|
let mut cp = json!({"plane": p.get("plane").cloned().unwrap_or(json!("XY")), "project_edges": false});
|
|
if let Some(n) = str_(p, "name") {
|
|
cp["name"] = json!(n);
|
|
}
|
|
let r = (super::find_command("sketch.create").ok_or_else(|| bad(cmd, "sketch.create"))?.run)(s, &cp)?;
|
|
created = r.get("sketch").and_then(Value::as_u64);
|
|
}
|
|
let tol = num(p, "tolerance").filter(|t| t.is_finite() && *t > 0.0).unwrap_or(1e-4).clamp(1e-9, 10.0);
|
|
let f = move |q: Vec2| {
|
|
let v = (q - pivot) * k;
|
|
Vec2::new(v.x * cos - v.y * sin, v.x * sin + v.y * cos) + off
|
|
};
|
|
let ((curves, texts), info) = edit(s, p, cmd, false, |sk, _| {
|
|
let (cs, texts) = add_geometry(sk, &geom, &f, k, tol)?;
|
|
Ok((ids_of(sk, &cs), texts))
|
|
})?;
|
|
// Text becomes sketch text.
|
|
let id = s.active_sketch.ok_or_else(|| bad(cmd, "no sketch"))?;
|
|
let mut n_text = 0;
|
|
for t in texts {
|
|
if let Geom2::Text { text, at, height, angle: own } = t {
|
|
let mut doc = (*s.doc).clone();
|
|
let mut sk = doc.sketch(id)?.clone();
|
|
if super::sketch_project::add_link(
|
|
s,
|
|
&doc,
|
|
id,
|
|
&mut sk,
|
|
LinkKind::Text,
|
|
LinkSource::Text { text, at, height, angle: own + angle, font: None, style: Default::default(), exprs: Default::default() },
|
|
)
|
|
.is_ok()
|
|
{
|
|
*doc.sketch_mut(id)? = sk;
|
|
*s.doc_mut() = doc;
|
|
n_text += 1;
|
|
}
|
|
}
|
|
}
|
|
Ok(json!({"sketch": id, "created_sketch": created, "curves": curves.len(), "texts": n_text, "info": info}))
|
|
}
|
|
|
|
fn insert_dxf(s: &mut Session, p: &Value) -> Result<Value> {
|
|
insert(s, p, "sketch.insert_dxf", false)
|
|
}
|
|
|
|
fn insert_svg(s: &mut Session, p: &Value) -> Result<Value> {
|
|
insert(s, p, "sketch.insert_svg", true)
|
|
}
|
|
|
|
fn export_dxf(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let cmd = "sketch.export_dxf";
|
|
let id = match p.get("sketch") {
|
|
Some(Value::Number(n)) => n.as_u64(),
|
|
Some(Value::String(x)) => s.doc.find_feature(x).map(|f| f.id),
|
|
_ => s.active_sketch,
|
|
}
|
|
.ok_or_else(|| bad(cmd, "no such sketch (and none is active)"))?;
|
|
let st = s.model.state();
|
|
let ss = st.sketch(id).ok_or_else(|| EngineError::Other("that sketch is not evaluated".into()))?;
|
|
let sk = &ss.sketch;
|
|
let mut geom = Vec::new();
|
|
for (i, c) in sk.curves.iter().enumerate() {
|
|
if c.construction {
|
|
continue;
|
|
}
|
|
let pt = |k: usize| sk.point(k).unwrap_or_default();
|
|
match &c.kind {
|
|
CurveKind::Line { a, b } => geom.push(Geom2::Line(pt(*a), pt(*b))),
|
|
CurveKind::Circle { c, r } => geom.push(Geom2::Circle(pt(*c), *r)),
|
|
CurveKind::Arc { c, a, b } => geom.push(Geom2::Arc { c: pt(*c), a: pt(*a), b: pt(*b) }),
|
|
_ => {
|
|
let poly = sk.polyline(i);
|
|
geom.extend(poly.windows(2).map(|w| Geom2::Line(w[0], w[1])));
|
|
}
|
|
}
|
|
}
|
|
let text = solidcraft_io::write_dxf(&geom);
|
|
if let Some(path) = str_(p, "path").filter(|x| !x.trim().is_empty()) {
|
|
solidcraft_io::vfs::write(path, text.as_bytes()).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
|
return Ok(json!({"path": path, "entities": geom.len()}));
|
|
}
|
|
Ok(json!({"dxf": text, "entities": geom.len()}))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use serde_json::json;
|
|
|
|
use crate::Session;
|
|
|
|
#[test]
|
|
fn dxf_and_svg_round_trip_into_sketches() {
|
|
let mut s = Session::default();
|
|
s.execute("sketch.create", &json!({"plane": "XY"})).unwrap();
|
|
s.execute("sketch.rectangle.two_point", &json!({"p0": [0, 0], "p1": [30, 20]})).unwrap();
|
|
s.execute("sketch.circle.center", &json!({"center": [10, 10], "radius": 4})).unwrap();
|
|
let out = s.execute("sketch.export_dxf", &json!({})).unwrap();
|
|
let dxf = out["dxf"].as_str().unwrap().to_string();
|
|
s.execute("sketch.finish", &json!({})).unwrap();
|
|
// Insert it again as a new sketch, offset: same profiles.
|
|
let r = s.execute("sketch.insert_dxf", &json!({"text": dxf, "at": [100, 0]})).unwrap();
|
|
assert_eq!(r["curves"], 5, "{r}");
|
|
let id = r["sketch"].as_u64().unwrap();
|
|
let ss = s.model.state().sketch(id).unwrap().clone();
|
|
let mut areas: Vec<f64> = ss.profiles.iter().map(|p| p.area).collect();
|
|
areas.sort_by(|a, b| a.total_cmp(b));
|
|
assert_eq!(areas.len(), 2, "{areas:?}");
|
|
assert!((areas[1] - (600.0 - std::f64::consts::PI * 16.0)).abs() < 1e-6, "{areas:?}");
|
|
// SVG with a path and text, into the active sketch.
|
|
let svg =
|
|
r#"<svg width="40mm" height="40mm" viewBox="0 0 40 40"><path d="M0 0 H40 V40 H0 Z"/><text x="5" y="20" font-size="10">Hi</text></svg>"#;
|
|
let r = s.execute("sketch.insert_svg", &json!({"text": svg})).unwrap();
|
|
assert_eq!(r["curves"], 4, "{r}");
|
|
assert_eq!(r["texts"], 1, "{r}");
|
|
assert!(s.execute("sketch.insert_svg", &json!({"text": "<svg></svg>"})).is_err());
|
|
assert!(s.execute("sketch.insert_dxf", &json!({"path": "/nonexistent.dxf"})).is_err());
|
|
s.execute("sketch.finish", &json!({})).unwrap();
|
|
// Ends that miss by 0.00005 still close the square; with a tighter tolerance they don't.
|
|
let gappy = "0\nSECTION\n2\nENTITIES\n0\nLINE\n10\n0\n20\n0\n11\n10\n21\n0\n0\nLINE\n10\n10.00005\n20\n0\n11\n10\n21\n10\n0\nLINE\n10\n10\n20\n10\n11\n0\n21\n10\n0\nLINE\n10\n0\n20\n10.00003\n11\n0\n21\n0.00004\n0\nENDSEC\n0\nEOF\n";
|
|
for (tol, n) in [(1e-4, 1), (1e-6, 0)] {
|
|
let r = s.execute("sketch.insert_dxf", &json!({"text": gappy, "tolerance": tol})).unwrap();
|
|
let ss = s.model.state().sketch(r["sketch"].as_u64().unwrap()).unwrap().clone();
|
|
assert_eq!(ss.profiles.len(), n, "tolerance {tol}");
|
|
s.execute("sketch.finish", &json!({})).unwrap();
|
|
}
|
|
}
|
|
|
|
/// A degree-9 SPLINE (solidcraft#5269) with fewer control points than its degree needs still
|
|
/// comes in as one spline over all its points, which the sketch draws at the degree they allow.
|
|
#[test]
|
|
fn a_degree_nine_spline_with_few_control_points_comes_in_whole() {
|
|
let mut s = Session::default();
|
|
for n in 2..=10 {
|
|
let points: String = (0..n).map(|i| format!("10\n{i}\n20\n{}\n", i % 2)).collect();
|
|
let dxf = format!("0\nSECTION\n2\nENTITIES\n0\nSPLINE\n71\n9\n{points}0\nENDSEC\n0\nEOF\n");
|
|
let r = s.execute("sketch.insert_dxf", &json!({"text": dxf})).unwrap();
|
|
assert_eq!(r["curves"], 1, "{n} points: {r}");
|
|
s.execute("sketch.finish", &json!({})).unwrap();
|
|
}
|
|
}
|
|
|
|
const PLATE: &str =
|
|
r#"<svg xmlns="http://www.w3.org/2000/svg" width="20mm" height="10mm" viewBox="0 0 20 10"><rect x="0" y="0" width="20" height="10"/></svg>"#;
|
|
|
|
fn span(s: &Session) -> (f64, f64, f64, f64) {
|
|
let st = s.model.state();
|
|
let sk = &st.sketch(s.active_sketch.unwrap()).unwrap().sketch;
|
|
let (mut lo, mut hi) = ((f64::MAX, f64::MAX), (f64::MIN, f64::MIN));
|
|
for p in (0..sk.curves.len()).flat_map(|i| sk.polyline(i)) {
|
|
lo = (lo.0.min(p.x), lo.1.min(p.y));
|
|
hi = (hi.0.max(p.x), hi.1.max(p.y));
|
|
}
|
|
(lo.0, lo.1, hi.0, hi.1)
|
|
}
|
|
|
|
fn inserted(params: serde_json::Value) -> (f64, f64, f64, f64) {
|
|
let mut s = Session::default();
|
|
s.execute("sketch.create", &json!({"plane": "XY"})).unwrap();
|
|
let mut p = json!({"text": PLATE});
|
|
p.as_object_mut().unwrap().extend(params.as_object().unwrap().clone());
|
|
s.execute("sketch.insert_svg", &p).unwrap();
|
|
span(&s)
|
|
}
|
|
|
|
/// A drawing can be given a size in millimetres (its aspect kept), turned about its origin and
|
|
/// placed; the size of the file itself is read without inserting anything.
|
|
#[test]
|
|
fn an_inserted_drawing_takes_a_width_a_height_and_an_angle() {
|
|
let mut s = Session::default();
|
|
let b = s.execute("sketch.drawing_bounds", &json!({"text": PLATE, "format": "svg"})).unwrap();
|
|
assert_eq!((b["width"].as_f64(), b["height"].as_f64()), (Some(20.0), Some(10.0)));
|
|
let near = |a: (f64, f64, f64, f64), e: (f64, f64, f64, f64)| {
|
|
assert!((a.0 - e.0).abs() < 1e-6 && (a.1 - e.1).abs() < 1e-6 && (a.2 - e.2).abs() < 1e-6 && (a.3 - e.3).abs() < 1e-6, "{a:?} vs {e:?}")
|
|
};
|
|
near(inserted(json!({"width": 40})), (0.0, -20.0, 40.0, 0.0));
|
|
near(inserted(json!({"height": 5, "at": [3, 4]})), (3.0, -1.0, 13.0, 4.0));
|
|
near(inserted(json!({"width": 40, "scale": 9})), (0.0, -20.0, 40.0, 0.0));
|
|
// Turned 90 degrees about the origin, then placed.
|
|
near(inserted(json!({"angle": 90, "at": [100, 0]})), (100.0, 0.0, 110.0, 20.0));
|
|
// About its centre: the middle lands on `at` whatever the size and angle.
|
|
near(inserted(json!({"align": "center", "at": [50, 50]})), (40.0, 45.0, 60.0, 55.0));
|
|
near(inserted(json!({"align": "center", "at": [50, 50], "width": 40})), (30.0, 40.0, 70.0, 60.0));
|
|
near(inserted(json!({"align": "center", "at": [50, 50], "angle": 90})), (45.0, 40.0, 55.0, 60.0));
|
|
for bad in [json!({"width": 0}), json!({"height": -1}), json!({"angle": "x"}), json!({"align": "left"})] {
|
|
s.execute("sketch.create", &json!({"plane": "XY"})).unwrap();
|
|
let mut p = json!({"text": PLATE});
|
|
p.as_object_mut().unwrap().extend(bad.as_object().unwrap().clone());
|
|
assert!(s.execute("sketch.insert_svg", &p).is_err(), "{bad}");
|
|
}
|
|
}
|
|
}
|