@bill_nee/dwg-reader 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/LICENSE +615 -0
  2. package/cordis.patch.yml +3 -0
  3. package/icon.svg +15 -0
  4. package/lib/index.js +355 -0
  5. package/locale/en.json +6 -0
  6. package/locale/zh.json +6 -0
  7. package/package.json +32 -4
  8. package/vendor/libredwg-web/lib/converter/converter.js +1102 -0
  9. package/vendor/libredwg-web/lib/converter/dwgColorToMLeaderRawColor.js +55 -0
  10. package/vendor/libredwg-web/lib/converter/entityConverter.js +2050 -0
  11. package/vendor/libredwg-web/lib/converter/index.js +3 -0
  12. package/vendor/libredwg-web/lib/converter/utils.js +46 -0
  13. package/vendor/libredwg-web/lib/database/classes/class.js +2 -0
  14. package/vendor/libredwg-web/lib/database/classes/index.js +2 -0
  15. package/vendor/libredwg-web/lib/database/codepage.js +101 -0
  16. package/vendor/libredwg-web/lib/database/common.js +2 -0
  17. package/vendor/libredwg-web/lib/database/database.js +2 -0
  18. package/vendor/libredwg-web/lib/database/entities/3dface.js +2 -0
  19. package/vendor/libredwg-web/lib/database/entities/arc.js +2 -0
  20. package/vendor/libredwg-web/lib/database/entities/arcAlignedText.js +2 -0
  21. package/vendor/libredwg-web/lib/database/entities/attdef.js +2 -0
  22. package/vendor/libredwg-web/lib/database/entities/attrib.js +2 -0
  23. package/vendor/libredwg-web/lib/database/entities/circle.js +2 -0
  24. package/vendor/libredwg-web/lib/database/entities/dimension.js +70 -0
  25. package/vendor/libredwg-web/lib/database/entities/ellipse.js +2 -0
  26. package/vendor/libredwg-web/lib/database/entities/entity.js +2 -0
  27. package/vendor/libredwg-web/lib/database/entities/hatch.js +54 -0
  28. package/vendor/libredwg-web/lib/database/entities/image.js +2 -0
  29. package/vendor/libredwg-web/lib/database/entities/index.js +38 -0
  30. package/vendor/libredwg-web/lib/database/entities/insert.js +2 -0
  31. package/vendor/libredwg-web/lib/database/entities/leader.js +2 -0
  32. package/vendor/libredwg-web/lib/database/entities/line.js +2 -0
  33. package/vendor/libredwg-web/lib/database/entities/lwpolyline.js +2 -0
  34. package/vendor/libredwg-web/lib/database/entities/mline.js +2 -0
  35. package/vendor/libredwg-web/lib/database/entities/mtext.js +2 -0
  36. package/vendor/libredwg-web/lib/database/entities/multileader.js +2 -0
  37. package/vendor/libredwg-web/lib/database/entities/ole2frame.js +2 -0
  38. package/vendor/libredwg-web/lib/database/entities/oleframe.js +2 -0
  39. package/vendor/libredwg-web/lib/database/entities/point.js +2 -0
  40. package/vendor/libredwg-web/lib/database/entities/polyline.js +19 -0
  41. package/vendor/libredwg-web/lib/database/entities/proxy.js +10 -0
  42. package/vendor/libredwg-web/lib/database/entities/ray.js +2 -0
  43. package/vendor/libredwg-web/lib/database/entities/section.js +2 -0
  44. package/vendor/libredwg-web/lib/database/entities/shape.js +2 -0
  45. package/vendor/libredwg-web/lib/database/entities/solid.js +2 -0
  46. package/vendor/libredwg-web/lib/database/entities/solid3d.js +2 -0
  47. package/vendor/libredwg-web/lib/database/entities/spline.js +2 -0
  48. package/vendor/libredwg-web/lib/database/entities/table.js +2 -0
  49. package/vendor/libredwg-web/lib/database/entities/text.js +23 -0
  50. package/vendor/libredwg-web/lib/database/entities/tolerance.js +2 -0
  51. package/vendor/libredwg-web/lib/database/entities/trace.js +2 -0
  52. package/vendor/libredwg-web/lib/database/entities/vertex.js +2 -0
  53. package/vendor/libredwg-web/lib/database/entities/viewport.js +2 -0
  54. package/vendor/libredwg-web/lib/database/entities/wipeout.js +2 -0
  55. package/vendor/libredwg-web/lib/database/entities/xline.js +2 -0
  56. package/vendor/libredwg-web/lib/database/header/header.js +2 -0
  57. package/vendor/libredwg-web/lib/database/header/index.js +3 -0
  58. package/vendor/libredwg-web/lib/database/header/variables.js +217 -0
  59. package/vendor/libredwg-web/lib/database/index.js +12 -0
  60. package/vendor/libredwg-web/lib/database/objects/common.js +2 -0
  61. package/vendor/libredwg-web/lib/database/objects/dictionary.js +19 -0
  62. package/vendor/libredwg-web/lib/database/objects/imageDef.js +2 -0
  63. package/vendor/libredwg-web/lib/database/objects/index.js +11 -0
  64. package/vendor/libredwg-web/lib/database/objects/layerFilter.js +2 -0
  65. package/vendor/libredwg-web/lib/database/objects/layerIndex.js +2 -0
  66. package/vendor/libredwg-web/lib/database/objects/layout.js +2 -0
  67. package/vendor/libredwg-web/lib/database/objects/mleaderStyle.js +2 -0
  68. package/vendor/libredwg-web/lib/database/objects/plotSetting.js +2 -0
  69. package/vendor/libredwg-web/lib/database/objects/spatialFilter.js +2 -0
  70. package/vendor/libredwg-web/lib/database/objects/xrecord.js +2 -0
  71. package/vendor/libredwg-web/lib/database/shared/index.js +2 -0
  72. package/vendor/libredwg-web/lib/database/shared/xdata.js +2 -0
  73. package/vendor/libredwg-web/lib/database/tables/appId.js +2 -0
  74. package/vendor/libredwg-web/lib/database/tables/blockRecord.js +2 -0
  75. package/vendor/libredwg-web/lib/database/tables/dimStyle.js +2 -0
  76. package/vendor/libredwg-web/lib/database/tables/index.js +9 -0
  77. package/vendor/libredwg-web/lib/database/tables/layer.js +2 -0
  78. package/vendor/libredwg-web/lib/database/tables/ltype.js +2 -0
  79. package/vendor/libredwg-web/lib/database/tables/style.js +2 -0
  80. package/vendor/libredwg-web/lib/database/tables/table.js +2 -0
  81. package/vendor/libredwg-web/lib/database/tables/vport.js +2 -0
  82. package/vendor/libredwg-web/lib/database/textEscapes.js +19 -0
  83. package/vendor/libredwg-web/lib/database/version.js +260 -0
  84. package/vendor/libredwg-web/lib/index.js +4 -0
  85. package/vendor/libredwg-web/lib/libredwg.js +1271 -0
  86. package/vendor/libredwg-web/lib/svg/box2d.js +104 -0
  87. package/vendor/libredwg-web/lib/svg/bspline.js +95 -0
  88. package/vendor/libredwg-web/lib/svg/color.js +371 -0
  89. package/vendor/libredwg-web/lib/svg/index.js +2 -0
  90. package/vendor/libredwg-web/lib/svg/polyline.js +70 -0
  91. package/vendor/libredwg-web/lib/svg/svgConverter.js +540 -0
  92. package/vendor/libredwg-web/lib/svg/transformBoundingBoxAndElement.js +20 -0
  93. package/vendor/libredwg-web/lib/svg/vector.js +27 -0
  94. package/vendor/libredwg-web/lib/types/common.js +2 -0
  95. package/vendor/libredwg-web/lib/types/enums.js +390 -0
  96. package/vendor/libredwg-web/lib/types/index.js +3 -0
  97. package/vendor/libredwg-web/wasm/libredwg-web.js +2 -0
  98. package/vendor/libredwg-web/wasm/libredwg-web.wasm +0 -0
  99. package/README.md +0 -3
@@ -0,0 +1,1102 @@
1
+ import { HEADER_VARIABLES } from '../database/index.js';
2
+ import { Dwg_Object_Supertype, Dwg_Object_Type } from '../types/index.js';
3
+ import { dwgColorToMLeaderRawColor } from './dwgColorToMLeaderRawColor.js';
4
+ import { LibreEntityConverter } from './entityConverter.js';
5
+ import { idToString, isModelSpace, isPaperSpace, uint8ArrayToHexString } from './utils.js';
6
+ /**
7
+ * Class used to convert Dwg_Data instance to DwgDatabase instance.
8
+ */
9
+ export class LibreDwgConverter {
10
+ libredwg;
11
+ entityConverter;
12
+ /**
13
+ * Entity object indices keyed by owning BLOCK_HEADER object pointer.
14
+ * Built lazily when an ownership walk returns no entities (LibreDWG
15
+ * https://github.com/LibreDWG/libredwg/issues/1199).
16
+ */
17
+ orphanEntitiesByBlock = null;
18
+ /** Entity indices whose ownerhandle does not resolve to any object. */
19
+ ownerlessOrphanIndices = null;
20
+ orphanIndexDataPtr = null;
21
+ constructor(instance) {
22
+ this.libredwg = instance;
23
+ this.entityConverter = new LibreEntityConverter(instance);
24
+ }
25
+ convert(data) {
26
+ this.entityConverter.clear();
27
+ this.orphanEntitiesByBlock = null;
28
+ this.ownerlessOrphanIndices = null;
29
+ this.orphanIndexDataPtr = data;
30
+ const db = {
31
+ tables: {
32
+ APPID: {
33
+ entries: []
34
+ },
35
+ BLOCK_RECORD: {
36
+ entries: []
37
+ },
38
+ DIMSTYLE: {
39
+ entries: []
40
+ },
41
+ LAYER: {
42
+ entries: []
43
+ },
44
+ LTYPE: {
45
+ entries: []
46
+ },
47
+ STYLE: {
48
+ entries: []
49
+ },
50
+ VPORT: {
51
+ entries: []
52
+ }
53
+ },
54
+ objects: {
55
+ DICTIONARY: [],
56
+ IMAGEDEF: [],
57
+ LAYER_FILTER: [],
58
+ LAYER_INDEX: [],
59
+ LAYOUT: [],
60
+ MLEADERSTYLE: [],
61
+ SPATIAL_FILTER: [],
62
+ XRECORD: []
63
+ },
64
+ header: {},
65
+ entities: [],
66
+ classes: []
67
+ };
68
+ const libredwg = this.libredwg;
69
+ this.convertHeader(data, db.header);
70
+ this.convertClasses(data, db.classes);
71
+ this.entityConverter.setClasses(db.classes);
72
+ const num_objects = libredwg.dwg_get_num_objects(data);
73
+ for (let i = 0; i < num_objects; i++) {
74
+ const obj = libredwg.dwg_get_object(data, i);
75
+ if (obj) {
76
+ const tio = this.safeObjectTio(obj);
77
+ if (tio) {
78
+ const fixedtype = libredwg.dwg_object_get_fixedtype(obj);
79
+ switch (fixedtype) {
80
+ case Dwg_Object_Type.DWG_TYPE_LAYER:
81
+ {
82
+ const layer = this.convertLayer(tio, obj);
83
+ db.tables.LAYER.entries.push(layer);
84
+ this.entityConverter.layers.set(layer.handle, layer.name);
85
+ }
86
+ break;
87
+ case Dwg_Object_Type.DWG_TYPE_LTYPE:
88
+ {
89
+ const ltype = this.convertLineType(tio, obj);
90
+ db.tables.LTYPE.entries.push(ltype);
91
+ this.entityConverter.ltypes.set(ltype.handle, ltype.name);
92
+ }
93
+ break;
94
+ default:
95
+ break;
96
+ }
97
+ }
98
+ }
99
+ }
100
+ for (let i = 0; i < num_objects; i++) {
101
+ const obj = libredwg.dwg_get_object(data, i);
102
+ if (obj) {
103
+ const tio = this.safeObjectTio(obj);
104
+ if (tio) {
105
+ const fixedtype = libredwg.dwg_object_get_fixedtype(obj);
106
+ switch (fixedtype) {
107
+ case Dwg_Object_Type.DWG_TYPE_APPID:
108
+ db.tables.APPID.entries.push(this.convertAppId(tio));
109
+ break;
110
+ case Dwg_Object_Type.DWG_TYPE_BLOCK_HEADER:
111
+ {
112
+ const btr = this.convertBlockRecord(tio, obj);
113
+ db.tables.BLOCK_RECORD.entries.push(btr);
114
+ // Store entities in model space and paper space to db.entities to keep the
115
+ // the consistent data structure as DXF file
116
+ if (isModelSpace(btr.name) || isPaperSpace(btr.name)) {
117
+ btr.entities.forEach(entity => {
118
+ db.entities.push(entity);
119
+ // Store ATTRIB entity in db.entities to keep the the consistent data
120
+ // structure as DXF file
121
+ if (entity.type === 'INSERT') {
122
+ entity.attribs.forEach(attrib => {
123
+ db.entities.push(attrib);
124
+ });
125
+ }
126
+ });
127
+ }
128
+ }
129
+ break;
130
+ case Dwg_Object_Type.DWG_TYPE_DIMSTYLE:
131
+ db.tables.DIMSTYLE.entries.push(this.convertDimStyle(tio, obj));
132
+ break;
133
+ case Dwg_Object_Type.DWG_TYPE_STYLE:
134
+ db.tables.STYLE.entries.push(this.convertStyle(tio, obj));
135
+ break;
136
+ case Dwg_Object_Type.DWG_TYPE_VPORT:
137
+ db.tables.VPORT.entries.push(this.convertViewport(tio, obj));
138
+ break;
139
+ case Dwg_Object_Type.DWG_TYPE_DICTIONARY:
140
+ db.objects.DICTIONARY.push(this.convertDictionary(tio, obj));
141
+ break;
142
+ case Dwg_Object_Type.DWG_TYPE_IMAGEDEF:
143
+ db.objects.IMAGEDEF.push(this.convertImageDef(tio, obj));
144
+ break;
145
+ case Dwg_Object_Type.DWG_TYPE_LAYERFILTER:
146
+ db.objects.LAYER_FILTER.push(this.convertLayerFilter(tio, obj));
147
+ break;
148
+ case Dwg_Object_Type.DWG_TYPE_LAYER_INDEX:
149
+ db.objects.LAYER_INDEX.push(this.convertLayerIndex(tio, obj));
150
+ break;
151
+ case Dwg_Object_Type.DWG_TYPE_LAYOUT:
152
+ db.objects.LAYOUT.push(this.convertLayout(tio, obj));
153
+ break;
154
+ case Dwg_Object_Type.DWG_TYPE_MLEADERSTYLE:
155
+ db.objects.MLEADERSTYLE.push(this.convertMLeaderStyle(tio, obj));
156
+ break;
157
+ case Dwg_Object_Type.DWG_TYPE_SPATIAL_FILTER:
158
+ db.objects.SPATIAL_FILTER.push(this.convertSpatialFilter(tio, obj));
159
+ break;
160
+ case Dwg_Object_Type.DWG_TYPE_XRECORD:
161
+ db.objects.XRECORD.push(this.convertXRecord(tio, obj));
162
+ break;
163
+ default:
164
+ break;
165
+ }
166
+ }
167
+ }
168
+ }
169
+ // Process viewport entities: sort by objectId (handle) and assign viewportId
170
+ const viewportEntities = db.entities.filter(entity => entity.type === 'VIEWPORT');
171
+ viewportEntities.sort((a, b) => {
172
+ // Convert handle to hex number for sorting
173
+ const handleA = parseInt(a.handle, 16);
174
+ const handleB = parseInt(b.handle, 16);
175
+ return handleA - handleB;
176
+ });
177
+ // Assign viewportId starting from 1
178
+ viewportEntities.forEach((viewport, index) => {
179
+ viewport.viewportId = index + 1;
180
+ });
181
+ const thumbnail = libredwg.dwg_bmp(data);
182
+ if (thumbnail?.data?.length) {
183
+ db.thumbnailImage = thumbnail.data;
184
+ }
185
+ return db;
186
+ }
187
+ getConversionStats() {
188
+ return {
189
+ unknownEntityCount: this.entityConverter.unknownEntityCount
190
+ };
191
+ }
192
+ safeObjectTio(obj) {
193
+ const libredwg = this.libredwg;
194
+ try {
195
+ if (libredwg.dwg_object_get_supertype(obj) !=
196
+ Dwg_Object_Supertype.DWG_SUPERTYPE_OBJECT) {
197
+ return null;
198
+ }
199
+ const tio = libredwg.dwg_object_to_object_tio(obj);
200
+ return tio ? tio : null;
201
+ }
202
+ catch {
203
+ return null;
204
+ }
205
+ }
206
+ convertHeader(data, header) {
207
+ const libredwg = this.libredwg;
208
+ HEADER_VARIABLES.forEach(name => {
209
+ let var_name = name;
210
+ if (name == 'DIMBLK' || name == 'DIMBLK1' || name == 'DIMBLK2') {
211
+ var_name = var_name + '_T';
212
+ }
213
+ let value = libredwg.dwg_dynapi_header_data(data, var_name);
214
+ // Get object name if the 'value' is one Dwg_Object_Ref instance.
215
+ // TODO: handle 'CMLSTYLE' correctly
216
+ if (name == 'CELTYPE' ||
217
+ name == 'CLAYER' ||
218
+ name == 'CLAYER' ||
219
+ name == 'DIMSTYLE' ||
220
+ name == 'DIMTXSTY' ||
221
+ name == 'TEXTSTYLE') {
222
+ value = value ? libredwg.dwg_ref_get_object_name(value) : '';
223
+ }
224
+ else if (name == 'DRAGVS') {
225
+ value = value ? (libredwg.dwg_ref_get_absref(value) ?? 2) : 2;
226
+ }
227
+ // @ts-expect-error header variable name
228
+ header[name] = value;
229
+ });
230
+ }
231
+ convertClasses(data, classes) {
232
+ const libredwg = this.libredwg;
233
+ const count = libredwg.dwg_get_num_classes(data);
234
+ for (let index = 0; index < count; index++) {
235
+ const cls = libredwg.dwg_get_class(data, index);
236
+ classes.push({
237
+ dxfName: cls.dxfname,
238
+ cppName: cls.cppname,
239
+ appName: cls.appname,
240
+ capabilitiesFlag: cls.proxyflag,
241
+ instanceCount: cls.num_instances,
242
+ wasAProxyFlag: cls.s_zombie,
243
+ // DWG_TYPE_PROXY_ENTITY = 0x1F2 /* 498 */,
244
+ // DWG_TYPE_PROXY_OBJECT = 0x1F3 /* 499 */,
245
+ isAnEntityFlag: cls.item_class_id === 0x1f2
246
+ });
247
+ }
248
+ }
249
+ convertAppId(item) {
250
+ const libredwg = this.libredwg;
251
+ const name = libredwg.dwg_dynapi_entity_data(item, 'name');
252
+ const flag = libredwg.dwg_dynapi_entity_data(item, 'flag');
253
+ return {
254
+ name: name,
255
+ standardFlag: flag
256
+ };
257
+ }
258
+ convertBlockRecord(item, obj) {
259
+ const libredwg = this.libredwg;
260
+ const commonAttrs = this.getCommonTableEntryAttrs(item, obj);
261
+ // The BLOCK_HEADER has only the abbrevated name, but we want "*D30" instead of "*D".
262
+ // So get full name from BLOCK entity.
263
+ const block = libredwg.dwg_entity_block_header_get_block(item);
264
+ if (block.name) {
265
+ commonAttrs.name = block.name;
266
+ }
267
+ // The number of entities
268
+ const num_owned = libredwg.dwg_dynapi_entity_data(item, 'num_owned');
269
+ const flags = libredwg.dwg_dynapi_entity_data(item, 'flag');
270
+ const description = libredwg.dwg_dynapi_entity_data(item, 'description');
271
+ const basePoint = libredwg.dwg_dynapi_entity_data(item, 'base_pt');
272
+ const insertionUnits = libredwg.dwg_dynapi_entity_data(item, 'insert_units');
273
+ const explodability = libredwg.dwg_dynapi_entity_data(item, 'explodable');
274
+ const scalability = libredwg.dwg_dynapi_entity_data(item, 'block_scaling');
275
+ const layout_ptr = libredwg.dwg_dynapi_entity_data(item, 'layout');
276
+ const layout = (libredwg.dwg_ref_get_id(layout_ptr) ?? '');
277
+ let bmpPreview;
278
+ const bmpPreviewBinaryData = libredwg.dwg_entity_block_header_get_preview(item);
279
+ if (bmpPreviewBinaryData && bmpPreviewBinaryData.length > 0) {
280
+ bmpPreview = uint8ArrayToHexString(bmpPreviewBinaryData);
281
+ }
282
+ // Sometimes function get_first_owned_entity returns 0 when trying to iterate entities in one block.
283
+ // I guess it is one bug on libredwg. I logged [one bug](https://github.com/LibreDWG/libredwg/issues/1199)
284
+ // on libredwg too. In this time, I try to use property 'entities' of block header to iterate entities.
285
+ let entities = this.convertEntities(obj, commonAttrs.handle);
286
+ if (!entities || entities.length == 0) {
287
+ entities = [];
288
+ const entities_ptr = libredwg.dwg_dynapi_entity_data(item, 'entities');
289
+ if (entities_ptr) {
290
+ const object_ref_ptr_array = libredwg.dwg_ptr_to_object_ref_ptr_array(entities_ptr, num_owned);
291
+ const converter = this.entityConverter;
292
+ for (let index = 0; index < num_owned; index++) {
293
+ const object = libredwg.dwg_ref_get_object(object_ref_ptr_array[index]);
294
+ const entity = converter.convert(object);
295
+ if (entity) {
296
+ entity.ownerBlockRecordSoftId = commonAttrs.handle;
297
+ entities.push(entity);
298
+ }
299
+ }
300
+ }
301
+ }
302
+ // Some DWG writers (including LibreDWG's own) declare first_entity /
303
+ // last_entity handles that resolve to no object, or only link a
304
+ // partial ownership chain. Fall back to entity objects whose owner
305
+ // (ownerhandle / entmode via dwg_entity_owner) names this block.
306
+ // For *Model_Space only, also pick up entities with no resolvable owner.
307
+ if (this.orphanIndexDataPtr != null) {
308
+ const orphans = this.convertOrphanOwnedEntities(this.orphanIndexDataPtr, obj, commonAttrs.handle, isModelSpace(commonAttrs.name) === true);
309
+ if (orphans.length) {
310
+ if (entities.length === 0) {
311
+ entities = orphans;
312
+ }
313
+ else {
314
+ const seen = new Set(entities.map(e => e.handle));
315
+ for (const entity of orphans) {
316
+ if (!seen.has(entity.handle)) {
317
+ entities.push(entity);
318
+ }
319
+ }
320
+ }
321
+ }
322
+ }
323
+ return {
324
+ ...commonAttrs,
325
+ flags: flags,
326
+ description: description,
327
+ basePoint: basePoint,
328
+ layout: layout,
329
+ insertionUnits: insertionUnits,
330
+ explodability: explodability,
331
+ scalability: scalability,
332
+ bmpPreview: bmpPreview,
333
+ entities: entities
334
+ };
335
+ }
336
+ convertEntities(obj, ownerHandle) {
337
+ const libredwg = this.libredwg;
338
+ const converter = this.entityConverter;
339
+ const entities = [];
340
+ let next = libredwg.get_first_owned_entity(obj);
341
+ while (next) {
342
+ const entity = converter.convert(next);
343
+ if (entity) {
344
+ entity.ownerBlockRecordSoftId = ownerHandle;
345
+ entities.push(entity);
346
+ }
347
+ next = libredwg.get_next_owned_entity(obj, next);
348
+ }
349
+ return entities;
350
+ }
351
+ /**
352
+ * Builds (once) a map of BLOCK_HEADER object pointer → entity object indices
353
+ * for entities that are not reachable through the ownership chain, then
354
+ * converts the entities owned by `blockHeaderPtr`.
355
+ *
356
+ * @param includeOwnerless - When true (only for *Model_Space), also convert
357
+ * entities that still have no resolvable owner after ownerhandle + entmode
358
+ * fallback via dwg_entity_owner.
359
+ */
360
+ convertOrphanOwnedEntities(data, blockHeaderPtr, ownerHandle, includeOwnerless) {
361
+ this.ensureOrphanEntityIndex(data);
362
+ const byBlock = this.orphanEntitiesByBlock ?? new Map();
363
+ const owned = byBlock.get(blockHeaderPtr) ?? [];
364
+ const ownerless = includeOwnerless && this.ownerlessOrphanIndices
365
+ ? this.ownerlessOrphanIndices
366
+ : [];
367
+ const indices = owned.length && ownerless.length
368
+ ? owned.concat(ownerless)
369
+ : owned.length
370
+ ? owned
371
+ : ownerless;
372
+ if (indices.length === 0) {
373
+ return [];
374
+ }
375
+ const libredwg = this.libredwg;
376
+ const converter = this.entityConverter;
377
+ const entities = [];
378
+ for (const index of indices) {
379
+ const object = libredwg.dwg_get_object(data, index);
380
+ const entity = converter.convert(object);
381
+ if (entity) {
382
+ entity.ownerBlockRecordSoftId = ownerHandle;
383
+ entities.push(entity);
384
+ }
385
+ }
386
+ return entities;
387
+ }
388
+ ensureOrphanEntityIndex(data) {
389
+ if (this.orphanEntitiesByBlock) {
390
+ return;
391
+ }
392
+ const libredwg = this.libredwg;
393
+ const numObjects = libredwg.dwg_get_num_objects(data);
394
+ const indexByEntityPtr = new Map();
395
+ const entityIndices = [];
396
+ const blockHeaderPtrs = [];
397
+ // dwg_entity_owner returns a BLOCK_HEADER TIO pointer; map it back to the
398
+ // Dwg_Object* used as the orphan-index key.
399
+ const blockObjByTio = new Map();
400
+ for (let i = 0; i < numObjects; i++) {
401
+ const obj = libredwg.dwg_get_object(data, i);
402
+ if (!obj)
403
+ continue;
404
+ if (libredwg.dwg_object_get_supertype(obj) ===
405
+ Dwg_Object_Supertype.DWG_SUPERTYPE_ENTITY) {
406
+ entityIndices.push(i);
407
+ indexByEntityPtr.set(obj, i);
408
+ }
409
+ else if (libredwg.dwg_object_get_fixedtype(obj) ===
410
+ Dwg_Object_Type.DWG_TYPE_BLOCK_HEADER) {
411
+ blockHeaderPtrs.push(obj);
412
+ const tio = libredwg.dwg_object_to_object_tio(obj);
413
+ if (tio)
414
+ blockObjByTio.set(tio, obj);
415
+ }
416
+ }
417
+ const claimed = new Set();
418
+ for (const blockHeader of blockHeaderPtrs) {
419
+ let current = libredwg.get_first_owned_entity(blockHeader);
420
+ let steps = 0;
421
+ while (current && steps++ <= entityIndices.length) {
422
+ const index = indexByEntityPtr.get(current);
423
+ if (index === undefined || claimed.has(index))
424
+ break;
425
+ claimed.add(index);
426
+ current = libredwg.get_next_owned_entity(blockHeader, current);
427
+ }
428
+ }
429
+ const orphansByBlock = new Map();
430
+ const ownerless = [];
431
+ for (const index of entityIndices) {
432
+ if (claimed.has(index))
433
+ continue;
434
+ const obj = libredwg.dwg_get_object(data, index);
435
+ const etio = obj ? libredwg.dwg_object_to_entity_tio(obj) : 0;
436
+ // Resolves ownerhandle, then entmode → MSPACE/PSPACE (LibreDWG).
437
+ const ownerTio = etio ? libredwg.dwg_entity_owner(etio) : 0;
438
+ const owner = ownerTio ? (blockObjByTio.get(ownerTio) ?? 0) : 0;
439
+ if (owner) {
440
+ const owned = orphansByBlock.get(owner);
441
+ if (owned)
442
+ owned.push(index);
443
+ else
444
+ orphansByBlock.set(owner, [index]);
445
+ }
446
+ else {
447
+ ownerless.push(index);
448
+ }
449
+ }
450
+ this.orphanEntitiesByBlock = orphansByBlock;
451
+ this.ownerlessOrphanIndices = ownerless;
452
+ }
453
+ convertDimStyle(item, obj) {
454
+ const libredwg = this.libredwg;
455
+ const commonAttrs = this.getCommonTableEntryAttrs(item, obj);
456
+ const DIMTOL = libredwg.dwg_dynapi_entity_data(item, 'DIMTOL');
457
+ const DIMLIM = libredwg.dwg_dynapi_entity_data(item, 'DIMLIM');
458
+ const DIMTIH = libredwg.dwg_dynapi_entity_data(item, 'DIMTIH');
459
+ const DIMTOH = libredwg.dwg_dynapi_entity_data(item, 'DIMTOH');
460
+ const DIMSE1 = libredwg.dwg_dynapi_entity_data(item, 'DIMSE1');
461
+ const DIMSE2 = libredwg.dwg_dynapi_entity_data(item, 'DIMSE2');
462
+ const DIMALT = libredwg.dwg_dynapi_entity_data(item, 'DIMALT');
463
+ const DIMTOFL = libredwg.dwg_dynapi_entity_data(item, 'DIMTOFL');
464
+ const DIMSAH = libredwg.dwg_dynapi_entity_data(item, 'DIMSAH');
465
+ const DIMTIX = libredwg.dwg_dynapi_entity_data(item, 'DIMTIX');
466
+ const DIMSOXD = libredwg.dwg_dynapi_entity_data(item, 'DIMSOXD');
467
+ const DIMALTD = libredwg.dwg_dynapi_entity_data(item, 'DIMALTD');
468
+ const DIMZIN = libredwg.dwg_dynapi_entity_data(item, 'DIMZIN');
469
+ const DIMSD1 = libredwg.dwg_dynapi_entity_data(item, 'DIMSD1');
470
+ const DIMSD2 = libredwg.dwg_dynapi_entity_data(item, 'DIMSD2');
471
+ const DIMTOLJ = libredwg.dwg_dynapi_entity_data(item, 'DIMTOLJ');
472
+ const DIMJUST = libredwg.dwg_dynapi_entity_data(item, 'DIMJUST');
473
+ const DIMFIT = libredwg.dwg_dynapi_entity_data(item, 'DIMFIT');
474
+ const DIMUPT = libredwg.dwg_dynapi_entity_data(item, 'DIMUPT');
475
+ const DIMTZIN = libredwg.dwg_dynapi_entity_data(item, 'DIMTZIN');
476
+ const DIMALTZ = libredwg.dwg_dynapi_entity_data(item, 'DIMALTZ');
477
+ const DIMALTTZ = libredwg.dwg_dynapi_entity_data(item, 'DIMALTTZ');
478
+ const DIMTAD = libredwg.dwg_dynapi_entity_data(item, 'DIMTAD');
479
+ const DIMUNIT = libredwg.dwg_dynapi_entity_data(item, 'DIMUNIT');
480
+ const DIMAUNIT = libredwg.dwg_dynapi_entity_data(item, 'DIMAUNIT');
481
+ const DIMDEC = libredwg.dwg_dynapi_entity_data(item, 'DIMDEC');
482
+ const DIMTDEC = libredwg.dwg_dynapi_entity_data(item, 'DIMTDEC');
483
+ const DIMALTU = libredwg.dwg_dynapi_entity_data(item, 'DIMALTU');
484
+ const DIMALTTD = libredwg.dwg_dynapi_entity_data(item, 'DIMALTTD');
485
+ const DIMSCALE = libredwg.dwg_dynapi_entity_data(item, 'DIMSCALE');
486
+ const DIMASZ = libredwg.dwg_dynapi_entity_data(item, 'DIMASZ');
487
+ const DIMEXO = libredwg.dwg_dynapi_entity_data(item, 'DIMEXO');
488
+ const DIMDLI = libredwg.dwg_dynapi_entity_data(item, 'DIMDLI');
489
+ const DIMEXE = libredwg.dwg_dynapi_entity_data(item, 'DIMEXE');
490
+ const DIMRND = libredwg.dwg_dynapi_entity_data(item, 'DIMRND');
491
+ const DIMDLE = libredwg.dwg_dynapi_entity_data(item, 'DIMDLE');
492
+ const DIMTP = libredwg.dwg_dynapi_entity_data(item, 'DIMTP');
493
+ const DIMTM = libredwg.dwg_dynapi_entity_data(item, 'DIMTM');
494
+ const DIMFXL = libredwg.dwg_dynapi_entity_data(item, 'DIMFXL');
495
+ const DIMJOGANG = libredwg.dwg_dynapi_entity_data(item, 'DIMJOGANG');
496
+ const DIMTFILL = libredwg.dwg_dynapi_entity_data(item, 'DIMTFILL');
497
+ const DIMTFILLCLR = libredwg.dwg_dynapi_entity_data(item, 'DIMTFILLCLR');
498
+ const DIMAZIN = libredwg.dwg_dynapi_entity_data(item, 'DIMAZIN');
499
+ const DIMARCSYM = libredwg.dwg_dynapi_entity_data(item, 'DIMARCSYM');
500
+ const DIMTXT = libredwg.dwg_dynapi_entity_data(item, 'DIMTXT');
501
+ const DIMCEN = libredwg.dwg_dynapi_entity_data(item, 'DIMCEN');
502
+ const DIMTSZ = libredwg.dwg_dynapi_entity_data(item, 'DIMTSZ');
503
+ const DIMALTF = libredwg.dwg_dynapi_entity_data(item, 'DIMALTF');
504
+ const DIMLFAC = libredwg.dwg_dynapi_entity_data(item, 'DIMLFAC');
505
+ const DIMTVP = libredwg.dwg_dynapi_entity_data(item, 'DIMTVP');
506
+ const DIMTFAC = libredwg.dwg_dynapi_entity_data(item, 'DIMTFAC');
507
+ const DIMGAP = libredwg.dwg_dynapi_entity_data(item, 'DIMGAP');
508
+ const DIMPOST = libredwg.dwg_dynapi_entity_data(item, 'DIMPOST');
509
+ const DIMAPOST = libredwg.dwg_dynapi_entity_data(item, 'DIMAPOST');
510
+ const DIMBLK_T = libredwg.dwg_dynapi_entity_data(item, 'DIMBLK_T');
511
+ const DIMBLK1_T = libredwg.dwg_dynapi_entity_data(item, 'DIMBLK1_T');
512
+ const DIMBLK2_T = libredwg.dwg_dynapi_entity_data(item, 'DIMBLK2_T');
513
+ const DIMALTRND = libredwg.dwg_dynapi_entity_data(item, 'DIMALTRND');
514
+ const DIMCLRD_N = libredwg.dwg_dynapi_entity_data(item, 'DIMCLRD_N');
515
+ const DIMCLRE_N = libredwg.dwg_dynapi_entity_data(item, 'DIMCLRE_N');
516
+ const DIMCLRT_N = libredwg.dwg_dynapi_entity_data(item, 'DIMCLRT_N');
517
+ const DIMCLRD = libredwg.dwg_dynapi_entity_data(item, 'DIMCLRD');
518
+ const DIMCLRE = libredwg.dwg_dynapi_entity_data(item, 'DIMCLRE');
519
+ const DIMCLRT = libredwg.dwg_dynapi_entity_data(item, 'DIMCLRT');
520
+ const DIMADEC = libredwg.dwg_dynapi_entity_data(item, 'DIMADEC');
521
+ const DIMFRAC = libredwg.dwg_dynapi_entity_data(item, 'DIMFRAC');
522
+ const DIMLUNIT = libredwg.dwg_dynapi_entity_data(item, 'DIMLUNIT');
523
+ const DIMDSEP = libredwg.dwg_dynapi_entity_data(item, 'DIMDSEP');
524
+ const DIMTMOVE = libredwg.dwg_dynapi_entity_data(item, 'DIMTMOVE');
525
+ const DIMATFIT = libredwg.dwg_dynapi_entity_data(item, 'DIMATFIT');
526
+ const DIMFXLON = libredwg.dwg_dynapi_entity_data(item, 'DIMFXLON');
527
+ const DIMTXTDIRECTION = libredwg.dwg_dynapi_entity_data(item, 'DIMTXTDIRECTION');
528
+ const DIMALTMZF = libredwg.dwg_dynapi_entity_data(item, 'DIMALTMZF');
529
+ const DIMALTMZS = libredwg.dwg_dynapi_entity_data(item, 'DIMALTMZS');
530
+ const DIMMZF = libredwg.dwg_dynapi_entity_data(item, 'DIMMZF');
531
+ const DIMMZS = libredwg.dwg_dynapi_entity_data(item, 'DIMMZS');
532
+ const DIMLWD = libredwg.dwg_dynapi_entity_data(item, 'DIMLWD');
533
+ const DIMLWE = libredwg.dwg_dynapi_entity_data(item, 'DIMLWE');
534
+ const DIMTXSTY_Ptr = libredwg.dwg_dynapi_entity_data(item, 'DIMTXSTY');
535
+ const DIMTXSTY = libredwg.dwg_ref_get_absref(DIMTXSTY_Ptr) ?? undefined;
536
+ const DIMLDRBLK_Ptr = libredwg.dwg_dynapi_entity_data(item, 'DIMLDRBLK');
537
+ const DIMLDRBLK = libredwg.dwg_ref_get_absref(DIMLDRBLK_Ptr) ?? undefined;
538
+ return {
539
+ ...commonAttrs,
540
+ DIMPOST: DIMPOST,
541
+ DIMAPOST: DIMAPOST,
542
+ DIMBLK: DIMBLK_T,
543
+ DIMBLK1: DIMBLK1_T,
544
+ DIMBLK2: DIMBLK2_T,
545
+ DIMSCALE: DIMSCALE,
546
+ DIMASZ: DIMASZ,
547
+ DIMEXO: DIMEXO,
548
+ DIMDLI: DIMDLI,
549
+ DIMEXE: DIMEXE,
550
+ DIMRND: DIMRND,
551
+ DIMDLE: DIMDLE,
552
+ DIMTP: DIMTP,
553
+ DIMTM: DIMTM,
554
+ DIMTXT: DIMTXT,
555
+ DIMCEN: DIMCEN,
556
+ DIMTSZ: DIMTSZ,
557
+ DIMALTF: DIMALTF,
558
+ DIMLFAC: DIMLFAC,
559
+ DIMTVP: DIMTVP,
560
+ DIMTFAC: DIMTFAC,
561
+ DIMGAP: DIMGAP,
562
+ DIMALTRND: DIMALTRND,
563
+ DIMTOL: DIMTOL,
564
+ DIMLIM: DIMLIM,
565
+ DIMTIH: DIMTIH,
566
+ DIMTOH: DIMTOH,
567
+ DIMSE1: DIMSE1,
568
+ DIMSE2: DIMSE2,
569
+ DIMTAD: DIMTAD,
570
+ DIMZIN: DIMZIN,
571
+ DIMAZIN: DIMAZIN,
572
+ DIMALT: DIMALT,
573
+ DIMALTD: DIMALTD,
574
+ DIMTOFL: DIMTOFL,
575
+ DIMSAH: DIMSAH,
576
+ DIMTIX: DIMTIX,
577
+ DIMSOXD: DIMSOXD,
578
+ DIMCLRD: DIMCLRD,
579
+ DIMCLRE: DIMCLRE,
580
+ DIMCLRT: DIMCLRT,
581
+ DIMADEC: DIMADEC,
582
+ DIMUNIT: DIMUNIT,
583
+ DIMDEC: DIMDEC,
584
+ DIMTDEC: DIMTDEC,
585
+ DIMALTU: DIMALTU,
586
+ DIMALTTD: DIMALTTD,
587
+ DIMAUNIT: DIMAUNIT,
588
+ DIMFRAC: DIMFRAC,
589
+ DIMLUNIT: DIMLUNIT,
590
+ DIMDSEP: String.fromCharCode(DIMDSEP),
591
+ DIMTMOVE: DIMTMOVE,
592
+ DIMJUST: DIMJUST,
593
+ DIMSD1: DIMSD1,
594
+ DIMSD2: DIMSD2,
595
+ DIMTOLJ: DIMTOLJ,
596
+ DIMTZIN: DIMTZIN,
597
+ DIMALTZ: DIMALTZ,
598
+ DIMALTTZ: DIMALTTZ,
599
+ DIMFIT: DIMFIT,
600
+ DIMUPT: DIMUPT,
601
+ DIMATFIT: DIMATFIT,
602
+ DIMTXSTY: DIMTXSTY,
603
+ DIMLDRBLK: DIMLDRBLK,
604
+ DIMLWD: DIMLWD,
605
+ DIMLWE: DIMLWE,
606
+ DIMFXL: DIMFXL,
607
+ DIMJOGANG: DIMJOGANG,
608
+ DIMTFILL: DIMTFILL,
609
+ DIMTFILLCLR: DIMTFILLCLR,
610
+ DIMARCSYM: DIMARCSYM,
611
+ DIMCLRD_N: DIMCLRD_N,
612
+ DIMCLRE_N: DIMCLRE_N,
613
+ DIMCLRT_N: DIMCLRT_N,
614
+ DIMFXLON: DIMFXLON,
615
+ DIMTXTDIRECTION: DIMTXTDIRECTION,
616
+ DIMALTMZF: DIMALTMZF,
617
+ DIMALTMZS: DIMALTMZS,
618
+ DIMMZF: DIMMZF,
619
+ DIMMZS: DIMMZS
620
+ };
621
+ }
622
+ convertLayer(item, obj) {
623
+ const libredwg = this.libredwg;
624
+ const commonAttrs = this.getCommonTableEntryAttrs(item, obj);
625
+ const flag = libredwg.dwg_dynapi_entity_data(item, 'flag');
626
+ const frozen = libredwg.dwg_dynapi_entity_data(item, 'frozen');
627
+ const off = libredwg.dwg_dynapi_entity_data(item, 'off');
628
+ const frozenInNew = libredwg.dwg_dynapi_entity_data(item, 'frozen_in_new');
629
+ const locked = libredwg.dwg_dynapi_entity_data(item, 'plotflockedlag');
630
+ const plotFlag = libredwg.dwg_dynapi_entity_data(item, 'plotflag');
631
+ const linewt = libredwg.dwg_dynapi_entity_data(item, 'linewt');
632
+ const color = libredwg.dwg_dynapi_entity_data(item, 'color');
633
+ const ltypeRef = libredwg.dwg_dynapi_entity_data(item, 'ltype');
634
+ let ltypeName = 'Continuous';
635
+ if (ltypeRef) {
636
+ try {
637
+ ltypeName = libredwg.dwg_ref_get_object_name(ltypeRef);
638
+ }
639
+ catch {
640
+ // ref may be invalid in some DWG files
641
+ }
642
+ }
643
+ // - 0xc0 for ByLayer (also c3 and rgb of 0x100)
644
+ // - 0xc1 for ByBlock (also c3 and rgb of 0)
645
+ // - 0xc2 for entities (default), with names with an additional name flag RC
646
+ // - 0xc3 for truecolor
647
+ // - 0xc5 for foreground color
648
+ // - 0xc8 for none (also c3 and rgb of 0x101)
649
+ const method = color.method;
650
+ let colorIndex = 256;
651
+ let rgbColor = 0xffffff;
652
+ // NOTE: Some older DWG formats use method=0x0 and provide the ACI index via color.index.
653
+ if (method === 0xc3 || ((color.rgb >>> 24) & 0xff) === 0xc3) {
654
+ colorIndex = color.rgb & 0x000000ff;
655
+ }
656
+ else if (method == 0xc2 || ((color.rgb >>> 24) & 0xff) === 0xc2) {
657
+ rgbColor = color.rgb & 0x00ffffff;
658
+ }
659
+ else if (color.index >= 1 && color.index <= 255) {
660
+ // Older DWG format: ACI index is directly available in color.index.
661
+ colorIndex = color.index;
662
+ }
663
+ return {
664
+ ...commonAttrs,
665
+ standardFlag: flag,
666
+ colorIndex: colorIndex,
667
+ color: rgbColor,
668
+ colorName: color.name,
669
+ transparency: color.alpha,
670
+ lineType: ltypeName,
671
+ frozen: frozen != 0,
672
+ off: off != 0,
673
+ frozenInNew: frozenInNew != 0,
674
+ locked: locked != 0,
675
+ plotFlag: plotFlag,
676
+ lineweight: linewt,
677
+ plotStyleNameObjectId: '',
678
+ materialObjectId: ''
679
+ };
680
+ }
681
+ convertLineType(item, obj) {
682
+ const libredwg = this.libredwg;
683
+ const commonAttrs = this.getCommonTableEntryAttrs(item, obj);
684
+ const flag = libredwg.dwg_dynapi_entity_data(item, 'flag');
685
+ const description = libredwg.dwg_dynapi_entity_data(item, 'description');
686
+ const numDashes = libredwg.dwg_dynapi_entity_data(item, 'numdashes');
687
+ const patternLen = libredwg.dwg_dynapi_entity_data(item, 'pattern_len');
688
+ const dashes = libredwg.dwg_dynapi_entity_data(item, 'dashes');
689
+ const dashArray = dashes
690
+ ? libredwg.dwg_ptr_to_ltype_dash_array(dashes, numDashes)
691
+ : [];
692
+ return {
693
+ ...commonAttrs,
694
+ description: description,
695
+ standardFlag: flag,
696
+ numberOfLineTypes: numDashes,
697
+ totalPatternLength: patternLen,
698
+ pattern: this.convertLineTypePattern(dashArray)
699
+ };
700
+ }
701
+ convertLineTypePattern(dashes) {
702
+ const patterns = [];
703
+ dashes.forEach(dash => {
704
+ patterns.push({
705
+ elementLength: dash.length || 0,
706
+ // DXF 74: 1=absolute rotation, 2=text, 4=shape
707
+ elementTypeFlag: dash.shape_flag || 0,
708
+ // DXF 75: shape number (when flag has bit 4)
709
+ shapeNumber: dash.complex_shapecode,
710
+ // TODO: convert style handle to style object id
711
+ // styleObjectId: dash.style,
712
+ scale: dash.scale,
713
+ rotation: dash.rotation,
714
+ offsetX: dash.x_offset,
715
+ offsetY: dash.y_offset,
716
+ text: dash.text
717
+ });
718
+ });
719
+ return patterns;
720
+ }
721
+ convertStyle(item, obj) {
722
+ const libredwg = this.libredwg;
723
+ const commonAttrs = this.getCommonTableEntryAttrs(item, obj);
724
+ const standardFlag = libredwg.dwg_dynapi_entity_data(item, 'flag');
725
+ const widthFactor = libredwg.dwg_dynapi_entity_data(item, 'width_factor');
726
+ const obliqueAngle = libredwg.dwg_dynapi_entity_data(item, 'oblique_angle');
727
+ const textGenerationFlag = libredwg.dwg_dynapi_entity_data(item, 'generation');
728
+ const lastHeight = libredwg.dwg_dynapi_entity_data(item, 'last_height');
729
+ const font = libredwg.dwg_dynapi_entity_data(item, 'font_file');
730
+ const bigFont = libredwg.dwg_dynapi_entity_data(item, 'bigfont_file');
731
+ return {
732
+ ...commonAttrs,
733
+ standardFlag: standardFlag,
734
+ fixedTextHeight: 0, // TODO: Set the correct value
735
+ widthFactor: widthFactor,
736
+ obliqueAngle: obliqueAngle,
737
+ textGenerationFlag: textGenerationFlag,
738
+ lastHeight: lastHeight,
739
+ font: font,
740
+ bigFont: bigFont
741
+ };
742
+ }
743
+ convertViewport(item, obj) {
744
+ const libredwg = this.libredwg;
745
+ const commonAttrs = this.getCommonTableEntryAttrs(item, obj);
746
+ const standardFlag = libredwg.dwg_dynapi_entity_data(item, 'flag');
747
+ const viewHeight = libredwg.dwg_dynapi_entity_data(item, 'VIEWSIZE');
748
+ const aspectRatio = libredwg.dwg_dynapi_entity_data(item, 'aspect_ratio');
749
+ const center = libredwg.dwg_dynapi_entity_data(item, 'VIEWCTR');
750
+ const viewTarget = libredwg.dwg_dynapi_entity_data(item, 'view_target');
751
+ const viewDirectionFromTarget = libredwg.dwg_dynapi_entity_data(item, 'VIEWDIR');
752
+ const viewTwistAngle = libredwg.dwg_dynapi_entity_data(item, 'view_twist');
753
+ const lensLength = libredwg.dwg_dynapi_entity_data(item, 'lens_length');
754
+ const frontClippingPlane = libredwg.dwg_dynapi_entity_data(item, 'front_clip_z');
755
+ const backClippingPlane = libredwg.dwg_dynapi_entity_data(item, 'back_clip_z');
756
+ const viewMode = libredwg.dwg_dynapi_entity_data(item, 'VIEWMODE');
757
+ const renderMode = libredwg.dwg_dynapi_entity_data(item, 'render_mode');
758
+ const isDefaultLightingOn = libredwg.dwg_dynapi_entity_data(item, 'use_default_lights') != 0;
759
+ const defaultLightningType = libredwg.dwg_dynapi_entity_data(item, 'default_lightning_type');
760
+ const brightness = libredwg.dwg_dynapi_entity_data(item, 'brightness');
761
+ const contrast = libredwg.dwg_dynapi_entity_data(item, 'contrast');
762
+ const ambient_color = libredwg.dwg_dynapi_entity_data(item, 'ambient_color');
763
+ // ViewportTableRecord
764
+ const lowerLeftCorner = libredwg.dwg_dynapi_entity_data(item, 'lower_left');
765
+ const upperRightCorner = libredwg.dwg_dynapi_entity_data(item, 'upper_right');
766
+ // TODO: Not sure whether 'circleSides' is equal to 'circle_zoom'
767
+ const circleSides = libredwg.dwg_dynapi_entity_data(item, 'circle_zoom');
768
+ const ucsIconSetting = libredwg.dwg_dynapi_entity_data(item, 'UCSICON');
769
+ // TODO: Not sure whether 'gridSpacing' is equal to 'GRIDUNIT'
770
+ const gridSpacing = libredwg.dwg_dynapi_entity_data(item, 'GRIDUNIT');
771
+ const snapRotationAngle = libredwg.dwg_dynapi_entity_data(item, 'SNAPANG');
772
+ const snapBasePoint = libredwg.dwg_dynapi_entity_data(item, 'SNAPBASE');
773
+ // TODO: Not sure whether 'snapSpacing' is equal to 'SNAPUNIT'
774
+ const snapSpacing = libredwg.dwg_dynapi_entity_data(item, 'SNAPUNIT');
775
+ const ucsOrigin = libredwg.dwg_dynapi_entity_data(item, 'ucsorg');
776
+ const ucsXAxis = libredwg.dwg_dynapi_entity_data(item, 'ucsxdir');
777
+ const ucsYAxis = libredwg.dwg_dynapi_entity_data(item, 'ucsydir');
778
+ const elevation = libredwg.dwg_dynapi_entity_data(item, 'ucs_elevation');
779
+ const majorGridLines = libredwg.dwg_dynapi_entity_data(item, 'grid_major');
780
+ const background = libredwg.dwg_dynapi_entity_data(item, 'background');
781
+ const backgroundObjectId = background
782
+ ? (libredwg.dwg_ref_get_id(background) ?? '')
783
+ : undefined;
784
+ const visualstyle = libredwg.dwg_dynapi_entity_data(item, 'visualstyle');
785
+ const visualStyleObjectId = visualstyle
786
+ ? (libredwg.dwg_ref_get_id(visualstyle) ?? '')
787
+ : undefined;
788
+ // BITCODE_B UCSFOLLOW;
789
+ // BITCODE_B FASTZOOM;
790
+ // BITCODE_B GRIDMODE; /* DXF 76: on or off */
791
+ // BITCODE_B SNAPMODE; /* DXF 75: on or off */
792
+ // BITCODE_B SNAPSTYLE;
793
+ // BITCODE_BS SNAPISOPAIR;
794
+ // BITCODE_B ucs_at_origin;
795
+ // BITCODE_B UCSVP;
796
+ // BITCODE_BS UCSORTHOVIEW;
797
+ // BITCODE_BS grid_flags; /* bit 1: bound to limits, bit 2: adaptive */
798
+ // BITCODE_H sun;
799
+ // BITCODE_H named_ucs;
800
+ // BITCODE_H base_ucs;
801
+ return {
802
+ ...commonAttrs,
803
+ standardFlag: standardFlag,
804
+ lowerLeftCorner: lowerLeftCorner,
805
+ upperRightCorner: upperRightCorner,
806
+ center: center,
807
+ snapBasePoint: snapBasePoint,
808
+ snapSpacing: snapSpacing,
809
+ gridSpacing: gridSpacing,
810
+ viewDirectionFromTarget: viewDirectionFromTarget,
811
+ viewTarget: viewTarget,
812
+ lensLength: lensLength,
813
+ frontClippingPlane: frontClippingPlane,
814
+ backClippingPlane: backClippingPlane,
815
+ viewHeight: viewHeight,
816
+ aspectRatio: aspectRatio,
817
+ snapRotationAngle: snapRotationAngle,
818
+ viewTwistAngle: viewTwistAngle,
819
+ circleSides: circleSides,
820
+ frozenLayers: [], // TODO: Set the correct value
821
+ styleSheet: '', // TODO: Set the correct value
822
+ renderMode: renderMode,
823
+ viewMode: viewMode,
824
+ ucsIconSetting: ucsIconSetting,
825
+ ucsOrigin: ucsOrigin,
826
+ ucsXAxis: ucsXAxis,
827
+ ucsYAxis: ucsYAxis,
828
+ orthographicType: 0, // TODO: Set the correct value
829
+ elevation: elevation,
830
+ shadePlotSetting: 0, // TODO: Set the correct value
831
+ majorGridLines: majorGridLines,
832
+ backgroundObjectId: backgroundObjectId,
833
+ // shadePlotObjectId: undefined,
834
+ visualStyleObjectId: visualStyleObjectId,
835
+ isDefaultLightingOn: isDefaultLightingOn,
836
+ defaultLightingType: defaultLightningType,
837
+ brightness: brightness,
838
+ contrast: contrast,
839
+ // TODO: Not sure whether 'index' or 'rgb' should be used
840
+ ambientColor: ambient_color.index
841
+ };
842
+ }
843
+ getCommonTableEntryAttrs(tio, obj) {
844
+ const libredwg = this.libredwg;
845
+ const object_tio = libredwg.dwg_object_get_tio(obj);
846
+ const ownerhandle = libredwg.dwg_object_object_get_ownerhandle_object(object_tio);
847
+ const handle = libredwg.dwg_object_get_handle_object(obj);
848
+ return {
849
+ handle: idToString(handle.value),
850
+ ownerHandle: idToString(ownerhandle.absolute_ref),
851
+ name: libredwg.dwg_dynapi_entity_data(tio, 'name')
852
+ };
853
+ }
854
+ convertDictionary(item, obj) {
855
+ const libredwg = this.libredwg;
856
+ const commonAttrs = this.getCommonObjectAttrs(obj);
857
+ const isHardOwner = libredwg.dwg_dynapi_entity_data(item, 'is_hardowner');
858
+ const cloningFlag = libredwg.dwg_dynapi_entity_data(item, 'cloning');
859
+ const numitems = libredwg.dwg_dynapi_entity_data(item, 'numitems');
860
+ const itemhandles_ptr = libredwg.dwg_dynapi_entity_data(item, 'itemhandles');
861
+ const itemhandles = libredwg.dwg_ptr_to_object_ref_array(itemhandles_ptr, numitems);
862
+ const texts = libredwg.dwg_object_dictionary_get_texts(obj);
863
+ const entries = {};
864
+ itemhandles.forEach((handle, index) => (entries[texts[index]] = idToString(handle.absolute_ref)));
865
+ return {
866
+ ...commonAttrs,
867
+ isHardOwner: !!isHardOwner,
868
+ cloningFlag: cloningFlag,
869
+ entries: entries
870
+ };
871
+ }
872
+ convertImageDef(item, obj) {
873
+ const libredwg = this.libredwg;
874
+ const commonAttrs = this.getCommonObjectAttrs(obj);
875
+ // const classVersion = libredwg.dwg_dynapi_entity_data<number>(item, 'class_version')
876
+ const size = libredwg.dwg_dynapi_entity_data(item, 'image_size');
877
+ const fileName = libredwg.dwg_dynapi_entity_data(item, 'file_path');
878
+ const isLoaded = libredwg.dwg_dynapi_entity_data(item, 'is_loaded');
879
+ const sizeOfOnePixel = libredwg.dwg_dynapi_entity_data(item, 'pixel_size');
880
+ const resolutionUnits = libredwg.dwg_dynapi_entity_data(item, 'resunits');
881
+ return {
882
+ ...commonAttrs,
883
+ fileName: fileName,
884
+ size: size,
885
+ sizeOfOnePixel: sizeOfOnePixel,
886
+ isLoaded: isLoaded,
887
+ resolutionUnits: resolutionUnits
888
+ };
889
+ }
890
+ convertLayerFilter(item, obj) {
891
+ const libredwg = this.libredwg;
892
+ const commonAttrs = this.getCommonObjectAttrs(obj);
893
+ const numNames = libredwg.dwg_dynapi_entity_data(item, 'num_names') ?? 0;
894
+ const namesPtr = libredwg.dwg_dynapi_entity_data(item, 'names');
895
+ const layerNames = namesPtr && numNames > 0
896
+ ? libredwg
897
+ .dwg_ptr_to_wchar_string_array(namesPtr, numNames)
898
+ .filter((name) => name != null && name !== '')
899
+ : [];
900
+ return {
901
+ ...commonAttrs,
902
+ ...(layerNames.length ? { layerNames } : {})
903
+ };
904
+ }
905
+ convertLayerIndex(item, obj) {
906
+ const libredwg = this.libredwg;
907
+ const commonAttrs = this.getCommonObjectAttrs(obj);
908
+ const timeStamp = libredwg.dwg_dynapi_entity_data(item, 'last_updated');
909
+ const numEntries = libredwg.dwg_dynapi_entity_data(item, 'num_entries') ?? 0;
910
+ const entriesPtr = libredwg.dwg_dynapi_entity_data(item, 'entries');
911
+ const entrySize = libredwg.dwg_dynapi_subclass_size('LAYER_entry');
912
+ const layerNames = [];
913
+ const idBufferIds = [];
914
+ const idBufferEntryCounts = [];
915
+ if (entriesPtr && entrySize > 0) {
916
+ for (let i = 0; i < numEntries; i++) {
917
+ const entryPtr = entriesPtr + i * entrySize;
918
+ const name = libredwg.dwg_dynapi_subclass_data(entryPtr, 'LAYER_entry', 'name');
919
+ // Keep parallel arrays aligned with entries even when name is absent.
920
+ layerNames.push(name ?? '');
921
+ const handleRef = libredwg.dwg_dynapi_subclass_data(entryPtr, 'LAYER_entry', 'handle');
922
+ idBufferIds.push(handleRef ? (libredwg.dwg_ref_get_id(handleRef) ?? '') : '');
923
+ idBufferEntryCounts.push(libredwg.dwg_dynapi_subclass_data(entryPtr, 'LAYER_entry', 'numlayers') ?? 0);
924
+ }
925
+ }
926
+ return {
927
+ ...commonAttrs,
928
+ ...(timeStamp != null ? { timeStamp } : {}),
929
+ ...(layerNames.length ? { layerNames } : {}),
930
+ ...(idBufferIds.length ? { idBufferIds } : {}),
931
+ ...(idBufferEntryCounts.length ? { idBufferEntryCounts } : {})
932
+ };
933
+ }
934
+ convertLayout(item, obj) {
935
+ const libredwg = this.libredwg;
936
+ const commonAttrs = this.getCommonObjectAttrs(obj);
937
+ // AcDbLayout
938
+ const layoutName = libredwg.dwg_dynapi_entity_data(item, 'layout_name');
939
+ const tabOrder = libredwg.dwg_dynapi_entity_data(item, 'tab_order');
940
+ const controlFlag = libredwg.dwg_dynapi_entity_data(item, 'layout_flags');
941
+ const insertionPoint = libredwg.dwg_dynapi_entity_data(item, 'INSBASE');
942
+ const minLimit = libredwg.dwg_dynapi_entity_data(item, 'LIMMIN');
943
+ const maxLimit = libredwg.dwg_dynapi_entity_data(item, 'LIMMAX');
944
+ const ucsOrigin = libredwg.dwg_dynapi_entity_data(item, 'UCSORG');
945
+ const ucsXAxis = libredwg.dwg_dynapi_entity_data(item, 'UCSXDIR');
946
+ const ucsYAxis = libredwg.dwg_dynapi_entity_data(item, 'UCSYDIR');
947
+ const orthographicType = libredwg.dwg_dynapi_entity_data(item, 'UCSORTHOVIEW');
948
+ const minExtent = libredwg.dwg_dynapi_entity_data(item, 'EXTMIN');
949
+ const maxExtent = libredwg.dwg_dynapi_entity_data(item, 'EXTMAX');
950
+ const elevation = libredwg.dwg_dynapi_entity_data(item, 'ucs_elevation');
951
+ const block_header_ref = libredwg.dwg_dynapi_entity_data(item, 'block_header');
952
+ const paperSpaceTableId = (libredwg.dwg_ref_get_id(block_header_ref) ?? '');
953
+ const active_viewport_ref = libredwg.dwg_dynapi_entity_data(item, 'active_viewport');
954
+ const viewportId = (libredwg.dwg_ref_get_id(active_viewport_ref) ?? '');
955
+ const named_ucs_ref = libredwg.dwg_dynapi_entity_data(item, 'named_ucs');
956
+ const namedUcsId = named_ucs_ref
957
+ ? (libredwg.dwg_ref_get_id(named_ucs_ref) ?? '')
958
+ : undefined;
959
+ // BITCODE_H base_ucs;
960
+ // BITCODE_BL num_viewports; // r2004+
961
+ // BITCODE_H *viewports; // r2004+
962
+ return {
963
+ ...commonAttrs,
964
+ layoutName: layoutName,
965
+ controlFlag: controlFlag,
966
+ tabOrder: tabOrder,
967
+ minLimit: minLimit,
968
+ maxLimit: maxLimit,
969
+ insertionPoint: insertionPoint,
970
+ minExtent: minExtent,
971
+ maxExtent: maxExtent,
972
+ elevation: elevation,
973
+ ucsOrigin: ucsOrigin,
974
+ ucsXAxis: ucsXAxis,
975
+ ucsYAxis: ucsYAxis,
976
+ orthographicType: orthographicType,
977
+ paperSpaceTableId: paperSpaceTableId,
978
+ viewportId: viewportId,
979
+ namedUcsId: namedUcsId,
980
+ // orthographicUcsId?: string;
981
+ shadePlotId: '' // TODO: Set the correct value
982
+ };
983
+ }
984
+ convertMLeaderStyle(item, obj) {
985
+ const libredwg = this.libredwg;
986
+ const commonAttrs = this.getCommonObjectAttrs(obj);
987
+ const objectVal = (field) => libredwg.dwg_dynapi_entity_data(item, field);
988
+ const refToId = (ref) => libredwg.dwg_ref_get_id(ref);
989
+ const asBool = (value) => value > 0;
990
+ const mleaderColor = (color) => color != null ? dwgColorToMLeaderRawColor(color) : undefined;
991
+ return {
992
+ ...commonAttrs,
993
+ subclassMarker: 'AcDbMLeaderStyle',
994
+ unknown1: objectVal('class_version'),
995
+ contentType: objectVal('content_type'),
996
+ drawMLeaderOrderType: objectVal('mleader_order'),
997
+ drawLeaderOrderType: objectVal('leader_order'),
998
+ maxLeaderSegmentPoints: objectVal('max_points'),
999
+ firstSegmentAngleConstraint: objectVal('first_seg_angle'),
1000
+ secondSegmentAngleConstraint: objectVal('second_seg_angle'),
1001
+ leaderLineType: objectVal('type'),
1002
+ leaderLineColor: mleaderColor(objectVal('line_color')),
1003
+ leaderLineTypeId: refToId(objectVal('line_type')),
1004
+ leaderLineWeight: objectVal('linewt'),
1005
+ landingEnabled: asBool(objectVal('has_landing')),
1006
+ landingGap: objectVal('landing_gap'),
1007
+ doglegEnabled: asBool(objectVal('has_dogleg')),
1008
+ doglegLength: objectVal('landing_dist'),
1009
+ description: objectVal('description'),
1010
+ arrowheadId: refToId(objectVal('arrow_head')),
1011
+ arrowheadSize: objectVal('arrow_head_size'),
1012
+ defaultMTextContents: objectVal('text_default'),
1013
+ textStyleId: refToId(objectVal('text_style')),
1014
+ textLeftAttachmentType: objectVal('attach_left'),
1015
+ textAngleType: objectVal('text_angle_type'),
1016
+ textAlignmentType: objectVal('text_align_type'),
1017
+ textRightAttachmentType: objectVal('attach_right'),
1018
+ textColor: mleaderColor(objectVal('text_color')),
1019
+ textHeight: objectVal('text_height'),
1020
+ textFrameEnabled: asBool(objectVal('has_text_frame')),
1021
+ textAlignAlwaysLeft: asBool(objectVal('text_always_left')),
1022
+ alignSpace: objectVal('align_space'),
1023
+ blockContentId: refToId(objectVal('block')),
1024
+ blockContentColor: mleaderColor(objectVal('block_color')),
1025
+ blockContentScale: objectVal('block_scale'),
1026
+ blockContentScaleEnabled: asBool(objectVal('use_block_scale')),
1027
+ blockContentRotation: objectVal('block_rotation'),
1028
+ blockContentRotationEnabled: asBool(objectVal('use_block_rotation')),
1029
+ blockContentConnectionType: objectVal('block_connection'),
1030
+ scale: objectVal('scale'),
1031
+ overwritePropertyValue: asBool(objectVal('is_changed')),
1032
+ annotative: asBool(objectVal('is_annotative')),
1033
+ breakGapSize: objectVal('break_size'),
1034
+ textAttachmentDirection: objectVal('attach_dir'),
1035
+ bottomTextAttachmentDirection: objectVal('attach_bottom'),
1036
+ topTextAttachmentDirection: objectVal('attach_top'),
1037
+ unknown2: asBool(objectVal('text_extended'))
1038
+ };
1039
+ }
1040
+ convertSpatialFilter(item, obj) {
1041
+ const libredwg = this.libredwg;
1042
+ const commonAttrs = this.getCommonObjectAttrs(obj);
1043
+ const origin = libredwg.dwg_dynapi_entity_data(item, 'origin');
1044
+ const numberOfPointsOnClipBoundary = libredwg.dwg_dynapi_entity_data(item, 'num_clip_verts');
1045
+ const clip_verts_ptr = libredwg.dwg_dynapi_entity_data(item, 'clip_verts');
1046
+ const vertices = libredwg.dwg_ptr_to_point2d_array(clip_verts_ptr, numberOfPointsOnClipBoundary);
1047
+ const extrusionDirection = libredwg.dwg_dynapi_entity_data(item, 'extrusion');
1048
+ const clipBoundaryVisible = libredwg.dwg_dynapi_entity_data(item, 'display_boundary_on');
1049
+ const frontClippingPlaneFlag = libredwg.dwg_dynapi_entity_data(item, 'front_clip_on');
1050
+ const frontClippingPlaneDistance = libredwg.dwg_dynapi_entity_data(item, 'front_clip_z');
1051
+ const backClippingPlaneFlag = libredwg.dwg_dynapi_entity_data(item, 'back_clip_on');
1052
+ const backClippingPlaneDistance = libredwg.dwg_dynapi_entity_data(item, 'back_clip_z');
1053
+ const transform_ptr = libredwg.dwg_dynapi_entity_data(item, 'transform');
1054
+ const matrix = libredwg.dwg_ptr_to_double_array(transform_ptr, 12);
1055
+ const inverse_transform_ptr = libredwg.dwg_dynapi_entity_data(item, 'inverse_transform');
1056
+ const invertBlockMatrix = libredwg.dwg_ptr_to_double_array(inverse_transform_ptr, 12);
1057
+ return {
1058
+ ...commonAttrs,
1059
+ origin: origin,
1060
+ numberOfPointsOnClipBoundary: numberOfPointsOnClipBoundary,
1061
+ vertices: vertices,
1062
+ extrusionDirection: extrusionDirection,
1063
+ clipBoundaryVisible: !!clipBoundaryVisible,
1064
+ frontClippingPlaneFlag: !!frontClippingPlaneFlag,
1065
+ frontClippingPlaneDistance: frontClippingPlaneDistance,
1066
+ backClippingPlaneFlag: !!backClippingPlaneFlag,
1067
+ backClippingPlaneDistance: backClippingPlaneDistance,
1068
+ matrix: matrix,
1069
+ invertBlockMatrix: invertBlockMatrix
1070
+ };
1071
+ }
1072
+ convertXRecord(item, obj) {
1073
+ const libredwg = this.libredwg;
1074
+ const commonAttrs = this.getCommonObjectAttrs(obj);
1075
+ const cloning = libredwg.dwg_dynapi_entity_data(item, 'cloning');
1076
+ const data = libredwg.dwg_object_xrecord_get_xdata(item) ?? [];
1077
+ const objectObj = libredwg.dwg_object_get_tio(obj);
1078
+ const xdic = objectObj
1079
+ ? libredwg.dwg_object_object_get_xdicobjhandle_object(objectObj)
1080
+ : null;
1081
+ const extensionDictionary = xdic && xdic.absolute_ref
1082
+ ? idToString(xdic.absolute_ref)
1083
+ : undefined;
1084
+ return {
1085
+ ...commonAttrs,
1086
+ ...(cloning != null ? { cloning } : {}),
1087
+ ...(extensionDictionary ? { extensionDictionary } : {}),
1088
+ data
1089
+ };
1090
+ }
1091
+ getCommonObjectAttrs(obj) {
1092
+ const libredwg = this.libredwg;
1093
+ const object_tio = libredwg.dwg_object_get_tio(obj);
1094
+ const ownerhandle = libredwg.dwg_object_object_get_ownerhandle_object(object_tio);
1095
+ const handle = libredwg.dwg_object_get_handle_object(obj);
1096
+ return {
1097
+ handle: idToString(handle.value),
1098
+ ownerHandle: idToString(ownerhandle.absolute_ref)
1099
+ };
1100
+ }
1101
+ }
1102
+ //# sourceMappingURL=converter.js.map