!152 正常绘制椭圆,s拉伸和夹点拖拽,修复背板模态框小bug

Merge pull request !152 from ZoeLeeFZ/ellipse_opt
pull/152/MERGE
ChenX 6 years ago
parent e0b62d49d1
commit 5ad127f23f

@ -3,6 +3,8 @@ import { Command } from "../Editor/CommandMachine";
import { PromptStatus } from "../Editor/PromptResult";
import { Ellipse } from "../DatabaseServices/Ellipse";
import { RenderType } from "../GraphicsSystem/Enum";
import { Jig } from "../Editor/Jig";
import { Vector3, Matrix4 } from "three";
/**
*
*
@ -14,8 +16,6 @@ export class DrawEllipse implements Command
{
async exec()
{
app.m_Editor.Prompt("指定椭圆中心:");
app.m_Editor.UpdateScreen();
let ptRes = await app.m_Editor.GetPoint({
Msg: "指定椭圆中心",
});
@ -24,28 +24,35 @@ export class DrawEllipse implements Command
return;
//椭圆X轴半径点
let radX = await app.m_Editor.GetPoint({
let radXRes = await app.m_Editor.GetPoint({
Msg: "指定轴端点:",
BasePoint: ptRes.Value,
AllowDrawRubberBand: true,
});
let disVec = radX.Value.sub(ptRes.Value);
let ellipseDist = radX.Value.distanceTo(ptRes.Value);
if (radXRes.Status != PromptStatus.OK)
return;
let disVec = radXRes.Value.sub(ptRes.Value);
let ucs = app.m_Editor.UCSMatrix;
disVec.applyMatrix4(new Matrix4().getInverse(ucs));
//实例化椭圆
let ellipse = new Ellipse(ptRes.Value, ellipseDist, 0, Math.atan2(disVec.y, disVec.x));
let ellipse = new Ellipse(new Vector3(), disVec.length(), 0, Math.atan2(disVec.y, disVec.x));
ellipse.ApplyMatrix(ucs);
ellipse.Center = ptRes.Value;
Jig.Draw(ellipse);
app.m_Database.ModelSpace.Append(ellipse);
app.m_Editor.AddNoSnapEntity(ellipse);
//椭圆Y轴
let radY = await app.m_Editor.GetDistance({
let radYRes = await app.m_Editor.GetDistance({
Msg: "指定轴端点:",
BasePoint: ptRes.Value,
Callback: (v) => ellipse.RadY = v,
});
if (radX.Status === PromptStatus.OK)
ellipse.RadY = radY.Value;
app.m_Database.hm.EndCmd();
if (radYRes.Status === PromptStatus.OK)
{
ellipse.RadY = radYRes.Value;
app.m_Database.ModelSpace.Append(ellipse);
}
Jig.Destroy();
}
}

@ -1,7 +1,7 @@
import * as THREE from 'three';
import { Geometry, Object3D, Shape, Vector3 } from 'three';
import { ColorMaterial } from '../Common/ColorPalette';
import { rotatePoint } from '../Geometry/GeUtils';
import { MoveMatrix, rotatePoint } from '../Geometry/GeUtils';
import { RenderType } from '../GraphicsSystem/Enum';
import { Factory } from './CADFactory';
import { CADFile } from './CADFile';
@ -10,23 +10,26 @@ import { Curve } from './Curve';
@Factory
export class Ellipse extends Curve
{
private m_Center: Vector3;
private m_RadX: number;
private m_RadY: number;
private m_Angle: number;
constructor(center?: Vector3, radX?: number, radY?: number, angle?: number)
{
super()
this.m_Center = center || new Vector3();
this.m_RadX = radX || 0;
this.m_RadY = radY || 0;
this.m_Angle = angle || 0;
constructor(
center?: Vector3,
radX: number = 1e-3,
radY: number = 1e-3,
angle: number = 0)
{
super();
center && this.m_Matrix.setPosition(center);
this.m_RadX = radX;
this.m_RadY = radY;
this.m_Angle = angle;
}
get Shape(): Shape
{
let sp = new Shape();
sp.ellipse(this.m_Center.x, this.m_Center.y, this.m_RadX, this.m_RadY, 0, 2 * Math.PI, false, this.m_Angle);
sp.ellipse(0, 0, this.m_RadX, this.m_RadY, 0, 2 * Math.PI, false, this.m_Angle);
return sp;
}
get IsClose(): boolean
@ -35,12 +38,12 @@ export class Ellipse extends Curve
}
get Center()
{
return this.m_Center;
return new Vector3().setFromMatrixPosition(this.m_Matrix);
}
set Center(v: Vector3)
{
this.WriteAllObjectRecord();
this.m_Center = v;
this.m_Matrix.setPosition(v);
this.Update();
}
get RadX()
@ -76,9 +79,7 @@ export class Ellipse extends Curve
PtInCurve(pt: Vector3)
{
let p = rotatePoint(pt.clone().sub(this.Center), -this.Angle);
let a = this.RadX;
let b = this.RadY;
return Math.pow(p.x, 2) / Math.pow(a, 2) + Math.pow(p.y, 2) / Math.pow(b, 2) < 1
return p.x ** 2 / this.RadX ** 2 + p.y ** 2 / this.RadY ** 2 < 1;
}
InitDrawObject(renderType: RenderType = RenderType.Wireframe): Object3D
@ -90,7 +91,7 @@ export class Ellipse extends Curve
}
UpdateDrawObject(type: RenderType, en: Object3D)
{
let geo = en["geometry"] as Geometry;
let geo = (en as THREE.Line).geometry as Geometry;
this.UpdateGeometry(geo);
}
//更新Geometry
@ -102,41 +103,45 @@ export class Ellipse extends Curve
}
GetStretchPoints(): Array<Vector3>
{
let tmpMat4 = new THREE.Matrix4();
tmpMat4.makeRotationZ(this.Angle);
return [
this.m_Center.clone().add(new Vector3(this.m_RadX, 0).applyMatrix4(tmpMat4)),
this.m_Center.clone().add(new Vector3(-this.m_RadX, 0).applyMatrix4(tmpMat4)),
this.m_Center.clone().add(new Vector3(0, -this.m_RadY).applyMatrix4(tmpMat4)),
this.m_Center.clone().add(new Vector3(0, this.m_RadY).applyMatrix4(tmpMat4)),
this.m_Center.clone()
return this.GetSnapPoints();
}
GetSnapPoints(): Array<Vector3>
{
let tmpMat4 = new THREE.Matrix4().makeRotationZ(this.Angle);
let pts = [
new Vector3(this.m_RadX, 0),
new Vector3(-this.m_RadX, 0),
new Vector3(0, -this.m_RadY),
new Vector3(0, this.m_RadY),
new Vector3(),
]
return pts.map(p => p.applyMatrix4(tmpMat4).applyMatrix4(this.OCS));
}
MoveStretchPoints(indexList: Array<number>, vec: Vector3)
{
this.ApplyMatrix(MoveMatrix(vec));
}
MoveSnapPoints(indexList: Array<number>, vec: Vector3)
{
let pts = this.GetStretchPoints();
if (indexList.length > 0)
{
let p = pts[indexList[0]];
if (p) p.add(vec);
if (indexList[0] === 1 || indexList[0] === 0)
{
this.RadX = p.distanceTo(this.m_Center);
} else if (indexList[0] === 2 || indexList[0] === 3)
{
this.RadY = p.distanceTo(this.m_Center);
} else
{
this.Center = p;
}
p.add(vec);
if (indexList[0] === 0 || indexList[0] === 1)
this.RadX = p.distanceTo(this.Center);
else if (indexList[0] === 2 || indexList[0] === 3)
this.RadY = p.distanceTo(this.Center);
else
this.Center = p;
}
}
ReadFile(file: CADFile)
{
super.ReadFile(file);
let ver = file.Read();
this.Center.fromArray(file.Read());
this.RadX = file.Read();
this.RadY = file.Read();
this.Angle = file.Read();
@ -146,7 +151,6 @@ export class Ellipse extends Curve
{
super.WriteFile(file);
file.Write(1);
file.Write(this.Center.toArray());
file.Write(this.RadX);
file.Write(this.RadY);
file.Write(this.Angle);

Loading…
Cancel
Save