Skip to content

Commit

Permalink
include the node in each step
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Zervas <[email protected]>
  • Loading branch information
dzervas committed Jun 12, 2024
1 parent 55d0eb1 commit 60519a5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 47 deletions.
57 changes: 35 additions & 22 deletions packages/cadmium/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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))
}
}
}
}
9 changes: 7 additions & 2 deletions packages/cadmium/src/message/idwrap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ where
) -> anyhow::Result<Option<IDType>> {
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)
}
}

Expand Down
76 changes: 53 additions & 23 deletions packages/cadmium/src/workbench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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())));
Expand All @@ -80,20 +81,26 @@ impl Workbench {
}

pub fn get_sketch_by_id(&self, id: IDType) -> Result<Rc<RefCell<ISketch>>, 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<interop::Node>) {
self.history.push(Rc::new(RefCell::new(Step::new(
self.history.len() as IDType,
message.clone(),
node,
))));
}

pub fn get_solids(&self) -> Vec<Solid> {
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()
}
}

Expand Down Expand Up @@ -130,7 +137,10 @@ pub struct AddPoint {

impl MessageHandler for AddPoint {
type Parent = Rc<RefCell<Workbench>>;
fn handle_message(&self, sketch_ref: Self::Parent) -> anyhow::Result<Option<(IDType, interop::Node)>> {
fn handle_message(
&self,
sketch_ref: Self::Parent,
) -> anyhow::Result<Option<(IDType, interop::Node)>> {
let mut wb = sketch_ref.borrow_mut();

let new_id = wb.points_next_id;
Expand All @@ -141,7 +151,6 @@ impl MessageHandler for AddPoint {
}
}


#[derive(Tsify, Debug, Clone, Serialize, Deserialize)]
#[tsify(from_wasm_abi, into_wasm_abi)]
pub struct AddPlane {
Expand All @@ -152,7 +161,10 @@ pub struct AddPlane {

impl MessageHandler for AddPlane {
type Parent = Rc<RefCell<Workbench>>;
fn handle_message(&self, sketch_ref: Self::Parent) -> anyhow::Result<Option<(IDType, interop::Node)>> {
fn handle_message(
&self,
sketch_ref: Self::Parent,
) -> anyhow::Result<Option<(IDType, interop::Node)>> {
let mut wb = sketch_ref.borrow_mut();

let new_id = wb.planes_next_id;
Expand All @@ -171,7 +183,10 @@ pub struct AddSketch {

impl MessageHandler for AddSketch {
type Parent = Rc<RefCell<Workbench>>;
fn handle_message(&self, workbench_ref: Self::Parent) -> anyhow::Result<Option<(IDType, interop::Node)>> {
fn handle_message(
&self,
workbench_ref: Self::Parent,
) -> anyhow::Result<Option<(IDType, interop::Node)>> {
let mut wb = workbench_ref.borrow_mut();
let sketch = ISketch::try_from_plane_description(&wb, &self.plane_description)?;

Expand All @@ -191,7 +206,10 @@ pub struct WorkbenchRename {

impl MessageHandler for WorkbenchRename {
type Parent = Rc<RefCell<Workbench>>;
fn handle_message(&self, workbench_ref: Self::Parent) -> anyhow::Result<Option<(IDType, interop::Node)>> {
fn handle_message(
&self,
workbench_ref: Self::Parent,
) -> anyhow::Result<Option<(IDType, interop::Node)>> {
let mut workbench = workbench_ref.borrow_mut();
workbench.name = self.new_name.clone();
Ok(None)
Expand All @@ -207,16 +225,28 @@ pub struct SetSketchPlane {

impl MessageHandler for SetSketchPlane {
type Parent = Rc<RefCell<Workbench>>;
fn handle_message(&self, workbench_ref: Self::Parent) -> anyhow::Result<Option<(IDType, interop::Node)>> {
fn handle_message(
&self,
workbench_ref: Self::Parent,
) -> anyhow::Result<Option<(IDType, interop::Node)>> {
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)
Expand Down

0 comments on commit 60519a5

Please sign in to comment.