@bitbybit-dev/base 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/NOTICE +24 -0
  2. package/README.md +3 -1
  3. package/lib/api/GlobalCDNProvider.js +1 -1
  4. package/lib/api/inputs/base-inputs.d.ts +20 -0
  5. package/lib/api/inputs/base-inputs.js +22 -0
  6. package/lib/api/inputs/io-inputs.d.ts +408 -8
  7. package/lib/api/inputs/io-inputs.js +344 -7
  8. package/lib/api/inputs/mesh-inputs.d.ts +20 -1
  9. package/lib/api/inputs/mesh-inputs.js +23 -0
  10. package/lib/api/inputs/recipes-inputs.d.ts +13 -5
  11. package/lib/api/inputs/recipes-inputs.js +10 -5
  12. package/lib/api/kernel-calls/index.d.ts +1 -0
  13. package/lib/api/kernel-calls/index.js +1 -0
  14. package/lib/api/kernel-calls/resolve-dto.d.ts +3 -1
  15. package/lib/api/kernel-calls/resolve-dto.js +7 -2
  16. package/lib/api/kernel-calls/settings.d.ts +19 -0
  17. package/lib/api/kernel-calls/up-axis-aliases.d.ts +25 -0
  18. package/lib/api/kernel-calls/up-axis-aliases.js +70 -0
  19. package/lib/api/models/simplex.d.ts +2 -0
  20. package/lib/api/models/simplex.js +10 -1
  21. package/lib/api/resolved-inputs/index.d.ts +11 -2
  22. package/lib/api/services/helpers/curves/conic-arcs.d.ts +3 -1
  23. package/lib/api/services/helpers/curves/conic-arcs.js +4 -2
  24. package/lib/api/services/helpers/files/drawing/drawing-budget.js +16 -6
  25. package/lib/api/services/helpers/files/drawing/drawing-dimension.constants.d.ts +14 -0
  26. package/lib/api/services/helpers/files/drawing/drawing-dimension.constants.js +14 -0
  27. package/lib/api/services/helpers/files/drawing/drawing-dimension.d.ts +4 -0
  28. package/lib/api/services/helpers/files/drawing/drawing-dimension.js +271 -0
  29. package/lib/api/services/helpers/files/drawing/drawing-geometry.d.ts +3 -0
  30. package/lib/api/services/helpers/files/drawing/drawing-geometry.js +28 -11
  31. package/lib/api/services/helpers/files/drawing/drawing-hatch.d.ts +10 -0
  32. package/lib/api/services/helpers/files/drawing/drawing-hatch.js +123 -0
  33. package/lib/api/services/helpers/files/drawing/drawing-loops.d.ts +3 -0
  34. package/lib/api/services/helpers/files/drawing/drawing-loops.js +91 -0
  35. package/lib/api/services/helpers/files/drawing/drawing-model.d.ts +6 -1
  36. package/lib/api/services/helpers/files/drawing/drawing-model.js +97 -13
  37. package/lib/api/services/helpers/files/drawing/drawing-scale.d.ts +2 -0
  38. package/lib/api/services/helpers/files/drawing/drawing-scale.js +47 -0
  39. package/lib/api/services/helpers/files/drawing/drawing-text.d.ts +9 -0
  40. package/lib/api/services/helpers/files/drawing/drawing-text.js +25 -0
  41. package/lib/api/services/helpers/files/drawing/drawing-types.d.ts +57 -0
  42. package/lib/api/services/helpers/files/drawing/drawing.constants.d.ts +13 -0
  43. package/lib/api/services/helpers/files/drawing/drawing.constants.js +17 -0
  44. package/lib/api/services/helpers/files/dxf/dxf-annotations.d.ts +5 -0
  45. package/lib/api/services/helpers/files/dxf/dxf-annotations.js +96 -0
  46. package/lib/api/services/helpers/files/dxf/dxf-entities.d.ts +5 -1
  47. package/lib/api/services/helpers/files/dxf/dxf-entities.js +5 -2
  48. package/lib/api/services/helpers/files/dxf/dxf-generator.js +26 -10
  49. package/lib/api/services/helpers/files/dxf/dxf-reader-annotations.d.ts +9 -0
  50. package/lib/api/services/helpers/files/dxf/dxf-reader-annotations.js +312 -0
  51. package/lib/api/services/helpers/files/dxf/dxf-reader-dtos.js +37 -3
  52. package/lib/api/services/helpers/files/dxf/dxf-reader-segments.d.ts +9 -0
  53. package/lib/api/services/helpers/files/dxf/dxf-reader-segments.js +151 -0
  54. package/lib/api/services/helpers/files/dxf/dxf-reader-types.d.ts +27 -0
  55. package/lib/api/services/helpers/files/dxf/dxf-reader.constants.d.ts +63 -0
  56. package/lib/api/services/helpers/files/dxf/dxf-reader.constants.js +17 -0
  57. package/lib/api/services/helpers/files/dxf/dxf-reader.d.ts +1 -1
  58. package/lib/api/services/helpers/files/dxf/dxf-reader.js +71 -181
  59. package/lib/api/services/helpers/files/dxf/dxf-tables.d.ts +2 -0
  60. package/lib/api/services/helpers/files/dxf/dxf-tables.js +5 -4
  61. package/lib/api/services/helpers/files/dxf/dxf-tokens.d.ts +2 -0
  62. package/lib/api/services/helpers/files/dxf/dxf-tokens.js +25 -1
  63. package/lib/api/services/helpers/files/dxf/dxf.constants.d.ts +11 -0
  64. package/lib/api/services/helpers/files/dxf/dxf.constants.js +13 -0
  65. package/lib/api/services/helpers/files/svg/svg-strokes.d.ts +1 -1
  66. package/lib/api/services/helpers/files/svg/svg-strokes.js +47 -23
  67. package/lib/api/services/helpers/files/svg/svg-writer.d.ts +2 -0
  68. package/lib/api/services/helpers/files/svg/svg-writer.js +100 -23
  69. package/lib/api/services/helpers/files/svg/svg.constants.d.ts +2 -1
  70. package/lib/api/services/helpers/files/svg/svg.constants.js +2 -1
  71. package/lib/api/services/helpers/geometry/loop-fill.d.ts +2 -0
  72. package/lib/api/services/helpers/geometry/loop-fill.js +37 -0
  73. package/lib/api/services/helpers/helper-types.d.ts +33 -0
  74. package/lib/api/services/helpers/text/stroke-font.d.ts +6 -0
  75. package/lib/api/services/helpers/text/stroke-font.js +177 -0
  76. package/lib/api/services/helpers/text/stroke-letters.constants.d.ts +2 -0
  77. package/lib/api/services/helpers/text/stroke-letters.constants.js +18 -0
  78. package/lib/api/services/helpers/text/stroke-marks.constants.d.ts +14 -0
  79. package/lib/api/services/helpers/text/stroke-marks.constants.js +34 -0
  80. package/lib/api/services/io/drawing.d.ts +198 -0
  81. package/lib/api/services/io/drawing.js +242 -0
  82. package/lib/api/services/io/dxf.d.ts +3 -3
  83. package/lib/api/services/io/dxf.js +4 -4
  84. package/lib/api/services/io/svg.d.ts +13 -11
  85. package/lib/api/services/io/svg.js +15 -12
  86. package/lib/api/services/mesh.d.ts +19 -0
  87. package/lib/api/services/mesh.js +40 -0
  88. package/lib/api/services/recipes.js +3 -2
  89. package/lib/api/services/text.d.ts +3 -3
  90. package/lib/api/services/text.js +7 -25
  91. package/package.json +1 -1
@@ -0,0 +1,96 @@
1
+ import * as Inputs from "../../../../inputs";
2
+ import { InputError } from "../../../../kernel-calls";
3
+ import { angleWithinTurn } from "../../curves/conic-arcs";
4
+ import { scale2 } from "../../geometry/vectors";
5
+ import { hatchFamiliesOf } from "../drawing/drawing-hatch";
6
+ import { drawingLoopsOf } from "../drawing/drawing-loops";
7
+ import { textEndOf, textInked, textStartOf } from "../drawing/drawing-text";
8
+ import { FULL_TURN_DEGREES } from "../drawing/drawing.constants";
9
+ import { isModern, openEntity } from "./dxf-entities";
10
+ import { encodedText } from "./dxf-text";
11
+ import { DXF_CHARACTER_CODE_START, DXF_CODED_BACKSLASH, DXF_EXTRUSION_CODES, DXF_FIT_JUSTIFICATION, DXF_HATCH_NAMES, DXF_HATCH_NATIVE_ANGLE, DXF_HATCH_NATIVE_SPACING, DXF_OTHER_POINT_CODES, DXF_POINT_CODES, DXF_POLYLINE_BOUNDARY, DXF_TEXT_STYLE, } from "./dxf.constants";
12
+ export const dxfTextValue = (value) => encodedText(value.replace(DXF_CHARACTER_CODE_START, DXF_CODED_BACKSLASH).replace(/\^/g, "^ ").replace(/%%/g, "%%%%%%"));
13
+ export const writeText = (context, text, style) => {
14
+ if (!textInked(text)) {
15
+ return;
16
+ }
17
+ openEntity(context, "TEXT", Object.assign(Object.assign({}, style), { lineType: undefined }));
18
+ const out = context.text;
19
+ if (isModern(context)) {
20
+ out.pair("100", "AcDbText");
21
+ }
22
+ out.point(DXF_POINT_CODES, textStartOf(text));
23
+ out.real("40", text.height);
24
+ out.pair("1", dxfTextValue(text.value));
25
+ if (text.rotationDegrees !== 0) {
26
+ out.real("50", angleWithinTurn(text.rotationDegrees, FULL_TURN_DEGREES));
27
+ }
28
+ if (text.widthFactor !== 1) {
29
+ out.real("41", text.widthFactor);
30
+ }
31
+ out.pair("7", DXF_TEXT_STYLE);
32
+ out.pair("72", DXF_FIT_JUSTIFICATION);
33
+ out.point(DXF_OTHER_POINT_CODES, textEndOf(text));
34
+ if (isModern(context)) {
35
+ out.pair("100", "AcDbText");
36
+ out.pair("73", "0");
37
+ }
38
+ };
39
+ export const writeHatch = (context, hatch, style, property) => {
40
+ if (!isModern(context)) {
41
+ throw new InputError(`\`${property}\` holds a hatch, which an AC1009 (R12) file has no entity for: write AC1015.`, property);
42
+ }
43
+ const out = context.text;
44
+ const loops = drawingLoopsOf(hatch.boundaries, context.curveTolerance, out.budget);
45
+ out.refuseWhenSpent();
46
+ if (loops.length === 0) {
47
+ return;
48
+ }
49
+ const solid = hatch.pattern === Inputs.IO.hatchPatternEnum.solid;
50
+ openEntity(context, "HATCH", Object.assign(Object.assign({}, style), { lineType: undefined }));
51
+ out.pair("100", "AcDbHatch");
52
+ out.point(DXF_POINT_CODES, [0, 0, 0]);
53
+ out.point(DXF_EXTRUSION_CODES, [0, 0, 1]);
54
+ out.pair("2", DXF_HATCH_NAMES[hatch.pattern]);
55
+ out.pair("70", solid ? "1" : "0");
56
+ out.pair("71", "0");
57
+ out.pair("91", String(loops.length));
58
+ for (const loop of loops) {
59
+ const bulged = loop.bulges.some(bulge => bulge !== 0);
60
+ out.pair("92", DXF_POLYLINE_BOUNDARY);
61
+ out.pair("72", bulged ? "1" : "0");
62
+ out.pair("73", "1");
63
+ out.pair("93", String(loop.points.length));
64
+ loop.points.forEach((point, at) => {
65
+ var _a;
66
+ out.real("10", point[0]);
67
+ out.real("20", point[1]);
68
+ if (bulged) {
69
+ out.real("42", (_a = loop.bulges[at]) !== null && _a !== void 0 ? _a : 0);
70
+ }
71
+ });
72
+ out.pair("97", "0");
73
+ }
74
+ out.pair("75", "0");
75
+ out.pair("76", "1");
76
+ if (!solid) {
77
+ const families = hatchFamiliesOf(hatch);
78
+ out.real("52", angleWithinTurn(hatch.angle - DXF_HATCH_NATIVE_ANGLE, FULL_TURN_DEGREES));
79
+ out.real("41", hatch.spacing / DXF_HATCH_NATIVE_SPACING[context.measurement][hatch.pattern]);
80
+ out.pair("77", "0");
81
+ out.pair("78", String(families.length));
82
+ for (const family of families) {
83
+ const base = scale2(family.across, family.phase);
84
+ const offset = scale2(family.across, hatch.spacing);
85
+ out.real("53", angleWithinTurn(family.angle, FULL_TURN_DEGREES));
86
+ out.real("43", base[0]);
87
+ out.real("44", base[1]);
88
+ out.real("45", offset[0]);
89
+ out.real("46", offset[1]);
90
+ out.pair("79", "0");
91
+ }
92
+ }
93
+ out.pair("98", "1");
94
+ out.real("10", loops[0].points[0][0]);
95
+ out.real("20", loops[0].points[0][1]);
96
+ };
@@ -1,11 +1,12 @@
1
1
  import type * as Inputs from "../../../../inputs";
2
2
  import type { DrawingSegment } from "../drawing/drawing-types";
3
3
  import type { DxfText } from "./dxf-text";
4
- import type { DxfVersion } from "./dxf-tables";
4
+ import type { DxfMeasurement, DxfVersion } from "./dxf-tables";
5
5
  export interface DxfEntityStyle {
6
6
  readonly layer: string;
7
7
  readonly color: string;
8
8
  readonly lineType: Inputs.IO.lineTypeEnum | undefined;
9
+ readonly lineWeight: number | undefined;
9
10
  }
10
11
  export interface DxfEntityContext {
11
12
  readonly text: DxfText;
@@ -13,5 +14,8 @@ export interface DxfEntityContext {
13
14
  readonly owner: string;
14
15
  readonly colorFormat: "aci" | "truecolor";
15
16
  readonly curveTolerance: number;
17
+ readonly measurement: DxfMeasurement;
16
18
  }
19
+ export declare const isModern: (context: DxfEntityContext) => boolean;
20
+ export declare const openEntity: (context: DxfEntityContext, kind: string, style: DxfEntityStyle, owner?: string) => string;
17
21
  export declare const writeSegment: (context: DxfEntityContext, segment: DrawingSegment, style: DxfEntityStyle) => void;
@@ -9,9 +9,9 @@ import { DXF_CLOSED_FLAG, DXF_EXTRUSION_CODES, DXF_KNOT_TOLERANCE, DXF_OTHER_POI
9
9
  const PLANAR_FLAG = 8;
10
10
  const VERTEX_3D_FLAG = "32";
11
11
  const PLUS_Z = [0, 0, 1];
12
- const isModern = (context) => context.version === "AC1015";
12
+ export const isModern = (context) => context.version === "AC1015";
13
13
  const isPlusZ = (normal) => normal[0] === 0 && normal[1] === 0 && normal[2] > 0;
14
- const openEntity = (context, kind, style, owner = context.owner) => {
14
+ export const openEntity = (context, kind, style, owner = context.owner) => {
15
15
  const text = context.text;
16
16
  text.pair("0", kind);
17
17
  let handle = "";
@@ -29,6 +29,9 @@ const openEntity = (context, kind, style, owner = context.owner) => {
29
29
  if (isModern(context) && context.colorFormat === "truecolor") {
30
30
  text.pair("420", String(trueColorOf(style.color)));
31
31
  }
32
+ if (isModern(context) && style.lineWeight !== undefined) {
33
+ text.pair("370", String(style.lineWeight));
34
+ }
32
35
  return handle;
33
36
  };
34
37
  const writeLine = (context, line, style) => {
@@ -1,14 +1,15 @@
1
1
  import * as Inputs from "../../../../inputs";
2
2
  import { InputError } from "../../../../kernel-calls";
3
- import { checkedUnitOfLength } from "../../geometry/lengths";
3
+ import { checkedUnitOfLength, millimetresPerUnitOf } from "../../geometry/lengths";
4
4
  import { drawingBounds, drawingSize, patternUnitOf } from "../drawing/drawing-geometry";
5
5
  import { writerBudgetOf } from "../drawing/drawing-budget";
6
6
  import { drawingPartsOf, positiveNumber } from "../drawing/drawing-model";
7
7
  import { DEFAULT_LAYER, LEAST_TOLERANCE_SHARE, SPACE_AXES } from "../drawing/drawing.constants";
8
+ import { writeHatch, writeText } from "./dxf-annotations";
8
9
  import { writeSegment } from "./dxf-entities";
9
- import { writeBlocks, writeClasses, writeHeader, writeObjects, writeTables } from "./dxf-tables";
10
+ import { dxfMeasurementOf, writeBlocks, writeClasses, writeHeader, writeObjects, writeTables } from "./dxf-tables";
10
11
  import { DxfText } from "./dxf-text";
11
- import { DXF_FIRST_HANDLE } from "./dxf.constants";
12
+ import { DXF_FIRST_HANDLE, DXF_LINE_WEIGHTS, DXF_LINE_WEIGHTS_PER_MILLIMETRE } from "./dxf.constants";
12
13
  const VERSIONS = new Set(["AC1009", "AC1015"]);
13
14
  const COLOR_FORMATS = new Set(["aci", "truecolor"]);
14
15
  const layersOf = (parts) => {
@@ -23,10 +24,17 @@ const layersOf = (parts) => {
23
24
  }
24
25
  return rows;
25
26
  };
26
- const entityStyleOf = (part, layers) => {
27
+ const lineWeightOf = (width, millimetresPerUnit) => {
28
+ if (width === undefined) {
29
+ return undefined;
30
+ }
31
+ const wanted = width * millimetresPerUnit * DXF_LINE_WEIGHTS_PER_MILLIMETRE;
32
+ return DXF_LINE_WEIGHTS.reduce((nearest, weight) => Math.abs(weight - wanted) < Math.abs(nearest - wanted) ? weight : nearest);
33
+ };
34
+ const entityStyleOf = (part, layers, millimetresPerUnit) => {
27
35
  const continuous = Inputs.IO.lineTypeEnum.continuous;
28
36
  const byLayer = part.lineType === continuous && layers.get(part.layer.toUpperCase()).lineType === continuous;
29
- return { layer: part.layer, color: part.color, lineType: byLayer ? undefined : part.lineType };
37
+ return { layer: part.layer, color: part.color, lineType: byLayer ? undefined : part.lineType, lineWeight: lineWeightOf(part.lineWeight, millimetresPerUnit) };
30
38
  };
31
39
  const lineTypesOf = (parts) => [...new Set(parts.map(part => part.lineType))].filter(lineType => lineType !== Inputs.IO.lineTypeEnum.continuous);
32
40
  const versionOf = (value) => {
@@ -63,22 +71,30 @@ export class DxfGenerator {
63
71
  text.refuseWhenSpent();
64
72
  const curveTolerance = Math.max(givenTolerance, LEAST_TOLERANCE_SHARE * drawingSize(bounds, SPACE_AXES));
65
73
  const layers = layersOf(parts);
66
- const layout = { version, colorFormat, unit, bounds, layers: [...layers.values()], lineTypes: lineTypesOf(parts), patternUnit: patternUnitOf(bounds) };
74
+ const patternUnit = model.lineTypeScale === undefined ? patternUnitOf(bounds) : positiveNumber(model.lineTypeScale, "lineTypeScale");
75
+ const millimetresPerUnit = unit === undefined ? 1 : millimetresPerUnitOf(unit);
76
+ const layout = { version, colorFormat, unit, bounds, layers: [...layers.values()], lineTypes: lineTypesOf(parts), patternUnit };
67
77
  const handseed = writeHeader(text, layout);
68
78
  writeClasses(text, layout);
69
79
  const owners = writeTables(text, layout);
70
80
  writeBlocks(text, layout, owners);
71
81
  text.pair("0", "SECTION");
72
82
  text.pair("2", "ENTITIES");
73
- const context = { text, version, owner: owners.modelSpace, colorFormat, curveTolerance };
74
- for (const part of parts) {
75
- const style = entityStyleOf(part, layers);
83
+ const context = { text, version, owner: owners.modelSpace, colorFormat, curveTolerance, measurement: dxfMeasurementOf(unit) };
84
+ parts.forEach((part, at) => {
85
+ const style = entityStyleOf(part, layers, millimetresPerUnit);
86
+ for (const hatch of part.hatches) {
87
+ writeHatch(context, hatch, style, `dxfPathsParts[${at}].hatches`);
88
+ }
76
89
  for (const path of part.paths) {
77
90
  for (const segment of path.segments) {
78
91
  writeSegment(context, segment, style);
79
92
  }
80
93
  }
81
- }
94
+ for (const text of part.texts) {
95
+ writeText(context, text, style);
96
+ }
97
+ });
82
98
  text.pair("0", "ENDSEC");
83
99
  writeObjects(text, layout);
84
100
  text.pair("0", "EOF");
@@ -0,0 +1,9 @@
1
+ import type { FrameAxes } from "../../geometry/frame-axes";
2
+ import type { DxfEntityRecord, PlanarTurn, ReadHatch, ReadText } from "./dxf-reader-types";
3
+ import type { DxfMeasurement } from "./dxf-tables";
4
+ export declare const planarTurnOf: (map: FrameAxes) => PlanarTurn | undefined;
5
+ export declare const plainDxfText: (value: string) => string;
6
+ export declare const readTextOf: (record: DxfEntityRecord) => ReadText | string | undefined;
7
+ export declare const placedText: (text: ReadText, turn: PlanarTurn, map: FrameAxes) => ReadText;
8
+ export declare const readHatchOf: (record: DxfEntityRecord, measurement: DxfMeasurement) => ReadHatch | string | undefined;
9
+ export declare const placedHatch: (hatch: ReadHatch, turn: PlanarTurn, map: FrameAxes, isPlaced: boolean) => ReadHatch;
@@ -0,0 +1,312 @@
1
+ import * as Inputs from "../../../../inputs";
2
+ import { simplex } from "../../../../models/simplex";
3
+ import { angleWithinTurn } from "../../curves/conic-arcs";
4
+ import { pointToWorld } from "../../geometry/frame-axes";
5
+ import { degreesOf, radiansOf } from "../../geometry/scalars";
6
+ import { distance2, dot2, leftOf2, polar2, scale2, subtract2 } from "../../geometry/vectors";
7
+ import { strokeFontInk, strokeTextAdvance } from "../../text/stroke-font";
8
+ import { FULL_TURN_DEGREES, LONGEST_TEXT, QUARTER_TURN_DEGREES } from "../drawing/drawing.constants";
9
+ import { mappedSegments } from "./dxf-reader-geometry";
10
+ import { ANNOTATION_GROUP, GROUP, HATCH_EDGE, HATCH_POLYLINE_FLAG, MAX_SPLINE_DEGREE, MIDDLE_DROP_SHARE, SAME_ANGLE_DEGREES, SAME_TURN, TEXT_CODES, TEXT_CONTROLS, TEXT_JUSTIFIED, TEXT_VERTICAL, } from "./dxf-reader.constants";
11
+ import { decodedText, numberIn, numberOf, textOf, valueOf } from "./dxf-tokens";
12
+ import { DXF_EXTRUSION_CODES, DXF_HATCH_NAMES, DXF_HATCH_NATIVE_ANGLE, DXF_HATCH_NATIVE_SPACING } from "./dxf.constants";
13
+ const HATCH_DEFAULTS = new Inputs.IO.DrawingHatchDto();
14
+ const NAMED_PATTERNS = new Map(Object.values(Inputs.IO.hatchPatternEnum)
15
+ .filter(pattern => pattern !== Inputs.IO.hatchPatternEnum.solid)
16
+ .map(pattern => [DXF_HATCH_NAMES[pattern].toLowerCase(), pattern]));
17
+ const CUT_SHORT = "its boundary is cut short or is not one this reads";
18
+ const TOO_HIGH_DEGREE = `its boundary holds a spline of a degree above ${MAX_SPLINE_DEGREE}, the highest a kernel curve takes`;
19
+ class Unread extends Error {
20
+ constructor(reason) {
21
+ super(reason);
22
+ this.reason = reason;
23
+ }
24
+ }
25
+ class Cursor {
26
+ constructor(groups, at) {
27
+ this.groups = groups;
28
+ this.at = at;
29
+ }
30
+ peek(ahead = 0) {
31
+ var _a;
32
+ return (_a = this.groups[this.at + ahead]) === null || _a === void 0 ? void 0 : _a.code;
33
+ }
34
+ take(code) {
35
+ return numberIn(this.next(code));
36
+ }
37
+ pass(code) {
38
+ this.next(code);
39
+ }
40
+ count(code) {
41
+ const value = this.take(code);
42
+ if (!Number.isInteger(value) || value < 0 || value > this.groups.length - this.at) {
43
+ throw new Unread(CUT_SHORT);
44
+ }
45
+ return value;
46
+ }
47
+ seek(code) {
48
+ const found = this.groups.findIndex((group, at) => at >= this.at && group.code === code);
49
+ if (found >= 0) {
50
+ this.at = found;
51
+ }
52
+ return found >= 0;
53
+ }
54
+ next(code) {
55
+ while (this.at < this.groups.length) {
56
+ const group = this.groups[this.at];
57
+ this.at += 1;
58
+ if (group.code === code) {
59
+ return group;
60
+ }
61
+ }
62
+ throw new Unread(CUT_SHORT);
63
+ }
64
+ }
65
+ export const planarTurnOf = (map) => {
66
+ const scale = Math.hypot(map.x[0], map.x[1]);
67
+ const slack = SAME_TURN * Math.max(1, scale);
68
+ const flat = Math.abs(map.x[2]) <= slack && Math.abs(map.y[2]) <= slack && Math.abs(map.z[0]) <= slack && Math.abs(map.z[1]) <= slack && map.z[2] > 0;
69
+ const square = Math.abs(map.y[0] + map.x[1]) <= slack && Math.abs(map.y[1] - map.x[0]) <= slack;
70
+ return flat && square && scale > 0 ? { scale, rotation: Math.atan2(map.x[1], map.x[0]) } : undefined;
71
+ };
72
+ export const plainDxfText = (value) => decodedText(value).replace(TEXT_CODES, (_found, code, caret) => {
73
+ if (code === undefined) {
74
+ return caret === " " ? "^" : "";
75
+ }
76
+ switch (code.toLowerCase()) {
77
+ case "%":
78
+ return "%";
79
+ case "c":
80
+ return "⌀";
81
+ case "d":
82
+ return "°";
83
+ case "p":
84
+ return "±";
85
+ case "u":
86
+ case "o":
87
+ case "k":
88
+ return "";
89
+ default:
90
+ return String.fromCharCode(Number(code));
91
+ }
92
+ }).replace(TEXT_CONTROLS, "");
93
+ const facesUp = (record) => numberOf(record, DXF_EXTRUSION_CODES[0], 0) === 0 && numberOf(record, DXF_EXTRUSION_CODES[1], 0) === 0 && numberOf(record, DXF_EXTRUSION_CODES[2], 1) > 0;
94
+ const pointAt = (record, xCode, yCode, fallback) => [numberOf(record, xCode, fallback[0]), numberOf(record, yCode, fallback[1]), 0];
95
+ const flat = (point) => [point[0], point[1]];
96
+ const verticalOf = (record, justified) => {
97
+ if (justified === TEXT_JUSTIFIED.middle) {
98
+ return TEXT_VERTICAL.middle;
99
+ }
100
+ return justified <= TEXT_JUSTIFIED.right ? numberOf(record, ANNOTATION_GROUP.verticalJustified, TEXT_VERTICAL.baseline) : TEXT_VERTICAL.baseline;
101
+ };
102
+ const dropOf = (vertical, height) => {
103
+ switch (vertical) {
104
+ case TEXT_VERTICAL.bottom:
105
+ return strokeFontInk(simplex).lowest * height;
106
+ case TEXT_VERTICAL.middle:
107
+ return MIDDLE_DROP_SHARE * height;
108
+ case TEXT_VERTICAL.top:
109
+ return height;
110
+ default:
111
+ return 0;
112
+ }
113
+ };
114
+ const baselineOf = (point, rotation, drop) => {
115
+ if (drop === 0) {
116
+ return point;
117
+ }
118
+ const lowered = subtract2(flat(point), scale2(leftOf2(polar2(1, radiansOf(rotation))), drop));
119
+ return [lowered[0], lowered[1], point[2]];
120
+ };
121
+ export const readTextOf = (record) => {
122
+ var _a;
123
+ if (!facesUp(record)) {
124
+ return "it is drawn out of the drawing's plane";
125
+ }
126
+ const value = plainDxfText((_a = valueOf(record, ANNOTATION_GROUP.text)) !== null && _a !== void 0 ? _a : "");
127
+ if (value.trim() === "") {
128
+ return undefined;
129
+ }
130
+ if (value.length > LONGEST_TEXT) {
131
+ return `its text is longer than ${LONGEST_TEXT} characters`;
132
+ }
133
+ const given = numberOf(record, ANNOTATION_GROUP.height, 0);
134
+ if (!(given > 0)) {
135
+ return "it has no height";
136
+ }
137
+ const first = pointAt(record, GROUP.x, GROUP.y, [0, 0, 0]);
138
+ const second = pointAt(record, GROUP.otherX, ANNOTATION_GROUP.otherY, first);
139
+ const justified = numberOf(record, ANNOTATION_GROUP.justified, TEXT_JUSTIFIED.left);
140
+ const natural = strokeTextAdvance(simplex, value) * given;
141
+ const span = distance2(flat(second), flat(first));
142
+ const written = numberOf(record, ANNOTATION_GROUP.widthFactor, 1);
143
+ const factor = written > 0 ? written : 1;
144
+ if ((justified === TEXT_JUSTIFIED.fit || justified === TEXT_JUSTIFIED.aligned) && span > 0 && natural > 0) {
145
+ const fitted = justified === TEXT_JUSTIFIED.fit;
146
+ return {
147
+ value,
148
+ position: first,
149
+ height: fitted ? given : given * span / (natural * factor),
150
+ anchor: "start",
151
+ rotation: degreesOf(Math.atan2(second[1] - first[1], second[0] - first[0])),
152
+ widthFactor: fitted ? span / natural : factor,
153
+ };
154
+ }
155
+ const centred = justified === TEXT_JUSTIFIED.center || justified === TEXT_JUSTIFIED.middle;
156
+ const ended = justified === TEXT_JUSTIFIED.right;
157
+ const vertical = verticalOf(record, justified);
158
+ const rotation = numberOf(record, GROUP.rotation, 0);
159
+ const aligned = centred || ended || vertical !== TEXT_VERTICAL.baseline ? second : first;
160
+ return {
161
+ value,
162
+ position: baselineOf(aligned, rotation, dropOf(vertical, given)),
163
+ height: given,
164
+ anchor: centred ? "middle" : ended ? "end" : "start",
165
+ rotation,
166
+ widthFactor: factor,
167
+ };
168
+ };
169
+ export const placedText = (text, turn, map) => (Object.assign(Object.assign({}, text), { position: pointToWorld(map, text.position), height: text.height * turn.scale, rotation: text.rotation + degreesOf(turn.rotation) }));
170
+ const arcOf = (cursor, xAxis, yAxis, center) => {
171
+ const from = cursor.take(GROUP.startAngle);
172
+ const to = cursor.take(GROUP.endAngle);
173
+ const counterclockwise = cursor.take(ANNOTATION_GROUP.counterclockwise) !== 0;
174
+ const start = counterclockwise ? from : FULL_TURN_DEGREES - to;
175
+ const end = counterclockwise ? to : FULL_TURN_DEGREES - from;
176
+ const sweep = angleWithinTurn(end - start, FULL_TURN_DEGREES);
177
+ return { kind: "conic", center, xAxis, yAxis, start: radiansOf(angleWithinTurn(start, FULL_TURN_DEGREES)), sweep: radiansOf(sweep === 0 ? FULL_TURN_DEGREES : sweep) };
178
+ };
179
+ const splineEdgeOf = (cursor) => {
180
+ const degree = cursor.take(ANNOTATION_GROUP.edgeDegree);
181
+ const rational = cursor.take(ANNOTATION_GROUP.rational) !== 0;
182
+ cursor.take(ANNOTATION_GROUP.periodic);
183
+ const knotCount = cursor.count(ANNOTATION_GROUP.knotCount);
184
+ const pointCount = cursor.count(ANNOTATION_GROUP.pointCount);
185
+ const knots = Array.from({ length: knotCount }, () => cursor.take(GROUP.knot));
186
+ const points = [];
187
+ const weights = [];
188
+ for (let at = 0; at < pointCount; at += 1) {
189
+ points.push([cursor.take(GROUP.x), cursor.take(GROUP.y), 0]);
190
+ weights.push(rational && cursor.peek() === ANNOTATION_GROUP.edgeWeight ? cursor.take(ANNOTATION_GROUP.edgeWeight) : 1);
191
+ }
192
+ if (cursor.peek() === ANNOTATION_GROUP.fitCount && (cursor.peek(1) === GROUP.otherX || cursor.peek(1) === ANNOTATION_GROUP.sources)) {
193
+ const fitted = cursor.count(ANNOTATION_GROUP.fitCount);
194
+ for (let at = 0; at < fitted; at += 1) {
195
+ cursor.take(GROUP.otherX);
196
+ cursor.take(ANNOTATION_GROUP.otherY);
197
+ }
198
+ }
199
+ if (degree > MAX_SPLINE_DEGREE) {
200
+ throw new Unread(TOO_HIGH_DEGREE);
201
+ }
202
+ if (!Number.isInteger(degree) || degree < 1 || pointCount <= degree || knots.length !== pointCount + degree + 1 || weights.some(weight => !(weight > 0))) {
203
+ throw new Unread(CUT_SHORT);
204
+ }
205
+ return { kind: "spline", curve: { degree, points, weights, knots } };
206
+ };
207
+ const edgeOf = (cursor) => {
208
+ const kind = cursor.take(ANNOTATION_GROUP.edgeType);
209
+ if (kind === HATCH_EDGE.spline) {
210
+ return splineEdgeOf(cursor);
211
+ }
212
+ const first = [cursor.take(GROUP.x), cursor.take(GROUP.y), 0];
213
+ if (kind === HATCH_EDGE.line) {
214
+ return { kind: "line", start: first, end: [cursor.take(GROUP.otherX), cursor.take(ANNOTATION_GROUP.otherY), 0] };
215
+ }
216
+ if (kind === HATCH_EDGE.arc) {
217
+ const radius = cursor.take(GROUP.radius);
218
+ return arcOf(cursor, [radius, 0, 0], [0, radius, 0], first);
219
+ }
220
+ if (kind === HATCH_EDGE.ellipse) {
221
+ const major = [cursor.take(GROUP.otherX), cursor.take(ANNOTATION_GROUP.otherY), 0];
222
+ const ratio = cursor.take(GROUP.ratio);
223
+ return arcOf(cursor, major, [-major[1] * ratio, major[0] * ratio, 0], first);
224
+ }
225
+ throw new Unread(CUT_SHORT);
226
+ };
227
+ const loopOf = (cursor) => {
228
+ const flags = cursor.take(ANNOTATION_GROUP.loopFlags);
229
+ let segments;
230
+ if ((flags & HATCH_POLYLINE_FLAG) !== 0) {
231
+ const bulged = cursor.take(ANNOTATION_GROUP.hasBulge) !== 0;
232
+ cursor.take(ANNOTATION_GROUP.closedLoop);
233
+ const count = cursor.count(ANNOTATION_GROUP.vertexCount);
234
+ const points = [];
235
+ const bulges = [];
236
+ for (let at = 0; at < count; at += 1) {
237
+ points.push([cursor.take(GROUP.x), cursor.take(GROUP.y), 0]);
238
+ bulges.push(bulged && cursor.peek() === GROUP.bulge ? cursor.take(GROUP.bulge) : 0);
239
+ }
240
+ segments = points.length < 2 ? [] : [{ kind: "polyline", points, bulges, closed: true, normal: [0, 0, 1] }];
241
+ }
242
+ else {
243
+ segments = Array.from({ length: cursor.count(ANNOTATION_GROUP.vertexCount) }, () => edgeOf(cursor));
244
+ }
245
+ const sources = cursor.count(ANNOTATION_GROUP.sources);
246
+ for (let at = 0; at < sources; at += 1) {
247
+ cursor.pass(ANNOTATION_GROUP.owner);
248
+ }
249
+ return segments;
250
+ };
251
+ const linesOf = (cursor) => {
252
+ if (!cursor.seek(ANNOTATION_GROUP.lineCount)) {
253
+ return [];
254
+ }
255
+ return Array.from({ length: cursor.count(ANNOTATION_GROUP.lineCount) }, () => {
256
+ const angle = cursor.take(ANNOTATION_GROUP.lineAngle);
257
+ cursor.take(ANNOTATION_GROUP.baseX);
258
+ cursor.take(ANNOTATION_GROUP.baseY);
259
+ const offset = [cursor.take(ANNOTATION_GROUP.offsetX), cursor.take(ANNOTATION_GROUP.offsetY)];
260
+ const dashes = cursor.count(ANNOTATION_GROUP.dashCount);
261
+ for (let at = 0; at < dashes; at += 1) {
262
+ cursor.take(ANNOTATION_GROUP.dash);
263
+ }
264
+ return { angle, spacing: Math.abs(dot2(offset, leftOf2(polar2(1, radiansOf(angle))))) };
265
+ });
266
+ };
267
+ const apart = (first, second) => {
268
+ const turned = angleWithinTurn(second - first, FULL_TURN_DEGREES / 2);
269
+ return Math.min(turned, FULL_TURN_DEGREES / 2 - turned);
270
+ };
271
+ const patternOf = (record, lines, measurement) => {
272
+ const pattern = Inputs.IO.hatchPatternEnum;
273
+ if (numberOf(record, ANNOTATION_GROUP.solid, 0) === 1) {
274
+ return { pattern: pattern.solid, angle: HATCH_DEFAULTS.angle, spacing: HATCH_DEFAULTS.spacing, simplified: false };
275
+ }
276
+ const [first, second] = lines;
277
+ if (first === undefined || !(first.spacing > 0)) {
278
+ const named = NAMED_PATTERNS.get(textOf(record, GROUP.name).toLowerCase());
279
+ const known = named !== null && named !== void 0 ? named : pattern.ansi31;
280
+ const scale = numberOf(record, ANNOTATION_GROUP.patternScale, 1);
281
+ return {
282
+ pattern: known,
283
+ angle: angleWithinTurn(DXF_HATCH_NATIVE_ANGLE + numberOf(record, ANNOTATION_GROUP.patternAngle, 0), FULL_TURN_DEGREES),
284
+ spacing: DXF_HATCH_NATIVE_SPACING[measurement][known] * (scale > 0 ? scale : 1),
285
+ simplified: named === undefined,
286
+ };
287
+ }
288
+ const paired = second !== undefined && lines.length === 2 && apart(first.angle, second.angle) <= SAME_ANGLE_DEGREES;
289
+ const crossed = second !== undefined && lines.length === 2 && Math.abs(apart(first.angle, second.angle) - QUARTER_TURN_DEGREES) <= SAME_ANGLE_DEGREES;
290
+ return { pattern: paired ? pattern.ansi32 : crossed ? pattern.ansi37 : pattern.ansi31, angle: first.angle, spacing: first.spacing, simplified: lines.length > 1 && !paired && !crossed };
291
+ };
292
+ export const readHatchOf = (record, measurement) => {
293
+ if (!facesUp(record)) {
294
+ return "it is drawn out of the drawing's plane";
295
+ }
296
+ const start = record.groups.findIndex(group => group.code === ANNOTATION_GROUP.loopCount);
297
+ if (start < 0) {
298
+ return "it has no boundary";
299
+ }
300
+ try {
301
+ const cursor = new Cursor(record.groups, start);
302
+ const boundaries = Array.from({ length: cursor.count(ANNOTATION_GROUP.loopCount) }, () => loopOf(cursor)).filter(loop => loop.length > 0);
303
+ return boundaries.length === 0 ? undefined : Object.assign(Object.assign({}, patternOf(record, linesOf(cursor), measurement)), { boundaries });
304
+ }
305
+ catch (error) {
306
+ if (error instanceof Unread) {
307
+ return error.reason;
308
+ }
309
+ throw error;
310
+ }
311
+ };
312
+ export const placedHatch = (hatch, turn, map, isPlaced) => (Object.assign(Object.assign({}, hatch), { boundaries: isPlaced ? hatch.boundaries.map(loop => loop.flatMap(segment => mappedSegments(map, segment))) : hatch.boundaries, angle: hatch.angle + degreesOf(turn.rotation), spacing: hatch.spacing * turn.scale }));
@@ -1,3 +1,14 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
1
12
  import { unitOf } from "../../geometry/frame-axes";
2
13
  import { degreesOf } from "../../geometry/scalars";
3
14
  import { dot3, length3 } from "../../geometry/vectors";
@@ -54,10 +65,11 @@ const segmentDtos = (segment) => {
54
65
  }
55
66
  };
56
67
  export const drawingDtoOf = (drawing) => {
57
- var _a;
68
+ var _a, _b, _c;
58
69
  const parts = new Map();
59
70
  let fitSplines = 0;
60
71
  let flatArcs = 0;
72
+ let simplified = 0;
61
73
  for (const entity of drawing.entities) {
62
74
  const key = `${entity.style.layer}\n${entity.style.color}\n${entity.style.lineType}`;
63
75
  const part = (_a = parts.get(key)) !== null && _a !== void 0 ? _a : { layer: entity.style.layer, color: entity.style.color, lineType: entity.style.lineType, paths: [] };
@@ -69,13 +81,35 @@ export const drawingDtoOf = (drawing) => {
69
81
  if (segments.length > 0) {
70
82
  part.paths.push({ segments });
71
83
  }
84
+ if (entity.text !== undefined) {
85
+ const _d = entity.text, { value } = _d, placed = __rest(_d, ["value"]);
86
+ const texts = (_b = part.texts) !== null && _b !== void 0 ? _b : [];
87
+ part.texts = texts;
88
+ texts.push(Object.assign({ text: value }, placed));
89
+ }
90
+ if (entity.hatch !== undefined) {
91
+ const { boundaries, pattern, angle, spacing } = entity.hatch;
92
+ const paths = boundaries.map(loop => {
93
+ const read = loop.flatMap(segmentDtos);
94
+ const kept = read.filter((dto) => dto !== undefined);
95
+ flatArcs += read.length - kept.length;
96
+ return { segments: kept };
97
+ }).filter(path => path.segments.length > 0);
98
+ simplified += entity.hatch.simplified ? 1 : 0;
99
+ if (paths.length > 0) {
100
+ const hatches = (_c = part.hatches) !== null && _c !== void 0 ? _c : [];
101
+ part.hatches = hatches;
102
+ hatches.push({ boundaries: paths, pattern, angle, spacing });
103
+ }
104
+ }
72
105
  }
106
+ const simplifiedWarning = `${simplified} ${simplified === 1 ? "hatch has" : "hatches have"} a pattern other than single, paired or crossed lines; ${simplified === 1 ? "it is" : "they are"} read as single lines.`;
73
107
  const fitWarning = `${fitSplines} ${fitSplines === 1 ? "spline is" : "splines are"} given only by fit points, which need a kernel to interpolate; they were left out here, and occt.io.loadDxf reads them.`;
74
108
  const flatWarning = `${flatArcs} ${flatArcs === 1 ? "arc" : "arcs"} could not be drawn: a radius, a plane or a sweep is missing.`;
75
109
  return {
76
- parts: [...parts.values()].filter(part => part.paths.length > 0),
110
+ parts: [...parts.values()].filter(part => part.paths.length > 0 || part.texts !== undefined || part.hatches !== undefined),
77
111
  unit: drawing.unit,
78
112
  version: drawing.version,
79
- warnings: [...drawing.warnings, ...(fitSplines === 0 ? [] : [fitWarning]), ...(flatArcs === 0 ? [] : [flatWarning])],
113
+ warnings: [...drawing.warnings, ...(fitSplines === 0 ? [] : [fitWarning]), ...(flatArcs === 0 ? [] : [flatWarning]), ...(simplified === 0 ? [] : [simplifiedWarning])],
80
114
  };
81
115
  };
@@ -0,0 +1,9 @@
1
+ import type * as Inputs from "../../../../inputs";
2
+ import type { DxfEntityRecord, OwnSegments, ReadSegment } from "./dxf-reader-types";
3
+ type Point3 = Inputs.Base.Point3;
4
+ type Vec3 = Inputs.Base.Vector3;
5
+ export declare const segmentWork: (segment: ReadSegment) => number;
6
+ export declare const recordPointOf: (record: DxfEntityRecord, codes: readonly number[], fallback?: Point3) => Point3;
7
+ export declare const recordNormalOf: (record: DxfEntityRecord) => Vec3;
8
+ export declare const readSegmentsOf: (record: DxfEntityRecord) => OwnSegments;
9
+ export {};