Repository navigation
Dynamic blocks: lookup parameters, lookup actions and lookup tables (BPARAMETER Lookup, BACTION, BLOOKUPTABLE) (stacked on #2169) #2184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
codyvogt
wants to merge
78
commits into
storytold:main
Choose a base branch
from
codyvogt:feat/dynblock-author-lookup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+7,744
−50
Open
Changes from all commits
Commits
Show all changes
78 commits
Select commit
Hold shift + click to select a range
3eb74db
Dynamic blocks: author linear and point parameters with stretch and m…
codyvogt 65d2b46
Docs: F3 and the blocks row list the dynamic-block authoring that landed
codyvogt d30a082
i18n(uk): Parameter and Action command labels
codyvogt 64c0cf2
Dynamic blocks: a linear parameter between points infinitely far apar…
codyvogt 27ce8f7
Dynamic blocks: a grip's location components name their own parameter
codyvogt c4c5339
Block Editor: the marks cache id uses egui 0.37's Id::unique
codyvogt 4da3598
Dynamic blocks: BSAVEAS gives a dynamic block's copy its own graph
codyvogt 60eaa87
Dynamic blocks: BSAVEAS leaves the edited block as it was and the edi…
codyvogt fc204c2
BSAVEAS of a dynamic block: the copy starts a new Block Editor history
codyvogt d777e8b
Dynamic blocks: author visibility parameters and states (BPARAMETER V…
codyvogt aa953e5
Docs: F3 and the blocks row list visibility authoring
codyvogt 9627ade
Dynamic blocks: the Visibility States dialog and Object Visibility on…
codyvogt 879689b
Dynamic blocks: a Visibility Mode button in the Block Editor, and BVM…
codyvogt f9743cb
Dynamic blocks: the Block Editor draws a visibility or lookup paramet…
codyvogt a4d84e2
Dynamic blocks: pick, move and erase parameters and actions in the Bl…
codyvogt f1bb35e
Visibility States dialogs: window ids use egui 0.37's Id::unique
codyvogt 3c344a4
Dynamic blocks: erasing keeps edge flags; MOVE leaves an unmovable pa…
codyvogt 69eaaad
Dynamic blocks: a linear parameter's lone end grip moves with it; not…
codyvogt c049412
Dynamic blocks: picked elements follow PICKFIRST, JSON handles and wi…
codyvogt 513d024
Block Editor: the visibility state list uses craft-ui's ComboBox
codyvogt 9884b5c
Dynamic blocks: author lookup parameters, lookup actions and lookup t…
codyvogt db3e5ac
Dynamic blocks: the Property Lookup Table dialog (BLOOKUPTABLE, or do…
codyvogt dfd0a21
Docs: F3 and the blocks row list lookup authoring
codyvogt 36856fa
Dynamic blocks: a lookup on a position follows its point; BEDIT names…
codyvogt d6991a1
Merge storytold/cadcraft main into feat/dynblock-author-vis
codyvogt 8978feb
Merge storytold/cadcraft main into feat/dynblock-author-vis
codyvogt 1f4aba9
i18n: Chinese for the Block Editor's visibility and parameter labels
codyvogt fc0cb7a
Docs: leave the parity docs to the maintainers (AGENTS.md: PRs don't …
codyvogt ebfd8f2
Docs: leave the parity docs to the maintainers (AGENTS.md: PRs don't …
codyvogt 9131053
Docs: leave the parity docs to the maintainers (AGENTS.md: PRs don't …
codyvogt 9f2d5a9
Merge storytold/cadcraft main into feat/dynblock-author-vis
codyvogt 3424934
Merge storytold/cadcraft main into feat/dynblock-author-vis
codyvogt d48a2fd
Merge storytold/cadcraft main into feat/dynblock-author-vis
codyvogt 8be0801
Merge storytold/cadcraft main into feat/dynblock-author-vis
codyvogt fab5044
Merge feat/dynblock-author-vis (#2169, with current main) into feat/d…
codyvogt 375b9d6
Merge storytold/cadcraft main into feat/dynblock-author-lookup
codyvogt 49cac9e
Dynamic blocks: lookup tables are written all or nothing, compared by…
codyvogt 9c0a904
Merge storytold/cadcraft main into feat/dynblock-author
codyvogt 850eeb1
Dynamic blocks: BSAVEAS refuses a block too big to copy whole
codyvogt cddb0d6
Merge storytold/cadcraft main into feat/dynblock-author-vis
codyvogt dbf8a73
Dynamic blocks: BSAVEAS refuses a block too big to copy whole
codyvogt 5895689
i18n: Chinese for the Lookup Table command
codyvogt 32e6291
Merge feat/dynblock-author-vis (#2169: current main, BSAVEAS copy cap…
codyvogt c6b4e4c
i18n: Chinese for the Parameter and Action commands
codyvogt f1c037b
Merge remote-tracking branch 'upstream/main' into feat/dynblock-autho…
codyvogt 1b3bd1c
Merge branch 'feat/dynblock-author-vis' into feat/dynblock-author-lookup
codyvogt 9cc7092
Merge remote-tracking branch 'upstream/main' into feat/dynblock-author
codyvogt 918af22
File access: BPARAMETER and BACTION touch no file
codyvogt 58b57c9
Merge branch 'feat/dynblock-author' into feat/dynblock-author-vis
codyvogt f172a7e
Merge branch 'feat/dynblock-author-vis' into feat/dynblock-author-lookup
codyvogt 681ee73
File access: BLOOKUPTABLE touches no file
codyvogt 351e9a8
fmt
codyvogt 522ffa1
Merge branch 'feat/dynblock-author-vis' into feat/dynblock-author-lookup
codyvogt 0d54bc2
Merge remote-tracking branch 'upstream/main' into feat/dynblock-autho…
codyvogt e491827
Merge branch 'feat/dynblock-author-vis' into feat/dynblock-author-lookup
codyvogt cfe1d8b
Merge remote-tracking branch 'upstream/main' into feat/dynblock-author
codyvogt 3695610
Merge branch 'feat/dynblock-author' into feat/dynblock-author-vis
codyvogt 69a4e81
Merge branch 'feat/dynblock-author-vis' into feat/dynblock-author-lookup
codyvogt 0bbb7f5
Dynamic blocks: lookups follow main's evaluation (drop this branch's …
codyvogt 230b76a
Merge remote-tracking branch 'upstream/main' into feat/dynblock-author
codyvogt b9acb9e
Merge branch 'feat/dynblock-author' into feat/dynblock-author-vis
codyvogt 2c64172
Merge branch 'feat/dynblock-author-vis' into feat/dynblock-author-lookup
codyvogt c0c4a35
Block Editor: egui's ComboBox, as main's craft-ui port uses
codyvogt d1e8a3a
Merge branch 'feat/dynblock-author-vis' into feat/dynblock-author-lookup
codyvogt 349e3b6
Lookup tables: egui's ComboBox, as main's craft-ui port uses
codyvogt fc42c33
Translations to craft-i18n, tests into the crates' it binaries (AGENT…
codyvogt 8e49252
Merge branch 'feat/dynblock-author' into feat/dynblock-author-vis
codyvogt 6ee02e9
Translations to craft-i18n, tests into the engine's it binary (AGENTS…
codyvogt d1eeac8
Merge branch 'feat/dynblock-author-vis' into feat/dynblock-author-lookup
codyvogt adc0848
Translations to craft-i18n, tests into the engine's it binary (AGENTS…
codyvogt 8906452
Lookup tables: switching actions keeps each table's edits, and OK wri…
codyvogt 218faa7
Translations: the Chinese labels of BPARAMETER and BACTION stay, as t…
codyvogt 619341d
Merge branch 'feat/dynblock-author' into feat/dynblock-author-vis
codyvogt 0917bbf
Translations: the Chinese labels the #1129 coverage test requires stay
codyvogt a761e99
Merge branch 'feat/dynblock-author-vis' into feat/dynblock-author-lookup
codyvogt 5d38e19
Translations: the Chinese label of BLOOKUPTABLE stays, as the #1129 c…
codyvogt 947764e
Lookup tables: OK writes every edited table as one undo step
codyvogt d6bf0f1
Merge main into feat/dynblock-author-lookup
codyvogt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| //! Saving a dynamic block under another name (BSAVEAS): the copy gets its own evaluation graph, | ||
| //! made from the original's objects with new handles, and its own copies of the block's objects | ||
| //! (no two blocks share a handle), so references to either change as AutoCAD would change them. | ||
|
|
||
| use std::collections::{HashMap, HashSet}; | ||
|
|
||
| use crate::foreign::{ForeignExtra, ForeignObject}; | ||
| use crate::{Drawing, EntityKind, EntityStore, Handle, RawTag}; | ||
|
|
||
| use super::write::{ETAG, dict_entry, guid, record_handle, strip_app}; | ||
| use super::{MAX_NODES, dynamic_block}; | ||
|
|
||
| /// Most objects of one block's dynamic data that are copied. | ||
| const MAX_COPIED: usize = 8 * MAX_NODES; | ||
|
|
||
| /// Group codes whose values are handles: the object's own (5), owners and pointers (330 to 369) | ||
| /// and 390 to 399. | ||
| fn is_handle_code(code: i32) -> bool { | ||
| code == 5 || (330..=369).contains(&code) || (390..=399).contains(&code) | ||
| } | ||
|
|
||
| /// The owner of an object: its first 330 group outside the `{ACAD_REACTORS` ... `}` groups. | ||
| fn owner_of(o: &ForeignObject) -> Option<Handle> { | ||
| let mut inside = false; | ||
| for t in &o.tags { | ||
| match t.code { | ||
| 102 => inside = t.value.starts_with('{'), | ||
| 330 if !inside => return Handle::parse_hex(&t.value), | ||
| _ => {} | ||
| } | ||
| } | ||
| None | ||
| } | ||
|
|
||
| /// `root` and the objects it owns, directly or not (at most [`MAX_COPIED`]). | ||
| fn owned_subtree(d: &Drawing, root: Handle) -> Vec<Handle> { | ||
| let mut children: HashMap<Handle, Vec<Handle>> = HashMap::new(); | ||
| for o in &d.foreign.objects { | ||
| if let Some(owner) = owner_of(o) { | ||
| children.entry(owner).or_default().push(o.handle); | ||
| } | ||
| } | ||
| let mut out = vec![root]; | ||
| let mut seen: HashSet<Handle> = HashSet::from([root]); | ||
| let mut at = 0; | ||
| while let Some(h) = out.get(at).copied() { | ||
| at += 1; | ||
| for c in children.get(&h).into_iter().flatten() { | ||
| if out.len() < MAX_COPIED && seen.insert(*c) { | ||
| out.push(*c); | ||
| } | ||
| } | ||
| } | ||
| out | ||
| } | ||
|
|
||
| /// [`owned_subtree`], refused when it reaches [`MAX_COPIED`]: a copy of only part of it would keep | ||
| /// pointing at the original's objects. | ||
| fn whole_subtree(d: &Drawing, root: Handle) -> Result<Vec<Handle>, String> { | ||
| let out = owned_subtree(d, root); | ||
| if out.len() >= MAX_COPIED { | ||
| return Err(format!("The dynamic block has {MAX_COPIED} or more objects; it can't be copied.")); | ||
| } | ||
| Ok(out) | ||
| } | ||
|
|
||
| /// The handle `h` names, as `map` has it. | ||
| fn mapped(map: &HashMap<Handle, Handle>, value: &str) -> Option<Handle> { | ||
| Handle::parse_hex(value).and_then(|h| map.get(&h)).copied() | ||
| } | ||
|
|
||
| /// Every handle in `tags` that `map` has, as the new one. | ||
| fn retarget(tags: &mut [RawTag], map: &HashMap<Handle, Handle>) { | ||
| for t in tags.iter_mut().filter(|t| is_handle_code(t.code)) { | ||
| if let Some(n) = mapped(map, &t.value) { | ||
| t.value = n.hex(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// The objects of `store` again with new handles (added to `map`). | ||
| fn rehandle(d: &mut Drawing, store: &EntityStore, map: &mut HashMap<Handle, Handle>) -> EntityStore { | ||
| for e in store.iter() { | ||
| let n = d.new_handle(); | ||
| map.insert(e.handle, n); | ||
| } | ||
| remap_handles(store, map) | ||
| } | ||
|
|
||
| /// The objects of `store` again with the handles `map` gives them: each one's element tag names | ||
| /// the object itself, and a hatch's sources are the new copies. | ||
| pub fn remap_handles(store: &EntityStore, map: &HashMap<Handle, Handle>) -> EntityStore { | ||
| let mut out = EntityStore::new(); | ||
| for e in store.iter() { | ||
| let mut c = (**e).clone(); | ||
| c.handle = map.get(&e.handle).copied().unwrap_or(e.handle); | ||
| for t in c.common.xdata.iter_mut().filter(|t| t.code == 1005) { | ||
| if let Some(n) = mapped(map, &t.value) { | ||
| t.value = n.hex(); | ||
| } | ||
| } | ||
| if let EntityKind::Hatch(h) = &mut c.kind { | ||
| for l in &mut h.loops { | ||
| l.sources = l.sources.iter().map(|s| map.get(s).copied().unwrap_or(*s)).collect(); | ||
| } | ||
| } | ||
| out.push(c); | ||
| } | ||
| out | ||
| } | ||
|
|
||
| /// The kept extras of the objects of `store` (reactors within the block; not their extension | ||
| /// dictionaries), for their new handles. | ||
| fn entity_extras(d: &Drawing, store: &EntityStore, map: &HashMap<Handle, Handle>) -> Vec<(Handle, ForeignExtra)> { | ||
| store | ||
| .iter() | ||
| .filter_map(|e| { | ||
| let mut x = d.foreign.entities.get(&e.handle)?.clone(); | ||
| x.xdict = None; | ||
| x.reactors = x.reactors.iter().filter_map(|r| map.get(r).copied()).collect(); | ||
| Some((map.get(&e.handle).copied()?, x)) | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| /// The applications whose record xdata make a block dynamic. | ||
| const DYNAMIC_APPS: [&str; 3] = ["AcDbDynamicBlockTrueName", "AcDbDynamicBlockGUID", ETAG]; | ||
|
|
||
| /// Block `name`'s (upper case) dynamic data removed: the objects its extension dictionary owns | ||
| /// and its record's dynamic data. What a redefined block's old graph would point at is gone. | ||
| fn remove_definition(d: &mut Drawing, name: &str) { | ||
| let key = ("BLOCK_RECORD".to_string(), name.to_string()); | ||
| let Some(xdict) = d.foreign.records.get(&key).and_then(|x| x.xdict) else { return }; | ||
| let owned: HashSet<Handle> = owned_subtree(d, xdict).into_iter().collect(); | ||
| let f = std::sync::Arc::make_mut(&mut d.foreign); | ||
| f.objects.retain(|o| !owned.contains(&o.handle)); | ||
| let empty = f.records.get_mut(&key).is_some_and(|x| { | ||
| x.xdict = None; | ||
| DYNAMIC_APPS.iter().for_each(|app| strip_app(&mut x.xdata, app)); | ||
| x.is_empty() | ||
| }); | ||
| if empty { | ||
| f.records.remove(&key); | ||
| } | ||
| } | ||
|
|
||
| /// Block `name`'s dynamic data in `d` as `host` has it (BSAVEAS leaves the edited block as it | ||
| /// was: the editor's changes to its graph belong to the copy only). | ||
| pub fn restore_definition(d: &mut Drawing, host: &Drawing, name: &str) { | ||
| let name = name.trim().to_ascii_uppercase(); | ||
| remove_definition(d, &name); | ||
| let key = ("BLOCK_RECORD".to_string(), name); | ||
| let Some(record) = host.foreign.records.get(&key) else { return }; | ||
| let owned: HashSet<Handle> = record.xdict.map(|x| owned_subtree(host, x)).unwrap_or_default().into_iter().collect(); | ||
| let objects: Vec<ForeignObject> = host.foreign.objects.iter().filter(|o| owned.contains(&o.handle)).cloned().collect(); | ||
| let f = std::sync::Arc::make_mut(&mut d.foreign); | ||
| f.objects.extend(objects); | ||
| f.records.insert(key, record.clone()); | ||
| } | ||
|
|
||
| /// Block record xdata `xdata` with the dynamic block's true name and GUID replaced. | ||
| fn rename_in_xdata(xdata: &mut [RawTag], name: &str, guid: &str) { | ||
| let mut app = String::new(); | ||
| for t in xdata { | ||
| match t.code { | ||
| 1001 => app = t.value.to_ascii_uppercase(), | ||
| 1000 if app == "ACDBDYNAMICBLOCKTRUENAME" => t.value = name.to_string(), | ||
| 1000 if app == "ACDBDYNAMICBLOCKGUID" => t.value = guid.to_string(), | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Make block `to` a copy of dynamic block `from` (BSAVEAS): `store`, the objects it is saved | ||
| /// with, get new handles; the copy has its own evaluation graph and objects, made from the | ||
| /// original's with new handles, and its own true name and GUID. What `to` had of dynamic data goes | ||
| /// first (a block replaced by one that isn't dynamic keeps none). Returns the handles the objects | ||
| /// of `store` got, by their old ones; nothing changes when `from` has no dynamic data or is `to`. | ||
| /// Refused, changing nothing, when `from` has too many objects to copy whole. | ||
| pub fn copy_definition(d: &mut Drawing, from: &str, to: &str, store: &mut EntityStore) -> Result<HashMap<Handle, Handle>, String> { | ||
| let (from_name, to_name) = (from.trim().to_ascii_uppercase(), to.trim().to_ascii_uppercase()); | ||
| let key = |n: &str| ("BLOCK_RECORD".to_string(), n.to_string()); | ||
| if from_name == to_name { | ||
| return Ok(HashMap::new()); | ||
| } | ||
| let source = d.foreign.records.get(&key(&from_name)).cloned(); | ||
| let owned = match source.as_ref().and_then(|s| s.xdict) { | ||
| Some(xdict) if dynamic_block(d, from).is_some() => Some(whole_subtree(d, xdict)?), | ||
| _ => None, | ||
| }; | ||
| remove_definition(d, &to_name); | ||
| let (Some(source), Some(owned)) = (source, owned) else { return Ok(HashMap::new()) }; | ||
| let Some(xdict) = source.xdict else { return Ok(HashMap::new()) }; | ||
| let graph = dict_entry(d, xdict, "ACAD_ENHANCEDBLOCK"); | ||
| let owned_set: HashSet<Handle> = owned.iter().copied().collect(); | ||
| let mut map: HashMap<Handle, Handle> = owned.iter().map(|h| (*h, d.new_handle())).collect(); | ||
| let old_record = d.foreign.targets.iter().find(|(_, (k, n))| k == "BLOCK_RECORD" && *n == from_name).map(|(h, _)| *h); | ||
| let new_record = record_handle(d, &to_name); | ||
| map.extend(old_record.map(|h| (h, new_record))); | ||
| let entities = rehandle(d, store, &mut map); | ||
| let extras = entity_extras(d, store, &map); | ||
| let copies: Vec<ForeignObject> = d | ||
| .foreign | ||
| .objects | ||
| .iter() | ||
| .filter(|o| owned_set.contains(&o.handle)) | ||
| .map(|o| { | ||
| let mut c = o.clone(); | ||
| retarget(&mut c.tags, &map); | ||
| c.handle = map.get(&o.handle).copied().unwrap_or(o.handle); | ||
| c | ||
| }) | ||
| .collect(); | ||
| let mut record = source; | ||
| record.xdict = map.get(&xdict).copied(); | ||
| let new_graph = graph.and_then(|g| map.get(&g).copied()).unwrap_or(new_record); | ||
| rename_in_xdata(&mut record.xdata, to.trim(), &guid(to.trim(), new_graph)); | ||
| let f = std::sync::Arc::make_mut(&mut d.foreign); | ||
| f.objects.extend(copies); | ||
| f.records.insert(key(&to_name), record); | ||
| f.entities.extend(extras); | ||
| let made = store.iter().filter_map(|e| Some((e.handle, *map.get(&e.handle)?))).collect(); | ||
| *store = entities; | ||
| Ok(made) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.