Skip to content

Commit

Permalink
Fix most stuff
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Zervas <[email protected]>
  • Loading branch information
dzervas committed May 29, 2024
1 parent 3c1d81d commit 29cea1f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
19 changes: 10 additions & 9 deletions packages/cadmium-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,26 @@ pub fn derive_step_data(input: TokenStream) -> TokenStream {
}

// Process type and workbench_field
let mut field_var = quote! {};
let mut _field_var = quote! {};
let mut _parent_var_ = quote! { wb_ };
let id_arg_name = if let Some(f) = parent_type.clone() {
Ident::new(format!("{}_id", f.to_string().to_case(Case::Snake)).as_str(), f.span())
} else {
Ident::new("id", variant_name.span())
};
if let Some(field_ident) = workbench_field.clone() {
let field_name = Ident::new(field_ident.as_str(), field_ident.span());
field_var = quote! {
_field_var = quote! {
let parent_ref_ = wb_.#field_name
.get(& #id_arg_name)
.ok_or(anyhow::anyhow!("Could not find parent"))?;
.ok_or(anyhow::anyhow!(concat!("Could not find parent ", stringify!(#parent_type), " with ID {}"), #id_arg_name))?;
let mut parent_ = parent_ref_.borrow_mut();
};
_parent_var_ = quote! { parent_ };
} else if !skip_workbench {
field_var = quote! { let mut parent_ = wb_; };
_parent_var_ = quote! { wb_ };
} else {
field_var = quote! { let mut parent_ = self; };
_parent_var_ = quote! { self };
}

// Function declaration
Expand Down Expand Up @@ -116,15 +118,14 @@ pub fn derive_step_data(input: TokenStream) -> TokenStream {
quote! {
pub fn #add_func_name(&mut self, name: String, #( #function_defs ),*) -> Result<crate::IDType, anyhow::Error> {
#wb_var
#field_var
let result_id_ = parent_.#add_func_name(#( #function_args_noauto.clone() ),*)?;
drop(parent_);
#_field_var
let result_id_ = #_parent_var_.#add_func_name(#( #function_args_noauto.clone() ),*)?;

let step_ = Step {
name,
id: result_id_,
operation: StepOperation::Add,
unique_id: format!(concat!("Add:", stringify!(#name), "-{}"), result_id_),
unique_id: format!(concat!("Add:", stringify!(#variant_name), "-{}"), result_id_),
suppressed: false,
data: #name::#variant_name {
#( #function_args_full ),*
Expand Down
2 changes: 1 addition & 1 deletion packages/cadmium/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::ops::{Sub, SubAssign};

use cadmium::extrusion::fuse;
use cadmium::solid::extrusion::fuse;
use truck_meshalgo::filters::OptimizingFilter;
use truck_meshalgo::tessellation::{MeshableShape, MeshedShape};
use truck_modeling::builder::{translated, tsweep, vertex};
Expand Down
28 changes: 14 additions & 14 deletions packages/cadmium/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,26 @@ pub mod tests {
let mut p = Project::new("Test Project");
let plane_desc = PlaneDescription::PlaneId(0);
let sid = p.add_workbench_sketch("Sketch 1".to_string(), 0, plane_desc).unwrap();
let wb = p.workbenches.get_mut(0).unwrap();
let s_ref = wb.get_sketch_by_id(sid).unwrap();
let mut s = s_ref.borrow_mut();
let ll = s.add_sketch_point(Point2 { x: 0.0, y: 0.0, hidden: false }).unwrap();
let lr = s.add_sketch_point(Point2 { x: 40.0, y: 0.0, hidden: false }).unwrap();
let ul = s.add_sketch_point(Point2 { x: 0.0, y: 40.0, hidden: false }).unwrap();
let ur = s.add_sketch_point(Point2 { x: 40.0, y: 40.0, hidden: false }).unwrap();
s.add_sketch_line(ll, lr);
s.add_sketch_line(lr, ur);
s.add_sketch_line(ur, ul);
s.add_sketch_line(ul, ll);

wb.add_solid_extrusion(

let ll = p.add_sketch_point("bottom left".to_string(), 0, sid, Point2 { x: 0.0, y: 0.0, hidden: false }).unwrap();
let lr = p.add_sketch_point("bottom right".to_string(), 0, sid, Point2 { x: 40.0, y: 0.0, hidden: false }).unwrap();
let ul = p.add_sketch_point("top left".to_string(), 0, sid, Point2 { x: 0.0, y: 40.0, hidden: false }).unwrap();
let ur = p.add_sketch_point("top right".to_string(), 0, sid, Point2 { x: 40.0, y: 40.0, hidden: false }).unwrap();
p.add_sketch_line("bottom".to_string(), 0, sid, ll, lr).unwrap();
p.add_sketch_line("right".to_string(), 0, sid, lr, ur).unwrap();
p.add_sketch_line("up".to_string(), 0, sid, ur, ul).unwrap();
p.add_sketch_line("left".to_string(), 0, sid, ul, ll).unwrap();

p.add_solid_extrusion(
"Extrusion 1".to_string(),
0,
vec![0],
0,
25.0,
0.0,
Mode::New,
Direction::Normal,
);
).unwrap();

p
}
Expand Down
18 changes: 10 additions & 8 deletions packages/cadmium/src/workbench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct Workbench {

impl Workbench {
pub fn new(name: &str) -> Self {
println!("Creating new workbench: {:?}", name);
let mut wb = Workbench {
name: name.to_owned(),
history: vec![],
Expand Down Expand Up @@ -81,6 +82,7 @@ impl Workbench {
}

pub fn get_sketch_by_id(&self, id: IDType) -> Result<Rc<RefCell<ISketch>>, CADmiumError> {
println!("Getting sketch by id: {:?} {:?}", id, self.sketches);
self.sketches.get(&id).ok_or(CADmiumError::SketchIDNotFound(id)).cloned()
}

Expand Down Expand Up @@ -137,8 +139,8 @@ impl Workbench {
..
} => match plane_description {
PlaneDescription::PlaneId(plane_id) => {
let plane = realized.planes.get(&plane_id).ok_or(anyhow::anyhow!("Failed to find plane with id {}", plane_id))?;
let plane_ref = Rc::new(RefCell::new(plane.plane.clone()));
let plane = self.planes.get(&plane_id).ok_or(anyhow::anyhow!("Failed to find plane with id {}", plane_id))?;
let plane_ref = plane.clone();
let sketch = ISketch::new(plane_ref);

realized.sketches.insert(
Expand Down Expand Up @@ -217,7 +219,7 @@ impl Workbench {

match &new_extrusion.mode {
extrusion::Mode::New => {
new_solids.iter().map(|s| {
new_solids.iter().for_each(|s| {
realized.solids.insert(self.solids_next_id, s.clone());
self.solids_next_id += 1;
});
Expand Down Expand Up @@ -286,14 +288,14 @@ impl Workbench {
// Step operations
impl Workbench {
pub(super) fn add_workbench_point(&mut self, point: Point3) -> Result<IDType, anyhow::Error> {
self.points.insert(self.points_next_id, point).ok_or(anyhow::anyhow!("Failed to insert point"));
self.points.insert(self.points_next_id, point);
self.points_next_id += 1;
Ok(self.points_next_id - 1)
}

pub(super) fn add_workbench_plane(&mut self, plane: Plane, _width: f64, _height: f64) -> Result<IDType, anyhow::Error> {
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.insert(self.planes_next_id, plane_cell);
self.planes_next_id += 1;
Ok(self.planes_next_id - 1)
}
Expand All @@ -302,16 +304,16 @@ impl Workbench {
&mut self,
plane_description: PlaneDescription,
) -> Result<IDType, anyhow::Error> {
println!("Adding sketch with plane description: {:?}", plane_description);
let plane = match plane_description {
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"),
}.clone();

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"));
self.sketches.insert(self.sketches_next_id, Rc::new(RefCell::new(sketch)));
println!("Added sketch with id: {:?}", self.sketches);
self.sketches_next_id += 1;
Ok(self.sketches_next_id - 1)
}
Expand Down

0 comments on commit 29cea1f

Please sign in to comment.