From e0c1feca82e16b2f4cd23b29a12010be94051134 Mon Sep 17 00:00:00 2001 From: Dimitris Zervas Date: Tue, 28 May 2024 04:08:09 +0300 Subject: [PATCH] Touch a bit the workspaces Signed-off-by: Dimitris Zervas --- packages/cadmium/src/isketch.rs | 33 +++++++++++------------ packages/cadmium/src/lib.rs | 6 ++--- packages/cadmium/src/message.rs | 45 ------------------------------- packages/cadmium/src/solid.rs | 2 +- packages/cadmium/src/step.rs | 13 +++++---- packages/cadmium/src/workbench.rs | 37 +++++++++++++------------ 6 files changed, 46 insertions(+), 90 deletions(-) diff --git a/packages/cadmium/src/isketch.rs b/packages/cadmium/src/isketch.rs index a0340e2b..6e19f5da 100644 --- a/packages/cadmium/src/isketch.rs +++ b/packages/cadmium/src/isketch.rs @@ -28,38 +28,32 @@ pub struct IPlane { #[derive(Tsify, Debug, Clone, Serialize, Deserialize)] #[tsify(into_wasm_abi, from_wasm_abi)] pub struct ISketch { - pub plane_id: String, - plane: Rc>, + plane: Rc>, - // TODO: Make the sketch private - pub sketch: Rc>, + sketch: Rc>, points_3d: BTreeMap, - // primitives: BTreeMap>>, - // constraints: VecDeque>>, - // TODO: Make the faces private pub faces: Vec, } impl ISketch { // TODO: Maybe pass the plane as refcell? - pub fn new(plane_id: &str, plane: &IPlane, sketch: Rc>) -> Self { + pub fn new(plane: Rc>) -> Self { // The key difference between Sketch and RealSketch is that Sketch lives // in 2D and RealSketch lives in 3D. So we need to convert the points let mut real_sketch = Self { - plane_id: plane_id.to_owned(), - plane: Rc::new(RefCell::new(plane.clone())), + plane, points_3d: BTreeMap::new(), // primitives: sketch.borrow().primitives().iter().map(|(id, prim)| (*id, prim.borrow().to_primitive())).collect(), // constraints: sketch.borrow().constraints().iter().map(|c| c.borrow().get_type()).collect(), - sketch: sketch, + sketch: Rc::new(RefCell::new(Sketch::new())), faces: vec![], }; for (id, point) in real_sketch.sketch.borrow().get_all_points().iter() { - real_sketch.points_3d.insert(*id, Self::calculate_point_3d(plane, point)); + real_sketch.points_3d.insert(*id, Self::calculate_point_3d(&plane, point)); } real_sketch @@ -84,10 +78,15 @@ impl ISketch { } } - fn calculate_point_3d(plane: &IPlane, point: &ISOPoint2) -> Point3 { - let o = plane.plane.origin.clone(); - let x = plane.plane.primary.clone(); - let y = plane.plane.secondary.clone(); + pub fn sketch(&self) -> Rc> { + self.sketch.clone() + } + + fn calculate_point_3d(plane_cell: &Rc>, point: &ISOPoint2) -> Point3 { + let plane = plane_cell.borrow(); + let o = plane.origin.clone(); + let x = plane.primary.clone(); + let y = plane.secondary.clone(); let pt3 = o.plus(x.times(point.x())).plus(y.times(point.y())); Point3::new(pt3.x, pt3.y, pt3.z) @@ -100,7 +99,7 @@ impl ISketch { let mut sketch = self.sketch.borrow_mut(); let point_id = sketch.add_primitive(iso_point)?; - self.points_3d.insert(point_id, Self::calculate_point_3d(&self.plane.borrow(), &point.into())); + self.points_3d.insert(point_id, Self::calculate_point_3d(&self.plane, &point.into())); Ok(point_id) } diff --git a/packages/cadmium/src/lib.rs b/packages/cadmium/src/lib.rs index 1b8eb6f7..93cbae84 100644 --- a/packages/cadmium/src/lib.rs +++ b/packages/cadmium/src/lib.rs @@ -67,16 +67,16 @@ impl Project { } #[wasm_bindgen] - pub fn get_realization(&self, workbench_id: u32, max_steps: u32) -> Realization { + pub fn get_realization(&self, workbench_id: IDType, max_steps: u64) -> Realization { let realized = self .native - .get_realization(workbench_id as u64, max_steps as u64); + .get_realization(workbench_id, max_steps); Realization { native: realized } } #[wasm_bindgen] - pub fn get_workbench(&self, workbench_index: u32) -> workbench::Workbench { + pub fn get_workbench(&self, workbench_index: IDType) -> workbench::Workbench { // TODO: Use get() and return a Result self.native.workbenches[workbench_index as usize] } diff --git a/packages/cadmium/src/message.rs b/packages/cadmium/src/message.rs index 3593ffca..fcce9b79 100644 --- a/packages/cadmium/src/message.rs +++ b/packages/cadmium/src/message.rs @@ -164,51 +164,6 @@ impl Message { Ok(format!("\"name\": \"{}\"", new_name)) } - Message::DeleteSketchPrimitives { - workbench_id, - sketch_id, - ids, - } => { - let workbench = project.get_workbench_by_id_mut(*workbench_id)?; - let sketch = workbench.get_sketch_by_id_mut(sketch_id)?; - for id in ids { - sketch.delete_primitive(*id)?; - } - Ok("".to_owned()) - } - Message::AddSketchPrimitive { - workbench_id, - sketch_id, - primitive, - } => { - let workbench = project.get_workbench_by_id_mut(*workbench_id)?; - let sketch = workbench.get_sketch_by_id_mut(sketch_id)?; - let id = sketch.add_primitive(primitive.clone())?; - Ok(format!("\"id\": \"{}\"", id)) - }, - Message::AddSketchArc { - workbench_id, - sketch_id, - center_id, - radius, - clockwise, - start_angle, - end_angle, - } => { - let workbench = project.get_workbench_by_id_mut(*workbench_id)?; - let sketch = workbench.get_sketch_by_id_mut(sketch_id)?; - let center = sketch.get_all_points().get(center_id) - .ok_or(CADmiumError::PrimitiveNotInSketch)?; - // let arc = arc::Arc::new( - // center.clone(), - // *radius, - // *clockwise, - // *start_angle, - // *end_angle, - // ); - // Ok(format!("\"id\": \"{}\"", id)) - Ok("".to_owned()) - }, Message::NewSketchOnPlane { workbench_id, sketch_name, diff --git a/packages/cadmium/src/solid.rs b/packages/cadmium/src/solid.rs index dc4deed5..6f006bbd 100644 --- a/packages/cadmium/src/solid.rs +++ b/packages/cadmium/src/solid.rs @@ -163,7 +163,7 @@ impl Solid { // .iter() // .map(|face_id| sketch.faces.get(*face_id as usize).unwrap().clone()) // .collect(); - let merged_faces = sketch.sketch.borrow().get_merged_faces(); + let merged_faces = sketch.sketch().borrow().get_merged_faces(); for (f_index, face) in merged_faces.iter().enumerate() { // let face = sketch.faces.get(*face_id as usize).unwrap(); diff --git a/packages/cadmium/src/step.rs b/packages/cadmium/src/step.rs index bb4d09f7..367368d9 100644 --- a/packages/cadmium/src/step.rs +++ b/packages/cadmium/src/step.rs @@ -41,6 +41,7 @@ macro_rules! define_steps { $( $( [<$parent_type $name>] { + workbench_id: crate::IDType, [<$parent_type:snake _id>]: crate::IDType, $($field: $type),* } @@ -49,7 +50,7 @@ macro_rules! define_steps { } } - impl crate::workbench::Workbench { + impl crate::Project { $( paste::paste! { pub fn [](name: String, id: crate::IDType) -> Step { @@ -59,9 +60,10 @@ macro_rules! define_steps { $( paste::paste! { - pub fn [](&mut self, [< $parent_type:snake _id >]: crate::IDType, name: String, $($field: $type),*) -> Result { - let parent = self.[<$wb_field>].get_mut(&[< $parent_type:snake _id >]).ok_or(anyhow::anyhow!("Could not find parent"))?; - let result_id_ = parent.[< add_ $name:snake >]($($field),* )?; + pub fn [](&mut self, workbench_id: crate::IDType, [< $parent_type:snake _id >]: crate::IDType, name: String, $($field: $type),*) -> Result { + let wb_ = self.native.workbenches.get_mut(workbench_id as usize).ok_or(anyhow::anyhow!("Could not find workbench"))?; + let parent_ = wb_.[<$wb_field>].get_mut(&[< $parent_type:snake _id >]).ok_or(anyhow::anyhow!("Could not find parent"))?; + let result_id_ = parent_.borrow_mut().[< add_ $name:snake >]($($field),* )?; let step_ = Step { name, @@ -69,12 +71,13 @@ macro_rules! define_steps { unique_id: format!(concat!("Add:", stringify!($parent_type), stringify!($name), "-{}"), result_id_), suppressed: false, data: StepData::[<$parent_type $name>] { + workbench_id, [< $parent_type:snake _id >], $($field),* }, }; - self.history.push(step_); + wb_.history.push(step_); Ok(result_id_) } diff --git a/packages/cadmium/src/workbench.rs b/packages/cadmium/src/workbench.rs index a371b2d5..4a077dfa 100644 --- a/packages/cadmium/src/workbench.rs +++ b/packages/cadmium/src/workbench.rs @@ -24,15 +24,17 @@ use truck_shapeops::and as solid_and; pub struct Workbench { pub(crate) name: String, pub(crate) history: Vec, - pub(crate) step_counters: HashMap, + // These are free-standing points in 3D space, not part of sketches pub(crate) points: BTreeMap, pub(crate) points_next_id: IDType, - pub(crate) sketches: BTreeMap, - pub(crate) sketches_next_id: IDType, + pub(crate) planes: BTreeMap, pub(crate) planes_next_id: IDType, - pub(crate) solids: BTreeMap, + + pub(crate) sketches: BTreeMap>>, + pub(crate) sketches_next_id: IDType, + pub(crate) solids: BTreeMap>>, pub(crate) solids_next_id: IDType, } @@ -41,26 +43,22 @@ impl Workbench { let mut wb = Workbench { name: name.to_owned(), history: vec![], - step_counters: HashMap::from([ - ("Point".to_owned(), 0), - ("Plane".to_owned(), 0), - ("Sketch".to_owned(), 0), - ("Extrusion".to_owned(), 0), - ]), + points: BTreeMap::new(), points_next_id: 0, - sketches: BTreeMap::new(), - sketches_next_id: 0, planes: BTreeMap::new(), planes_next_id: 0, + + sketches: BTreeMap::new(), + sketches_next_id: 0, solids: BTreeMap::new(), solids_next_id: 0, }; - wb.add_workbench_point("Origin".to_string(), Point3::new(0.0, 0.0, 0.0)).unwrap(); - wb.add_workbench_plane("Front".to_string(), Plane::front(), 100.0, 100.0).unwrap(); - wb.add_workbench_plane("Right".to_string(), Plane::right(), 100.0, 100.0).unwrap(); - wb.add_workbench_plane("Top".to_string(), Plane::top(), 100.0, 100.0).unwrap(); + wb.add_point(Point3::new(0.0, 0.0, 0.0)).unwrap(); + wb.add_plane(Plane::front(), 100.0, 100.0).unwrap(); + wb.add_plane(Plane::right(), 100.0, 100.0).unwrap(); + wb.add_plane(Plane::top(), 100.0, 100.0).unwrap(); wb } @@ -81,6 +79,10 @@ impl Workbench { } } + pub fn get_sketch_by_id(&mut self, id: IDType) -> Result<&mut Rc>, CADmiumError> { + self.sketches.get_mut(&id).ok_or(CADmiumError::SketchIDNotFound(id)) + } + pub fn update_step_data(&mut self, step_id: &str, new_step_data: StepData) { let mut index = 0; for step in self.history.iter() { @@ -96,7 +98,6 @@ impl Workbench { pub fn add_extrusion(&mut self, name: &str, extrusion: Extrusion) -> u64 { // If the extrusion name is empty string, then we need to generate a new name // Let's use "Extrusion n" where n is the number of extrusions - let counter = self.step_counters.get_mut("Extrusion").unwrap(); let extrusion_name = if name == "" { format!("Extrusion {}", *counter + 1) } else { @@ -104,8 +105,6 @@ impl Workbench { }; self.history .push(Step::new_extrusion(&extrusion_name, extrusion, *counter)); - *counter += 1; - *counter - 1 } pub fn realize(&self, max_steps: u64) -> Realization {