From 3cf6d537493d07fe847f23693a73fe2eaa320645 Mon Sep 17 00:00:00 2001 From: Matt Ferraro Date: Mon, 6 May 2024 03:50:47 -0400 Subject: [PATCH] support adding lines to sketches --- packages/cadmium/src/main.rs | 2 +- packages/cadmium/src/oplog/mod.rs | 46 ++++++++++++++++++++++++++++-- packages/cadmium/src/sketch/mod.rs | 19 ++++++++---- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/packages/cadmium/src/main.rs b/packages/cadmium/src/main.rs index dd99680f..7441557d 100644 --- a/packages/cadmium/src/main.rs +++ b/packages/cadmium/src/main.rs @@ -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(), diff --git a/packages/cadmium/src/oplog/mod.rs b/packages/cadmium/src/oplog/mod.rs index a0ba631c..16183629 100644 --- a/packages/cadmium/src/oplog/mod.rs +++ b/packages/cadmium/src/oplog/mod.rs @@ -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; @@ -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!() }; @@ -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!() + }; + } _ => {} } } diff --git a/packages/cadmium/src/sketch/mod.rs b/packages/cadmium/src/sketch/mod.rs index e4f32714..0739b3ce 100644 --- a/packages/cadmium/src/sketch/mod.rs +++ b/packages/cadmium/src/sketch/mod.rs @@ -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; @@ -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)); @@ -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,