1use uuid::Uuid;
4
5use super::sketch::FaceTag;
6use crate::errors::KclError;
7use crate::errors::KclErrorDetails;
8use crate::execution::ExecState;
9use crate::execution::Face;
10use crate::execution::KclValue;
11use crate::execution::Segment;
12use crate::execution::Solid;
13use crate::execution::types::RuntimeType;
14use crate::std::Args;
15
16const SEGMENT_MUST_HAVE_TAG_ERROR: &str =
17 "Face specifier must have a tag. For sketch block segments, assign the segment to a variable.";
18
19pub(crate) enum FaceSpecifier {
21 FaceTag(FaceTag),
22 Segment(Box<Segment>),
23}
24
25impl FaceSpecifier {
26 fn tag_name(&self) -> Option<String> {
30 match self {
31 FaceSpecifier::FaceTag(face_tag) => Some(face_tag.to_string()),
32 FaceSpecifier::Segment(segment) => segment.tag.as_ref().map(|tag| tag.to_string()),
33 }
34 }
35
36 pub(super) async fn face_id(
37 &self,
38 solid: &Solid,
39 exec_state: &mut ExecState,
40 args: &Args,
41 must_be_planar: bool,
42 ) -> Result<Uuid, KclError> {
43 match self {
44 FaceSpecifier::FaceTag(face_tag) => face_tag.get_face_id(solid, exec_state, args, must_be_planar).await,
45 FaceSpecifier::Segment(segment) => {
46 let tag_id = segment.tag.as_ref().ok_or_else(|| {
47 KclError::new_semantic(KclErrorDetails::new(
48 SEGMENT_MUST_HAVE_TAG_ERROR.to_owned(),
49 vec![args.source_range],
50 ))
51 })?;
52 args.get_adjacent_face_to_tag(exec_state, tag_id, must_be_planar).await
53 }
54 }
55 }
56}
57
58pub async fn face_of(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
60 let solid = args.get_unlabeled_kw_arg("solid", &RuntimeType::solid(), exec_state)?;
61 let face = args.get_kw_arg("face", &RuntimeType::tagged_face_or_segment(), exec_state)?;
62
63 let face = make_face(solid, face, exec_state, &args).await?;
64
65 Ok(KclValue::Face { value: face })
66}
67
68pub(super) async fn make_face(
69 solid: Box<Solid>,
70 face: FaceSpecifier,
71 exec_state: &mut ExecState,
72 args: &Args,
73) -> Result<Box<Face>, KclError> {
74 let Some(tag_name) = face.tag_name() else {
75 return Err(KclError::new_semantic(KclErrorDetails::new(
76 SEGMENT_MUST_HAVE_TAG_ERROR.to_owned(),
77 vec![args.source_range],
78 )));
79 };
80 let extrude_plane_id = face.face_id(&solid, exec_state, args, true).await?;
81 let sketch = solid.sketch().ok_or_else(|| {
82 KclError::new_type(crate::errors::KclErrorDetails::new(
83 "This solid was created without a sketch, so its face axes are unavailable.".to_owned(),
84 vec![args.source_range],
85 ))
86 })?;
87
88 let object_id = exec_state.next_object_id();
89 #[cfg(feature = "artifact-graph")]
90 {
91 let face_object = crate::front::Object {
92 id: object_id,
93 kind: crate::front::ObjectKind::Face(crate::front::Face { id: object_id }),
94 label: Default::default(),
95 comments: Default::default(),
96 artifact_id: extrude_plane_id.into(),
97 source: args.source_range.into(),
98 };
99 exec_state.add_scene_object(face_object, args.source_range);
100 }
101
102 Ok(Box::new(Face {
103 id: extrude_plane_id,
104 artifact_id: extrude_plane_id.into(),
105 object_id,
106 value: tag_name,
107 x_axis: sketch.on.x_axis(),
109 y_axis: sketch.on.y_axis(),
110 units: solid.units,
111 solid,
112 meta: vec![args.source_range.into()],
113 }))
114}