Skip to content

Commit

Permalink
Touch a bit the workspaces
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Zervas <[email protected]>
  • Loading branch information
dzervas committed May 28, 2024
1 parent e1de1e0 commit e0c1fec
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 90 deletions.
33 changes: 16 additions & 17 deletions packages/cadmium/src/isketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<IPlane>>,
plane: Rc<RefCell<Plane>>,

// TODO: Make the sketch private
pub sketch: Rc<RefCell<Sketch>>,
sketch: Rc<RefCell<Sketch>>,
points_3d: BTreeMap<u64, Point3>,

// primitives: BTreeMap<u64, Rc<RefCell<Primitive>>>,
// constraints: VecDeque<Rc<RefCell<ConstraintType>>>,

// TODO: Make the faces private
pub faces: Vec<Face>,
}

impl ISketch {
// TODO: Maybe pass the plane as refcell?
pub fn new(plane_id: &str, plane: &IPlane, sketch: Rc<RefCell<Sketch>>) -> Self {
pub fn new(plane: Rc<RefCell<Plane>>) -> 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
Expand All @@ -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<RefCell<Sketch>> {
self.sketch.clone()
}

fn calculate_point_3d(plane_cell: &Rc<RefCell<Plane>>, 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)
Expand All @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions packages/cadmium/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
45 changes: 0 additions & 45 deletions packages/cadmium/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/cadmium/src/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 8 additions & 5 deletions packages/cadmium/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ macro_rules! define_steps {
$(
$(
[<$parent_type $name>] {
workbench_id: crate::IDType,
[<$parent_type:snake _id>]: crate::IDType,
$($field: $type),*
}
Expand All @@ -49,7 +50,7 @@ macro_rules! define_steps {
}
}

impl crate::workbench::Workbench {
impl crate::Project {
$(
paste::paste! {
pub fn [<delete_ $parent_type:snake _id>](name: String, id: crate::IDType) -> Step {
Expand All @@ -59,22 +60,24 @@ macro_rules! define_steps {

$(
paste::paste! {
pub fn [<add_ $parent_type:snake _ $name:snake>](&mut self, [< $parent_type:snake _id >]: crate::IDType, name: String, $($field: $type),*) -> Result<crate::IDType, anyhow::Error> {
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 [<add_ $parent_type:snake _ $name:snake>](&mut self, workbench_id: crate::IDType, [< $parent_type:snake _id >]: crate::IDType, name: String, $($field: $type),*) -> Result<crate::IDType, anyhow::Error> {
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,
operation: StepOperation::Add,
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_)
}
Expand Down
37 changes: 18 additions & 19 deletions packages/cadmium/src/workbench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ use truck_shapeops::and as solid_and;
pub struct Workbench {
pub(crate) name: String,
pub(crate) history: Vec<Step>,
pub(crate) step_counters: HashMap<String, u64>,

// These are free-standing points in 3D space, not part of sketches
pub(crate) points: BTreeMap<IDType, Point3>,
pub(crate) points_next_id: IDType,
pub(crate) sketches: BTreeMap<IDType, ISketch>,
pub(crate) sketches_next_id: IDType,

pub(crate) planes: BTreeMap<IDType, Plane>,
pub(crate) planes_next_id: IDType,
pub(crate) solids: BTreeMap<IDType, Solid>,

pub(crate) sketches: BTreeMap<IDType, Rc<RefCell<ISketch>>>,
pub(crate) sketches_next_id: IDType,
pub(crate) solids: BTreeMap<IDType, Rc<RefCell<Solid>>>,
pub(crate) solids_next_id: IDType,
}

Expand All @@ -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
}
Expand All @@ -81,6 +79,10 @@ impl Workbench {
}
}

pub fn get_sketch_by_id(&mut self, id: IDType) -> Result<&mut Rc<RefCell<ISketch>>, 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() {
Expand All @@ -96,16 +98,13 @@ 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 {
name.to_owned()
};
self.history
.push(Step::new_extrusion(&extrusion_name, extrusion, *counter));
*counter += 1;
*counter - 1
}

pub fn realize(&self, max_steps: u64) -> Realization {
Expand Down

0 comments on commit e0c1fec

Please sign in to comment.