Skip to content

Commit

Permalink
Most not strictly add commands done
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Zervas <dzervas@dzervas.gr>
  • Loading branch information
dzervas committed May 28, 2024
1 parent 6be3df4 commit b76d30b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/cadmium/src/isketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<Plane>>,
// TODO: Make it private with a setter
pub plane: Rc<RefCell<Plane>>,

sketch: Rc<RefCell<Sketch>>,
points_3d: BTreeMap<u64, Point3>,
Expand Down
7 changes: 4 additions & 3 deletions packages/cadmium/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Realization, String> {
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]
Expand Down
21 changes: 6 additions & 15 deletions packages/cadmium/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions packages/cadmium/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Realization, anyhow::Error> {
let workbench = &self.workbenches[workbench_id as usize];
let realization = workbench.realize(max_steps);
realization
workbench.realize(max_steps)
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/cadmium/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -46,6 +47,8 @@ pub enum StepData {
// width: f64,
// height: f64,
},

// Sketch Primitives
#[step_data(workbench_field = "sketches", type = "Sketch")]
SketchPoint {
workbench_id: IDType,
Expand Down
11 changes: 5 additions & 6 deletions packages/cadmium/src/workbench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Workbench {
pub(crate) points: BTreeMap<IDType, Point3>,
pub(crate) points_next_id: IDType,

pub(crate) planes: BTreeMap<IDType, Plane>,
pub(crate) planes: BTreeMap<IDType, Rc<RefCell<Plane>>>,
pub(crate) planes_next_id: IDType,

pub(crate) sketches: BTreeMap<IDType, Rc<RefCell<ISketch>>>,
Expand Down Expand Up @@ -369,7 +369,8 @@ impl Workbench {
}

pub(super) fn add_workbench_plane(&mut self, plane: Plane, width: f64, height: f64) -> Result<IDType, anyhow::Error> {
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)
}
Expand All @@ -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"));
Expand Down

0 comments on commit b76d30b

Please sign in to comment.