tsdraft 0.1.0 → 0.2.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,50 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0
4
+
5
+ 0.1.1 was never published, so its changes ship here too.
6
+
7
+ Every fix below comes from opening tsdraft's output in AutoCAD 2025.
8
+
9
+ - `createDrawing({ dxf: "R2018" })` targets AC1032, the DXF version AutoCAD has
10
+ used since 2018. It writes handles, owner links, the standard tables, model
11
+ and paper space blocks, plot styles and `$INSUNITS`. R12 stays the default.
12
+ `toDXF(drawing)` writes the drawing's version, and
13
+ `toDXF(drawing, { version })` writes another.
14
+ - Layer names, layer colors, radii, text heights and rectangle sizes written as
15
+ literals are checked by the compiler. The same checks run when the code runs.
16
+ - `lint(drawing, { version })` returns every problem in a drawing. `toDXF`
17
+ throws a `DrawingError` listing the errors instead of writing a file AutoCAD
18
+ would reject.
19
+ - Fixed: AutoCAD discarded R12 drawings whose layer names had spaces, dots or
20
+ other punctuation, or more than 31 characters. Those names are now errors in
21
+ an R12 drawing and allowed in an R2018 drawing.
22
+ - Fixed: a layer name containing a line break wrote extra records into the
23
+ file. Commas, carets and leading or trailing spaces are rejected too.
24
+ - Fixed: AutoCAD discarded any drawing with a `^` in its text. A caret is now
25
+ escaped.
26
+ - Fixed: R12 text outside ASCII came out garbled. It is now written as
27
+ `\U+XXXX` escapes.
28
+ - Fixed: arc angles outside 0 to 360 failed `AUDIT`. Angles are now stored from
29
+ 0 up to 360.
30
+ - Fixed: `wall` and `WALL` were two layers, and AutoCAD kept only the first.
31
+ They are now one layer.
32
+ - Text with control characters, or with emoji and other characters above
33
+ U+FFFF, is rejected, because AutoCAD cannot read it.
34
+ - Unknown units, and rectangle sizes of 0 or less, are rejected.
35
+ - Breaking: a `Drawing` now has a required `dxf` field, and R12 layer names
36
+ AutoCAD rejects now throw (see above).
37
+ - `pnpm test:cad` runs the output through AutoCAD over ssh.
38
+ - Published from GitHub Actions through npm trusted publishing, with a
39
+ provenance attestation linking the package to its source commit.
40
+ - `tsdraft/package.json` can be imported or resolved.
41
+ - The package now includes this changelog.
42
+
43
+ ## 0.1.0
44
+
45
+ First release.
46
+
47
+ - `createDrawing()` builds a drawing with layers (AutoCAD Color Index colors),
48
+ lines, polylines, rectangles, circles, arcs and single-line text.
49
+ - `toDXF()` writes ASCII DXF R12 (AC1009) with layer and linetype tables.
50
+ - `toSVG()` writes an SVG preview with model Y pointing up.
package/README.md CHANGED
@@ -1,14 +1,18 @@
1
1
  # tsdraft
2
2
 
3
- Write 2D CAD drawings in TypeScript and export them as DXF for AutoCAD or
4
- BricsCAD, or as SVG for a quick preview. A drawing is a program, so dimensions
5
- can come from variables, sheets can share code, and changes show up as diffs.
6
- [tscircuit](https://github.com/tscircuit/tscircuit) takes the same approach to
7
- circuit boards.
3
+ Write 2D CAD drawings in TypeScript and export them as DXF, the exchange format
4
+ that AutoCAD, BricsCAD and most other CAD programs open, or as SVG for a quick
5
+ preview. A drawing is a program, so dimensions can come from variables, drawings
6
+ can share code, and changes show up as diffs.
8
7
 
9
- This is an early release. It has layers, lines, polylines, rectangles, circles,
10
- arcs and single-line text. It does not yet have dimensions, hatches, blocks,
11
- paper space or a JSX syntax.
8
+ ## Status
9
+
10
+ tsdraft is at an early stage. It draws layers, lines, polylines, rectangles,
11
+ circles, arcs and single-line text. It does not yet have dimensions, hatches,
12
+ blocks, linetypes, lineweights or paper space layouts. It writes DXF, not DWG.
13
+
14
+ The DXF output is tested by opening it in AutoCAD 2025 and running `AUDIT`. It
15
+ has not been tested in BricsCAD or any other CAD program yet.
12
16
 
13
17
  ## Install
14
18
 
@@ -42,10 +46,67 @@ Coordinates are in drawing units with Y pointing up, as in CAD. Angles are in
42
46
  degrees, counterclockwise from +X. Layer colors are AutoCAD Color Index numbers.
43
47
  Entities go on layer `0` unless they are drawn inside a `layer()` callback.
44
48
 
45
- `toDXF` writes ASCII DXF R12, because nearly every CAD program can open that
46
- version. R12 has no field for drawing units, so the file holds plain numbers.
47
- Set the units in CAD after opening it, for example with `UNITS` in AutoCAD or
48
- BricsCAD.
49
+ ## DXF versions
50
+
51
+ A drawing is written for one DXF version. `createDrawing()` targets R12, the
52
+ oldest version still in common use, which nearly every CAD program can open.
53
+ `createDrawing({ dxf: "R2018" })` targets the version AutoCAD has used since
54
+ 2018, which is the newest one Autodesk has published. `toDXF(drawing)` writes
55
+ the drawing's version, and `toDXF(drawing, { version })` writes another one.
56
+
57
+ | | `"R12"`, the default | `"R2018"` |
58
+ | ------------- | -------------------------------------------------- | ------------------------------------------------------ |
59
+ | Format code | AC1009 | AC1032 |
60
+ | Drawing units | Not stored. Set them in CAD with `UNITS`. | Stored in `$INSUNITS`. |
61
+ | Polylines | `POLYLINE` with `VERTEX` records | `LWPOLYLINE` |
62
+ | Text | ASCII, with `\U+XXXX` escapes for other characters | UTF-8 |
63
+ | Layer names | Up to 31 letters, digits, `$`, `-` and `_` | Up to 255 characters, with the exceptions listed below |
64
+
65
+ Choose R2018 when the drawing will be inserted into another drawing, because CAD
66
+ needs the stored units to scale it correctly. AutoCAD shows layer names from an
67
+ R12 file in upper case.
68
+
69
+ ## Checks
70
+
71
+ AutoCAD discards a whole drawing over one bad layer name, and it reports no
72
+ line of your code when it does. tsdraft checks the rules AutoCAD enforces at
73
+ three points, so the mistake shows up where you made it.
74
+
75
+ 1. **In the editor.** Layer names, colors and sizes written as literals are
76
+ checked by the TypeScript compiler, and the error message says what is wrong.
77
+ 2. **When the code runs.** The builder throws at the call that breaks a rule.
78
+ This covers values the compiler cannot see, such as a name read from a file.
79
+ 3. **Before export.** `lint(drawing)` returns every problem in a drawing, and
80
+ `toDXF` throws a `DrawingError` that lists them instead of writing a file
81
+ AutoCAD would reject.
82
+
83
+ ```ts
84
+ const d = createDrawing({ units: "ft" });
85
+
86
+ d.layer("A WALL");
87
+ // error TS2345: Argument of type '"A WALL"' is not assignable to parameter of
88
+ // type '"R12 layer names can only use letters, digits, $, - and _. Use DXF
89
+ // version \"R2018\" for other characters"'.
90
+
91
+ createDrawing({ dxf: "R2018" }).layer("A WALL"); // fine
92
+
93
+ lint(drawing, { version: "R12" }); // check a drawing against another version
94
+ ```
95
+
96
+ The rules:
97
+
98
+ - Layer names cannot be empty, start or end with a space, or contain a line
99
+ break or any of ``< > / \ " : ; ? * | = ` ^ ,``. R12 names have the tighter
100
+ limits in the table above.
101
+ - `wall` and `WALL` are the same layer, as in CAD. The first spelling is kept.
102
+ - Layer colors are whole numbers from 1 to 255.
103
+ - Radii, text heights and rectangle sizes are greater than 0.
104
+ - Text is a single line. It cannot contain emoji or other characters above
105
+ U+FFFF, because AutoCAD cannot read them from DXF.
106
+ - Arc angles are stored from 0 up to 360, so -90 becomes 270.
107
+
108
+ `lint` also returns warnings, such as a line with no length. Warnings do not
109
+ stop an export.
49
110
 
50
111
  ## Development
51
112
 
@@ -55,9 +116,16 @@ directly, so tests and examples need no build step.
55
116
  ```sh
56
117
  pnpm install
57
118
  pnpm verify # format check, lint, type check, tests, package smoke test
58
- pnpm example # writes examples/out/site-plan.dxf and .svg
119
+ pnpm example # writes a site plan to examples/out/ as DXF and SVG
59
120
  ```
60
121
 
122
+ `pnpm test:cad` opens the output in AutoCAD. It needs
123
+ `TSDRAFT_CAD_SSH=user@host`, an ssh login to a WSL shell on a Windows machine
124
+ that has AutoCAD. Each test drawing runs in AutoCAD's headless console, which
125
+ runs `AUDIT` and saves the drawing back as DXF. The script then compares that
126
+ file with what was drawn. Run it after any change to a DXF writer, because
127
+ AutoCAD rejects files that DXF parsing libraries accept.
128
+
61
129
  `pnpm test:package` packs the package, installs the tarball into a throwaway
62
130
  project and checks that it type-checks and runs there. `pnpm build` compiles
63
131
  `src/` to `dist/`, and `npm pack` and `npm publish` run it automatically.
@@ -68,6 +136,16 @@ as `pnpm check:tsrs` and as an informational CI step. It tracks TypeScript 7.1
68
136
  beta, so it does not gate releases yet. Its WebAssembly build is the planned
69
137
  route for type-checking drawings in a browser playground.
70
138
 
139
+ ## Releasing
140
+
141
+ Bump `version` in `package.json` and add a CHANGELOG entry in a pull request.
142
+ After it merges, publish a GitHub release tagged `v<version>` on the merge
143
+ commit. The Release workflow checks that the tag matches the version, runs
144
+ `pnpm verify` and publishes to npm through
145
+ [trusted publishing](https://docs.npmjs.com/trusted-publishers), so no npm token
146
+ is stored anywhere. npm adds a provenance attestation that links each version to
147
+ the commit it was built from.
148
+
71
149
  ## License
72
150
 
73
151
  [MIT](LICENSE)
package/dist/drawing.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { type LayerColor, type LayerName, type Positive } from "./rules.ts";
1
2
  /** A model-space point, in drawing units. Y points up, as in CAD. */
2
3
  export type Point = readonly [x: number, y: number];
3
4
  /**
@@ -5,6 +6,15 @@ export type Point = readonly [x: number, y: number];
5
6
  * the DXF R12 writer has no field for it, so set units in CAD after opening.
6
7
  */
7
8
  export type Units = "in" | "ft" | "mm" | "cm" | "m";
9
+ /**
10
+ * DXF versions tsdraft writes.
11
+ *
12
+ * - `"R12"` (AC1009) opens in nearly every CAD program, but stores no units and
13
+ * limits layer names to 31 letters, digits, `$`, `-` and `_`.
14
+ * - `"R2018"` (AC1032) is the newest published version and the one AutoCAD has
15
+ * used since 2018. It records the drawing's units.
16
+ */
17
+ export type DxfVersion = "R12" | "R2018";
8
18
  export interface Layer {
9
19
  readonly name: string;
10
20
  /** AutoCAD Color Index, 1 to 255. 7 is white on dark backgrounds, black on light. */
@@ -28,7 +38,7 @@ export interface Circle extends EntityBase {
28
38
  readonly center: Point;
29
39
  readonly radius: number;
30
40
  }
31
- /** Angles are in degrees, counterclockwise from +X, as in DXF. */
41
+ /** Angles are in degrees from 0 up to 360, counterclockwise from +X, as in DXF. */
32
42
  export interface Arc extends EntityBase {
33
43
  readonly kind: "arc";
34
44
  readonly center: Point;
@@ -48,39 +58,59 @@ export interface Text extends EntityBase {
48
58
  export type Entity = Line | Polyline | Circle | Arc | Text;
49
59
  export interface Drawing {
50
60
  readonly units: Units;
61
+ /** The DXF version the drawing is written for. `toDXF` uses it unless told otherwise. */
62
+ readonly dxf: DxfVersion;
51
63
  readonly layers: readonly Layer[];
52
64
  readonly entities: readonly Entity[];
53
65
  }
54
- export interface DrawingOptions {
66
+ export interface DrawingOptions<V extends DxfVersion = DxfVersion> {
67
+ /** Defaults to `"in"`. */
55
68
  readonly units?: Units;
69
+ /** The DXF version to check the drawing against and export. Defaults to `"R12"`. */
70
+ readonly dxf?: V;
56
71
  }
57
- export interface LayerOptions {
58
- readonly color?: number;
72
+ export interface LayerOptions<C extends number = number> {
73
+ /** AutoCAD Color Index, 1 to 255. */
74
+ readonly color?: LayerColor<C>;
59
75
  }
60
- export interface TextOptions {
61
- readonly height?: number;
76
+ export interface TextOptions<H extends number = number> {
77
+ readonly height?: Positive<H>;
78
+ /** Degrees counterclockwise. */
62
79
  readonly rotation?: number;
63
80
  }
64
81
  /**
65
82
  * Builds a drawing. Entities go on the current layer, which starts as "0".
66
83
  * `layer(name, opts, draw)` scopes everything drawn inside `draw` to that layer.
84
+ *
85
+ * Mistakes written as literals, such as a layer name the DXF version does not
86
+ * allow or a negative radius, are compile errors. The same checks run again
87
+ * when the code runs, for values the compiler cannot see.
67
88
  */
68
- export declare class DrawingBuilder {
89
+ export declare class DrawingBuilder<V extends DxfVersion = "R12"> {
69
90
  #private;
70
91
  readonly units: Units;
71
- constructor(options?: DrawingOptions);
72
- /** Defines or updates a layer. With `draw`, entities drawn inside it go on this layer. */
73
- layer(name: string, options?: LayerOptions, draw?: () => void): this;
92
+ readonly dxf: V;
93
+ constructor(options?: DrawingOptions<V>);
94
+ /**
95
+ * Defines or updates a layer. With `draw`, entities drawn inside it go on this layer.
96
+ * `wall` and `WALL` are the same layer, as in CAD, and keep the first spelling.
97
+ */
98
+ layer<N extends string, C extends number = number>(name: LayerName<N, V>, options?: LayerOptions<C>, draw?: () => void): this;
74
99
  line(from: Point, to: Point): this;
75
100
  polyline(points: readonly Point[], closed?: boolean): this;
76
101
  /** Closed rectangle from its lower-left corner. */
77
- rect(corner: Point, width: number, height: number): this;
78
- circle(center: Point, radius: number): this;
79
- arc(center: Point, radius: number, startAngle: number, endAngle: number): this;
80
- text(at: Point, value: string, options?: TextOptions): this;
102
+ rect<W extends number, H extends number>(corner: Point, width: Positive<W>, height: Positive<H>): this;
103
+ circle<R extends number>(center: Point, radius: Positive<R>): this;
104
+ /**
105
+ * Arc from `startAngle` counterclockwise to `endAngle`. Angles are stored
106
+ * from 0 up to 360, so -90 becomes 270, because AutoCAD's AUDIT rejects
107
+ * angles outside that range.
108
+ */
109
+ arc<R extends number>(center: Point, radius: Positive<R>, startAngle: number, endAngle: number): this;
110
+ text<H extends number = number>(at: Point, value: string, options?: TextOptions<H>): this;
81
111
  build(): Drawing;
82
112
  }
83
- export declare function createDrawing(options?: DrawingOptions): DrawingBuilder;
113
+ export declare function createDrawing<V extends DxfVersion = "R12">(options?: DrawingOptions<V>): DrawingBuilder<V>;
84
114
  /** Counterclockwise sweep from start to end, in degrees, within [0, 360). */
85
115
  export declare function sweep(startAngle: number, endAngle: number): number;
86
116
  export {};
package/dist/drawing.js CHANGED
@@ -1,28 +1,53 @@
1
+ import { layerColorProblem, layerNameProblem, textProblem, } from "./rules.js";
2
+ const UNITS = ["in", "ft", "mm", "cm", "m"];
3
+ const DXF_VERSIONS = ["R12", "R2018"];
1
4
  /**
2
5
  * Builds a drawing. Entities go on the current layer, which starts as "0".
3
6
  * `layer(name, opts, draw)` scopes everything drawn inside `draw` to that layer.
7
+ *
8
+ * Mistakes written as literals, such as a layer name the DXF version does not
9
+ * allow or a negative radius, are compile errors. The same checks run again
10
+ * when the code runs, for values the compiler cannot see.
4
11
  */
5
12
  export class DrawingBuilder {
6
13
  units;
14
+ dxf;
15
+ /** Keyed by lower-cased name, because CAD ignores case in layer names. */
7
16
  #layers = new Map([["0", { name: "0", color: 7 }]]);
8
17
  #entities = [];
9
18
  #current = "0";
10
19
  constructor(options = {}) {
11
- this.units = options.units ?? "in";
20
+ const units = options.units ?? "in";
21
+ if (!UNITS.includes(units))
22
+ throw new Error(`Unknown units: ${JSON.stringify(units)}`);
23
+ const dxf = options.dxf ?? "R12";
24
+ if (!DXF_VERSIONS.includes(dxf)) {
25
+ throw new Error(`Unsupported DXF version: ${JSON.stringify(dxf)}`);
26
+ }
27
+ this.units = units;
28
+ this.dxf = dxf;
12
29
  }
13
- /** Defines or updates a layer. With `draw`, entities drawn inside it go on this layer. */
30
+ /**
31
+ * Defines or updates a layer. With `draw`, entities drawn inside it go on this layer.
32
+ * `wall` and `WALL` are the same layer, as in CAD, and keep the first spelling.
33
+ */
14
34
  layer(name, options = {}, draw) {
15
- if (!/^[^<>/\\":;?*|=`]{1,255}$/.test(name)) {
16
- throw new Error(`Invalid layer name: ${JSON.stringify(name)}`);
17
- }
18
- const color = options.color ?? this.#layers.get(name)?.color ?? 7;
19
- if (!Number.isInteger(color) || color < 1 || color > 255) {
20
- throw new Error(`Layer ${name}: color must be an ACI integer from 1 to 255`);
21
- }
22
- this.#layers.set(name, { name, color });
35
+ // The parameter type is the error message when a literal name is wrong, so read it as a string.
36
+ const requested = name;
37
+ const nameProblem = layerNameProblem(requested, this.dxf);
38
+ if (nameProblem)
39
+ throw new Error(`Layer ${JSON.stringify(requested)}: ${nameProblem}`);
40
+ const key = requested.toLowerCase();
41
+ const existing = this.#layers.get(key);
42
+ const color = options.color ?? existing?.color ?? 7;
43
+ const colorProblem = layerColorProblem(color);
44
+ if (colorProblem)
45
+ throw new Error(`Layer ${JSON.stringify(requested)}: ${colorProblem}`);
46
+ const layer = { name: existing?.name ?? requested, color };
47
+ this.#layers.set(key, layer);
23
48
  if (draw) {
24
49
  const previous = this.#current;
25
- this.#current = name;
50
+ this.#current = layer.name;
26
51
  try {
27
52
  draw();
28
53
  }
@@ -43,13 +68,13 @@ export class DrawingBuilder {
43
68
  /** Closed rectangle from its lower-left corner. */
44
69
  rect(corner, width, height) {
45
70
  const [x, y] = pt(corner);
46
- num(width, "width");
47
- num(height, "height");
71
+ const w = positive(width, "width");
72
+ const h = positive(height, "height");
48
73
  return this.polyline([
49
74
  [x, y],
50
- [x + width, y],
51
- [x + width, y + height],
52
- [x, y + height],
75
+ [x + w, y],
76
+ [x + w, y + h],
77
+ [x, y + h],
53
78
  ], true);
54
79
  }
55
80
  circle(center, radius) {
@@ -60,23 +85,29 @@ export class DrawingBuilder {
60
85
  radius: positive(radius, "radius"),
61
86
  });
62
87
  }
88
+ /**
89
+ * Arc from `startAngle` counterclockwise to `endAngle`. Angles are stored
90
+ * from 0 up to 360, so -90 becomes 270, because AutoCAD's AUDIT rejects
91
+ * angles outside that range.
92
+ */
63
93
  arc(center, radius, startAngle, endAngle) {
64
- num(startAngle, "startAngle");
65
- num(endAngle, "endAngle");
66
- if (sweep(startAngle, endAngle) === 0)
94
+ const start = turn(num(startAngle, "startAngle"));
95
+ const end = turn(num(endAngle, "endAngle"));
96
+ if (start === end)
67
97
  throw new Error("An arc's start and end angles must differ");
68
98
  return this.#add({
69
99
  kind: "arc",
70
100
  layer: this.#current,
71
101
  center: pt(center),
72
102
  radius: positive(radius, "radius"),
73
- startAngle,
74
- endAngle,
103
+ startAngle: start,
104
+ endAngle: end,
75
105
  });
76
106
  }
77
107
  text(at, value, options = {}) {
78
- if (/[\r\n]/.test(value))
79
- throw new Error("Text must be a single line");
108
+ const problem = textProblem(value);
109
+ if (problem)
110
+ throw new Error(problem);
80
111
  return this.#add({
81
112
  kind: "text",
82
113
  layer: this.#current,
@@ -87,7 +118,12 @@ export class DrawingBuilder {
87
118
  });
88
119
  }
89
120
  build() {
90
- return { units: this.units, layers: [...this.#layers.values()], entities: [...this.#entities] };
121
+ return {
122
+ units: this.units,
123
+ dxf: this.dxf,
124
+ layers: [...this.#layers.values()],
125
+ entities: [...this.#entities],
126
+ };
91
127
  }
92
128
  #add(entity) {
93
129
  this.#entities.push(entity);
@@ -99,15 +135,25 @@ export function createDrawing(options) {
99
135
  }
100
136
  /** Counterclockwise sweep from start to end, in degrees, within [0, 360). */
101
137
  export function sweep(startAngle, endAngle) {
102
- return (((endAngle - startAngle) % 360) + 360) % 360;
138
+ return turn(endAngle - startAngle);
139
+ }
140
+ /** Brings an angle in degrees into [0, 360). */
141
+ function turn(degrees) {
142
+ const wrapped = ((degrees % 360) + 360) % 360;
143
+ // A tiny negative angle rounds up to exactly 360.
144
+ return wrapped === 360 ? 0 : wrapped;
103
145
  }
104
146
  function num(value, label) {
105
147
  if (!Number.isFinite(value))
106
148
  throw new Error(`${label} must be a finite number`);
107
149
  return value;
108
150
  }
151
+ /** Takes `unknown` because a `Positive<N>` parameter is a message string when the literal is wrong. */
109
152
  function positive(value, label) {
110
- if (num(value, label) <= 0)
153
+ if (typeof value !== "number" || !Number.isFinite(value)) {
154
+ throw new Error(`${label} must be a finite number`);
155
+ }
156
+ if (value <= 0)
111
157
  throw new Error(`${label} must be greater than 0`);
112
158
  return value;
113
159
  }
@@ -0,0 +1,10 @@
1
+ import type { Drawing } from "./drawing.ts";
2
+ /**
3
+ * Writes an ASCII DXF (AC1009, R12). R12 is the most widely readable DXF:
4
+ * AutoCAD, BricsCAD, LibreCAD, QCAD and most converters open it.
5
+ *
6
+ * R12 has no header variable for drawing units, so coordinates are written as
7
+ * plain numbers in the drawing's units and `drawing.units` is not written.
8
+ * R12 predates Unicode DXF, so text outside ASCII is written as `\U+XXXX`.
9
+ */
10
+ export declare function toDXFR12(drawing: Drawing): string;
@@ -0,0 +1,113 @@
1
+ import { dxfText, fmt } from "./format.js";
2
+ /**
3
+ * Writes an ASCII DXF (AC1009, R12). R12 is the most widely readable DXF:
4
+ * AutoCAD, BricsCAD, LibreCAD, QCAD and most converters open it.
5
+ *
6
+ * R12 has no header variable for drawing units, so coordinates are written as
7
+ * plain numbers in the drawing's units and `drawing.units` is not written.
8
+ * R12 predates Unicode DXF, so text outside ASCII is written as `\U+XXXX`.
9
+ */
10
+ export function toDXFR12(drawing) {
11
+ const out = [];
12
+ const g = (code, value) => {
13
+ out.push(String(code), typeof value === "number" ? fmt(value) : value);
14
+ };
15
+ g(0, "SECTION");
16
+ g(2, "HEADER");
17
+ g(9, "$ACADVER");
18
+ g(1, "AC1009");
19
+ g(0, "ENDSEC");
20
+ g(0, "SECTION");
21
+ g(2, "TABLES");
22
+ g(0, "TABLE");
23
+ g(2, "LTYPE");
24
+ g(70, "1");
25
+ g(0, "LTYPE");
26
+ g(2, "CONTINUOUS");
27
+ g(70, "0");
28
+ g(3, "Solid line");
29
+ g(72, "65");
30
+ g(73, "0");
31
+ g(40, "0.0");
32
+ g(0, "ENDTAB");
33
+ g(0, "TABLE");
34
+ g(2, "LAYER");
35
+ g(70, String(drawing.layers.length));
36
+ for (const layer of drawing.layers) {
37
+ g(0, "LAYER");
38
+ g(2, layer.name);
39
+ g(70, "0");
40
+ g(62, String(layer.color));
41
+ g(6, "CONTINUOUS");
42
+ }
43
+ g(0, "ENDTAB");
44
+ g(0, "ENDSEC");
45
+ g(0, "SECTION");
46
+ g(2, "ENTITIES");
47
+ for (const entity of drawing.entities)
48
+ writeEntity(entity, g);
49
+ g(0, "ENDSEC");
50
+ g(0, "EOF");
51
+ return out.join("\r\n") + "\r\n";
52
+ }
53
+ function writeEntity(e, g) {
54
+ switch (e.kind) {
55
+ case "line":
56
+ g(0, "LINE");
57
+ g(8, e.layer);
58
+ g(10, e.from[0]);
59
+ g(20, e.from[1]);
60
+ g(30, 0);
61
+ g(11, e.to[0]);
62
+ g(21, e.to[1]);
63
+ g(31, 0);
64
+ return;
65
+ case "polyline":
66
+ g(0, "POLYLINE");
67
+ g(8, e.layer);
68
+ g(66, "1");
69
+ g(10, 0);
70
+ g(20, 0);
71
+ g(30, 0);
72
+ g(70, e.closed ? "1" : "0");
73
+ for (const [x, y] of e.points) {
74
+ g(0, "VERTEX");
75
+ g(8, e.layer);
76
+ g(10, x);
77
+ g(20, y);
78
+ g(30, 0);
79
+ }
80
+ g(0, "SEQEND");
81
+ g(8, e.layer);
82
+ return;
83
+ case "circle":
84
+ g(0, "CIRCLE");
85
+ g(8, e.layer);
86
+ g(10, e.center[0]);
87
+ g(20, e.center[1]);
88
+ g(30, 0);
89
+ g(40, e.radius);
90
+ return;
91
+ case "arc":
92
+ g(0, "ARC");
93
+ g(8, e.layer);
94
+ g(10, e.center[0]);
95
+ g(20, e.center[1]);
96
+ g(30, 0);
97
+ g(40, e.radius);
98
+ g(50, e.startAngle);
99
+ g(51, e.endAngle);
100
+ return;
101
+ case "text":
102
+ g(0, "TEXT");
103
+ g(8, e.layer);
104
+ g(10, e.at[0]);
105
+ g(20, e.at[1]);
106
+ g(30, 0);
107
+ g(40, e.height);
108
+ g(1, dxfText(e.value, true));
109
+ if (e.rotation !== 0)
110
+ g(50, e.rotation);
111
+ return;
112
+ }
113
+ }
@@ -0,0 +1,10 @@
1
+ import type { Drawing } from "./drawing.ts";
2
+ /**
3
+ * Writes an ASCII DXF for AutoCAD 2018 and later (AC1032), the newest DXF
4
+ * version Autodesk has published. AutoCAD 2027 still reads and writes AC1032.
5
+ *
6
+ * Unlike R12, this version has handles, owner links, an OBJECTS section and the
7
+ * `$INSUNITS` header, so the drawing's units reach CAD and xref and block
8
+ * insertion scale correctly. Text is UTF-8.
9
+ */
10
+ export declare function toDXFR2018(drawing: Drawing): string;