1use std::collections::HashMap;
4
5use anyhow::Result;
6use kcmc::shared::Point3d as KPoint3d; use kcmc::{
8 ModelingCmd, each_cmd as mcmd,
9 length_unit::LengthUnit,
10 ok_response::OkModelingCmdResponse,
11 output::ExtrusionFaceInfo,
12 shared::{ExtrudeReference, ExtrusionFaceCapType, Opposite},
13 websocket::{ModelingCmdReq, OkWebSocketResponseData},
14};
15use kittycad_modeling_cmds::{
16 self as kcmc,
17 shared::{Angle, BodyType, ExtrudeMethod, Point2d},
18};
19use uuid::Uuid;
20
21use super::{DEFAULT_TOLERANCE_MM, args::TyF64, utils::point_to_mm};
22use crate::{
23 errors::{KclError, KclErrorDetails},
24 execution::{
25 ArtifactId, ExecState, Extrudable, ExtrudeSurface, GeoMeta, KclValue, ModelingCmdMeta, Path, ProfileClosed,
26 Sketch, SketchSurface, Solid, annotations,
27 types::{ArrayLen, PrimitiveType, RuntimeType},
28 },
29 parsing::ast::types::TagNode,
30 std::{Args, axis_or_reference::Point3dAxis3dOrGeometryReference},
31};
32
33pub async fn extrude(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
35 let sketches: Vec<Extrudable> = args.get_unlabeled_kw_arg(
36 "sketches",
37 &RuntimeType::Array(
38 Box::new(RuntimeType::Union(vec![
39 RuntimeType::sketch(),
40 RuntimeType::face(),
41 RuntimeType::tagged_face(),
42 ])),
43 ArrayLen::Minimum(1),
44 ),
45 exec_state,
46 )?;
47
48 let length: Option<TyF64> = args.get_kw_arg_opt("length", &RuntimeType::length(), exec_state)?;
49 let to = args.get_kw_arg_opt(
50 "to",
51 &RuntimeType::Union(vec![
52 RuntimeType::point3d(),
53 RuntimeType::Primitive(PrimitiveType::Axis3d),
54 RuntimeType::Primitive(PrimitiveType::Edge),
55 RuntimeType::plane(),
56 RuntimeType::Primitive(PrimitiveType::Face),
57 RuntimeType::sketch(),
58 RuntimeType::Primitive(PrimitiveType::Solid),
59 RuntimeType::tagged_edge(),
60 RuntimeType::tagged_face(),
61 ]),
62 exec_state,
63 )?;
64 let symmetric = args.get_kw_arg_opt("symmetric", &RuntimeType::bool(), exec_state)?;
65 let bidirectional_length: Option<TyF64> =
66 args.get_kw_arg_opt("bidirectionalLength", &RuntimeType::length(), exec_state)?;
67 let tag_start = args.get_kw_arg_opt("tagStart", &RuntimeType::tag_decl(), exec_state)?;
68 let tag_end = args.get_kw_arg_opt("tagEnd", &RuntimeType::tag_decl(), exec_state)?;
69 let twist_angle: Option<TyF64> = args.get_kw_arg_opt("twistAngle", &RuntimeType::degrees(), exec_state)?;
70 let twist_angle_step: Option<TyF64> = args.get_kw_arg_opt("twistAngleStep", &RuntimeType::degrees(), exec_state)?;
71 let twist_center: Option<[TyF64; 2]> = args.get_kw_arg_opt("twistCenter", &RuntimeType::point2d(), exec_state)?;
72 let tolerance: Option<TyF64> = args.get_kw_arg_opt("tolerance", &RuntimeType::length(), exec_state)?;
73 let method: Option<String> = args.get_kw_arg_opt("method", &RuntimeType::string(), exec_state)?;
74 let hide_seams: Option<bool> = args.get_kw_arg_opt("hideSeams", &RuntimeType::bool(), exec_state)?;
75 let body_type: Option<BodyType> = args.get_kw_arg_opt("bodyType", &RuntimeType::string(), exec_state)?;
76
77 let result = inner_extrude(
78 sketches,
79 length,
80 to,
81 symmetric,
82 bidirectional_length,
83 tag_start,
84 tag_end,
85 twist_angle,
86 twist_angle_step,
87 twist_center,
88 tolerance,
89 method,
90 hide_seams,
91 body_type,
92 exec_state,
93 args,
94 )
95 .await?;
96
97 Ok(result.into())
98}
99
100#[allow(clippy::too_many_arguments)]
101async fn inner_extrude(
102 extrudables: Vec<Extrudable>,
103 length: Option<TyF64>,
104 to: Option<Point3dAxis3dOrGeometryReference>,
105 symmetric: Option<bool>,
106 bidirectional_length: Option<TyF64>,
107 tag_start: Option<TagNode>,
108 tag_end: Option<TagNode>,
109 twist_angle: Option<TyF64>,
110 twist_angle_step: Option<TyF64>,
111 twist_center: Option<[TyF64; 2]>,
112 tolerance: Option<TyF64>,
113 method: Option<String>,
114 hide_seams: Option<bool>,
115 body_type: Option<BodyType>,
116 exec_state: &mut ExecState,
117 args: Args,
118) -> Result<Vec<Solid>, KclError> {
119 let body_type = body_type.unwrap_or_default();
120
121 if matches!(body_type, BodyType::Solid) && extrudables.iter().any(|sk| matches!(sk.is_closed(), ProfileClosed::No))
122 {
123 return Err(KclError::new_semantic(KclErrorDetails::new(
124 "Cannot solid extrude an open profile. Either close the profile, or use a surface extrude.".to_owned(),
125 vec![args.source_range],
126 )));
127 }
128
129 let mut solids = Vec::new();
131 let tolerance = LengthUnit(tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE_MM));
132
133 let extrude_method = match method.as_deref() {
134 Some("new" | "NEW") => ExtrudeMethod::New,
135 Some("merge" | "MERGE") => ExtrudeMethod::Merge,
136 None => ExtrudeMethod::default(),
137 Some(other) => {
138 return Err(KclError::new_semantic(KclErrorDetails::new(
139 format!("Unknown merge method {other}, try using `MERGE` or `NEW`"),
140 vec![args.source_range],
141 )));
142 }
143 };
144
145 if symmetric.unwrap_or(false) && bidirectional_length.is_some() {
146 return Err(KclError::new_semantic(KclErrorDetails::new(
147 "You cannot give both `symmetric` and `bidirectional` params, you have to choose one or the other"
148 .to_owned(),
149 vec![args.source_range],
150 )));
151 }
152
153 if (length.is_some() || twist_angle.is_some()) && to.is_some() {
154 return Err(KclError::new_semantic(KclErrorDetails::new(
155 "You cannot give `length` or `twist` params with the `to` param, you have to choose one or the other"
156 .to_owned(),
157 vec![args.source_range],
158 )));
159 }
160
161 let bidirection = bidirectional_length.map(|l| LengthUnit(l.to_mm()));
162
163 let opposite = match (symmetric, bidirection) {
164 (Some(true), _) => Opposite::Symmetric,
165 (None, None) => Opposite::None,
166 (Some(false), None) => Opposite::None,
167 (None, Some(length)) => Opposite::Other(length),
168 (Some(false), Some(length)) => Opposite::Other(length),
169 };
170
171 for extrudable in &extrudables {
172 let extrude_cmd_id = exec_state.next_uuid();
173 let sketch_or_face_id = extrudable.id_to_extrude(exec_state, &args, false).await?;
174 let cmd = match (&twist_angle, &twist_angle_step, &twist_center, length.clone(), &to) {
175 (Some(angle), angle_step, center, Some(length), None) => {
176 let center = center.clone().map(point_to_mm).map(Point2d::from).unwrap_or_default();
177 let total_rotation_angle = Angle::from_degrees(angle.to_degrees(exec_state, args.source_range));
178 let angle_step_size = Angle::from_degrees(
179 angle_step
180 .clone()
181 .map(|a| a.to_degrees(exec_state, args.source_range))
182 .unwrap_or(15.0),
183 );
184 ModelingCmd::from(
185 mcmd::TwistExtrude::builder()
186 .target(sketch_or_face_id.into())
187 .distance(LengthUnit(length.to_mm()))
188 .center_2d(center)
189 .total_rotation_angle(total_rotation_angle)
190 .angle_step_size(angle_step_size)
191 .tolerance(tolerance)
192 .body_type(body_type)
193 .build(),
194 )
195 }
196 (None, None, None, Some(length), None) => ModelingCmd::from(
197 mcmd::Extrude::builder()
198 .target(sketch_or_face_id.into())
199 .distance(LengthUnit(length.to_mm()))
200 .opposite(opposite.clone())
201 .extrude_method(extrude_method)
202 .body_type(body_type)
203 .maybe_merge_coplanar_faces(hide_seams)
204 .build(),
205 ),
206 (None, None, None, None, Some(to)) => match to {
207 Point3dAxis3dOrGeometryReference::Point(point) => ModelingCmd::from(
208 mcmd::ExtrudeToReference::builder()
209 .target(sketch_or_face_id.into())
210 .reference(ExtrudeReference::Point {
211 point: KPoint3d {
212 x: LengthUnit(point[0].to_mm()),
213 y: LengthUnit(point[1].to_mm()),
214 z: LengthUnit(point[2].to_mm()),
215 },
216 })
217 .extrude_method(extrude_method)
218 .body_type(body_type)
219 .build(),
220 ),
221 Point3dAxis3dOrGeometryReference::Axis { direction, origin } => ModelingCmd::from(
222 mcmd::ExtrudeToReference::builder()
223 .target(sketch_or_face_id.into())
224 .reference(ExtrudeReference::Axis {
225 axis: KPoint3d {
226 x: direction[0].to_mm(),
227 y: direction[1].to_mm(),
228 z: direction[2].to_mm(),
229 },
230 point: KPoint3d {
231 x: LengthUnit(origin[0].to_mm()),
232 y: LengthUnit(origin[1].to_mm()),
233 z: LengthUnit(origin[2].to_mm()),
234 },
235 })
236 .extrude_method(extrude_method)
237 .body_type(body_type)
238 .build(),
239 ),
240 Point3dAxis3dOrGeometryReference::Plane(plane) => {
241 let plane_id = if plane.is_uninitialized() {
242 if plane.info.origin.units.is_none() {
243 return Err(KclError::new_semantic(KclErrorDetails::new(
244 "Origin of plane has unknown units".to_string(),
245 vec![args.source_range],
246 )));
247 }
248 let sketch_plane = crate::std::sketch::make_sketch_plane_from_orientation(
249 plane.clone().info.into_plane_data(),
250 exec_state,
251 &args,
252 )
253 .await?;
254 sketch_plane.id
255 } else {
256 plane.id
257 };
258 ModelingCmd::from(
259 mcmd::ExtrudeToReference::builder()
260 .target(sketch_or_face_id.into())
261 .reference(ExtrudeReference::EntityReference { entity_id: plane_id })
262 .extrude_method(extrude_method)
263 .body_type(body_type)
264 .build(),
265 )
266 }
267 Point3dAxis3dOrGeometryReference::Edge(edge_ref) => {
268 let edge_id = edge_ref.get_engine_id(exec_state, &args)?;
269 ModelingCmd::from(
270 mcmd::ExtrudeToReference::builder()
271 .target(sketch_or_face_id.into())
272 .reference(ExtrudeReference::EntityReference { entity_id: edge_id })
273 .extrude_method(extrude_method)
274 .body_type(body_type)
275 .build(),
276 )
277 }
278 Point3dAxis3dOrGeometryReference::Face(face_tag) => {
279 let face_id = face_tag.get_face_id_from_tag(exec_state, &args, false).await?;
280 ModelingCmd::from(
281 mcmd::ExtrudeToReference::builder()
282 .target(sketch_or_face_id.into())
283 .reference(ExtrudeReference::EntityReference { entity_id: face_id })
284 .extrude_method(extrude_method)
285 .body_type(body_type)
286 .build(),
287 )
288 }
289 Point3dAxis3dOrGeometryReference::Sketch(sketch_ref) => ModelingCmd::from(
290 mcmd::ExtrudeToReference::builder()
291 .target(sketch_or_face_id.into())
292 .reference(ExtrudeReference::EntityReference {
293 entity_id: sketch_ref.id,
294 })
295 .extrude_method(extrude_method)
296 .body_type(body_type)
297 .build(),
298 ),
299 Point3dAxis3dOrGeometryReference::Solid(solid) => ModelingCmd::from(
300 mcmd::ExtrudeToReference::builder()
301 .target(sketch_or_face_id.into())
302 .reference(ExtrudeReference::EntityReference { entity_id: solid.id })
303 .extrude_method(extrude_method)
304 .body_type(body_type)
305 .build(),
306 ),
307 Point3dAxis3dOrGeometryReference::TaggedEdgeOrFace(tag) => {
308 let tagged_edge_or_face = args.get_tag_engine_info(exec_state, tag)?;
309 let tagged_edge_or_face_id = tagged_edge_or_face.id;
310 ModelingCmd::from(
311 mcmd::ExtrudeToReference::builder()
312 .target(sketch_or_face_id.into())
313 .reference(ExtrudeReference::EntityReference {
314 entity_id: tagged_edge_or_face_id,
315 })
316 .extrude_method(extrude_method)
317 .body_type(body_type)
318 .build(),
319 )
320 }
321 },
322 (Some(_), _, _, None, None) => {
323 return Err(KclError::new_semantic(KclErrorDetails::new(
324 "The `length` parameter must be provided when using twist angle for extrusion.".to_owned(),
325 vec![args.source_range],
326 )));
327 }
328 (_, _, _, None, None) => {
329 return Err(KclError::new_semantic(KclErrorDetails::new(
330 "Either `length` or `to` parameter must be provided for extrusion.".to_owned(),
331 vec![args.source_range],
332 )));
333 }
334 (_, _, _, Some(_), Some(_)) => {
335 return Err(KclError::new_semantic(KclErrorDetails::new(
336 "You cannot give both `length` and `to` params, you have to choose one or the other".to_owned(),
337 vec![args.source_range],
338 )));
339 }
340 (_, _, _, _, _) => {
341 return Err(KclError::new_semantic(KclErrorDetails::new(
342 "Invalid combination of parameters for extrusion.".to_owned(),
343 vec![args.source_range],
344 )));
345 }
346 };
347
348 let being_extruded = match extrudable {
349 Extrudable::Sketch(..) => BeingExtruded::Sketch,
350 Extrudable::Face(..) => BeingExtruded::Face,
351 };
352 if let Some(post_extr_sketch) = extrudable.as_sketch() {
353 let cmds = post_extr_sketch.build_sketch_mode_cmds(
354 exec_state,
355 ModelingCmdReq {
356 cmd_id: extrude_cmd_id.into(),
357 cmd,
358 },
359 );
360 exec_state
361 .batch_modeling_cmds(ModelingCmdMeta::from_args_id(exec_state, &args, extrude_cmd_id), &cmds)
362 .await?;
363 solids.push(
364 do_post_extrude(
365 &post_extr_sketch,
366 extrude_cmd_id.into(),
367 false,
368 &NamedCapTags {
369 start: tag_start.as_ref(),
370 end: tag_end.as_ref(),
371 },
372 extrude_method,
373 exec_state,
374 &args,
375 None,
376 None,
377 body_type,
378 being_extruded,
379 )
380 .await?,
381 );
382 } else {
383 return Err(KclError::new_type(KclErrorDetails::new(
384 "Expected a sketch for extrusion".to_owned(),
385 vec![args.source_range],
386 )));
387 }
388 }
389
390 Ok(solids)
391}
392
393#[derive(Debug, Default)]
394pub(crate) struct NamedCapTags<'a> {
395 pub start: Option<&'a TagNode>,
396 pub end: Option<&'a TagNode>,
397}
398
399#[derive(Debug, Clone, Copy)]
400pub enum BeingExtruded {
401 Sketch,
402 Face,
403}
404
405#[allow(clippy::too_many_arguments)]
406pub(crate) async fn do_post_extrude<'a>(
407 sketch: &Sketch,
408 extrude_cmd_id: ArtifactId,
409 sectional: bool,
410 named_cap_tags: &'a NamedCapTags<'a>,
411 extrude_method: ExtrudeMethod,
412 exec_state: &mut ExecState,
413 args: &Args,
414 edge_id: Option<Uuid>,
415 clone_id_map: Option<&HashMap<Uuid, Uuid>>, body_type: BodyType,
417 being_extruded: BeingExtruded,
418) -> Result<Solid, KclError> {
419 exec_state
423 .batch_modeling_cmd(
424 ModelingCmdMeta::from_args(exec_state, args),
425 ModelingCmd::from(mcmd::ObjectBringToFront::builder().object_id(sketch.id).build()),
426 )
427 .await?;
428
429 let any_edge_id = if let Some(edge_id) = sketch.mirror {
430 edge_id
431 } else if let Some(id) = edge_id {
432 id
433 } else {
434 let Some(any_edge_id) = sketch.paths.first().map(|edge| edge.get_base().geo_meta.id) else {
437 return Err(KclError::new_type(KclErrorDetails::new(
438 "Expected a non-empty sketch".to_owned(),
439 vec![args.source_range],
440 )));
441 };
442 any_edge_id
443 };
444
445 let mut extrusion_info_edge_id = any_edge_id;
447 if sketch.clone.is_some() && clone_id_map.is_some() {
448 extrusion_info_edge_id = if let Some(clone_map) = clone_id_map {
449 if let Some(new_edge_id) = clone_map.get(&extrusion_info_edge_id) {
450 *new_edge_id
451 } else {
452 extrusion_info_edge_id
453 }
454 } else {
455 any_edge_id
456 };
457 }
458
459 let mut sketch = sketch.clone();
460 match body_type {
461 BodyType::Solid => {
462 sketch.is_closed = ProfileClosed::Explicitly;
463 }
464 BodyType::Surface => {}
465 _other => {
466 }
469 }
470
471 match (extrude_method, being_extruded) {
472 (ExtrudeMethod::Merge, BeingExtruded::Face) => {
473 if let SketchSurface::Face(ref face) = sketch.on {
476 sketch.id = face.solid.sketch.id;
479 }
480 }
481 (ExtrudeMethod::New, BeingExtruded::Face) => {
482 sketch.id = extrude_cmd_id.into();
485 }
486 (ExtrudeMethod::New, BeingExtruded::Sketch) => {
487 }
490 (ExtrudeMethod::Merge, BeingExtruded::Sketch) => {
491 if let SketchSurface::Face(ref face) = sketch.on {
492 sketch.id = face.solid.sketch.id;
495 }
496 }
497 (other, _) => {
498 return Err(KclError::new_internal(KclErrorDetails::new(
500 format!("Zoo does not yet support creating bodies via {other:?}"),
501 vec![args.source_range],
502 )));
503 }
504 }
505
506 let sketch_id = if let Some(cloned_from) = sketch.clone
508 && clone_id_map.is_some()
509 {
510 cloned_from
511 } else {
512 sketch.id
513 };
514
515 let solid3d_info = exec_state
516 .send_modeling_cmd(
517 ModelingCmdMeta::from_args(exec_state, args),
518 ModelingCmd::from(
519 mcmd::Solid3dGetExtrusionFaceInfo::builder()
520 .edge_id(extrusion_info_edge_id)
521 .object_id(sketch_id)
522 .build(),
523 ),
524 )
525 .await?;
526
527 let face_infos = if let OkWebSocketResponseData::Modeling {
528 modeling_response: OkModelingCmdResponse::Solid3dGetExtrusionFaceInfo(data),
529 } = solid3d_info
530 {
531 data.faces
532 } else {
533 vec![]
534 };
535
536 #[cfg(feature = "artifact-graph")]
538 {
539 if !sectional {
542 exec_state
543 .batch_modeling_cmd(
544 ModelingCmdMeta::from_args(exec_state, args),
545 ModelingCmd::from(
546 mcmd::Solid3dGetAdjacencyInfo::builder()
547 .object_id(sketch.id)
548 .edge_id(any_edge_id)
549 .build(),
550 ),
551 )
552 .await?;
553 }
554 }
555
556 let Faces {
557 sides: mut face_id_map,
558 start_cap_id,
559 end_cap_id,
560 } = analyze_faces(exec_state, args, face_infos).await;
561
562 if sketch.clone.is_some()
564 && let Some(clone_id_map) = clone_id_map
565 {
566 face_id_map = face_id_map
567 .into_iter()
568 .filter_map(|(k, v)| {
569 let fe_key = clone_id_map.get(&k)?;
570 let fe_value = clone_id_map.get(&(v?)).copied();
571 Some((*fe_key, fe_value))
572 })
573 .collect::<HashMap<Uuid, Option<Uuid>>>();
574 }
575
576 let no_engine_commands = args.ctx.no_engine_commands().await;
578 let mut new_value: Vec<ExtrudeSurface> = Vec::with_capacity(sketch.paths.len() + sketch.inner_paths.len() + 2);
579 let outer_surfaces = sketch.paths.iter().flat_map(|path| {
580 if let Some(Some(actual_face_id)) = face_id_map.get(&path.get_base().geo_meta.id) {
581 surface_of(path, *actual_face_id)
582 } else if no_engine_commands {
583 crate::log::logln!(
584 "No face ID found for path ID {:?}, but in no-engine-commands mode, so faking it",
585 path.get_base().geo_meta.id
586 );
587 fake_extrude_surface(exec_state, path)
589 } else if sketch.clone.is_some()
590 && let Some(clone_map) = clone_id_map
591 {
592 let new_path = clone_map.get(&(path.get_base().geo_meta.id));
593
594 if let Some(new_path) = new_path {
595 match face_id_map.get(new_path) {
596 Some(Some(actual_face_id)) => clone_surface_of(path, *new_path, *actual_face_id),
597 _ => {
598 let actual_face_id = face_id_map.iter().find_map(|(key, value)| {
599 if let Some(value) = value {
600 if value == new_path { Some(key) } else { None }
601 } else {
602 None
603 }
604 });
605 match actual_face_id {
606 Some(actual_face_id) => clone_surface_of(path, *new_path, *actual_face_id),
607 None => {
608 crate::log::logln!("No face ID found for clone path ID {:?}, so skipping it", new_path);
609 None
610 }
611 }
612 }
613 }
614 } else {
615 None
616 }
617 } else {
618 crate::log::logln!(
619 "No face ID found for path ID {:?}, and not in no-engine-commands mode, so skipping it",
620 path.get_base().geo_meta.id
621 );
622 None
623 }
624 });
625
626 new_value.extend(outer_surfaces);
627 let inner_surfaces = sketch.inner_paths.iter().flat_map(|path| {
628 if let Some(Some(actual_face_id)) = face_id_map.get(&path.get_base().geo_meta.id) {
629 surface_of(path, *actual_face_id)
630 } else if no_engine_commands {
631 fake_extrude_surface(exec_state, path)
633 } else {
634 None
635 }
636 });
637 new_value.extend(inner_surfaces);
638
639 if let Some(tag_start) = named_cap_tags.start {
641 let Some(start_cap_id) = start_cap_id else {
642 return Err(KclError::new_type(KclErrorDetails::new(
643 format!(
644 "Expected a start cap ID for tag `{}` for extrusion of sketch {:?}",
645 tag_start.name, sketch.id
646 ),
647 vec![args.source_range],
648 )));
649 };
650
651 new_value.push(ExtrudeSurface::ExtrudePlane(crate::execution::ExtrudePlane {
652 face_id: start_cap_id,
653 tag: Some(tag_start.clone()),
654 geo_meta: GeoMeta {
655 id: start_cap_id,
656 metadata: args.source_range.into(),
657 },
658 }));
659 }
660 if let Some(tag_end) = named_cap_tags.end {
661 let Some(end_cap_id) = end_cap_id else {
662 return Err(KclError::new_type(KclErrorDetails::new(
663 format!(
664 "Expected an end cap ID for tag `{}` for extrusion of sketch {:?}",
665 tag_end.name, sketch.id
666 ),
667 vec![args.source_range],
668 )));
669 };
670
671 new_value.push(ExtrudeSurface::ExtrudePlane(crate::execution::ExtrudePlane {
672 face_id: end_cap_id,
673 tag: Some(tag_end.clone()),
674 geo_meta: GeoMeta {
675 id: end_cap_id,
676 metadata: args.source_range.into(),
677 },
678 }));
679 }
680
681 Ok(Solid {
682 id: sketch.id,
683 artifact_id: extrude_cmd_id,
684 value: new_value,
685 meta: sketch.meta.clone(),
686 units: sketch.units,
687 sectional,
688 sketch,
689 start_cap_id,
690 end_cap_id,
691 edge_cuts: vec![],
692 })
693}
694
695#[derive(Default)]
696struct Faces {
697 sides: HashMap<Uuid, Option<Uuid>>,
699 end_cap_id: Option<Uuid>,
701 start_cap_id: Option<Uuid>,
703}
704
705async fn analyze_faces(exec_state: &mut ExecState, args: &Args, face_infos: Vec<ExtrusionFaceInfo>) -> Faces {
706 let mut faces = Faces {
707 sides: HashMap::with_capacity(face_infos.len()),
708 ..Default::default()
709 };
710 if args.ctx.no_engine_commands().await {
711 faces.start_cap_id = Some(exec_state.next_uuid());
713 faces.end_cap_id = Some(exec_state.next_uuid());
714 }
715 for face_info in face_infos {
716 match face_info.cap {
717 ExtrusionFaceCapType::Bottom => faces.start_cap_id = face_info.face_id,
718 ExtrusionFaceCapType::Top => faces.end_cap_id = face_info.face_id,
719 ExtrusionFaceCapType::Both => {
720 faces.end_cap_id = face_info.face_id;
721 faces.start_cap_id = face_info.face_id;
722 }
723 ExtrusionFaceCapType::None => {
724 if let Some(curve_id) = face_info.curve_id {
725 faces.sides.insert(curve_id, face_info.face_id);
726 }
727 }
728 other => {
729 exec_state.warn(
730 crate::CompilationError {
731 source_range: args.source_range,
732 message: format!("unknown extrusion face type {other:?}"),
733 suggestion: None,
734 severity: crate::errors::Severity::Warning,
735 tag: crate::errors::Tag::Unnecessary,
736 },
737 annotations::WARN_NOT_YET_SUPPORTED,
738 );
739 }
740 }
741 }
742 faces
743}
744fn surface_of(path: &Path, actual_face_id: Uuid) -> Option<ExtrudeSurface> {
745 match path {
746 Path::Arc { .. }
747 | Path::TangentialArc { .. }
748 | Path::TangentialArcTo { .. }
749 | Path::Ellipse { .. }
751 | Path::Conic {.. }
752 | Path::Circle { .. }
753 | Path::CircleThreePoint { .. } => {
754 let extrude_surface = ExtrudeSurface::ExtrudeArc(crate::execution::ExtrudeArc {
755 face_id: actual_face_id,
756 tag: path.get_base().tag.clone(),
757 geo_meta: GeoMeta {
758 id: path.get_base().geo_meta.id,
759 metadata: path.get_base().geo_meta.metadata,
760 },
761 });
762 Some(extrude_surface)
763 }
764 Path::Base { .. } | Path::ToPoint { .. } | Path::Horizontal { .. } | Path::AngledLineTo { .. } | Path::Bezier { .. } => {
765 let extrude_surface = ExtrudeSurface::ExtrudePlane(crate::execution::ExtrudePlane {
766 face_id: actual_face_id,
767 tag: path.get_base().tag.clone(),
768 geo_meta: GeoMeta {
769 id: path.get_base().geo_meta.id,
770 metadata: path.get_base().geo_meta.metadata,
771 },
772 });
773 Some(extrude_surface)
774 }
775 Path::ArcThreePoint { .. } => {
776 let extrude_surface = ExtrudeSurface::ExtrudeArc(crate::execution::ExtrudeArc {
777 face_id: actual_face_id,
778 tag: path.get_base().tag.clone(),
779 geo_meta: GeoMeta {
780 id: path.get_base().geo_meta.id,
781 metadata: path.get_base().geo_meta.metadata,
782 },
783 });
784 Some(extrude_surface)
785 }
786 }
787}
788
789fn clone_surface_of(path: &Path, clone_path_id: Uuid, actual_face_id: Uuid) -> Option<ExtrudeSurface> {
790 match path {
791 Path::Arc { .. }
792 | Path::TangentialArc { .. }
793 | Path::TangentialArcTo { .. }
794 | Path::Ellipse { .. }
796 | Path::Conic {.. }
797 | Path::Circle { .. }
798 | Path::CircleThreePoint { .. } => {
799 let extrude_surface = ExtrudeSurface::ExtrudeArc(crate::execution::ExtrudeArc {
800 face_id: actual_face_id,
801 tag: path.get_base().tag.clone(),
802 geo_meta: GeoMeta {
803 id: clone_path_id,
804 metadata: path.get_base().geo_meta.metadata,
805 },
806 });
807 Some(extrude_surface)
808 }
809 Path::Base { .. } | Path::ToPoint { .. } | Path::Horizontal { .. } | Path::AngledLineTo { .. } | Path::Bezier { .. } => {
810 let extrude_surface = ExtrudeSurface::ExtrudePlane(crate::execution::ExtrudePlane {
811 face_id: actual_face_id,
812 tag: path.get_base().tag.clone(),
813 geo_meta: GeoMeta {
814 id: clone_path_id,
815 metadata: path.get_base().geo_meta.metadata,
816 },
817 });
818 Some(extrude_surface)
819 }
820 Path::ArcThreePoint { .. } => {
821 let extrude_surface = ExtrudeSurface::ExtrudeArc(crate::execution::ExtrudeArc {
822 face_id: actual_face_id,
823 tag: path.get_base().tag.clone(),
824 geo_meta: GeoMeta {
825 id: clone_path_id,
826 metadata: path.get_base().geo_meta.metadata,
827 },
828 });
829 Some(extrude_surface)
830 }
831 }
832}
833
834fn fake_extrude_surface(exec_state: &mut ExecState, path: &Path) -> Option<ExtrudeSurface> {
836 let extrude_surface = ExtrudeSurface::ExtrudePlane(crate::execution::ExtrudePlane {
837 face_id: exec_state.next_uuid(),
839 tag: path.get_base().tag.clone(),
840 geo_meta: GeoMeta {
841 id: path.get_base().geo_meta.id,
842 metadata: path.get_base().geo_meta.metadata,
843 },
844 });
845 Some(extrude_surface)
846}