From b76d30bd09a6d42e4a0f585d60c43070b9577a32 Mon Sep 17 00:00:00 2001 From: Dimitris Zervas Date: Tue, 28 May 2024 11:39:48 +0300 Subject: [PATCH] Most not strictly add commands done Signed-off-by: Dimitris Zervas --- packages/cadmium/src/isketch.rs | 3 ++- packages/cadmium/src/lib.rs | 7 ++++--- packages/cadmium/src/message.rs | 21 ++++++--------------- packages/cadmium/src/project.rs | 5 ++--- packages/cadmium/src/step.rs | 3 +++ packages/cadmium/src/workbench.rs | 11 +++++------ 6 files changed, 22 insertions(+), 28 deletions(-) diff --git a/packages/cadmium/src/isketch.rs b/packages/cadmium/src/isketch.rs index f5de1e8c..b0d3ca30 100644 --- a/packages/cadmium/src/isketch.rs +++ b/packages/cadmium/src/isketch.rs @@ -28,7 +28,8 @@ pub struct IPlane { #[derive(Tsify, Debug, Clone, Serialize, Deserialize)] #[tsify(into_wasm_abi, from_wasm_abi)] pub struct ISketch { - plane: Rc>, + // TODO: Make it private with a setter + pub plane: Rc>, sketch: Rc>, points_3d: BTreeMap, diff --git a/packages/cadmium/src/lib.rs b/packages/cadmium/src/lib.rs index 3e50932d..bec6d0ed 100644 --- a/packages/cadmium/src/lib.rs +++ b/packages/cadmium/src/lib.rs @@ -67,12 +67,13 @@ impl Project { } #[wasm_bindgen] - pub fn get_realization(&self, workbench_id: IDType, max_steps: u64) -> Realization { + pub fn get_realization(&self, workbench_id: IDType, max_steps: u64) -> Result { let realized = self .native - .get_realization(workbench_id, max_steps); + .get_realization(workbench_id, max_steps) + .map_err(|e| format!("Realization Error: {}", e))?; - Realization { native: realized } + Ok(Realization { native: realized }) } #[wasm_bindgen] diff --git a/packages/cadmium/src/message.rs b/packages/cadmium/src/message.rs index 57b5a8f6..83836baa 100644 --- a/packages/cadmium/src/message.rs +++ b/packages/cadmium/src/message.rs @@ -96,8 +96,8 @@ pub enum Message { }, SetSketchPlane { workbench_id: u64, - sketch_id: String, - plane_id: String, + sketch_id: IDType, + plane_id: IDType, }, DeleteStep { workbench_id: u64, @@ -182,20 +182,11 @@ impl Message { plane_id: pid, } => { let workbench = project.get_workbench_by_id_mut(*workbench_id)?; - let step = workbench.get_step_by_id_mut(&sketch_id)?; - let plane_description: &mut PlaneDescription = if let StepData::Sketch { plane_description, .. } = &mut step.data { - plane_description - } else { - return Err(CADmiumError::IncorrectStepDataType("Sketch".to_owned()).into()); - }; + let plane = workbench.planes.iter().find(|(p, _)| *p == pid).ok_or(anyhow::anyhow!(""))?; + let sketch = workbench.get_sketch_by_id(*sketch_id)?.borrow_mut(); + sketch.plane = plane.1.clone(); - match plane_description { - PlaneDescription::PlaneId(ref mut plane_id) => { - *plane_id = pid.to_owned(); - Ok(format!("\"plane_id\": \"{}\"", pid)) - } - _ => Err(CADmiumError::NotImplemented.into()) - } + Ok(format!("\"plane_id\": \"{}\"", plane.0)) } Message::DeleteStep { workbench_id, diff --git a/packages/cadmium/src/project.rs b/packages/cadmium/src/project.rs index d8757282..e88f60a9 100644 --- a/packages/cadmium/src/project.rs +++ b/packages/cadmium/src/project.rs @@ -73,10 +73,9 @@ impl Project { .ok_or(CADmiumError::WorkbenchIDNotFound(id)) } - pub fn get_realization(&self, workbench_id: u64, max_steps: u64) -> Realization { + pub fn get_realization(&self, workbench_id: u64, max_steps: u64) -> Result { let workbench = &self.workbenches[workbench_id as usize]; - let realization = workbench.realize(max_steps); - realization + workbench.realize(max_steps) } } diff --git a/packages/cadmium/src/step.rs b/packages/cadmium/src/step.rs index c1509635..51e7e8b3 100644 --- a/packages/cadmium/src/step.rs +++ b/packages/cadmium/src/step.rs @@ -29,6 +29,7 @@ pub struct Step { #[derive(StepDataActions, Tsify, Debug, Serialize, Deserialize)] #[tsify(into_wasm_abi, from_wasm_abi)] pub enum StepData { + // Workbench Primitives WorkbenchPoint { workbench_id: IDType, point: Point3, @@ -46,6 +47,8 @@ pub enum StepData { // width: f64, // height: f64, }, + + // Sketch Primitives #[step_data(workbench_field = "sketches", type = "Sketch")] SketchPoint { workbench_id: IDType, diff --git a/packages/cadmium/src/workbench.rs b/packages/cadmium/src/workbench.rs index e7bec81b..dc409f28 100644 --- a/packages/cadmium/src/workbench.rs +++ b/packages/cadmium/src/workbench.rs @@ -29,7 +29,7 @@ pub struct Workbench { pub(crate) points: BTreeMap, pub(crate) points_next_id: IDType, - pub(crate) planes: BTreeMap, + pub(crate) planes: BTreeMap>>, pub(crate) planes_next_id: IDType, pub(crate) sketches: BTreeMap>>, @@ -369,7 +369,8 @@ impl Workbench { } pub(super) fn add_workbench_plane(&mut self, plane: Plane, width: f64, height: f64) -> Result { - self.planes.insert(self.planes_next_id, plane).ok_or(anyhow::anyhow!("Failed to insert plane")); + let plane_cell = Rc::new(RefCell::new(plane)); + self.planes.insert(self.planes_next_id, plane_cell).ok_or(anyhow::anyhow!("Failed to insert plane")); self.planes_next_id += 1; Ok(self.planes_next_id - 1) } @@ -382,11 +383,9 @@ impl Workbench { PlaneDescription::PlaneId(plane_id) => self.planes.get(&plane_id).ok_or(anyhow::anyhow!("Failed to find plane with id {}", plane_id))?, PlaneDescription::SolidFace { solid_id, normal } => todo!("Implement SolidFace"), - }; - - let plane_cell = Rc::new(RefCell::new(plane.clone())); + }.clone(); - let sketch = ISketch::new(plane_cell); + let sketch = ISketch::new(plane); self.sketches .insert(self.sketches_next_id, Rc::new(RefCell::new(sketch))) .ok_or(anyhow::anyhow!("Failed to insert sketch"));