mirror of https://gitee.com/cf-fz/WebCAD.git
!1951 开发:画多边形
parent
7f2880a87f
commit
783aaa85fd
@ -0,0 +1,104 @@
|
||||
import { Intent } from "@blueprintjs/core";
|
||||
import { Vector3 } from "three";
|
||||
import { app } from "../ApplicationServices/Application";
|
||||
import { TransformVector } from "../Common/Matrix4Utils";
|
||||
import { Circle } from "../DatabaseServices/Entity/Circle";
|
||||
import { Polyline } from "../DatabaseServices/Entity/Polyline";
|
||||
import { Command } from "../Editor/CommandMachine";
|
||||
import { JigUtils } from "../Editor/JigUtils";
|
||||
import { PromptStatus } from "../Editor/PromptResult";
|
||||
import { angle } from "../Geometry/GeUtils";
|
||||
import { AppToaster } from "../UI/Components/Toaster";
|
||||
|
||||
export class DrawPolygon implements Command
|
||||
{
|
||||
async exec()
|
||||
{
|
||||
let countRes = await app.Editor.GetDistance({
|
||||
Msg: "请指定多边形边数",
|
||||
Default: 3,
|
||||
});
|
||||
if (countRes.Status === PromptStatus.Cancel) return;
|
||||
|
||||
let count = countRes.Distance;
|
||||
if (count < 3 || count > 1024)
|
||||
{
|
||||
AppToaster.show({
|
||||
message: "多边形边数最少为3,最多为1024!",
|
||||
timeout: 5000,
|
||||
intent: Intent.WARNING,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
let cenRes = await app.Editor.GetPoint({
|
||||
Msg: "请指定一个点",
|
||||
});
|
||||
if (cenRes.Status === PromptStatus.Cancel) return;
|
||||
|
||||
let center = cenRes.Point;
|
||||
|
||||
let isInscribedCircle = true;//内切圆
|
||||
let cir = new Circle();
|
||||
|
||||
let pl = new Polyline();
|
||||
pl.ApplyMatrix(app.Editor.UCSMatrix);
|
||||
pl.Position = center;
|
||||
pl.CloseMark = true;
|
||||
let ocsInv = pl.OCSInv;
|
||||
|
||||
let plJig = JigUtils.Draw(pl);
|
||||
|
||||
let pts: Vector3[] = [];
|
||||
let divParam = 1 / count;
|
||||
|
||||
const UpdataPolygon = (point: Vector3, polyline: Polyline) =>
|
||||
{
|
||||
let redius = center.distanceTo(point);
|
||||
let ang = angle(TransformVector(point.clone().sub(center), ocsInv));
|
||||
|
||||
if (!isInscribedCircle)
|
||||
{
|
||||
redius = redius / Math.cos(Math.PI / count);
|
||||
ang -= Math.PI / count;
|
||||
}
|
||||
|
||||
cir.Radius = redius;
|
||||
|
||||
for (let i = 0; i < count; i++)
|
||||
{
|
||||
let pt = cir.GetPointAtParam(divParam * i + ang / (Math.PI * 2));
|
||||
pts[i] = pt;
|
||||
}
|
||||
|
||||
polyline.FromPoints2d(pts);
|
||||
};
|
||||
|
||||
while (true)
|
||||
{
|
||||
let radiusRes = await app.Editor.GetPoint({
|
||||
Msg: `指定圆的半径${isInscribedCircle ? "<内切与圆>" : "<外切与圆>"}:`,
|
||||
BasePoint: center,
|
||||
KeyWordList: isInscribedCircle ? [{ msg: "外切与圆", key: "O" }] : [{ msg: "内切与圆", key: "I" }],
|
||||
Callback: (pt: Vector3) => UpdataPolygon(pt, plJig),
|
||||
});
|
||||
|
||||
if (radiusRes.Status === PromptStatus.OK)
|
||||
{
|
||||
UpdataPolygon(radiusRes.Point, pl);
|
||||
app.LayoutTool.AppendDatabaseSpace(pl);
|
||||
break;
|
||||
}
|
||||
else if (radiusRes.Status === PromptStatus.Keyword)
|
||||
{
|
||||
if (radiusRes.StringResult === "O")
|
||||
isInscribedCircle = false;
|
||||
else if (radiusRes.StringResult === "I")
|
||||
isInscribedCircle = true;
|
||||
}
|
||||
else if (radiusRes.Status === PromptStatus.Cancel)
|
||||
break;
|
||||
}
|
||||
JigUtils.Destroy();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue