Skip to content

Commit c04b13d

Browse files
committed
feat: support parsing ATTRIB (#4)
1 parent 27e72b6 commit c04b13d

9 files changed

Lines changed: 113 additions & 8 deletions

File tree

.github/FUNDING.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

bindings/javascript/embind/binding_ent.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,23 @@ emscripten::val dwg_entity_polyline_3d_get_vertices_wrapper(Dwg_Object_Ptr obj_p
327327
return result;
328328
}
329329

330+
emscripten::val dwg_entity_insert_get_attribs_wrapper(Dwg_Object_Entity_Ptr obj_ptr) {
331+
Dwg_Entity_INSERT* ent = reinterpret_cast<Dwg_Entity_INSERT*>(obj_ptr);
332+
emscripten::val result = emscripten::val::object();
333+
emscripten::val attribs = emscripten::val::array();
334+
result.set("data", attribs);
335+
336+
Dwg_Object *o;
337+
for (BITCODE_BL j = 0; j < ent->num_owned; j++) {
338+
o = ent->attribs && ent->attribs[j] ? ent->attribs[j]->obj : NULL;
339+
if (o && o->fixedtype == DWG_TYPE_ATTRIB) {
340+
attribs.call<void>("push", reinterpret_cast<uintptr_t>(o));
341+
}
342+
}
343+
344+
return result;
345+
}
346+
330347
EMSCRIPTEN_BINDINGS(libredwg_dwg_entity) {
331348
DEFINE_FUNC(dwg_entity_owner);
332349
DEFINE_FUNC(dwg_entity_polyline_2d_get_numpoints);
@@ -336,6 +353,7 @@ EMSCRIPTEN_BINDINGS(libredwg_dwg_entity) {
336353
DEFINE_FUNC(dwg_entity_polyline_3d_get_points);
337354
DEFINE_FUNC(dwg_entity_polyline_3d_get_vertices);
338355
DEFINE_FUNC(dwg_entity_block_header_get_preview);
356+
DEFINE_FUNC(dwg_entity_insert_get_attribs);
339357
}
340358

341359
emscripten::val dwg_object_dictionary_get_texts_wrapper(Dwg_Object_Ptr obj_ptr) {

bindings/javascript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "A DWG/DXF JavaScript parser based on libredwg",
44
"license": "GPL-2.0-only",
55
"private": false,
6-
"version": "0.6.5",
6+
"version": "0.6.6",
77
"author": "MLight Lee <mlight.lee@outlook.com>",
88
"type": "module",
99
"repository": {

bindings/javascript/src/converter/entityConverter.ts

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
DwgArcEntity,
77
DwgAttachmentPoint,
88
DwgAttdefEntity,
9+
DwgAttribEntity,
910
DwgBoundaryPath,
1011
DwgBoundaryPathEdge,
1112
DwgBoundaryPathEdgeType,
@@ -138,6 +139,8 @@ export class LibreEntityConverter {
138139
return this.convertArc(entity_tio, commonAttrs)
139140
} else if (fixedtype == Dwg_Object_Type.DWG_TYPE_ATTDEF) {
140141
return this.convertAttdef(entity_tio, commonAttrs)
142+
} else if (fixedtype == Dwg_Object_Type.DWG_TYPE_ATTRIB) {
143+
return this.convertAttrib(entity_tio, commonAttrs)
141144
} else if (fixedtype == Dwg_Object_Type.DWG_TYPE_CIRCLE) {
142145
return this.convertCircle(entity_tio, commonAttrs)
143146
} else if (
@@ -389,7 +392,7 @@ export class LibreEntityConverter {
389392
).data as number
390393
const isReallyLocked = libredwg.dwg_dynapi_entity_value(
391394
entity,
392-
'is_locked_in_block'
395+
'is_really_locked'
393396
).data as number
394397
// TODO: double check whether 'mtext_type' is 'mtextFlag'
395398
const mtextFlag = libredwg.dwg_dynapi_entity_value(entity, 'mtext_type')
@@ -418,6 +421,58 @@ export class LibreEntityConverter {
418421
}
419422
}
420423

424+
private convertAttrib(
425+
entity: Dwg_Object_Entity_Ptr,
426+
commonAttrs: DwgCommonAttributes
427+
): DwgAttribEntity {
428+
const libredwg = this.libredwg
429+
430+
const text = this.convertTextBase(entity)
431+
const tag = libredwg.dwg_dynapi_entity_value(entity, 'tag').data as string
432+
const flags = libredwg.dwg_dynapi_entity_value(entity, 'flags')
433+
.data as number
434+
const fieldLength = libredwg.dwg_dynapi_entity_value(entity, 'field_length')
435+
.data as number
436+
const lockPositionFlag = libredwg.dwg_dynapi_entity_value(
437+
entity,
438+
'lock_position_flag'
439+
).data as number
440+
const duplicateRecordCloningFlag = libredwg.dwg_dynapi_entity_value(
441+
entity,
442+
'keep_duplicate_records'
443+
).data as number
444+
// TODO: double check whether 'mtext_type' is 'mtextFlag'
445+
const mtextFlag = libredwg.dwg_dynapi_entity_value(entity, 'mtext_type')
446+
.data as number
447+
const isReallyLocked = libredwg.dwg_dynapi_entity_value(
448+
entity,
449+
'is_really_locked'
450+
).data as number
451+
const alignmentPoint = libredwg.dwg_dynapi_entity_value(
452+
entity,
453+
'alignment_pt'
454+
).data as DwgPoint2D
455+
456+
return {
457+
type: 'ATTRIB',
458+
...commonAttrs,
459+
text: text,
460+
tag: tag,
461+
flags: flags,
462+
fieldLength: fieldLength,
463+
lockPositionFlag: !!lockPositionFlag,
464+
duplicateRecordCloningFlag: !!duplicateRecordCloningFlag,
465+
mtextFlag: mtextFlag,
466+
isReallyLocked: !!isReallyLocked,
467+
numberOfSecondaryAttrs: 0, // TODO: libredwg doesn't support it yet.
468+
secondaryAttrsHardId: '0', // TODO: libredwg doesn't support it yet.
469+
alignmentPoint: { ...alignmentPoint, z: 0 },
470+
annotationScale: 1, // TODO: Set the correct value
471+
attrTag: '', // TODO: Set the correct value
472+
mtext: this.convertEmbeddedMText(entity, 'ATTDEF_mtext')
473+
}
474+
}
475+
421476
private convertCircle(
422477
entity: Dwg_Object_Entity_Ptr,
423478
commonAttrs: DwgCommonAttributes
@@ -919,6 +974,21 @@ export class LibreEntityConverter {
919974
'extrusion'
920975
).data as DwgPoint3D
921976

977+
const attrib_ptr_array = libredwg.dwg_entity_insert_get_attribs(entity)
978+
const attribs: DwgAttribEntity[] = []
979+
attrib_ptr_array.forEach(object_ptr => {
980+
const entity = libredwg.dwg_object_to_entity(object_ptr)
981+
const entity_tio = libredwg.dwg_object_to_entity_tio(object_ptr)
982+
if (entity && entity_tio) {
983+
// Get values of the common attributes of ATTRIB entity
984+
const commonAttrs = this.getCommonAttrs(entity)
985+
const fixedtype = libredwg.dwg_object_get_fixedtype(object_ptr)
986+
if (fixedtype == Dwg_Object_Type.DWG_TYPE_ATTRIB) {
987+
attribs.push(this.convertAttrib(entity_tio, commonAttrs))
988+
}
989+
}
990+
})
991+
922992
// TODO: convert block attributes
923993
return {
924994
type: 'INSERT',
@@ -933,7 +1003,8 @@ export class LibreEntityConverter {
9331003
rowCount: rowCount,
9341004
columnSpacing: columnSpacing,
9351005
rowSpacing: rowSpacing,
936-
extrusionDirection: extrusionDirection
1006+
extrusionDirection: extrusionDirection,
1007+
attribs: attribs
9371008
}
9381009
}
9391010

bindings/javascript/src/database/entities/attrib.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export interface DwgAttribEntity extends DwgEntity {
5858
/**
5959
* hard-pointer id of secondary attribute(s) or attribute definition(s)
6060
*/
61-
secondaryAttrsHardId: number
61+
secondaryAttrsHardId: string
6262
/**
6363
* Alignment point of attribute or attribute definition.
6464
*/

bindings/javascript/src/database/entities/insert.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DwgPoint3D } from '../common'
2+
import { DwgAttribEntity } from './attrib'
23
import { DwgEntity } from './entity'
34

45
export interface DwgInsertEntity extends DwgEntity {
@@ -56,4 +57,8 @@ export interface DwgInsertEntity extends DwgEntity {
5657
* Extrusion direction (optional; default = 0, 0, 1)
5758
*/
5859
extrusionDirection: DwgPoint3D
60+
/**
61+
* Attributes associated with the INSERT entity
62+
*/
63+
attribs: DwgAttribEntity[]
5964
}

bindings/javascript/src/libredwg.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,20 @@ export class LibreDwg {
12651265
.data as Dwg_Entity_VERTEX_3D[]
12661266
}
12671267

1268+
/**
1269+
* Returns attributes associated with INSERT entity.
1270+
* @group Dwg_Entity_INSERT Methods
1271+
* @param ptr Pointer to one Dwg_Object (not Dwg_Entity_INSERT) instance.
1272+
* @returns Returns attributes associated with INSERT entity.
1273+
*/
1274+
dwg_entity_insert_get_attribs(
1275+
ptr: Dwg_Object_Ptr
1276+
): number[] {
1277+
const wasmInstance = this.wasmInstance
1278+
return wasmInstance.dwg_entity_insert_get_attribs(ptr)
1279+
.data as number[]
1280+
}
1281+
12681282
/**
12691283
* Returns texts in Dwg_Object_DICTIONARY.
12701284
* @group Dwg_Object_DICTIONARY Methods

bindings/javascript/wasm/libredwg-web.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ interface EmbindModule {
518518
dwg_entity_polyline_3d_get_points(_0: number): any;
519519
dwg_entity_polyline_3d_get_vertices(_0: number): any;
520520
dwg_entity_block_header_get_preview(_0: number): any;
521+
dwg_entity_insert_get_attribs(_0: number): any;
521522
dwg_object_dictionary_get_texts(_0: number): any;
522523
dwg_read_file(_0: EmbindString): any;
523524
dwg_bmp(_0: number): any;
332 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)