From d5bdf07cead1249e2a2203cabfe42d8506d113ea Mon Sep 17 00:00:00 2001 From: Matt Ferraro Date: Wed, 15 May 2024 12:34:17 -0400 Subject: [PATCH] creating a solid now automatically creates truck faces --- packages/cadmium/src/main.rs | 99 ++++++++++++++++++++++++++++++- packages/cadmium/src/oplog/mod.rs | 75 ++++++++++++++++++++++- 2 files changed, 171 insertions(+), 3 deletions(-) diff --git a/packages/cadmium/src/main.rs b/packages/cadmium/src/main.rs index 352d7329..8e24a126 100644 --- a/packages/cadmium/src/main.rs +++ b/packages/cadmium/src/main.rs @@ -17,7 +17,104 @@ use truck_shapeops::{and, or, ShapeOpsCurve, ShapeOpsSurface}; use truck_topology::{Shell, Solid}; fn main() { - simple_cube(); + stacked_cubes(); +} + +fn stacked_cubes() { + let mut el = EvolutionLog::new(); + + let workbench_id = el.append(Operation::CreateWorkbench { + nonce: "Workbench 1".to_string(), + }); + el.append(Operation::SetWorkbenchName { + workbench_id: workbench_id.clone(), + name: "Main Workbench".to_string(), + }); + + // Create the Top Plane + let top_plane_id = el.append(Operation::CreatePlane { + nonce: "the top plane".to_string(), + workbench_id: workbench_id.clone(), + }); + el.append(Operation::SetPlaneName { + plane_id: top_plane_id.clone(), + name: "Top".to_string(), + }); + let set_plane = el.append(Operation::SetPlane { + plane_id: top_plane_id.clone(), + plane: Plane::top(), + }); + let top_plane_real = el.realize_plane(&top_plane_id); + + // Create the sketch + let sketch_id = el.append(Operation::CreateSketch { + nonce: "top sketch".to_string(), + workbench_id: workbench_id.clone(), + }); + el.append(Operation::SetSketchName { + sketch_id: sketch_id.clone(), + name: "Original Sketch".to_string(), + }); + el.append(Operation::SetSketchPlane { + sketch_id: sketch_id.clone(), + plane_id: top_plane_real.clone(), + }); + + // make a square + el.append(Operation::AddSketchLine { + sketch_id: sketch_id.clone(), + start: (0.0, 0.0), + end: (0.0, 100.0), + }); + el.append(Operation::AddSketchLine { + sketch_id: sketch_id.clone(), + start: (0.0, 100.0), + end: (100.0, 100.0), + }); + el.append(Operation::AddSketchLine { + sketch_id: sketch_id.clone(), + start: (100.0, 100.0), + end: (100.0, 0.0), + }); + el.append(Operation::AddSketchLine { + sketch_id: sketch_id.clone(), + start: (100.0, 0.0), + end: (0.0, 0.0), + }); + let realized_sketch = el.realize_sketch(&sketch_id); + + // extrude the square + let extrusion_id = el.append(Operation::CreateExtrusion { + workbench_id: workbench_id.clone(), + nonce: "first extrusion".to_string(), + }); + el.append(Operation::SetExtrusionName { + extrusion_id: extrusion_id.clone(), + name: "Extrude1".to_string(), + }); + el.append(Operation::SetExtrusionDepth { + extrusion_id: extrusion_id.clone(), + depth: 100.0, + }); + el.append(Operation::SetExtrusionSketch { + extrusion_id: extrusion_id.clone(), + sketch_id: realized_sketch.clone(), + }); + el.append(Operation::SetExtrusionFaces { + extrusion_id: extrusion_id.clone(), + faces: vec![0], + }); + + el.realize_extrusion(&extrusion_id); + + // print each solid + // for (solid_id, solid) in el.solids.iter() { + // println!("Solid: {:?}", solid.truck_solid); + // solid.save_as_obj("first_solid.obj", 0.01); + // } + + el.git_log(); + // el.to_project(); } fn simple_cube() { diff --git a/packages/cadmium/src/oplog/mod.rs b/packages/cadmium/src/oplog/mod.rs index e8e2dc4d..3c98c491 100644 --- a/packages/cadmium/src/oplog/mod.rs +++ b/packages/cadmium/src/oplog/mod.rs @@ -5,12 +5,21 @@ use std::hash::{Hash, Hasher}; use std::process::id; use std::vec; use truck_polymesh::faces; +use truck_topology::{ + FaceDisplayFormat, ShellDisplayFormat, SolidDisplayFormat, VertexDisplayFormat, + WireDisplayFormat, +}; use crate::extrusion::Solid; use crate::project::{ Plane, PlaneDescription, Project, RealPlane, RealSketch, StepData, Workbench, }; use crate::sketch::Face; +use FaceDisplayFormat as FDF; +use ShellDisplayFormat as ShDF; +use SolidDisplayFormat as SDF; +use VertexDisplayFormat as VDF; +use WireDisplayFormat as WDF; pub type Sha = String; @@ -411,6 +420,16 @@ impl EvolutionLog { pub fn realize_extrusion(&mut self, extrusion_id: &Sha) { let (workbench_idx, extrusion_idx) = self.extrusions.get(extrusion_id).unwrap(); + // iterate through all of self.workbenches to find the one whose index matches workbench_idx + let workbench_sha = self.workbenches.iter().find_map(|(key, &val)| { + if val == *workbench_idx { + Some(key) + } else { + None + } + }); + let workbench_sha = workbench_sha.unwrap().clone(); + let mut wb = self.project.workbenches.get_mut(*workbench_idx).unwrap(); let step = wb.history.get_mut(*extrusion_idx).unwrap(); @@ -425,8 +444,19 @@ impl EvolutionLog { nonce: name, solid: solid.clone(), }; + self.append(new_op); - self.solids.insert(self.cursor.clone(), solid); + self.solids.insert(self.cursor.clone(), solid.clone()); + for boundary in solid.truck_solid.boundaries() { + boundary.face_iter().for_each(|face| { + let o = Operation::CreateTruckFace { + workbench_id: workbench_sha.clone(), + solid_id: self.cursor.clone(), + face: face.clone(), + }; + self.append(o); + }); + } } } else { unreachable!() @@ -720,6 +750,15 @@ pub enum Operation { sketch_id: Sha, face: Face, }, + CreateTruckFace { + workbench_id: Sha, + solid_id: Sha, + face: truck_topology::Face< + truck_meshalgo::prelude::cgmath::Point3, + truck_modeling::Curve, + truck_modeling::Surface, + >, + }, CreateExtrusion { workbench_id: Sha, @@ -845,6 +884,11 @@ impl Operation { sketch_id, face, } => hasher.update(format!("{workbench_id}-{sketch_id}-{face:?}").as_bytes()), + Operation::CreateTruckFace { + workbench_id, + solid_id, + face, + } => hasher.update(format!("{workbench_id}-{solid_id}-{face:?}").as_bytes()), Operation::CreateExtrusion { nonce, @@ -1061,6 +1105,22 @@ impl Operation { face ) } + Operation::CreateTruckFace { + workbench_id, + solid_id, + face, + } => { + format!( + "CreateTruckFace: {} {} {:?}", + workbench_id.to_owned()[..num_chars].to_string(), + solid_id.to_owned()[..num_chars].to_string(), + face.display(FDF::BoundariesAndID { + wire_format: WDF::VerticesList { + vertex_format: VDF::AsPoint, + }, + }) + ) + } Operation::CreateExtrusion { nonce, workbench_id, @@ -1125,7 +1185,18 @@ impl Operation { ) } Operation::CreateSolid { nonce, solid } => { - format!("CreateSolid: {nonce} {solid:?}") + format!( + "CreateSolid: {nonce} {:?}", + solid.truck_solid.display(SDF::ShellsList { + shell_format: ShDF::FacesList { + face_format: FDF::LoopsList { + wire_format: WDF::VerticesList { + vertex_format: VDF::AsPoint + } + } + } + }) + ) } } }