@miclivs/cadcli 0.1.2 → 0.3.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.
@@ -0,0 +1,142 @@
1
+ export type AcadPoint = {
2
+ x: number;
3
+ y: number;
4
+ z?: number;
5
+ };
6
+ export type AcadColor = {
7
+ mode: "byLayer";
8
+ } | {
9
+ mode: "byBlock";
10
+ } | {
11
+ mode: "index";
12
+ index: number;
13
+ } | {
14
+ mode: "rgb";
15
+ r: number;
16
+ g: number;
17
+ b: number;
18
+ };
19
+ export type AcadEditOperation = {
20
+ kind: "setText";
21
+ entityId: string;
22
+ text: string;
23
+ } | {
24
+ kind: "setLayer";
25
+ entityId: string;
26
+ layer: string;
27
+ } | {
28
+ kind: "setColor";
29
+ entityId: string;
30
+ color: AcadColor;
31
+ } | {
32
+ kind: "setLineType";
33
+ entityId: string;
34
+ lineType: string;
35
+ } | {
36
+ kind: "setLineWeight";
37
+ entityId: string;
38
+ weight: number;
39
+ } | {
40
+ kind: "setTransparency";
41
+ entityId: string;
42
+ alpha: number;
43
+ } | {
44
+ kind: "setVisibility";
45
+ entityId: string;
46
+ visible: boolean;
47
+ } | {
48
+ kind: "move";
49
+ entityId: string;
50
+ dx: number;
51
+ dy: number;
52
+ dz?: number;
53
+ } | {
54
+ kind: "rotate";
55
+ entityId: string;
56
+ angleDegrees: number;
57
+ origin?: AcadPoint;
58
+ } | {
59
+ kind: "scale";
60
+ entityId: string;
61
+ factor: number;
62
+ origin?: AcadPoint;
63
+ } | {
64
+ kind: "copy";
65
+ entityId: string;
66
+ dx?: number;
67
+ dy?: number;
68
+ dz?: number;
69
+ } | {
70
+ kind: "matchProperties";
71
+ entityId: string;
72
+ sourceId: string;
73
+ } | {
74
+ kind: "setAttribute";
75
+ entityId: string;
76
+ tag: string;
77
+ value: string;
78
+ } | {
79
+ kind: "delete";
80
+ entityId: string;
81
+ } | {
82
+ kind: "addPoint";
83
+ at: AcadPoint;
84
+ layer?: string;
85
+ color?: AcadColor;
86
+ } | {
87
+ kind: "addLine";
88
+ from: AcadPoint;
89
+ to: AcadPoint;
90
+ layer?: string;
91
+ color?: AcadColor;
92
+ } | {
93
+ kind: "addCircle";
94
+ center: AcadPoint;
95
+ radius: number;
96
+ layer?: string;
97
+ color?: AcadColor;
98
+ } | {
99
+ kind: "addArc";
100
+ center: AcadPoint;
101
+ radius: number;
102
+ startAngleDegrees: number;
103
+ endAngleDegrees: number;
104
+ layer?: string;
105
+ color?: AcadColor;
106
+ } | {
107
+ kind: "addText";
108
+ text: string;
109
+ at: AcadPoint;
110
+ height?: number;
111
+ rotationDegrees?: number;
112
+ layer?: string;
113
+ color?: AcadColor;
114
+ } | {
115
+ kind: "addMText";
116
+ text: string;
117
+ at: AcadPoint;
118
+ height?: number;
119
+ width?: number;
120
+ rotationDegrees?: number;
121
+ layer?: string;
122
+ color?: AcadColor;
123
+ } | {
124
+ kind: "addPolyline";
125
+ points: AcadPoint[];
126
+ closed?: boolean;
127
+ layer?: string;
128
+ color?: AcadColor;
129
+ };
130
+ export interface AcadEditResult {
131
+ input: string;
132
+ output: string;
133
+ backend: "acad-ts";
134
+ operations: AcadEditOperation[];
135
+ changed: number;
136
+ }
137
+ export declare function editWithAcadTs(opts: {
138
+ input: string;
139
+ output: string;
140
+ operations: AcadEditOperation[];
141
+ overwrite?: boolean;
142
+ }): AcadEditResult;
@@ -0,0 +1,234 @@
1
+ import { Arc, Circle, Color, Insert, Layer, Line, LineType, LineWeightType, LwPolyline, MText, Point, TextEntity, Transparency, XY, XYZ, } from "@node-projects/acad-ts";
2
+ import { EXIT_USER_ERROR } from "../utils/exit-codes.js";
3
+ import { readAcadFile, writeAcadDocument } from "./acad.js";
4
+ import { DwgCliError } from "./errors.js";
5
+ import { writeOutput } from "./files.js";
6
+ const degreesToRadians = (degrees) => (degrees * Math.PI) / 180;
7
+ function xyz(point) {
8
+ return new XYZ(point.x, point.y, point.z ?? 0);
9
+ }
10
+ function xy(point) {
11
+ return new XY(point.x, point.y);
12
+ }
13
+ function handleMatches(handle, entityId) {
14
+ const expected = entityId.toLowerCase();
15
+ const text = String(handle ?? "").toLowerCase();
16
+ if (text === expected)
17
+ return true;
18
+ const numeric = Number(handle);
19
+ return (Number.isFinite(numeric) && numeric.toString(16).toLowerCase() === expected);
20
+ }
21
+ function modelEntities(doc) {
22
+ return [...(doc.modelSpace?.entities ?? [])];
23
+ }
24
+ function findEntity(doc, entityId) {
25
+ const entity = modelEntities(doc).find((item) => handleMatches(item.handle, entityId));
26
+ if (!entity) {
27
+ throw new DwgCliError(`Entity not found: ${entityId}`, "ENTITY_NOT_FOUND", EXIT_USER_ERROR);
28
+ }
29
+ return entity;
30
+ }
31
+ function ensureLayer(doc, name) {
32
+ const existing = doc.layers?.tryGetValue(name);
33
+ if (existing)
34
+ return existing;
35
+ const layer = new Layer(name);
36
+ doc.layers?.add(layer);
37
+ return layer;
38
+ }
39
+ function ensureLineType(doc, name) {
40
+ const existing = doc.lineTypes?.tryGetValue(name);
41
+ if (existing)
42
+ return existing;
43
+ const lineType = new LineType(name);
44
+ doc.lineTypes?.add(lineType);
45
+ return lineType;
46
+ }
47
+ function acadColor(color) {
48
+ switch (color.mode) {
49
+ case "byLayer":
50
+ return Color.byLayer;
51
+ case "byBlock":
52
+ return Color.byBlock;
53
+ case "index":
54
+ return new Color(color.index);
55
+ case "rgb":
56
+ return new Color(color.r, color.g, color.b);
57
+ }
58
+ }
59
+ function lineWeight(weight) {
60
+ if (!Object.values(LineWeightType).includes(weight)) {
61
+ throw new DwgCliError(`Unsupported line weight: ${weight}`, "INVALID_LINE_WEIGHT", EXIT_USER_ERROR);
62
+ }
63
+ return weight;
64
+ }
65
+ function applyCommonOptions(doc, entity, options) {
66
+ if (options.layer)
67
+ entity.layer = ensureLayer(doc, options.layer);
68
+ if (options.color)
69
+ entity.color = acadColor(options.color);
70
+ }
71
+ function addEntity(doc, entity) {
72
+ if (!doc.modelSpace) {
73
+ throw new DwgCliError("Drawing has no model space to edit.", "MODEL_SPACE_NOT_FOUND", EXIT_USER_ERROR);
74
+ }
75
+ doc.modelSpace.entities.add(entity);
76
+ }
77
+ function deleteEntity(entity, entityId) {
78
+ const owner = entity.owner;
79
+ if (!owner?.entities?.remove) {
80
+ throw new DwgCliError(`Entity cannot be deleted: ${entityId}`, "ENTITY_NOT_DELETABLE", EXIT_USER_ERROR);
81
+ }
82
+ owner.entities.remove(entity);
83
+ }
84
+ function setAttribute(entity, tag, value, id) {
85
+ if (!(entity instanceof Insert)) {
86
+ throw new DwgCliError(`Entity is not a block insert: ${id}`, "ENTITY_NOT_INSERT", EXIT_USER_ERROR);
87
+ }
88
+ const attr = [...entity.attributes].find((item) => item.tag.toLowerCase() === tag.toLowerCase());
89
+ if (!attr) {
90
+ throw new DwgCliError(`Attribute not found on ${id}: ${tag}`, "ATTRIBUTE_NOT_FOUND", EXIT_USER_ERROR);
91
+ }
92
+ attr.value = value;
93
+ }
94
+ function applyOperation(doc, operation) {
95
+ switch (operation.kind) {
96
+ case "addPoint": {
97
+ const point = new Point(xyz(operation.at));
98
+ applyCommonOptions(doc, point, operation);
99
+ addEntity(doc, point);
100
+ return;
101
+ }
102
+ case "addLine": {
103
+ const line = new Line(xyz(operation.from), xyz(operation.to));
104
+ applyCommonOptions(doc, line, operation);
105
+ addEntity(doc, line);
106
+ return;
107
+ }
108
+ case "addCircle": {
109
+ const circle = new Circle();
110
+ circle.center = xyz(operation.center);
111
+ circle.radius = operation.radius;
112
+ applyCommonOptions(doc, circle, operation);
113
+ addEntity(doc, circle);
114
+ return;
115
+ }
116
+ case "addArc": {
117
+ const arc = new Arc(xyz(operation.center), operation.radius, degreesToRadians(operation.startAngleDegrees), degreesToRadians(operation.endAngleDegrees));
118
+ applyCommonOptions(doc, arc, operation);
119
+ addEntity(doc, arc);
120
+ return;
121
+ }
122
+ case "addText": {
123
+ const text = new TextEntity();
124
+ text.value = operation.text;
125
+ text.insertPoint = xyz(operation.at);
126
+ if (operation.height !== undefined)
127
+ text.height = operation.height;
128
+ if (operation.rotationDegrees !== undefined) {
129
+ text.rotation = degreesToRadians(operation.rotationDegrees);
130
+ }
131
+ applyCommonOptions(doc, text, operation);
132
+ addEntity(doc, text);
133
+ return;
134
+ }
135
+ case "addMText": {
136
+ const text = new MText();
137
+ text.value = operation.text;
138
+ text.insertPoint = xyz(operation.at);
139
+ if (operation.height !== undefined)
140
+ text.height = operation.height;
141
+ if (operation.width !== undefined)
142
+ text.rectangleWidth = operation.width;
143
+ if (operation.rotationDegrees !== undefined) {
144
+ text.rotation = degreesToRadians(operation.rotationDegrees);
145
+ }
146
+ applyCommonOptions(doc, text, operation);
147
+ addEntity(doc, text);
148
+ return;
149
+ }
150
+ case "addPolyline": {
151
+ const polyline = new LwPolyline(operation.points.map(xy));
152
+ polyline.isClosed = operation.closed ?? false;
153
+ applyCommonOptions(doc, polyline, operation);
154
+ addEntity(doc, polyline);
155
+ return;
156
+ }
157
+ }
158
+ const entity = findEntity(doc, operation.entityId);
159
+ switch (operation.kind) {
160
+ case "setText":
161
+ if (!(entity instanceof TextEntity || entity instanceof MText)) {
162
+ throw new DwgCliError(`Entity is not editable text: ${operation.entityId}`, "ENTITY_NOT_TEXT", EXIT_USER_ERROR);
163
+ }
164
+ entity.value = operation.text;
165
+ return;
166
+ case "setLayer":
167
+ entity.layer = ensureLayer(doc, operation.layer);
168
+ return;
169
+ case "setColor":
170
+ entity.color = acadColor(operation.color);
171
+ return;
172
+ case "setLineType":
173
+ entity.lineType = ensureLineType(doc, operation.lineType);
174
+ return;
175
+ case "setLineWeight":
176
+ entity.lineWeight = lineWeight(operation.weight);
177
+ return;
178
+ case "setTransparency":
179
+ entity.transparency = Transparency.fromAlphaValue(operation.alpha);
180
+ return;
181
+ case "setVisibility":
182
+ entity.isInvisible = !operation.visible;
183
+ return;
184
+ case "move":
185
+ entity.applyTranslation(new XYZ(operation.dx, operation.dy, operation.dz ?? 0));
186
+ return;
187
+ case "rotate": {
188
+ const origin = operation.origin ? xyz(operation.origin) : XYZ.zero;
189
+ entity.applyTranslation(new XYZ(-origin.x, -origin.y, -origin.z));
190
+ entity.applyRotation(XYZ.axisZ, degreesToRadians(operation.angleDegrees));
191
+ entity.applyTranslation(origin);
192
+ return;
193
+ }
194
+ case "scale":
195
+ entity.applyScaling(new XYZ(operation.factor, operation.factor, operation.factor), operation.origin ? xyz(operation.origin) : XYZ.zero);
196
+ return;
197
+ case "copy": {
198
+ const clone = entity.clone();
199
+ clone.applyTranslation(new XYZ(operation.dx ?? 0, operation.dy ?? 0, operation.dz ?? 0));
200
+ addEntity(doc, clone);
201
+ return;
202
+ }
203
+ case "matchProperties": {
204
+ const source = findEntity(doc, operation.sourceId);
205
+ entity.matchProperties(source);
206
+ return;
207
+ }
208
+ case "setAttribute":
209
+ setAttribute(entity, operation.tag, operation.value, operation.entityId);
210
+ return;
211
+ case "delete":
212
+ deleteEntity(entity, operation.entityId);
213
+ return;
214
+ }
215
+ }
216
+ export function editWithAcadTs(opts) {
217
+ if (opts.operations.length === 0) {
218
+ throw new DwgCliError("No acad-ts edit operation specified.", "MISSING_EDIT_OPERATION", EXIT_USER_ERROR);
219
+ }
220
+ if (opts.input === opts.output && !opts.overwrite) {
221
+ throw new DwgCliError("Refusing to edit in place without --overwrite. Provide --output or pass --overwrite.", "EDIT_REQUIRES_OUTPUT_OR_OVERWRITE", EXIT_USER_ERROR);
222
+ }
223
+ const { bytes, doc, format } = readAcadFile(opts.input);
224
+ for (const operation of opts.operations)
225
+ applyOperation(doc, operation);
226
+ writeOutput(opts.output, writeAcadDocument(doc, format, bytes.length));
227
+ return {
228
+ input: opts.input,
229
+ output: opts.output,
230
+ backend: "acad-ts",
231
+ operations: opts.operations,
232
+ changed: opts.operations.length,
233
+ };
234
+ }
@@ -0,0 +1,6 @@
1
+ export interface AcadSvgViewResult {
2
+ input: string;
3
+ svg: string;
4
+ backend: "acad-ts";
5
+ }
6
+ export declare function renderSvgWithAcadTs(file: string): AcadSvgViewResult;
@@ -0,0 +1,22 @@
1
+ import { Color } from "@node-projects/acad-ts";
2
+ import { readAcadFile, writeAcadSvg } from "./acad.js";
3
+ // acad-ts SvgWriter currently crashes when text resolves ByLayer/ByBlock
4
+ // through an index that has no RGB entry. Normalize render-only colors here;
5
+ // the source file is untouched because view reparses the document every time.
6
+ function makeEntityColorsRenderable(doc) {
7
+ for (const entity of doc.modelSpace?.entities ?? []) {
8
+ const candidate = entity;
9
+ if (candidate.color?.isByLayer || candidate.color?.isByBlock) {
10
+ candidate.color = Color.black;
11
+ }
12
+ }
13
+ }
14
+ export function renderSvgWithAcadTs(file) {
15
+ const { doc } = readAcadFile(file);
16
+ makeEntityColorsRenderable(doc);
17
+ return {
18
+ input: file,
19
+ svg: writeAcadSvg(doc),
20
+ backend: "acad-ts",
21
+ };
22
+ }
@@ -0,0 +1,11 @@
1
+ import { type CadDocument } from "@node-projects/acad-ts";
2
+ import type { DwgFormat } from "../types.js";
3
+ export interface AcadFile {
4
+ bytes: Uint8Array;
5
+ doc: CadDocument;
6
+ format: DwgFormat;
7
+ }
8
+ export declare function parseAcadDocument(bytes: Uint8Array, format: DwgFormat): CadDocument;
9
+ export declare function readAcadFile(file: string): AcadFile;
10
+ export declare function writeAcadDocument(doc: CadDocument, format: DwgFormat, originalBytes: number): Uint8Array;
11
+ export declare function writeAcadSvg(doc: CadDocument): string;
@@ -0,0 +1,82 @@
1
+ import { DwgReader, DwgWriter, DxfReader, DxfWriter, SvgWriter, } from "@node-projects/acad-ts";
2
+ import { EXIT_USER_ERROR } from "../utils/exit-codes.js";
3
+ import { arrayBufferFor } from "./adapter.js";
4
+ import { DwgCliError } from "./errors.js";
5
+ import { readCadFile } from "./files.js";
6
+ export function parseAcadDocument(bytes, format) {
7
+ try {
8
+ return format === "DWG"
9
+ ? DwgReader.readFromStream(arrayBufferFor(bytes))
10
+ : DxfReader.readFromStream(bytes);
11
+ }
12
+ catch (error) {
13
+ throw new DwgCliError(`Could not parse ${format}: ${error.message}`, "PARSE_FAILED", EXIT_USER_ERROR);
14
+ }
15
+ }
16
+ export function readAcadFile(file) {
17
+ const { bytes, format } = readCadFile(file);
18
+ return { bytes, format, doc: parseAcadDocument(bytes, format) };
19
+ }
20
+ function writeDwg(doc, originalBytes) {
21
+ let size = Math.max(1024 * 1024, originalBytes * 4);
22
+ const maxSize = 512 * 1024 * 1024;
23
+ while (size <= maxSize) {
24
+ const buffer = new ArrayBuffer(size);
25
+ const writer = new DwgWriter(buffer, doc);
26
+ writer.write();
27
+ if (writer.bytesWritten <= size) {
28
+ return new Uint8Array(buffer).slice(0, writer.bytesWritten);
29
+ }
30
+ size *= 2;
31
+ }
32
+ throw new DwgCliError("Could not write DWG: output exceeded the maximum supported buffer size.", "ACAD_WRITE_FAILED", EXIT_USER_ERROR);
33
+ }
34
+ function writeDxf(doc) {
35
+ let content = "";
36
+ DxfWriter.writeToStream({
37
+ write(value) {
38
+ content +=
39
+ typeof value === "string" ? value : new TextDecoder().decode(value);
40
+ },
41
+ flush() { },
42
+ close() { },
43
+ }, doc);
44
+ return new TextEncoder().encode(content);
45
+ }
46
+ export function writeAcadDocument(doc, format, originalBytes) {
47
+ try {
48
+ return format === "DWG" ? writeDwg(doc, originalBytes) : writeDxf(doc);
49
+ }
50
+ catch (error) {
51
+ if (error instanceof DwgCliError)
52
+ throw error;
53
+ throw new DwgCliError(`Could not write ${format}: ${error.message}`, "ACAD_WRITE_FAILED", EXIT_USER_ERROR);
54
+ }
55
+ }
56
+ function trimNullPadding(bytes) {
57
+ const end = bytes.indexOf(0);
58
+ return end === -1 ? bytes : bytes.slice(0, end);
59
+ }
60
+ function isOutputCapacityError(error) {
61
+ return /offset is out of bounds|too small|outside the bounds/i.test(error.message);
62
+ }
63
+ export function writeAcadSvg(doc) {
64
+ let size = 1024 * 1024;
65
+ const maxSize = 256 * 1024 * 1024;
66
+ while (size <= maxSize) {
67
+ const output = new Uint8Array(size);
68
+ try {
69
+ const writer = new SvgWriter(output, doc);
70
+ writer.write();
71
+ writer.dispose();
72
+ return new TextDecoder().decode(trimNullPadding(output));
73
+ }
74
+ catch (error) {
75
+ if (!isOutputCapacityError(error) || size >= maxSize) {
76
+ throw new DwgCliError(`Could not render SVG with acad-ts: ${error.message}`, "ACAD_SVG_FAILED", EXIT_USER_ERROR);
77
+ }
78
+ size *= 2;
79
+ }
80
+ }
81
+ throw new DwgCliError("Could not render SVG with acad-ts: output exceeded the maximum supported buffer size.", "ACAD_SVG_FAILED", EXIT_USER_ERROR);
82
+ }
@@ -6,9 +6,3 @@ export declare class AcadTsReader implements DrawingReader {
6
6
  parse(_file: string, bytes: Uint8Array, format: DwgFormat): Promise<unknown>;
7
7
  thumbnail(): Promise<ThumbnailResult | null>;
8
8
  }
9
- export declare class NativeLibreDwgReader implements DrawingReader {
10
- private readonly toolDir?;
11
- constructor(toolDir?: string | undefined);
12
- parse(file: string, _bytes: Uint8Array, _format: DwgFormat): Promise<unknown>;
13
- thumbnail(): Promise<ThumbnailResult | null>;
14
- }
@@ -1,7 +1,6 @@
1
1
  import { DwgReader, DxfReader } from "@node-projects/acad-ts";
2
2
  import { EXIT_UNAVAILABLE, EXIT_USER_ERROR } from "../utils/exit-codes.js";
3
3
  import { DwgCliError } from "./errors.js";
4
- import { readJsonWithLibreDwg } from "./libredwg.js";
5
4
  function asRecord(value) {
6
5
  return value && typeof value === "object"
7
6
  ? value
@@ -67,6 +66,14 @@ function blockName(entity) {
67
66
  const block = asRecord(entity).block;
68
67
  return valueString(asRecord(block).name);
69
68
  }
69
+ function textStyle(entity) {
70
+ const style = asRecord(entity)._style ?? asRecord(entity).style;
71
+ const rec = asRecord(style);
72
+ return {
73
+ name: valueString(rec.name ?? rec._name),
74
+ file: valueString(rec.filename),
75
+ };
76
+ }
70
77
  function vertices(entity) {
71
78
  const raw = asRecord(entity).vertices;
72
79
  if (!Array.isArray(raw))
@@ -113,8 +120,14 @@ function normalizeAcadEntity(entity) {
113
120
  data.startAngle = rec.startAngle;
114
121
  if (typeof rec.endAngle === "number")
115
122
  data.endAngle = rec.endAngle;
116
- if (text)
123
+ if (text) {
124
+ const style = textStyle(entity);
117
125
  data.text = text;
126
+ if (style.name)
127
+ data.textStyle = style.name;
128
+ if (style.file)
129
+ data.textStyleFile = style.file;
130
+ }
118
131
  if (name)
119
132
  data.blockName = name;
120
133
  return data;
@@ -130,6 +143,7 @@ export function normalizeAcadDocument(doc) {
130
143
  const entities = items(doc.modelSpace?.entities).map(normalizeAcadEntity);
131
144
  return {
132
145
  version: doc.header?.versionString ?? String(doc.header?.version ?? ""),
146
+ codePage: doc.header?.codePage,
133
147
  layers,
134
148
  blocks,
135
149
  entities,
@@ -151,15 +165,3 @@ export class AcadTsReader {
151
165
  throw new DwgCliError("Thumbnail extraction is not available through acad-ts.", "THUMBNAIL_UNAVAILABLE", EXIT_UNAVAILABLE);
152
166
  }
153
167
  }
154
- export class NativeLibreDwgReader {
155
- toolDir;
156
- constructor(toolDir) {
157
- this.toolDir = toolDir;
158
- }
159
- async parse(file, _bytes, _format) {
160
- return readJsonWithLibreDwg(file, { toolDir: this.toolDir }).json;
161
- }
162
- async thumbnail() {
163
- throw new DwgCliError("Thumbnail extraction is not available through native LibreDWG.", "THUMBNAIL_UNAVAILABLE", EXIT_UNAVAILABLE);
164
- }
165
- }
@@ -1,7 +1,6 @@
1
1
  import type { DrawingReader, DwgBlock, DwgDocument, DwgEntity, DwgLayer, EntityFilter, SvgResult, ThumbnailResult } from "../types.js";
2
2
  export interface LoadOptions {
3
3
  reader?: DrawingReader;
4
- toolDir?: string;
5
4
  }
6
5
  export declare function loadDrawing(file: string, opts?: LoadOptions): Promise<DwgDocument>;
7
6
  export declare function getInfo(file: string, opts?: LoadOptions): Promise<import("../types.js").DwgSummary>;
@@ -4,13 +4,14 @@ import { DwgCliError } from "./errors.js";
4
4
  import { readCadFile } from "./files.js";
5
5
  import { normalizeDocument } from "./normalize.js";
6
6
  import { renderSvg } from "./svg.js";
7
+ import { normalizeTextAuto } from "./text-normalize.js";
7
8
  function readerFor(opts) {
8
9
  return opts.reader ?? new AcadTsReader();
9
10
  }
10
11
  export async function loadDrawing(file, opts = {}) {
11
12
  const { bytes, format } = readCadFile(file);
12
13
  const raw = await readerFor(opts).parse(file, bytes, format);
13
- return normalizeDocument(file, format, raw);
14
+ return normalizeTextAuto(normalizeDocument(file, format, raw));
14
15
  }
15
16
  export async function getInfo(file, opts) {
16
17
  return (await loadDrawing(file, opts)).summary;
@@ -140,6 +140,9 @@ export function normalizeDocument(file, format, raw) {
140
140
  const layers = normalizeLayers(firstArray(root, LAYER_ARRAY_FIELDS), entities);
141
141
  const blocks = normalizeBlocks(firstArray(root, BLOCK_ARRAY_FIELDS));
142
142
  const version = stringField(root, ["version", "headerVersion", "dwgVersion"]);
143
+ const metadata = {
144
+ codePage: stringField(root, ["codePage", "encoding"]),
145
+ };
143
146
  const summary = {
144
147
  file: basename(file),
145
148
  format,
@@ -152,5 +155,5 @@ export function normalizeDocument(file, format, raw) {
152
155
  },
153
156
  bounds: computeBounds(entities),
154
157
  };
155
- return { summary, layers, blocks, entities, unsupported, raw };
158
+ return { summary, metadata, layers, blocks, entities, unsupported, raw };
156
159
  }
@@ -0,0 +1,17 @@
1
+ import type { DwgDocument } from "../types.js";
2
+ export interface QuerySchemaColumn {
3
+ name: string;
4
+ type: "text" | "integer" | "real";
5
+ }
6
+ export interface QuerySchemaTable {
7
+ name: string;
8
+ description: string;
9
+ columns: QuerySchemaColumn[];
10
+ }
11
+ export interface QueryResult {
12
+ columns: string[];
13
+ rows: Record<string, unknown>[];
14
+ }
15
+ export declare function querySchema(): string;
16
+ export declare function querySchemaTables(): QuerySchemaTable[];
17
+ export declare function queryDrawing(doc: DwgDocument, sql: string): Promise<QueryResult>;