Skip to main content

kcl_lib/std/
axis_or_reference.rs

1//! Types for referencing an axis or edge.
2
3use super::args::TyF64;
4use crate::KclError;
5use crate::errors::KclErrorDetails;
6use crate::execution::Plane;
7use crate::execution::Segment;
8use crate::execution::SegmentKind;
9use crate::execution::Sketch;
10use crate::execution::Solid;
11use crate::execution::TagIdentifier;
12use crate::std::fillet::EdgeReference;
13use crate::std::sketch::FaceTag;
14
15/// A 2D axis or tagged edge.
16#[derive(Debug, Clone, PartialEq)]
17pub enum Axis2dOrEdgeReference {
18    /// 2D axis and origin.
19    Axis { direction: [TyF64; 2], origin: [TyF64; 2] },
20    /// Tagged edge.
21    Edge(EdgeReference),
22}
23
24impl Axis2dOrEdgeReference {
25    /// Use a sketch-solve segment by finding its engine ID.
26    pub fn from_segment(segment: &Segment) -> Result<Self, KclError> {
27        match &segment.kind {
28            SegmentKind::Line { .. } => Ok(Self::Edge(EdgeReference::Uuid(segment.id))),
29            SegmentKind::Point { .. } => Err(KclError::new_type(KclErrorDetails {
30                source_ranges: segment.meta.iter().map(|meta| meta.source_range).collect(),
31                backtrace: Default::default(),
32                message: "Cannot use a point as an axis".to_owned(),
33            })),
34            SegmentKind::Arc { .. } => Err(KclError::new_type(KclErrorDetails {
35                source_ranges: segment.meta.iter().map(|meta| meta.source_range).collect(),
36                backtrace: Default::default(),
37                message: "Cannot use an arc as an axis".to_owned(),
38            })),
39            SegmentKind::Circle { .. } => Err(KclError::new_type(KclErrorDetails {
40                source_ranges: segment.meta.iter().map(|meta| meta.source_range).collect(),
41                backtrace: Default::default(),
42                message: "Cannot use a circle as an axis".to_owned(),
43            })),
44        }
45    }
46}
47
48/// A 3D axis or tagged edge.
49#[allow(clippy::large_enum_variant)]
50#[derive(Debug, Clone, PartialEq)]
51pub enum Axis3dOrEdgeReference {
52    /// 3D axis and origin.
53    Axis { direction: [TyF64; 3], origin: [TyF64; 3] },
54    /// Tagged edge.
55    Edge(EdgeReference),
56}
57
58impl Axis3dOrEdgeReference {
59    /// Use a sketch-solve segment by finding its engine ID.
60    pub fn from_segment(segment: &Segment) -> Result<Self, KclError> {
61        match &segment.kind {
62            SegmentKind::Line { .. } => Ok(Self::Edge(EdgeReference::Uuid(segment.id))),
63            SegmentKind::Point { .. } => Err(KclError::new_type(KclErrorDetails {
64                source_ranges: segment.meta.iter().map(|meta| meta.source_range).collect(),
65                backtrace: Default::default(),
66                message: "Cannot use a point as an axis".to_owned(),
67            })),
68            SegmentKind::Arc { .. } => Err(KclError::new_type(KclErrorDetails {
69                source_ranges: segment.meta.iter().map(|meta| meta.source_range).collect(),
70                backtrace: Default::default(),
71                message: "Cannot use an arc as an axis".to_owned(),
72            })),
73            SegmentKind::Circle { .. } => Err(KclError::new_type(KclErrorDetails {
74                source_ranges: segment.meta.iter().map(|meta| meta.source_range).collect(),
75                backtrace: Default::default(),
76                message: "Cannot use a circle as an axis".to_owned(),
77            })),
78        }
79    }
80}
81
82/// A 2D axis or a raw point2d.
83#[derive(Debug, Clone, PartialEq)]
84pub enum Axis2dOrPoint2d {
85    /// 2D axis and origin.
86    Axis { direction: [TyF64; 2], origin: [TyF64; 2] },
87    /// Raw point2d.
88    Point([TyF64; 2]),
89}
90
91impl Axis2dOrPoint2d {
92    /// Convert to a 2D axis.
93    pub fn to_point2d(&self) -> [TyF64; 2] {
94        match self {
95            Axis2dOrPoint2d::Axis { direction, origin: _ } => direction.clone(),
96            Axis2dOrPoint2d::Point(point) => point.clone(),
97        }
98    }
99}
100
101/// A 3D axis or a raw point3d.
102#[derive(Debug, Clone, PartialEq)]
103pub enum Axis3dOrPoint3d {
104    /// 3D axis and origin.
105    Axis { direction: [TyF64; 3], origin: [TyF64; 3] },
106    /// Raw point3d.
107    Point([TyF64; 3]),
108}
109
110impl Axis3dOrPoint3d {
111    /// Convert to a 3D axis.
112    pub fn to_point3d(&self) -> [TyF64; 3] {
113        match self {
114            Axis3dOrPoint3d::Axis { direction, origin: _ } => direction.clone(),
115            Axis3dOrPoint3d::Point(point) => point.clone(),
116        }
117    }
118
119    pub fn axis_origin(&self) -> Option<[TyF64; 3]> {
120        match self {
121            Axis3dOrPoint3d::Axis { origin, .. } => Some(origin.clone()),
122            Axis3dOrPoint3d::Point(..) => None,
123        }
124    }
125}
126
127/// A raw point3d, 3D axis, Edge, Face, Solid or Tag.
128#[allow(clippy::large_enum_variant)]
129#[derive(Debug, Clone, PartialEq)]
130pub enum Point3dAxis3dOrGeometryReference {
131    /// Raw point3d.
132    Point([TyF64; 3]),
133    /// 3D axis and origin.
134    Axis { direction: [TyF64; 3], origin: [TyF64; 3] },
135    /// Plane.
136    Plane(Box<Plane>),
137    /// Edge Reference.
138    Edge(EdgeReference),
139    /// Face.
140    Face(FaceTag),
141    /// Sketch.
142    Sketch(Box<Sketch>),
143    /// Solid.
144    Solid(Box<Solid>),
145    /// Tagged edge or face.
146    TaggedEdgeOrFace(TagIdentifier),
147}