-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8a3b44
commit 2c02b10
Showing
26 changed files
with
824 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ coverage | |
**/*.dwl2 | ||
**/*.err | ||
**/*.py | ||
**/*.svg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Colors, Writer, dline, point } from "@/index"; | ||
import { fileURLToPath, save } from "./utils"; | ||
import { svg } from "@/svg"; | ||
|
||
const writer = new Writer(); | ||
const modelSpace = writer.document.modelSpace; | ||
|
||
const green = writer.document.tables.addLayer({ | ||
name: "Green", | ||
colorNumber: Colors.Green, | ||
}); | ||
|
||
const blue = writer.document.tables.addLayer({ | ||
name: "Blue", | ||
colorNumber: Colors.Blue, | ||
}); | ||
|
||
modelSpace.currentLayerName = green.name; | ||
|
||
modelSpace.addLine({ start: point(), end: point(20, 20) }); | ||
modelSpace.addLine({ start: point(0, 2.5), end: point(20, 2.5) }); | ||
modelSpace.addLine({ start: point(0, 5), end: point(5, 10) }); | ||
|
||
modelSpace.currentLayerName = blue.name; | ||
|
||
const aligned = modelSpace.addAlignedDim({ | ||
start: point(), | ||
end: point(20, 20), | ||
offset: 5, | ||
}); | ||
|
||
writer.document.renderer.aligned(aligned); | ||
|
||
|
||
const angular = modelSpace.addAngularLinesDim({ | ||
firstLine: dline(point(0, 2.5), point(20, 2.5)), | ||
secondLine: dline(point(0, 5), point(5, 10)), | ||
positionArc: point(15, 10), | ||
middle: point(6.575676, 6.259268), | ||
measurement: 0.785398, | ||
}); | ||
|
||
writer.document.renderer.angularLines(angular); | ||
|
||
save(svg(writer.document), fileURLToPath(import.meta.url), ".svg"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Point2D, Point3D, WithSeeder } from "@/types"; | ||
import { Seeder, point, point2d } from "@/utils"; | ||
import { Solid } from "@/entities"; | ||
import { transform } from "@/helpers"; | ||
|
||
export interface DimensionArrowOptions extends WithSeeder { | ||
size?: number; | ||
rotation?: number; | ||
position?: Point3D; | ||
} | ||
|
||
export function arrow(options: DimensionArrowOptions) { | ||
return new DimensionArrow(options).entity(); | ||
} | ||
|
||
export class DimensionArrow { | ||
readonly seeder: Seeder; | ||
size: number; | ||
rotation: number; | ||
position: Point3D; | ||
|
||
constructor(options: DimensionArrowOptions) { | ||
this.seeder = options.seeder; | ||
this.size = options.size ?? 2.5; | ||
this.rotation = options.rotation ?? 0; | ||
this.position = options.position ?? point(); | ||
} | ||
|
||
entity() { | ||
const { size: s, seeder } = this; | ||
const h = this.size / 3 / 2; | ||
return new Solid({ | ||
seeder, | ||
first: this.position, | ||
second: this._transform(point2d(-s, -h)), | ||
third: this._transform(point2d(-s, h)), | ||
}); | ||
} | ||
|
||
private _transform(target: Point2D) { | ||
const result = transform({ | ||
target, | ||
center: this.position, | ||
angle: this.rotation, | ||
translation: this.position, | ||
}); | ||
return point(result.x, result.y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./arrow"; | ||
export * from "./renderer"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { AlignedDimension, AngularLinesDimension, AttachmentPoint, arrow } from "@/entities"; | ||
import { ArcPrimitive, LinePrimitive, PI, calculateAngle, linep } from "@/helpers"; | ||
import { Block, Blocks } from "@/blocks"; | ||
import { Seeder, angle, deg, point, polar } from "@/utils"; | ||
import { Tables } from "@/tables"; | ||
import { WithSeeder } from "@/types"; | ||
|
||
function round(v: number, accuracy = 0.01) { | ||
const EPSILON = Number.EPSILON || Math.pow(2, -52); | ||
const temp = 1 / accuracy; | ||
return Math.round((v + EPSILON) * temp) / temp; | ||
} | ||
|
||
export interface DimensionRendererOptions extends WithSeeder { | ||
blocks: Blocks; | ||
tables: Tables; | ||
} | ||
|
||
export class DimensionRenderer implements WithSeeder { | ||
readonly seeder: Seeder; | ||
readonly blocks: Blocks; | ||
readonly tables: Tables; | ||
|
||
private _seed: number; | ||
private get _blockName() { | ||
return `*D${++this._seed}`; | ||
} | ||
|
||
constructor(options: DimensionRendererOptions) { | ||
this.seeder = options.seeder; | ||
this.blocks = options.blocks; | ||
this.tables = options.tables; | ||
this._seed = 0; | ||
} | ||
|
||
aligned(dim: AlignedDimension) { | ||
const { seeder } = this.blocks; | ||
const rotation = calculateAngle(dim.start, dim.end); | ||
const sign = Math.sign(dim.offset); | ||
const block = this.blocks.addBlock({ name: this._blockName }); | ||
block.currentLayerName = dim.layerName || "0"; | ||
const angle = rotation + (PI / 2) * sign; | ||
const start = polar(dim.start, deg(angle), dim.offset); | ||
const end = polar(dim.end, deg(angle), dim.offset); | ||
block.push(arrow({ seeder, rotation: rotation - PI, position: start })); | ||
block.push(arrow({ seeder, rotation, position: end })); | ||
linep(start, end).trimStart(2.5).trimEnd(2.5).write(block); | ||
this._trimExpand(linep(dim.start, start), block); | ||
this._trimExpand(linep(dim.end, end), block); | ||
const layerName = this.tables.addDefpointsLayer(); | ||
block.addPoint({ ...dim.start, layerName }); | ||
block.addPoint({ ...dim.end, layerName }); | ||
|
||
const distance = round(linep(dim.start, dim.end).length); | ||
const middle = linep(start, end).middle; | ||
|
||
dim.textRotation = deg(rotation); | ||
dim.measurement = distance; | ||
dim.middle = polar(middle, 90 * sign, 1.25); | ||
block.addMText({ | ||
insertionPoint: dim.middle, | ||
height: 2.5, | ||
value: distance.toString(), | ||
rotation: deg(rotation), | ||
attachmentPoint: AttachmentPoint.BottomCenter, | ||
}); | ||
|
||
dim.blockName = block.name; | ||
} | ||
|
||
angularLines(dim: AngularLinesDimension) { | ||
const { seeder } = this.blocks; | ||
const block = this.blocks.addBlock({ name: this._blockName }); | ||
block.currentLayerName = dim.layerName || "0"; | ||
const fline = linep(dim.firstLine.start, dim.firstLine.end); | ||
const sline = linep(dim.secondLine.start, dim.secondLine.end); | ||
const intersection = fline.intersect(sline); | ||
if (intersection == null) return; | ||
const center = point(intersection.x, intersection.y); | ||
const radius = linep(center, dim.positionArc).length; | ||
const startAngle = angle(dim.firstLine.start, dim.firstLine.end); | ||
const endAngle = angle(dim.secondLine.start, dim.secondLine.end); | ||
const arc = new ArcPrimitive({ center, endAngle, startAngle, radius }); | ||
const tarc = arc.trimStart(2.5, true).trimEnd(2.5, true); | ||
tarc.write(block); | ||
block.push(arrow({ | ||
seeder, | ||
rotation: calculateAngle(tarc.start, arc.start), | ||
position: arc.start | ||
})); | ||
block.push(arrow({ | ||
seeder, | ||
rotation: calculateAngle(tarc.end, arc.end), | ||
position: arc.end | ||
})); | ||
dim.middle = polar(arc.middle, arc.middleAngle, 1.25); | ||
block.addMText({ | ||
insertionPoint: dim.middle, | ||
height: 2.5, | ||
value: `${arc.angle.toFixed(2)}°`, | ||
rotation: arc.middleAngle - 90, | ||
attachmentPoint: AttachmentPoint.BottomCenter, | ||
}); | ||
|
||
if (fline.length <= radius) { | ||
this._trimExpand(linep(fline.end, arc.start), block); | ||
} | ||
|
||
if (sline.length <= radius) { | ||
this._trimExpand(linep(sline.end, arc.end), block); | ||
} | ||
|
||
dim.blockName = block.name; | ||
} | ||
|
||
private _trimExpand(line: LinePrimitive, block: Block) { | ||
line.trimStart(0.625).expandEnd(1.25).write(block); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.