ofs-dxf-ts 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 (97) hide show
  1. package/API.md +411 -0
  2. package/CHANGELOG.md +26 -0
  3. package/LICENSE +21 -0
  4. package/README.md +119 -0
  5. package/dist/core/dxf-builder.d.ts +348 -0
  6. package/dist/core/dxf-builder.js +549 -0
  7. package/dist/core/dxf-document.d.ts +220 -0
  8. package/dist/core/dxf-document.js +529 -0
  9. package/dist/core/dxf-entities.d.ts +327 -0
  10. package/dist/core/dxf-entities.js +22 -0
  11. package/dist/core/dxf-serializer.d.ts +48 -0
  12. package/dist/core/dxf-serializer.js +595 -0
  13. package/dist/core/hatch-patterns.d.ts +15 -0
  14. package/dist/core/hatch-patterns.js +214 -0
  15. package/dist/core/modelspace.d.ts +106 -0
  16. package/dist/core/modelspace.js +351 -0
  17. package/dist/geometry/math.utils.d.ts +8 -0
  18. package/dist/geometry/math.utils.js +43 -0
  19. package/dist/geometry/offset.utils.d.ts +32 -0
  20. package/dist/geometry/offset.utils.js +87 -0
  21. package/dist/geometry/polyline.utils.d.ts +10 -0
  22. package/dist/geometry/polyline.utils.js +63 -0
  23. package/dist/geometry/text.utils.d.ts +15 -0
  24. package/dist/geometry/text.utils.js +28 -0
  25. package/dist/index.d.ts +23 -0
  26. package/dist/index.js +29 -0
  27. package/dist/parser/auto-cad-color-index.d.ts +8 -0
  28. package/dist/parser/auto-cad-color-index.js +265 -0
  29. package/dist/parser/dim-style-codes.d.ts +5 -0
  30. package/dist/parser/dim-style-codes.js +32 -0
  31. package/dist/parser/dxf-parser.d.ts +16 -0
  32. package/dist/parser/dxf-parser.js +824 -0
  33. package/dist/parser/entities/3dface.d.ts +5 -0
  34. package/dist/parser/entities/3dface.js +76 -0
  35. package/dist/parser/entities/arc.d.ts +5 -0
  36. package/dist/parser/entities/arc.js +34 -0
  37. package/dist/parser/entities/attdef.d.ts +5 -0
  38. package/dist/parser/entities/attdef.js +86 -0
  39. package/dist/parser/entities/attribute.d.ts +5 -0
  40. package/dist/parser/entities/attribute.js +106 -0
  41. package/dist/parser/entities/circle.d.ts +5 -0
  42. package/dist/parser/entities/circle.js +40 -0
  43. package/dist/parser/entities/dimension.d.ts +5 -0
  44. package/dist/parser/entities/dimension.js +65 -0
  45. package/dist/parser/entities/ellipse.d.ts +5 -0
  46. package/dist/parser/entities/ellipse.js +42 -0
  47. package/dist/parser/entities/hatch.d.ts +5 -0
  48. package/dist/parser/entities/hatch.js +321 -0
  49. package/dist/parser/entities/insert.d.ts +5 -0
  50. package/dist/parser/entities/insert.js +52 -0
  51. package/dist/parser/entities/line.d.ts +5 -0
  52. package/dist/parser/entities/line.js +30 -0
  53. package/dist/parser/entities/lwpolyline.d.ts +5 -0
  54. package/dist/parser/entities/lwpolyline.js +101 -0
  55. package/dist/parser/entities/mtext.d.ts +5 -0
  56. package/dist/parser/entities/mtext.js +54 -0
  57. package/dist/parser/entities/point.d.ts +5 -0
  58. package/dist/parser/entities/point.js +30 -0
  59. package/dist/parser/entities/polyline.d.ts +5 -0
  60. package/dist/parser/entities/polyline.js +85 -0
  61. package/dist/parser/entities/solid.d.ts +5 -0
  62. package/dist/parser/entities/solid.js +35 -0
  63. package/dist/parser/entities/spline.d.ts +5 -0
  64. package/dist/parser/entities/spline.js +69 -0
  65. package/dist/parser/entities/text.d.ts +5 -0
  66. package/dist/parser/entities/text.js +47 -0
  67. package/dist/parser/entities/vertex.d.ts +5 -0
  68. package/dist/parser/entities/vertex.js +59 -0
  69. package/dist/parser/extended-data-parser.d.ts +18 -0
  70. package/dist/parser/extended-data-parser.js +100 -0
  71. package/dist/parser/index.d.ts +10 -0
  72. package/dist/parser/index.js +9 -0
  73. package/dist/parser/log.d.ts +14 -0
  74. package/dist/parser/log.js +39 -0
  75. package/dist/parser/parse-helpers.d.ts +23 -0
  76. package/dist/parser/parse-helpers.js +128 -0
  77. package/dist/parser/scanner.d.ts +29 -0
  78. package/dist/parser/scanner.js +143 -0
  79. package/dist/parser/types.d.ts +78 -0
  80. package/dist/parser/types.js +5 -0
  81. package/dist/symbols/slope-symbol.d.ts +21 -0
  82. package/dist/symbols/slope-symbol.js +81 -0
  83. package/dist/symbols/structures.d.ts +9 -0
  84. package/dist/symbols/structures.js +120 -0
  85. package/dist/template/blank-document.d.ts +13 -0
  86. package/dist/template/blank-document.js +13 -0
  87. package/dist/template/block-registry.d.ts +16 -0
  88. package/dist/template/block-registry.js +135 -0
  89. package/dist/template/index.d.ts +17 -0
  90. package/dist/template/index.js +17 -0
  91. package/dist/template/template-injector.d.ts +45 -0
  92. package/dist/template/template-injector.js +487 -0
  93. package/dist/template/template-reader.d.ts +28 -0
  94. package/dist/template/template-reader.js +80 -0
  95. package/dist/types/index.d.ts +24 -0
  96. package/dist/types/index.js +8 -0
  97. package/package.json +59 -0
@@ -0,0 +1,220 @@
1
+ /**
2
+ * DxfDocument — Core document class with template parsing.
3
+ *
4
+ * ✅ Works in both Node.js AND Browser/Angular environments.
5
+ *
6
+ * Every document is built on a base DXF: a user template, or the built-in
7
+ * blank DXF R2018 document when created from scratch. New layers, linetypes,
8
+ * text styles, blocks, images and layouts are injected into that base on
9
+ * output, so both modes produce the same, complete R2000+ structure.
10
+ *
11
+ * Usage (Node.js):
12
+ * const doc = DxfDocument.readFile('./template/template2018.dxf');
13
+ * doc.saveas('output.dxf');
14
+ *
15
+ * Usage (Browser/Angular):
16
+ * const doc = DxfDocument.fromString(templateDxfString);
17
+ * const dxfOutput = doc.toDxfString();
18
+ * // download via Blob, or send to server
19
+ *
20
+ * Usage (from scratch — both environments):
21
+ * const doc = DxfDocument.create();
22
+ * doc.addLayer('MyLayer', 7, 'CONTINUOUS');
23
+ * const msp = doc.modelspace();
24
+ * msp.add_circle([50, 50], 25);
25
+ * const output = doc.toDxfString();
26
+ */
27
+ import { Modelspace } from './modelspace.js';
28
+ import { LayerDef, DxfEntity, GeoReferenceDef, LayoutDef } from './dxf-entities.js';
29
+ export declare class DxfDocument {
30
+ private _layers;
31
+ private _linetypes;
32
+ private _styles;
33
+ private _blocks;
34
+ private _msp;
35
+ private _layouts;
36
+ /** Upper-cased layout name → entity collector for that layout. */
37
+ private _paperspaces;
38
+ private _rawSections;
39
+ private _templateBlockNames;
40
+ private _templateLayerNames;
41
+ private _templateStyleNames;
42
+ private _templateLayoutNames;
43
+ private _parsedTemplate;
44
+ private constructor();
45
+ /**
46
+ * Read a DXF file from disk (Node.js only).
47
+ * @throws Error if called in browser environment.
48
+ */
49
+ static readFile(filePath: string): DxfDocument;
50
+ /**
51
+ * Parse a DXF template from a string.
52
+ * ✅ Works in both Node.js and Browser/Angular.
53
+ */
54
+ static fromString(content: string): DxfDocument;
55
+ /**
56
+ * Create a blank DXF document (DXF R2018, meters).
57
+ * ✅ Works in both Node.js and Browser/Angular.
58
+ */
59
+ static create(): DxfDocument;
60
+ /** Get the modelspace for adding entities. */
61
+ modelspace(): Modelspace;
62
+ /** Add or update a layer. */
63
+ addLayer(name: string, color?: number, linetype?: string): void;
64
+ /** Check if a layer exists. */
65
+ hasLayer(name: string): boolean;
66
+ /** Get layer definition. */
67
+ getLayer(name: string): LayerDef | undefined;
68
+ /** Access layers as a record (backward compat). */
69
+ get layers(): Record<string, LayerDef>;
70
+ /**
71
+ * Add a text style. For Thai text use a TrueType font file name,
72
+ * e.g. `addTextStyle('THAI', 'THSarabunNew.ttf')`.
73
+ * Styles that already exist in the base document are kept as they are.
74
+ */
75
+ addTextStyle(name: string, font: string, height?: number): void;
76
+ /** ezdxf-compatible styles.new() */
77
+ get styles(): {
78
+ new: (name: string, dxfattribs: {
79
+ font: string;
80
+ }) => void;
81
+ };
82
+ /**
83
+ * Add a linetype definition. Pattern values are in drawing units:
84
+ * positive = dash, negative = gap, 0 = dot.
85
+ * Linetypes that already exist in the base document are kept as they are.
86
+ */
87
+ addLineType(name: string, description: string, pattern: number[]): void;
88
+ private _geoRef;
89
+ /**
90
+ * Set geographic coordinate reference for the drawing.
91
+ * This embeds coordinate system info into the DXF HEADER.
92
+ *
93
+ * @example
94
+ * // UTM Zone 47N (Thailand)
95
+ * doc.setGeoReference({
96
+ * crsId: 'EPSG:32647',
97
+ * designPoint: [0, 0],
98
+ * referencePoint: [600000, 1700000], // Easting, Northing
99
+ * northDirection: 0,
100
+ * scaleFactor: 1,
101
+ * coordinateType: 2, // projected
102
+ * });
103
+ */
104
+ setGeoReference(geoRef: GeoReferenceDef): void;
105
+ /** Get the geo reference definition (if set). */
106
+ get geoReference(): GeoReferenceDef | null;
107
+ /**
108
+ * Add a Paper Space layout for plotting.
109
+ * Paper space units are mm; the origin is the lower-left corner of the
110
+ * printable area (inside the margins), as in AutoCAD.
111
+ *
112
+ * @example
113
+ * doc.addLayout({
114
+ * name: 'A1',
115
+ * paperWidth: 841, // mm
116
+ * paperHeight: 594, // mm
117
+ * margins: [0, 0, 0, 0],
118
+ * viewports: [{
119
+ * center: [420, 297],
120
+ * width: 800, height: 560,
121
+ * viewCenter: [519500, 1033500],
122
+ * scale: 12500, // 1:12,500 — model in meters
123
+ * frozenLayers: ['GRID'],
124
+ * }]
125
+ * });
126
+ */
127
+ addLayout(layout: LayoutDef): void;
128
+ /** Get all layout definitions added with addLayout(). */
129
+ get layouts(): LayoutDef[];
130
+ /** Names of all layouts: those of the base document (incl. "Model") and added ones. */
131
+ get layoutNames(): string[];
132
+ /** Check if a layout exists (case-insensitive). */
133
+ hasLayout(name: string): boolean;
134
+ /**
135
+ * Entity collector for a layout: an existing layout of the base document
136
+ * (e.g. "Layout1") or one added with addLayout(). "Model" returns model space.
137
+ */
138
+ paperspace(name: string): Modelspace;
139
+ /**
140
+ * Define (or redefine) a block. Entity coordinates are block-local,
141
+ * relative to `basePoint`.
142
+ */
143
+ addBlockDefinition(name: string, entities: DxfEntity[], basePoint?: [number, number]): void;
144
+ /** Check if a block exists (in template or user-defined). */
145
+ hasBlock(name: string): boolean;
146
+ /** Get all block names. */
147
+ getBlockNames(): string[];
148
+ /** Freeze a layer (entities on this layer are hidden and ignored). */
149
+ freezeLayer(name: string): void;
150
+ /** Thaw a layer (undo freeze). */
151
+ thawLayer(name: string): void;
152
+ /** Lock a layer (entities visible but not editable). */
153
+ lockLayer(name: string): void;
154
+ /** Unlock a layer. */
155
+ unlockLayer(name: string): void;
156
+ /** Turn a layer off (entities hidden but still selectable). */
157
+ setLayerOff(name: string): void;
158
+ /** Turn a layer on. */
159
+ setLayerOn(name: string): void;
160
+ /** Set layer color. */
161
+ setLayerColor(name: string, color: number): void;
162
+ /** Set layer linetype. */
163
+ setLayerLinetype(name: string, linetype: string): void;
164
+ private _headerVars;
165
+ /**
166
+ * Set a HEADER variable (replaces the base document's value).
167
+ * @example doc.setHeaderVar('$INSUNITS', [{ code: 70, value: '6' }]); // Meters
168
+ */
169
+ setHeaderVar(name: string, values: {
170
+ code: number;
171
+ value: string;
172
+ }[]): void;
173
+ /**
174
+ * Set the drawing extents (bounding box).
175
+ * This controls the initial zoom-to-extents view in CAD software.
176
+ * Equivalent to ezdxf: doc.header['$EXTMIN'] / doc.header['$EXTMAX']
177
+ */
178
+ setExtents(minX: number, minY: number, maxX: number, maxY: number): void;
179
+ /**
180
+ * Auto-calculate extents from all entities in modelspace.
181
+ * Scans all entity coordinates and sets $EXTMIN/$EXTMAX accordingly.
182
+ */
183
+ zoomExtents(padding?: number): void;
184
+ /**
185
+ * Set the view center and height (zoom level) for the initial view.
186
+ * Equivalent to ezdxf setting $VIEWCTR and $VIEWSIZE.
187
+ */
188
+ setViewCenter(x: number, y: number, viewHeight?: number): void;
189
+ /**
190
+ * Zoom to a specific window (rectangle).
191
+ */
192
+ zoomWindow(minX: number, minY: number, maxX: number, maxY: number): void;
193
+ /** Access the HEADER vars map (used by serializer). */
194
+ get headerVars(): Map<string, {
195
+ code: number;
196
+ value: string;
197
+ }[]>;
198
+ /**
199
+ * Calculate bounding box of all entities in modelspace.
200
+ */
201
+ private _calculateBounds;
202
+ setActiveLayer(_name: string): void;
203
+ drawPolyline(points: [number, number][], closed?: boolean): void;
204
+ drawCircle(cx: number, cy: number, radius: number): void;
205
+ drawText(x: number, y: number, height: number, rotation: number, text: string): void;
206
+ /** Serialize to DXF string. ✅ Works everywhere. */
207
+ toDxfString(): string;
208
+ /**
209
+ * Save to file (Node.js only).
210
+ * @throws Error if called in browser environment.
211
+ */
212
+ saveas(filePath: string): void;
213
+ /**
214
+ * Download DXF file in browser.
215
+ * ✅ Works in Browser/Angular.
216
+ */
217
+ downloadAs(filename: string): void;
218
+ private _loadBase;
219
+ private _addDefaultLinetypes;
220
+ }