acadrust 0.3.2

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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
//! Common object-writing helpers
//!
//! These methods form the shared infrastructure for writing every DWG
//! object record: CRC wrapping, size encoding, entity/non-entity
//! preambles, extended-data emission, and handle-map registration.

use crate::io::dwg::crc;
use crate::io::dwg::dwg_reference_type::DwgReferenceType;
use crate::types::{Color, Handle, Transparency};

use super::DwgObjectWriter;

// ── DWG fixed object-type codes ─────────────────────────────────────
/// Standard DWG object type codes for entities and table entries.
pub const OBJ_TEXT: i16 = 1;
pub const OBJ_ATTRIB: i16 = 2;
pub const OBJ_ATTDEF: i16 = 3;
pub const OBJ_BLOCK: i16 = 4;
pub const OBJ_ENDBLK: i16 = 5;
pub const OBJ_SEQEND: i16 = 6;
pub const OBJ_INSERT: i16 = 7;
pub const OBJ_MINSERT: i16 = 8;
pub const OBJ_VERTEX_2D: i16 = 10;
pub const OBJ_VERTEX_3D: i16 = 11;
pub const OBJ_VERTEX_MESH: i16 = 12;
pub const OBJ_VERTEX_PFACE: i16 = 13;
pub const OBJ_VERTEX_PFACE_FACE: i16 = 14;
pub const OBJ_POLYLINE_2D: i16 = 15;
pub const OBJ_POLYLINE_3D: i16 = 16;
pub const OBJ_ARC: i16 = 17;
pub const OBJ_CIRCLE: i16 = 18;
pub const OBJ_LINE: i16 = 19;
pub const OBJ_DIMENSION_ORDINATE: i16 = 20;
pub const OBJ_DIMENSION_LINEAR: i16 = 21;
pub const OBJ_DIMENSION_ALIGNED: i16 = 22;
pub const OBJ_DIMENSION_ANG_3PT: i16 = 23;
pub const OBJ_DIMENSION_ANG_2LN: i16 = 24;
pub const OBJ_DIMENSION_RADIUS: i16 = 25;
pub const OBJ_DIMENSION_DIAMETER: i16 = 26;
pub const OBJ_POINT: i16 = 27;
pub const OBJ_3DFACE: i16 = 28;
pub const OBJ_POLYLINE_PFACE: i16 = 29;
pub const OBJ_POLYLINE_MESH: i16 = 30;
pub const OBJ_SOLID: i16 = 31;
pub const OBJ_TRACE: i16 = 32;
pub const OBJ_SHAPE: i16 = 33;
pub const OBJ_VIEWPORT: i16 = 34;
pub const OBJ_ELLIPSE: i16 = 35;
pub const OBJ_SPLINE: i16 = 36;
pub const OBJ_REGION: i16 = 37;
pub const OBJ_3DSOLID: i16 = 38;
pub const OBJ_BODY: i16 = 39;
pub const OBJ_RAY: i16 = 40;
pub const OBJ_XLINE: i16 = 41;
pub const OBJ_DICTIONARY: i16 = 42;
pub const OBJ_OLEFRAME: i16 = 43;
pub const OBJ_MTEXT: i16 = 44;
pub const OBJ_LEADER: i16 = 45;
pub const OBJ_TOLERANCE: i16 = 46;
pub const OBJ_MLINE: i16 = 47;
pub const OBJ_BLOCK_CONTROL: i16 = 48;
pub const OBJ_BLOCK_HEADER: i16 = 49;
pub const OBJ_LAYER_CONTROL: i16 = 50;
pub const OBJ_LAYER: i16 = 51;
pub const OBJ_STYLE_CONTROL: i16 = 52;
pub const OBJ_STYLE: i16 = 53;
pub const OBJ_LTYPE_CONTROL: i16 = 56;
pub const OBJ_LTYPE: i16 = 57;
pub const OBJ_VIEW_CONTROL: i16 = 60;
pub const OBJ_VIEW: i16 = 61;
pub const OBJ_UCS_CONTROL: i16 = 62;
pub const OBJ_UCS: i16 = 63;
pub const OBJ_VPORT_CONTROL: i16 = 64;
pub const OBJ_VPORT: i16 = 65;
pub const OBJ_APPID_CONTROL: i16 = 66;
pub const OBJ_APPID: i16 = 67;
pub const OBJ_DIMSTYLE_CONTROL: i16 = 68;
pub const OBJ_DIMSTYLE: i16 = 69;
pub const OBJ_VPENT_HDR_CONTROL: i16 = 70;
pub const OBJ_VPENT_HDR: i16 = 71;
pub const OBJ_GROUP: i16 = 72;
pub const OBJ_MLINESTYLE: i16 = 73;
pub const OBJ_OLE2FRAME: i16 = 74;

// Standard table-type fixed codes (77+)
pub const OBJ_LWPOLYLINE: i16 = 77;    // standard fixed type in R14+
pub const OBJ_HATCH: i16 = 78;         // standard fixed type

// Class-based (UNLISTED) entity types.
// These must ALWAYS be resolved via class_type_code(), never used directly.
// The values below are just fallbacks — never valid as fixed type codes.
pub const OBJ_IMAGE: i16 = -1;         // UNLISTED: always use class number
pub const OBJ_MESH: i16 = -2;          // UNLISTED: always use class number
pub const OBJ_MULTILEADER: i16 = -3;   // UNLISTED: always use class number

// Fixed-type non-graphical objects (standard type codes from ODA spec)
pub const OBJ_XRECORD: i16 = 79;        // 0x4F
pub const OBJ_PLACEHOLDER: i16 = 80;    // 0x50
pub const OBJ_LAYOUT: i16 = 82;         // 0x52 (R2004+; for R2004Pre use class number)

// Class-based (variable) non-graphical objects:
// These are UNLISTED in C# — for R2004+ the standard type code works,
// but for pre-R2004 they should use the DXF class number.
// We use the ODA-documented type codes that work for R2004+.
pub const OBJ_DICTIONARYWDFLT: i16 = 0x78;  // 120 (class-based)
pub const OBJ_DICTIONARYVAR: i16 = 0x79;    // 121
pub const OBJ_PLOTSETTINGS: i16 = 0x7A;     // 122
pub const OBJ_MLEADERSTYLE: i16 = 0x7B;     // 123
pub const OBJ_IMAGEDEF: i16 = 0x7C;         // 124
pub const OBJ_IMAGEDEFREACTOR: i16 = 0x7D;  // 125
pub const OBJ_SCALE: i16 = 0x7E;            // 126
pub const OBJ_SORTENTSTABLE: i16 = 0x7F;    // 127
pub const OBJ_RASTERVARIABLES: i16 = 0x80;  // 128
pub const OBJ_DBCOLOR: i16 = 0x81;          // 129
pub const OBJ_WIPEOUTVARIABLES: i16 = 0x82; // 130

// ── Methods on DwgObjectWriter ──────────────────────────────────────
impl<'a> DwgObjectWriter<'a> {
    // ── register_object ─────────────────────────────────────────────
    /// Finalise the current object record in `self.writer` and append it
    /// to the output stream, recording the handle→offset mapping.
    ///
    /// Record structure (per ODA spec §20.2):
    /// ```text
    /// [ModularShort(size)][R2010+: ModularChar(handle_bits)][merged_data][CRC16]
    /// ```
    ///
    /// **CRC-16** (seed `0xC0C1`) covers everything from the ModularShort
    /// through the end of `merged_data`.  AutoCAD validates this checksum
    /// on every object load ("Level 3" check).
    ///
    /// **Byte alignment**: The merged data from `merge()` is guaranteed
    /// byte-aligned (all three sub-streams are spear-shifted and the
    /// final output is flushed).  This is critical because the CRC-16
    /// operates on complete bytes.
    pub fn register_object(&mut self, handle: Handle) {
        // 1. Merge all sub-streams → single byte buffer
        //    (handle_start_bits is recorded during merge by the merged writer)
        let data = self.writer.merge();

        // Verify the merged data is byte-aligned.  If this fires, the
        // merge function has a bug — partial bytes would corrupt the
        // CRC-16 and cause "Invalid Input" in AutoCAD.
        debug_assert!(
            !data.is_empty() || handle.is_null(),
            "register_object: merged data is empty for handle {:#X}",
            handle.value()
        );

        // 2. Compute handle-stream bit count for R2010+ MC header field.
        // Must be computed AFTER merge: handle_bits = total_bits - handle_start.
        // This matches C#: sizeb = (msmain.Length << 3) - SavedPositionInBits
        let handle_bits = if self.version.r2010_plus() {
            let total_bits = (data.len() as i64) * 8;
            let hstart = self.writer.handle_start_bits();
            debug_assert!(
                hstart >= 0 && hstart <= total_bits,
                "handle_start_bits ({}) out of range [0, {}]",
                hstart,
                total_bits
            );
            total_bits - hstart
        } else {
            0
        };

        // 3. Build output record
        let pos = self.output.len() as u32;

        // 3a. Size (modular short) — the merged data byte count.
        //     Per the ODA spec, this is the byte count of the data area
        //     (includes object data AND handle reference data).
        write_modular_short_bytes(&mut self.output, data.len());

        // 3b. R2010+: handle stream size in bits (modular char)
        if self.version.r2010_plus() {
            write_modular_char_bytes(&mut self.output, handle_bits as usize);
        }

        // 3c. Merged data (byte-aligned, complete object record)
        self.output.extend_from_slice(&data);

        // 4. CRC-16 over everything from `pos`:
        //    [MS(size)] [MC(handle_bits)] [merged_data]
        //    Seed 0xC0C1, no final XOR.  This is the per-object checksum
        //    that AutoCAD validates ("Level 3" object integrity check).
        let crc_val = crc::crc16(crc::CRC16_SEED, &self.output[pos as usize..]);
        self.output.extend_from_slice(&crc_val.to_le_bytes());

        // 5. Record handle → byte-offset mapping.
        //    These offsets are relative to the start of the AcDbObjects
        //    section data (which includes the 0x0DCA marker for R2004+).
        //    The Handles section (Object Map / Address Table) uses these
        //    offsets for handle → object lookup.
        if !handle.is_null() {
            self.handle_map.push((handle.value(), pos));
        }

        // 6. Reset per-object writer for the next object
        self.writer.reset();
    }

    /// Write a raw (pre-encoded) object record verbatim.
    ///
    /// Used for unknown entities that carry the original merged-stream
    /// bytes from the source file.  The raw data is the exact payload
    /// that was between `[ModularShort(size)]` and `[CRC16]` in the
    /// original file.  We re-frame it with a fresh MS prefix and CRC.
    pub fn register_raw_object(&mut self, handle: Handle, raw_data: &[u8], handle_bits: i64) {
        let pos = self.output.len() as u32;

        // MS (size)
        write_modular_short_bytes(&mut self.output, raw_data.len());

        // R2010+: MC (handle bits) — reproduce the original value
        if self.version.r2010_plus() {
            write_modular_char_bytes(&mut self.output, handle_bits as usize);
        }

        // Merged data bytes
        self.output.extend_from_slice(raw_data);

        // CRC-16
        let crc_val = crc::crc16(crc::CRC16_SEED, &self.output[pos as usize..]);
        self.output.extend_from_slice(&crc_val.to_le_bytes());

        // Handle → offset mapping
        if !handle.is_null() {
            self.handle_map.push((handle.value(), pos));
        }

        // No need to reset writer — we didn't use it
    }

    // ── write_common_data ───────────────────────────────────────────
    /// Object type + handle + extended-data preamble shared by
    /// every object (entities AND non-graphical objects).
    pub fn write_common_data(
        &mut self,
        type_code: i16,
        handle: Handle,
        xdata: &crate::xdata::ExtendedData,
    ) {
        // Object type (BS or MC depending on version)
        self.writer.write_object_type(type_code);

        // R2000..R2007: save position for deferred size field
        if self.version.r2000_plus() && !self.version.r2010_plus() {
            self.writer.save_position_for_size();
        }

        // Handle (absolute)
        self.writer.main_mut().write_handle_undefined(handle.value());

        // Extended data
        self.write_extended_data(xdata);
    }

    // ── write_common_entity_data ────────────────────────────────────
    /// Full preamble for an entity: type code, handle, xdata, graphic
    /// flag, entity mode, reactors/xdic, layer/linetype, colour,
    /// line-weight, prev/next entity chain.
    ///
    /// Field order must match the C# reference exactly because both the
    /// main-stream and handle-stream are order-sensitive.
    pub fn write_common_entity_data(
        &mut self,
        type_code: i16,
        handle: Handle,
        owner_handle: Handle,
        layer: &str,
        color: &Color,
        line_weight: &crate::types::LineWeight,
        transparency: &Transparency,
        invisible: bool,
        linetype_scale: f64,
        linetype: &str,
        xdata: &crate::xdata::ExtendedData,
        reactors: &[Handle],
        xdictionary_handle: &Option<Handle>,
    ) {
        // ── MAIN + HANDLE: shared preamble (type + handle + xdata) ──
        self.write_common_data(type_code, handle, xdata);

        // ── MAIN: graphic presence flag ──
        self.writer.write_bit(false);

        // ── MAIN: R13-R14 save position for size ──
        if self.version.r13_14_only() {
            self.writer.save_position_for_size();
        }

        // ── MAIN: entity mode (2 bits) ──
        let entmode = self.get_entity_mode(&owner_handle);
        self.writer.write_2bits(entmode);

        // ── HANDLE: owner (if entmode == 0) ──
        if entmode == 0 {
            self.writer
                .write_handle(DwgReferenceType::SoftPointer, owner_handle.value());
        }

        // ── MAIN + HANDLE: reactors + xdic ──
        // Reactor count (MAIN)
        self.writer.write_bit_long(reactors.len() as i32);
        // Reactor handles (HANDLE)
        for r in reactors {
            self.writer
                .write_handle(DwgReferenceType::SoftPointer, r.value());
        }

        // R2004+: no-xdic flag (MAIN) + conditional xdic handle (HANDLE)
        // Pre-R2004: always write xdic handle (0 if none)
        if self.version.r2004_plus() {
            self.writer.write_bit(xdictionary_handle.is_none());
            // Xdic handle (HANDLE) — only if present
            if let Some(xdic) = xdictionary_handle {
                self.writer
                    .write_handle(DwgReferenceType::HardOwnership, xdic.value());
            }
        } else {
            // Pre-R2004: always write xdic handle (0 if none)
            let xdic_val = xdictionary_handle
                .map(|h| h.value())
                .unwrap_or(0);
            self.writer
                .write_handle(DwgReferenceType::HardOwnership, xdic_val);
        }

        // Enqueue extension dictionary so its object is written later
        if let Some(xdic) = xdictionary_handle {
            if !xdic.is_null() {
                self.object_queue.push_back(*xdic);
            }
        }

        // R2013+: binary-data-present flag (MAIN)
        if self.version.r2013_plus(self.dxf_version) {
            self.writer.write_bit(false);
        }

        // ── R13-R14 only: layer + linetype ──
        if self.version.r13_14_only() {
            // Layer handle (HANDLE: hard pointer)
            let layer_h = self
                .document
                .layers
                .get(layer)
                .map(|l| l.handle)
                .unwrap_or(Handle::NULL);
            self.writer
                .write_handle(DwgReferenceType::HardPointer, layer_h.value());

            // Isbylayerlt flag (MAIN)
            self.writer.write_bit(true); // simplified: always by-layer
            // If not by-layer, would write linetype handle here
        }

        // ── R13-R2000 (pre-R2004): Nolinks + prev/next entity chain ──
        // In R13/R14/R2000, entities in a block form a doubly-linked list.
        // NOLINKS bit = 1 means handles are sequential (reader infers
        // prev = handle-1, next = handle+1) and prev/next handles are omitted.
        // NOLINKS bit = 0 means prev/next handles are written explicitly.
        if !self.version.r2004_plus() {
            let prev_h = self.prev_handle.unwrap_or(Handle::NULL);
            let next_h = self.next_handle.unwrap_or(Handle::NULL);
            let has_links = !prev_h.is_null()
                && prev_h.value() == handle.value().wrapping_sub(1)
                && !next_h.is_null()
                && next_h.value() == handle.value().wrapping_add(1);

            // MAIN: Nolinks bit (true = sequential, reader infers prev/next)
            self.writer.write_bit(has_links);

            // HANDLE: prev + next entity handles only when NOT sequential
            if !has_links {
                self.writer
                    .write_handle(DwgReferenceType::SoftPointer, prev_h.value());
                self.writer
                    .write_handle(DwgReferenceType::SoftPointer, next_h.value());
            }
        }

        // ── MAIN: Color (EnColor) ──
        if self.version.r2000_plus() {
            self.writer.write_en_color(color, transparency);
        } else {
            // R13-R14: colour as CMC
            self.writer.write_cm_color(color);
        }

        // ── MAIN: Linetype scale ──
        self.writer.write_bit_double(linetype_scale);

        // ── R13-R14 only: invisibility + early return ──
        // DXF group 60 convention (all DWG versions): 0 = visible, non-zero = invisible
        if self.version.r13_14_only() {
            self.writer.write_bit_short(if invisible { 1 } else { 0 });
            return;
        }

        // ── R2000+: Layer handle (HANDLE: hard pointer) ──
        let layer_h = self
            .document
            .layers
            .get(layer)
            .map(|l| l.handle)
            .unwrap_or(Handle::NULL);
        self.writer
            .write_handle(DwgReferenceType::HardPointer, layer_h.value());

        // ── MAIN: Linetype flags ──
        // 00 = bylayer, 01 = byblock, 10 = continuous, 11 = handle present
        let lt_lower = linetype.to_ascii_lowercase();
        if lt_lower == "bylayer" || lt_lower.is_empty() {
            self.writer.write_2bits(0b00);
        } else if lt_lower == "byblock" {
            self.writer.write_2bits(0b01);
        } else if lt_lower == "continuous" {
            self.writer.write_2bits(0b10);
        } else {
            // Named linetype — write handle
            self.writer.write_2bits(0b11);
            let lt_handle = self
                .document
                .line_types
                .get(linetype)
                .map(|lt| lt.handle)
                .unwrap_or(Handle::NULL);
            self.writer
                .write_handle(DwgReferenceType::HardPointer, lt_handle.value());
        }

        // ── R2007+: material flags + shadow flags ──
        if self.version.r2007_plus() {
            // Material flags BB (00 = by layer)
            self.writer.write_2bits(0b00);
            // Shadow flags RC
            self.writer.write_byte(0);
        }

        // ── R2000+: Plotstyle flags ──
        self.writer.write_2bits(0b00); // simplified: always by-layer

        // ── R2007+ (>AC1021): visual style bits ──
        if self.version.r2010_plus() {
            self.writer.write_bit(false); // has full visual style
            self.writer.write_bit(false); // has face visual style
            self.writer.write_bit(false); // has edge visual style
        }

        // ── MAIN: Invisibility ──
        self.writer.write_bit_short(if invisible { 1 } else { 0 });

        // ── R2000+: Lineweight (5-bit DWG index) ──
        self.writer.write_byte(line_weight.to_dwg_index());
    }

    // ── write_common_non_entity_data ────────────────────────────────
    /// Preamble for non-entity objects: type code, handle,
    /// owner handle, reactors, xdictionary.
    pub fn write_common_non_entity_data(
        &mut self,
        type_code: i16,
        handle: Handle,
        owner_handle: Handle,
        reactors: &[Handle],
        xdictionary_handle: &Option<Handle>,
    ) {
        // ── writeCommonData portion ──

        // Object type
        self.writer.write_object_type(type_code);

        // R2000..R2007: size placeholder (AC1015..AC1021)
        // C#: this._version >= AC1015 && this._version < AC1024
        // R2010 (AC1024) does NOT get a size placeholder.
        if self.version.r2000_plus() && !self.version.r2010_plus() {
            self.writer.save_position_for_size();
        }

        // Handle (absolute)
        self.writer.main_mut().write_handle_undefined(handle.value());

        // Extended data — empty for non-entities
        let empty = crate::xdata::ExtendedData::default();
        self.write_extended_data(&empty);

        // ── R13-R14 Only: size placeholder (after xdata, before owner) ──
        if self.version.r13_14_only() {
            self.writer.save_position_for_size();
        }

        // ── HANDLE: Owner handle (soft pointer) ──
        // Check for owner override (e.g. for ATTRIB extension dictionaries
        // whose parent entity was re-allocated with a new handle)
        let effective_owner = self.owner_overrides
            .get(&handle)
            .copied()
            .unwrap_or(owner_handle);
        self.writer
            .write_handle(DwgReferenceType::SoftPointer, effective_owner.value());

        // ── writeReactorsAndDictionaryHandle portion ──

        // MAIN: Reactor count
        self.writer.write_bit_long(reactors.len() as i32);

        // HANDLE: Reactor handles
        for r in reactors {
            self.writer
                .write_handle(DwgReferenceType::SoftPointer, r.value());
        }

        // R2004+: MAIN no-xdic flag + conditional HANDLE xdic
        // Pre-R2004: HANDLE xdic always written (0 if null)
        let no_xdic = xdictionary_handle.is_none();
        if self.version.r2004_plus() {
            self.writer.write_bit(no_xdic);
            if !no_xdic {
                self.writer.write_handle(
                    DwgReferenceType::HardOwnership,
                    xdictionary_handle.unwrap().value(),
                );
            }
        } else {
            // Pre-R2004: always emit xdic handle (0 if None)
            let xdic_val = xdictionary_handle
                .map(|h| h.value())
                .unwrap_or(0);
            self.writer
                .write_handle(DwgReferenceType::HardOwnership, xdic_val);
        }

        // Enqueue extension dictionary so its object is written later
        if let Some(xdic) = xdictionary_handle {
            if !xdic.is_null() {
                self.object_queue.push_back(*xdic);
            }
        }

        // R2013+: binary-data flag
        if self.version.r2013_plus(self.dxf_version) {
            self.writer.write_bit(false);
        }
    }

    // ── write_xref_dependant_bit ────────────────────────────────────
    /// 64-group "xref dependant" flag.
    pub fn write_xref_dependant_bit(&mut self) {
        self.write_xref_dependant_bit_value(false);
    }

    /// 64-group "xref dependant" flag with explicit value.
    pub fn write_xref_dependant_bit_value(&mut self, xref_dep: bool) {
        if self.version.r2007_plus() {
            // R2007+: xrefindex+1 BS 70 (combined flags)
            let combined: i16 = if xref_dep { 0x10 } else { 0 };
            self.writer.write_bit_short(combined);
        } else {
            // Pre-R2007: 64-flag B (Referenced), xrefindex+1 BS, Xdep B (XrefDependent)
            self.writer.write_bit(false); // referenced flag
            self.writer.write_bit_short(0); // xrefindex+1
            self.writer.write_bit(xref_dep); // xref dependent flag
        }
    }

    // ── write_extended_data ─────────────────────────────────────────
    /// Write registered-application extended data (XDATA) blocks.
    /// For now, writes a zero-count, meaning "no xdata".
    pub fn write_extended_data(&mut self, _xdata: &crate::xdata::ExtendedData) {
        // EED size terminator: BS 0 = no more xdata applications
        self.writer.write_bit_short(0);
    }

    // ── sub-entity handle allocator ────────────────────────────────
    /// Allocate a new unique handle for sub-entities (vertices, seqend)
    /// that don't have handles assigned by the document.
    pub fn alloc_handle(&mut self) -> Handle {
        let h = self.next_alloc_handle;
        self.next_alloc_handle += 1;
        Handle::new(h)
    }

    // ── class_type_code ─────────────────────────────────────────────
    /// Look up the DXF class number for an UNLISTED object type.
    ///
    /// In C# ACadSharp, types not in the `ObjectType` enum (UNLISTED)
    /// **always** use their DXF class number (500+) as the DWG type
    /// code — regardless of version.  Only fixed types (0–82 in the
    /// ODA spec) use literal type codes.
    pub fn class_type_code(&self, dxf_name: &str, fallback: i16) -> i16 {
        self.document
            .classes
            .get_by_name(dxf_name)
            .map(|c| c.class_number)
            .unwrap_or(fallback)
    }

    // ── entity-mode helper ──────────────────────────────────────────
    /// Returns the 2-bit entity-mode value (per ODA spec §19.4.4):
    /// - 0 = owned (owner handle present) — VERTEX, ATTRIB, SEQEND,
    ///       or entity inside a named block
    /// - 1 = paper-space entity (BB 01 → any *Paper_Space block)
    /// - 2 = model-space entity (BB 10 → *Model_Space)
    ///
    /// AutoCAD uses entmode=1 for entities in ALL paper-space blocks,
    /// not just the canonical `*Paper_Space`.  Additional layouts like
    /// `*Paper_Space0`, `*Paper_Space1`, … also get entmode=1.  The
    /// correct owner is determined by the BLOCK_HEADER's entity_handles
    /// list, not by the entity's owner handle (which is omitted for
    /// entmode != 0).
    fn get_entity_mode(&self, owner_handle: &Handle) -> u8 {
        for br in self.document.block_records.iter() {
            if br.handle == *owner_handle {
                let upper = br.name.to_ascii_uppercase();
                if upper == "*MODEL_SPACE" {
                    return 2; // model space (BB 10)
                }
                if upper == "*PAPER_SPACE"
                    || (upper.starts_with("*PAPER_SPACE")
                        && upper.len() > 12
                        && upper[12..].bytes().all(|b| b.is_ascii_digit()))
                {
                    return 1; // paper space (BB 01)
                }
                return 0; // named block → owned
            }
        }
        0
    }
}

// ── Helper: write modular-short to a byte vec ───────────────────────
/// Encode `value` as a DWG modular short (MS) and append to `output`.
/// Each 16-bit word carries 15 data bits; bit 15 = continuation flag.
pub(crate) fn write_modular_short_bytes(output: &mut Vec<u8>, value: usize) {
    let mut remaining = value;
    loop {
        let word = (remaining & 0x7FFF) as u16;
        remaining >>= 15;
        if remaining > 0 {
            output.extend_from_slice(&(word | 0x8000).to_le_bytes());
        } else {
            output.extend_from_slice(&word.to_le_bytes());
            break;
        }
    }
}

/// Encode `value` as a DWG modular char (MC) and append to `output`.
/// Each byte carries 7 data bits; bit 7 = continuation flag.
/// This is used for R2010+ handle-stream bit count in the record header.
pub(crate) fn write_modular_char_bytes(output: &mut Vec<u8>, value: usize) {
    if value == 0 {
        output.push(0);
        return;
    }
    let mut remaining = value;
    while remaining > 0 {
        let b = (remaining & 0x7F) as u8;
        remaining >>= 7;
        if remaining > 0 {
            output.push(b | 0x80);
        } else {
            output.push(b);
        }
    }
}

// ── Tests ──────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn modular_short_small() {
        let mut buf = Vec::new();
        write_modular_short_bytes(&mut buf, 100);
        assert_eq!(buf.len(), 2);
        assert_eq!(u16::from_le_bytes([buf[0], buf[1]]), 100);
    }

    #[test]
    fn modular_short_large() {
        let mut buf = Vec::new();
        // 0x8000 requires two words
        write_modular_short_bytes(&mut buf, 0x8000);
        assert_eq!(buf.len(), 4);
        // First word has continuation flag
        let w0 = u16::from_le_bytes([buf[0], buf[1]]);
        assert_ne!(w0 & 0x8000, 0);
        // Second word has no flag
        let w1 = u16::from_le_bytes([buf[2], buf[3]]);
        assert_eq!(w1 & 0x8000, 0);
        // Reconstruct
        let lo = (w0 & 0x7FFF) as usize;
        let hi = (w1 & 0x7FFF) as usize;
        assert_eq!(lo | (hi << 15), 0x8000);
    }

    #[test]
    fn modular_short_zero() {
        let mut buf = Vec::new();
        write_modular_short_bytes(&mut buf, 0);
        assert_eq!(buf.len(), 2);
        assert_eq!(u16::from_le_bytes([buf[0], buf[1]]), 0);
    }

    #[test]
    fn modular_short_max_single_word() {
        let mut buf = Vec::new();
        write_modular_short_bytes(&mut buf, 0x7FFF);
        assert_eq!(buf.len(), 2);
        assert_eq!(u16::from_le_bytes([buf[0], buf[1]]), 0x7FFF);
    }
}