1use ifc_model::{Entity, EntityId, Transaction, Value};
24
25use super::profile::ProfileType;
26use crate::error::GeometryError;
27use crate::slots::profile_slot as slot;
28
29use super::std_profile::positive;
30use super::{invalid, reals, refs, require_finite};
31
32fn header(attrs: &mut [Value], profile_type: ProfileType, name: Option<&str>) {
34 attrs[slot::PROFILE_TYPE] = Value::Enum(profile_type.token().into());
35 attrs[slot::PROFILE_NAME] = name.map_or(Value::Null, |n| Value::Text(n.into()));
36}
37
38pub fn arbitrary_open_profile(
46 tx: &mut Transaction,
47 name: Option<&str>,
48 curve: EntityId,
49) -> EntityId {
50 let mut attrs = vec![Value::Null; 3];
51 header(&mut attrs, ProfileType::Curve, name);
52 attrs[slot::OUTER_CURVE] = Value::Ref(curve);
53 tx.create(Entity::new("IFCARBITRARYOPENPROFILEDEF", attrs))
54}
55
56pub fn center_line_profile(
67 tx: &mut Transaction,
68 name: Option<&str>,
69 curve: EntityId,
70 thickness: f64,
71) -> Result<EntityId, GeometryError> {
72 const T: &str = "IFCCENTERLINEPROFILEDEF";
73 positive(T, "Thickness", thickness)?;
74 let mut attrs = vec![Value::Null; 4];
75 header(&mut attrs, ProfileType::Area, name);
76 attrs[slot::OUTER_CURVE] = Value::Ref(curve);
77 attrs[3] = Value::Real(thickness);
78 Ok(tx.create(Entity::new(T, attrs)))
79}
80
81pub fn arbitrary_profile_with_voids(
91 tx: &mut Transaction,
92 name: Option<&str>,
93 outer_curve: EntityId,
94 inner_curves: &[EntityId],
95) -> Result<EntityId, GeometryError> {
96 const T: &str = "IFCARBITRARYPROFILEDEFWITHVOIDS";
97 if inner_curves.is_empty() {
98 return Err(invalid(
99 T,
100 "InnerCurves",
101 "expected at least one void; without one this is an IfcArbitraryClosedProfileDef",
102 ));
103 }
104 let mut attrs = vec![Value::Null; 4];
105 header(&mut attrs, ProfileType::Area, name);
107 attrs[slot::OUTER_CURVE] = Value::Ref(outer_curve);
108 attrs[slot::INNER_CURVES] = refs(inner_curves);
109 Ok(tx.create(Entity::new(T, attrs)))
110}
111
112pub fn composite_profile(
119 tx: &mut Transaction,
120 profile_type: ProfileType,
121 name: Option<&str>,
122 profiles: &[EntityId],
123 label: Option<&str>,
124) -> Result<EntityId, GeometryError> {
125 const T: &str = "IFCCOMPOSITEPROFILEDEF";
126 if profiles.len() < 2 {
127 return Err(invalid(
128 T,
129 "Profiles",
130 format!("expected at least 2 profiles, got {}", profiles.len()),
131 ));
132 }
133 let mut attrs = vec![Value::Null; 4];
134 header(&mut attrs, profile_type, name);
135 attrs[2] = refs(profiles);
136 attrs[3] = label.map_or(Value::Null, |l| Value::Text(l.into()));
137 Ok(tx.create(Entity::new(T, attrs)))
138}
139
140pub fn derived_profile(
145 tx: &mut Transaction,
146 profile_type: ProfileType,
147 name: Option<&str>,
148 parent: EntityId,
149 operator: EntityId,
150 label: Option<&str>,
151) -> EntityId {
152 let mut attrs = vec![Value::Null; 5];
153 header(&mut attrs, profile_type, name);
154 attrs[2] = Value::Ref(parent);
155 attrs[3] = Value::Ref(operator);
156 attrs[4] = label.map_or(Value::Null, |l| Value::Text(l.into()));
157 tx.create(Entity::new("IFCDERIVEDPROFILEDEF", attrs))
158}
159
160pub fn mirrored_profile(
170 tx: &mut Transaction,
171 profile_type: ProfileType,
172 name: Option<&str>,
173 parent: EntityId,
174 label: Option<&str>,
175) -> EntityId {
176 let mut attrs = vec![Value::Null; 5];
177 header(&mut attrs, profile_type, name);
178 attrs[2] = Value::Ref(parent);
179 attrs[3] = Value::Derived;
180 attrs[4] = label.map_or(Value::Null, |l| Value::Text(l.into()));
181 tx.create(Entity::new("IFCMIRROREDPROFILEDEF", attrs))
182}
183
184pub fn rounded_rectangle_profile(
192 tx: &mut Transaction,
193 name: Option<&str>,
194 position: Option<EntityId>,
195 x_dim: f64,
196 y_dim: f64,
197 rounding_radius: f64,
198) -> Result<EntityId, GeometryError> {
199 const T: &str = "IFCROUNDEDRECTANGLEPROFILEDEF";
200 positive(T, "XDim", x_dim)?;
201 positive(T, "YDim", y_dim)?;
202 positive(T, "RoundingRadius", rounding_radius)?;
203 if rounding_radius > x_dim / 2.0 || rounding_radius > y_dim / 2.0 {
204 return Err(invalid(
205 T,
206 "RoundingRadius",
207 format!("{rounding_radius} exceeds half of the {x_dim} by {y_dim} rectangle"),
208 ));
209 }
210 let mut attrs = vec![Value::Null; 6];
211 header(&mut attrs, ProfileType::Area, name);
212 attrs[slot::POSITION] = position.map_or(Value::Null, Value::Ref);
213 attrs[slot::X_DIM] = Value::Real(x_dim);
214 attrs[slot::Y_DIM] = Value::Real(y_dim);
215 attrs[5] = Value::Real(rounding_radius);
216 Ok(tx.create(Entity::new(T, attrs)))
217}
218
219pub fn profile_def(
227 tx: &mut Transaction,
228 profile_type: ProfileType,
229 name: Option<&str>,
230) -> EntityId {
231 let attrs = vec![
232 Value::Enum(profile_type.token().into()),
233 name.map_or(Value::Null, |n| Value::Text(n.into())),
234 ];
235 tx.create(Entity::new("IFCPROFILEDEF", attrs))
236}
237
238pub fn open_cross_profile(
256 tx: &mut Transaction,
257 name: Option<&str>,
258 horizontal_widths: bool,
259 widths: &[f64],
260 slopes: &[f64],
261 tags: Option<&[&str]>,
262 offset_point: Option<EntityId>,
263) -> Result<EntityId, GeometryError> {
264 const ENTITY: &str = "IFCOPENCROSSPROFILEDEF";
265 if widths.is_empty() {
266 return Err(invalid(ENTITY, "Widths", "expected LIST [1:?]"));
267 }
268 if widths.len() != slopes.len() {
269 return Err(invalid(
270 ENTITY,
271 "Slopes",
272 format!(
273 "expected one slope per width: {} widths, {} slopes",
274 widths.len(),
275 slopes.len()
276 ),
277 ));
278 }
279 require_finite(ENTITY, "Widths", widths)?;
280 require_finite(ENTITY, "Slopes", slopes)?;
281 if let Some(bad) = widths.iter().position(|w| *w < 0.0) {
282 return Err(invalid(
283 ENTITY,
284 "Widths",
285 format!("width at index {bad} is negative"),
286 ));
287 }
288 if let Some(tags) = tags {
289 if tags.len() != widths.len() + 1 {
292 return Err(invalid(
293 ENTITY,
294 "Tags",
295 format!("expected {} tags, got {}", widths.len() + 1, tags.len()),
296 ));
297 }
298 }
299 let attrs = vec![
300 Value::Enum(ProfileType::Curve.token().into()),
302 name.map_or(Value::Null, |n| Value::Text(n.into())),
303 Value::Bool(horizontal_widths),
304 reals(widths),
305 reals(slopes),
306 tags.map_or(Value::Null, |t| {
307 Value::List(t.iter().map(|v| Value::Text((*v).into())).collect())
308 }),
309 offset_point.map_or(Value::Null, Value::Ref),
310 ];
311 Ok(tx.create(Entity::new(ENTITY, attrs)))
312}