1use std::f64::consts::TAU;
2use std::ops::Add;
3use std::ops::AddAssign;
4use std::ops::Mul;
5use std::ops::Sub;
6use std::ops::SubAssign;
7
8use anyhow::Result;
9use indexmap::IndexMap;
10use kcl_error::SourceRange;
11use kittycad_modeling_cmds::ModelingCmd;
12use kittycad_modeling_cmds::each_cmd as mcmd;
13use kittycad_modeling_cmds::length_unit::LengthUnit;
14use kittycad_modeling_cmds::units::UnitLength;
15use kittycad_modeling_cmds::websocket::ModelingCmdReq;
16use kittycad_modeling_cmds::{self as kcmc};
17use parse_display::Display;
18use parse_display::FromStr;
19use serde::Deserialize;
20use serde::Serialize;
21use uuid::Uuid;
22
23use crate::NodePath;
24use crate::engine::DEFAULT_PLANE_INFO;
25use crate::engine::PlaneName;
26use crate::errors::KclError;
27use crate::errors::KclErrorDetails;
28use crate::exec::KclValue;
29use crate::execution::ArtifactId;
30use crate::execution::ExecState;
31use crate::execution::ExecutorContext;
32use crate::execution::Metadata;
33use crate::execution::TagEngineInfo;
34use crate::execution::TagIdentifier;
35use crate::execution::normalize_to_solver_distance_unit;
36use crate::execution::types::NumericType;
37use crate::execution::types::adjust_length;
38use crate::front::ArcCtor;
39use crate::front::CircleCtor;
40use crate::front::Freedom;
41use crate::front::LineCtor;
42use crate::front::ObjectId;
43use crate::front::PointCtor;
44use crate::parsing::ast::types::Node;
45use crate::parsing::ast::types::NodeRef;
46use crate::parsing::ast::types::TagDeclarator;
47use crate::parsing::ast::types::TagNode;
48use crate::std::Args;
49use crate::std::args::TyF64;
50use crate::std::sketch::FaceTag;
51use crate::std::sketch::PlaneData;
52
53type Point3D = kcmc::shared::Point3d<f64>;
54
55#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
57#[ts(export)]
58#[serde(tag = "type", rename_all = "camelCase")]
59pub struct GdtAnnotation {
60 pub id: uuid::Uuid,
62 #[serde(skip)]
63 pub meta: Vec<Metadata>,
64}
65
66#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
68#[ts(export)]
69#[serde(tag = "type")]
70#[allow(clippy::large_enum_variant)]
71pub enum Geometry {
72 Sketch(Sketch),
73 Solid(Solid),
74}
75
76impl Geometry {
77 pub fn id(&self) -> uuid::Uuid {
78 match self {
79 Geometry::Sketch(s) => s.id,
80 Geometry::Solid(e) => e.id,
81 }
82 }
83
84 pub fn original_id(&self) -> uuid::Uuid {
88 match self {
89 Geometry::Sketch(s) => s.original_id,
90 Geometry::Solid(e) => e.original_id(),
91 }
92 }
93}
94
95#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
97#[ts(export)]
98#[serde(tag = "type")]
99#[allow(clippy::large_enum_variant)]
100pub enum GeometryWithImportedGeometry {
101 Sketch(Sketch),
102 Solid(Solid),
103 ImportedGeometry(Box<ImportedGeometry>),
104}
105
106impl GeometryWithImportedGeometry {
107 pub async fn id(&mut self, ctx: &ExecutorContext) -> Result<uuid::Uuid, KclError> {
108 match self {
109 GeometryWithImportedGeometry::Sketch(s) => Ok(s.id),
110 GeometryWithImportedGeometry::Solid(e) => Ok(e.id),
111 GeometryWithImportedGeometry::ImportedGeometry(i) => {
112 let id = i.id(ctx).await?;
113 Ok(id)
114 }
115 }
116 }
117}
118
119#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
121#[ts(export)]
122#[serde(tag = "type")]
123#[allow(clippy::vec_box)]
124pub enum Geometries {
125 Sketches(Vec<Sketch>),
126 Solids(Vec<Solid>),
127}
128
129impl From<Geometry> for Geometries {
130 fn from(value: Geometry) -> Self {
131 match value {
132 Geometry::Sketch(x) => Self::Sketches(vec![x]),
133 Geometry::Solid(x) => Self::Solids(vec![x]),
134 }
135 }
136}
137
138#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
140#[ts(export)]
141#[serde(rename_all = "camelCase")]
142pub struct ImportedGeometry {
143 pub id: uuid::Uuid,
145 pub value: Vec<String>,
147 #[serde(skip)]
148 pub meta: Vec<Metadata>,
149 #[serde(skip)]
151 completed: bool,
152}
153
154impl ImportedGeometry {
155 pub fn new(id: uuid::Uuid, value: Vec<String>, meta: Vec<Metadata>) -> Self {
156 Self {
157 id,
158 value,
159 meta,
160 completed: false,
161 }
162 }
163
164 async fn wait_for_finish(&mut self, ctx: &ExecutorContext) -> Result<(), KclError> {
165 if self.completed {
166 return Ok(());
167 }
168
169 ctx.engine
170 .ensure_async_command_completed(self.id, self.meta.first().map(|m| m.source_range))
171 .await?;
172
173 self.completed = true;
174
175 Ok(())
176 }
177
178 pub async fn id(&mut self, ctx: &ExecutorContext) -> Result<uuid::Uuid, KclError> {
179 if !self.completed {
180 self.wait_for_finish(ctx).await?;
181 }
182
183 Ok(self.id)
184 }
185}
186
187#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
189#[ts(export)]
190#[serde(tag = "type", rename_all = "camelCase")]
191#[allow(clippy::vec_box)]
192pub enum HideableGeometry {
193 ImportedGeometry(Box<ImportedGeometry>),
194 SolidSet(Vec<Solid>),
195 SketchSet(Vec<Sketch>),
196 HelixSet(Vec<Helix>),
197}
198
199impl From<HideableGeometry> for crate::execution::KclValue {
200 fn from(value: HideableGeometry) -> Self {
201 match value {
202 HideableGeometry::ImportedGeometry(s) => crate::execution::KclValue::ImportedGeometry(*s),
203 HideableGeometry::SolidSet(mut s) => {
204 if s.len() == 1
205 && let Some(s) = s.pop()
206 {
207 crate::execution::KclValue::Solid { value: Box::new(s) }
208 } else {
209 crate::execution::KclValue::HomArray {
210 value: s
211 .into_iter()
212 .map(|s| crate::execution::KclValue::Solid { value: Box::new(s) })
213 .collect(),
214 ty: crate::execution::types::RuntimeType::solid(),
215 }
216 }
217 }
218 HideableGeometry::SketchSet(mut s) => {
219 if s.len() == 1
220 && let Some(s) = s.pop()
221 {
222 crate::execution::KclValue::Sketch { value: Box::new(s) }
223 } else {
224 crate::execution::KclValue::HomArray {
225 value: s
226 .into_iter()
227 .map(|s| crate::execution::KclValue::Sketch { value: Box::new(s) })
228 .collect(),
229 ty: crate::execution::types::RuntimeType::sketch(),
230 }
231 }
232 }
233 HideableGeometry::HelixSet(mut s) => {
234 if s.len() == 1
235 && let Some(s) = s.pop()
236 {
237 crate::execution::KclValue::Helix { value: Box::new(s) }
238 } else {
239 crate::execution::KclValue::HomArray {
240 value: s
241 .into_iter()
242 .map(|s| crate::execution::KclValue::Helix { value: Box::new(s) })
243 .collect(),
244 ty: crate::execution::types::RuntimeType::helices(),
245 }
246 }
247 }
248 }
249 }
250}
251
252impl HideableGeometry {
253 pub(crate) async fn ids(&mut self, ctx: &ExecutorContext) -> Result<Vec<uuid::Uuid>, KclError> {
254 match self {
255 HideableGeometry::ImportedGeometry(s) => {
256 let id = s.id(ctx).await?;
257
258 Ok(vec![id])
259 }
260 HideableGeometry::SolidSet(s) => Ok(s.iter().map(|s| s.id).collect()),
261 HideableGeometry::SketchSet(s) => Ok(s.iter().map(|s| s.id).collect()),
262 HideableGeometry::HelixSet(s) => Ok(s.iter().map(|s| s.value).collect()),
263 }
264 }
265}
266
267#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
269#[ts(export)]
270#[serde(tag = "type", rename_all = "camelCase")]
271#[allow(clippy::vec_box)]
272pub enum SolidOrSketchOrImportedGeometry {
273 ImportedGeometry(Box<ImportedGeometry>),
274 SolidSet(Vec<Solid>),
275 SketchSet(Vec<Sketch>),
276}
277
278impl From<SolidOrSketchOrImportedGeometry> for crate::execution::KclValue {
279 fn from(value: SolidOrSketchOrImportedGeometry) -> Self {
280 match value {
281 SolidOrSketchOrImportedGeometry::ImportedGeometry(s) => crate::execution::KclValue::ImportedGeometry(*s),
282 SolidOrSketchOrImportedGeometry::SolidSet(mut s) => {
283 if s.len() == 1
284 && let Some(s) = s.pop()
285 {
286 crate::execution::KclValue::Solid { value: Box::new(s) }
287 } else {
288 crate::execution::KclValue::HomArray {
289 value: s
290 .into_iter()
291 .map(|s| crate::execution::KclValue::Solid { value: Box::new(s) })
292 .collect(),
293 ty: crate::execution::types::RuntimeType::solid(),
294 }
295 }
296 }
297 SolidOrSketchOrImportedGeometry::SketchSet(mut s) => {
298 if s.len() == 1
299 && let Some(s) = s.pop()
300 {
301 crate::execution::KclValue::Sketch { value: Box::new(s) }
302 } else {
303 crate::execution::KclValue::HomArray {
304 value: s
305 .into_iter()
306 .map(|s| crate::execution::KclValue::Sketch { value: Box::new(s) })
307 .collect(),
308 ty: crate::execution::types::RuntimeType::sketch(),
309 }
310 }
311 }
312 }
313 }
314}
315
316impl SolidOrSketchOrImportedGeometry {
317 pub(crate) async fn ids(&mut self, ctx: &ExecutorContext) -> Result<Vec<uuid::Uuid>, KclError> {
318 match self {
319 SolidOrSketchOrImportedGeometry::ImportedGeometry(s) => {
320 let id = s.id(ctx).await?;
321
322 Ok(vec![id])
323 }
324 SolidOrSketchOrImportedGeometry::SolidSet(s) => Ok(s.iter().map(|s| s.id).collect()),
325 SolidOrSketchOrImportedGeometry::SketchSet(s) => Ok(s.iter().map(|s| s.id).collect()),
326 }
327 }
328}
329
330#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
332#[ts(export)]
333#[serde(tag = "type", rename_all = "camelCase")]
334#[allow(clippy::vec_box)]
335pub enum SolidOrImportedGeometry {
336 ImportedGeometry(Box<ImportedGeometry>),
337 SolidSet(Vec<Solid>),
338}
339
340impl From<SolidOrImportedGeometry> for crate::execution::KclValue {
341 fn from(value: SolidOrImportedGeometry) -> Self {
342 match value {
343 SolidOrImportedGeometry::ImportedGeometry(s) => crate::execution::KclValue::ImportedGeometry(*s),
344 SolidOrImportedGeometry::SolidSet(mut s) => {
345 if s.len() == 1
346 && let Some(s) = s.pop()
347 {
348 crate::execution::KclValue::Solid { value: Box::new(s) }
349 } else {
350 crate::execution::KclValue::HomArray {
351 value: s
352 .into_iter()
353 .map(|s| crate::execution::KclValue::Solid { value: Box::new(s) })
354 .collect(),
355 ty: crate::execution::types::RuntimeType::solid(),
356 }
357 }
358 }
359 }
360 }
361}
362
363impl SolidOrImportedGeometry {
364 pub(crate) async fn ids(&mut self, ctx: &ExecutorContext) -> Result<Vec<uuid::Uuid>, KclError> {
365 match self {
366 SolidOrImportedGeometry::ImportedGeometry(s) => {
367 let id = s.id(ctx).await?;
368
369 Ok(vec![id])
370 }
371 SolidOrImportedGeometry::SolidSet(s) => Ok(s.iter().map(|s| s.id).collect()),
372 }
373 }
374}
375
376#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
378#[ts(export)]
379#[serde(rename_all = "camelCase")]
380pub struct Helix {
381 pub value: uuid::Uuid,
383 pub artifact_id: ArtifactId,
385 pub revolutions: f64,
387 pub angle_start: f64,
389 pub ccw: bool,
391 pub cylinder_id: Option<uuid::Uuid>,
393 pub units: UnitLength,
394 #[serde(skip)]
395 pub meta: Vec<Metadata>,
396}
397
398#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
399#[ts(export)]
400#[serde(rename_all = "camelCase")]
401pub struct Plane {
402 pub id: uuid::Uuid,
404 pub artifact_id: ArtifactId,
406 #[serde(skip_serializing_if = "Option::is_none")]
409 pub object_id: Option<ObjectId>,
410 pub kind: PlaneKind,
412 #[serde(flatten)]
414 pub info: PlaneInfo,
415 #[serde(skip)]
416 pub meta: Vec<Metadata>,
417}
418
419#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ts_rs::TS)]
420#[ts(export)]
421#[serde(rename_all = "camelCase")]
422pub struct PlaneInfo {
423 pub origin: Point3d,
425 pub x_axis: Point3d,
427 pub y_axis: Point3d,
429 pub z_axis: Point3d,
431}
432
433impl PlaneInfo {
434 pub(crate) fn into_plane_data(self) -> PlaneData {
435 if self.origin.is_zero() {
436 match self {
437 Self {
438 origin:
439 Point3d {
440 x: 0.0,
441 y: 0.0,
442 z: 0.0,
443 units: Some(UnitLength::Millimeters),
444 },
445 x_axis:
446 Point3d {
447 x: 1.0,
448 y: 0.0,
449 z: 0.0,
450 units: _,
451 },
452 y_axis:
453 Point3d {
454 x: 0.0,
455 y: 1.0,
456 z: 0.0,
457 units: _,
458 },
459 z_axis: _,
460 } => return PlaneData::XY,
461 Self {
462 origin:
463 Point3d {
464 x: 0.0,
465 y: 0.0,
466 z: 0.0,
467 units: Some(UnitLength::Millimeters),
468 },
469 x_axis:
470 Point3d {
471 x: -1.0,
472 y: 0.0,
473 z: 0.0,
474 units: _,
475 },
476 y_axis:
477 Point3d {
478 x: 0.0,
479 y: 1.0,
480 z: 0.0,
481 units: _,
482 },
483 z_axis: _,
484 } => return PlaneData::NegXY,
485 Self {
486 origin:
487 Point3d {
488 x: 0.0,
489 y: 0.0,
490 z: 0.0,
491 units: Some(UnitLength::Millimeters),
492 },
493 x_axis:
494 Point3d {
495 x: 1.0,
496 y: 0.0,
497 z: 0.0,
498 units: _,
499 },
500 y_axis:
501 Point3d {
502 x: 0.0,
503 y: 0.0,
504 z: 1.0,
505 units: _,
506 },
507 z_axis: _,
508 } => return PlaneData::XZ,
509 Self {
510 origin:
511 Point3d {
512 x: 0.0,
513 y: 0.0,
514 z: 0.0,
515 units: Some(UnitLength::Millimeters),
516 },
517 x_axis:
518 Point3d {
519 x: -1.0,
520 y: 0.0,
521 z: 0.0,
522 units: _,
523 },
524 y_axis:
525 Point3d {
526 x: 0.0,
527 y: 0.0,
528 z: 1.0,
529 units: _,
530 },
531 z_axis: _,
532 } => return PlaneData::NegXZ,
533 Self {
534 origin:
535 Point3d {
536 x: 0.0,
537 y: 0.0,
538 z: 0.0,
539 units: Some(UnitLength::Millimeters),
540 },
541 x_axis:
542 Point3d {
543 x: 0.0,
544 y: 1.0,
545 z: 0.0,
546 units: _,
547 },
548 y_axis:
549 Point3d {
550 x: 0.0,
551 y: 0.0,
552 z: 1.0,
553 units: _,
554 },
555 z_axis: _,
556 } => return PlaneData::YZ,
557 Self {
558 origin:
559 Point3d {
560 x: 0.0,
561 y: 0.0,
562 z: 0.0,
563 units: Some(UnitLength::Millimeters),
564 },
565 x_axis:
566 Point3d {
567 x: 0.0,
568 y: -1.0,
569 z: 0.0,
570 units: _,
571 },
572 y_axis:
573 Point3d {
574 x: 0.0,
575 y: 0.0,
576 z: 1.0,
577 units: _,
578 },
579 z_axis: _,
580 } => return PlaneData::NegYZ,
581 _ => {}
582 }
583 }
584
585 PlaneData::Plane(Self {
586 origin: self.origin,
587 x_axis: self.x_axis,
588 y_axis: self.y_axis,
589 z_axis: self.z_axis,
590 })
591 }
592
593 pub(crate) fn is_right_handed(&self) -> bool {
594 let lhs = self
597 .x_axis
598 .axes_cross_product(&self.y_axis)
599 .axes_dot_product(&self.z_axis);
600 let rhs_x = self.x_axis.axes_dot_product(&self.x_axis);
601 let rhs_y = self.y_axis.axes_dot_product(&self.y_axis);
602 let rhs_z = self.z_axis.axes_dot_product(&self.z_axis);
603 let rhs = (rhs_x * rhs_y * rhs_z).sqrt();
604 (lhs - rhs).abs() <= 0.0001
606 }
607
608 #[cfg(test)]
609 pub(crate) fn is_left_handed(&self) -> bool {
610 !self.is_right_handed()
611 }
612
613 pub(crate) fn make_right_handed(self) -> Self {
614 if self.is_right_handed() {
615 return self;
616 }
617 Self {
619 origin: self.origin,
620 x_axis: self.x_axis.negated(),
621 y_axis: self.y_axis,
622 z_axis: self.z_axis,
623 }
624 }
625}
626
627impl TryFrom<PlaneData> for PlaneInfo {
628 type Error = KclError;
629
630 fn try_from(value: PlaneData) -> Result<Self, Self::Error> {
631 let name = match value {
632 PlaneData::XY => PlaneName::Xy,
633 PlaneData::NegXY => PlaneName::NegXy,
634 PlaneData::XZ => PlaneName::Xz,
635 PlaneData::NegXZ => PlaneName::NegXz,
636 PlaneData::YZ => PlaneName::Yz,
637 PlaneData::NegYZ => PlaneName::NegYz,
638 PlaneData::Plane(info) => {
639 return Ok(info);
640 }
641 };
642
643 let info = DEFAULT_PLANE_INFO.get(&name).ok_or_else(|| {
644 KclError::new_internal(KclErrorDetails::new(
645 format!("Plane {name} not found"),
646 Default::default(),
647 ))
648 })?;
649
650 Ok(info.clone())
651 }
652}
653
654impl From<&PlaneData> for PlaneKind {
655 fn from(value: &PlaneData) -> Self {
656 match value {
657 PlaneData::XY => PlaneKind::XY,
658 PlaneData::NegXY => PlaneKind::XY,
659 PlaneData::XZ => PlaneKind::XZ,
660 PlaneData::NegXZ => PlaneKind::XZ,
661 PlaneData::YZ => PlaneKind::YZ,
662 PlaneData::NegYZ => PlaneKind::YZ,
663 PlaneData::Plane(_) => PlaneKind::Custom,
664 }
665 }
666}
667
668impl From<&PlaneInfo> for PlaneKind {
669 fn from(value: &PlaneInfo) -> Self {
670 let data = PlaneData::Plane(value.clone());
671 PlaneKind::from(&data)
672 }
673}
674
675impl From<PlaneInfo> for PlaneKind {
676 fn from(value: PlaneInfo) -> Self {
677 let data = PlaneData::Plane(value);
678 PlaneKind::from(&data)
679 }
680}
681
682impl Plane {
683 #[cfg(test)]
684 pub(crate) fn from_plane_data_skipping_engine(
685 value: PlaneData,
686 exec_state: &mut ExecState,
687 ) -> Result<Self, KclError> {
688 let id = exec_state.next_uuid();
689 let kind = PlaneKind::from(&value);
690 Ok(Plane {
691 id,
692 artifact_id: id.into(),
693 info: PlaneInfo::try_from(value)?,
694 object_id: None,
695 kind,
696 meta: vec![],
697 })
698 }
699
700 pub fn is_initialized(&self) -> bool {
702 self.object_id.is_some()
703 }
704
705 pub fn is_uninitialized(&self) -> bool {
707 !self.is_initialized()
708 }
709
710 pub fn is_standard(&self) -> bool {
712 match &self.kind {
713 PlaneKind::XY | PlaneKind::YZ | PlaneKind::XZ => true,
714 PlaneKind::Custom => false,
715 }
716 }
717
718 pub fn project(&self, point: Point3d) -> Point3d {
721 let v = point - self.info.origin;
722 let dot = v.axes_dot_product(&self.info.z_axis);
723
724 point - self.info.z_axis * dot
725 }
726}
727
728#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
730#[ts(export)]
731#[serde(rename_all = "camelCase")]
732pub struct Face {
733 pub id: uuid::Uuid,
735 pub artifact_id: ArtifactId,
737 pub object_id: ObjectId,
739 pub value: String,
741 pub x_axis: Point3d,
743 pub y_axis: Point3d,
745 pub solid: Box<Solid>,
747 pub units: UnitLength,
748 #[serde(skip)]
749 pub meta: Vec<Metadata>,
750}
751
752#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
754#[ts(export)]
755#[serde(rename_all = "camelCase")]
756pub struct BoundedEdge {
757 pub face_id: uuid::Uuid,
759 pub edge_id: uuid::Uuid,
761 pub lower_bound: f32,
764 pub upper_bound: f32,
767}
768
769#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq, ts_rs::TS, FromStr, Display)]
771#[ts(export)]
772#[display(style = "camelCase")]
773pub enum PlaneKind {
774 #[serde(rename = "XY", alias = "xy")]
775 #[display("XY")]
776 XY,
777 #[serde(rename = "XZ", alias = "xz")]
778 #[display("XZ")]
779 XZ,
780 #[serde(rename = "YZ", alias = "yz")]
781 #[display("YZ")]
782 YZ,
783 #[display("Custom")]
785 Custom,
786}
787
788#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
789#[ts(export)]
790#[serde(tag = "type", rename_all = "camelCase")]
791pub struct Sketch {
792 pub id: uuid::Uuid,
794 pub paths: Vec<Path>,
798 #[serde(default, skip_serializing_if = "Vec::is_empty")]
800 pub inner_paths: Vec<Path>,
801 pub on: SketchSurface,
803 pub start: BasePath,
805 #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
807 pub tags: IndexMap<String, TagIdentifier>,
808 pub artifact_id: ArtifactId,
811 #[ts(skip)]
812 pub original_id: uuid::Uuid,
813 #[serde(skip_serializing_if = "Option::is_none")]
818 #[ts(skip)]
819 pub origin_sketch_id: Option<uuid::Uuid>,
820 #[serde(skip)]
822 pub mirror: Option<uuid::Uuid>,
823 #[serde(skip)]
825 pub clone: Option<uuid::Uuid>,
826 #[serde(skip)]
828 #[ts(skip)]
829 pub synthetic_jump_path_ids: Vec<uuid::Uuid>,
830 pub units: UnitLength,
831 #[serde(skip)]
833 pub meta: Vec<Metadata>,
834 #[serde(
837 default = "ProfileClosed::explicitly",
838 skip_serializing_if = "ProfileClosed::is_explicitly"
839 )]
840 pub is_closed: ProfileClosed,
841}
842
843impl ProfileClosed {
844 #[expect(dead_code, reason = "it's not actually dead, it's called by serde")]
845 fn explicitly() -> Self {
846 Self::Explicitly
847 }
848
849 fn is_explicitly(&self) -> bool {
850 matches!(self, ProfileClosed::Explicitly)
851 }
852}
853
854#[derive(Debug, Serialize, Eq, PartialEq, Clone, Copy, Hash, Ord, PartialOrd, ts_rs::TS)]
856#[serde(rename_all = "camelCase")]
857pub enum ProfileClosed {
858 No,
860 Maybe,
862 Implicitly,
864 Explicitly,
866}
867
868impl Sketch {
869 pub(crate) fn build_sketch_mode_cmds(
872 &self,
873 exec_state: &mut ExecState,
874 inner_cmd: ModelingCmdReq,
875 ) -> Vec<ModelingCmdReq> {
876 vec![
877 ModelingCmdReq {
880 cmd: ModelingCmd::from(
881 mcmd::EnableSketchMode::builder()
882 .animated(false)
883 .ortho(false)
884 .entity_id(self.on.id())
885 .adjust_camera(false)
886 .maybe_planar_normal(if let SketchSurface::Plane(plane) = &self.on {
887 let normal = plane.info.x_axis.axes_cross_product(&plane.info.y_axis);
889 Some(normal.into())
890 } else {
891 None
892 })
893 .build(),
894 ),
895 cmd_id: exec_state.next_uuid().into(),
896 },
897 inner_cmd,
898 ModelingCmdReq {
899 cmd: ModelingCmd::SketchModeDisable(mcmd::SketchModeDisable::builder().build()),
900 cmd_id: exec_state.next_uuid().into(),
901 },
902 ]
903 }
904}
905
906#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
908#[ts(export)]
909#[serde(tag = "type", rename_all = "camelCase")]
910pub enum SketchSurface {
911 Plane(Box<Plane>),
912 Face(Box<Face>),
913}
914
915impl SketchSurface {
916 pub(crate) fn id(&self) -> uuid::Uuid {
917 match self {
918 SketchSurface::Plane(plane) => plane.id,
919 SketchSurface::Face(face) => face.id,
920 }
921 }
922 pub(crate) fn x_axis(&self) -> Point3d {
923 match self {
924 SketchSurface::Plane(plane) => plane.info.x_axis,
925 SketchSurface::Face(face) => face.x_axis,
926 }
927 }
928 pub(crate) fn y_axis(&self) -> Point3d {
929 match self {
930 SketchSurface::Plane(plane) => plane.info.y_axis,
931 SketchSurface::Face(face) => face.y_axis,
932 }
933 }
934
935 pub(crate) fn object_id(&self) -> Option<ObjectId> {
936 match self {
937 SketchSurface::Plane(plane) => plane.object_id,
938 SketchSurface::Face(face) => Some(face.object_id),
939 }
940 }
941
942 pub(crate) fn set_object_id(&mut self, object_id: ObjectId) {
943 match self {
944 SketchSurface::Plane(plane) => plane.object_id = Some(object_id),
945 SketchSurface::Face(face) => face.object_id = object_id,
946 }
947 }
948}
949
950#[derive(Debug, Clone, PartialEq)]
952pub enum Extrudable {
953 Sketch(Box<Sketch>),
955 Face(FaceTag),
957}
958
959impl Extrudable {
960 pub async fn id_to_extrude(
962 &self,
963 exec_state: &mut ExecState,
964 args: &Args,
965 must_be_planar: bool,
966 ) -> Result<uuid::Uuid, KclError> {
967 match self {
968 Extrudable::Sketch(sketch) => Ok(sketch.id),
969 Extrudable::Face(face_tag) => face_tag.get_face_id_from_tag(exec_state, args, must_be_planar).await,
970 }
971 }
972
973 pub fn as_sketch(&self) -> Option<Sketch> {
974 match self {
975 Extrudable::Sketch(sketch) => Some((**sketch).clone()),
976 Extrudable::Face(face_tag) => match face_tag.geometry() {
977 Some(Geometry::Sketch(sketch)) => Some(sketch),
978 Some(Geometry::Solid(solid)) => solid.sketch().cloned(),
979 None => None,
980 },
981 }
982 }
983
984 pub fn is_closed(&self) -> ProfileClosed {
985 match self {
986 Extrudable::Sketch(sketch) => sketch.is_closed,
987 Extrudable::Face(face_tag) => match face_tag.geometry() {
988 Some(Geometry::Sketch(sketch)) => sketch.is_closed,
989 Some(Geometry::Solid(solid)) => solid
990 .sketch()
991 .map(|sketch| sketch.is_closed)
992 .unwrap_or(ProfileClosed::Maybe),
993 _ => ProfileClosed::Maybe,
994 },
995 }
996 }
997}
998
999impl From<Sketch> for Extrudable {
1000 fn from(value: Sketch) -> Self {
1001 Extrudable::Sketch(Box::new(value))
1002 }
1003}
1004
1005#[derive(Debug, Clone)]
1006pub(crate) enum GetTangentialInfoFromPathsResult {
1007 PreviousPoint([f64; 2]),
1008 Arc {
1009 center: [f64; 2],
1010 ccw: bool,
1011 },
1012 Circle {
1013 center: [f64; 2],
1014 ccw: bool,
1015 radius: f64,
1016 },
1017 Ellipse {
1018 center: [f64; 2],
1019 ccw: bool,
1020 major_axis: [f64; 2],
1021 _minor_radius: f64,
1022 },
1023}
1024
1025impl GetTangentialInfoFromPathsResult {
1026 pub(crate) fn tan_previous_point(&self, last_arc_end: [f64; 2]) -> [f64; 2] {
1027 match self {
1028 GetTangentialInfoFromPathsResult::PreviousPoint(p) => *p,
1029 GetTangentialInfoFromPathsResult::Arc { center, ccw } => {
1030 crate::std::utils::get_tangent_point_from_previous_arc(*center, *ccw, last_arc_end)
1031 }
1032 GetTangentialInfoFromPathsResult::Circle {
1035 center, radius, ccw, ..
1036 } => [center[0] + radius, center[1] + if *ccw { -1.0 } else { 1.0 }],
1037 GetTangentialInfoFromPathsResult::Ellipse {
1038 center,
1039 major_axis,
1040 ccw,
1041 ..
1042 } => [center[0] + major_axis[0], center[1] + if *ccw { -1.0 } else { 1.0 }],
1043 }
1044 }
1045}
1046
1047impl Sketch {
1048 pub(crate) fn add_tag(
1049 &mut self,
1050 tag: NodeRef<'_, TagDeclarator>,
1051 current_path: &Path,
1052 exec_state: &ExecState,
1053 surface: Option<&ExtrudeSurface>,
1054 ) {
1055 let mut tag_identifier: TagIdentifier = tag.into();
1056 let base = current_path.get_base();
1057 let mut sketch_copy = self.clone();
1058 sketch_copy.tags.clear();
1059 tag_identifier.info.push((
1060 exec_state.stack().current_epoch(),
1061 TagEngineInfo {
1062 id: base.geo_meta.id,
1063 geometry: Geometry::Sketch(sketch_copy),
1064 path: Some(current_path.clone()),
1065 surface: surface.cloned(),
1066 },
1067 ));
1068
1069 self.tags.insert(tag.name.to_string(), tag_identifier);
1070 }
1071
1072 pub(crate) fn merge_tags<'a>(&mut self, tags: impl Iterator<Item = &'a TagIdentifier>) {
1073 for t in tags {
1074 match self.tags.get_mut(&t.value) {
1075 Some(id) => {
1076 id.merge_info(t);
1077 }
1078 None => {
1079 self.tags.insert(t.value.clone(), t.clone());
1080 }
1081 }
1082 }
1083 }
1084
1085 pub(crate) fn latest_path(&self) -> Option<&Path> {
1087 self.paths.last()
1088 }
1089
1090 pub(crate) fn current_pen_position(&self) -> Result<Point2d, KclError> {
1094 let Some(path) = self.latest_path() else {
1095 return Ok(Point2d::new(self.start.to[0], self.start.to[1], self.start.units));
1096 };
1097
1098 let to = path.get_base().to;
1099 Ok(Point2d::new(to[0], to[1], path.get_base().units))
1100 }
1101
1102 pub(crate) fn get_tangential_info_from_paths(&self) -> GetTangentialInfoFromPathsResult {
1103 let Some(path) = self.latest_path() else {
1104 return GetTangentialInfoFromPathsResult::PreviousPoint(self.start.to);
1105 };
1106 path.get_tangential_info()
1107 }
1108}
1109
1110#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1111#[ts(export)]
1112#[serde(tag = "type", rename_all = "camelCase")]
1113pub struct Solid {
1114 pub id: uuid::Uuid,
1116 pub artifact_id: ArtifactId,
1118 pub value: Vec<ExtrudeSurface>,
1120 #[serde(rename = "sketch")]
1122 pub creator: SolidCreator,
1123 pub start_cap_id: Option<uuid::Uuid>,
1125 pub end_cap_id: Option<uuid::Uuid>,
1127 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1129 pub edge_cuts: Vec<EdgeCut>,
1130 pub units: UnitLength,
1132 pub sectional: bool,
1134 #[serde(skip)]
1136 pub meta: Vec<Metadata>,
1137}
1138
1139#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1140#[ts(export)]
1141pub struct CreatorFace {
1142 pub face_id: uuid::Uuid,
1144 pub solid_id: uuid::Uuid,
1146 pub sketch: Sketch,
1148}
1149
1150#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1152#[ts(export)]
1153#[serde(tag = "creatorType", rename_all = "camelCase")]
1154pub enum SolidCreator {
1155 Sketch(Sketch),
1157 Face(CreatorFace),
1159 Procedural,
1161}
1162
1163impl Solid {
1164 pub fn sketch(&self) -> Option<&Sketch> {
1165 match &self.creator {
1166 SolidCreator::Sketch(sketch) => Some(sketch),
1167 SolidCreator::Face(CreatorFace { sketch, .. }) => Some(sketch),
1168 SolidCreator::Procedural => None,
1169 }
1170 }
1171
1172 pub fn sketch_mut(&mut self) -> Option<&mut Sketch> {
1173 match &mut self.creator {
1174 SolidCreator::Sketch(sketch) => Some(sketch),
1175 SolidCreator::Face(CreatorFace { sketch, .. }) => Some(sketch),
1176 SolidCreator::Procedural => None,
1177 }
1178 }
1179
1180 pub fn sketch_id(&self) -> Option<uuid::Uuid> {
1181 self.sketch().map(|sketch| sketch.id)
1182 }
1183
1184 pub fn original_id(&self) -> uuid::Uuid {
1185 self.sketch().map(|sketch| sketch.original_id).unwrap_or(self.id)
1186 }
1187
1188 pub(crate) fn get_all_edge_cut_ids(&self) -> impl Iterator<Item = uuid::Uuid> + '_ {
1189 self.edge_cuts.iter().map(|foc| foc.id())
1190 }
1191}
1192
1193#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1195#[ts(export)]
1196#[serde(tag = "type", rename_all = "camelCase")]
1197pub enum EdgeCut {
1198 Fillet {
1200 id: uuid::Uuid,
1202 radius: TyF64,
1203 #[serde(rename = "edgeId")]
1205 edge_id: uuid::Uuid,
1206 tag: Box<Option<TagNode>>,
1207 },
1208 Chamfer {
1210 id: uuid::Uuid,
1212 length: TyF64,
1213 #[serde(rename = "edgeId")]
1215 edge_id: uuid::Uuid,
1216 tag: Box<Option<TagNode>>,
1217 },
1218}
1219
1220impl EdgeCut {
1221 pub fn id(&self) -> uuid::Uuid {
1222 match self {
1223 EdgeCut::Fillet { id, .. } => *id,
1224 EdgeCut::Chamfer { id, .. } => *id,
1225 }
1226 }
1227
1228 pub fn set_id(&mut self, id: uuid::Uuid) {
1229 match self {
1230 EdgeCut::Fillet { id: i, .. } => *i = id,
1231 EdgeCut::Chamfer { id: i, .. } => *i = id,
1232 }
1233 }
1234
1235 pub fn edge_id(&self) -> uuid::Uuid {
1236 match self {
1237 EdgeCut::Fillet { edge_id, .. } => *edge_id,
1238 EdgeCut::Chamfer { edge_id, .. } => *edge_id,
1239 }
1240 }
1241
1242 pub fn set_edge_id(&mut self, id: uuid::Uuid) {
1243 match self {
1244 EdgeCut::Fillet { edge_id: i, .. } => *i = id,
1245 EdgeCut::Chamfer { edge_id: i, .. } => *i = id,
1246 }
1247 }
1248
1249 pub fn tag(&self) -> Option<TagNode> {
1250 match self {
1251 EdgeCut::Fillet { tag, .. } => *tag.clone(),
1252 EdgeCut::Chamfer { tag, .. } => *tag.clone(),
1253 }
1254 }
1255}
1256
1257#[derive(Debug, Serialize, PartialEq, Clone, Copy, ts_rs::TS)]
1258#[ts(export)]
1259pub struct Point2d {
1260 pub x: f64,
1261 pub y: f64,
1262 pub units: UnitLength,
1263}
1264
1265impl Point2d {
1266 pub const ZERO: Self = Self {
1267 x: 0.0,
1268 y: 0.0,
1269 units: UnitLength::Millimeters,
1270 };
1271
1272 pub fn new(x: f64, y: f64, units: UnitLength) -> Self {
1273 Self { x, y, units }
1274 }
1275
1276 pub fn into_x(self) -> TyF64 {
1277 TyF64::new(self.x, self.units.into())
1278 }
1279
1280 pub fn into_y(self) -> TyF64 {
1281 TyF64::new(self.y, self.units.into())
1282 }
1283
1284 pub fn ignore_units(self) -> [f64; 2] {
1285 [self.x, self.y]
1286 }
1287}
1288
1289#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, Copy, ts_rs::TS, Default)]
1290#[ts(export)]
1291pub struct Point3d {
1292 pub x: f64,
1293 pub y: f64,
1294 pub z: f64,
1295 pub units: Option<UnitLength>,
1296}
1297
1298impl Point3d {
1299 pub const ZERO: Self = Self {
1300 x: 0.0,
1301 y: 0.0,
1302 z: 0.0,
1303 units: Some(UnitLength::Millimeters),
1304 };
1305
1306 pub fn new(x: f64, y: f64, z: f64, units: Option<UnitLength>) -> Self {
1307 Self { x, y, z, units }
1308 }
1309
1310 pub const fn is_zero(&self) -> bool {
1311 self.x == 0.0 && self.y == 0.0 && self.z == 0.0
1312 }
1313
1314 pub fn axes_cross_product(&self, other: &Self) -> Self {
1319 Self {
1320 x: self.y * other.z - self.z * other.y,
1321 y: self.z * other.x - self.x * other.z,
1322 z: self.x * other.y - self.y * other.x,
1323 units: None,
1324 }
1325 }
1326
1327 pub fn axes_dot_product(&self, other: &Self) -> f64 {
1332 let x = self.x * other.x;
1333 let y = self.y * other.y;
1334 let z = self.z * other.z;
1335 x + y + z
1336 }
1337
1338 pub fn normalize(&self) -> Self {
1339 let len = f64::sqrt(self.x * self.x + self.y * self.y + self.z * self.z);
1340 Point3d {
1341 x: self.x / len,
1342 y: self.y / len,
1343 z: self.z / len,
1344 units: None,
1345 }
1346 }
1347
1348 pub fn as_3_dims(&self) -> ([f64; 3], Option<UnitLength>) {
1349 let p = [self.x, self.y, self.z];
1350 let u = self.units;
1351 (p, u)
1352 }
1353
1354 pub(crate) fn negated(self) -> Self {
1355 Self {
1356 x: -self.x,
1357 y: -self.y,
1358 z: -self.z,
1359 units: self.units,
1360 }
1361 }
1362}
1363
1364impl From<[TyF64; 3]> for Point3d {
1365 fn from(p: [TyF64; 3]) -> Self {
1366 Self {
1367 x: p[0].n,
1368 y: p[1].n,
1369 z: p[2].n,
1370 units: p[0].ty.as_length(),
1371 }
1372 }
1373}
1374
1375impl From<Point3d> for Point3D {
1376 fn from(p: Point3d) -> Self {
1377 Self { x: p.x, y: p.y, z: p.z }
1378 }
1379}
1380
1381impl From<Point3d> for kittycad_modeling_cmds::shared::Point3d<LengthUnit> {
1382 fn from(p: Point3d) -> Self {
1383 if let Some(units) = p.units {
1384 Self {
1385 x: LengthUnit(adjust_length(units, p.x, UnitLength::Millimeters).0),
1386 y: LengthUnit(adjust_length(units, p.y, UnitLength::Millimeters).0),
1387 z: LengthUnit(adjust_length(units, p.z, UnitLength::Millimeters).0),
1388 }
1389 } else {
1390 Self {
1391 x: LengthUnit(p.x),
1392 y: LengthUnit(p.y),
1393 z: LengthUnit(p.z),
1394 }
1395 }
1396 }
1397}
1398
1399impl Add for Point3d {
1400 type Output = Point3d;
1401
1402 fn add(self, rhs: Self) -> Self::Output {
1403 Point3d {
1405 x: self.x + rhs.x,
1406 y: self.y + rhs.y,
1407 z: self.z + rhs.z,
1408 units: self.units,
1409 }
1410 }
1411}
1412
1413impl AddAssign for Point3d {
1414 fn add_assign(&mut self, rhs: Self) {
1415 *self = *self + rhs
1416 }
1417}
1418
1419impl Sub for Point3d {
1420 type Output = Point3d;
1421
1422 fn sub(self, rhs: Self) -> Self::Output {
1423 let (x, y, z) = if rhs.units != self.units
1424 && let Some(sunits) = self.units
1425 && let Some(runits) = rhs.units
1426 {
1427 (
1428 adjust_length(runits, rhs.x, sunits).0,
1429 adjust_length(runits, rhs.y, sunits).0,
1430 adjust_length(runits, rhs.z, sunits).0,
1431 )
1432 } else {
1433 (rhs.x, rhs.y, rhs.z)
1434 };
1435 Point3d {
1436 x: self.x - x,
1437 y: self.y - y,
1438 z: self.z - z,
1439 units: self.units,
1440 }
1441 }
1442}
1443
1444impl SubAssign for Point3d {
1445 fn sub_assign(&mut self, rhs: Self) {
1446 *self = *self - rhs
1447 }
1448}
1449
1450impl Mul<f64> for Point3d {
1451 type Output = Point3d;
1452
1453 fn mul(self, rhs: f64) -> Self::Output {
1454 Point3d {
1455 x: self.x * rhs,
1456 y: self.y * rhs,
1457 z: self.z * rhs,
1458 units: self.units,
1459 }
1460 }
1461}
1462
1463#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1465#[ts(export)]
1466#[serde(rename_all = "camelCase")]
1467pub struct BasePath {
1468 #[ts(type = "[number, number]")]
1470 pub from: [f64; 2],
1471 #[ts(type = "[number, number]")]
1473 pub to: [f64; 2],
1474 pub units: UnitLength,
1475 pub tag: Option<TagNode>,
1477 #[serde(rename = "__geoMeta")]
1479 pub geo_meta: GeoMeta,
1480}
1481
1482impl BasePath {
1483 pub fn get_to(&self) -> [TyF64; 2] {
1484 let ty: NumericType = self.units.into();
1485 [TyF64::new(self.to[0], ty), TyF64::new(self.to[1], ty)]
1486 }
1487
1488 pub fn get_from(&self) -> [TyF64; 2] {
1489 let ty: NumericType = self.units.into();
1490 [TyF64::new(self.from[0], ty), TyF64::new(self.from[1], ty)]
1491 }
1492}
1493
1494#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1496#[ts(export)]
1497#[serde(rename_all = "camelCase")]
1498pub struct GeoMeta {
1499 pub id: uuid::Uuid,
1501 #[serde(flatten)]
1503 pub metadata: Metadata,
1504}
1505
1506#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1508#[ts(export)]
1509#[serde(tag = "type")]
1510pub enum Path {
1511 ToPoint {
1513 #[serde(flatten)]
1514 base: BasePath,
1515 },
1516 TangentialArcTo {
1518 #[serde(flatten)]
1519 base: BasePath,
1520 #[ts(type = "[number, number]")]
1522 center: [f64; 2],
1523 ccw: bool,
1525 },
1526 TangentialArc {
1528 #[serde(flatten)]
1529 base: BasePath,
1530 #[ts(type = "[number, number]")]
1532 center: [f64; 2],
1533 ccw: bool,
1535 },
1536 Circle {
1539 #[serde(flatten)]
1540 base: BasePath,
1541 #[ts(type = "[number, number]")]
1543 center: [f64; 2],
1544 radius: f64,
1546 ccw: bool,
1549 },
1550 CircleThreePoint {
1551 #[serde(flatten)]
1552 base: BasePath,
1553 #[ts(type = "[number, number]")]
1555 p1: [f64; 2],
1556 #[ts(type = "[number, number]")]
1558 p2: [f64; 2],
1559 #[ts(type = "[number, number]")]
1561 p3: [f64; 2],
1562 },
1563 ArcThreePoint {
1564 #[serde(flatten)]
1565 base: BasePath,
1566 #[ts(type = "[number, number]")]
1568 p1: [f64; 2],
1569 #[ts(type = "[number, number]")]
1571 p2: [f64; 2],
1572 #[ts(type = "[number, number]")]
1574 p3: [f64; 2],
1575 },
1576 Horizontal {
1578 #[serde(flatten)]
1579 base: BasePath,
1580 x: f64,
1582 },
1583 AngledLineTo {
1585 #[serde(flatten)]
1586 base: BasePath,
1587 x: Option<f64>,
1589 y: Option<f64>,
1591 },
1592 Base {
1594 #[serde(flatten)]
1595 base: BasePath,
1596 },
1597 Arc {
1599 #[serde(flatten)]
1600 base: BasePath,
1601 center: [f64; 2],
1603 radius: f64,
1605 ccw: bool,
1607 },
1608 Ellipse {
1609 #[serde(flatten)]
1610 base: BasePath,
1611 center: [f64; 2],
1612 major_axis: [f64; 2],
1613 minor_radius: f64,
1614 ccw: bool,
1615 },
1616 Conic {
1618 #[serde(flatten)]
1619 base: BasePath,
1620 },
1621 Bezier {
1623 #[serde(flatten)]
1624 base: BasePath,
1625 #[ts(type = "[number, number]")]
1627 control1: [f64; 2],
1628 #[ts(type = "[number, number]")]
1630 control2: [f64; 2],
1631 },
1632}
1633
1634impl Path {
1635 pub fn get_id(&self) -> uuid::Uuid {
1636 match self {
1637 Path::ToPoint { base } => base.geo_meta.id,
1638 Path::Horizontal { base, .. } => base.geo_meta.id,
1639 Path::AngledLineTo { base, .. } => base.geo_meta.id,
1640 Path::Base { base } => base.geo_meta.id,
1641 Path::TangentialArcTo { base, .. } => base.geo_meta.id,
1642 Path::TangentialArc { base, .. } => base.geo_meta.id,
1643 Path::Circle { base, .. } => base.geo_meta.id,
1644 Path::CircleThreePoint { base, .. } => base.geo_meta.id,
1645 Path::Arc { base, .. } => base.geo_meta.id,
1646 Path::ArcThreePoint { base, .. } => base.geo_meta.id,
1647 Path::Ellipse { base, .. } => base.geo_meta.id,
1648 Path::Conic { base, .. } => base.geo_meta.id,
1649 Path::Bezier { base, .. } => base.geo_meta.id,
1650 }
1651 }
1652
1653 pub fn set_id(&mut self, id: uuid::Uuid) {
1654 match self {
1655 Path::ToPoint { base } => base.geo_meta.id = id,
1656 Path::Horizontal { base, .. } => base.geo_meta.id = id,
1657 Path::AngledLineTo { base, .. } => base.geo_meta.id = id,
1658 Path::Base { base } => base.geo_meta.id = id,
1659 Path::TangentialArcTo { base, .. } => base.geo_meta.id = id,
1660 Path::TangentialArc { base, .. } => base.geo_meta.id = id,
1661 Path::Circle { base, .. } => base.geo_meta.id = id,
1662 Path::CircleThreePoint { base, .. } => base.geo_meta.id = id,
1663 Path::Arc { base, .. } => base.geo_meta.id = id,
1664 Path::ArcThreePoint { base, .. } => base.geo_meta.id = id,
1665 Path::Ellipse { base, .. } => base.geo_meta.id = id,
1666 Path::Conic { base, .. } => base.geo_meta.id = id,
1667 Path::Bezier { base, .. } => base.geo_meta.id = id,
1668 }
1669 }
1670
1671 pub fn get_tag(&self) -> Option<TagNode> {
1672 match self {
1673 Path::ToPoint { base } => base.tag.clone(),
1674 Path::Horizontal { base, .. } => base.tag.clone(),
1675 Path::AngledLineTo { base, .. } => base.tag.clone(),
1676 Path::Base { base } => base.tag.clone(),
1677 Path::TangentialArcTo { base, .. } => base.tag.clone(),
1678 Path::TangentialArc { base, .. } => base.tag.clone(),
1679 Path::Circle { base, .. } => base.tag.clone(),
1680 Path::CircleThreePoint { base, .. } => base.tag.clone(),
1681 Path::Arc { base, .. } => base.tag.clone(),
1682 Path::ArcThreePoint { base, .. } => base.tag.clone(),
1683 Path::Ellipse { base, .. } => base.tag.clone(),
1684 Path::Conic { base, .. } => base.tag.clone(),
1685 Path::Bezier { base, .. } => base.tag.clone(),
1686 }
1687 }
1688
1689 pub fn get_base(&self) -> &BasePath {
1690 match self {
1691 Path::ToPoint { base } => base,
1692 Path::Horizontal { base, .. } => base,
1693 Path::AngledLineTo { base, .. } => base,
1694 Path::Base { base } => base,
1695 Path::TangentialArcTo { base, .. } => base,
1696 Path::TangentialArc { base, .. } => base,
1697 Path::Circle { base, .. } => base,
1698 Path::CircleThreePoint { base, .. } => base,
1699 Path::Arc { base, .. } => base,
1700 Path::ArcThreePoint { base, .. } => base,
1701 Path::Ellipse { base, .. } => base,
1702 Path::Conic { base, .. } => base,
1703 Path::Bezier { base, .. } => base,
1704 }
1705 }
1706
1707 pub fn get_from(&self) -> [TyF64; 2] {
1709 let p = &self.get_base().from;
1710 let ty: NumericType = self.get_base().units.into();
1711 [TyF64::new(p[0], ty), TyF64::new(p[1], ty)]
1712 }
1713
1714 pub fn get_to(&self) -> [TyF64; 2] {
1716 let p = &self.get_base().to;
1717 let ty: NumericType = self.get_base().units.into();
1718 [TyF64::new(p[0], ty), TyF64::new(p[1], ty)]
1719 }
1720
1721 pub fn start_point_components(&self) -> ([f64; 2], NumericType) {
1723 let p = &self.get_base().from;
1724 let ty: NumericType = self.get_base().units.into();
1725 (*p, ty)
1726 }
1727
1728 pub fn end_point_components(&self) -> ([f64; 2], NumericType) {
1730 let p = &self.get_base().to;
1731 let ty: NumericType = self.get_base().units.into();
1732 (*p, ty)
1733 }
1734
1735 pub fn length(&self) -> Option<TyF64> {
1738 let n = match self {
1739 Self::ToPoint { .. } | Self::Base { .. } | Self::Horizontal { .. } | Self::AngledLineTo { .. } => {
1740 Some(linear_distance(&self.get_base().from, &self.get_base().to))
1741 }
1742 Self::TangentialArc {
1743 base: _,
1744 center,
1745 ccw: _,
1746 }
1747 | Self::TangentialArcTo {
1748 base: _,
1749 center,
1750 ccw: _,
1751 } => {
1752 let radius = linear_distance(&self.get_base().from, center);
1755 debug_assert_eq!(radius, linear_distance(&self.get_base().to, center));
1756 Some(linear_distance(&self.get_base().from, &self.get_base().to))
1758 }
1759 Self::Circle { radius, .. } => Some(TAU * radius),
1760 Self::CircleThreePoint { .. } => {
1761 let circle_center = crate::std::utils::calculate_circle_from_3_points([
1762 self.get_base().from,
1763 self.get_base().to,
1764 self.get_base().to,
1765 ]);
1766 let radius = linear_distance(
1767 &[circle_center.center[0], circle_center.center[1]],
1768 &self.get_base().from,
1769 );
1770 Some(TAU * radius)
1771 }
1772 Self::Arc { .. } => {
1773 Some(linear_distance(&self.get_base().from, &self.get_base().to))
1775 }
1776 Self::ArcThreePoint { .. } => {
1777 Some(linear_distance(&self.get_base().from, &self.get_base().to))
1779 }
1780 Self::Ellipse { .. } => {
1781 None
1783 }
1784 Self::Conic { .. } => {
1785 None
1787 }
1788 Self::Bezier { .. } => {
1789 None
1791 }
1792 };
1793 n.map(|n| TyF64::new(n, self.get_base().units.into()))
1794 }
1795
1796 pub fn get_base_mut(&mut self) -> &mut BasePath {
1797 match self {
1798 Path::ToPoint { base } => base,
1799 Path::Horizontal { base, .. } => base,
1800 Path::AngledLineTo { base, .. } => base,
1801 Path::Base { base } => base,
1802 Path::TangentialArcTo { base, .. } => base,
1803 Path::TangentialArc { base, .. } => base,
1804 Path::Circle { base, .. } => base,
1805 Path::CircleThreePoint { base, .. } => base,
1806 Path::Arc { base, .. } => base,
1807 Path::ArcThreePoint { base, .. } => base,
1808 Path::Ellipse { base, .. } => base,
1809 Path::Conic { base, .. } => base,
1810 Path::Bezier { base, .. } => base,
1811 }
1812 }
1813
1814 pub(crate) fn get_tangential_info(&self) -> GetTangentialInfoFromPathsResult {
1815 match self {
1816 Path::TangentialArc { center, ccw, .. }
1817 | Path::TangentialArcTo { center, ccw, .. }
1818 | Path::Arc { center, ccw, .. } => GetTangentialInfoFromPathsResult::Arc {
1819 center: *center,
1820 ccw: *ccw,
1821 },
1822 Path::ArcThreePoint { p1, p2, p3, .. } => {
1823 let circle = crate::std::utils::calculate_circle_from_3_points([*p1, *p2, *p3]);
1824 GetTangentialInfoFromPathsResult::Arc {
1825 center: circle.center,
1826 ccw: crate::std::utils::is_points_ccw(&[*p1, *p2, *p3]) > 0,
1827 }
1828 }
1829 Path::Circle {
1830 center, ccw, radius, ..
1831 } => GetTangentialInfoFromPathsResult::Circle {
1832 center: *center,
1833 ccw: *ccw,
1834 radius: *radius,
1835 },
1836 Path::CircleThreePoint { p1, p2, p3, .. } => {
1837 let circle = crate::std::utils::calculate_circle_from_3_points([*p1, *p2, *p3]);
1838 let center_point = [circle.center[0], circle.center[1]];
1839 GetTangentialInfoFromPathsResult::Circle {
1840 center: center_point,
1841 ccw: true,
1843 radius: circle.radius,
1844 }
1845 }
1846 Path::Ellipse {
1848 center,
1849 major_axis,
1850 minor_radius,
1851 ccw,
1852 ..
1853 } => GetTangentialInfoFromPathsResult::Ellipse {
1854 center: *center,
1855 major_axis: *major_axis,
1856 _minor_radius: *minor_radius,
1857 ccw: *ccw,
1858 },
1859 Path::Conic { .. }
1860 | Path::ToPoint { .. }
1861 | Path::Horizontal { .. }
1862 | Path::AngledLineTo { .. }
1863 | Path::Base { .. }
1864 | Path::Bezier { .. } => {
1865 let base = self.get_base();
1866 GetTangentialInfoFromPathsResult::PreviousPoint(base.from)
1867 }
1868 }
1869 }
1870
1871 pub(crate) fn is_straight_line(&self) -> bool {
1873 matches!(self, Path::AngledLineTo { .. } | Path::ToPoint { .. })
1874 }
1875}
1876
1877#[rustfmt::skip]
1879fn linear_distance(
1880 [x0, y0]: &[f64; 2],
1881 [x1, y1]: &[f64; 2]
1882) -> f64 {
1883 let y_sq = (y1 - y0).powi(2);
1884 let x_sq = (x1 - x0).powi(2);
1885 (y_sq + x_sq).sqrt()
1886}
1887
1888#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1890#[ts(export)]
1891#[serde(tag = "type", rename_all = "camelCase")]
1892pub enum ExtrudeSurface {
1893 ExtrudePlane(ExtrudePlane),
1895 ExtrudeArc(ExtrudeArc),
1896 Chamfer(ChamferSurface),
1897 Fillet(FilletSurface),
1898}
1899
1900#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1902#[ts(export)]
1903#[serde(rename_all = "camelCase")]
1904pub struct ChamferSurface {
1905 pub face_id: uuid::Uuid,
1907 pub tag: Option<Node<TagDeclarator>>,
1909 #[serde(flatten)]
1911 pub geo_meta: GeoMeta,
1912}
1913
1914#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1916#[ts(export)]
1917#[serde(rename_all = "camelCase")]
1918pub struct FilletSurface {
1919 pub face_id: uuid::Uuid,
1921 pub tag: Option<Node<TagDeclarator>>,
1923 #[serde(flatten)]
1925 pub geo_meta: GeoMeta,
1926}
1927
1928#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1930#[ts(export)]
1931#[serde(rename_all = "camelCase")]
1932pub struct ExtrudePlane {
1933 pub face_id: uuid::Uuid,
1935 pub tag: Option<Node<TagDeclarator>>,
1937 #[serde(flatten)]
1939 pub geo_meta: GeoMeta,
1940}
1941
1942#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1944#[ts(export)]
1945#[serde(rename_all = "camelCase")]
1946pub struct ExtrudeArc {
1947 pub face_id: uuid::Uuid,
1949 pub tag: Option<Node<TagDeclarator>>,
1951 #[serde(flatten)]
1953 pub geo_meta: GeoMeta,
1954}
1955
1956impl ExtrudeSurface {
1957 pub fn get_id(&self) -> uuid::Uuid {
1958 match self {
1959 ExtrudeSurface::ExtrudePlane(ep) => ep.geo_meta.id,
1960 ExtrudeSurface::ExtrudeArc(ea) => ea.geo_meta.id,
1961 ExtrudeSurface::Fillet(f) => f.geo_meta.id,
1962 ExtrudeSurface::Chamfer(c) => c.geo_meta.id,
1963 }
1964 }
1965
1966 pub fn face_id(&self) -> uuid::Uuid {
1967 match self {
1968 ExtrudeSurface::ExtrudePlane(ep) => ep.face_id,
1969 ExtrudeSurface::ExtrudeArc(ea) => ea.face_id,
1970 ExtrudeSurface::Fillet(f) => f.face_id,
1971 ExtrudeSurface::Chamfer(c) => c.face_id,
1972 }
1973 }
1974
1975 pub fn set_face_id(&mut self, face_id: uuid::Uuid) {
1976 match self {
1977 ExtrudeSurface::ExtrudePlane(ep) => ep.face_id = face_id,
1978 ExtrudeSurface::ExtrudeArc(ea) => ea.face_id = face_id,
1979 ExtrudeSurface::Fillet(f) => f.face_id = face_id,
1980 ExtrudeSurface::Chamfer(c) => c.face_id = face_id,
1981 }
1982 }
1983
1984 pub fn set_surface_tag(&mut self, tag: &TagNode) {
1985 match self {
1986 ExtrudeSurface::ExtrudePlane(extrude_plane) => extrude_plane.tag = Some(tag.clone()),
1987 ExtrudeSurface::ExtrudeArc(extrude_arc) => extrude_arc.tag = Some(tag.clone()),
1988 ExtrudeSurface::Chamfer(chamfer) => chamfer.tag = Some(tag.clone()),
1989 ExtrudeSurface::Fillet(fillet) => fillet.tag = Some(tag.clone()),
1990 }
1991 }
1992
1993 pub fn get_tag(&self) -> Option<Node<TagDeclarator>> {
1994 match self {
1995 ExtrudeSurface::ExtrudePlane(ep) => ep.tag.clone(),
1996 ExtrudeSurface::ExtrudeArc(ea) => ea.tag.clone(),
1997 ExtrudeSurface::Fillet(f) => f.tag.clone(),
1998 ExtrudeSurface::Chamfer(c) => c.tag.clone(),
1999 }
2000 }
2001}
2002
2003#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, ts_rs::TS)]
2004pub struct SketchVarId(pub usize);
2005
2006impl SketchVarId {
2007 pub fn to_constraint_id(self, range: SourceRange) -> Result<ezpz::Id, KclError> {
2008 self.0.try_into().map_err(|_| {
2009 KclError::new_type(KclErrorDetails::new(
2010 "Cannot convert to constraint ID since the sketch variable ID is too large".to_owned(),
2011 vec![range],
2012 ))
2013 })
2014 }
2015}
2016
2017#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2018#[ts(export_to = "Geometry.ts")]
2019#[serde(rename_all = "camelCase")]
2020pub struct SketchVar {
2021 pub id: SketchVarId,
2022 pub initial_value: f64,
2023 pub ty: NumericType,
2024 #[serde(skip)]
2025 pub meta: Vec<Metadata>,
2026}
2027
2028impl SketchVar {
2029 pub fn initial_value_to_solver_units(
2030 &self,
2031 exec_state: &mut ExecState,
2032 source_range: SourceRange,
2033 description: &str,
2034 ) -> Result<TyF64, KclError> {
2035 let x_initial_value = KclValue::Number {
2036 value: self.initial_value,
2037 ty: self.ty,
2038 meta: vec![source_range.into()],
2039 };
2040 let normalized_value =
2041 normalize_to_solver_distance_unit(&x_initial_value, source_range, exec_state, description)?;
2042 normalized_value.as_ty_f64().ok_or_else(|| {
2043 let message = format!(
2044 "Expected number after coercion, but found {}",
2045 normalized_value.human_friendly_type()
2046 );
2047 debug_assert!(false, "{}", &message);
2048 KclError::new_internal(KclErrorDetails::new(message, vec![source_range]))
2049 })
2050 }
2051}
2052
2053#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2054#[ts(export_to = "Geometry.ts")]
2055#[serde(tag = "type")]
2056pub enum UnsolvedExpr {
2057 Known(TyF64),
2058 Unknown(SketchVarId),
2059}
2060
2061impl UnsolvedExpr {
2062 pub fn var(&self) -> Option<SketchVarId> {
2063 match self {
2064 UnsolvedExpr::Known(_) => None,
2065 UnsolvedExpr::Unknown(id) => Some(*id),
2066 }
2067 }
2068}
2069
2070pub type UnsolvedPoint2dExpr = [UnsolvedExpr; 2];
2071
2072#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2073#[ts(export_to = "Geometry.ts")]
2074#[serde(rename_all = "camelCase")]
2075pub struct ConstrainablePoint2d {
2076 pub vars: crate::front::Point2d<SketchVarId>,
2077 pub object_id: ObjectId,
2078}
2079
2080#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2081#[ts(export_to = "Geometry.ts")]
2082pub enum ConstrainablePoint2dOrOrigin {
2083 Point(ConstrainablePoint2d),
2084 Origin,
2085}
2086
2087#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2088#[ts(export_to = "Geometry.ts")]
2089#[serde(rename_all = "camelCase")]
2090pub struct ConstrainableLine2d {
2091 pub vars: [crate::front::Point2d<SketchVarId>; 2],
2092 pub object_id: ObjectId,
2093}
2094
2095#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2096#[ts(export_to = "Geometry.ts")]
2097#[serde(rename_all = "camelCase")]
2098pub struct UnsolvedSegment {
2099 pub id: Uuid,
2101 pub object_id: ObjectId,
2102 pub kind: UnsolvedSegmentKind,
2103 #[serde(skip_serializing_if = "Option::is_none")]
2104 pub tag: Option<TagIdentifier>,
2105 #[serde(skip)]
2106 pub node_path: Option<NodePath>,
2107 #[serde(skip)]
2108 pub meta: Vec<Metadata>,
2109}
2110
2111#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2112#[ts(export_to = "Geometry.ts")]
2113#[serde(rename_all = "camelCase")]
2114pub enum UnsolvedSegmentKind {
2115 Point {
2116 position: UnsolvedPoint2dExpr,
2117 ctor: Box<PointCtor>,
2118 },
2119 Line {
2120 start: UnsolvedPoint2dExpr,
2121 end: UnsolvedPoint2dExpr,
2122 ctor: Box<LineCtor>,
2123 start_object_id: ObjectId,
2124 end_object_id: ObjectId,
2125 construction: bool,
2126 },
2127 Arc {
2128 start: UnsolvedPoint2dExpr,
2129 end: UnsolvedPoint2dExpr,
2130 center: UnsolvedPoint2dExpr,
2131 ctor: Box<ArcCtor>,
2132 start_object_id: ObjectId,
2133 end_object_id: ObjectId,
2134 center_object_id: ObjectId,
2135 construction: bool,
2136 },
2137 Circle {
2138 start: UnsolvedPoint2dExpr,
2139 center: UnsolvedPoint2dExpr,
2140 ctor: Box<CircleCtor>,
2141 start_object_id: ObjectId,
2142 center_object_id: ObjectId,
2143 construction: bool,
2144 },
2145}
2146
2147impl UnsolvedSegmentKind {
2148 pub fn human_friendly_kind_with_article(&self) -> &'static str {
2151 match self {
2152 Self::Point { .. } => "a Point",
2153 Self::Line { .. } => "a Line",
2154 Self::Arc { .. } => "an Arc",
2155 Self::Circle { .. } => "a Circle",
2156 }
2157 }
2158}
2159
2160#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2161#[ts(export_to = "Geometry.ts")]
2162#[serde(rename_all = "camelCase")]
2163pub struct Segment {
2164 pub id: Uuid,
2166 pub object_id: ObjectId,
2167 pub kind: SegmentKind,
2168 pub surface: SketchSurface,
2169 pub sketch_id: Uuid,
2171 #[serde(skip)]
2172 pub sketch: Option<Sketch>,
2173 #[serde(skip_serializing_if = "Option::is_none")]
2174 pub tag: Option<TagIdentifier>,
2175 #[serde(skip)]
2176 pub node_path: Option<NodePath>,
2177 #[serde(skip)]
2178 pub meta: Vec<Metadata>,
2179}
2180
2181impl Segment {
2182 pub fn is_construction(&self) -> bool {
2183 match &self.kind {
2184 SegmentKind::Point { .. } => true,
2185 SegmentKind::Line { construction, .. } => *construction,
2186 SegmentKind::Arc { construction, .. } => *construction,
2187 SegmentKind::Circle { construction, .. } => *construction,
2188 }
2189 }
2190}
2191
2192#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2193#[ts(export_to = "Geometry.ts")]
2194#[serde(rename_all = "camelCase")]
2195pub enum SegmentKind {
2196 Point {
2197 position: [TyF64; 2],
2198 ctor: Box<PointCtor>,
2199 #[serde(skip_serializing_if = "Option::is_none")]
2200 freedom: Option<Freedom>,
2201 },
2202 Line {
2203 start: [TyF64; 2],
2204 end: [TyF64; 2],
2205 ctor: Box<LineCtor>,
2206 start_object_id: ObjectId,
2207 end_object_id: ObjectId,
2208 #[serde(skip_serializing_if = "Option::is_none")]
2209 start_freedom: Option<Freedom>,
2210 #[serde(skip_serializing_if = "Option::is_none")]
2211 end_freedom: Option<Freedom>,
2212 construction: bool,
2213 },
2214 Arc {
2215 start: [TyF64; 2],
2216 end: [TyF64; 2],
2217 center: [TyF64; 2],
2218 ctor: Box<ArcCtor>,
2219 start_object_id: ObjectId,
2220 end_object_id: ObjectId,
2221 center_object_id: ObjectId,
2222 #[serde(skip_serializing_if = "Option::is_none")]
2223 start_freedom: Option<Freedom>,
2224 #[serde(skip_serializing_if = "Option::is_none")]
2225 end_freedom: Option<Freedom>,
2226 #[serde(skip_serializing_if = "Option::is_none")]
2227 center_freedom: Option<Freedom>,
2228 construction: bool,
2229 },
2230 Circle {
2231 start: [TyF64; 2],
2232 center: [TyF64; 2],
2233 ctor: Box<CircleCtor>,
2234 start_object_id: ObjectId,
2235 center_object_id: ObjectId,
2236 #[serde(skip_serializing_if = "Option::is_none")]
2237 start_freedom: Option<Freedom>,
2238 #[serde(skip_serializing_if = "Option::is_none")]
2239 center_freedom: Option<Freedom>,
2240 construction: bool,
2241 },
2242}
2243
2244#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2245#[ts(export_to = "Geometry.ts")]
2246#[serde(rename_all = "camelCase")]
2247pub struct AbstractSegment {
2248 pub repr: SegmentRepr,
2249 #[serde(skip)]
2250 pub meta: Vec<Metadata>,
2251}
2252
2253#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2254pub enum SegmentRepr {
2255 Unsolved { segment: Box<UnsolvedSegment> },
2256 Solved { segment: Box<Segment> },
2257}
2258
2259#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2260#[ts(export_to = "Geometry.ts")]
2261#[serde(rename_all = "camelCase")]
2262pub struct SketchConstraint {
2263 pub kind: SketchConstraintKind,
2264 #[serde(skip)]
2265 pub meta: Vec<Metadata>,
2266}
2267
2268#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2269#[ts(export_to = "Geometry.ts")]
2270#[serde(rename_all = "camelCase")]
2271pub enum SketchConstraintKind {
2272 Angle {
2273 line0: ConstrainableLine2d,
2274 line1: ConstrainableLine2d,
2275 },
2276 Distance {
2277 points: [ConstrainablePoint2dOrOrigin; 2],
2278 },
2279 Radius {
2280 points: [ConstrainablePoint2d; 2],
2281 },
2282 Diameter {
2283 points: [ConstrainablePoint2d; 2],
2284 },
2285 HorizontalDistance {
2286 points: [ConstrainablePoint2dOrOrigin; 2],
2287 },
2288 VerticalDistance {
2289 points: [ConstrainablePoint2dOrOrigin; 2],
2290 },
2291}
2292
2293impl SketchConstraintKind {
2294 pub fn name(&self) -> &'static str {
2295 match self {
2296 SketchConstraintKind::Angle { .. } => "angle",
2297 SketchConstraintKind::Distance { .. } => "distance",
2298 SketchConstraintKind::Radius { .. } => "radius",
2299 SketchConstraintKind::Diameter { .. } => "diameter",
2300 SketchConstraintKind::HorizontalDistance { .. } => "horizontalDistance",
2301 SketchConstraintKind::VerticalDistance { .. } => "verticalDistance",
2302 }
2303 }
2304}