Face colours and opacity in STEP and 3MF export (and STEP import)

- Bodies carry a Paint for export (opacity, per-face colours), set by
  Document::painted from the appearance model. The paint comes from a
  face appearance, a body/component appearance's opacity or a material
  look (Glass), and falls back to the paint the body was imported with.
- STEP: faces with colours of their own get a STYLED_ITEM on their
  ADVANCED_FACE. See-through looks add SURFACE_STYLE_TRANSPARENT.
  Differently coloured pieces are not merged into one face.
- STEP import reads face styles and transparency. The reader tracks the
  file face of each kernel face (bands split in two keep it), so imported
  face colours land on the right faces; the design shows them through
  face_colors.
- 3MF: per-triangle colours (pid/p1 into the base materials, carried
  through welding) and the object's alpha (#RRGGBBAA).
- Tests: a blue half-transparent cube with a red top exports, re-imports
  with its opacity and the red top face, and keeps it when opened and
  exported again. 3MF writes the alpha and the face entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AS5Tn3xieewUSg3uxrMVdL
This commit is contained in:
bherbruckandClaude Opus 5.5 committed 2026-10-08 14:02:10 +00:00
1 parent 8045904c54
commit 294597cc96
12 files changed
+313 -49

No files matched your search

+44 -1
View File
@@ -17,6 +17,24 @@ pub struct Body {
pub(crate) mesh: Option<std::sync::Arc<crate::meshbody::TriMesh>>,
/// Display colour (linear RGB 0..1) from an imported file; operations drop it.
pub(crate) color: Option<[f32; 3]>,
/// Opacity and face colours for export (set by the document; operations drop it).
pub(crate) paint: Option<std::sync::Arc<Paint>>,
}
/// How a body looks beyond its colour, for export: its opacity and its faces' own colours.
#[derive(Clone, Debug, PartialEq)]
pub struct Paint {
/// 1 = opaque.
pub opacity: f32,
pub faces: Vec<FacePaint>,
}
/// A face's own colour: the B-rep face index (`face_iter` order), sRGB as 0..1, opacity.
#[derive(Clone, Debug, PartialEq)]
pub struct FacePaint {
pub face: usize,
pub color: [f32; 3],
pub opacity: f32,
}
/// A B-rep edge as seen by the rest of the application.
@@ -259,6 +277,7 @@ impl Body {
_ => false,
}
&& self.color == other.color
&& self.paint == other.paint
}
pub(crate) fn new(solid: Solid) -> Result<Body> {
@@ -283,7 +302,7 @@ impl Body {
if v < 0.0 {
solid.not();
}
Ok(Body { solid: std::sync::Arc::new(solid), mesh: None, color: None })
Ok(Body { solid: std::sync::Arc::new(solid), mesh: None, color: None, paint: None })
}
/// A deep copy of the solid that can be mutated without affecting other bodies.
@@ -316,6 +335,30 @@ impl Body {
self
}
/// Opacity and face colours for export, if any.
pub fn paint(&self) -> Option<&Paint> {
self.paint.as_deref()
}
/// The same body with opacity and face colours for export (`None` or opaque with no face
/// colours clears them).
pub fn with_paint(mut self, p: Option<Paint>) -> Body {
let clean = |x: f32| if x.is_finite() { x.clamp(0.0, 1.0) } else { 1.0 };
self.paint = p
.map(|mut p| {
p.opacity = clean(p.opacity);
p.faces.retain(|f| f.color.iter().all(|x| x.is_finite()));
for f in &mut p.faces {
f.color = f.color.map(|x| x.clamp(0.0, 1.0));
f.opacity = clean(f.opacity);
}
p
})
.filter(|p| p.opacity < 1.0 || !p.faces.is_empty())
.map(std::sync::Arc::new);
self
}
/// Characteristic size (bounding box diagonal) for tolerances.
pub fn size(&self) -> f64 {
let mut b = solvecraft_geom::Aabb3::EMPTY;