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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//! Native ACAD layer-state storage.
//!
//! ACAD keeps named layer states as XRecords in the
//! `ACAD_LAYERSTATES` dictionary attached to the layer table's extension
//! dictionary.  This module exposes that object graph as semantic Rust data
//! and provides capture, restore, rename, and delete operations.

use std::ops::{BitOr, BitOrAssign};

use crate::objects::{
    Dictionary, DictionaryCloningFlags, ObjectType, XRecord, XRecordEntry, XRecordValue,
};
use crate::tables::normalize_name;
use crate::types::{Color, Handle, LineWeight, Transparency};
use crate::CadDocument;

const LAYER_STATES_DICTIONARY: &str = "ACAD_LAYERSTATES";

/// Layer properties selected for restoration from a named state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LayerStateMask(u32);

impl LayerStateMask {
    pub const NONE: Self = Self(0);
    pub const ON: Self = Self(0x0001);
    pub const FROZEN: Self = Self(0x0002);
    pub const LOCKED: Self = Self(0x0004);
    pub const PLOT: Self = Self(0x0008);
    pub const NEW_VIEWPORT: Self = Self(0x0010);
    pub const COLOR: Self = Self(0x0020);
    pub const LINE_TYPE: Self = Self(0x0040);
    pub const LINE_WEIGHT: Self = Self(0x0080);
    pub const PLOT_STYLE: Self = Self(0x0100);
    pub const CURRENT_VIEWPORT: Self = Self(0x0200);
    pub const TRANSPARENCY: Self = Self(0x0400);
    pub const LAST_RESTORED: Self = Self(0x1_0000);

    /// Properties represented by [`crate::tables::Layer`].
    pub const SUPPORTED: Self = Self(
        Self::ON.0
            | Self::FROZEN.0
            | Self::LOCKED.0
            | Self::PLOT.0
            | Self::NEW_VIEWPORT.0
            | Self::COLOR.0
            | Self::LINE_TYPE.0
            | Self::LINE_WEIGHT.0
            | Self::PLOT_STYLE.0
            | Self::TRANSPARENCY.0,
    );

    /// All standard ACAD layer-state properties.
    pub const ALL: Self = Self(0x07ff);

    pub const fn from_bits(bits: u32) -> Self {
        Self(bits)
    }

    pub const fn bits(self) -> u32 {
        self.0
    }

    pub const fn contains(self, other: Self) -> bool {
        (self.0 & other.0) == other.0
    }
}

impl BitOr for LayerStateMask {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self(self.0 | rhs.0)
    }
}

impl BitOrAssign for LayerStateMask {
    fn bitor_assign(&mut self, rhs: Self) {
        self.0 |= rhs.0;
    }
}

/// Saved properties for one layer.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LayerStateLayer {
    pub layer_handle: Handle,
    pub layer_name: String,
    pub off: bool,
    pub frozen: bool,
    pub locked: bool,
    pub plottable: bool,
    pub new_viewport_frozen: bool,
    pub color: Color,
    #[cfg_attr(feature = "serde", serde(default))]
    pub color_name: Option<String>,
    #[cfg_attr(feature = "serde", serde(default))]
    pub book_name: Option<String>,
    pub line_type: String,
    pub line_weight: LineWeight,
    pub plot_style: String,
    pub transparency: Option<Transparency>,
}

/// A named layer state stored in a drawing.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LayerState {
    pub name: String,
    pub description: String,
    pub mask: LayerStateMask,
    pub hidden: bool,
    pub current_layer: String,
    pub layers: Vec<LayerStateLayer>,
}

impl CadDocument {
    pub(crate) fn rename_layer_state_references(&mut self, old_key: &str, new_name: &str) {
        let Some(dictionary_handle) = self.layer_states_dictionary_handle() else {
            return;
        };
        let handles = match self.objects.get(&dictionary_handle) {
            Some(ObjectType::Dictionary(dictionary)) => dictionary
                .entries
                .iter()
                .map(|(_, handle)| *handle)
                .collect::<Vec<_>>(),
            _ => return,
        };
        for handle in handles {
            let Some(ObjectType::XRecord(xrecord)) = self.objects.get_mut(&handle) else {
                continue;
            };
            let header_end = xrecord
                .entries
                .iter()
                .position(|entry| entry.code == 330)
                .unwrap_or(xrecord.entries.len());
            let Some(entry) = xrecord.entries[..header_end].iter_mut().find(|entry| {
                entry.code == 302
                    && entry
                        .value
                        .as_string()
                        .is_some_and(|name| normalize_name(name) == old_key)
            }) else {
                continue;
            };
            entry.value = XRecordValue::String(new_name.to_string());
            xrecord.raw_data.clear();
            xrecord.raw_dwg_data = None;
        }
    }

    /// Return every native layer state stored in the drawing.
    pub fn layer_states(&self) -> Vec<LayerState> {
        let Some(dictionary_handle) = self.layer_states_dictionary_handle() else {
            return Vec::new();
        };
        let Some(ObjectType::Dictionary(dictionary)) = self.objects.get(&dictionary_handle) else {
            return Vec::new();
        };

        dictionary
            .entries
            .iter()
            .filter_map(|(name, handle)| {
                let ObjectType::XRecord(xrecord) = self.objects.get(handle)? else {
                    return None;
                };
                Some(self.decode_layer_state(name, xrecord))
            })
            .collect()
    }

    /// Return one native layer state by name (case-insensitive).
    pub fn layer_state(&self, name: &str) -> Option<LayerState> {
        self.layer_states()
            .into_iter()
            .find(|state| state.name.eq_ignore_ascii_case(name))
    }

    /// Capture the current layer table and store it under `name`.
    ///
    /// Existing states with the same name are updated in place.
    pub fn capture_layer_state(
        &mut self,
        name: impl Into<String>,
        description: impl Into<String>,
    ) -> Handle {
        let name = name.into();
        let current_layer = if self.header.current_layer_name.is_empty() {
            self.layers
                .iter()
                .find(|layer| layer.handle == self.header.current_layer_handle)
                .map(|layer| layer.name.clone())
                .unwrap_or_else(|| "0".to_string())
        } else {
            self.header.current_layer_name.clone()
        };
        let layers = self
            .layers
            .iter()
            .map(|layer| LayerStateLayer {
                layer_handle: layer.handle,
                layer_name: layer.name.clone(),
                off: layer.flags.off,
                frozen: layer.flags.frozen,
                locked: layer.flags.locked,
                plottable: layer.is_plottable,
                new_viewport_frozen: layer.flags.frozen_in_new_viewport,
                color: layer.color,
                color_name: layer.color_name.clone(),
                book_name: layer.book_name.clone(),
                line_type: layer.line_type.clone(),
                line_weight: layer.line_weight,
                plot_style: if layer.plot_style.is_empty() {
                    format!("Color_{}", layer.color.approximate_index())
                } else {
                    layer.plot_style.clone()
                },
                transparency: Some(layer.transparency),
            })
            .collect();

        self.store_layer_state(LayerState {
            name,
            description: description.into(),
            mask: LayerStateMask::SUPPORTED,
            hidden: false,
            current_layer,
            layers,
        })
    }

    /// Store a semantic layer state in the native ACAD dictionary/XRecord form.
    ///
    /// Existing states with the same name are updated in place.
    pub fn store_layer_state(&mut self, state: LayerState) -> Handle {
        let dictionary_handle = self.ensure_layer_states_dictionary();
        let existing_handle = self
            .dictionary_entry(dictionary_handle, &state.name)
            .map(|(_, handle)| handle);
        let xrecord_handle = existing_handle.unwrap_or_else(|| self.allocate_handle());
        let entries = self.encode_layer_state(&state);

        let mut xrecord = match self.objects.remove(&xrecord_handle) {
            Some(ObjectType::XRecord(xrecord)) => xrecord,
            _ => XRecord::named(&state.name),
        };
        xrecord.handle = xrecord_handle;
        xrecord.owner = dictionary_handle;
        xrecord.reactors = vec![dictionary_handle];
        xrecord.name = state.name.clone();
        xrecord.cloning_flags = DictionaryCloningFlags::KeepExisting;
        xrecord.entries = entries;
        xrecord.raw_data.clear();
        xrecord.raw_dwg_data = None;
        xrecord.raw_dwg_handle_bits = 0;
        xrecord.raw_dwg_version = None;
        self.objects
            .insert(xrecord_handle, ObjectType::XRecord(xrecord));

        if let Some(ObjectType::Dictionary(dictionary)) = self.objects.get_mut(&dictionary_handle) {
            if let Some((key, handle)) = dictionary
                .entries
                .iter_mut()
                .find(|(key, _)| key.eq_ignore_ascii_case(&state.name))
            {
                *key = state.name;
                *handle = xrecord_handle;
            } else {
                dictionary.entries.push((state.name, xrecord_handle));
            }
        }

        xrecord_handle
    }

    /// Restore the selected properties of a named state.
    pub fn restore_layer_state(&mut self, name: &str) -> Option<usize> {
        let state = self.layer_state(name)?;
        let mut restored = 0usize;

        for saved in &state.layers {
            let layer_name = if !saved.layer_handle.is_null() {
                self.layers
                    .iter()
                    .find(|layer| layer.handle == saved.layer_handle)
                    .map(|layer| layer.name.clone())
            } else {
                None
            }
            .unwrap_or_else(|| saved.layer_name.clone());
            let layer = self.layers.get_mut(&layer_name);

            let Some(layer) = layer else {
                continue;
            };
            if state.mask.contains(LayerStateMask::ON) {
                layer.flags.off = saved.off;
            }
            if state.mask.contains(LayerStateMask::FROZEN) {
                layer.flags.frozen = saved.frozen;
            }
            if state.mask.contains(LayerStateMask::LOCKED) {
                layer.flags.locked = saved.locked;
            }
            if state.mask.contains(LayerStateMask::PLOT) {
                layer.is_plottable = saved.plottable;
            }
            if state.mask.contains(LayerStateMask::NEW_VIEWPORT) {
                layer.flags.frozen_in_new_viewport = saved.new_viewport_frozen;
            }
            if state.mask.contains(LayerStateMask::COLOR) {
                layer.color = saved.color;
                layer.color_name.clone_from(&saved.color_name);
                layer.book_name.clone_from(&saved.book_name);
            }
            if state.mask.contains(LayerStateMask::LINE_TYPE) && !saved.line_type.is_empty() {
                layer.line_type.clone_from(&saved.line_type);
            }
            if state.mask.contains(LayerStateMask::LINE_WEIGHT) {
                layer.line_weight = saved.line_weight;
            }
            if state.mask.contains(LayerStateMask::PLOT_STYLE) {
                layer.plot_style.clone_from(&saved.plot_style);
            }
            if state.mask.contains(LayerStateMask::TRANSPARENCY) {
                if let Some(transparency) = saved.transparency {
                    layer.transparency = transparency;
                }
            }
            restored += 1;
        }

        if let Some(layer) = self.layers.get(&state.current_layer) {
            self.header.current_layer_handle = layer.handle;
            self.header.current_layer_name.clone_from(&layer.name);
        }

        Some(restored)
    }

    /// Rename a native layer state. Returns `false` when the source is missing
    /// or the destination already exists.
    pub fn rename_layer_state(&mut self, old_name: &str, new_name: &str) -> bool {
        let Some(dictionary_handle) = self.layer_states_dictionary_handle() else {
            return false;
        };
        if self.dictionary_entry(dictionary_handle, new_name).is_some()
            && !old_name.eq_ignore_ascii_case(new_name)
        {
            return false;
        }
        let Some(ObjectType::Dictionary(dictionary)) = self.objects.get_mut(&dictionary_handle)
        else {
            return false;
        };
        let Some((key, handle)) = dictionary
            .entries
            .iter_mut()
            .find(|(key, _)| key.eq_ignore_ascii_case(old_name))
        else {
            return false;
        };
        *key = new_name.to_string();
        let xrecord_handle = *handle;
        if let Some(ObjectType::XRecord(xrecord)) = self.objects.get_mut(&xrecord_handle) {
            xrecord.name = new_name.to_string();
        }
        true
    }

    /// Delete a native layer state.
    pub fn delete_layer_state(&mut self, name: &str) -> bool {
        let Some(dictionary_handle) = self.layer_states_dictionary_handle() else {
            return false;
        };
        let Some(ObjectType::Dictionary(dictionary)) = self.objects.get_mut(&dictionary_handle)
        else {
            return false;
        };
        let Some(index) = dictionary
            .entries
            .iter()
            .position(|(key, _)| key.eq_ignore_ascii_case(name))
        else {
            return false;
        };
        let (_, handle) = dictionary.entries.remove(index);
        self.objects.remove(&handle);
        true
    }

    fn layer_extension_dictionary_handle(&self) -> Option<Handle> {
        let table_handle = self.layers.handle();
        self.xdic_by_handle
            .get(&table_handle)
            .copied()
            .filter(|handle| self.objects.contains_key(handle))
            .or_else(|| {
                self.objects
                    .iter()
                    .find_map(|(handle, object)| match object {
                        ObjectType::Dictionary(dictionary) if dictionary.owner == table_handle => {
                            Some(*handle)
                        }
                        _ => None,
                    })
            })
    }

    fn layer_states_dictionary_handle(&self) -> Option<Handle> {
        let extension_handle = self.layer_extension_dictionary_handle()?;
        let Some(ObjectType::Dictionary(extension)) = self.objects.get(&extension_handle) else {
            return None;
        };
        extension
            .entries
            .iter()
            .find(|(key, _)| {
                key.eq_ignore_ascii_case(LAYER_STATES_DICTIONARY)
                    || key.eq_ignore_ascii_case("ACAD_LAYERSTATE")
            })
            .map(|(_, handle)| *handle)
    }

    fn ensure_layer_states_dictionary(&mut self) -> Handle {
        let table_handle = self.layers.handle();
        let extension_handle = self.layer_extension_dictionary_handle().unwrap_or_else(|| {
            let handle = self.allocate_handle();
            let mut dictionary = Dictionary::new();
            dictionary.handle = handle;
            dictionary.owner = table_handle;
            dictionary.hard_owner = true;
            self.objects
                .insert(handle, ObjectType::Dictionary(dictionary));
            handle
        });
        self.xdic_by_handle.insert(table_handle, extension_handle);

        if let Some(handle) = self.layer_states_dictionary_handle() {
            if matches!(self.objects.get(&handle), Some(ObjectType::Dictionary(_))) {
                return handle;
            }
        }

        let dictionary_handle = self.allocate_handle();
        let mut dictionary = Dictionary::new();
        dictionary.handle = dictionary_handle;
        dictionary.owner = extension_handle;
        dictionary.reactors = vec![extension_handle];
        self.objects
            .insert(dictionary_handle, ObjectType::Dictionary(dictionary));

        if let Some(ObjectType::Dictionary(extension)) = self.objects.get_mut(&extension_handle) {
            if let Some((key, handle)) = extension.entries.iter_mut().find(|(key, _)| {
                key.eq_ignore_ascii_case(LAYER_STATES_DICTIONARY)
                    || key.eq_ignore_ascii_case("ACAD_LAYERSTATE")
            }) {
                *key = LAYER_STATES_DICTIONARY.to_string();
                *handle = dictionary_handle;
            } else {
                extension
                    .entries
                    .push((LAYER_STATES_DICTIONARY.to_string(), dictionary_handle));
            }
        }
        dictionary_handle
    }

    fn dictionary_entry(&self, dictionary_handle: Handle, name: &str) -> Option<(String, Handle)> {
        let Some(ObjectType::Dictionary(dictionary)) = self.objects.get(&dictionary_handle) else {
            return None;
        };
        dictionary
            .entries
            .iter()
            .find(|(key, _)| key.eq_ignore_ascii_case(name))
            .cloned()
    }

    fn decode_layer_state(&self, name: &str, xrecord: &XRecord) -> LayerState {
        let first_layer = xrecord
            .entries
            .iter()
            .position(|entry| entry.code == 330)
            .unwrap_or(xrecord.entries.len());
        let header = &xrecord.entries[..first_layer];
        let mask = header
            .iter()
            .find(|entry| entry.code == 91)
            .and_then(|entry| entry.value.as_i32())
            .map(|value| LayerStateMask::from_bits(value as u32))
            .unwrap_or(LayerStateMask::ALL);
        let description = header
            .iter()
            .find(|entry| entry.code == 301)
            .and_then(|entry| entry.value.as_string())
            .unwrap_or_default()
            .to_string();
        let hidden = header
            .iter()
            .find(|entry| entry.code == 290)
            .and_then(|entry| entry.value.as_bool())
            .unwrap_or(false);
        let current_layer = header
            .iter()
            .find(|entry| entry.code == 302)
            .and_then(|entry| entry.value.as_string())
            .unwrap_or_default()
            .to_string();

        let mut layers = Vec::new();
        let mut start = first_layer;
        while start < xrecord.entries.len() {
            let end = xrecord.entries[start + 1..]
                .iter()
                .position(|entry| entry.code == 330)
                .map(|offset| start + 1 + offset)
                .unwrap_or(xrecord.entries.len());
            let group = &xrecord.entries[start..end];
            if let Some(layer) = self.decode_layer_state_layer(group) {
                layers.push(layer);
            }
            start = end;
        }

        LayerState {
            name: name.to_string(),
            description,
            mask,
            hidden,
            current_layer,
            layers,
        }
    }

    fn decode_layer_state_layer(&self, entries: &[XRecordEntry]) -> Option<LayerStateLayer> {
        let layer_handle = entries
            .first()
            .filter(|entry| entry.code == 330)?
            .value
            .as_handle()?;
        let flags = entry_i32(entries, 90).unwrap_or(0);
        let color = entry_i32(entries, 92)
            .or_else(|| entry_i32(entries, 420))
            .map(Color::from_true_color_value)
            .unwrap_or_else(|| Color::from_index(entry_i32(entries, 62).unwrap_or(7) as i16));
        let (book_name, color_name) = entry_string(entries, 300)
            .map(crate::io::dxf::split_color_book_name)
            .unwrap_or((None, None));
        let line_type_handle = entry_handle(entries, 331).unwrap_or(Handle::NULL);
        let layer_name = self
            .layers
            .iter()
            .find(|layer| layer.handle == layer_handle)
            .map(|layer| layer.name.clone())
            .unwrap_or_default();
        let line_type = self
            .line_types
            .iter()
            .find(|line_type| line_type.handle == line_type_handle)
            .map(|line_type| line_type.name.clone())
            .unwrap_or_default();

        Some(LayerStateLayer {
            layer_handle,
            layer_name,
            off: flags & 0x01 != 0,
            frozen: flags & 0x02 != 0,
            locked: flags & 0x04 != 0,
            plottable: flags & 0x08 != 0,
            new_viewport_frozen: flags & 0x10 != 0,
            color,
            color_name,
            book_name,
            line_type,
            line_weight: LineWeight::from_value(entry_i32(entries, 370).unwrap_or(-3) as i16),
            plot_style: entry_string(entries, 1).unwrap_or_default().to_string(),
            transparency: entry_i32(entries, 440)
                .and_then(|value| Transparency::from_layer_alpha_value(value as u32)),
        })
    }

    fn encode_layer_state(&self, state: &LayerState) -> Vec<XRecordEntry> {
        let mut entries = vec![
            XRecordEntry::int32(91, state.mask.bits() as i32),
            XRecordEntry::string(301, &state.description),
            XRecordEntry::bool(290, state.hidden),
            XRecordEntry::string(302, &state.current_layer),
        ];

        for layer in &state.layers {
            let layer_handle = if layer.layer_handle.is_null() {
                self.layers
                    .get(&layer.layer_name)
                    .map(|layer| layer.handle)
                    .unwrap_or(Handle::NULL)
            } else {
                layer.layer_handle
            };
            if layer_handle.is_null() {
                continue;
            }

            let mut flags = 0i32;
            if layer.off {
                flags |= 0x01;
            }
            if layer.frozen {
                flags |= 0x02;
            }
            if layer.locked {
                flags |= 0x04;
            }
            if layer.plottable {
                flags |= 0x08;
            }
            if layer.new_viewport_frozen {
                flags |= 0x10;
            }

            let line_type_handle = self
                .line_types
                .iter()
                .find(|line_type| line_type.name.eq_ignore_ascii_case(&layer.line_type))
                .map(|line_type| line_type.handle)
                .or_else(|| {
                    self.line_types
                        .get("Continuous")
                        .map(|line_type| line_type.handle)
                })
                .unwrap_or(Handle::NULL);
            entries.push(XRecordEntry::handle(330, layer_handle));
            entries.push(XRecordEntry::int32(90, flags));
            entries.push(XRecordEntry::int16(62, layer.color.approximate_index()));
            if let Some(true_color) = layer.color.to_true_color_value() {
                entries.push(XRecordEntry::int32(
                    92,
                    (0xC200_0000u32 | true_color as u32) as i32,
                ));
            }
            entries.push(XRecordEntry::int16(370, layer.line_weight.value()));
            entries.push(XRecordEntry::handle(331, line_type_handle));
            entries.push(XRecordEntry::string(1, &layer.plot_style));
            if let Some(transparency) = layer.transparency {
                entries.push(XRecordEntry::int32(440, transparency.to_dxf_value()));
            }
            if let Some(name) = crate::io::dxf::join_color_book_name(
                layer.book_name.as_deref(),
                layer.color_name.as_deref(),
            ) {
                entries.push(XRecordEntry::string(300, &name));
            }
        }
        entries
    }
}

fn entry_i32(entries: &[XRecordEntry], code: i32) -> Option<i32> {
    entries
        .iter()
        .find(|entry| entry.code == code)
        .and_then(|entry| entry.value.as_i32())
}

fn entry_handle(entries: &[XRecordEntry], code: i32) -> Option<Handle> {
    entries
        .iter()
        .find(|entry| entry.code == code)
        .and_then(|entry| entry.value.as_handle())
}

fn entry_string(entries: &[XRecordEntry], code: i32) -> Option<&str> {
    entries
        .iter()
        .find(|entry| entry.code == code)
        .and_then(|entry| entry.value.as_string())
}