lineadraw 0.8.0 → 0.9.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.
@@ -1431,19 +1431,6 @@ var ViewportSchema = ObjectBaseSchema.extend({
1431
1431
  locked: z3.boolean().default(false),
1432
1432
  hiddenLayerIds: z3.array(z3.string()).default([])
1433
1433
  });
1434
- var ImageSchema = ObjectBaseSchema.extend({
1435
- type: z3.literal("image"),
1436
- /** Key into `document.assets`. */
1437
- assetId: z3.string(),
1438
- /** Bottom-left corner (world/sheet mm) — rotation pivots here. */
1439
- origin: Vec2Schema,
1440
- width: z3.number().positive(),
1441
- height: z3.number().positive(),
1442
- /** Radians CCW about `origin`. */
1443
- rotation: z3.number().default(0)
1444
- // (opacity moved to ObjectBaseSchema — every object paints with it now;
1445
- // documents that stored it per-image parse unchanged.)
1446
- });
1447
1434
  var REFERENCE_FORMATS = [
1448
1435
  "linea",
1449
1436
  "dxf",
@@ -1458,7 +1445,8 @@ var ReferenceSourceSchema = z3.discriminatedUnion("kind", [
1458
1445
  path: z3.string().min(1),
1459
1446
  cacheKey: z3.string().min(1).optional()
1460
1447
  }),
1461
- z3.object({ kind: z3.literal("url"), url: z3.string().url() })
1448
+ z3.object({ kind: z3.literal("url"), url: z3.string().url() }),
1449
+ z3.object({ kind: z3.literal("asset"), assetId: z3.string().min(1) })
1462
1450
  ]);
1463
1451
  var ReferenceSchema = ObjectBaseSchema.extend({
1464
1452
  type: z3.literal("reference"),
@@ -1510,7 +1498,6 @@ var ModelObjectSchema = z3.discriminatedUnion("type", [
1510
1498
  TextSchema,
1511
1499
  DimensionSchema,
1512
1500
  BlockSchema,
1513
- ImageSchema,
1514
1501
  ViewportSchema,
1515
1502
  CommentSchema,
1516
1503
  TableSchema,
@@ -1955,7 +1942,7 @@ var RowParam = z5.object({
1955
1942
  type: z5.literal("row"),
1956
1943
  items: z5.array(RowItemSchema).min(2).max(3)
1957
1944
  });
1958
- var BlockParamSchema = z5.discriminatedUnion("type", [
1945
+ var ParamDefSchema = z5.discriminatedUnion("type", [
1959
1946
  NumberParam,
1960
1947
  StringParam,
1961
1948
  BooleanParam,
@@ -2009,16 +1996,6 @@ var remapBlockInputRefs = (obj, idMap) => {
2009
1996
  };
2010
1997
  var mapInputPoints = (obj, fn) => obj.inputs.map((value) => typeof value === "string" ? value : fn(value));
2011
1998
  var pointsCarryPose = (obj) => obj.inputs.some((v) => typeof v === "string") || obj.inputs.filter((v) => typeof v !== "string").length >= 2;
2012
- var imageCenter = (obj) => add(obj.origin, rot({ x: obj.width / 2, y: obj.height / 2 }, obj.rotation));
2013
- var imageCorners = (obj) => {
2014
- const c = (x, y) => add(obj.origin, rot({ x, y }, obj.rotation));
2015
- return [
2016
- obj.origin,
2017
- c(obj.width, 0),
2018
- c(obj.width, obj.height),
2019
- c(0, obj.height)
2020
- ];
2021
- };
2022
1999
  var mapObjectPoints = (obj, fn) => {
2023
2000
  switch (obj.type) {
2024
2001
  case "line":
@@ -2034,8 +2011,10 @@ var mapObjectPoints = (obj, fn) => {
2034
2011
  case "hatch":
2035
2012
  return {
2036
2013
  ...obj,
2037
- // An anchored pattern frame moves with its region.
2038
- fill: obj.fill.kind === "pattern" && obj.fill.origin ? { ...obj.fill, origin: fn(obj.fill.origin) } : obj.fill,
2014
+ // The pattern frame moves with its region, as in AutoCAD — an
2015
+ // unanchored fill's frame is the drawing origin, which moves along
2016
+ // too (else the pattern would swim inside a moved hatch).
2017
+ fill: obj.fill.kind === "pattern" ? { ...obj.fill, origin: fn(obj.fill.origin ?? { x: 0, y: 0 }) } : obj.fill,
2039
2018
  loops: obj.loops.map((loop) => ({
2040
2019
  points: loop.points.map((v) => ({ ...v, ...fn(v) }))
2041
2020
  }))
@@ -2052,8 +2031,6 @@ var mapObjectPoints = (obj, fn) => {
2052
2031
  return { ...obj, inputs: mapInputPoints(obj, fn) };
2053
2032
  case "viewport":
2054
2033
  return obj;
2055
- case "image":
2056
- return { ...obj, origin: fn(obj.origin) };
2057
2034
  case "comment":
2058
2035
  return { ...obj, position: fn(obj.position) };
2059
2036
  case "table":
@@ -2103,7 +2080,6 @@ var rotateObject = (obj, center, ang) => {
2103
2080
  return pointsCarryPose(mapped) ? mapped : { ...mapped, rotation: mapped.rotation + ang };
2104
2081
  case "viewport":
2105
2082
  return moveViewportCenter(mapped, r);
2106
- case "image":
2107
2083
  case "reference":
2108
2084
  return { ...mapped, rotation: normAngle(mapped.rotation + ang) };
2109
2085
  case "hatch":
@@ -2161,8 +2137,6 @@ var scaleObjectUniform = (obj, s) => {
2161
2137
  // mm per paper mm) shrinks as the frame grows.
2162
2138
  scale: mapped.scale / s
2163
2139
  };
2164
- case "image":
2165
- return { ...mapped, width: mapped.width * s, height: mapped.height * s };
2166
2140
  case "reference":
2167
2141
  return { ...mapped, scale: mapped.scale * s };
2168
2142
  case "point":
@@ -2198,12 +2172,6 @@ var mirrorObject = (obj, a, b) => {
2198
2172
  mirrored: !obj.mirrored
2199
2173
  };
2200
2174
  }
2201
- if (obj.type === "image") {
2202
- const c = imageCenter(obj);
2203
- const rotation = mirrorAngle(obj.rotation);
2204
- const moved = { ...obj, rotation };
2205
- return translateObject(moved, sub(m(c), imageCenter(moved)));
2206
- }
2207
2175
  if (obj.type === "table") {
2208
2176
  const width = obj.columns.reduce((a2, w) => a2 + w, 0) * obj.scale;
2209
2177
  const topRight = add(obj.position, scale(fromAngle(obj.rotation), width));
@@ -2269,7 +2237,6 @@ var mirrorObject = (obj, a, b) => {
2269
2237
  // Mirrored above, before the point map.
2270
2238
  case "ellipse":
2271
2239
  case "reference":
2272
- case "image":
2273
2240
  case "table":
2274
2241
  return mapped;
2275
2242
  default:
@@ -2309,8 +2276,51 @@ ${indent}}`;
2309
2276
  return "undefined";
2310
2277
  };
2311
2278
 
2279
+ // ../core/src/model/assets.ts
2280
+ var hashAsset = (data) => {
2281
+ let h = 5381;
2282
+ for (let i = 0; i < data.length; i++)
2283
+ h = (h << 5) + h + data.charCodeAt(i) | 0;
2284
+ return `${(h >>> 0).toString(16)}-${data.length.toString(16)}`;
2285
+ };
2286
+ var dataUrlMime = (dataUrl) => /^data:([^;,]*)/.exec(dataUrl)?.[1] ?? "";
2287
+ var dataUrlBytes = (dataUrl) => {
2288
+ const comma = dataUrl.indexOf(",");
2289
+ const payload = dataUrl.slice(comma + 1);
2290
+ if (!/;base64$/.test(dataUrl.slice(0, comma)))
2291
+ return new TextEncoder().encode(decodeURIComponent(payload));
2292
+ const bin = atob(payload);
2293
+ const out = new Uint8Array(bin.length);
2294
+ for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
2295
+ return out;
2296
+ };
2297
+ var bytesToDataUrl = (bytes, mime) => {
2298
+ let bin = "";
2299
+ for (let i = 0; i < bytes.length; i += 32768)
2300
+ bin += String.fromCharCode(...bytes.subarray(i, i + 32768));
2301
+ return `data:${mime};base64,${btoa(bin)}`;
2302
+ };
2303
+ var imagePixelSize = (b) => {
2304
+ const u32 = (i) => (b[i] << 24 | b[i + 1] << 16 | b[i + 2] << 8 | b[i + 3]) >>> 0;
2305
+ const u16 = (i) => b[i] << 8 | b[i + 1];
2306
+ if (b[0] === 137 && b[1] === 80 && b[2] === 78 && b[3] === 71)
2307
+ return { width: u32(16), height: u32(20) };
2308
+ if (b[0] !== 255 || b[1] !== 216) return null;
2309
+ for (let i = 2; i + 9 < b.length; ) {
2310
+ if (b[i] !== 255) return null;
2311
+ const marker = b[i + 1];
2312
+ const len3 = u16(i + 2);
2313
+ if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204)
2314
+ return { width: u16(i + 7), height: u16(i + 5) };
2315
+ i += 2 + len3;
2316
+ }
2317
+ return null;
2318
+ };
2319
+ var PX_TO_MM = 25.4 / 96;
2320
+ var embeddedAssetId = (o) => o.type === "reference" && o.source?.kind === "asset" ? o.source.assetId : void 0;
2321
+
2312
2322
  // ../core/src/model/migrations.ts
2313
- var CURRENT_DOCUMENT_VERSION = 22;
2323
+ var CURRENT_DOCUMENT_VERSION = 23;
2314
2324
  var leaderRawToText = (raw) => {
2315
2325
  const points = Array.isArray(raw.points) ? raw.points : [];
2316
2326
  const extraLines = Array.isArray(raw.extraLines) ? raw.extraLines : [];
@@ -2898,6 +2908,48 @@ var migrateV20toV21 = (doc) => {
2898
2908
  return { ...doc, objects: mapObjects(doc.objects), layouts, version: 21 };
2899
2909
  };
2900
2910
  var migrateV21toV22 = (doc) => ({ ...doc, version: 22 });
2911
+ var imageRawToReference = (raw, assets) => {
2912
+ const rest = { ...raw };
2913
+ delete rest.assetId;
2914
+ delete rest.origin;
2915
+ delete rest.width;
2916
+ delete rest.height;
2917
+ const assetId = typeof raw.assetId === "string" ? raw.assetId : "";
2918
+ const data = assets[assetId];
2919
+ const width = typeof raw.width === "number" && raw.width > 0 ? raw.width : 1;
2920
+ const height = typeof raw.height === "number" && raw.height > 0 ? raw.height : 1;
2921
+ let px;
2922
+ try {
2923
+ px = typeof data === "string" ? imagePixelSize(dataUrlBytes(data)) : null;
2924
+ } catch {
2925
+ px = null;
2926
+ }
2927
+ const w = px ? px.width * PX_TO_MM : width;
2928
+ const h = px ? px.height * PX_TO_MM : height;
2929
+ return {
2930
+ ...rest,
2931
+ type: "reference",
2932
+ format: typeof data === "string" && dataUrlMime(data) === "image/jpeg" ? "jpg" : "png",
2933
+ name: "Image",
2934
+ source: { kind: "asset", assetId: assetId || "missing" },
2935
+ position: raw.origin ?? { x: 0, y: 0 },
2936
+ rotation: typeof raw.rotation === "number" ? raw.rotation : 0,
2937
+ scale: width / w,
2938
+ extent: { minX: 0, minY: 0, maxX: w, maxY: h }
2939
+ };
2940
+ };
2941
+ var migrateV22toV23 = (doc) => {
2942
+ const assets = doc.assets !== null && typeof doc.assets === "object" ? doc.assets : {};
2943
+ const mapObjects = (list) => Array.isArray(list) ? list.map(
2944
+ (raw) => raw !== null && typeof raw === "object" && raw.type === "image" ? imageRawToReference(raw, assets) : raw
2945
+ ) : list;
2946
+ const layouts = Array.isArray(doc.layouts) ? doc.layouts.map((raw) => {
2947
+ if (raw === null || typeof raw !== "object") return raw;
2948
+ const layout = raw;
2949
+ return { ...layout, objects: mapObjects(layout.objects) };
2950
+ }) : doc.layouts;
2951
+ return { ...doc, objects: mapObjects(doc.objects), layouts, version: 23 };
2952
+ };
2901
2953
  var MIGRATION_STEPS = [
2902
2954
  migrateV1toV2,
2903
2955
  migrateV2toV3,
@@ -2919,7 +2971,8 @@ var MIGRATION_STEPS = [
2919
2971
  migrateV18toV19,
2920
2972
  migrateV19toV20,
2921
2973
  migrateV20toV21,
2922
- migrateV21toV22
2974
+ migrateV21toV22,
2975
+ migrateV22toV23
2923
2976
  ];
2924
2977
  function migrateDocument(raw) {
2925
2978
  if (raw === null || typeof raw !== "object") return raw;
@@ -2938,7 +2991,10 @@ function migrateDocument(raw) {
2938
2991
 
2939
2992
  // ../core/src/extensions/blockDtos.ts
2940
2993
  var CHILD_OMIT = { id: true, layerId: true };
2941
- var CHILD_LAYER = { layerId: z6.string().optional() };
2994
+ var CHILD_LAYER = {
2995
+ layerId: z6.string().optional(),
2996
+ layer: z6.string().optional()
2997
+ };
2942
2998
  var BlockChildSchema = z6.preprocess(
2943
2999
  // Back-compat: scripts written before document v6 may still emit "leader"
2944
3000
  // children (→ text with leaderLines), pre-v9 scripts may emit dimensions
@@ -2969,6 +3025,7 @@ var BlockChildSchema = z6.preprocess(
2969
3025
  TextSchema.omit(CHILD_OMIT).extend(CHILD_LAYER),
2970
3026
  TableShapeSchema.omit(CHILD_OMIT).extend(CHILD_LAYER).superRefine(tableRefinement),
2971
3027
  DimensionShapeSchema.omit(CHILD_OMIT).extend(CHILD_LAYER).superRefine(dimensionRefinement),
3028
+ ReferenceSchema.omit(CHILD_OMIT).extend(CHILD_LAYER),
2972
3029
  BlockSchema.omit(CHILD_OMIT).extend(CHILD_LAYER).refine((b) => b.inputs.every((v) => typeof v !== "string"), {
2973
3030
  message: "nested block instances may not carry object references"
2974
3031
  })
@@ -2982,7 +3039,6 @@ __export(scriptHelpers_exports, {
2982
3039
  add: () => add2,
2983
3040
  dist: () => dist2,
2984
3041
  dot: () => dot2,
2985
- helpers: () => helpers,
2986
3042
  len: () => len2,
2987
3043
  norm: () => norm2,
2988
3044
  polar: () => polar,
@@ -3026,22 +3082,9 @@ var rotate = (p, angle2) => {
3026
3082
  const sin = Math.sin(angle2);
3027
3083
  return { x: p.x * cos - p.y * sin, y: p.x * sin + p.y * cos };
3028
3084
  };
3029
- var helpers = Object.freeze({
3030
- range,
3031
- polar,
3032
- toDeg,
3033
- toRad,
3034
- len: len2,
3035
- dist: dist2,
3036
- add: add2,
3037
- sub: sub2,
3038
- scale: scale2,
3039
- norm: norm2,
3040
- dot: dot2,
3041
- rotate
3042
- });
3043
3085
 
3044
3086
  // ../core/src/extensions/scriptRuntime.ts
3087
+ var helpers = Object.freeze({ ...scriptHelpers_exports });
3045
3088
  var onTranspilerReady = null;
3046
3089
  var transpiler = null;
3047
3090
  var transpilerLoading = null;
@@ -3161,7 +3204,7 @@ var toBlockModule = (mod) => {
3161
3204
  );
3162
3205
  return { ...mod, draw, place };
3163
3206
  };
3164
- var ParamsExportSchema = z7.array(BlockParamSchema);
3207
+ var ParamsExportSchema = z7.array(ParamDefSchema);
3165
3208
  var OptionalBlockMetaSchema = z7.object({
3166
3209
  description: z7.string().optional(),
3167
3210
  version: z7.string().optional(),
@@ -3425,15 +3468,17 @@ var testBlockDefinition = async (script) => {
3425
3468
  return { ok: false, error: e instanceof Error ? e.message : String(e) };
3426
3469
  }
3427
3470
  };
3428
- var childToObject = (child, instance, index) => {
3471
+ var childToObject = (child, instance, index, layers) => {
3472
+ const { layer, ...rest } = child;
3473
+ const named = layer === void 0 ? void 0 : (layers?.find((l) => l.id === layer) ?? layers?.find((l) => l.name === layer))?.id;
3429
3474
  return {
3430
3475
  color: instance.color,
3431
3476
  lineType: instance.lineType,
3432
3477
  lineTypeScale: instance.lineTypeScale,
3433
3478
  lineWidth: instance.lineWidth,
3434
- ...child,
3479
+ ...rest,
3435
3480
  id: `${instance.id}:${index}`,
3436
- layerId: child.layerId ?? instance.layerId
3481
+ layerId: named ?? child.layerId ?? instance.layerId
3437
3482
  };
3438
3483
  };
3439
3484
  var evaluating = /* @__PURE__ */ new Set();
@@ -3501,7 +3546,7 @@ var getEvaluatedWorldChildren = (instance, scripts, resolveObject, styles = DEFA
3501
3546
  evalErrors.delete(instance.id);
3502
3547
  const pivot = blockPivot(instance);
3503
3548
  return children.map((child, i) => {
3504
- let obj = childToObject(child, instance, i);
3549
+ let obj = childToObject(child, instance, i, styles.layers);
3505
3550
  if (instance.scale !== 1) obj = scaleObjectUniform(obj, instance.scale);
3506
3551
  if (instance.rotation !== 0)
3507
3552
  obj = rotateObject(obj, { x: 0, y: 0 }, instance.rotation);
@@ -7916,6 +7961,18 @@ var cellAnchor = (t, colX, rowY, a) => {
7916
7961
  vAlign: v
7917
7962
  };
7918
7963
  };
7964
+ var tableCellFrame = (t, textStyles, row, col) => {
7965
+ const { colX, rowY, anchors } = tableLayout(t, textStyles);
7966
+ const a = anchors.find(
7967
+ (a2) => row >= a2.row && row < a2.row + a2.rows && col >= a2.col && col < a2.col + a2.cols
7968
+ ) ?? { row, col, rows: 1, cols: 1 };
7969
+ return {
7970
+ row: a.row,
7971
+ col: a.col,
7972
+ text: { ...cellText(t, a.row, a.col), ...cellAnchor(t, colX, rowY, a) },
7973
+ innerWidth: colX[a.col + a.cols] - colX[a.col] - 2 * t.cellMargin * t.scale
7974
+ };
7975
+ };
7919
7976
  var cache = /* @__PURE__ */ new WeakMap();
7920
7977
  var tableLayout = (t, textStyles) => {
7921
7978
  const hit = cache.get(t);
@@ -8398,7 +8455,7 @@ var lineWeightToMm = (lineWeight) => lineWeight === void 0 || lineWeight < 0 ? v
8398
8455
  var mmToLineWeight = (mm) => LINE_WEIGHTS.reduce(
8399
8456
  (best, w) => Math.abs(w - mm * 100) < Math.abs(best - mm * 100) ? w : best
8400
8457
  );
8401
- var encodeMtext = (s) => s.replace(/\\/g, "\\\\").replace(/\{/g, "\\{").replace(/\}/g, "\\}").replace(/\^/g, "^ ").replace(/\n/g, "\\P").replace(/\t/g, "^I");
8458
+ var encodeMtext = (s) => s.replace(/\r\n/g, "\n").replace(/\r/g, "").replace(/\\/g, "\\\\").replace(/\{/g, "\\{").replace(/\}/g, "\\}").replace(/\^/g, "^ ").replace(/\n/g, "\\P").replace(/\t/g, "^I");
8402
8459
  var decodeTextEscapes = (text) => text.replace(/%%([dpcuo%])/gi, (_, ch) => {
8403
8460
  switch (ch.toLowerCase()) {
8404
8461
  case "d":
@@ -8962,6 +9019,130 @@ var MLeaderStyleObject = class extends DxfObject {
8962
9019
  dx.push(273, 9);
8963
9020
  }
8964
9021
  };
9022
+ var AcadTableEntity = class extends Point {
9023
+ args;
9024
+ constructor(args) {
9025
+ super(args.position.x, args.position.y, 0);
9026
+ this.args = args;
9027
+ this.type = "ACAD_TABLE";
9028
+ this.subclassMarker = "AcDbBlockReference";
9029
+ }
9030
+ dxfyChild(dx) {
9031
+ const a = this.args;
9032
+ dx.push(2, a.blockName);
9033
+ dx.push(10, a.position.x);
9034
+ dx.push(20, a.position.y);
9035
+ dx.push(30, 0);
9036
+ dx.push(100, "AcDbTable");
9037
+ dx.push(280, 0);
9038
+ dx.push(342, a.tableStyleHandle);
9039
+ dx.push(343, a.blockRecordHandle);
9040
+ dx.push(11, a.direction.x);
9041
+ dx.push(21, a.direction.y);
9042
+ dx.push(31, 0);
9043
+ dx.push(90, 22);
9044
+ dx.push(91, a.rowHeights.length);
9045
+ dx.push(92, a.colWidths.length);
9046
+ dx.push(93, 2);
9047
+ dx.push(94, 0);
9048
+ dx.push(95, 0);
9049
+ dx.push(96, 0);
9050
+ dx.push(281, 1);
9051
+ for (const h of a.rowHeights) dx.push(141, h);
9052
+ for (const w of a.colWidths) dx.push(142, w);
9053
+ for (const c of a.cells) {
9054
+ dx.push(171, 1);
9055
+ dx.push(172, 0);
9056
+ dx.push(173, c.merged ? 1 : 0);
9057
+ dx.push(174, 0);
9058
+ dx.push(175, c.cols);
9059
+ dx.push(176, c.rows);
9060
+ dx.push(91, 1);
9061
+ dx.push(178, 0);
9062
+ dx.push(145, 0);
9063
+ dx.push(170, c.alignment);
9064
+ dx.push(92, 0);
9065
+ dx.push(301, "CELL_VALUE");
9066
+ if (c.text === "") {
9067
+ dx.push(93, 3);
9068
+ dx.push(90, 0);
9069
+ } else {
9070
+ dx.push(93, 6);
9071
+ dx.push(90, 4);
9072
+ dx.push(1, c.text);
9073
+ }
9074
+ dx.push(94, 0);
9075
+ dx.push(300, "");
9076
+ dx.push(302, c.text);
9077
+ dx.push(304, "ACVALUE_END");
9078
+ }
9079
+ }
9080
+ };
9081
+ var TableStyleObject = class extends DxfObject {
9082
+ args;
9083
+ constructor(args) {
9084
+ super("TABLESTYLE");
9085
+ this.args = args;
9086
+ }
9087
+ dxfy(dx) {
9088
+ const a = this.args;
9089
+ dx.push(0, "TABLESTYLE");
9090
+ dx.push(5, this.handle);
9091
+ dx.push(102, "{ACAD_REACTORS");
9092
+ dx.push(330, a.dictionaryHandle);
9093
+ dx.push(102, "}");
9094
+ dx.push(330, a.dictionaryHandle);
9095
+ dx.push(100, "AcDbTableStyle");
9096
+ dx.push(280, 0);
9097
+ dx.push(3, a.name);
9098
+ dx.push(70, 0);
9099
+ dx.push(71, 0);
9100
+ dx.push(40, a.margin);
9101
+ dx.push(41, a.margin);
9102
+ dx.push(280, 0);
9103
+ dx.push(281, 0);
9104
+ for (const [height, alignment] of [
9105
+ [a.textHeight, 2],
9106
+ [a.textHeight, 5],
9107
+ [a.textHeight, 5]
9108
+ ]) {
9109
+ dx.push(7, a.textStyleName);
9110
+ dx.push(140, height);
9111
+ dx.push(170, alignment);
9112
+ dx.push(62, 0);
9113
+ dx.push(63, 7);
9114
+ dx.push(283, 0);
9115
+ dx.push(90, 4);
9116
+ dx.push(91, 0);
9117
+ dx.push(1, "");
9118
+ for (const [lw, vis, color] of [
9119
+ [274, 284, 64],
9120
+ [275, 285, 65],
9121
+ [276, 286, 66],
9122
+ [277, 287, 67],
9123
+ [278, 288, 68],
9124
+ [279, 289, 69]
9125
+ ]) {
9126
+ dx.push(lw, -2);
9127
+ dx.push(vis, 1);
9128
+ dx.push(color, 0);
9129
+ }
9130
+ }
9131
+ }
9132
+ };
9133
+ var DXF_CLASSES = [
9134
+ ["ACAD_TABLE", "AcDbTable", "ObjectDBX Classes", 1025, 1],
9135
+ ["TABLESTYLE", "AcDbTableStyle", "ObjectDBX Classes", 4095, 0],
9136
+ ["MULTILEADER", "AcDbMLeader", "ACDB_MLEADER_CLASS", 1025, 1],
9137
+ ["MLEADERSTYLE", "AcDbMLeaderStyle", "ACDB_MLEADERSTYLE_CLASS", 4095, 0]
9138
+ ];
9139
+ var classesSection = (content) => DXF_CLASSES.filter(([name]) => content.includes(`
9140
+ 0
9141
+ ${name}
9142
+ `)).map(
9143
+ ([name, cpp, app, proxy, isEntity]) => [0, "CLASS", 1, name, 2, cpp, 3, app, 90, proxy, 91, 0, 280, 0, 281, isEntity].map((v) => `${v}
9144
+ `).join("")
9145
+ ).join("");
8965
9146
 
8966
9147
  // ../core/src/export/dxf/hatchPattern.ts
8967
9148
  var ANSI31_BASE_SPACING = 3.175;
@@ -9155,7 +9336,8 @@ var addTextLines = (space, content, position, height, rotation, hAlign, vAlign,
9155
9336
  return;
9156
9337
  }
9157
9338
  const down = scale(perp(fromAngle(rotation)), -1);
9158
- block.lines.forEach((line, i) => {
9339
+ block.lines.forEach((raw, i) => {
9340
+ const line = raw.replace(/\r/g, "");
9159
9341
  if (!line) return;
9160
9342
  const anchor = add(position, scale(down, i * block.lineHeight));
9161
9343
  const text = space.addText(v3(anchor), height, line, {
@@ -9174,8 +9356,7 @@ var documentPattern = (ctx, name) => ctx.doc.hatchPatterns.find(
9174
9356
  (p) => hatchPatternKey(p.name) === hatchPatternKey(name)
9175
9357
  );
9176
9358
  var injectHatchPatternLines = (content, ctx) => {
9177
- if (ctx.doc.hatchPatterns.length === 0 && ctx.hatchOrigins.size === 0)
9178
- return content;
9359
+ if (!content.includes("\n0\nHATCH\n")) return content;
9179
9360
  const out = [];
9180
9361
  let pos = 0;
9181
9362
  for (; ; ) {
@@ -9191,7 +9372,7 @@ var injectHatchPatternLines = (content, ctx) => {
9191
9372
  const name = /\n2\n([^\n]*)\n/.exec(body)?.[1];
9192
9373
  const anchor = handle ? ctx.hatchOrigins.get(handle) : void 0;
9193
9374
  const own = name ? documentPattern(ctx, name) : void 0;
9194
- const pat = own ?? (name && anchor ? resolveHatchPattern(name, []) : void 0);
9375
+ const pat = own ?? (name ? resolveHatchPattern(name, []) : void 0);
9195
9376
  const m77 = pat ? /\n77\n[^\n]*\n/.exec(body) : null;
9196
9377
  if (pat && m77 && bodyAt !== -1) {
9197
9378
  const angle2 = Number(/\n52\n([^\n]*)\n/.exec(body)?.[1] ?? 0) * Math.PI / 180;
@@ -9236,6 +9417,17 @@ ${(d * scale3).toFixed(6)}
9236
9417
  ${pat.lines.length}
9237
9418
  ` + lines + tail.slice(writerBlock);
9238
9419
  if (own) record = record.replace(/\n76\n1\n/, "\n76\n2\n");
9420
+ else if (name && name !== pat.name.toUpperCase()) {
9421
+ const nameAt = record.indexOf(`
9422
+ 2
9423
+ ${name}
9424
+ `, bodyAt);
9425
+ if (nameAt !== -1)
9426
+ record = record.slice(0, nameAt) + `
9427
+ 2
9428
+ ${pat.name.toUpperCase()}
9429
+ ` + record.slice(nameAt + name.length + 4);
9430
+ }
9239
9431
  }
9240
9432
  out.push(content.slice(pos, start), record);
9241
9433
  pos = end;
@@ -9460,6 +9652,82 @@ var writeDimension = (space, obj, opts, ctx) => {
9460
9652
  }
9461
9653
  return true;
9462
9654
  };
9655
+ var tableCellMtext = (text, ctx) => {
9656
+ if (text.content.trim() === "") return "";
9657
+ const eff = resolveTextStyle(text, ctx.doc.textStyles);
9658
+ const face = fontFace(eff.font, eff.bold, eff.italic);
9659
+ const { font, bold, italic } = parseFontFace(face);
9660
+ const height = eff.textHeight * text.scale;
9661
+ const body = text.spans?.length ? encodeRichMtext(text.content, text.spans, face) : encodeMtext(text.content);
9662
+ return `{\\f${fontTypeface(resolveFont(font))}|b${bold ? 1 : 0}|i${italic ? 1 : 0}|c0|p34;\\H${+height.toFixed(6)};` + (eff.widthFactor !== 1 ? `\\W${eff.widthFactor};` : "") + `${body}}`;
9663
+ };
9664
+ var attachmentOf = (hAlign, vAlign) => (vAlign === "top" ? 0 : vAlign === "center" ? 3 : 6) + (hAlign === "left" ? 1 : hAlign === "center" ? 2 : 3);
9665
+ var writeNativeTable = (space, obj, ctx) => {
9666
+ const layout = tableLayout(obj, ctx.doc.textStyles);
9667
+ const name = `*T${++ctx.tableBlockCount}`;
9668
+ const block = ctx.writer.document.blocks.addBlock(
9669
+ name,
9670
+ ctx.writer.document.objects,
9671
+ false
9672
+ );
9673
+ block.flags = 1;
9674
+ const before = ctx.exported;
9675
+ const origin = { x: 0, y: 0 };
9676
+ for (const child of layout.children)
9677
+ writeObject(
9678
+ block,
9679
+ rotateObject(
9680
+ translateObject(child, { x: -obj.position.x, y: -obj.position.y }),
9681
+ origin,
9682
+ -obj.rotation
9683
+ ),
9684
+ ctx
9685
+ );
9686
+ ctx.exported = before;
9687
+ const blockRecordHandle = ctx.writer.document.tables.blockRecordTable.records.find(
9688
+ (r) => r.name === name
9689
+ )?.handle ?? "0";
9690
+ const rows = obj.rows.length;
9691
+ const cols = obj.columns.length;
9692
+ const anchorAt = new Map(
9693
+ layout.anchors.map((a) => [`${a.row}:${a.col}`, a])
9694
+ );
9695
+ const cells = [];
9696
+ for (let r = 0; r < rows; r++)
9697
+ for (let c = 0; c < cols; c++) {
9698
+ const a = anchorAt.get(`${r}:${c}`);
9699
+ if (!a) {
9700
+ cells.push({ text: "", cols: 1, rows: 1, merged: true, alignment: 1 });
9701
+ continue;
9702
+ }
9703
+ const { text } = tableCellFrame(obj, ctx.doc.textStyles, r, c);
9704
+ cells.push({
9705
+ text: tableCellMtext(text, ctx),
9706
+ cols: a.cols,
9707
+ rows: a.rows,
9708
+ merged: false,
9709
+ alignment: attachmentOf(text.hAlign, text.vAlign)
9710
+ });
9711
+ }
9712
+ const diffs = (xs) => xs.slice(1).map((x, i) => x - xs[i]);
9713
+ const entity = new AcadTableEntity({
9714
+ blockName: name,
9715
+ blockRecordHandle,
9716
+ tableStyleHandle: ctx.tableStyleHandle,
9717
+ position: obj.position,
9718
+ direction: { x: Math.cos(obj.rotation), y: Math.sin(obj.rotation) },
9719
+ rowHeights: diffs(layout.rowY),
9720
+ colWidths: diffs(layout.colX),
9721
+ cells
9722
+ });
9723
+ entity.layerName = ctx.layerNames.get(obj.layerId) ?? "0";
9724
+ if (obj.color) {
9725
+ entity.colorNumber = nearestAci(obj.color);
9726
+ const tc = hexToTrueColor(obj.color);
9727
+ if (tc !== null) entity.trueColor = String(tc);
9728
+ }
9729
+ space.addEntity(entity);
9730
+ };
9463
9731
  var writeObject = (space, obj, ctx) => {
9464
9732
  const before = space.entities.length;
9465
9733
  writeObjectEntities(space, obj, ctx);
@@ -9613,6 +9881,10 @@ var writeObjectEntities = (space, obj, ctx) => {
9613
9881
  }
9614
9882
  break;
9615
9883
  case "block": {
9884
+ if (ctx.blockChain.includes(obj.definitionId)) {
9885
+ skip(ctx, "block");
9886
+ return;
9887
+ }
9616
9888
  const children = getEvaluatedWorldChildren(
9617
9889
  obj,
9618
9890
  ctx.doc.blockDefinitions,
@@ -9624,14 +9896,23 @@ var writeObjectEntities = (space, obj, ctx) => {
9624
9896
  return;
9625
9897
  }
9626
9898
  const before = ctx.exported;
9627
- for (const child of children) {
9628
- if (child.type === "block" || child.type === "viewport") continue;
9629
- writeObject(space, child, ctx);
9899
+ ctx.blockChain.push(obj.definitionId);
9900
+ try {
9901
+ for (const child of children) {
9902
+ if (child.type === "viewport") continue;
9903
+ writeObject(space, child, ctx);
9904
+ }
9905
+ } finally {
9906
+ ctx.blockChain.pop();
9630
9907
  }
9631
9908
  ctx.exported = before;
9632
9909
  break;
9633
9910
  }
9634
9911
  case "table": {
9912
+ if (!ctx.explodeTables) {
9913
+ writeNativeTable(space, obj, ctx);
9914
+ break;
9915
+ }
9635
9916
  const before = ctx.exported;
9636
9917
  for (const child of tableLayout(obj, ctx.doc.textStyles).children)
9637
9918
  writeObject(space, child, ctx);
@@ -9641,9 +9922,6 @@ var writeObjectEntities = (space, obj, ctx) => {
9641
9922
  case "viewport":
9642
9923
  skip(ctx, "viewport");
9643
9924
  return;
9644
- case "image":
9645
- skip(ctx, "image");
9646
- return;
9647
9925
  case "reference": {
9648
9926
  if (obj.hidden || obj.format !== "dxf" && obj.format !== "dwg" || obj.source.kind !== "path") {
9649
9927
  skip(ctx, "reference");
@@ -9697,6 +9975,10 @@ var writeViewport = (space, vp, id, ctx) => {
9697
9975
  );
9698
9976
  ctx.exported++;
9699
9977
  };
9978
+ var DWG_DXF_OPTIONS = {
9979
+ explodeLeaders: true,
9980
+ explodeTables: true
9981
+ };
9700
9982
  var buildDxf = (doc, measure, options = {}) => {
9701
9983
  const writer = new DxfWriter();
9702
9984
  writer.setUnits(Units.Millimeters);
@@ -9766,6 +10048,22 @@ var buildDxf = (doc, measure, options = {}) => {
9766
10048
  );
9767
10049
  mleaderStyle.ownerObjecthandle = mleaderDict.handle;
9768
10050
  mleaderDict.addEntryObject("Standard", mleaderStyle.handle);
10051
+ const tableStyleDict = writer.document.objects.addDictionary();
10052
+ writer.document.objects.addEntryToRoot(
10053
+ "ACAD_TABLESTYLE",
10054
+ tableStyleDict.handle
10055
+ );
10056
+ const tableStyle = writer.document.objects.addObject(
10057
+ new TableStyleObject({
10058
+ name: "Standard",
10059
+ textStyleName: "Standard",
10060
+ textHeight: doc.textStyles[0]?.textHeight ?? 2.5,
10061
+ margin: 1.5,
10062
+ dictionaryHandle: tableStyleDict.handle
10063
+ })
10064
+ );
10065
+ tableStyle.ownerObjecthandle = tableStyleDict.handle;
10066
+ tableStyleDict.addEntryObject("Standard", tableStyle.handle);
9769
10067
  const ctx = {
9770
10068
  doc,
9771
10069
  measure,
@@ -9776,7 +10074,11 @@ var buildDxf = (doc, measure, options = {}) => {
9776
10074
  exported: 0,
9777
10075
  dimBlockCount: 0,
9778
10076
  xrefBlocks: /* @__PURE__ */ new Map(),
10077
+ blockChain: [],
9779
10078
  explodeLeaders: options.explodeLeaders ?? false,
10079
+ explodeTables: options.explodeTables ?? false,
10080
+ tableStyleHandle: tableStyle.handle,
10081
+ tableBlockCount: 0,
9780
10082
  mleaderStyleHandle: mleaderStyle.handle,
9781
10083
  textStyleHandle,
9782
10084
  lineTypeHandle,
@@ -9861,10 +10163,23 @@ var buildDxf = (doc, measure, options = {}) => {
9861
10163
  );
9862
10164
  if (record) record.layoutObject = layoutObj.handle;
9863
10165
  }
10166
+ const content = injectHatchPatternLines(
10167
+ injectStyleTypefaces(writer.stringify(), ctx.textStyles),
10168
+ ctx
10169
+ );
9864
10170
  return {
9865
- content: injectHatchPatternLines(
9866
- injectStyleTypefaces(writer.stringify(), ctx.textStyles),
9867
- ctx
10171
+ // The writer's CLASSES section is always empty: declare the non-fixed
10172
+ // types this file uses (AutoCAD expects them declared).
10173
+ content: content.replace(
10174
+ "\n0\nSECTION\n2\nCLASSES\n0\nENDSEC\n",
10175
+ `
10176
+ 0
10177
+ SECTION
10178
+ 2
10179
+ CLASSES
10180
+ ${classesSection(content)}0
10181
+ ENDSEC
10182
+ `
9868
10183
  ),
9869
10184
  exported: ctx.exported,
9870
10185
  skipped: ctx.skipped
@@ -9917,8 +10232,6 @@ export {
9917
10232
  normAngle,
9918
10233
  closestPointOnSegment,
9919
10234
  remapBlockInputRefs,
9920
- imageCenter,
9921
- imageCorners,
9922
10235
  translateObject,
9923
10236
  rotateObject,
9924
10237
  scaleObjectUniform,
@@ -9959,6 +10272,12 @@ export {
9959
10272
  textBoxCorners,
9960
10273
  textFrameGeometry,
9961
10274
  tableLayout,
10275
+ hashAsset,
10276
+ dataUrlBytes,
10277
+ bytesToDataUrl,
10278
+ imagePixelSize,
10279
+ PX_TO_MM,
10280
+ embeddedAssetId,
9962
10281
  matIdentity,
9963
10282
  matMultiply,
9964
10283
  matApply,
@@ -9986,10 +10305,10 @@ export {
9986
10305
  valueParams,
9987
10306
  resolveDocumentObject,
9988
10307
  documentObjectResolver,
9989
- BlockChildSchema,
9990
10308
  ensureScriptTranspiler,
9991
10309
  executeScriptModule,
9992
10310
  scriptTranspilerReady,
10311
+ BlockChildSchema,
9993
10312
  expandWithBlockDependents,
9994
10313
  invalidateBlockCache,
9995
10314
  getBlockError,
@@ -10015,6 +10334,7 @@ export {
10015
10334
  mtextFontFlags,
10016
10335
  cleanText,
10017
10336
  cleanRichText,
10337
+ DWG_DXF_OPTIONS,
10018
10338
  buildDxf
10019
10339
  };
10020
- //# sourceMappingURL=chunk-VWED6V6F.js.map
10340
+ //# sourceMappingURL=chunk-BQCSFVAF.js.map