@mlightcad/libredwg-web 0.6.10 → 0.7.1

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.
@@ -1,4 +1,4 @@
1
- import { DwgBoundaryPathEdgeType, DwgHatchAssociativity, DwgHatchSolidFill } from '../database';
1
+ import { DwgBoundaryPathEdgeType, DwgHatchAssociativity, DwgHatchGradientColorFlag, DwgHatchGradientFlag, DwgHatchSolidFill } from '../database';
2
2
  import { Dwg_Hatch_Edge_Type, Dwg_Object_Type } from '../types';
3
3
  import { idToString } from './utils';
4
4
  export class LibreEntityConverter {
@@ -483,8 +483,7 @@ export class LibreEntityConverter {
483
483
  const seedPoints_ptr = libredwg.dwg_dynapi_entity_value(entity, 'seeds')
484
484
  .data;
485
485
  const seedPoints = libredwg.dwg_ptr_to_point2d_array(seedPoints_ptr, numberOfSeedPoints);
486
- return {
487
- type: 'HATCH',
486
+ const result = {
488
487
  ...commonAttrs,
489
488
  // elevationPoint: DwgPoint3D
490
489
  extrusionDirection: extrusionDirection,
@@ -518,6 +517,42 @@ export class LibreEntityConverter {
518
517
  seedPoints: seedPoints
519
518
  // gradientFlag?: DwgHatchGradientFlag
520
519
  };
520
+ const gradientFlag = libredwg.dwg_dynapi_entity_value(entity, 'is_gradient_fill')
521
+ .data;
522
+ if (gradientFlag > 0) {
523
+ const gradientName = libredwg.dwg_dynapi_entity_value(entity, 'gradient_name')
524
+ .data;
525
+ const gradientRotation = libredwg.dwg_dynapi_entity_value(entity, 'gradient_angle')
526
+ .data;
527
+ const gradientDefinition = libredwg.dwg_dynapi_entity_value(entity, 'gradient_shift')
528
+ .data;
529
+ const colorTint = libredwg.dwg_dynapi_entity_value(entity, 'gradient_tint')
530
+ .data;
531
+ const gradientColorFlag = libredwg.dwg_dynapi_entity_value(entity, 'single_color_gradient')
532
+ .data;
533
+ // const numberOfColors = libredwg.dwg_dynapi_entity_value(entity, 'num_colors')
534
+ // .data as number
535
+ const gradientColors_ptr = libredwg.dwg_dynapi_entity_value(entity, 'colors')
536
+ .data;
537
+ const gradientColors = libredwg.dwg_ptr_to_hatch_gradient_color_array(gradientColors_ptr, (gradientColorFlag == 1) ? 1 : 2);
538
+ return {
539
+ type: 'HATCH',
540
+ ...result,
541
+ gradientFlag: DwgHatchGradientFlag.Gradient,
542
+ gradientColorFlag: gradientColorFlag == 1 ? DwgHatchGradientColorFlag.OneColor : DwgHatchGradientColorFlag.TwoColor,
543
+ gradientName,
544
+ gradientRotation,
545
+ gradientDefinition,
546
+ colorTint,
547
+ gradientColors
548
+ };
549
+ }
550
+ else {
551
+ return {
552
+ type: 'HATCH',
553
+ ...result
554
+ };
555
+ }
521
556
  }
522
557
  convertHatchBoundaryPaths(paths) {
523
558
  const converted = paths
@@ -124,13 +124,20 @@ export interface DwgHatchEntityBase extends DwgEntity {
124
124
  seedPoints?: DwgPoint2D[];
125
125
  gradientFlag?: DwgHatchGradientFlag;
126
126
  }
127
+ export interface DwgGradientColor {
128
+ reservedData: number;
129
+ rgb: number;
130
+ colorIndex?: number;
131
+ }
127
132
  export interface DwgGradientHatchEntity extends DwgHatchEntityBase {
133
+ gradientName: string;
128
134
  gradientFlag: DwgHatchGradientFlag.Gradient;
129
135
  gradientColorFlag: DwgHatchGradientColorFlag;
130
136
  numberOfColors: 0 | 2;
131
137
  gradientRotation?: number;
132
138
  gradientDefinition: number;
133
139
  colorTint?: number;
140
+ gradientColors?: Array<DwgGradientColor>;
134
141
  }
135
142
  export type DwgHatchEntity = DwgGradientHatchEntity | DwgHatchEntityBase;
136
143
  export {};
package/lib/libredwg.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { MainModule } from '../wasm/libredwg-web';
2
2
  import createModule from '../wasm/libredwg-web.js';
3
- import { DwgCodePage, DwgDatabase, DwgPoint2D, DwgPoint3D, DwgPoint4D, DwgVersion, DwgXData } from './database';
3
+ import { DwgCodePage, DwgDatabase, DwgGradientColor, DwgPoint2D, DwgPoint3D, DwgPoint4D, DwgVersion, DwgXData } from './database';
4
4
  import { Dwg_Array_Ptr, Dwg_Class, Dwg_Color, Dwg_Data_Ptr, Dwg_Entity_BLOCK, Dwg_Entity_IMAGE_Ptr, Dwg_Entity_LWPOLYLINE_Ptr, Dwg_Entity_MTEXT_Ptr, Dwg_Entity_POLYLINE_2D_Ptr, Dwg_Entity_POLYLINE_3D_Ptr, Dwg_Entity_TEXT_Ptr, Dwg_Entity_VERTEX_2D, Dwg_Entity_VERTEX_3D, Dwg_Field_Value, Dwg_Handle, Dwg_HATCH_DefLine, Dwg_HATCH_Path, Dwg_LTYPE_Dash, Dwg_MLINE_Vertex, Dwg_Object_BLOCK_HEADER_Ptr, Dwg_Object_BLOCK_Ptr, Dwg_Object_DIMSTYLE_Ptr, Dwg_Object_Entity_Ptr, Dwg_Object_Entity_TIO_Ptr, Dwg_Object_Generic_Ptr, Dwg_Object_IMAGEDEF_Ptr, Dwg_Object_LAYER_Ptr, Dwg_Object_LTYPE_Ptr, Dwg_Object_Object_Ptr, Dwg_Object_Object_TIO_Ptr, Dwg_Object_Ptr, Dwg_Object_Ref, Dwg_Object_Ref_Ptr, Dwg_Object_STYLE_Ptr, Dwg_Object_Type, Dwg_Object_VERTEX_2D_Ptr, Dwg_Object_VERTEX_3D_Ptr, Dwg_Object_VPORT_Ptr, Dwg_TABLE_Cell } from './types';
5
5
  export { createModule };
6
6
  export type LibreDwgEx = LibreDwg & MainModule;
@@ -19,6 +19,12 @@ export declare class LibreDwg {
19
19
  private decoder?;
20
20
  private constructor();
21
21
  dwg_read_data(fileContent: string | ArrayBuffer, fileType: number): number | undefined;
22
+ /**
23
+ * Converts DWG file content to DXF file content.
24
+ * @param fileContent DWG file content.
25
+ * @returns Returns DXF file content if conversion succeeds. Otherwise returns null.
26
+ */
27
+ dwg_write_dxf(fileContent: string | ArrayBuffer): Uint8Array | null;
22
28
  /**
23
29
  * Gets the version of the dwg.
24
30
  * @param data Pointer to Dwg_Data instance.
@@ -472,6 +478,15 @@ export declare class LibreDwg {
472
478
  * @returns Returns one JavaScript hatch path array from the specified C++ hatch path array.
473
479
  */
474
480
  dwg_ptr_to_hatch_path_array(ptr: Dwg_Array_Ptr, size: number): Dwg_HATCH_Path[];
481
+ /**
482
+ * Converts one C++ hatch gradient color array to one JavaScript hatch gradient color array.
483
+ * @group Array Methods
484
+ * @group Dwg_Entity_HATCH Methods
485
+ * @param ptr Pointer to C++ hatch gradient color array.
486
+ * @param size The size of C++ hatch gradient color array.
487
+ * @returns Returns one JavaScript hatch gradient color array from the specified C++ hatch gradient color array.
488
+ */
489
+ dwg_ptr_to_hatch_gradient_color_array(ptr: Dwg_Array_Ptr, size: number): DwgGradientColor[];
475
490
  /**
476
491
  * Converts one C++ mline vertex array to one JavaScript mline vertex array.
477
492
  * @group Array Methods
package/lib/libredwg.js CHANGED
@@ -29,13 +29,19 @@ export class LibreDwg {
29
29
  dwg_read_data(fileContent, fileType) {
30
30
  if (fileType == Dwg_File_Type.DWG) {
31
31
  const fileName = 'tmp.dwg';
32
- this.wasmInstance.FS.writeFile(fileName, new Uint8Array(fileContent));
33
- const result = this.wasmInstance.dwg_read_file(fileName);
34
- if (result.error != 0) {
35
- console.log('Open dwg file with error code: ', result.error);
32
+ try {
33
+ this.wasmInstance.FS.writeFile(fileName, new Uint8Array(fileContent));
34
+ const result = this.wasmInstance.dwg_read_file(fileName);
35
+ if (result.error != 0) {
36
+ console.log('Open dwg file with error code: ', result.error);
37
+ }
38
+ return result.data;
39
+ }
40
+ finally {
41
+ if (this.wasmInstance.FS.analyzePath(fileName, false).exists) {
42
+ this.wasmInstance.FS.unlink(fileName);
43
+ }
36
44
  }
37
- this.wasmInstance.FS.unlink(fileName);
38
- return result.data;
39
45
  }
40
46
  // else if (fileType == Dwg_File_Type.DXF) {
41
47
  // const fileName = "tmp.dxf";
@@ -48,6 +54,32 @@ export class LibreDwg {
48
54
  // return result.data as Dwg_Data_Ptr;
49
55
  // }
50
56
  }
57
+ /**
58
+ * Converts DWG file content to DXF file content.
59
+ * @param fileContent DWG file content.
60
+ * @returns Returns DXF file content if conversion succeeds. Otherwise returns null.
61
+ */
62
+ dwg_write_dxf(fileContent) {
63
+ const inputFileName = 'tmp.dwg';
64
+ const outputFileName = 'tmp.dxf';
65
+ try {
66
+ this.wasmInstance.FS.writeFile(inputFileName, new Uint8Array(fileContent));
67
+ const error = this.wasmInstance.dwg_write_dxf(inputFileName, outputFileName);
68
+ if (error != 0) {
69
+ console.log('Convert dwg to dxf with error code: ', error);
70
+ return null;
71
+ }
72
+ return this.wasmInstance.FS.readFile(outputFileName);
73
+ }
74
+ finally {
75
+ if (this.wasmInstance.FS.analyzePath(inputFileName, false).exists) {
76
+ this.wasmInstance.FS.unlink(inputFileName);
77
+ }
78
+ if (this.wasmInstance.FS.analyzePath(outputFileName, false).exists) {
79
+ this.wasmInstance.FS.unlink(outputFileName);
80
+ }
81
+ }
82
+ }
51
83
  /**
52
84
  * Gets the version of the dwg.
53
85
  * @param data Pointer to Dwg_Data instance.
@@ -665,6 +697,17 @@ export class LibreDwg {
665
697
  dwg_ptr_to_hatch_path_array(ptr, size) {
666
698
  return this.wasmInstance.dwg_ptr_to_hatch_path_array(ptr, size);
667
699
  }
700
+ /**
701
+ * Converts one C++ hatch gradient color array to one JavaScript hatch gradient color array.
702
+ * @group Array Methods
703
+ * @group Dwg_Entity_HATCH Methods
704
+ * @param ptr Pointer to C++ hatch gradient color array.
705
+ * @param size The size of C++ hatch gradient color array.
706
+ * @returns Returns one JavaScript hatch gradient color array from the specified C++ hatch gradient color array.
707
+ */
708
+ dwg_ptr_to_hatch_gradient_color_array(ptr, size) {
709
+ return this.wasmInstance.dwg_ptr_to_hatch_gradient_color_array(ptr, size);
710
+ }
668
711
  /**
669
712
  * Converts one C++ mline vertex array to one JavaScript mline vertex array.
670
713
  * @group Array Methods
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "A DWG/DXF JavaScript parser based on libredwg",
4
4
  "license": "GPL-3.0",
5
5
  "private": false,
6
- "version": "0.6.10",
6
+ "version": "0.7.1",
7
7
  "author": "MLight Lee <mlight.lee@outlook.com>",
8
8
  "type": "module",
9
9
  "repository": {
@@ -70,7 +70,7 @@
70
70
  "demo": "live-server examples --entry-file=index.html",
71
71
  "doc": "typedoc",
72
72
  "build": "tsc && vite build && cp -rf wasm examples/ && cp -rf dist examples/",
73
- "build:prepare": "mkdir -p ../../build-wasm && cd ../../build-wasm && emconfigure ../configure CFLAGS=\"-O2 -sUSE_ZLIB=1\" CC=emcc --enable-release --disable-docs --disable-write --disable-python --disable-bindings --disable-shared --disable-json --disable-dxf --enable-partial --enable-experimental",
73
+ "build:prepare": "mkdir -p ../../build-wasm && cd ../../build-wasm && emconfigure ../configure CFLAGS=\"-O2 -sUSE_ZLIB=1\" CC=emcc --enable-release --disable-docs --disable-write --disable-python --disable-bindings --disable-shared --disable-json --enable-partial --enable-experimental",
74
74
  "build:obj": "cd ../../build-wasm && emmake make",
75
75
  "build:wasm": "cd ../../build-wasm && emcc ../bindings/javascript/embind/*.cpp src/*.o -O2 -lembind -std=c++17 -Isrc -I../src -I../include -o libredwg-web.js -s ALLOW_MEMORY_GROWTH=1 -s EXPORT_ES6=1 -s MODULARIZE=1 -s EXPORT_NAME=\"createModule\" -sEXPORTED_RUNTIME_METHODS=FS,ENV,ccall,cwrap,UTF8ToString,stringToNewUTF8,setValue --emit-tsd libredwg-web.d.ts",
76
76
  "clean": "rm -rf ../../build-wasm ./docs ./dist ./lib",
@@ -10,9 +10,10 @@ declare namespace RuntimeExports {
10
10
  export let currentPath: string;
11
11
  export let initialized: boolean;
12
12
  export let ignorePermissions: boolean;
13
+ export { ErrnoError };
13
14
  export let filesystems: any;
14
15
  export let syncFSRequests: number;
15
- export { ErrnoError };
16
+ export let readFiles: {};
16
17
  export { FSStream };
17
18
  export { FSNode };
18
19
  export function lookupPath(path: any, opts?: {}): {
@@ -113,10 +114,11 @@ declare namespace RuntimeExports {
113
114
  export function llseek(stream: any, offset: any, whence: any): any;
114
115
  export function read(stream: any, buffer: any, offset: any, length: any, position: any): any;
115
116
  export function write(stream: any, buffer: any, offset: any, length: any, position: any, canOwn: any): any;
117
+ export function allocate(stream: any, offset: any, length: any): void;
116
118
  export function mmap(stream: any, length: any, position: any, prot: any, flags: any): any;
117
119
  export function msync(stream: any, buffer: any, offset: any, length: any, mmapFlags: any): any;
118
120
  export function ioctl(stream: any, cmd: any, arg: any): any;
119
- export function readFile(path: any, opts?: {}): Uint8Array<any>;
121
+ export function readFile(path: any, opts?: {}): any;
120
122
  export function writeFile(path: any, data: any, opts?: {}): void;
121
123
  export function cwd(): any;
122
124
  export function chdir(path: any): void;
@@ -150,10 +152,10 @@ declare namespace RuntimeExports {
150
152
  /**
151
153
  * @param {string|null=} returnType
152
154
  * @param {Array=} argTypes
153
- * @param {Array=} args
155
+ * @param {Arguments|Array=} args
154
156
  * @param {Object=} opts
155
157
  */
156
- function ccall(ident: any, returnType?: (string | null) | undefined, argTypes?: any[] | undefined, args?: any[] | undefined, opts?: any | undefined): any;
158
+ function ccall(ident: any, returnType?: (string | null) | undefined, argTypes?: any[] | undefined, args?: (Arguments | any[]) | undefined, opts?: any | undefined): any;
157
159
  /**
158
160
  * @param {string=} returnType
159
161
  * @param {Array=} argTypes
@@ -169,11 +171,13 @@ declare namespace RuntimeExports {
169
171
  * maximum number of bytes to read. You can omit this parameter to scan the
170
172
  * string until the first 0 byte. If maxBytesToRead is passed, and the string
171
173
  * at [ptr, ptr+maxBytesToReadr[ contains a null byte in the middle, then the
172
- * string will cut short at that byte index.
173
- * @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character.
174
+ * string will cut short at that byte index (i.e. maxBytesToRead will not
175
+ * produce a string of exact length [ptr, ptr+maxBytesToRead[) N.B. mixing
176
+ * frequent uses of UTF8ToString() with and without maxBytesToRead may throw
177
+ * JS JIT optimizations off, so it is worth to consider consistently using one
174
178
  * @return {string}
175
179
  */
176
- function UTF8ToString(ptr: number, maxBytesToRead?: number | undefined, ignoreNul?: boolean | undefined): string;
180
+ function UTF8ToString(ptr: number, maxBytesToRead?: number | undefined): string;
177
181
  function stringToNewUTF8(str: any): any;
178
182
  /**
179
183
  * @param {number} ptr
@@ -181,6 +185,17 @@ declare namespace RuntimeExports {
181
185
  * @param {string} type
182
186
  */
183
187
  function setValue(ptr: number, value: number, type?: string): void;
188
+ let HEAPF32: any;
189
+ let HEAPF64: any;
190
+ let HEAP_DATA_VIEW: any;
191
+ let HEAP8: any;
192
+ let HEAPU8: any;
193
+ let HEAP16: any;
194
+ let HEAPU16: any;
195
+ let HEAP32: any;
196
+ let HEAPU32: any;
197
+ let HEAP64: any;
198
+ let HEAPU64: any;
184
199
  }
185
200
  declare class ErrnoError {
186
201
  constructor(errno: any);
@@ -232,8 +247,6 @@ export interface ClassHandle {
232
247
  delete(): void;
233
248
  deleteLater(): this;
234
249
  isDeleted(): boolean;
235
- // @ts-ignore - If targeting lower than ESNext, this symbol might not exist.
236
- [Symbol.dispose](): void;
237
250
  clone(): this;
238
251
  }
239
252
  export interface Dwg_Version_TypeValue<T extends number> {
@@ -426,6 +439,7 @@ interface EmbindModule {
426
439
  dwg_dynapi_entity_set_value(_0: number, _1: EmbindString, _2: EmbindString, _3: number, _4: boolean): boolean;
427
440
  dwg_dynapi_common_set_value(_0: number, _1: EmbindString, _2: number, _3: boolean): boolean;
428
441
  dwg_dynapi_handle_name(_0: number, _1: number, _2: number): string;
442
+ dwg_write_dxf(_0: EmbindString, _1: EmbindString): number;
429
443
  dwg_find_tablehandle(_0: number, _1: EmbindString, _2: EmbindString): number;
430
444
  dwg_find_tablehandle_index(_0: number, _1: number, _2: EmbindString): number;
431
445
  dwg_handle_name(_0: number, _1: EmbindString, _2: number): string;
@@ -481,6 +495,7 @@ interface EmbindModule {
481
495
  dwg_ptr_to_table_cell_array(_0: number, _1: number): any;
482
496
  dwg_ptr_to_hatch_defline_array(_0: number, _1: number): any;
483
497
  dwg_ptr_to_hatch_path_array(_0: number, _1: number): any;
498
+ dwg_ptr_to_hatch_gradient_color_array(_0: number, _1: number): any;
484
499
  dwg_ptr_to_mline_vertex_array(_0: number, _1: number): any;
485
500
  dwg_object_get_name(_0: number): any;
486
501
  dwg_object_get_handle_object(_0: number): any;