!1951 开发:画多边形

pull/1937/MERGE
黄诗津 2 years ago committed by ChenX
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();
}
}

@ -15,6 +15,7 @@ export enum CommandNames
Undo = "UNDO",
Redo = "REDO",
RECTANG = "RECTANG",
Polygon = "POLYGON",
Circle = "CIRCLE",
Ellipse = "ELLIPSE",
Spline = "SPLINE",

@ -1,4 +1,4 @@
import { Box3, Matrix3, Matrix4, Shape, Vector2, Vector3 } from 'three';
import { Box3, Matrix3, Matrix4, Shape, Vec2, Vector2, Vector3 } from 'three';
import { arrayLast, arrayRemoveDuplicateBySort, changeArrayStartIndex } from '../../Common/ArrayExt';
import { ComputerCurvesNormalOCS, getDeterminantFor2V } from '../../Common/CurveUtils';
import { matrixAlignCoordSys, matrixIsCoplane, reviseMirrorMatrix } from '../../Common/Matrix4Utils';
@ -254,6 +254,7 @@ export class Polyline extends Curve
this.Reverse();
return this;
}
SetBulgeAt(index: number, bul: number): this
{
let d = this._LineData[index];
@ -265,10 +266,12 @@ export class Polyline extends Curve
}
return this;
}
GetBulgeAt(index: number): number
{
return this._LineData[index].bul;
}
Rectangle(length: number, height: number): this
{
this.LineData = [
@ -279,6 +282,7 @@ export class Polyline extends Curve
this.CloseMark = true;
return this;
}
RectangleFrom2Pt(p1: Vector3, p2: Vector3): this
{
let box = new Box3();
@ -298,6 +302,17 @@ export class Polyline extends Curve
this.CloseMark = true;
return this;
}
FromPoints2d(pts: Vec2[]): this
{
this.WriteAllObjectRecord();
this._LineData.length = 0;
for (let p of pts)
this._LineData.push({ pt: AsVector2(p), bul: 0 });
this.Update();
return this;
}
//多段线起点
get StartPoint()
{

@ -94,6 +94,7 @@ import { DrawRectAreaLight } from "../Add-on/DrawLight/DrawRectAreaLight";
import { DrawSpotLight, DrawSpotLight2 } from "../Add-on/DrawLight/DrawSpotLight";
import { Command_DrawXLine, DrawLine } from "../Add-on/DrawLine";
import { CMD_DrawPoint } from "../Add-on/DrawPoint";
import { DrawPolygon } from "../Add-on/DrawPolygon";
import { DrawPolyline } from "../Add-on/DrawPolyline";
import { DrawRect } from "../Add-on/DrawRect";
import { DrawRegion } from "../Add-on/DrawRegion";
@ -313,6 +314,7 @@ export function registerCommand()
commandMachine.RegisterCommand(CommandNames.Zoome, new ZoomE());
commandMachine.RegisterCommand(CommandNames.ZoomObject, new Command_ZoomObject());
commandMachine.RegisterCommand(CommandNames.RECTANG, new DrawRect());
commandMachine.RegisterCommand(CommandNames.Polygon, new DrawPolygon());
commandMachine.RegisterCommand(CommandNames.Circle, new DrawCircle());
commandMachine.RegisterCommand(CommandNames.Ellipse, new DrawEllipse());
commandMachine.RegisterCommand(CommandNames.Spline, new DrawSpline());

@ -99,6 +99,17 @@ export const CommandList: ICommand[] = [
// enName: "Rectangle",
chDes: "画一个矩形",
},
{
icon: IconEnum.Rectangle,
typeId: "i2d",
link: `#`,
defaultCustom: "POLYGON",
command: CommandNames.Polygon,
type: "二维",
chName: "多边形",
// enName: "Polygon",
chDes: "画一个多边形",
},
{
typeId: "i2d",
link: `#`,

@ -39,6 +39,7 @@ export class TopToolBar extends React.Component<{}, {}>
{ svg: IconEnum.CircleWithCrossDottedLine, title: "圆", command: CommandNames.Circle },
{ svg: IconEnum.PolyLine, title: "多段线", command: CommandNames.Polyline },
{ svg: IconEnum.Rectangle, title: "矩形", command: CommandNames.RECTANG },
{ svg: IconEnum.Rectangle, title: "多边形", command: CommandNames.Polygon },
// { svg: IconEnum.SpLine, title: "样条曲线", command:CommandNames.Spline },
{ svg: IconEnum.Region, title: "面域", command: CommandNames.Reg },
{ svg: IconEnum.Text, title: "文字", command: CommandNames.Text },

Loading…
Cancel
Save