Skip to content

Commit

Permalink
support adding lines to sketches
Browse files Browse the repository at this point in the history
  • Loading branch information
MattFerraro committed May 6, 2024
1 parent ce71455 commit 3cf6d53
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/cadmium/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn stacked_cubes() {
});
el.append(Operation::SetSketchName {
sketch_id: sketch_id.clone(),
name: "Sketch1".to_string(),
name: "Original Sketch".to_string(),
});
el.append(Operation::SetSketchPlane {
sketch_id: sketch_id.clone(),
Expand Down
46 changes: 43 additions & 3 deletions packages/cadmium/src/oplog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::process::id;

use crate::project::{Plane, Project, Workbench};
use crate::project::{Plane, PlaneDescription, Project, StepData, Workbench};

pub type Sha = String;

Expand Down Expand Up @@ -291,8 +291,8 @@ impl EvolutionLog {
let step_idx = wb.step_id_from_unique_id(step_id).unwrap();
let step = wb.history.get_mut(step_idx as usize).unwrap();
let new_plane = plane; // this is just to change the name to avoid a collision
if let crate::project::StepData::Plane { plane, .. } = &mut step.data {
let temp_plane = std::mem::replace(plane, new_plane.clone());
if let StepData::Plane { plane, .. } = &mut step.data {
*plane = new_plane.clone();
} else {
unreachable!()
};
Expand All @@ -306,6 +306,46 @@ impl EvolutionLog {
let sketch_id = wb.add_blank_sketch("Untitled-Sketch");
sketches.insert(commit.id.clone(), (*workbench_index, sketch_id));
}
Operation::SetSketchName { sketch_id, name } => {
let (workbench_idx, step_id) = sketches.get(sketch_id).unwrap();
let mut wb = project.workbenches.get_mut(*workbench_idx).unwrap();
let step_idx = wb.step_id_from_unique_id(step_id).unwrap();
wb.history.get_mut(step_idx as usize).unwrap().name = name.to_owned();
}
Operation::SetSketchPlane {
sketch_id,
plane_id,
} => {
let (workbench_idx_sketch, sketch_id) = sketches.get(sketch_id).unwrap();
let (workbench_idx_plane, plane_id) = planes.get(plane_id).unwrap();
assert_eq!(workbench_idx_sketch, workbench_idx_plane);
let mut wb = project.workbenches.get_mut(*workbench_idx_plane).unwrap();
let step_idx = wb.step_id_from_unique_id(sketch_id).unwrap();
let step = wb.history.get_mut(step_idx as usize).unwrap();
if let StepData::Sketch {
plane_description, ..
} = &mut step.data
{
*plane_description = PlaneDescription::PlaneId(plane_id.clone());
} else {
unreachable!()
};
}
Operation::AddSketchLine {
sketch_id,
start,
end,
} => {
let (workbench_idx, sketch_id) = sketches.get(sketch_id).unwrap();
let mut wb = project.workbenches.get_mut(*workbench_idx).unwrap();
let step_idx = wb.step_id_from_unique_id(sketch_id).unwrap();
let step = wb.history.get_mut(step_idx as usize).unwrap();
if let StepData::Sketch { sketch, .. } = &mut step.data {
sketch.add_line_segment(start.0, start.1, end.0, end.1);
} else {
unreachable!()
};
}
_ => {}
}
}
Expand Down
19 changes: 14 additions & 5 deletions packages/cadmium/src/sketch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![allow(unused)]
use serde::{Deserialize, Serialize};
use tsify::Tsify;
use serde_with::{serde_as, DisplayFromStr};
use geo::line_intersection::{line_intersection, LineIntersection};
use geo::Line;
use geo::{point, Contains};
use geo::{within, Intersects};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use truck_polymesh::stl::PolygonMeshSTLFaceIterator;
use tsify::Tsify;

use core::panic;
use geo::LineString;
Expand Down Expand Up @@ -335,6 +335,15 @@ impl Sketch {
id
}

pub fn add_or_get_point(&mut self, x: f64, y: f64) -> u64 {
for (id, point) in self.points.iter() {
if (point.x - x).abs() < 1e-6 && (point.y - y).abs() < 1e-6 {
return *id;
}
}
self.add_point(x, y)
}

pub fn add_hidden_point(&mut self, x: f64, y: f64) -> u64 {
let id = self.highest_point_id + 1;
self.points.insert(id, Point2::new_hidden(x, y));
Expand Down Expand Up @@ -457,8 +466,8 @@ impl Sketch {
}

pub fn add_line_segment(&mut self, x0: f64, y0: f64, x1: f64, y1: f64) -> u64 {
let id0 = self.add_point(x0, y0);
let id1 = self.add_point(x1, y1);
let id0 = self.add_or_get_point(x0, y0);
let id1 = self.add_or_get_point(x1, y1);
let l = Line2 {
start: id0,
end: id1,
Expand Down

0 comments on commit 3cf6d53

Please sign in to comment.