From 9e022cf31cb996cbb4d8de47584270a2281f9f94 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Thu, 22 Oct 2026 00:46:28 +1100 Subject: [PATCH 1/5] AutoLISP: read block definitions: tblsearch BLOCK gives the first entity (-2), entnext and entget walk a block's entities (part of #1700) A routine couldn't reach the entities of a block definition, so the text a dimension's *D block stores (which CADCraft keeps and draws) was out of reach. tblsearch "BLOCK" now gives the first entity in -2, entnext goes from one entity of a definition to the next (nil after the last), entget lists them without a layout (410), and handent finds them. entmod and entdel still leave definitions alone. --- crates/engine/src/lisp/drawing.rs | 46 +++++++++++++++-- crates/engine/src/lisp/tests.rs | 82 +++++++++++++++++++++++++++++++ docs/autolisp.md | 5 ++ 3 files changed, 128 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/lisp/drawing.rs b/crates/engine/src/lisp/drawing.rs index 573eaa24a..74aa37bde 100644 --- a/crates/engine/src/lisp/drawing.rs +++ b/crates/engine/src/lisp/drawing.rs @@ -295,7 +295,14 @@ pub fn entget(ctx: &mut Ctx<'_>, a: Vec) -> Result { arity(&a, 1, 2)?; let h = ename(&a[0])?; let Ok(d) = ctx.s.doc() else { return Ok(Flow::Value(Value::Nil)) }; - let Some(e) = d.entity(h) else { return Ok(Flow::Value(Value::Nil)) }; + // An entity of a block definition (#1700) has no layout. + let (e, in_block) = match d.entity(h) { + Some(e) => (e, false), + None => match block_entity(d, h) { + Some(e) => (e, true), + None => return Ok(Flow::Value(Value::Nil)), + }, + }; let (kind, mut tags) = (codec()?.groups)(d, e); convert_angles(&kind, &mut tags, f64::to_radians); let layout = match space_of(d, h) { @@ -303,7 +310,7 @@ pub fn entget(ctx: &mut Ctx<'_>, a: Vec) -> Result { _ => "Model".into(), }; let mut items = vec![Value::cons(Value::Int(-1), Value::Ename(h)), Value::cons(Value::Int(0), Value::str(&kind))]; - let mut placed_layout = false; + let mut placed_layout = in_block; for v in groups_to_list(&tags) { // The layout name follows the AcDbEntity marker, as AutoCAD lists it. let after_marker = !placed_layout && v.car().as_number() == Some(100.0) && matches!(v.cdr(), Value::Str(ref s) if &**s == "AcDbEntity"); @@ -438,6 +445,17 @@ pub fn entlast(ctx: &mut Ctx<'_>, a: Vec) -> Result { pub fn entnext(ctx: &mut Ctx<'_>, a: Vec) -> Result { arity(&a, 0, 1)?; + // From an entity of a block definition: the next one in that block, nil after the last (#1700). + if let Some(v) = a.first().filter(|v| !v.is_nil()) { + let h = ename(v)?; + if let Ok(d) = ctx.s.doc() + && d.entity(h).is_none() + && let Some(b) = block_of(d, h) + { + let next = b.entities.iter().skip_while(|e| e.handle != h).nth(1).map(|e| Value::Ename(e.handle)); + return Ok(Flow::Value(next.unwrap_or_default())); + } + } let order = handles_in_order(ctx); let next = match a.first() { None | Some(Value::Nil) => order.first().copied(), @@ -453,10 +471,23 @@ pub fn entnext(ctx: &mut Ctx<'_>, a: Vec) -> Result { pub fn handent(ctx: &mut Ctx<'_>, a: Vec) -> Result { arity(&a, 1, 1)?; let s = string(&a[0])?; - let h = Handle::parse_hex(&s).filter(|h| ctx.s.doc().is_ok_and(|d| d.entity(*h).is_some())); + let h = Handle::parse_hex(&s).filter(|h| ctx.s.doc().is_ok_and(|d| d.entity(*h).is_some() || block_entity(d, *h).is_some())); Ok(Flow::Value(h.map(Value::Ename).unwrap_or_default())) } +/// The block definition holding entity `h`, when it is one of a block's (not a drawn entity). +fn block_of(d: &cadcraft_doc::Drawing, h: Handle) -> Option<&cadcraft_doc::Block> { + if h == Handle(0) { + return None; + } + d.blocks.values().map(|b| b.as_ref()).find(|b| b.entities.get(h).is_some()) +} + +/// Entity `h` of a block definition (#1700). +fn block_entity(d: &cadcraft_doc::Drawing, h: Handle) -> Option<&Arc> { + block_of(d, h).and_then(|b| b.entities.get(h)) +} + // ---------------- selection sets ---------------- /// `(ssget [mode] [pt1 [pt2]] [filter])`. Modes: "X" every entity, "L" the last, "P" the @@ -842,12 +873,17 @@ fn table_records(d: &cadcraft_doc::Drawing, table: &str) -> Vec { .blocks .values() .map(|b| { - Value::list(vec![ + let mut items = vec![ pair(0, Value::str("BLOCK")), pair(2, Value::str(&b.name)), pair(70, Value::Int(0)), Value::list(vec![Value::Int(10), Value::Real(b.base.x), Value::Real(b.base.y), Value::Real(b.base.z)]), - ]) + ]; + // Its first entity (-2): entnext walks the rest of the definition (#1700). + if let Some(first) = b.entities.iter().next() { + items.push(pair(-2, Value::Ename(first.handle))); + } + Value::list(items) }) .collect(), "DIMSTYLE" => d diff --git a/crates/engine/src/lisp/tests.rs b/crates/engine/src/lisp/tests.rs index c02b48b17..0b1c940fb 100644 --- a/crates/engine/src/lisp/tests.rs +++ b/crates/engine/src/lisp/tests.rs @@ -619,3 +619,85 @@ fn a_panic_inside_a_routine_ends_it_with_an_error() { assert!(s.log.iter().any(|l| l.contains("internal error")), "{:?}", s.log); assert_eq!(ok(&mut s, "(+ 1 2)"), "3"); } + +// ---------------- entities of block definitions (#1700) ---------------- + +/// A drawing with one linear dimension measuring 100 whose `*D1` block, as stored in the file, +/// says 99.5 (a stale block: the defpoints moved, the block wasn't rebuilt). +fn stale_dimension() -> Session { + let mut s = session(); + s.execute("dimlinear", &serde_json::json!({"p1": [0, 0], "p2": [100, 0], "at": [50, -20], "rotation": 0})).unwrap(); + let dxf = cadcraft_io::write_dxf(s.doc().unwrap()); + // The *D1 block's MTEXT says 100 (100.0000 here); make the file say 99.5. + let lines: Vec<&str> = dxf.lines().collect(); + let (mut kind, mut block, mut out, mut changed) = (String::new(), String::new(), Vec::new(), 0); + for (i, l) in lines.iter().enumerate() { + let code = if i % 2 == 0 { l.trim() } else { "" }; + let value = lines.get(i + 1).map(|v| v.trim()).unwrap_or(""); + if code == "0" { + kind = value.to_string(); + } + if code == "2" && kind == "BLOCK" { + block = value.to_string(); + } + if i % 2 == 1 && kind == "MTEXT" && block == "*D1" && lines.get(i - 1).map(|c| c.trim()) == Some("1") && l.trim().starts_with("100") { + out.push("99.5".to_string()); + changed += 1; + continue; + } + out.push(l.to_string()); + } + assert_eq!(changed, 1, "the fixture found the *D1 text"); + let mut s = session(); + s.open_drawing(cadcraft_io::read_dxf(out.join("\n").as_bytes()).unwrap(), "stale", None); + s +} + +/// The issue's use: a routine reads the text a dimension's block stores and compares it with the +/// measurement, so it finds the stale block. +#[test] +fn a_routine_reads_the_text_a_dimension_block_stores() { + let mut s = stale_dimension(); + let code = r#"(progn + (defun dtext (dim / e txt) + (setq e (cdr (assoc -2 (tblsearch "BLOCK" (cdr (assoc 2 (entget dim))))))) + (while e + (if (= (cdr (assoc 0 (entget e))) "MTEXT") (setq txt (cdr (assoc 1 (entget e))))) + (setq e (entnext e))) + txt) + (setq dim (entlast)) + (list (dtext dim) (cdr (assoc 42 (entget dim)))))"#; + assert_eq!(ok(&mut s, code), "(\"99.5\" 100.0)"); +} + +/// tblsearch names a block's first entity (-2); entnext walks the definition in order and ends +/// with nil; entget lists each one (without a layout); handent finds them by handle. +#[test] +fn entnext_and_entget_walk_a_block_definition() { + let mut s = session(); + let l = s.execute("line", &serde_json::json!({"points": [[0, 0], [10, 0]]})).unwrap()["handles"][0].as_str().unwrap().to_string(); + let c = s.execute("circle", &serde_json::json!({"center": [5, 5], "radius": 2})).unwrap()["handle"].as_str().unwrap().to_string(); + s.execute("block", &serde_json::json!({"name": "B", "base": [0, 0], "handles": [l, c], "keep": "delete"})).unwrap(); + s.execute("line", &serde_json::json!({"points": [[50, 0], [60, 0]]})).unwrap(); + let walk = r#"(progn (setq e (cdr (assoc -2 (tblsearch "BLOCK" "B"))) types nil) + (while e (setq types (cons (cdr (assoc 0 (entget e))) types) e (entnext e))) + (reverse types))"#; + assert_eq!(ok(&mut s, walk), "(\"LINE\" \"CIRCLE\")", "the block's own entities, not the drawing's"); + let first = "(cdr (assoc -2 (tblsearch \"BLOCK\" \"B\")))"; + assert_eq!(ok(&mut s, &format!("(assoc 410 (entget {first}))")), "nil", "no layout"); + assert_eq!(ok(&mut s, &format!("(equal (handent (cdr (assoc 5 (entget {first})))) {first})")), "T"); + // The drawing's own entities walk as before. + assert_eq!(ok(&mut s, "(cdr (assoc 0 (entget (entnext))))"), "\"LINE\""); + assert_eq!(ok(&mut s, "(entnext (entlast))"), "nil"); + // Editing a block's entity through entmod or entdel isn't offered: nil, nothing changes. + assert_eq!(ok(&mut s, &format!("(entdel {first})")), "nil"); + assert_eq!(ok(&mut s, &format!("(entmod (entget {first}))")), "nil"); + assert_eq!(s.doc().unwrap().block("B").unwrap().entities.len(), 2); +} + +#[test] +fn an_empty_block_has_no_first_entity() { + let mut s = session(); + s.doc_mut().unwrap().blocks.insert("EMPTY".into(), std::sync::Arc::new(cadcraft_doc::Block::new("EMPTY"))); + assert_eq!(ok(&mut s, "(assoc -2 (tblsearch \"BLOCK\" \"EMPTY\"))"), "nil"); +} diff --git a/docs/autolisp.md b/docs/autolisp.md index e26702c2c..811c44bf9 100644 --- a/docs/autolisp.md +++ b/docs/autolisp.md @@ -82,6 +82,11 @@ DXF file holds for the object, plus `-1` (its name) and `410` (its layout). Angl are in radians, as in AutoCAD, where the file holds degrees; `entmake`, `entmod` and `ssget` filters take radians too. +Block definitions can be read: `(tblsearch "BLOCK" name)` gives the block's first entity in group +-2, `entnext` goes from one entity of a definition to the next (nil after the last) and `entget` +lists them (without a layout, group 410). So a routine can read what a block stores, such as the +text of a dimension's `*D` block. `entmod` and `entdel` don't change a definition's entities. + ## Testing against the corpus The AutoLISP conformance corpus (#648) lives in its own repository. Point `CADCRAFT_LISP_CORPUS` From 9f7117036845dda3e88468a74f7cf5980583b277 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Tue, 27 Oct 2026 19:45:51 +1100 Subject: [PATCH 2/5] Parity checklist: regenerated after merging main --- docs/parity-checklist.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index 9c2186c90..10c1b14a1 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -1,6 +1,6 @@ # Parity checklist -> **Generated:** 2026-10-25 by `cargo xtask parity`. Do not edit by hand. +> **Generated:** 2026-10-27 by `cargo xtask parity`. Do not edit by hand. Menu-breadth parity: a reference menu item counts as covered when a CADCraft command is registered under the same menu label. It measures breadth, not depth; see [target-app-parity.md](target-app-parity.md) for the weighted assessment. From 744e99cafd3401fd53521014cc90831093fda9c7 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Wed, 28 Oct 2026 18:50:56 +1100 Subject: [PATCH 3/5] lisp: entnext through block entities uses a cached successor index Replaces the per-call scan of every block (containment) and of the block's entities (successor) with a per-drawing hash map built once, invalidated when ENDBLK redefines a block. --- crates/engine/src/lisp.rs | 4 ++++ crates/engine/src/lisp/block_tests.rs | 19 +++++++++++++++ crates/engine/src/lisp/drawing.rs | 33 +++++++++++++++++++++++---- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/lisp.rs b/crates/engine/src/lisp.rs index b3175c512..24afd4c32 100644 --- a/crates/engine/src/lisp.rs +++ b/crates/engine/src/lisp.rs @@ -56,6 +56,9 @@ pub struct Env { pub deleted: HashMap)>, /// Every entity's handle in creation order, for the drawing it was taken from (`entnext`). pub order: Option<(usize, Arc>)>, + /// Each block-definition entity's successor in its block (None for the last), for the drawing + /// it was taken from: `entnext`'s indexed lookup, which also tells whether a handle is a block's. + pub block_next: Option<(usize, Arc>>)>, /// `tblnext`'s place in each table. pub tblnext: HashMap, /// A block definition `entmake` has begun (a BLOCK list) and not yet ended (ENDBLK): the @@ -77,6 +80,7 @@ impl Default for Env { next_file: 1, deleted: HashMap::new(), order: None, + block_next: None, tblnext: HashMap::new(), pending_block: None, printed_at: None, diff --git a/crates/engine/src/lisp/block_tests.rs b/crates/engine/src/lisp/block_tests.rs index 6e85a22ef..1c7589410 100644 --- a/crates/engine/src/lisp/block_tests.rs +++ b/crates/engine/src/lisp/block_tests.rs @@ -234,3 +234,22 @@ fn a_definition_that_isnt_added_leaves_no_layer() { assert_eq!(ok(&mut s, ENDBLK), "\"EB\""); assert!(has(&s, "PARTS"), "the block's layer exists once it is added"); } + +/// entnext walks entmade block entities in order through the cached successor index, and a +/// redefinition (same drawing, same block name) is not served from the stale index. +#[test] +fn entnext_walks_entmade_blocks_and_sees_a_redefinition() { + let mut s = session(); + let walk = r#"(progn (setq e (cdr (assoc -2 (tblsearch "BLOCK" "EB"))) types nil) + (while e (setq types (cons (cdr (assoc 0 (entget e))) types) e (entnext e))) + (reverse types))"#; + for code in [BLOCK_EB, LINE, CIRCLE, ENDBLK] { + ok(&mut s, code); + } + assert_eq!(ok(&mut s, walk), "(\"LINE\" \"CIRCLE\")"); + assert_eq!(ok(&mut s, walk), "(\"LINE\" \"CIRCLE\")", "second walk from the cache"); + for code in [BLOCK_EB, CIRCLE, LINE, LINE, ENDBLK] { + ok(&mut s, code); + } + assert_eq!(ok(&mut s, walk), "(\"CIRCLE\" \"LINE\" \"LINE\")"); +} diff --git a/crates/engine/src/lisp/drawing.rs b/crates/engine/src/lisp/drawing.rs index e94fb8cc3..1d330532a 100644 --- a/crates/engine/src/lisp/drawing.rs +++ b/crates/engine/src/lisp/drawing.rs @@ -7,6 +7,7 @@ //! [`crate::cmd::file::entity_codec`]), so what `entget` shows is what a DXF file holds, except //! for angles, which are radians in a list and degrees in a file. +use std::collections::HashMap; use std::sync::{Arc, Mutex}; use cadcraft_doc::{Entity, Handle, Space}; @@ -461,6 +462,7 @@ fn end_block(ctx: &mut Ctx<'_>) -> Value { } let name = b.name.clone(); d.blocks.insert(name.clone(), Arc::new(b)); + ctx.env.block_next = None; ctx.s.touch(); Value::str(&name) } @@ -545,6 +547,29 @@ fn handles_in_order(ctx: &mut Ctx<'_>) -> Arc> { v } +/// Every block-definition entity's successor within its block, cached per drawing like +/// [`handles_in_order`]: one pass over the blocks, then `entnext` is a hash lookup instead of a +/// scan of the block (and of every block, to find which holds the entity). +fn block_successors(ctx: &mut Ctx<'_>) -> Arc>> { + let Ok(st) = ctx.s.state() else { return Arc::default() }; + let key = Arc::as_ptr(&st.doc) as usize; + if let Some((k, v)) = &ctx.env.block_next + && *k == key + { + return v.clone(); + } + let mut m = HashMap::new(); + for b in st.doc.blocks.values() { + let mut it = b.entities.iter().map(|e| e.handle).peekable(); + while let Some(h) = it.next() { + m.insert(h, it.peek().copied()); + } + } + let m = Arc::new(m); + ctx.env.block_next = Some((key, m.clone())); + m +} + pub fn entlast(ctx: &mut Ctx<'_>, a: Vec) -> Result { arity(&a, 0, 0)?; Ok(Flow::Value(handles_in_order(ctx).last().map(|h| Value::Ename(*h)).unwrap_or_default())) @@ -555,12 +580,10 @@ pub fn entnext(ctx: &mut Ctx<'_>, a: Vec) -> Result { // From an entity of a block definition: the next one in that block, nil after the last (#1700). if let Some(v) = a.first().filter(|v| !v.is_nil()) { let h = ename(v)?; - if let Ok(d) = ctx.s.doc() - && d.entity(h).is_none() - && let Some(b) = block_of(d, h) + if ctx.s.doc().is_ok_and(|d| d.entity(h).is_none()) + && let Some(next) = block_successors(ctx).get(&h) { - let next = b.entities.iter().skip_while(|e| e.handle != h).nth(1).map(|e| Value::Ename(e.handle)); - return Ok(Flow::Value(next.unwrap_or_default())); + return Ok(Flow::Value(next.map(Value::Ename).unwrap_or_default())); } } let order = handles_in_order(ctx); From 853ca574bdb9d51e55955941b542864426abf1b5 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Wed, 28 Oct 2026 23:23:09 +1100 Subject: [PATCH 4/5] lisp: entget and handent on block entities use the cached block index The per-drawing index now maps each block entity handle to the entity and its successor, so entget/handent/entnext are hash lookups instead of scanning every block. Test: entget_and_handent_reach_every_block_entity_also_after_a_redefinition. --- crates/engine/src/lisp.rs | 7 ++-- crates/engine/src/lisp/block_tests.rs | 18 ++++++++++ crates/engine/src/lisp/drawing.rs | 48 ++++++++++++--------------- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/crates/engine/src/lisp.rs b/crates/engine/src/lisp.rs index 24afd4c32..d1d305e7e 100644 --- a/crates/engine/src/lisp.rs +++ b/crates/engine/src/lisp.rs @@ -5,6 +5,9 @@ //! (`getpoint`, `pause`) and carry on later, so the interface keeps drawing while a routine //! waits. Each drawing has its own symbols, as in AutoCAD. A run is one undo step. +/// A block-definition entity and its successor in the block, by handle. +pub type BlockSlots = HashMap, Option)>; + pub mod builtins; mod drawing; pub mod eval; @@ -56,9 +59,9 @@ pub struct Env { pub deleted: HashMap)>, /// Every entity's handle in creation order, for the drawing it was taken from (`entnext`). pub order: Option<(usize, Arc>)>, - /// Each block-definition entity's successor in its block (None for the last), for the drawing + /// Each block-definition entity with its successor in its block (None for the last), for the drawing /// it was taken from: `entnext`'s indexed lookup, which also tells whether a handle is a block's. - pub block_next: Option<(usize, Arc>>)>, + pub block_next: Option<(usize, Arc)>, /// `tblnext`'s place in each table. pub tblnext: HashMap, /// A block definition `entmake` has begun (a BLOCK list) and not yet ended (ENDBLK): the diff --git a/crates/engine/src/lisp/block_tests.rs b/crates/engine/src/lisp/block_tests.rs index 1c7589410..596ed3df3 100644 --- a/crates/engine/src/lisp/block_tests.rs +++ b/crates/engine/src/lisp/block_tests.rs @@ -253,3 +253,21 @@ fn entnext_walks_entmade_blocks_and_sees_a_redefinition() { } assert_eq!(ok(&mut s, walk), "(\"CIRCLE\" \"LINE\" \"LINE\")"); } + +/// entget and handent reach every entity of an entmade block through the cached index, and a +/// redefinition shows the new entities, not the old ones. +#[test] +fn entget_and_handent_reach_every_block_entity_also_after_a_redefinition() { + let mut s = session(); + let walk = r#"(progn (setq e (cdr (assoc -2 (tblsearch "BLOCK" "EB"))) r nil) + (while e (setq r (cons (list (cdr (assoc 0 (entget e))) (equal (handent (cdr (assoc 5 (entget e)))) e)) r) e (entnext e))) + (reverse r))"#; + for code in [BLOCK_EB, LINE, CIRCLE, LINE, ENDBLK] { + ok(&mut s, code); + } + assert_eq!(ok(&mut s, walk), "((\"LINE\" T) (\"CIRCLE\" T) (\"LINE\" T))"); + for code in [BLOCK_EB, CIRCLE, ENDBLK] { + ok(&mut s, code); + } + assert_eq!(ok(&mut s, walk), "((\"CIRCLE\" T))"); +} diff --git a/crates/engine/src/lisp/drawing.rs b/crates/engine/src/lisp/drawing.rs index 1d330532a..d5c5810bc 100644 --- a/crates/engine/src/lisp/drawing.rs +++ b/crates/engine/src/lisp/drawing.rs @@ -8,16 +8,17 @@ //! for angles, which are radians in a list and degrees in a file. use std::collections::HashMap; + use std::sync::{Arc, Mutex}; use cadcraft_doc::{Entity, Handle, Space}; use cadcraft_dxf::Tag; use cadcraft_geom::Vec2; -use super::LispError; use super::builtins::args_err; use super::eval::{Ctx, Flow, Need}; use super::value::{PickSet, Value, to_text}; +use super::{BlockSlots, LispError}; use crate::{Input, Session}; fn arity(a: &[Value], min: usize, max: usize) -> Result<(), LispError> { @@ -318,14 +319,14 @@ fn space_of(d: &cadcraft_doc::Drawing, h: Handle) -> Option { pub fn entget(ctx: &mut Ctx<'_>, a: Vec) -> Result { arity(&a, 1, 2)?; let h = ename(&a[0])?; + let drawn = ctx.s.doc().is_ok_and(|d| d.entity(h).is_some()); + let from_block = if drawn { None } else { block_entity(ctx, h) }; let Ok(d) = ctx.s.doc() else { return Ok(Flow::Value(Value::Nil)) }; // An entity of a block definition (#1700) has no layout. - let (e, in_block) = match d.entity(h) { - Some(e) => (e, false), - None => match block_entity(d, h) { - Some(e) => (e, true), - None => return Ok(Flow::Value(Value::Nil)), - }, + let (e, in_block) = match (d.entity(h), &from_block) { + (Some(e), _) => (e, false), + (None, Some(e)) => (e, true), + (None, None) => return Ok(Flow::Value(Value::Nil)), }; let (kind, mut tags) = (codec()?.groups)(d, e); convert_angles(&kind, &mut tags, f64::to_radians); @@ -547,10 +548,11 @@ fn handles_in_order(ctx: &mut Ctx<'_>) -> Arc> { v } -/// Every block-definition entity's successor within its block, cached per drawing like -/// [`handles_in_order`]: one pass over the blocks, then `entnext` is a hash lookup instead of a -/// scan of the block (and of every block, to find which holds the entity). -fn block_successors(ctx: &mut Ctx<'_>) -> Arc>> { +/// Every block-definition entity with its successor within its block, cached per drawing like +/// [`handles_in_order`]: one pass over the blocks, then `entnext`, `entget` and `handent` on a +/// block's entity are hash lookups instead of scans of the block (and of every block, to find +/// which holds the entity). +fn block_slots(ctx: &mut Ctx<'_>) -> Arc { let Ok(st) = ctx.s.state() else { return Arc::default() }; let key = Arc::as_ptr(&st.doc) as usize; if let Some((k, v)) = &ctx.env.block_next @@ -560,9 +562,9 @@ fn block_successors(ctx: &mut Ctx<'_>) -> Arc>> { } let mut m = HashMap::new(); for b in st.doc.blocks.values() { - let mut it = b.entities.iter().map(|e| e.handle).peekable(); - while let Some(h) = it.next() { - m.insert(h, it.peek().copied()); + let mut it = b.entities.iter().peekable(); + while let Some(e) = it.next() { + m.insert(e.handle, (e.clone(), it.peek().map(|n| n.handle))); } } let m = Arc::new(m); @@ -581,7 +583,7 @@ pub fn entnext(ctx: &mut Ctx<'_>, a: Vec) -> Result { if let Some(v) = a.first().filter(|v| !v.is_nil()) { let h = ename(v)?; if ctx.s.doc().is_ok_and(|d| d.entity(h).is_none()) - && let Some(next) = block_successors(ctx).get(&h) + && let Some((_, next)) = block_slots(ctx).get(&h) { return Ok(Flow::Value(next.map(Value::Ename).unwrap_or_default())); } @@ -601,21 +603,13 @@ pub fn entnext(ctx: &mut Ctx<'_>, a: Vec) -> Result { pub fn handent(ctx: &mut Ctx<'_>, a: Vec) -> Result { arity(&a, 1, 1)?; let s = string(&a[0])?; - let h = Handle::parse_hex(&s).filter(|h| ctx.s.doc().is_ok_and(|d| d.entity(*h).is_some() || block_entity(d, *h).is_some())); + let h = Handle::parse_hex(&s).filter(|h| ctx.s.doc().is_ok_and(|d| d.entity(*h).is_some()) || block_entity(ctx, *h).is_some()); Ok(Flow::Value(h.map(Value::Ename).unwrap_or_default())) } -/// The block definition holding entity `h`, when it is one of a block's (not a drawn entity). -fn block_of(d: &cadcraft_doc::Drawing, h: Handle) -> Option<&cadcraft_doc::Block> { - if h == Handle(0) { - return None; - } - d.blocks.values().map(|b| b.as_ref()).find(|b| b.entities.get(h).is_some()) -} - -/// Entity `h` of a block definition (#1700). -fn block_entity(d: &cadcraft_doc::Drawing, h: Handle) -> Option<&Arc> { - block_of(d, h).and_then(|b| b.entities.get(h)) +/// Entity `h` of a block definition (#1700), by the cached index. +fn block_entity(ctx: &mut Ctx<'_>, h: Handle) -> Option> { + block_slots(ctx).get(&h).map(|(e, _)| e.clone()) } // ---------------- selection sets ---------------- From 7f590216eb5bca3adb6ef4f1500bc89c71445f74 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Fri, 30 Oct 2026 13:34:46 +1100 Subject: [PATCH 5/5] Parity checklist: regenerated after merging main --- docs/parity-checklist.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index d0bd6f967..53ffffc96 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -1,7 +1,6 @@ # Parity checklist -> **Generated:** 2026-10-29 by `cargo xtask parity`. Do not edit by hand. -> **Generated:** 2026-10-28 by `cargo xtask parity`. Do not edit by hand. +> **Generated:** 2026-10-30 by `cargo xtask parity`. Do not edit by hand. Menu-breadth parity: a reference menu item counts as covered when a CADCraft command is registered under the same menu label. It measures breadth, not depth; see [target-app-parity.md](target-app-parity.md) for the weighted assessment.