acadrust 0.6.7

A pure Rust library for reading and writing CAD files in DXF format (ASCII and Binary) and DWG format (Binary).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use super::*;
use crate::tables::DimStyle;

pub(super) fn format_dimension_value(dim: &Dimension, style: &DimStyle) -> String {
    let is_angular = dim.base().is_angular();
    let mut value = dim.measurement();
    if !is_angular {
        value *= style.dimlfac;
    }
    if style.dimrnd.is_finite() && style.dimrnd > 0.0 {
        value = (value / style.dimrnd).round() * style.dimrnd;
    }
    let decimals = if is_angular {
        style.dimadec.clamp(0, 12) as usize
    } else {
        style.dimdec.clamp(0, 12) as usize
    };
    let mut text = format!("{value:.decimals$}");
    if style.dimdsep == 44 {
        text = text.replace('.', ",");
    }
    let zero_flags = if is_angular {
        style.dimazin
    } else {
        style.dimzin
    };
    if zero_flags & 1 != 0 {
        if let Some(index) = text.find("0.") {
            if index == 0 {
                text.remove(0);
            } else if index == 1 && text.starts_with('-') {
                text.remove(1);
            }
        }
        if let Some(index) = text.find("0,") {
            if index == 0 {
                text.remove(0);
            } else if index == 1 && text.starts_with('-') {
                text.remove(1);
            }
        }
    }
    if zero_flags & 2 != 0 {
        let separator = if style.dimdsep == 44 { ',' } else { '.' };
        if let Some(index) = text.find(separator) {
            let trimmed = text.trim_end_matches('0');
            text = trimmed.trim_end_matches(separator).to_owned();
            if text.is_empty() || text == "-" {
                text = "0".to_owned();
            } else if index >= text.len() && text.ends_with(separator) {
                text.pop();
            }
        }
    }
    let postfix = &style.dimpost;
    if postfix.is_empty() {
        return text;
    }
    if postfix.contains("<>") {
        postfix.replace("<>", &text)
    } else {
        format!("{text}{postfix}")
    }
}

pub(super) fn dimension_text_value(dim: &Dimension, style: Option<&DimStyle>) -> Option<String> {
    let base = dim.base();
    let Some(override_text) = base.text_override() else {
        return Some(
            style
                .map(|style| format_dimension_value(dim, style))
                .unwrap_or_else(|| format!("{:.4}", dim.measurement())),
        );
    };
    // A single space is ACAD's documented text suppression convention.
    if override_text == " " {
        return None;
    }
    let formatted = style
        .map(|style| format_dimension_value(dim, style))
        .unwrap_or_else(|| format!("{:.4}", dim.measurement()));
    Some(override_text.replace("<>", &formatted))
}

pub(super) fn dimension_extension_points(
    measured: Vector3,
    projected: Vector3,
    style: &DimStyle,
) -> (Vector3, Vector3) {
    let delta = projected - measured;
    let direction = if delta.length_squared() > 1.0e-20 {
        delta.normalize()
    } else {
        Vector3::ZERO
    };
    let scale = if style.dimscale.is_finite() {
        style.dimscale.abs()
    } else {
        1.0
    };
    let start = measured + direction * (style.dimexo.max(0.0) * scale);
    let end = if style.dimfxlon {
        start + direction * (style.dimfxl.max(0.0) * scale)
    } else {
        projected + direction * (style.dimexe.max(0.0) * scale)
    };
    (start, end)
}

pub(super) fn styled_dimension_line_segments(
    first: Vector3,
    second: Vector3,
    text_middle: Vector3,
    arrow_size: f64,
    gap: f64,
) -> Vec<(Vector3, Vector3)> {
    let axis = second - first;
    let length = axis.length();
    if length <= 1.0e-20 {
        return Vec::new();
    }
    let direction = axis / length;
    let projection = (text_middle - first).dot(&direction);
    let first_limit = first + direction * arrow_size.min(length * 0.5);
    let second_limit = second - direction * arrow_size.min(length * 0.5);
    let mut result = Vec::new();
    if projection > 0.0 && projection < length {
        let center = first + direction * projection;
        let half_gap = gap.max(0.0);
        let left = center - direction * half_gap;
        let right = center + direction * half_gap;
        if first_limit.distance(&left) > 1.0e-12 {
            result.push((first_limit, left));
        }
        if right.distance(&second_limit) > 1.0e-12 {
            result.push((right, second_limit));
        }
    } else if projection >= length {
        if first_limit.distance(&second_limit) > 1.0e-12 {
            result.push((first_limit, second_limit));
        }
        let outside_end = second + direction * arrow_size;
        if second.distance(&outside_end) > 1.0e-12 {
            result.push((second, outside_end));
        }
    } else {
        if first_limit.distance(&second_limit) > 1.0e-12 {
            result.push((first_limit, second_limit));
        }
        let outside_start = first - direction * arrow_size;
        if outside_start.distance(&first) > 1.0e-12 {
            result.push((outside_start, first));
        }
    }
    result
}

/// Explode a dimension with an optional DIMSTYLE table entry.
///
/// The detached [`EntityType::explode`] API has no document/table context and
/// therefore retains its historical defaults. [`CadDocument::explode_entity`]
/// supplies the resolved style through this function so offsets, precision,
/// suppression flags, and text height follow the drawing's DIMSTYLE.
pub(crate) fn explode_dimension_with_style(
    dim: &Dimension,
    style: Option<&DimStyle>,
) -> Vec<EntityType> {
    // Simplified decomposition: produce extension lines, dimension line,
    // and a text entity with the measurement value.
    let base = dim.base();
    let common = &base.common;

    let text_value = dimension_text_value(dim, style);
    let style_scale = style
        .map(|style| {
            if style.dimscale.is_finite() {
                style.dimscale.abs()
            } else {
                1.0
            }
        })
        .unwrap_or(1.0);
    let arrow_size = style
        .map(|style| (style.dimasz * style_scale).abs())
        .filter(|size| size.is_finite() && *size > 0.0)
        .unwrap_or(2.5);
    let dimension_gap = style
        .map(|style| (style.dimgap * style_scale).abs())
        .filter(|gap| gap.is_finite())
        .unwrap_or(0.0);

    let mut result = Vec::new();
    let mut arrows = Vec::new();

    // Dimension geometry is defined by two measured points and a point on the
    // dimension line.  Project each measured point onto that line so the
    // exploded extension and dimension lines meet at their endpoints.
    let projected_pair =
        |first: Vector3, second: Vector3, on_line: Vector3, line_direction: Vector3| {
            let direction = line_direction.normalize();
            if direction.length_squared() <= 1e-20 {
                return (first, second);
            }
            let project = |point: Vector3| {
                let offset = point - on_line;
                on_line + direction * offset.dot(&direction)
            };
            (project(first), project(second))
        };

    match dim {
        Dimension::Linear(d) => {
            let direction = Matrix3::arbitrary_axis(base.normal)
                * Vector3::new(d.rotation.cos(), d.rotation.sin(), 0.0);
            let (first_on_line, second_on_line) =
                projected_pair(d.first_point, d.second_point, d.definition_point, direction);
            if let Some(style) = style {
                if !style.dimse1 {
                    let (start, end) =
                        dimension_extension_points(d.first_point, first_on_line, style);
                    result.push(line_entity(start, end, 0.0, base.normal, common));
                }
                if !style.dimse2 {
                    let (start, end) =
                        dimension_extension_points(d.second_point, second_on_line, style);
                    result.push(line_entity(start, end, 0.0, base.normal, common));
                }
                if !style.dimsd1 || !style.dimsd2 {
                    for (start, end) in styled_dimension_line_segments(
                        first_on_line,
                        second_on_line,
                        base.text_middle_point,
                        arrow_size,
                        dimension_gap,
                    ) {
                        result.push(line_entity(start, end, 0.0, base.normal, common));
                    }
                }
            } else {
                result.push(line_entity(
                    d.first_point,
                    first_on_line,
                    0.0,
                    base.normal,
                    common,
                ));
                result.push(line_entity(
                    d.second_point,
                    second_on_line,
                    0.0,
                    base.normal,
                    common,
                ));
                // ACAD leaves a text gap in the dimension line.  Emit the two
                // visible portions around the text location.
                let axis = second_on_line - first_on_line;
                let t = if axis.length_squared() > 1e-20 {
                    ((base.text_middle_point - first_on_line).dot(&axis) / axis.length_squared())
                        .clamp(0.0, 1.0)
                } else {
                    0.5
                };
                let split = first_on_line + axis * t;
                result.push(line_entity(first_on_line, split, 0.0, base.normal, common));
                result.push(line_entity(split, second_on_line, 0.0, base.normal, common));
            }
            arrows.push((first_on_line, second_on_line));
            arrows.push((second_on_line, first_on_line));
        }
        Dimension::Aligned(d) => {
            let (first_on_line, second_on_line) = projected_pair(
                d.first_point,
                d.second_point,
                d.definition_point,
                d.second_point - d.first_point,
            );
            if let Some(style) = style {
                if !style.dimse1 {
                    let (start, end) =
                        dimension_extension_points(d.first_point, first_on_line, style);
                    result.push(line_entity(start, end, 0.0, base.normal, common));
                }
                if !style.dimse2 {
                    let (start, end) =
                        dimension_extension_points(d.second_point, second_on_line, style);
                    result.push(line_entity(start, end, 0.0, base.normal, common));
                }
                if !style.dimsd1 || !style.dimsd2 {
                    for (start, end) in styled_dimension_line_segments(
                        first_on_line,
                        second_on_line,
                        base.text_middle_point,
                        arrow_size,
                        dimension_gap,
                    ) {
                        result.push(line_entity(start, end, 0.0, base.normal, common));
                    }
                }
            } else {
                result.push(line_entity(
                    d.first_point,
                    first_on_line,
                    0.0,
                    base.normal,
                    common,
                ));
                result.push(line_entity(
                    d.second_point,
                    second_on_line,
                    0.0,
                    base.normal,
                    common,
                ));
                // Keep the measured alignment and split the dimension line at
                // the text location, matching ACAD's five LINE result.
                result.push(line_entity(
                    d.first_point,
                    d.second_point,
                    0.0,
                    base.normal,
                    common,
                ));
                let axis = second_on_line - first_on_line;
                let t = if axis.length_squared() > 1e-20 {
                    ((base.text_middle_point - first_on_line).dot(&axis) / axis.length_squared())
                        .clamp(0.0, 1.0)
                } else {
                    0.5
                };
                let split = first_on_line + axis * t;
                result.push(line_entity(first_on_line, split, 0.0, base.normal, common));
                result.push(line_entity(split, second_on_line, 0.0, base.normal, common));
            }
            arrows.push((first_on_line, second_on_line));
            arrows.push((second_on_line, first_on_line));
        }
        Dimension::Radius(d) => {
            result.push(line_entity(
                d.angle_vertex,
                d.definition_point,
                0.0,
                base.normal,
                common,
            ));
        }
        Dimension::Diameter(d) => {
            result.push(line_entity(
                d.angle_vertex,
                d.definition_point,
                0.0,
                base.normal,
                common,
            ));
        }
        Dimension::Angular2Ln(d) => {
            result.push(line_entity(
                d.angle_vertex,
                d.first_point,
                0.0,
                base.normal,
                common,
            ));
            result.push(line_entity(
                d.angle_vertex,
                d.second_point,
                0.0,
                base.normal,
                common,
            ));
        }
        Dimension::Angular3Pt(d) => {
            result.push(line_entity(
                d.angle_vertex,
                d.first_point,
                0.0,
                base.normal,
                common,
            ));
            result.push(line_entity(
                d.angle_vertex,
                d.second_point,
                0.0,
                base.normal,
                common,
            ));
        }
        Dimension::Ordinate(d) => {
            let points = d.leader_polyline(0.0, 0.0, None);
            for pair in points.windows(2) {
                if (pair[1] - pair[0]).length() > 1e-12 {
                    result.push(line_entity(pair[0], pair[1], 0.0, base.normal, common));
                }
            }
        }
        Dimension::Arc(d) => {
            result.push(line_entity(
                d.center_point,
                d.first_extension_point,
                0.0,
                base.normal,
                common,
            ));
            result.push(line_entity(
                d.center_point,
                d.second_extension_point,
                0.0,
                base.normal,
                common,
            ));
            if d.has_leader {
                result.push(line_entity(
                    d.first_leader_point,
                    d.second_leader_point,
                    0.0,
                    base.normal,
                    common,
                ));
            }
        }
        Dimension::LargeRadial(d) => {
            result.push(line_entity(
                d.definition_point,
                d.jog_point,
                0.0,
                base.normal,
                common,
            ));
            result.push(line_entity(
                d.jog_point,
                d.chord_point,
                0.0,
                base.normal,
                common,
            ));
        }
    }

    for (index, (tip, toward)) in arrows.into_iter().enumerate() {
        // A named _NONE arrow block suppresses the corresponding arrowhead;
        // custom block geometry still requires document block context and is
        // handled by the regular default arrow fallback here.
        let arrow_suppressed = style.is_some_and(|style| {
            (index == 0 && style.dimblk1_name.eq_ignore_ascii_case("_none"))
                || (index == 1 && style.dimblk2_name.eq_ignore_ascii_case("_none"))
        });
        if !arrow_suppressed {
            result.push(dimension_style_arrow(
                tip,
                toward,
                arrow_size,
                common,
                base.normal,
            ));
        }
    }
    if let Some(text_value) = text_value {
        result.push(dimension_mtext(base, text_value, style));
    }

    result
}

pub(super) fn explode_dimension(dim: &Dimension) -> Vec<EntityType> {
    explode_dimension_with_style(dim, None)
}