tsdraft

tsdraft docs

Editing a firm's drawing

An edit changes a drawing file a firm already has, and keeps the rest of it as the file holds it. It names the file by its path and by the SHA-256 of the bytes it was planned on, so it is never made on other bytes, and it is written to an edited copy, never over the firm's file.

This version erases, moves, copies, mirrors, changes, stretches, sets a polyline's vertices, adds, and adds a row to a schedule: e.erase(...handles), e.move(handles, transform), e.copy(handles, transform), e.mirror(handles, line), e.change(handle, changes), e.layer(name, changes), e.viewport(handle, changes), e.stretch(handles, { window, by }), e.vertices(handle, { points, bulges }), e.model(draw), e.layout(name, draw), e.row(schedule, row), and a revision's clouds, delta and row: e.revision(record), e.cloud(around, options) and e.delta(at, options). Later versions build on this footing.

The edit file

An edit file is a TypeScript module whose default export is editDrawing's:

// rev-1.edit.ts
import { editDrawing } from "tsdraft";

export default editDrawing({
  file: "../1_Model/FLOOR PLAN.dxf",
  sha256: "9c1f…", // the file's, as `DrawingRead.sha256` gives it
  name: "rev-1",
});
  • file is the path from the edit file.
  • sha256 is the hash of the bytes the edit was planned on.
  • name holds 1 to 64 letters, digits, - and _.
  • sheets names the sheets that show a model file through an xref, by their paths from the edit file. units gives the units of a file whose $INSUNITS is 0, and standard the standard that judges what an edit adds.

A second argument, (e, read) => { ... }, states the edit. read is the file's read with geometry, read only when the function first uses it. The function may run more than once, such as once to find what it reads and once with the read, so it must give the same edit each time and change nothing outside it.

editDrawing returns an EditFile, { source, build }, and build(read) gives the Edit: a value that goes through JSON as a Drawing does, so a process or a service can take one with the file's bytes. checkEdit(value) says what is wrong with a value meant as one, as checkDrawingRead does for a read, and stays bounded on any value. EDIT_FORMAT is 0, and like the read model it is not settled.

Erase and move

export default editDrawing(
  { file: "../1_Model/FLOOR PLAN.dxf", sha256: "9c1f…", name: "rev-1" },
  (e) => {
    e.erase("436D8", "436D9");
    e.move(["4360A", "4360C"], { by: [0, -14.25] });
    e.move(["4361E"], { about: [1204, 880], rotate: 90, scale: 0.5 });
  },
);

A handle is the hexadecimal digits the read gives, in any case. e.erase(...handles) erases each entity with what it owns: an insert's attributes, a 2D polyline's vertices, a dimension's or a table's picture, kept where another record names its block too, its extension dictionary, an image's reactor and a text's fields. Each reference to what goes is taken out: from a group's list, which is kept when it is left empty, from a sort table, from another object's reactors, from a block's list of its inserts and from the list of fields. An OLE2FRAME a space's unread lists, whose data CAD programs would refuse or could not place, may be erased, and nothing else of a kind the reader does not read.

e.move(handles, transform) scales each entity by scale and turns it by rotate degrees counterclockwise, both about about, then moves it by by. A turn or a scale names about. Every point, angle and size of the entity moves: an insert with its attributes, a 2D polyline with its vertices, a dimension with its picture, moved only, a multileader, a hatch with its loops and its pattern, an image and a wipeout with their vectors, an OLE frame by the corners its data holds and its groups 10 and 11, moved and scaled only, as CAD programs keep one upright, and a viewport on its paper, moved only, with the entity that clips it. An entity of a named block the file inserts once is erased or moved in its block's definition, in the block's coordinates, with the warning edit-block-definition.

lintEdit(edit, read) gives the refusals the read can tell, and never throws: a handle two statements reach, one the read does not hold or keeps outside only, a move of one, or a change that takes one, out of what only keeps, edit-only, an attribute or a picture's entity named alone, an entity of a block inserted more than once, of an anonymous block or of an xref, by its name in any case or a name with |, or of a block whose inserts a read in part does not all list, a move of a dimension whose picture block is not its alone, a 3D solid, a region or a body, a turn or a scale of a dimension, since the leading CAD app draws its picture again, a turn of an OLE frame, which the app keeps upright, a scale of a table or of paragraph text whose codes set a height of its own, a turn or a scale of an old leader with its note, an old leader and its note apart, and a turn or a scale of a viewport. An entity on a locked layer is erased or moved with the warning edit-locked-layer. The writers refuse besides what only the file's records tell: a move of what an associative dimension or hatch names, or of the dimension or hatch, apart from the other; an erasure of what an associative dimension names, the dimension kept; a reference to what goes that no rule takes out; a dynamic block's definition; and a handle the file holds twice.

Copy

// The sheet's design as it is, kept beside it before the client's revisions.
e.copy(["4360A", "4360C", "4368F"], { by: [2400, 0] });

e.copy(handles, transform) leaves each entity where it is and adds a copy of it, transformed as e.move would transform it, as the leading CAD app's COPY makes one. A copy takes with it what the entity owns: an insert's attributes, a 2D polyline's vertices, each with its SEQEND, its extension dictionary and every record that owns, and a dimension's picture, as a block of its own, *D with the next number the file does not hold. It goes last in the entity's space, model space or a layout's paper, and a copy of an insert joins its block's list of inserts.

A copy of an insert of an anonymous block, *U and a number, shows a copy of that block, with the next *U number, and of each such block it inserts; their copies insert one another. Each copy makes its blocks anew, so two copies of inserts of one block show two blocks, and a block's copy lists its own insert alone among its inserts. A block's copy whose source holds a sort table holds one too, which draws the copies in the order the source draws its entities. An associative array is such an insert, and the leading CAD app's COPY makes its blocks anew the same way, so the copy keeps its picture when the array changes. The copy holds no association: it is an insert of a block of its own, which no array changes.

A copy refers to what the same statement copies by that one's copy, and to anything else, a layer, a style, a block, as the entity does. A reactor that names a record outside the copy is left out, so a copy joins no group. An associative hatch is copied associative only where the same statement copies every entity its loops are made from, which the read names in each loop's boundaries; otherwise its copy is a hatch of none, which the copies of its boundaries no longer name. A dimension or a multileader is copied with no association with what it measures or points at, as the leading CAD app copies one without that, even where the same statement copies all of it. Extended data is copied as it is, but for the mark of an addition: a copy is not what an edit added.

Each record a copy makes takes the next handle from the file's $HANDSEED, in a fixed order: statement by statement, each handle in the order it names them, the copy, then its attributes, then its picture's block and its entities, then each anonymous block it shows and their entities, in the order of the file's blocks, as the read shows them; a block's attribute definitions come after its entities and their attributes. An edit's additions take theirs after those. What a copy takes that a read does not show, a SEQEND, a 2D polyline's vertices, an extension dictionary and what it owns, a block's BLOCK and ENDBLK, takes its handles last, after the additions and the edit's record. So editedRead gives each copy the handle both writers give it; its header.handseed is past the handles the read shows, and the file's own seed is past those it does not show too. A handle past FFFFFFFFFFFFFFFF is refused.

lintEdit refuses, with edit-copy, a copy of an entity of a block's definition, a viewport, an image, an underlay, a table, a text whose field the read lists, an insert whose attribute shows one, an insert that shows a block whose stateOf the read gives, a dynamic block's state, or one that inserts such a block, since its copy would keep the dynamic block's data, an insert whose copy would make more than 1,000 anonymous blocks anew, and one whose copy would take the anonymous blocks the edit's copies make anew past 10,000, counted statement by statement and each handle in the order it names them, where a copy refused counts for none; with edit-part, a dimension whose picture block another record names too; with edit-twice, a handle a copy names twice, one the edit also erases, moves, stretches, sets the vertices of or changes, and an addition of an earlier edit of the same name, which the edit replaces; with edit-unread-object, an entity of a kind the reader does not read; and whatever a move of the same entity refuses: a 3D solid, a turn or a scale of a dimension, a turn of an OLE frame, a scale of an old leader with its note, an old leader and its note apart, edit-leader-note, and a copy out of what a read with only keeps, edit-only. The writers refuse besides what only the file's records tell, every one of a statement's at once: a record that holds a field, an annotative entity, whose scale contexts no copy is made of, an entity an associative network holds, but for an insert whose anonymous blocks the copy makes anew, in a format whose records are decoded, one that does not decode, and in DXF, a copy whose block's sort table would leave the file more sort tables or sort entries than a read keeps.

Mirror

// The door at 4371C hung on its other side, then swung out of the room: its wall runs along X.
e.mirror(
  ["4371C"],
  [
    [1204.5, 880],
    [1204.5, 881],
  ],
);
e.mirror(
  ["4372A"],
  [
    [1180, 902.25],
    [1181, 902.25],
  ],
);
// A plan's restroom drawn again as its mirror beside it, the source kept.
e.copy(["4380B", "4380C", "4380D"], {
  mirror: [
    [1300, 0],
    [1300, 1],
  ],
});

e.mirror(handles, line) mirrors each entity in place across the line through the two points of line, as the leading CAD app's MIRROR does with MIRRTEXT 0 and its source erased, but each entity keeps its handle, and an insert keeps its attributes', so a dimension, a group or a later edit that names it still finds it. It is a move whose transform is { mirror: line }; a copy whose transform is { mirror: line } keeps the source and adds the mirror, as MIRROR keeps it. A transform that mirrors takes by too, a move after the mirror, and no about, rotate or scale; a line of two points too close to give one is refused.

  • An insert takes its X scale the other way and the turn of its mirror, as the leading CAD app writes one: a door's hand or swing changes, and its block, its attributes and their values stay. An insert of a dynamic block keeps its block, the anonymous *U block of a state it shows, its extension dictionary and the dynamic block's data as they are, so the state and its grips stay the insert's. One already mirrored, with a negative X scale, is mirrored back.
  • A text and an attribute stay readable. A text is placed where its ink lands mirrored: turned about the end of its baseline where the mirror sends its direction the other way along X, or along Y for one that runs up or down, and over its baseline otherwise. A text justified right on its baseline, aligned or fitted, turned about the end of its baseline, takes its two points the other way round. Its justification, its style and whether it is drawn backward or upside down are kept. Paragraph text keeps its point, and its attachment changes left for right, or top for bottom.
  • A line, a light or a 2D polyline, its bulges turned the other way, a circle, an arc, its ends swapped, an ellipse and its parameters, a spline, a point and a 2D solid or a trace are mirrored point by point.

The ink of a text is found from the widths of its font's characters. Along the baseline they give it in every font of FONTS; up and down, only in an SHX font: a TrueType font's characters are held no higher than a capital's top and no lower than the baseline. lintEdit warns with edit-mirror-text of a text, or an insert's attribute, whose place the widths do not give as the leading CAD app's MIRROR places it, such as one in a TrueType font turned over its baseline, or in a font tsdraft does not have: it may sit off by a small part of its height.

lintEdit refuses, with edit-mirror, a mirror of what the leading CAD app's MIRROR lays out again, or of what no case of it shows yet: a dimension, a multileader, an old leader, a hatch, whose pattern MIRRHATCH 0 keeps, a wipeout, an image, an underlay, an OLE frame, a table, a viewport, an xref's insert, an insert with a clip, a slanted text, paragraph text in columns, an insert whose attribute is of paragraph text, and a justified text whose file stores no start and whose start the widths do not give; and whatever a move of the same entity refuses. The writers refuse besides an annotative entity, whose scale contexts no mirror is made of. An insert in rows and columns is not read, so no edit names it.

Stretch and vertices

e.stretch(["4361F", "4360A"], {
  window: [
    [82960, 156080],
    [83660, 156100],
  ],
  by: [0, -14.25],
});
e.vertices("1035F0F", {
  points: [
    [24297, 98179.4],
    [24297, 97720.5],
    [23658.2, 97720.5],
  ],
});

e.stretch(handles, { window, by }) stretches each entity it names as the leading CAD app's STRETCH does by a crossing window. window is two opposite corners, and a point on its edge is inside. What the window holds of an entity it does not name stays as it is.

  • A line, a light or a 2D polyline, a 2D solid and a trace: each point inside moves by by. A side of a polyline whose one end moves keeps its arc's height, so its bulge is worked out again. Widths stay.
  • An arc whose one end is inside: that end moves, and the arc keeps its height. Its ends are worked out from its angles, so an end within the rounding of its sine and cosine of the edge is on it.
  • An insert with its attributes, paragraph text, a circle and a point move whole when the point they are placed by is inside. A justified text moves by its alignment point, a text on the left by its start, and an aligned or fitted one by either of its two points.
  • A hatch tied to its boundary, a polyline its loop copies: both are stretched by the same statement, point by point. A loop the window leaves as it is keeps its boundary as it is, so the outer edge of a hatch with a hole stretches and the hole stays. A hatch with no boundary is left as it is unless the window holds it whole, as the leading CAD app leaves it, with the warning edit-stretch-unchanged.
  • An entity whose points are all inside moves as e.move moves it, a dimension with its picture. One with no point inside is left as it is, with the warning edit-stretch-unchanged.

A stretch refuses, with edit-stretch: a dimension with some of its points inside, whose picture would be drawn again; an arc whose middle is inside and one end is not; an ellipse, a spline, a multileader, an old leader, a table, an image, a wipeout and an underlay; an OLE frame, which the leading CAD app's STRETCH resizes from its upper-left corner whatever the window holds of it; a viewport; and a side of a polyline, a hatch's loop or a 2D solid that would come to nothing. The entity that clips a viewport moves only with its viewport, so a stretch or vertices of it is refused with edit-part. The writers refuse a stretch in part of what an associative dimension names, unless the edit erases the dimension, and in part of an arc, a hatch or a 2D solid not seen from above.

e.vertices(handle, { points, bulges }) sets a light or a 2D polyline's points, in the world's coordinates. bulges gives one for each side, as the read gives them: an open polyline's one fewer than its points. Without it each side keeps its bulge, and its widths stay. A light polyline may take another number of points: then a polyline with arcs needs bulges, and one with widths of each side is refused. It refuses, with edit-vertices: another number of points for a 2D polyline, whose vertex records would be added or erased; a closed polyline's last point that repeats its first; bulges of another number than its sides; and anything that is not a polyline. The writers refuse a fitted 2D polyline, and the boundary of an associative hatch or what an associative dimension names, unless the edit erases the hatch or the dimension.

A stretch and a vertices statement reach what they name, and what a move would take with it, so one handle in two statements is refused, as for every other statement.

editedRead(read, edit) is the read of the file after the edit: what it erases left out of its space, its census and its lists, what it moves moved, what it gives new points as the edit leaves it, what it changes changed, with the census of each layer and the fields a whole value takes out, and what it adds added, with its marks and the edit's record. The objects an erased entity owns that the read counts and does not list, and the fields it counts by their code, it leaves as the source's.

Change a text, a pen, a layer and a viewport

export default editDrawing(
  { file: "../2_Sheets/TITLE BLOCK.dwg", sha256: "9c1f…", name: "rev-1" },
  (e) => {
    e.change("126DD", { text: { replace: ["2021", "2026"] } });
    e.change("126CA", { text: "100 MAIN STREET\\PSPRINGFIELD", width: 2.7 });
    e.change("1270A", { text: "11.19.2026" });
    e.change("4361E", { layer: "A-GLAZ-IDEN", color: 1, lineweight: 35 });
    e.change("436A0", { attributes: { SHEET: "C-3", ADDRESS: { replace: ["NORTH", "SOUTH"] } } });
    e.layer("SURVEY|TOPO", { frozen: true });
    e.layer("SURVEY|EOP", { color: 250 });
    e.viewport("2C41", { center: [1204.5, 880], ratio: 1 / 240, freeze: ["SURVEY|TEXT"] });
  },
);

e.change(handle, changes) changes an entity in place. It keeps its handle, its record, its style and every format its text does not replace.

  • text is a whole value, with its codes, or { replace: [from, to] }. In a single-line text or a single-line attribute, from is found in the value as held, %% codes as written, once and only once. In paragraph text, and an attribute of paragraph text, from is found in the characters as the paragraph reads, codes left out and each \P a line's end, once and only once. to takes the format in force at the first character it replaces, written with \, { and } escaped and each line's end as \P. A group or a stack inside the replaced part goes with it, and a code that would change the characters after it is refused. A whole value of paragraph text is refused where parseParagraph does not lay it out.
  • A text that shows a field takes a whole value only: the field goes with its objects, and the text holds the value, as a CAD program's conversion of a field to text leaves it. A replacement in one is refused, since the field writes its own value back over it.
  • height is a text's height or paragraph text's, and width the width paragraph text wraps at. Both keep the text's point and its attachment. Paragraph text of one column takes the width as its own and its column's; one of two or more columns, or one whose text the new width would flow into a second column, is refused. An aligned text's height its two points set.
  • A justified single-line text whose value or height changes starts where the widths of its font's characters put it, and an aligned or a fitted one takes the height or the width factor that keeps it between its points. Where the widths do not give it exactly, such as a value with %% codes or a TrueType text justified Middle, a DXF keeps the start it holds, which a CAD program works out again when it reads the file, and a DWG, which stores the start a CAD program draws from, refuses the change.
  • boxHeight: 0 fits a multileader's note box to its text. A file can store a box taller than its note, and the leading CAD app sizes the note's mask by that height, so the mask can hide what lies below. 0 is what the app writes for a new note that does not wrap, and the only height set. The read gives the stored height as the note's boxHeight. The picture draws the mask as tall as the note before and after, so the change shows in no picture and the changed sheets do not list it. A multileader with no note, an annotative one, which holds a box for each scale, and a note in columns are refused.
  • A multileader's note takes text, height and width as paragraph text does, and keeps its style, font, leader lines, arrowheads, landing, attachments, mask and frame. A whole value for a note underlined line by line starts with its \L, as a CAD program holds one. The note moves as the leading CAD app moves a note it changes: a note on the right of its landing keeps its left side and one on the left its right side, each moved up or down as its attachment needs for its new lines, and a note above or below keeps its middle and the side its landing meets. A note with no leader lines, as a firm's general notes often are, keeps its place. Its box is fitted to its text, as boxHeight: 0 fits it, and a new height becomes the multileader's own. The picture a CAD program saved with the multileader for programs that cannot draw one would show the old note, so it goes, and the program that opens the copy draws it again. Where the note's font is not one tsdraft measures, or its value holds a code whose size tsdraft estimates, the copy keeps the place the file stores, with the warning edit-multileader. A multileader whose content is a block, with attributes or without, one with no note, one with landings on both sides of its note, and one whose landing, and so its note, is turned are refused with edit-multileader; an annotative one, a note in columns, a turned note with no leader lines and one whose line spacing is not 1, which only the file's records tell, the writers refuse. A note holds at most 65,534 characters, and a line of it at most 5,000: the leading CAD app stops when it opens a note with a line of 5,500.
  • attributes changes an insert's attributes by tag, each as text is.
  • layer, color (1 to 255, 0 for ByBlock, 256 for ByLayer), trueColor (0xRRGGBB, with the nearest index beside it), linetype (one the file holds, ByLayer or ByBlock), lineweight (in hundredths of a millimetre, as the read gives it, or ByLayer, ByBlock or Default) and linetypeScale set the entity's own pen. A 2D polyline's vertices take it with it. The layer and the linetype must be the file's: an edit adds none yet.

e.layer(name, changes) changes a layer of the file's table: on, frozen, locked, plot, color, trueColor, linetype and lineweight. A host changes an xref's layer, XREF|LAYER, in its own record, marked as the host's override so a reload of the xref keeps it, and sets $VISRETAIN to 1, without which a host keeps no change of an xref's layer. A DWG host whose VISRETAIN is 0 holds no record of an xref's layers, and such a change is refused there. An xref layer's linetype is one the host holds, its own or the xref's, such as SURVEY|PHANTOM2.

e.viewport(handle, changes) re-aims a viewport: center is the point of model space at its middle, ratio its scale in paper units to drawing units, twist its view turned in degrees. on turns it on or off, and freeze and thaw list layers to freeze in it alone and to thaw.

lintEdit refuses besides: a change of a kind that holds no text or no width, a from found nowhere or more than once, a layer, linetype or attribute tag the file lacks, a change of an entity of a block the file inserts more than once, and a viewport re-aimed out of what the read with only keeps. A change on a locked layer is made with the warning edit-locked-layer, and one of a block inserted once with edit-block-definition.

Additions

export default editDrawing(
  { file: "../1_Model/FLOOR PLAN.dxf", sha256: "9c1f…", name: "rev-1" },
  (e) => {
    e.model((d) => {
      d.layer("NOTES", () =>
        d.text([1204.5, 880], "NEW WINDOW G", { height: 4.5, style: "ROMANS" }),
      );
      d.layer("DIMENSIONS", () =>
        d.linearDimension([1200, 860], [1236, 860], [0, 850], { style: "DETAILS", id: "g-width" }),
      );
    });
    e.layout("FLOOR PLAN", (sheet) =>
      sheet.layer("REV-1", { color: 6 }, () => sheet.rect([20, 2], 3, 2)),
    );
  },
);

e.model(draw) gives the drawing builder over the file's own layers, linetypes, text, dimension, multileader and table styles and blocks, in the file's units, $INSUNITS, or the edit's units for a file of none. e.layout(name, draw) gives it on the paper of one of the file's layouts, in the layout's own units; ReadLayout.plotScale says what it plots at. A name the file holds is the file's, found as the leading CAD app finds it: an addition uses it as the file defines it, the edit writes none, and another definition of it is refused with the rule file-definition. A layer of the file named with no options is the file's; one named with other properties is refused. A name the file lacks is defined by the edit. The file's own Standard multileader, dimension and table styles are named Standard. Under a standard, a text role's and a leader style's styleName names the firm's style: in an edit of a file that holds it, the addition draws in it, and a new drawing that draws to a styleName of Standard is refused. Every addition names its layer.

A style of the file whose font tsdraft does not hold, such as a firm's Baskerville Old Face, takes a text justified left and a paragraph of one column, which the leading CAD app lays out itself; a justified text or attribute, a paragraph in columns, a multileader, a dimension and a table in it are refused, since tsdraft would work out where they go from another font's widths.

An entity takes its own linetypeScale, group 48, and a dimension its own scale, DIMSCALE, which the writers write as the dimension's override of its style's, the extended data ACAD, DSTYLE. A dimension style's lineExtension, DIMDLE, is how far a tick's dimension line runs past each extension line; it defaults to the arrow size.

Each addition's handle is counted from the file's $HANDSEED, in a fixed order: the spaces' entities first, then the blocks they need, then the table records, then the objects, so the same file and edit give the same handles. Each entity added, and each table record and named block the edit defines, carries a mark, extended data under the application TSDRAFT: the mark's format, 1, the edit's name, the addition's id, or @model/3 for the fourth of model space with none and @layout/Sheet1/3 for one of a layout, the handle it was written with, and a hash of what it held, markHash. The file keeps a record of each edit's additions, an XRECORD of the edit's name in the dictionary TSDRAFT of the named object dictionary: group 70 the mark's format, then each id, group 1, and its handle, group 2. Both readers give an entity's mark, only for five strings of format 1 with a handle, and the file's edits, each addition with a handle in capitals and with no leading zero. An id is compared without regard to case, and one the edit gives may not start with @.

markUnchanged(entity) is true of an entity whose mark holds its own handle and whose hash matches what it holds: it is still the one the edit wrote. A copy a person makes with the leading CAD app's COPY, MIRROR or ARRAY carries the mark with another's handle, and a move or any other change makes the hash differ; a save of the file in that app keeps every mark unchanged, in both formats.

A later edit of the same name

An edit planned on a file that holds an earlier edit's record of the same name replaces that edit's additions:

  • an addition whose id the record holds and whose entity is unchanged is written over it, in its place and with its handle, where the kind is the same; of another kind, the old one is erased and the new one takes a new handle, with the warning edit-id-kind-changed;
  • an unchanged addition whose id the new edit drops is erased, and an edit that adds nothing erases them all;
  • an addition whose entity a person changed is refused, edit-mark-changed, until the edit erases that entity by handle or gives the addition another id, and one whose entity a person erased, edit-mark-erased, until it gives another id;
  • an addition a read with only leaves out is refused, edit-only: read the part that holds it.

An edit of another name leaves them alone, and may erase or move one by handle as it would any entity. replacement(read, edit) says what an edit makes of an earlier one's additions.

lintEdit refuses an addition the writers cannot make: additions that do not hold a drawing's shape or hold more than 100,000 items, a file with no $HANDSEED or one not above a handle the file holds, a style of the file tsdraft does not draw in, such as one whose arrowhead it does not draw, a linear or aligned dimension at an angle in a style that keeps its text level, a radial dimension whose text is placed along it in such a style, a justified text in the file's Standard when its font is not the one tsdraft measures Standard in, an id used twice across the spaces, a definition of a name the file holds other than the file's, and an image or an underlay, which an edit does not add yet. It warns of an addition on a layer that is off, frozen, set not to plot, Defpoints or 0, or that a viewport of the file freezes.

A row added to a schedule

A schedule a firm draws as text in boxes, lines or a polyline for each cell, reads as a grid:

export default editDrawing(
  { file: "../1_Model/FLOOR PLAN.dwg", sha256: "6d6f…", name: "rev-1" },
  (e, read) => {
    const window = [
      [1200, 860],
      [1900, 1110],
    ] as const;
    const f = readSchedule(read, { window }).cells.find((c) => c.text === "F")!;

    e.row(
      { window },
      {
        after: f.row,
        cells: ["G", "SINGLE HUNG", `3'-0"`, `2'-4"`, "NO", "YES", "ALUMINUM"],
      },
    );
  },
);

readSchedule(read, { window, layout }) reads the schedule a window holds whole, in model space or on a layout's paper: columns and rows, the edges its lines and the straight sides of its polylines draw across and up and down, and cells, each with the row and column it starts at, how many it spans, its box, its texts without their codes, top to bottom, and the handles of what it holds. A merged cell, one no edge divides, is listed once. Two edges closer than a twentieth of the smallest text are one, and an edge closes at least one cell and is joined, through the edges it meets, to the schedule's outside: a tag, a rule or a dash inside a cell is what the cell holds. Each text belongs to the cell its middle is in, as its font measures it and as it is turned, and an arc to the cell its middle is in.

e.row(schedule, { after, cells }) adds a row below the row after, as a person does with the leading CAD app's own commands: what lies below moves down by that row's height, what crosses the row's bottom edge, such as the frame or a merged cell, stretches to it, and what the row draws, its boxes, lines, circles, texts and paragraph text, is drawn again in the new row on the same layers, in the same styles, heights, justifications and pens. Where nothing would draw a part of the row's bottom edge or the new row's, a line in the pen of what drew that part does. cells holds a value for each of the row's cells, left to right: a string, written as it reads with the codes the cell's text opens with kept, such as its color, "" for a cell left empty, or undefined for the cell's texts as they are. A merged cell that spans more than the row grows by the new row and takes undefined; what it holds moves together, keeping its place: at the top when its middle is in the cell's top third, in the middle in its middle third and at the foot in its bottom third. A row at the foot of a merged cell that spans more than one row is refused, since whether the new row joins the cell is not clear. The row is a move, a stretch and additions, so every check of an edit holds them.

It refuses, with an EditError: a window that holds no grid, lines inside a schedule that make two cells or more apart from its outside, as inside a frame drawn clear of it, a text beside the columns in the rows' height, which no line up and down closes, a row that is not one, values that do not match the row's cells, a value for a cell that holds no text or more than one, an entity that ends inside the row, a kind it does not copy, and a text it cannot write where the leading CAD app starts it: aligned, fitted or justified middle, mirrored, or justified in the file's own Standard when its font is not the one tsdraft measures Standard in; a paragraph with a mask of a true color, and part of an ellipse. A single-line text given a value keeps its underline and overline. A schedule's window holds at most 20,000 entities, 200,000 sides and 250,000 cells, and the space around it at most 10,000,000 points.

A revision: clouds, a delta and a row

A revision of a firm's sheet marks what it changed with clouds, puts the firm's delta with the revision's number beside each, and adds the revision's row to the title block. e.revision declares the revision and returns its cloud, delta and row:

export default editDrawing({ file: "./M-101.dwg", sha256: "9c1f…", name: "rev-2" }, (e, read) => {
  const rev = e.revision({
    number: "2",
    date: "12/01/26",
    description: "MEP COORDINATION",
    by: "AI",
  });

  e.change("4361E", { text: 'NEW RTU-2 ON 14" CURB' });
  rev.delta({ cloud: rev.cloud(["4361E"]) });
  rev.row({ layout: "M-101" });
});
  • rev.cloud(around, options), or e.cloud, draws a cloud around entities by handle or around { window: [corner, corner] }. A sheet's cloud around what its viewport shows is drawn on the layout's paper through the viewport's view, a twisted one too, around the part the viewport shows: one cloud for each viewport, or the paper, that shows them apart. The sheet is the layout the handles are on, the one layout that shows them, or options.layout; with two layouts that show them it is refused until layout names one. model: true draws it in model space. A window is in the space the cloud is drawn in, so it takes layout or model: true.
  • A cloud is drawn once the edit's function returns, around each entity as the edit leaves it: where a move puts it, a changed text at its new length, and an erased entity where it was. The sheet it goes on is found then too, so a line moved from what one sheet shows to what another shows is clouded on the other, and a cloud around a moved viewport goes where it now is. A line, an arc, a circle or a polyline is clouded around the part a viewport shows of it, so a diagonal that crosses one corner is clouded in that corner.
  • arc and offset are as plotted: 3/8 in arcs 1/8 in outside what the cloud goes around, or 10 mm and 3 mm on a sheet in millimetres. A cloud in model space scales them by the view of the first viewport that shows it, or else by $DIMSCALE. On a sheet the cloud stays inside what the paper draws, its border among it, and one that would go around the whole sheet is refused.
  • The cloud is one closed polyline of arcs that each turn 110 degrees and bulge out, as the leading CAD app's REVCLOUD draws one, on the revision layer. route takes the place of the rectangle a cloud follows, so a later version can route it clear of text.
  • rev.delta(at, options), or e.delta, inserts the firm's delta block with the revision's number in its attribute, on the revision layer: at a point, with layout or model: true, or at { cloud }, just outside the upper right corner of a cloud the edit draws. Its scale is that of the block's inserts already in the space, or else 1 on a sheet and the cloud's scale in model space. A file with no delta block is refused, and tsdraft draws no stand-in.
  • rev.row(where) writes the revision's row in one of two forms. { grid: { window, layout } } is a grid of closed cells, as e.row reads one: the row goes into the drawn empty row below the last filled one, its texts copied from the row above, or is added below the last filled one with e.row. The columns are found from the headings, such as NO., DATE, DESCRIPTION and BY, or given as columns. A row holds text when any cell that spans it does, merged ones among them. { titleBlock: handle } or { layout } is a title block whose attributes hold the rows, such as REV1, DATE1, DESC1 and BY1: the row after the last numbered one is set with e.change, or the tags tags gives. A tag is a field's only from its start, as REV, REV_NO, DATE, REV_DATE, DESC, DESCRIPTION, BY or INITIALS with the row's number, so NOTE1, SHEETNO1 and ISSUE_DATE1 are none.
  • The revision layer is record.layer, or the layer named for revisions, such as G-ANNO-REVS, that holds the file's own clouds, or the layer of its deltas, or else the one layer named for revisions. A cloud on a layer not named for revisions is no proof, as a shrub is drawn the same way. A file that keeps a layer for each revision takes the one whose number is the revision's. A file read in part takes record.layer, since a part not read could hold a cloud or a delta on another layer. The delta block is record.delta.block, or the block named for a delta or a revision that holds an attribute; its attribute is record.delta.tag, its one attribute, or the one named for a number from its start, such as REV or DELTA, and not SHEET_NO.

An edit makes one revision. lintEdit warns, as edit-revision-cloud, of each entity a statement erases, moves, copies, changes, stretches or reshapes that no cloud of the revision encloses on a sheet that shows it, through each viewport and on the paper apart, or in its own space when no sheet does. A cloud on a layer the edit turns off, freezes or keeps from plotting encloses nothing. A moved or copied entity is judged where it goes, an erased one where it was, and any other both where it was and where it is. The statements of the revision's own row are not judged. The Edit records the revision as revision: { number, statements }.

It refuses, with an EditError: a cloud, a delta or a row before e.revision, a second revision, a cloud around no handle, handles on two layouts, a handle the sheet does not show, a window with no area or no space, a cloud that would have too many arcs or go around the whole sheet, a revision layer or a delta block it cannot find or tell apart, a revision layer on a file read in part, the deltas on a layer apart from the one named for revisions, a delta's number or a row's field with two tags that could hold it, a grid whose rows grow up from its heading or whose heading has empty rows above it and none filled, a row below the last numbered one that holds text, as a description carried on may, a field whose cell spans more than its row or another field's column, a second row in one grid or title block, a title block whose revision rows are all filled, and an argument of the wrong shape.

The preview

tsdraft render and tsdraft dev take an edit file as they take a drawing file, and draw the file it edits as it will plot after the edit, from editedRead(read, edit): model space and each layout, at the sheet's own pens, with the xrefs found beside the file. The edit of a model file draws through each sheet its sheets names too, with the edited read in place of each of the sheet's xrefs that is the file, as <sheet's file name>/<layout> in outputs.layouts with the sheet's file; where two of its sheets share a file name, each is named by its path as the edit gives it. A sheet's xrefs of model space are read by the areas its layouts show of them. A second picture, the picture of changes, draws every entity the edit does not reach faint, each it erases dashed in red where it was, each it moves, stretches or changes dashed in amber where it was and at full strength where it goes, and each it copies or adds at full strength, of the file's own model space and layouts. A change of a layer draws every picture again. dev toggles between the two with the Changes button or the C key, and serves the second at /drawing.svg?changes=1. The JSON's edit gives each statement with its call, its handles and its box in model space and on each sheet, and lintEdit's diagnostics come with the others, each with the call of its statement. A statement lintEdit gives an error for is left out of the pictures, as editedRead leaves it out. A file whose SHA-256 is not the edit's is drawn as it is.

dev keeps the read of the file and its pictures in a process of its own, as fragments, one for each entity, and joins them after each save, leaving out what the edit erases and drawing again only what it moves, so a save is drawn in a time in proportion to the picture, not to the read. editPreview, changesPicture and statementPlace are the core's parts of it.

The copy

editDXF(bytes, edit, read) makes the edited copy of a DXF of the 2018 format, AC1032. Each record the edit does not change is copied as the file holds it, line ends included, so an edit that changes nothing gives the file back byte for byte. It throws only an EditError:

  • for bytes whose SHA-256 is not the edit's, or a read of other bytes;
  • for a file of another format, a binary DXF, or one past the most records a document lists;
  • for a statement or an addition lintEdit refuses, or one the file's records refuse, with statement the place of a statement in the edit.

Additions are written by the core's DXF writer at its six decimals, each space's as a drawing of its own, and each of its records is the file's of the same name or new: entities before the end of their space, a block at the end of BLOCKS, a table record before its table's end with the table's count raised, an object at the end of OBJECTS, named in its dictionary, and a class at the end of CLASSES. A new insert of the file's block joins the block's list of its inserts, and $HANDSEED is raised past every handle added.

A copy writes each record it takes again, at its new handle, with the references the copy makes of it and the points, angles and sizes of its transform: an entity and what follows it at the end of its space, a block record it copies at the end of its table, with the table's count raised, its BLOCK, entities and ENDBLK at the end of BLOCKS, and an object at the end of OBJECTS.

A record an erasure leaves out is left out with its line ends. A record a move changes keeps every group but the values of its points, angles and sizes, each written in the fewest digits that read back as the same number; one the file leaves out at its default, such as an insert's rotation, is written where the edit changes it.

An editor of another format, such as DWG, is a package, as a reader is: it names its module in its package.json under "tsdraft": { "editors": { ".dwg": "./dist/edit.js" } }, beside its reader, and the module exports editor, a FileEditor: its read with geometry, its edit, its structure check, its records by key and what each record links to, its owner and the handles it refers to. dxfEditor is the core's own. An editor numbers a copy's records from copyPlan(read, edit, seed), which gives each copy as the read shows it and each record's handle by copyKey(statement, handle, of?), where of is the handle of the insert a copy is of, for a record of an anonymous block that copy makes anew, a *U block's record and its attribute definitions among them; copyPlanOf(read, edit) is the same plan from the read's own seed. isContext tells the kind of a scale context, and Refusals gathers what an editor refuses of the copies into one EditError. Its read gives a DrawingRead in the read format of the tsdraft it runs with, READ_FORMAT, which is 1: the command takes a read in format 0 from a reader built for an older tsdraft, but uses an editor's read as it is.

The checks of every job

checkEditedCopy(editor, source, read, edit, written) runs the checks a copy passes before a write leaves it, each against the source as it was, so a defect the firm's file already had is told from a new one. The fifth, the changed sheets, draws the job's sheets with every copy in place, so tsdraft write runs it once every copy of the job is made:

Check What it holds
read-back The written file reads, with no more of unread.malformed than the source
structure The structure check finds no defect the source does not hold. own counts the source's
before-after Every record the edit does not reach is the source's, byte for byte, in its place. A record is gone only where the edit erases it or what owns it, and changes only where the edit moves it or what owns it, takes references to what it erases out of it, or gains references to what it adds. A new record takes a handle from the source's $HANDSEED on, and the header changes in its seed alone. The written file reads as editedRead says, to nine digits, and counts no more objects of a kind the reader does not read than the source and the new records hold. The copy is read with the blocks the edit adds, addedBlocks, kept apart (ReadOptions.blocks), so a file read in part reads them whole; one still past its limit is held to the edit's count of entities and listed in notCompared
lint lintEdit's errors, those of the additions among them
sheets Each layout of the file, and of each sheet the edit names, whose picture the copies change, drawn as it plots with every copy of the job that passed the other checks in place of its source, as a PNG and a PDF, listed in sheets. A sheet it cannot draw fails it

dxfStructure(bytes) is the structure check of a DXF, as the leading CAD app's AUDIT would find a fault: every handle once and below $HANDSEED, every owner and pointer, groups 330 to 369, 390 to 399, 480 and 481, naming a record, every layer naming its plot style, every entity's layer, linetype and text or dimension style a record of its table, and a block's list of its inserts, where the file keeps one, naming each of them. A defect names handles and names, never offsets, so the defects of a file and of its copy compare. It never throws: groups that do not read are one defect, unreadable.

tsdraft write

tsdraft write <edit file>... [--in-place] [--out <dir>] [--root <dir>] makes the edited copy of each file the edit files name, as one job, and prints the job as JSON. For each edit file:

  1. It loads the edit file and finds the file it names.
  2. It looks for a lock beside the file: one named as it with .dwl or .dwl2 for its extension, in any case. A lock is a warning, since a copy writes nothing of the firm's.
  3. It checks the file's SHA-256 against the edit's. Another is refused: the file changed after the edit was planned.
  4. It reads the file and makes the edit. An edit lintEdit gives an error for is refused, and its JSON gives each error with the call of its statement in diagnostics.
  5. It makes the copy and runs the first four checks of every job on what it made, in a process of its own, under the limits below.
  6. It writes the copy to a temporary file in the folder it goes to and checks that the bytes on the disk are the ones the checks passed.
  7. Once every edit file has been through those steps, it runs the fifth check, the changed sheets: each layout of each file the job edits and each sheet the edits name, drawn as it plots from the project and again with every copy that passed in place of its source. Each reference is found in the project from the path its file holds, and a file the job edits is read from its copy. An xref of a sheet's model space is read by the areas its layouts show of it, a file that does not read whole by its layouts, and the blocks an edit adds, and those the kept entities draw that came past the limit, kept apart. A layout whose two pictures differ is drawn as a PNG, 1600 pixels wide, and plotted to a PDF, <name>.layout-<layout>.png and .pdf at its file's path from the root under the out folder, and listed in the check of each edit whose file or sheet it is, with its file, layout, png and pdf. A layout the copies cannot draw fails the check, and so does a sheet an edit names that attaches no xref found as its file, and a copy that holds more of model space where a sheet shows it than a read keeps, whose pictures could miss the edit.
  8. It renames each copy that passed all five to the source's path from the job's root, under --out, out/ beside the edit files by default. The root is --root, or else the deepest folder that holds every source of the job and every sheet the edits name, so an edited sheet's relative path to an edited model holds among the copies.

A copy that fails a check is not written, and its JSON says which check and why; one that fails one of the first four has its source drawn in its place, and its sheets check says it was not run. Without --in-place it never writes a source's own path or a sheet's, by any path to it. It refuses two edits of one file in one job. It keeps a record, .tsdraft-write.json in the out folder, of each copy and picture it wrote and its SHA-256, a picture with the sheet it is of, and replaces a file at a copy's or a picture's path only when the record names it with the bytes it holds: so a second run replaces its own copies, and never a file it did not make or a copy a person changed. A picture no written copy lists is taken away, and so is a recorded picture of a layout of the job's files that no longer changes. A machine with no PNG renderer writes nothing, and says so at once. It exits 0 when every copy was written and 2 when any was not. A .dwg needs an installed package that edits it, found as a reader is (Which reader), or --reader. A refusal of a source says which check made it in refusedBy: sha256 for a file that changed after the edit was planned, and with --in-place also lock, path and job, below.

Writing into the firm's file: --in-place

With --in-place, the edited file is written into the firm's file itself, and the firm's file is kept beside it as <name>.bak, as the leading CAD app keeps its last copy. The job writes every file or none:

  1. A lock beside a source refuses it before its edit runs, where without --in-place it is a warning: refusedBy is lock.
  2. So does a path a rename would not write as the firm's file, refusedBy path: a link, which the rename would replace and leave the file it leads to as it was; one of two or more names of one file; a read-only file, or a folder that cannot be written; a .bak that is there and is no file, or is another file of the job; and two edits that reach one file, or one .bak, as PLAN.dwg and PLAN.dxf would, by different paths.
  3. Each edit runs and its copy is checked as above, in a temporary file in the source's own folder, so the rename never leaves its file system.
  4. Once every file of the job has passed all five checks, each source's lock and then its SHA-256 are checked again. Then at once the source is renamed to its .bak, an older .bak removed first, and the copy, with the source's mode, into its place. If that second rename fails, the .bak is renamed back, and so is each file of the job already written.

A file that fails a check, or is refused, leaves every source of the job as it was, and each other file's refusedBy is job. Each written file gives its backup, and copy is the source's own path. The pictures of the changed sheets still go to --out. A drafter who opens a file in the moment between the last check and the rename is not seen: no check on a shared folder closes that moment, and the .bak keeps the file they opened. The same edit file cannot run twice in place, since the file it was planned on is then the .bak.

Limit of one edit's process Default
The process's heap, as --max-old-space-size 4096 MB
The time until it answers 300 s
The size of the file it writes 1 GB

The process runs under prlimit --core=1:1 --cpu=<seconds> -- where there is one, as a read's does. The changed sheets are drawn in one more process, with the same heap and the time of one edit for each file it draws, and each of its reads under the limits of a read.

This page as Markdown