easyeda 0.0.380 → 0.0.382

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -11999,8 +11999,10 @@ var Polygon$1 = class Polygon {
11999
11999
  return edge;
12000
12000
  }
12001
12001
  /**
12002
- * Split polygon into array of polygons, where each polygon is an outer face with all
12003
- * containing inner faces
12002
+ * Split polygon into an array of islands with their contained holes.
12003
+ * The largest face defines the outer orientation; opposite-oriented faces represent voids.
12004
+ * Holes not contained in any island are omitted from the result, without changing
12005
+ * their orientation or the input polygon. This method does not clip overlapping faces.
12004
12006
  * @returns {Flatten.Polygon[]}
12005
12007
  */
12006
12008
  splitToIslands() {
@@ -12045,7 +12047,7 @@ var Polygon$1 = class Polygon {
12045
12047
  }
12046
12048
  /**
12047
12049
  * Helper method to check if face is outer face of the polygon
12048
- * @param face
12050
+ * @param {Flatten.Face} face - face to check
12049
12051
  * @returns {boolean}
12050
12052
  */
12051
12053
  isOuter(face) {
@@ -12830,14 +12832,41 @@ var Distance = class _Distance {
12830
12832
  };
12831
12833
  Flatten.Distance = Distance;
12832
12834
  var { Multiline: Multiline2, Point: Point2, Segment: Segment2, Polygon: Polygon2 } = Flatten;
12835
+ function splitComponents(str) {
12836
+ const components = [];
12837
+ let depth = 0;
12838
+ let start = 0;
12839
+ for (let i = 0; i < str.length; i++) {
12840
+ if (str[i] === "(") depth++;
12841
+ if (str[i] === ")") depth--;
12842
+ if (depth < 0) throw Flatten.Errors.ILLEGAL_PARAMETERS;
12843
+ if (str[i] === "," && depth === 0) {
12844
+ components.push(str.slice(start, i).trim());
12845
+ start = i + 1;
12846
+ }
12847
+ }
12848
+ if (depth !== 0) throw Flatten.Errors.ILLEGAL_PARAMETERS;
12849
+ components.push(str.slice(start).trim());
12850
+ return components;
12851
+ }
12852
+ function unwrap(str) {
12853
+ str = str.trim();
12854
+ if (str === "EMPTY") throw new Error("Empty geometry not supported");
12855
+ if (!str.startsWith("(") || !str.endsWith(")")) {
12856
+ throw Flatten.Errors.ILLEGAL_PARAMETERS;
12857
+ }
12858
+ return str.slice(1, -1).trim();
12859
+ }
12833
12860
  function parseSinglePoint(pointStr) {
12834
- return new Point2(pointStr.split(" ").map(Number));
12861
+ pointStr = pointStr.trim();
12862
+ if (pointStr === "EMPTY") throw new Error("Empty geometry not supported");
12863
+ return new Point2(pointStr.split(/\s+/).map(Number));
12835
12864
  }
12836
12865
  function parseMultiPoint(multipointStr) {
12837
- return multipointStr.split(", ").map(parseSinglePoint);
12866
+ return splitComponents(multipointStr).map((str) => parseSinglePoint(str.startsWith("(") ? unwrap(str) : str));
12838
12867
  }
12839
12868
  function parseLineString(lineStr) {
12840
- const points = parseMultiPoint(lineStr);
12869
+ const points = splitComponents(lineStr).map(parseSinglePoint);
12841
12870
  let segments = [];
12842
12871
  for (let i = 0; i < points.length - 1; i++) {
12843
12872
  segments.push(new Segment2(points[i], points[i + 1]));
@@ -12845,45 +12874,31 @@ function parseLineString(lineStr) {
12845
12874
  return new Multiline2(segments);
12846
12875
  }
12847
12876
  function parseMultiLineString(multilineStr) {
12848
- const lineStrings = multilineStr.replace(/\(\(/, "").replace(/\)\)$/, "").split("), (");
12849
- return lineStrings.map(parseLineString);
12877
+ return splitComponents(multilineStr).map((str) => parseLineString(unwrap(str)));
12850
12878
  }
12851
12879
  function parseSinglePolygon(polygonStr) {
12852
- const facesStr = polygonStr.replace(/\(\(/, "").replace(/\)\)$/, "").split("), (");
12853
12880
  const polygon2 = new Polygon2();
12881
+ if (polygonStr.trim() === "EMPTY") return polygon2;
12882
+ const facesStr = splitComponents(unwrap(polygonStr));
12854
12883
  let orientation2;
12855
- facesStr.forEach((facesStr2, idx) => {
12856
- let points = facesStr2.split(", ").map((coordStr) => {
12857
- return new Point2(coordStr.split(" ").map(Number));
12858
- });
12884
+ facesStr.forEach((faceStr, idx) => {
12885
+ const points = splitComponents(unwrap(faceStr)).map(parseSinglePoint);
12859
12886
  const face = polygon2.addFace(points);
12860
12887
  if (idx === 0) {
12861
12888
  orientation2 = face.orientation();
12862
- } else {
12863
- if (face.orientation() === orientation2) {
12864
- face.reverse();
12865
- }
12889
+ } else if (face.orientation() === orientation2) {
12890
+ face.reverse();
12866
12891
  }
12867
12892
  });
12868
12893
  return polygon2;
12869
12894
  }
12870
- function parseMutliPolygon(multiPolygonString) {
12871
- const polygonStrings = multiPolygonString.split(/\)\), \(\(/).map((polygon3) => "((" + polygon3 + "))");
12872
- const polygons = polygonStrings.map(parseSinglePolygon);
12895
+ function parseMultiPolygon(multiPolygonStr) {
12873
12896
  const polygon2 = new Polygon2();
12874
- const faces = polygons.reduce((acc, polygon3) => [...acc, ...polygon3?.faces], []);
12875
- faces.forEach((face) => polygon2.addFace([...face?.shapes]));
12897
+ splitComponents(multiPolygonStr).map(parseSinglePolygon).forEach((part) => {
12898
+ part.faces.forEach((face) => polygon2.addFace([...face.shapes]));
12899
+ });
12876
12900
  return polygon2;
12877
12901
  }
12878
- function parsePolygon(wkt) {
12879
- if (wkt.startsWith("POLYGON")) {
12880
- const polygonStr = wkt.replace(/^POLYGON /, "");
12881
- return parseSinglePolygon(polygonStr);
12882
- } else {
12883
- const multiPolygonString = wkt.replace(/^MULTIPOLYGON \(\(\((.*)\)\)\)$/, "$1");
12884
- return parseMutliPolygon(multiPolygonString);
12885
- }
12886
- }
12887
12902
  function parseArrayOfPoints(str) {
12888
12903
  const arr = str.split("\n").map((x) => x.match(/\(([^)]+)\)/)[1]);
12889
12904
  return arr.map(parseSinglePoint);
@@ -12893,28 +12908,39 @@ function parseArrayOfLineStrings(str) {
12893
12908
  return arr.map(parseLineString).reduce((acc, x) => [...acc, ...x], []);
12894
12909
  }
12895
12910
  function parseWKT(str) {
12896
- if (str.startsWith("POINT")) {
12897
- const pointStr = str.replace(/^POINT \(/, "").replace(/\)$/, "");
12898
- return parseSinglePoint(pointStr);
12899
- } else if (str.startsWith("MULTIPOINT")) {
12900
- const multiPointStr = str.replace(/^MULTIPOINT \(/, "").replace(/\)$/, "");
12901
- return parseMultiPoint(multiPointStr);
12902
- } else if (str.startsWith("LINESTRING")) {
12903
- const lineStr = str.replace(/^LINESTRING \(/, "").replace(/\)$/, "");
12904
- return parseLineString(lineStr);
12905
- } else if (str.startsWith("MULTILINESTRING")) {
12906
- const multilineStr = str.replace(/^MULTILINESTRING /, "");
12907
- return parseMultiLineString(multilineStr);
12908
- } else if (str.startsWith("POLYGON") || str.startsWith("MULTIPOLYGON")) {
12909
- return parsePolygon(str);
12910
- } else if (str.startsWith("GEOMETRYCOLLECTION")) {
12911
- const regex = /(?<type>POINT|LINESTRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON) \((?:[^\(\)]|\([^\)]*\))*\)/g;
12912
- const wktArray = str.match(regex);
12913
- if (wktArray[0].startsWith("GEOMETRYCOLLECTION")) {
12914
- wktArray[0] = wktArray[0].replace("GEOMETRYCOLLECTION (", "");
12915
- }
12916
- const flArray = wktArray.map(parseWKT).map((x) => x instanceof Array ? x : [x]);
12917
- return flArray.reduce((acc, x) => [...acc, ...x], []);
12911
+ const records = str.trim().split("\n");
12912
+ if (records.length > 1) {
12913
+ if (records.every((record) => /^\s*POINT\s*\([^()]*\)\s*$/.test(record))) {
12914
+ return parseArrayOfPoints(str.trim());
12915
+ }
12916
+ if (records.every((record) => /^\s*LINESTRING\s*\([^()]*\)\s*$/.test(record))) {
12917
+ return parseArrayOfLineStrings(str.trim());
12918
+ }
12919
+ }
12920
+ const geometry = str.trim().match(/^(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING|POLYGON|MULTIPOLYGON|GEOMETRYCOLLECTION)\s*(EMPTY|\([\s\S]*\))$/);
12921
+ if (geometry) {
12922
+ const [, type, body] = geometry;
12923
+ if (body === "EMPTY") {
12924
+ if (type === "POLYGON") return new Polygon2();
12925
+ throw new Error("Empty geometry not supported");
12926
+ }
12927
+ const content = unwrap(body);
12928
+ switch (type) {
12929
+ case "POINT":
12930
+ return parseSinglePoint(content);
12931
+ case "MULTIPOINT":
12932
+ return parseMultiPoint(content);
12933
+ case "LINESTRING":
12934
+ return parseLineString(content);
12935
+ case "MULTILINESTRING":
12936
+ return parseMultiLineString(content);
12937
+ case "POLYGON":
12938
+ return parseSinglePolygon(body);
12939
+ case "MULTIPOLYGON":
12940
+ return parseMultiPolygon(content);
12941
+ case "GEOMETRYCOLLECTION":
12942
+ return splitComponents(content).map(parseWKT).reduce((acc, shapes) => acc.concat(shapes), []);
12943
+ }
12918
12944
  } else if (isArrayOfPoints(str)) {
12919
12945
  return parseArrayOfPoints(str);
12920
12946
  } else if (isArrayOfLines(str)) {
@@ -14448,22 +14474,15 @@ function generateArcFromSweep(startX, startY, endX, endY, radius, largeArcFlag,
14448
14474
  const centerY = midY - h * Math.cos(angle) * centerDirection;
14449
14475
  const startAngle = Math.atan2(startY - centerY, startX - centerX);
14450
14476
  let endAngle = Math.atan2(endY - centerY, endX - centerX);
14451
- if (sweepFlag && endAngle > startAngle) {
14452
- endAngle -= 2 * Math.PI;
14453
- } else if (!sweepFlag && endAngle < startAngle) {
14477
+ if (sweepFlag && endAngle < startAngle) {
14454
14478
  endAngle += 2 * Math.PI;
14479
+ } else if (!sweepFlag && endAngle > startAngle) {
14480
+ endAngle -= 2 * Math.PI;
14455
14481
  }
14456
- if (!largeArcFlag && Math.abs(endAngle - startAngle) > Math.PI || largeArcFlag && Math.abs(endAngle - startAngle) < Math.PI) {
14457
- if (endAngle > startAngle) {
14458
- endAngle -= 2 * Math.PI;
14459
- } else {
14460
- endAngle += 2 * Math.PI;
14461
- }
14462
- }
14463
- const numPoints = Math.max(
14464
- 2,
14465
- Math.ceil(Math.abs(endAngle - startAngle) * radius)
14466
- );
14482
+ const arcAngle = Math.abs(endAngle - startAngle);
14483
+ const minNumPoints = Math.max(2, Math.ceil(arcAngle * radius));
14484
+ const isSemicircle = Math.abs(arcAngle - Math.PI) < 1e-6;
14485
+ const numPoints = isSemicircle ? minNumPoints + minNumPoints % 2 : minNumPoints;
14467
14486
  const path = [];
14468
14487
  for (let i = 0; i <= numPoints; i++) {
14469
14488
  const t = i / numPoints;
@@ -14486,30 +14505,37 @@ var expandEasyEdaPathData = (pathData) => {
14486
14505
  const command = commandMatch[1]?.toUpperCase();
14487
14506
  const values = commandMatch[2]?.match(/[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?/g)?.map(Number) ?? [];
14488
14507
  if ((command === "M" || command === "L") && values.length >= 2) {
14489
- currentPoint = { x: values[0], y: values[1] };
14490
- rawRoute.push(currentPoint);
14508
+ for (let offset = 0; offset + 1 < values.length; offset += 2) {
14509
+ currentPoint = { x: values[offset], y: values[offset + 1] };
14510
+ rawRoute.push(currentPoint);
14511
+ }
14491
14512
  } else if (command === "A" && currentPoint && values.length >= 7) {
14492
- const [radiusX, , , largeArcFlag, sweepFlag, endX, endY] = values;
14493
- const generatedArcRoute = generateArcFromSweep(
14494
- currentPoint.x,
14495
- currentPoint.y,
14496
- endX,
14497
- endY,
14498
- radiusX,
14499
- largeArcFlag === 1,
14500
- sweepFlag === 1
14501
- );
14502
- const maxArcSegments = 16;
14503
- const arcRoute = generatedArcRoute.length <= maxArcSegments + 1 ? generatedArcRoute : Array.from(
14504
- { length: maxArcSegments + 1 },
14505
- (_, pointIndex) => generatedArcRoute.at(
14506
- Math.round(
14507
- pointIndex * (generatedArcRoute.length - 1) / maxArcSegments
14513
+ for (let offset = 0; offset + 6 < values.length; offset += 7) {
14514
+ const [radiusX, , , largeArcFlag, sweepFlag, endX, endY] = values.slice(
14515
+ offset,
14516
+ offset + 7
14517
+ );
14518
+ const generatedArcRoute = generateArcFromSweep(
14519
+ currentPoint.x,
14520
+ currentPoint.y,
14521
+ endX,
14522
+ endY,
14523
+ radiusX,
14524
+ largeArcFlag === 1,
14525
+ sweepFlag === 1
14526
+ );
14527
+ const maxArcSegments = 16;
14528
+ const arcRoute = generatedArcRoute.length <= maxArcSegments + 1 ? generatedArcRoute : Array.from(
14529
+ { length: maxArcSegments + 1 },
14530
+ (_, pointIndex) => generatedArcRoute.at(
14531
+ Math.round(
14532
+ pointIndex * (generatedArcRoute.length - 1) / maxArcSegments
14533
+ )
14508
14534
  )
14509
- )
14510
- ).filter((point4) => !!point4);
14511
- rawRoute.push(...arcRoute.slice(1));
14512
- currentPoint = { x: endX, y: endY };
14535
+ ).filter((point4) => !!point4);
14536
+ rawRoute.push(...arcRoute.slice(1));
14537
+ currentPoint = { x: endX, y: endY };
14538
+ }
14513
14539
  }
14514
14540
  }
14515
14541
  return rawRoute;
@@ -16587,7 +16613,7 @@ var PolygonShapeOutputSchema = z223.object({
16587
16613
  lineColor: z223.string(),
16588
16614
  id: z223.string()
16589
16615
  });
16590
- var parsePolygon2 = (str) => {
16616
+ var parsePolygon = (str) => {
16591
16617
  const [, ...rest] = str.split("~");
16592
16618
  const [pointsStr, lineColor, lineWidth, , fillColor, id] = rest;
16593
16619
  return {
@@ -16599,7 +16625,7 @@ var parsePolygon2 = (str) => {
16599
16625
  id
16600
16626
  };
16601
16627
  };
16602
- var PolygonShapeSchema = z223.string().startsWith("PG~").transform(parsePolygon2).pipe(PolygonShapeOutputSchema);
16628
+ var PolygonShapeSchema = z223.string().startsWith("PG~").transform(parsePolygon).pipe(PolygonShapeOutputSchema);
16603
16629
  var PathShapeOutputSchema = z223.object({
16604
16630
  type: z223.literal("PATH"),
16605
16631
  pathData: z223.string(),