Skip to content

Commit

Permalink
Finish the addition of node
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 da6c52f commit 55d0eb1
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 87 deletions.
65 changes: 51 additions & 14 deletions packages/cadmium/examples/project_simple_extrusion.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,65 @@
use cadmium::workbench::AddSketch;
use cadmium::archetypes::PlaneDescription;
use cadmium::feature::extrusion::{self, Direction, Mode};
use cadmium::project::Project;
use cadmium::message::MessageHandler as _;
use cadmium::isketch::primitive::{AddLine, SketchAddPointMessage};
use cadmium::archetypes::PlaneDescription;
use cadmium::message::MessageHandler as _;
use cadmium::project::Project;
use cadmium::workbench::AddSketch;

fn main() {
let p = Project::new("Test Project");
let wb_ref = p.workbenches.first().unwrap();
let plane_description = PlaneDescription::PlaneId(0);
let sketch_id = AddSketch { plane_description }.handle_message(wb_ref.clone()).unwrap().unwrap();
let sketch_id = AddSketch { plane_description }
.handle_message(wb_ref.clone())
.unwrap()
.unwrap()
.0;
let sketch = wb_ref.borrow().get_sketch_by_id(sketch_id).unwrap();

let ll = SketchAddPointMessage { x: 0.0, y: 0.0 }.handle_message(sketch.clone()).unwrap().unwrap();
let lr = SketchAddPointMessage { x: 40.0, y: 0.0 }.handle_message(sketch.clone()).unwrap().unwrap();
let ul = SketchAddPointMessage { x: 0.0, y: 40.0 }.handle_message(sketch.clone()).unwrap().unwrap();
let ur = SketchAddPointMessage { x: 40.0, y: 40.0 }.handle_message(sketch.clone()).unwrap().unwrap();
let ll = SketchAddPointMessage { x: 0.0, y: 0.0 }
.handle_message(sketch.clone())
.unwrap()
.unwrap()
.0;
let lr = SketchAddPointMessage { x: 40.0, y: 0.0 }
.handle_message(sketch.clone())
.unwrap()
.unwrap()
.0;
let ul = SketchAddPointMessage { x: 0.0, y: 40.0 }
.handle_message(sketch.clone())
.unwrap()
.unwrap()
.0;
let ur = SketchAddPointMessage { x: 40.0, y: 40.0 }
.handle_message(sketch.clone())
.unwrap()
.unwrap()
.0;

AddLine { start: ll, end: lr }.handle_message(sketch.clone()).unwrap();
AddLine { start: lr, end: ur }.handle_message(sketch.clone()).unwrap();
AddLine { start: ur, end: ul }.handle_message(sketch.clone()).unwrap();
AddLine { start: ul, end: ll }.handle_message(sketch.clone()).unwrap();
AddLine { start: ll, end: lr }
.handle_message(sketch.clone())
.unwrap();
AddLine { start: lr, end: ur }
.handle_message(sketch.clone())
.unwrap();
AddLine { start: ur, end: ul }
.handle_message(sketch.clone())
.unwrap();
AddLine { start: ul, end: ll }
.handle_message(sketch.clone())
.unwrap();

extrusion::Add { sketch_id, faces: vec![0], length: 25.0, offset: 0.0, direction: Direction::Normal, mode: Mode::New }.handle_message(wb_ref.clone()).unwrap();
extrusion::Add {
sketch_id,
faces: vec![0],
length: 25.0,
offset: 0.0,
direction: Direction::Normal,
mode: Mode::New,
}
.handle_message(wb_ref.clone())
.unwrap();

let wb = wb_ref.borrow();
let feature_ref = wb.features.first_key_value().unwrap().1;
Expand Down
51 changes: 36 additions & 15 deletions packages/cadmium/src/feature/extrusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::isketch::face::{FaceSelector, Selector};
use crate::isketch::ISketch;
use crate::message::MessageHandler;
use crate::workbench::Workbench;
use crate::IDType;
use crate::{interop, IDType};

use super::get_isoface_wires;
use super::{Feature, SolidLike};
Expand Down Expand Up @@ -86,10 +86,12 @@ impl SolidLike for Extrusion {

let extrusion_vector = extrusion_direction.times(self.length - self.offset);
let offset_vector = extrusion_direction.times(self.offset);
let extrusion_tvector = TruckVector3::new(extrusion_vector.x, extrusion_vector.y, extrusion_vector.z);
let extrusion_tvector =
TruckVector3::new(extrusion_vector.x, extrusion_vector.y, extrusion_vector.z);
let offset_tvector = TruckVector3::new(offset_vector.x, offset_vector.y, offset_vector.z);

Ok(self.faces
Ok(self
.faces
.get_selected_faces(&sketch)
.iter()
.map(|f| {
Expand All @@ -99,9 +101,9 @@ impl SolidLike for Extrusion {
// Can we calculate ALL the wires at once and not iter-sweep?
let sweep = builder::tsweep(&face, extrusion_tvector);


builder::translated(&sweep, offset_tvector)
}).collect())
})
.collect())
}
}

Expand All @@ -112,7 +114,7 @@ impl<'a> TryFrom<&'a mut Feature> for &'a mut Extrusion {
#[allow(irrefutable_let_patterns)]
fn try_from(value: &'a mut Feature) -> Result<Self, Self::Error> {
let Feature::Extrusion(ref mut extrusion) = value else {
return Err(anyhow::anyhow!("Failed to convert Feature to Extrusion"))
return Err(anyhow::anyhow!("Failed to convert Feature to Extrusion"));
};

Ok(extrusion)
Expand All @@ -133,7 +135,10 @@ pub struct Add {
impl MessageHandler for Add {
// Parent to workbench to add to solids and be able to reference the sketch
type Parent = Rc<RefCell<Workbench>>;
fn handle_message(&self, workbench_ref: Self::Parent) -> anyhow::Result<Option<IDType>> {
fn handle_message(
&self,
workbench_ref: Self::Parent,
) -> anyhow::Result<Option<(IDType, interop::Node)>> {
let mut workbench = workbench_ref.borrow_mut();
let sketch = workbench.get_sketch_by_id(self.sketch_id)?;

Expand All @@ -145,9 +150,10 @@ impl MessageHandler for Add {
self.direction.clone(),
self.mode.clone(),
);
let extrusion_cell = Rc::new(RefCell::new(extrusion.to_feature()));

let id = workbench.features_next_id;
workbench.features.insert(id, Rc::new(RefCell::new(extrusion.to_feature())));
workbench.features.insert(id, extrusion_cell);
workbench.features_next_id += 1;
let id = workbench.features_next_id - 1;
drop(workbench);
Expand All @@ -158,9 +164,10 @@ impl MessageHandler for Add {
extrusion_id: id,
sketch_id: self.sketch_id,
faces: self.faces.clone(),
}.handle_message(workbench_ref.clone())?;
}
.handle_message(workbench_ref.clone())?;

Ok(Some(id))
Ok(Some((id, interop::Node::Solid(extrusion.to_solids()?))))
}
}

Expand All @@ -175,11 +182,21 @@ pub struct UpdateFaces {
impl MessageHandler for UpdateFaces {
// Parent to workbench to add to solids and be able to reference the sketch
type Parent = Rc<RefCell<Workbench>>;
fn handle_message(&self, workbench_ref: Self::Parent) -> anyhow::Result<Option<IDType>> {
fn handle_message(
&self,
workbench_ref: Self::Parent,
) -> anyhow::Result<Option<(IDType, interop::Node)>> {
let workbench = workbench_ref.borrow_mut();
let feature_ref = workbench.features.get(&self.extrusion_id).ok_or(anyhow::anyhow!("No feature with ID {} was found", self.extrusion_id))?;
let feature_ref = workbench
.features
.get(&self.extrusion_id)
.ok_or(anyhow::anyhow!(
"No feature with ID {} was found",
self.extrusion_id
))?;
let sketch_ref = workbench.get_sketch_by_id(self.sketch_id)?;
let mut extrusion: RefMut<'_, Extrusion> = RefMut::map(feature_ref.borrow_mut(), |f| f.try_into().unwrap());
let mut extrusion: RefMut<'_, Extrusion> =
RefMut::map(feature_ref.borrow_mut(), |f| f.try_into().unwrap());

extrusion.faces = Selector::from_face_ids(&sketch_ref.borrow(), self.faces.clone());

Expand All @@ -199,8 +216,12 @@ pub struct UpdateForm {
impl MessageHandler for UpdateForm {
// Parent to workbench to add to solids and be able to reference the sketch
type Parent = Rc<RefCell<Feature>>;
fn handle_message(&self, feature_ref: Self::Parent) -> anyhow::Result<Option<IDType>> {
let mut extrusion: RefMut<'_, Extrusion> = RefMut::map(feature_ref.borrow_mut(), |f| f.try_into().unwrap());
fn handle_message(
&self,
feature_ref: Self::Parent,
) -> anyhow::Result<Option<(IDType, interop::Node)>> {
let mut extrusion: RefMut<'_, Extrusion> =
RefMut::map(feature_ref.borrow_mut(), |f| f.try_into().unwrap());

extrusion.length = self.length;
extrusion.offset = self.offset;
Expand Down
78 changes: 43 additions & 35 deletions packages/cadmium/src/feature/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::cell::RefCell;
use std::ops::{Add, Sub};
use std::rc::Rc;

use isotope::primitives::point2::Point2 as ISOPoint2;
use serde::{Deserialize, Serialize};
use truck_polymesh::Point3 as PolyTruckPoint3;
use isotope::primitives::point2::Point2 as ISOPoint2;
use tsify_next::Tsify;

use crate::archetypes::{Plane, Vector3};
Expand Down Expand Up @@ -51,7 +51,7 @@ impl Point3 {
(dx * dx + dy * dy + dz * dz).sqrt()
}

pub fn from_plane_point(plane: &Plane, point: &ISOPoint2) -> Point3 {
pub fn from_plane_point(plane: &Plane, point: &ISOPoint2) -> Point3 {
let o = plane.origin.clone();
let x = plane.primary.clone();
let y = plane.secondary.clone();
Expand All @@ -62,57 +62,62 @@ impl Point3 {
}

impl From<Point3> for PolyTruckPoint3 {
fn from(val: Point3) -> Self {
PolyTruckPoint3 {
x: val.x,
y: val.y,
z: val.z,
}
}
fn from(val: Point3) -> Self {
PolyTruckPoint3 {
x: val.x,
y: val.y,
z: val.z,
}
}
}

impl Add for Point3 {
type Output = Point3;

fn add(self, other: Point3) -> Point3 {
Point3 {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
hidden: false,
}
}
type Output = Point3;

fn add(self, other: Point3) -> Point3 {
Point3 {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
hidden: false,
}
}
}

impl Sub for Point3 {
type Output = Point3;

fn sub(self, other: Point3) -> Point3 {
Point3 {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
hidden: false,
}
}
type Output = Point3;

fn sub(self, other: Point3) -> Point3 {
Point3 {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
hidden: false,
}
}
}

impl PartialEq for Point3 {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y && self.z == other.z
}
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y && self.z == other.z
}
}

use crate::message::{Identifiable, MessageHandler};
use crate::workbench::Workbench;
use crate::IDType;
use crate::{interop, IDType};

impl Identifiable for Rc<RefCell<Point3>> {
type Parent = Rc<RefCell<Workbench>>;
const ID_NAME: &'static str = "point_id";

fn from_parent_id(parent: &Self::Parent, id: IDType) -> anyhow::Result<Self> {
Ok(parent.borrow().points.get(&id).ok_or(anyhow::anyhow!(""))?.clone())
Ok(parent
.borrow()
.points
.get(&id)
.ok_or(anyhow::anyhow!(""))?
.clone())
}
}

Expand All @@ -126,7 +131,10 @@ pub struct WorkbenchPointUpdate {

impl MessageHandler for WorkbenchPointUpdate {
type Parent = Rc<RefCell<Point3>>;
fn handle_message(&self, point_ref: Rc<RefCell<Point3>>) -> anyhow::Result<Option<IDType>> {
fn handle_message(
&self,
point_ref: Rc<RefCell<Point3>>,
) -> anyhow::Result<Option<(IDType, interop::Node)>> {
let mut point = point_ref.borrow_mut();
point.x = self.x;
point.y = self.y;
Expand Down
29 changes: 23 additions & 6 deletions packages/cadmium/src/isketch/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ use serde::{Deserialize, Serialize};
use tsify_next::Tsify;

use crate::message::{Identifiable, MessageHandler};
use crate::IDType;
use crate::{interop, IDType};

use super::{compound_rectangle, ISketch};

pub trait CompoundLike: Debug {
fn references(&self) -> Vec<PrimitiveCell>;
fn created_references(&self) -> Vec<PrimitiveCell>;
fn populate_created_references(&self, sketch: &mut isotope::sketch::Sketch) -> anyhow::Result<()> {
fn populate_created_references(
&self,
sketch: &mut isotope::sketch::Sketch,
) -> anyhow::Result<()> {
for reference in self.created_references() {
sketch.add_primitive(reference)?;
}
Expand All @@ -34,7 +37,12 @@ impl Identifiable for Rc<RefCell<Compound>> {
const ID_NAME: &'static str = "compound_id";

fn from_parent_id(parent: &Self::Parent, id: IDType) -> anyhow::Result<Self> {
Ok(parent.borrow().compounds.get(&id).ok_or(anyhow::anyhow!("No feature with ID {} was found", id))?.clone())
Ok(parent
.borrow()
.compounds
.get(&id)
.ok_or(anyhow::anyhow!("No feature with ID {} was found", id))?
.clone())
}
}

Expand All @@ -54,13 +62,22 @@ pub struct DeleteCompound {

impl MessageHandler for DeleteCompound {
type Parent = Rc<RefCell<ISketch>>;
fn handle_message(&self, sketch_ref: Rc<RefCell<ISketch>>) -> anyhow::Result<Option<IDType>> {
fn handle_message(
&self,
sketch_ref: Rc<RefCell<ISketch>>,
) -> anyhow::Result<Option<(IDType, interop::Node)>> {
let mut isketch = sketch_ref.borrow_mut();
let mut sketch = isketch.sketch.borrow_mut();
let compound = isketch.compounds.get(&self.id).ok_or(anyhow::anyhow!("No compound with ID {} was found", self.id))?;
let compound = isketch
.compounds
.get(&self.id)
.ok_or(anyhow::anyhow!("No compound with ID {} was found", self.id))?;

for reference in compound.borrow().as_compound_like().created_references() {
let id = sketch.get_primitive_id(&reference).ok_or(anyhow::anyhow!("Failed to find primitive with reference {:?}", reference))?;
let id = sketch.get_primitive_id(&reference).ok_or(anyhow::anyhow!(
"Failed to find primitive with reference {:?}",
reference
))?;
sketch.delete_primitive(id)?;
}
drop(sketch);
Expand Down
Loading

0 comments on commit 55d0eb1

Please sign in to comment.