Colored components (feature request #2507) (#2877)

* feat: add colored components inspection toggle

* feat: persist component colors and add cycling controls

* feat: assign component colors in creation order

* fix: update component color menu integration and translations

* fix: proper description for pending format change.

* fix: deleted duplicated format change description from docs/pending.

* Compare the component colour counter in the features-only check

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Vadim Kalinsky <vadim@kalinsky.com>
Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vadim Kalinsky
2026-10-25 21:11:03 +00:00
committed by GitHub
parent 5fbe058051
commit 59cad5f0bb
27 changed files with 488 additions and 20 deletions
+13
View File
@@ -28,6 +28,9 @@ mod transfer;
mod occurrence_tests;
pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("component.cycle_color", "Cycle Component Color", cycle_color)
.icon("colored_components").doc_only()
.params("component: id or name; saves the next identification colour and enables Component Colors; shared by every instance, separate from appearances"),
CommandSpec::new("component.create", "New Component", new_component)
.at("SOLID", "ASSEMBLE")
.icon("component")
@@ -565,3 +568,13 @@ pub fn to_active_frame(s: &Session, kind: &mut FeatureKind) {
kind.transform_geometry(&inv);
}
}
fn cycle_color(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "component.cycle_color";
let id = component(s, p, "component", cmd)?;
let next = solidcraft_doc::component_color::next_color(s.doc.component_identification_color(id));
let c = s.doc_mut().components.iter_mut().find(|c| c.id == id).ok_or_else(|| bad(cmd, "choose a component below the design root"))?;
c.identification_color = Some(next);
s.colored_components = Some(());
Ok(json!({"component": id, "color": next}))
}
+1
View File
@@ -257,6 +257,7 @@ pub(super) fn paste(
if let Some(added) = scratch.doc_mut().components.iter_mut().find(|x| x.id == new) {
added.metadata = c.metadata.clone();
added.properties = c.properties.clone();
added.identification_color = c.identification_color;
}
components.insert(*old, new);
if *old != component {
+16
View File
@@ -8,6 +8,9 @@ use crate::params::{bad, bool_, str_, string_list};
use crate::{EngineError, Result, Session};
pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("inspect.colored_components", "Enable Component Colors", colored_components)
.at("SOLID", "INSPECT").icon("colored_components").noundo()
.params("enabled?: bool (omitted: toggle); temporary component colours, without changing appearances"),
CommandSpec::new("inspect.measure", "Measure", measure)
.at("SOLID", "INSPECT")
.icon("measure")
@@ -724,3 +727,16 @@ fn material_list(s: &mut Session, _p: &Value) -> Result<Value> {
.collect::<Result<Vec<_>>>()?;
Ok(json!({"materials": built_in.chain(user).collect::<Vec<_>>()}))
}
fn colored_components(s: &mut Session, p: &Value) -> Result<Value> {
if p.get("enabled").is_some_and(|v| !v.is_boolean()) {
return Err(bad("inspect.colored_components", "enabled must be a boolean"));
}
let enabled = bool_(p, "enabled").unwrap_or(s.colored_components.is_none());
if enabled && s.colored_components.is_none() {
s.colored_components = Some(());
} else if !enabled {
s.colored_components = None;
}
Ok(json!({"enabled": enabled}))
}
+6 -2
View File
@@ -215,12 +215,16 @@ impl CommandSpec {
}
pub fn info(&self, s: &Session) -> CommandInfo {
let e = (self.enabled)(s);
let (label, icon) = match self.id {
"inspect.colored_components" if s.colored_components.is_some() => ("Disable Component Colors", "colored_components_off"),
_ => (self.label, self.icon),
};
CommandInfo {
id: self.id,
label: self.label,
label,
tab: self.tab,
panel: self.panel,
icon: self.icon,
icon,
shortcut: self.shortcut,
params: self.params,
enabled: e.is_ok(),
+6
View File
@@ -327,6 +327,8 @@ pub struct Session {
pub section: Option<(Vec3, Vec3)>,
/// Whether the section's cut faces are hatched (`inspect.section`'s `hatch`; on by default).
pub section_hatch: bool,
/// Component identification colours are enabled when present; view state only.
pub colored_components: Option<()>,
/// Surface analysis shading the model (zebra, draft, curvature map).
pub analysis: Option<SurfaceAnalysis>,
/// The seams `inspect.continuity` last measured, for the viewport to shade. View state: not
@@ -491,6 +493,7 @@ impl Session {
sketch_clipboard: None,
section: None,
section_hatch: true,
colored_components: None,
analysis: None,
continuity: Vec::new(),
checkpoints: Vec::new(),
@@ -1152,6 +1155,7 @@ impl Session {
sketch_clipboard: self.sketch_clipboard.clone(),
section: None,
section_hatch: true,
colored_components: None,
analysis: None,
continuity: Vec::new(),
checkpoints: Vec::new(),
@@ -1458,6 +1462,7 @@ fn same_but_features(a: &Document, b: &Document) -> bool {
browser_order,
named_views,
visibility,
next_component_color_index,
} = a;
*render == b.render
&& *format == b.format
@@ -1491,4 +1496,5 @@ fn same_but_features(a: &Document, b: &Document) -> bool {
&& *browser_order == b.browser_order
&& *named_views == b.named_views
&& *visibility == b.visibility
&& *next_component_color_index == b.next_component_color_index
}
+14 -2
View File
@@ -348,6 +348,12 @@ pub fn sketch_point_visible(sk: &Sketch, i: usize) -> bool {
sk.points.get(i).is_some_and(|p| SketchPointVisibility::new(sk).takes(p))
}
/// Stable identification colour shared by every body of a component.
pub fn component_color(s: &Session, component: u64) -> Option<[u8; 3]> {
s.colored_components?;
Some(s.doc.component_identification_color(component))
}
/// The scene for a headless render, cut by the session's section.
pub fn scene(s: &Session, cam: &Camera) -> Scene {
scene_with_section(s, cam, s.section)
@@ -520,8 +526,14 @@ fn build_scene(
// The body's appearance (or imported colour) and faces with looks of their own, as in
// the viewport.
let to8 = |x: f32| (x.clamp(0.0, 1.0) * 255.0).round() as u8;
let color = if selected { p.body_selected } else { s.doc.body_color(body).map_or(p.body, |[r, g, b]| Rgb(to8(r), to8(g), to8(b))) };
let face_colors = if selected {
let color = if selected {
p.body_selected
} else if let Some([r, g, b]) = component_color(s, body.component) {
Rgb(r, g, b)
} else {
s.doc.body_color(body).map_or(p.body, |[r, g, b]| Rgb(to8(r), to8(g), to8(b)))
};
let face_colors = if selected || s.colored_components.is_some() {
Vec::new()
} else {
s.doc.face_colors(body).into_iter().map(|(f, l)| (f as u32, Rgb(l.color[0], l.color[1], l.color[2]))).collect()
+81
View File
@@ -189,3 +189,84 @@ fn scene_sampler_uses_exact_active_occurrence_plane_and_preserves_other_scene_co
let hidden = scene_with_sketch_sampler(&s, &cam, |_, _, _| panic!("hidden sketch sampled"));
assert!(hidden.lines.iter().all(|line| !line.on_top));
}
#[test]
fn colored_components_toggle_is_stable_and_does_not_change_the_design() {
let mut s = Session::default();
let first_id = s.doc_mut().add_component(Some("First"), 0).unwrap();
let second_id = s.doc_mut().add_component(Some("Second"), 0).unwrap();
let before = serde_json::to_value(&s.doc).unwrap();
assert_eq!(component_color(&s, first_id), None);
assert_eq!(s.execute("inspect.colored_components", &json!({})).unwrap()["enabled"], true);
s.colored_components = Some(());
let first = component_color(&s, first_id);
assert!(first.is_some());
assert_ne!(first, component_color(&s, second_id));
s.execute("inspect.colored_components", &json!({"enabled": true})).unwrap();
assert_eq!(component_color(&s, first_id), first);
assert!(s.execute("inspect.colored_components", &json!({"enabled": "yes"})).is_err());
assert_eq!(component_color(&s, first_id), first);
assert_eq!(s.execute("inspect.colored_components", &json!({})).unwrap()["enabled"], false);
assert_eq!(component_color(&s, first_id), None);
assert_eq!(serde_json::to_value(&s.doc).unwrap(), before);
}
#[test]
fn colored_components_override_appearances_and_restore_them() {
let mut s = Session::default();
for (name, x) in [("Left", 0), ("Right", 20)] {
s.execute("component.activate", &json!({"component": 0})).unwrap();
s.execute("component.create", &json!({"name": name})).unwrap();
s.execute("solid.box", &json!({"length": 10, "width": 10, "height": 10, "corner": [x, 0, 0]})).unwrap();
}
s.selection.clear();
let cam = Camera::default();
let original = scene(&s, &cam);
s.execute("inspect.colored_components", &json!({"enabled": true})).unwrap();
s.colored_components = Some(());
let colored = scene(&s, &cam);
assert_eq!(colored.meshes.len(), 2);
assert_ne!(colored.meshes[0].color, colored.meshes[1].color);
for (body, mesh) in s.world_state().bodies.iter().zip(&colored.meshes) {
let [r, g, b] = component_color(&s, body.component).unwrap();
assert_eq!(mesh.color, Rgb(r, g, b));
assert!(mesh.face_colors.is_empty());
}
s.execute("inspect.colored_components", &json!({"enabled": false})).unwrap();
let restored = scene(&s, &cam);
for (a, b) in original.meshes.iter().zip(restored.meshes) {
assert_eq!(a.color, b.color);
assert_eq!(a.face_colors, b.face_colors);
}
}
#[test]
fn cycling_component_color_is_saved_undoable_and_shared_by_instances() {
let mut s = Session::default();
let component = s.execute("component.create", &json!({"name":"Part"})).unwrap()["component"].as_u64().unwrap();
s.execute("solid.box", &json!({"length":10,"width":10,"height":10})).unwrap();
s.execute("occurrence.copy", &json!({"component":component,"translate":[20,0,0]})).unwrap();
let appearances = s.doc.appearances.clone();
s.execute("inspect.colored_components", &json!({"enabled":true})).unwrap();
let automatic = component_color(&s, component).unwrap();
s.execute("inspect.colored_components", &json!({"enabled":false})).unwrap();
s.execute("inspect.colored_components", &json!({"enabled":true})).unwrap();
assert_eq!(component_color(&s, component), Some(automatic));
let result = s.execute("component.cycle_color", &json!({"component":component})).unwrap();
let custom = component_color(&s, component).unwrap();
assert_ne!(custom, automatic);
assert_eq!(result["color"], json!(custom));
assert_eq!(s.doc.appearances, appearances);
let loaded = Session::new(solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap());
assert_eq!(loaded.doc.component_identification_color(component), custom);
let copies: Vec<_> = s.world_state().bodies.iter().map(|b| b.component).collect();
assert_eq!(copies, [component, component]);
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(component_color(&s, component), Some(automatic));
s.execute("edit.redo", &json!({})).unwrap();
assert_eq!(component_color(&s, component), Some(custom));
s.execute("component.paste_new", &json!({"component":component,"translate":[40,0,0]})).unwrap();
assert!(s.doc.components.iter().filter(|c| c.identification_color == Some(custom)).count() >= 2);
assert!(s.execute("component.cycle_color", &json!({"component":"missing"})).is_err());
assert!(s.execute("component.cycle_color", &json!({"component":0})).is_err());
}