From 60519a5b44f7ebea64fc13081b8b10fdcde43ea1 Mon Sep 17 00:00:00 2001 From: Dimitris Zervas Date: Wed, 12 Jun 2024 11:46:02 +0300 Subject: [PATCH] include the node in each step Signed-off-by: Dimitris Zervas --- packages/cadmium/src/lib.rs | 57 +++++++++------- packages/cadmium/src/message/idwrap/mod.rs | 9 ++- packages/cadmium/src/workbench.rs | 76 +++++++++++++++------- 3 files changed, 95 insertions(+), 47 deletions(-) diff --git a/packages/cadmium/src/lib.rs b/packages/cadmium/src/lib.rs index 612af0aa..0aa269e3 100644 --- a/packages/cadmium/src/lib.rs +++ b/packages/cadmium/src/lib.rs @@ -6,12 +6,12 @@ use wasm_bindgen::prelude::*; extern crate console_error_panic_hook; pub mod archetypes; -pub mod interop; pub mod error; +pub mod feature; +pub mod interop; pub mod isketch; pub mod message; pub mod project; -pub mod feature; // pub mod state; pub mod step; pub mod workbench; @@ -70,7 +70,8 @@ impl Project { #[wasm_bindgen] pub fn get_workbench(&self, workbench_index: u32) -> workbench::Workbench { // TODO: Use get() and return a Result - self.native.workbenches + self.native + .workbenches .get(workbench_index as usize) .unwrap() .borrow() @@ -79,42 +80,54 @@ impl Project { #[wasm_bindgen] pub fn send_message(&mut self, message: &Message) -> MessageResult { - // TODO: Move this to a MessageHandler trait during first stage indirection - self.get_workbench(0).add_message_step(message); - message.handle(&mut self.native).into() } #[wasm_bindgen] pub fn get_workbench_solids(&self, workbench_index: u32) -> SolidArray { - SolidArray(self.native.workbenches - .get(workbench_index as usize) - .unwrap() - .borrow() - .get_solids()) + SolidArray( + self.native + .workbenches + .get(workbench_index as usize) + .unwrap() + .borrow() + .get_solids(), + ) } #[wasm_bindgen] - pub fn get_sketch_primitive(&self, workbench_id: IDType, sketch_id: IDType, primitive_id: IDType) -> archetypes::WrappedPrimitive { - let binding = self.native.get_workbench_by_id(workbench_id) - .unwrap(); - let workbench = binding - .borrow(); + pub fn get_sketch_primitive( + &self, + workbench_id: IDType, + sketch_id: IDType, + primitive_id: IDType, + ) -> archetypes::WrappedPrimitive { + let binding = self.native.get_workbench_by_id(workbench_id).unwrap(); + let workbench = binding.borrow(); let binding = workbench .get_sketch_by_id(sketch_id) .unwrap() .borrow() .sketch(); - let sketch = binding - .borrow(); + let sketch = binding.borrow(); let binding = sketch.primitives(); let primitive = binding.get(&primitive_id).unwrap().borrow().to_primitive(); match primitive { - isotope::primitives::Primitive::Point2(point) => archetypes::WrappedPrimitive::Point2(archetypes::Point2::from_sketch(&sketch, &point)), - isotope::primitives::Primitive::Line(line) => archetypes::WrappedPrimitive::Line2(archetypes::Line2::from_sketch(&sketch, &line)), - isotope::primitives::Primitive::Circle(circle) => archetypes::WrappedPrimitive::Circle2(archetypes::Circle2::from_sketch(&sketch, &circle)), - isotope::primitives::Primitive::Arc(arc) => archetypes::WrappedPrimitive::Arc2(archetypes::Arc2::from_sketch(&sketch, &arc)), + isotope::primitives::Primitive::Point2(point) => archetypes::WrappedPrimitive::Point2( + archetypes::Point2::from_sketch(&sketch, &point), + ), + isotope::primitives::Primitive::Line(line) => { + archetypes::WrappedPrimitive::Line2(archetypes::Line2::from_sketch(&sketch, &line)) + } + isotope::primitives::Primitive::Circle(circle) => { + archetypes::WrappedPrimitive::Circle2(archetypes::Circle2::from_sketch( + &sketch, &circle, + )) + } + isotope::primitives::Primitive::Arc(arc) => { + archetypes::WrappedPrimitive::Arc2(archetypes::Arc2::from_sketch(&sketch, &arc)) + } } } } diff --git a/packages/cadmium/src/message/idwrap/mod.rs b/packages/cadmium/src/message/idwrap/mod.rs index a9466d6f..ea4ceca9 100644 --- a/packages/cadmium/src/message/idwrap/mod.rs +++ b/packages/cadmium/src/message/idwrap/mod.rs @@ -51,10 +51,15 @@ where ) -> anyhow::Result> { let wb = T::Parent::from_parent_id(project, self.id)?; let result = self.inner.handle_message(wb.clone())?; + let (id, node) = if let Some((id, node)) = result { + (Some(id), Some(node)) + } else { + (None, None) + }; - wb.borrow_mut().add_message_step(&self.clone().into()); + wb.borrow_mut().add_message_step(&self.clone().into(), node); - Ok(result.map(|(id, _)| id)) + Ok(id) } } diff --git a/packages/cadmium/src/workbench.rs b/packages/cadmium/src/workbench.rs index 8c693d7d..f75fc7ee 100644 --- a/packages/cadmium/src/workbench.rs +++ b/packages/cadmium/src/workbench.rs @@ -5,10 +5,10 @@ use wasm_bindgen::prelude::*; use crate::archetypes::{Plane, PlaneDescription}; use crate::error::CADmiumError; +use crate::feature::point::Point3; use crate::feature::solid::Solid; -use crate::isketch::ISketch; use crate::feature::Feature; -use crate::feature::point::Point3; +use crate::isketch::ISketch; use crate::step::Step; use crate::{interop, IDType}; @@ -55,7 +55,8 @@ impl Workbench { features_next_id: 0, }; - wb.points.insert(0, Rc::new(RefCell::new(Point3::new(0.0, 0.0, 0.0)))); + wb.points + .insert(0, Rc::new(RefCell::new(Point3::new(0.0, 0.0, 0.0)))); wb.planes.insert(0, Rc::new(RefCell::new(Plane::front()))); wb.planes.insert(1, Rc::new(RefCell::new(Plane::front()))); wb.planes.insert(2, Rc::new(RefCell::new(Plane::front()))); @@ -80,20 +81,26 @@ impl Workbench { } pub fn get_sketch_by_id(&self, id: IDType) -> Result>, CADmiumError> { - self.sketches.get(&id).ok_or(CADmiumError::SketchIDNotFound(id)).cloned() + self.sketches + .get(&id) + .ok_or(CADmiumError::SketchIDNotFound(id)) + .cloned() } - pub fn add_message_step(&mut self, message: &Message) { - self.history.push( - Rc::new( - RefCell::new( - Step::new(self.history.len() as IDType, message.clone(), None)))); + pub fn add_message_step(&mut self, message: &Message, node: Option) { + self.history.push(Rc::new(RefCell::new(Step::new( + self.history.len() as IDType, + message.clone(), + node, + )))); } pub fn get_solids(&self) -> Vec { - self.features.values().map(|feature| { - feature.borrow().as_solid_like().to_solids().unwrap() - }).flatten().collect() + self.features + .values() + .map(|feature| feature.borrow().as_solid_like().to_solids().unwrap()) + .flatten() + .collect() } } @@ -130,7 +137,10 @@ pub struct AddPoint { impl MessageHandler for AddPoint { type Parent = Rc>; - fn handle_message(&self, sketch_ref: Self::Parent) -> anyhow::Result> { + fn handle_message( + &self, + sketch_ref: Self::Parent, + ) -> anyhow::Result> { let mut wb = sketch_ref.borrow_mut(); let new_id = wb.points_next_id; @@ -141,7 +151,6 @@ impl MessageHandler for AddPoint { } } - #[derive(Tsify, Debug, Clone, Serialize, Deserialize)] #[tsify(from_wasm_abi, into_wasm_abi)] pub struct AddPlane { @@ -152,7 +161,10 @@ pub struct AddPlane { impl MessageHandler for AddPlane { type Parent = Rc>; - fn handle_message(&self, sketch_ref: Self::Parent) -> anyhow::Result> { + fn handle_message( + &self, + sketch_ref: Self::Parent, + ) -> anyhow::Result> { let mut wb = sketch_ref.borrow_mut(); let new_id = wb.planes_next_id; @@ -171,7 +183,10 @@ pub struct AddSketch { impl MessageHandler for AddSketch { type Parent = Rc>; - fn handle_message(&self, workbench_ref: Self::Parent) -> anyhow::Result> { + fn handle_message( + &self, + workbench_ref: Self::Parent, + ) -> anyhow::Result> { let mut wb = workbench_ref.borrow_mut(); let sketch = ISketch::try_from_plane_description(&wb, &self.plane_description)?; @@ -191,7 +206,10 @@ pub struct WorkbenchRename { impl MessageHandler for WorkbenchRename { type Parent = Rc>; - fn handle_message(&self, workbench_ref: Self::Parent) -> anyhow::Result> { + fn handle_message( + &self, + workbench_ref: Self::Parent, + ) -> anyhow::Result> { let mut workbench = workbench_ref.borrow_mut(); workbench.name = self.new_name.clone(); Ok(None) @@ -207,16 +225,28 @@ pub struct SetSketchPlane { impl MessageHandler for SetSketchPlane { type Parent = Rc>; - fn handle_message(&self, workbench_ref: Self::Parent) -> anyhow::Result> { + fn handle_message( + &self, + workbench_ref: Self::Parent, + ) -> anyhow::Result> { let wb = workbench_ref.borrow(); let plane = match self.plane_description { - PlaneDescription::PlaneId(plane_id) => - wb.planes.get(&plane_id).ok_or(anyhow::anyhow!("Failed to find plane with id {}", plane_id))?, - PlaneDescription::SolidFace { solid_id: _, normal: _ } => todo!("Implement SolidFace"), - }.clone(); + PlaneDescription::PlaneId(plane_id) => wb + .planes + .get(&plane_id) + .ok_or(anyhow::anyhow!("Failed to find plane with id {}", plane_id))?, + PlaneDescription::SolidFace { + solid_id: _, + normal: _, + } => todo!("Implement SolidFace"), + } + .clone(); - let sketch = wb.sketches.get(&self.sketch_id).ok_or(anyhow::anyhow!("Failed to find sketch with id {}", self.sketch_id))?; + let sketch = wb.sketches.get(&self.sketch_id).ok_or(anyhow::anyhow!( + "Failed to find sketch with id {}", + self.sketch_id + ))?; sketch.borrow_mut().plane = plane; Ok(None)