You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
webcad-api/api.cjs.js

24047 lines
883 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var three = require('three');
var xaop = require('xaop');
var Line2 = require('three/examples/jsm/lines/Line2');
var LineGeometry = require('three/examples/jsm/lines/LineGeometry');
var LineMaterial = require('three/examples/jsm/lines/LineMaterial');
var Flatbush = require('flatbush');
var polylabel = require('polylabel');
var clipperLib = require('js-angusj-clipper/web');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var Flatbush__default = /*#__PURE__*/_interopDefaultLegacy(Flatbush);
var polylabel__default = /*#__PURE__*/_interopDefaultLegacy(polylabel);
var clipperLib__namespace = /*#__PURE__*/_interopNamespace(clipperLib);
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
/**
* 坐标系运算.
*/
class CoordinateSystem {
constructor(postion, xAxis, yAxis, zAxis) {
this.Postion = postion || new three.Vector3(0, 0, 0);
this.XAxis = xAxis || new three.Vector3(1, 0, 0);
this.YAxis = yAxis || new three.Vector3(0, 1, 0);
this.ZAxis = zAxis || new three.Vector3(0, 0, 1);
}
applyMatrix4(mat4) {
this.Postion.applyMatrix4(mat4);
let roMat = mat4.clone().setPosition(new three.Vector3());
this.XAxis.applyMatrix4(roMat);
this.YAxis.applyMatrix4(roMat);
this.ZAxis.applyMatrix4(roMat);
return this;
}
getMatrix4(m = new three.Matrix4) {
m.makeBasis(this.XAxis, this.YAxis, this.ZAxis);
m.setPosition(this.Postion);
return m;
}
CopyForm(mat4) {
this.Postion.setFromMatrixPosition(mat4);
mat4.extractBasis(this.XAxis, this.YAxis, this.ZAxis);
return this;
}
extractBasis(xAxisA, yAxisA, zAxisA) {
xAxisA.copy(this.XAxis);
yAxisA.copy(this.YAxis);
zAxisA.copy(this.ZAxis);
}
copy(cs) {
this.Postion.copy(cs.Postion);
this.XAxis.copy(cs.XAxis);
this.YAxis.copy(cs.YAxis);
this.ZAxis.copy(cs.ZAxis);
return this;
}
clone() {
let r = new CoordinateSystem();
r.Postion = this.Postion.clone();
r.XAxis = this.XAxis.clone();
r.YAxis = this.YAxis.clone();
r.ZAxis = this.ZAxis.clone();
return r;
}
}
var AAType;
(function (AAType) {
AAType[AAType["FXAA"] = 0] = "FXAA";
AAType[AAType["SMAA"] = 1] = "SMAA";
})(AAType || (AAType = {}));
var ViewDirType;
(function (ViewDirType) {
ViewDirType[ViewDirType["FS"] = 0] = "FS";
ViewDirType[ViewDirType["YAS"] = 1] = "YAS";
ViewDirType[ViewDirType["ZS"] = 2] = "ZS";
ViewDirType[ViewDirType["YS"] = 3] = "YS";
ViewDirType[ViewDirType["QS"] = 4] = "QS";
ViewDirType[ViewDirType["HS"] = 5] = "HS";
ViewDirType[ViewDirType["XN"] = 6] = "XN";
})(ViewDirType || (ViewDirType = {}));
var FractionDigitsType;
(function (FractionDigitsType) {
FractionDigitsType[FractionDigitsType["zero"] = 0] = "zero";
FractionDigitsType[FractionDigitsType["one"] = 1] = "one";
FractionDigitsType[FractionDigitsType["two"] = 2] = "two";
FractionDigitsType[FractionDigitsType["three"] = 3] = "three";
FractionDigitsType[FractionDigitsType["four"] = 4] = "four";
FractionDigitsType[FractionDigitsType["five"] = 5] = "five";
FractionDigitsType[FractionDigitsType["six"] = 6] = "six";
})(FractionDigitsType || (FractionDigitsType = {}));
/**
* 场景的渲染类型.
*/
var RenderType;
(function (RenderType) {
/**
* 线框模式
*/
RenderType[RenderType["Wireframe"] = 1] = "Wireframe";
/**
* 概念
*/
RenderType[RenderType["Conceptual"] = 2] = "Conceptual";
/**
* 物理着色PBR
*/
RenderType[RenderType["Physical"] = 3] = "Physical";
RenderType[RenderType["Jig"] = 4] = "Jig";
RenderType[RenderType["Print"] = 5] = "Print";
/**物理带线框 */
RenderType[RenderType["Physical2"] = 6] = "Physical2";
RenderType[RenderType["Edge"] = 7] = "Edge";
RenderType[RenderType["PlaceFace"] = 8] = "PlaceFace";
RenderType[RenderType["BigHoleFace"] = 81] = "BigHoleFace";
RenderType[RenderType["CustomNumber"] = 9] = "CustomNumber";
RenderType[RenderType["ModelGroove"] = 10] = "ModelGroove";
/******************************************** 在视口时的渲染模式 */
/**
* 线框模式
*/
RenderType[RenderType["WireframePrint"] = 101] = "WireframePrint";
/**
* 概念
*/
RenderType[RenderType["ConceptualPrint"] = 102] = "ConceptualPrint";
/**
* 物理着色PBR
*/
RenderType[RenderType["PhysicalPrint"] = 103] = "PhysicalPrint";
RenderType[RenderType["JigPrint"] = 104] = "JigPrint";
RenderType[RenderType["PrintPrint"] = 105] = "PrintPrint";
/**真实带线框 */
RenderType[RenderType["Physical2Print"] = 106] = "Physical2Print";
RenderType[RenderType["CustomNumberPrint"] = 109] = "CustomNumberPrint";
RenderType[RenderType["ConceptualTransparent"] = 182] = "ConceptualTransparent"; //概念透明
})(RenderType || (RenderType = {}));
class IHostApplicationServices {
get ProxyObject() { return this.__ProxyObject__; }
set ProxyObject(obj) {
this.__ProxyObject__ = obj;
for (let key of IHostApplicationServices.__ProxyKeys__) {
let v = this.__ProxyObject__[key];
if (v === undefined)
throw "程序内部错误:未能代理变量!";
}
}
constructor() {
this.IsRoomEntityVisible = true; //室内实体可见
this.isShowLightShadow = true; //灯光阴影 (除太阳光外)
this.ShowHistoryLog = true;
this.Physical2EdgeColor = 7; //真实视图带线框的线框颜色 默认白色
this.ConceptualEdgeColor = 7; //概念线框的颜色
this.ConceptualOpacity = 1; //概念透明的透明度
this.DrawWallBottomFace = false; //绘制底面
this.sealReserve = 0; //封边统计留头量
//#region _RenderType 渲染类型
this._renderType = RenderType.Wireframe;
//#endregion
//#region 排钻数据
this.DrillConfigs = new Map;
//#endregion
//#region 开启排钻反应器
this.openDrillingReactor = true;
//#endregion
//#region 封边数据
this.sealingColorMap = [];
//#endregion
//#region 显示纹路
this.showLines = false;
//#endregion
//#region 显示开门方向纹路
this.showOpenDirLines = false;
//#endregion
//#region 显示二维刀路路径线条
this.show2DPathLine = false;
//#endregion
//#region 显示二维刀路
this.show2DPathObject = false;
//#endregion
//#region 偏心轮过滤
this.forceFilterPxl = false;
//#endregion
this.chaidanOption = {
changXiuBian: 6,
duanXiuBian: 6,
useDefaultRad: false,
radius: 2.5,
modeling2HoleRad: 20, //圆造型小于等于该值拆成孔数据
isCheckInterfere: false,
noModeingData: "", //非造型遭数据
statTk: false, //统计通孔排钻
statSt: false, //统计双头排钻
drillRemark: false, //拆单获取排钻备注
checkSealType: "1",
sealMaxValue: 10, //封边最大值
sealValues: '', //封边值列表
hardwareExpressionFormattingAccuracy: 2, //复合实体表达式值格式化精度
partialSplitValueCanTakesEffect: false,
};
this.viewSize = {
minViewHeight: 1e-3,
maxViewHeight: 3e6,
zoomSpeed: 0.6
};
this.cursorSize = {
D2: 1000,
D3: 100,
SquareSize: 10,
};
this.dimTextHeight = 60;
this.boardCustomNumberTextHeight = 60; //板编号字体高度
this.lineWidth = 2; //打印线框
this.fractionDigitsType = FractionDigitsType.two;
}
;
static GetInstance() {
if (this._SingleInstance)
return this._SingleInstance;
this._SingleInstance = new IHostApplicationServices;
return this._SingleInstance;
}
//加载贴图,将在index.ts中设置实现的函数
async LoadDefaultExr() { return; }
async LoadMetalEnv() { return; }
}
IHostApplicationServices.__ProxyKeys__ = []; //代理对象,当代理对象存在时,获取内部的值指向代理对象
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "isShowLightShadow", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "Physical2EdgeColor", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "ConceptualEdgeColor", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "ConceptualOpacity", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "_renderType", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "DrillConfigs", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "openDrillingReactor", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "sealingColorMap", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "showLines", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "showOpenDirLines", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "show2DPathLine", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "show2DPathObject", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "uese", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "forceFilterPxl", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "chaidanOption", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "viewSize", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "cursorSize", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "dimTextHeight", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "boardCustomNumberTextHeight", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "lineWidth", void 0);
__decorate([
ProxyValue
], IHostApplicationServices.prototype, "fractionDigitsType", void 0);
let HostApplicationServices = IHostApplicationServices.GetInstance();
//将属性字段指向代理对象
function ProxyValue(target, propertyKey, descriptor) {
let privateKey = '__' + propertyKey;
IHostApplicationServices.__ProxyKeys__.push(propertyKey);
Object.defineProperty(target, propertyKey, {
set: function (value) {
if (this.ProxyObject)
this.ProxyObject[propertyKey] = value;
else
this[privateKey] = value;
},
get: function () {
if (this.ProxyObject)
return this.ProxyObject[propertyKey];
return this[privateKey];
},
enumerable: true,
configurable: true
});
}
/**
* 销毁Object对象的Geometry,并不会销毁材质(新版本销毁材质,好像问题不大?)
*/
function DisposeThreeObj(obj) {
for (let o of obj.children) {
let oany = o;
//文字的geometry缓存保留下来
if (oany.geometry && oany.geometry.name !== "Text")
oany.geometry.dispose();
if (oany.material)
if (Array.isArray(oany.material)) {
for (let m of oany.material)
m.dispose();
}
else {
oany.material.dispose();
}
DisposeThreeObj(o);
// 下面这个代码可能导致Object3d无法复用,删除它应该问题不大
// o.parent = null;
// o.dispatchEvent({ type: "removed" });
}
// 下面这个代码可能导致Object3d无法复用,删除它应该问题不大
// obj.children.length = 0;
return obj;
}
function Object3DRemoveAll(obj) {
for (let o of obj.children) {
o.parent = null;
o.dispatchEvent({ type: "removed" });
}
obj.children.length = 0;
return obj;
}
exports.Status = void 0;
(function (Status) {
Status[Status["False"] = 0] = "False";
Status[Status["True"] = 1] = "True";
Status[Status["Canel"] = -1] = "Canel";
Status[Status["ConverToCircle"] = 101] = "ConverToCircle";
Status[Status["DuplicateRecordName"] = 102] = "DuplicateRecordName";
})(exports.Status || (exports.Status = {}));
exports.UpdateDraw = void 0;
(function (UpdateDraw) {
UpdateDraw[UpdateDraw["None"] = 0] = "None";
UpdateDraw[UpdateDraw["Matrix"] = 1] = "Matrix";
UpdateDraw[UpdateDraw["Geometry"] = 2] = "Geometry";
UpdateDraw[UpdateDraw["Material"] = 4] = "Material";
UpdateDraw[UpdateDraw["All"] = 63] = "All";
})(exports.UpdateDraw || (exports.UpdateDraw = {}));
/**
* WblockClne时,遇到重复记录的操作方式
*/
exports.DuplicateRecordCloning = void 0;
(function (DuplicateRecordCloning) {
DuplicateRecordCloning[DuplicateRecordCloning["Ignore"] = 1] = "Ignore";
DuplicateRecordCloning[DuplicateRecordCloning["Replace"] = 2] = "Replace";
DuplicateRecordCloning[DuplicateRecordCloning["Rename"] = 3] = "Rename";
})(exports.DuplicateRecordCloning || (exports.DuplicateRecordCloning = {}));
/**
* 盒子的切割类型
*/
var SplitType;
(function (SplitType) {
SplitType[SplitType["X"] = 0] = "X";
SplitType[SplitType["Y"] = 1] = "Y";
SplitType[SplitType["Z"] = 2] = "Z";
})(SplitType || (SplitType = {}));
/**
* 扩展Box3,添加切割方法,体积等
*/
class Box3Ext extends three.Box3 {
get Volume() {
let size = this.getSize(new three.Vector3());
return size.x * size.y * size.z;
}
//每个轴的大小必须大于最小的size
isSolid(minSize = 1) {
return BoxIsSolid(this, minSize);
}
substract(b, spaceType) {
let interBox = this.clone().intersect(b);
if (interBox.isEmpty() || !interBox.isSolid())
return [this];
let p1 = interBox.min.clone().setComponent(spaceType, this.min.getComponent(spaceType));
let p2 = interBox.max.clone().setComponent(spaceType, interBox.min.getComponent(spaceType));
let p3 = interBox.min.clone().setComponent(spaceType, interBox.max.getComponent(spaceType));
let p4 = interBox.max.clone().setComponent(spaceType, this.max.getComponent(spaceType));
return [
new Box3Ext(p1, p2),
new Box3Ext(p3, p4)
].filter(b => b.isSolid());
}
clampSpace(b2, splitType) {
let interBox = this.clone();
interBox.min.max(b2.min);
interBox.max.min(b2.max);
interBox.min.setComponent(splitType, Math.min(this.max.getComponent(splitType), b2.max.getComponent(splitType)));
interBox.max.setComponent(splitType, Math.max(this.min.getComponent(splitType), b2.min.getComponent(splitType)));
return interBox;
}
intersectsBox(box, fuzz = 1e-8) {
return IntersectsBox(this, box, fuzz);
}
}
function IntersectsBox(box1, box2, fuzz = 1e-6) {
return box2.max.x < box1.min.x - fuzz || box2.min.x > box1.max.x + fuzz ||
box2.max.y < box1.min.y - fuzz || box2.min.y > box1.max.y + fuzz ||
box2.max.z < box1.min.z - fuzz || box2.min.z > box1.max.z + fuzz ? false : true;
}
/**盒子二维面是否相交 */
function IntersectBox2(box1, box2, fuzz = 1e-3) {
return box2.max.x < box1.min.x - fuzz || box2.min.x > box1.max.x + fuzz ||
box2.max.y < box1.min.y - fuzz || box2.min.y > box1.max.y + fuzz ? false : true;
}
let size = new three.Vector3;
function BoxIsSolid(box, minSize = 1) {
box.getSize(size);
return size.x > minSize && size.y > minSize && size.z > minSize;
}
const ISPROXYKEY = "_isProxy";
/**
* 自动对CADObject的属性添加属性记录器,自动调用 `WriteAllObjectRecord`
* 如果属性是数组,那么自动添加`Proxy`.
* 可以使用`ISPROXYKEY`覆盖这个函数的代理行为(使用CADObject.CreateProxyArray快速覆盖)
*
* ! 仅在{数组}或者{值}类型上使用,如果是Object,请使用AutoRecordObject
*
* @param target
* @param property
* @param [descriptor]
*/
function AutoRecord(target, property, descriptor) {
let privateKey = '__' + property;
Object.defineProperty(target, property, {
set: function (value) {
if (value instanceof Array) {
if (!this[privateKey]) {
if (value[ISPROXYKEY])
this[privateKey] = value;
else
this[privateKey] = new Proxy(value, {
set: (target, key, value, receiver) => {
if (Reflect.get(target, key, receiver) !== value)
this.WriteAllObjectRecord();
return Reflect.set(target, key, value, receiver);
},
get: (target, key, receiver) => {
if (key === ISPROXYKEY)
return true;
//实体先被删除后在触发length = xxx
if (key === "splice" || key === "pop" || key === "shift")
this.WriteAllObjectRecord();
return Reflect.get(target, key, receiver);
}
});
}
else {
let arr = this[privateKey];
arr.length = 0;
arr.push(...value);
// 可能的优化,没有启用这个代码
// arr.length = value.length;
// for (let i = 0; i < value.length; i++)
// arr[i] = value[i];
}
}
else {
let oldv = this[privateKey];
if (oldv !== value) {
this.WriteAllObjectRecord();
this[privateKey] = value;
}
}
},
get: function () {
return this[privateKey];
},
enumerable: true,
configurable: true
});
}
function AutoRecordObject(target, property, descriptor) {
let privateKey = '__' + property;
Object.defineProperty(target, property, {
set: function (value) {
if (value instanceof Object) {
if (!this[privateKey]) {
if (value[ISPROXYKEY])
this[privateKey] = value;
else
this[privateKey] = new Proxy(value, {
set: (target, key, value, receiver) => {
if (Reflect.get(target, key, receiver) !== value)
this.WriteAllObjectRecord();
return Reflect.set(target, key, value, receiver);
},
get: (target, key, receiver) => {
if (key === ISPROXYKEY)
return true;
return Reflect.get(target, key, receiver);
}
});
}
else {
let obj = this[privateKey];
for (let key in value) {
if (obj[key] !== value[key]) {
this.WriteAllObjectRecord();
obj[key] = value[key];
}
}
}
}
else {
let oldv = this[privateKey];
if (oldv !== value) {
this.WriteAllObjectRecord();
this[privateKey] = value;
}
}
},
get: function () {
return this[privateKey];
},
enumerable: true,
configurable: true
});
}
//const UE_REX_DEL = /_.*/g;
/**
* CAD对象工厂,通过注册 和暴露的创建方法,动态创建对象
*/
class CADFactory {
constructor() {
this.objectNameMap = new Map();
}
static RegisterObject(C) {
//this.factory.objectNameMap.set(C.name.replace(UE_REX_DEL, ""), C);
this.factory.objectNameMap.set(C.name, C);
}
static RegisterObjectAlias(C, name) {
this.factory.objectNameMap.set(name, C);
}
static CreateObject(name) {
let C = this.factory.objectNameMap.get(name);
if (C)
return new C();
}
}
CADFactory.factory = new CADFactory();
//可以通过添加装饰器 在类前面(@Factory),自动注册工厂的序列化
function Factory(target) {
CADFactory.RegisterObject(target);
}
var RelevancyType;
(function (RelevancyType) {
RelevancyType[RelevancyType["General"] = 0] = "General";
RelevancyType[RelevancyType["Soft"] = 1] = "Soft";
RelevancyType[RelevancyType["Hard"] = 2] = "Hard";
})(RelevancyType || (RelevancyType = {}));
/*
CADObject对象拥有Id属性,用来记录引用关系.
通过id可以得到对应的关联实体,或者记录实体的关联关系.
ObjectId必须使用 Database分配(db里面会存id的列表,以便同时更新id指向实体)
*/
class ObjectId {
constructor(_Index = 0, _Object) {
this._Index = _Index;
this._Object = _Object;
this._RelevancyType = RelevancyType.General;
}
get IsErase() {
return !this._Object || this._Object.IsErase;
}
set Object(obj) {
this._Object = obj;
}
get Object() {
return this._Object;
}
get Index() {
return this._Index;
}
set Index(index) {
this._Index = index;
}
}
/**
* CAD文件数据
*/
class CADFiler {
constructor(_datas = []) {
this._datas = _datas;
this.readIndex = 0;
}
Destroy() {
delete this._datas;
delete this.readIndex;
}
get Data() {
return this._datas;
}
set Data(data) {
this._datas = data;
this.Reset();
}
Clear() {
this._datas.length = 0;
return this.Reset();
}
Reset() {
this.readIndex = 0;
return this;
}
WriteString(str) {
this._datas.push(str);
return this;
}
ReadString() {
return this._datas[this.readIndex++];
}
WriteObject(obj) {
if (!obj) {
this.Write("");
return;
}
this.WriteString(obj.constructor.name);
obj.WriteFile(this);
return this;
}
ReadObject(obj) {
let className = this.ReadString();
if (className) {
if (obj === undefined) {
obj = CADFactory.CreateObject(className);
if (this.database !== undefined && obj instanceof CADObject)
obj.SetDefaultDb(this.database);
}
if (!obj)
console.error("未注册类名:", className);
obj.ReadFile(this);
return obj;
}
}
CloneObjects(objects, clonedObjects = []) {
for (let o of objects)
this.WriteObject(o);
let count = objects.length;
for (let i = 0; i < count; i++) {
let obj = this.ReadObject();
if (obj instanceof Entity)
obj.CloneDrawObject(objects[i]);
clonedObjects.push(obj);
}
return clonedObjects;
}
Write(data) {
if (data instanceof ObjectId)
this._datas.push(data.Index);
else
this._datas.push(data);
return this;
}
WriteBool(v) {
this.Write(v ? 1 : 0);
}
WriteVec3(v3) {
this._datas.push(v3.x, v3.y, v3.z);
}
Write2dArray(arr) {
this._datas.push(arr.length, ...arr.flat());
}
Read() {
return this._datas[this.readIndex++];
}
ReadBool() {
return Boolean(this.Read());
}
ReadArray(count) {
let arr = this._datas.slice(this.readIndex, this.readIndex + count);
this.readIndex += count;
return arr;
}
Read2dArray() {
let count = this.Read();
let arr = this._datas.slice(this.readIndex, this.readIndex + count * 2);
this.readIndex += count * 2;
let arr2d = [];
for (let i = 0; i < arr.length; i = i + 2)
arr2d.push([arr[i], arr[i + 1]]);
return arr2d;
}
//------------------------ID序列化------------------------
/*
Id关联分为三种情况:
1.普通关联:关联对象未被拷贝时,关联到空对象.
2.软关联 :关联对象未被拷贝时,关联到原先的对象.
3.硬关联 :对象被拷贝时,被关联的对象必须也被拷贝.
*/
//-------1.普通关联
WriteObjectId(id) {
if (id) // id?.Object 为什么没有这么写? 这么写会精简图纸,但是不确定会不会引发新的问题? 其他地方有没有可能依赖这个特性实现一些功能? 比如排钻,如果排钻被删除,我们依然知道排钻的顺序?(曾经拥有?) 暂时不优化似乎也没事?
this.Write(id.Index);
else
this.Write(0);
return this;
}
ReadObjectId() {
let index = this.Read();
if (this.database)
return this.database.GetObjectId(index, true);
}
//-------2.软关联
WriteSoftObjectId(id) {
return this.WriteObjectId(id);
}
ReadSoftObjectId() {
return this.ReadObjectId();
}
//-------3.硬关联
WriteHardObjectId(id) {
return this.WriteObjectId(id);
}
ReadHardObjectId() {
return this.ReadObjectId();
}
//序列化
ToString() {
return JSON.stringify(this._datas);
}
FromString(str) {
this._datas = JSON.parse(str);
}
}
/**
* 保存对象创建或者修改时的所有数据记录
*/
let AllObjectData = class AllObjectData {
constructor(obj) {
this.file = new CADFiler();
if (obj)
obj.WriteFile(this.file);
}
//#region -------------------------File-------------------------
//对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
//对象从文件中读取数据,初始化自身
ReadFile(file) {
file.Read();
let data = file.Read();
this.file.Data = data;
return this;
}
//对象将自身数据写入到文件.
WriteFile(file) {
file.Write(1);
file.Write(this.file.Data);
return this;
}
};
AllObjectData = __decorate([
Factory
], AllObjectData);
let EraseEntityData = class EraseEntityData {
ReadFile(file) {
this.isErase = file.ReadBool();
return this;
}
WriteFile(file) {
file.WriteBool(this.isErase);
return this;
}
constructor(isErase = true) {
this.isErase = isErase;
}
};
EraseEntityData = __decorate([
Factory
], EraseEntityData);
class CADObject {
constructor() {
//-------------------------DB End-------------------------
// -------------------------isErase-------------------------
this._isErase = false;
}
set Owner(owner) {
this._Owner = owner;
}
get Owner() {
return this._Owner;
}
Destroy() {
//在效果图同步反应器中需要知道被删除的实体的id,所以不删除这个属性
// this.objectId = undefined;
this._db = undefined;
if (this.objectId)
this.objectId.Object = undefined; //解除绑定(关联bug:绘制关联切割板后删除切割板,在pu时调用了这个,此时obj被删除但是还会被拷贝,导致错误崩溃)
}
//对象被彻底遗弃
GoodBye() {
this.Destroy();
this.Erase(true);
}
/**
* 当实体异步更新绘制实体完成后触发这个函数.
* Application通过注入的方式得知这个事件,刷新视图显示.
*/
AsyncUpdated() {
}
get Db() {
return this._db;
}
//对象在加入数据库时,必须指定一个源数据库,否则无法读取引用id.
SetDefaultDb(db) {
if (!this._db)
this._db = db;
else
console.warn("重复设置默认Database!");
return this;
}
//private 私有的方法,暴露给Db的添加对象,方法使用.
//只用对象加入到db中,我们才初始化ObjectId.
//从db池中分配id给自身使用. 除非你创建对象往db里面加,否则不要调用该方法
SetOwnerDatabase(db) {
if (!this._db) {
this._db = db;
this.objectId = db.AllocateId();
this.objectId.Object = this;
}
else
console.warn("重复设置源Database!");
return this;
}
/**
* WblockClone 的时候,id是db分配的,此刻我们只需要设置它的db
*/
SetDatabase(db) {
this._db = db;
}
get IsErase() {
return this._isErase;
}
Erase(isErase = true) {
if (isErase === this._isErase)
return;
let undoData = this.UndoRecord();
if (undoData)
undoData.CreateEraseHistory(this, isErase);
this._isErase = isErase;
}
get Id() {
return this.objectId;
}
// -------------------------id End-------------------------
// -------------------------File-------------------------
//对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
//对象从文件中读取数据,初始化自身
ReadFile(file) {
let ver = file.Read();
let id = file.ReadObjectId();
if (!this.objectId && id) //避免CopyFrom时错误的修改自身Id
{
this.objectId = id;
id.Object = this;
}
this._isErase = file.ReadBool();
if (ver > 1)
this._Owner = file.ReadObjectId();
}
//对象将自身数据写入到文件.
WriteFile(file) {
file.Write(2);
file.WriteObjectId(this.objectId);
file.WriteBool(this._isErase);
file.WriteObjectId(this._Owner);
}
//局部撤销
ApplyPartialUndo(undoData) {
if (undoData instanceof AllObjectData) {
undoData.file.database = this._db;
undoData.file.Reset();
this.ReadFile(undoData.file);
}
else if (undoData instanceof EraseEntityData) {
this.Erase(undoData.isErase);
}
}
//撤销所保存的位置
UndoRecord() {
if (this._db && this.objectId)
return this._db.hm.UndoData;
}
//写入所有的对象数据 以便还原对象
WriteAllObjectRecord() {
let undoData = this.UndoRecord();
if (undoData) {
undoData.WriteObjectSnapshoot(this);
return true;
}
return false;
}
//复制出一个实体,如果存在关联,则指向原关联实体
Clone() {
let newObject = CADFactory.CreateObject(this.constructor.name);
//备份
let bakId = this.objectId;
this.objectId = undefined;
let file = new CADFiler();
file.database = this._db;
this.WriteFile(file);
file.Reset();
newObject.ReadFile(file);
newObject.objectId = undefined;
newObject._db = undefined;
this.objectId = bakId;
return newObject;
}
DeepClone(ownerObject, cloneObejct, idMaping = undefined, isPrimary = true) {
return this;
}
//从一个实体拷贝数据,实体类型必须相同.
CopyFrom(obj) {
let idBak = this.objectId;
let ownerBak = this._Owner;
this.WriteAllObjectRecord();
let f = new CADFiler();
f.database = this._db; //这样保证了关联的数据(例如材质)
obj.WriteFile(f);
this.ReadFile(f);
this.objectId = idBak;
this._Owner = ownerBak;
}
//-------------------------File End-------------------------
//Utils
/**
* 配合 `@AutoRecord` 使用
* 使用这个方法来覆盖AutoRecord的监听行为.
* 这个行为只能用来监听实体添加和实体修改.
* 实体删除行为暂时无法监听
* @param setCallback 设置新的实体到数组时的回调函数
*/
CreateProxyArray(setCallback) {
return new Proxy([], {
set: (target, key, value, receiver) => {
if (Reflect.get(target, key, receiver) !== value) {
this.WriteAllObjectRecord();
setCallback(value);
}
return Reflect.set(target, key, value, receiver);
},
get: (target, key, receiver) => {
if (key === ISPROXYKEY)
return true;
//实体先被删除后在触发length = xxx
if (key === "splice" || key === "pop" || key === "shift") {
this.WriteAllObjectRecord();
setCallback(undefined);
}
return Reflect.get(target, key, receiver);
}
});
}
}
__decorate([
xaop.iaop
], CADObject.prototype, "AsyncUpdated", null);
/**
* 历史记录,用于撤销和重做的数据.
*/
let HistorycRecord = class HistorycRecord {
//#region -------------------------File-------------------------
//对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
//对象从文件中读取数据,初始化自身
ReadFile(file) {
let ver = file.Read();
this.undoData = file.ReadObject();
this.redoData = file.ReadObject();
if (ver === 1)
file.ReadObject();
}
//对象将自身数据写入到文件.
WriteFile(file) {
file.Write(2);
file.WriteObject(this.undoData);
file.WriteObject(this.redoData);
}
};
HistorycRecord = __decorate([
Factory
], HistorycRecord);
const USE_WORLD_UV = "USE_WORLD_UV";
const U_WORLD_REP = "u_w_rep";
const V_WORLD_REP = "v_w_rep";
const U_WORLD_MOVE = "u_w_move";
const V_WORLD_MOVE = "v_w_move";
const U_WORLD_RO = "v_w_ro";
class SymbolTableRecord extends CADObject {
constructor() {
super(...arguments);
this.name = "";
}
get Name() {
return this.name;
}
set Name(name) {
if (this.name === name)
return;
this.WriteAllObjectRecord();
if (this.Owner) {
let symbolTable = this.Owner.Object;
if (!symbolTable.ChangeRecordName(this, name))
return;
}
this.name = name;
}
Add(obj, isCheckObjectCleanly = true) {
return exports.Status.False;
}
WriteFile(file) {
super.WriteFile(file);
file.Write(1);
file.Write(this.name);
}
ReadFile(file) {
super.ReadFile(file);
file.Read();
this.name = file.Read();
}
}
let MaterialTableRecord = class MaterialTableRecord extends SymbolTableRecord {
};
MaterialTableRecord = __decorate([
Factory
], MaterialTableRecord);
var UVType;
(function (UVType) {
UVType[UVType["LocalUV"] = 0] = "LocalUV";
UVType[UVType["WorldUV"] = 1] = "WorldUV";
})(UVType || (UVType = {}));
//有关于pbr材质的属性解释说明: https://knowledge.autodesk.com/zh-hans/support/3ds-max/learn-explore/caas/CloudHelp/cloudhelp/2021/CHS/3DSMax-Lighting-Shading/files/GUID-18087194-B2A6-43EF-9B80-8FD1736FAE52-htm.html
let PhysicalMaterialRecord = class PhysicalMaterialRecord extends MaterialTableRecord {
get UseWorldUV() { return this.UVType === UVType.WorldUV; }
set UseWorldUV(b) { this.UVType = b ? UVType.WorldUV : UVType.LocalUV; }
constructor() {
super();
this.type = "木纹";
this.ref = ""; //参考材质,当存在这个变量时,使用ue材质
//基础色
this.color = "#ffffff"; //基础色
//#region 基础色附加 默认折叠
this.baseColorLuminance = 0; //基础色亮度 默认0 范围±1
this.baseColorLightColor = new three.Color(0.5, 0.5, 0.5); //基础色_亮部颜色 默认0.5灰色 范围RGB颜色
this.baseColorDarkColor = new three.Color(0, 0, 0); //基础色_暗部颜色 默认 0黑色 范围RGB颜色
this.baseColorSaturability = 1; //基础色饱和度 默认1 范围0-10;
//#endregion
//透明
this.transparent = false; //透明度 0-1
this.opacity = 1; //不透明度.
//#region 透明度附加 默认折叠
this.opacityContrast = 1; //不透明度对比 默认1
this.opacityBorder = 1; //不透明度边界 默认1
this.opacityMaximum = 1; //不透明度最大值 默认1
this.opacityMinimum = 0.3; // 不透明度最小值 默认0.3
//#endregion
//折射
this.refraction = 1; //玻璃折射 默认1
this.matalness = 0; //金属性 0-1
this.bumpScale = 0; //凹凸比例 UE:法线强度 默认0 范围0-20
this.roughness = 0.2; //粗糙度 0-1
this.specular = 1; //高光 范围0-1
this.selfLuminous = 0; //自发光强度 0-200
this.useMap = true;
//#region 法线贴图和粗糙贴图默认折叠
this.useBumpMap = true;
this.useRoughnessMap = true;
//#endregion
this.IsFull = false; //完全平铺板(此时修改板的uv)
this.side = three.FrontSide; //双面
this.UVType = UVType.LocalUV;
//#region 菲涅尔 默认折叠(绒毛?) 反射?(皮革 布料中可用)
this.fresnelPO = 1; //菲涅尔对比度 默认1 范围-1至10
this.fresnelST = 1; //菲涅尔强度 默认1 范围0至20
this.fresnelLuminance = 1; //菲涅尔亮度 默认1 范围0至20
this.fresnelLightColor = new three.Color(1, 1, 1); //菲涅尔亮部颜色 默认白色 范围RGB
this.fresnelDarkColor = new three.Color(1, 1, 1); //菲涅尔暗部颜色 默认白色 范围RGB
//#endregion
this.sharpen = 1; //锐化 默认1 范围0-20 默认折叠
this.UWroldRep = 1;
this.VWroldRep = 1;
this.UWroldRo = 0;
this.UWorldMove = 0;
this.VWorldMove = 0;
this.depthTest = true; //深度测试(默认true)(弃用(不在UI上显示)
this._goodsInfo = {
name: "",
color: "",
material: "",
};
this.material = new three.MeshPhysicalMaterial({});
Object.defineProperty(this.material, "ObjectId", {
get: () => {
return this?.objectId?.Index;
}
});
}
async Update() {
this.material[USE_WORLD_UV] = this.UseWorldUV;
if (this.material[USE_WORLD_UV]) {
this.material[U_WORLD_REP] = this.UWroldRep;
this.material[V_WORLD_REP] = this.VWroldRep;
this.material[U_WORLD_RO] = this.UWroldRo;
this.material[U_WORLD_MOVE] = this.UWorldMove;
this.material[V_WORLD_MOVE] = this.VWorldMove;
}
if (!this.material.color)
this.material.color = new three.Color(this.color);
else
this.material.color.set(this.color);
this.material.transparent = this.transparent;
if (this.type === "玻璃") {
this.material.metalness = 0.2;
this.material.reflectivity = Math.abs(this.refraction);
}
else
this.material.metalness = this.matalness;
this.material.side = this.side;
this.material.opacity = Math.max(0.1, this.opacity);
this.material.depthTest = this.depthTest;
this.material.bumpScale = this.bumpScale;
this.material.roughness = this.roughness;
if (this.material.metalness > 0.9)
HostApplicationServices.LoadMetalEnv().then(env => {
this.material.envMap = env;
this.material.needsUpdate = true;
});
else
HostApplicationServices.LoadDefaultExr().then(exr => {
this.material.envMap = exr;
this.material.needsUpdate = true;
});
this.material.needsUpdate = true;
if (this.useMap && this.map?.Object && !this.map.IsErase) {
let map = this.map.Object;
let texture = map.GetThreeTexture();
await map.WaitUpdate();
this.material.map = texture;
this.material.needsUpdate = true;
}
else
this.material.map = undefined;
if (this.type === "自发光") {
this.material.emissiveIntensity = this.selfLuminous;
this.material.emissive.copy(this.material.color);
this.material.emissiveMap = this.material.map;
}
else {
this.material.emissiveIntensity = 1;
this.material.emissive.setRGB(0, 0, 0);
this.material.emissiveMap = undefined;
}
if (this.useMap && this.useBumpMap && this.bumpMap?.Object && !this.bumpMap.IsErase) {
let map = this.bumpMap.Object;
let texture = map.GetThreeTexture();
await map.WaitUpdate();
this.material.bumpMap = texture;
this.material.needsUpdate = true;
}
else
this.material.bumpMap = undefined;
if (this.useMap && this.roughnessMap?.Object && !this.roughnessMap.IsErase) {
let map = this.roughnessMap.Object;
let texture = map.GetThreeTexture();
await map.WaitUpdate();
this.material.roughnessMap = texture;
this.material.needsUpdate = true;
}
else
this.material.roughnessMap = undefined;
this.material.needsUpdate = true;
this.AsyncUpdated();
}
get Material() {
return this.material;
}
get GoodsInfo() {
return this._goodsInfo;
}
set GoodsInfo(info) {
if (info.color === this._goodsInfo.color
&& info.material === this._goodsInfo.material
&& info.name === this._goodsInfo.name)
return;
this.WriteAllObjectRecord();
Object.assign(this._goodsInfo, info);
}
//#region -------------------------File-------------------------
ReadFile(file) {
super.ReadFile(file);
let ver = file.Read();
if (ver === 1)
this.name = file.Read();
this.color = file.Read();
this.transparent = file.Read();
this.matalness = file.Read();
this.opacity = file.Read();
this.depthTest = file.Read();
this.map = file.ReadObjectId();
this.bumpMap = file.ReadObjectId();
this.bumpScale = file.Read();
this.roughnessMap = file.ReadObjectId();
this.roughness = file.Read();
this.useMap = file.Read();
this.useBumpMap = file.Read();
this.useRoughnessMap = file.Read();
if (ver <= 2)
file.Read();
if (ver > 2) {
this._goodsInfo.name = file.Read();
this._goodsInfo.material = file.Read();
this._goodsInfo.color = file.Read();
}
if (ver > 3)
this.IsFull = file.Read();
if (ver > 4) {
this.baseColorLuminance = file.Read();
this.baseColorLightColor.r = file.Read();
this.baseColorLightColor.g = file.Read();
this.baseColorLightColor.b = file.Read();
this.baseColorDarkColor.r = file.Read();
this.baseColorDarkColor.g = file.Read();
this.baseColorDarkColor.b = file.Read();
this.baseColorSaturability = file.Read();
this.opacityContrast = file.Read();
this.opacityBorder = file.Read();
this.opacityMaximum = file.Read();
this.opacityMinimum = file.Read();
this.specular = file.Read();
this.selfLuminous = file.Read();
this.fresnelPO = file.Read();
this.fresnelST = file.Read();
this.fresnelLuminance = file.Read();
this.fresnelLightColor.r = file.Read();
this.fresnelLightColor.g = file.Read();
this.fresnelLightColor.b = file.Read();
this.fresnelDarkColor.r = file.Read();
this.fresnelDarkColor.g = file.Read();
this.fresnelDarkColor.b = file.Read();
this.sharpen = file.Read();
if (ver > 5)
this.UVType = file.Read();
if (ver > 6)
this.type = file.Read();
if (ver > 7)
this.ref = file.Read();
if (ver > 8) {
this.UWroldRep = file.Read();
this.VWroldRep = file.Read();
this.UWroldRo = file.Read();
this.UWorldMove = file.Read();
this.VWorldMove = file.Read();
}
if (ver > 9) {
this.refraction = file.Read();
this.side = file.Read();
}
}
this.Update();
}
//对象将自身数据写入到文件.
WriteFile(file) {
super.WriteFile(file);
file.Write(10);
file.Write(this.color);
file.Write(this.transparent);
file.Write(this.matalness);
file.Write(this.opacity);
file.Write(this.depthTest);
file.WriteHardObjectId(this.map);
file.WriteHardObjectId(this.bumpMap);
file.Write(this.bumpScale);
file.WriteHardObjectId(this.roughnessMap);
file.Write(this.roughness);
file.Write(this.useMap);
file.Write(this.useBumpMap);
file.Write(this.useRoughnessMap);
file.Write(this._goodsInfo.name);
file.Write(this._goodsInfo.material);
file.Write(this._goodsInfo.color);
file.Write(this.IsFull);
//ver 5
file.Write(this.baseColorLuminance);
file.Write(this.baseColorLightColor.r);
file.Write(this.baseColorLightColor.g);
file.Write(this.baseColorLightColor.b);
file.Write(this.baseColorDarkColor.r);
file.Write(this.baseColorDarkColor.g);
file.Write(this.baseColorDarkColor.b);
file.Write(this.baseColorSaturability);
file.Write(this.opacityContrast);
file.Write(this.opacityBorder);
file.Write(this.opacityMaximum);
file.Write(this.opacityMinimum);
file.Write(this.specular);
file.Write(this.selfLuminous);
file.Write(this.fresnelPO);
file.Write(this.fresnelST);
file.Write(this.fresnelLuminance);
file.Write(this.fresnelLightColor.r);
file.Write(this.fresnelLightColor.g);
file.Write(this.fresnelLightColor.b);
file.Write(this.fresnelDarkColor.r);
file.Write(this.fresnelDarkColor.g);
file.Write(this.fresnelDarkColor.b);
file.Write(this.sharpen);
//ver 6
file.Write(this.UVType);
//ver 7
file.Write(this.type);
//ver8
file.Write(this.ref);
//ver9
file.Write(this.UWroldRep);
file.Write(this.VWroldRep);
file.Write(this.UWroldRo);
file.Write(this.UWorldMove);
file.Write(this.VWorldMove);
//ver10
file.Write(this.refraction);
file.Write(this.side);
}
};
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "type", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "ref", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "color", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "baseColorLuminance", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "baseColorLightColor", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "baseColorDarkColor", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "baseColorSaturability", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "transparent", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "opacity", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "opacityContrast", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "opacityBorder", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "opacityMaximum", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "opacityMinimum", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "refraction", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "matalness", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "bumpScale", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "roughness", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "specular", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "selfLuminous", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "useMap", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "map", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "useBumpMap", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "bumpMap", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "useRoughnessMap", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "roughnessMap", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "IsFull", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "side", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "UVType", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "fresnelPO", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "fresnelST", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "fresnelLuminance", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "fresnelLightColor", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "fresnelDarkColor", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "sharpen", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "UWroldRep", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "VWroldRep", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "UWroldRo", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "UWorldMove", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "VWorldMove", void 0);
__decorate([
AutoRecord
], PhysicalMaterialRecord.prototype, "depthTest", void 0);
PhysicalMaterialRecord = __decorate([
Factory
], PhysicalMaterialRecord);
var Entity_1;
/**
* Entity 是所有图元的基类,绘制的实体都集成该类.
*/
let Entity = Entity_1 = class Entity extends CADObject {
constructor() {
super();
this.IsEmbedEntity = false;
/**
* 该实体的只有一个渲染类型,任何渲染类型都一个样
*/
this.OnlyRenderType = false;
this.HasEdgeRenderType = false; //拥有封边检查绘制模式
this.HasPlaceFaceRenderType = false; //拥有排版面绘制模式
this.HasBigHoleFaceRenderType = false; //拥有大孔面绘制模式
this._CacheDrawObject = new Map();
this._Color = 7;
//自身坐标系
this._Matrix = new three.Matrix4();
//模块空间的标系
this._SpaceOCS = new three.Matrix4();
this._Visible = true;
this._VisibleInRender = true; //在渲染器中显示
this._Freeze = false; //冻结(无法被选中)
//加工组
this.ProcessingGroupList = [];
/**
* 当AutoUpdate为false时,记录需要更新的标志.
* 以便延迟更新时找到相应的更新标志.
*/
this.NeedUpdateFlag = exports.UpdateDraw.None;
this.AutoUpdate = true;
this._drawObject = undefined;
//实体绘制更新版本号
this.__UpdateVersion__ = 0;
if (this._drawObject)
throw "未知错误 实体内部已经有对象";
this._drawObject = undefined;
}
get CacheDrawObject() { return this._CacheDrawObject; }
get SpaceCSNoClone() { return this._SpaceOCS; }
get SpaceOCS() {
return this._SpaceOCS.clone();
}
set SpaceOCS(m) {
this.WriteAllObjectRecord();
this._SpaceOCS.copy(m);
}
get SpaceOCSInv() {
return new three.Matrix4().getInverse(this._SpaceOCS);
}
get Freeze() { return this._Freeze; }
set Freeze(f) {
if (f === this._Freeze)
return;
this.WriteAllObjectRecord();
this._Freeze = f;
this.Update(exports.UpdateDraw.Material);
}
get VisibleInRender() { return this._VisibleInRender; }
set VisibleInRender(v) {
if (this._VisibleInRender !== v) {
this.WriteAllObjectRecord();
this._VisibleInRender = v;
}
}
set Material(materialId) {
if (materialId === this._MaterialId)
return;
if (this._db && materialId?.Object) //我们放宽校验,当图形未加入到图纸时,我们允许它任意设置材质
{
if (!(materialId.Object instanceof PhysicalMaterialRecord))
throw "程序内部错误!设置材质错误:该对象不是材质";
if (materialId.Object.Db !== this.Db)
throw "程序内部错误!设置材质错误:不同图纸间材质";
}
this.WriteAllObjectRecord();
this._MaterialId = materialId;
for (let [type, obj] of this._CacheDrawObject)
this.UpdateDrawObjectMaterial(type, obj);
}
get Material() { return this._MaterialId; }
GetMaterialSlots() { }
SetMaterialAtSlot(mtl, slotIndex) {
this.Material = mtl;
}
set ColorIndex(color) {
if (color !== this._Color) {
let undoRec = this.UndoRecord();
if (undoRec) {
let hisRec = new HistorycRecord();
hisRec.redoData = new EntityColorHistoryRecord(color);
hisRec.undoData = new EntityColorHistoryRecord(this._Color);
undoRec.WriteObjectHistoryPath(this, hisRec);
}
this._Color = color;
this.Update(exports.UpdateDraw.Material);
}
}
get ColorIndex() {
return this._Color;
}
/**
* 炸开实体
*/
Explode() { return []; }
/**
* 返回对象的包围框.
*/
get BoundingBox() {
for (let [, obj] of this._CacheDrawObject)
return GetBox(obj);
return GetBox(this.GetDrawObjectFromRenderType());
}
/**
* 返回对象在自身坐标系下的Box
*/
get BoundingBoxInOCS() {
let mtxBak = this._Matrix;
this._Matrix = IdentityMtx4;
let isClearDraw = this._CacheDrawObject.size === 0;
let box = this.BoundingBox;
this._Matrix = mtxBak;
if (isClearDraw) {
for (let [, obj] of this._CacheDrawObject)
obj.matrix = this._Matrix; //因为使用了备份的矩阵,导致此时这个矩形是错误的,这里还原它
this.Update(exports.UpdateDraw.Matrix); //保证盒子是正确的
}
return new Box3Ext().copy(box);
}
GetBoundingBoxInMtx(mtx) {
return this.BoundingBoxInOCS.applyMatrix4(this.OCS.premultiply(mtx));
}
get BoundingBoxInSpaceCS() {
return this.GetBoundingBoxInMtx(this.SpaceOCSInv);
}
get OCS() {
return this._Matrix.clone();
}
get OCSNoClone() {
return this._Matrix;
}
//直接设置实体的矩阵,谨慎使用该函数,没有更新实体.
set OCS(mat4) {
this._Matrix.copy(mat4);
}
get Normal() {
return new three.Vector3().setFromMatrixColumn(this._Matrix, 2);
}
get Position() {
return new three.Vector3().setFromMatrixPosition(this._Matrix);
}
Move(v) {
if (equaln$1(v.x, 0) && equaln$1(v.y, 0) && equaln$1(v.z, 0))
return;
tempMatrix1.identity().setPosition(v.x, v.y, v.z);
this.ApplyMatrix(tempMatrix1);
return this;
}
set Position(pt) {
let moveX = pt.x - this._Matrix.elements[12];
let moveY = pt.y - this._Matrix.elements[13];
let moveZ = pt.z - this._Matrix.elements[14];
this.Move({ x: moveX, y: moveY, z: moveZ });
}
get Z() { return this._Matrix.elements[14]; }
set Z(z) {
if (equaln$1(this.Z, z))
return;
this.Move({ x: 0, y: 0, z: z - this.Z });
}
//Z轴归0
Z0() {
if (this._Matrix.elements[14] === 0)
return this;
this.WriteAllObjectRecord();
this.Move({ x: 0, y: 0, z: -this.Z });
return this;
}
//坐标系二维化
MatrixPlanarizere() {
let z = this._Matrix.elements[10];
if (equaln$1(Math.abs(z), 1, 1e-4)) {
this.WriteAllObjectRecord();
MatrixPlanarizere(this._Matrix, false);
}
return this;
}
get OCSInv() {
return new three.Matrix4().getInverse(this._Matrix);
}
/**
* 与指定实体是否共面.
*/
IsCoplaneTo(e) {
return matrixIsCoplane(this._Matrix, e.OCSNoClone, 1e-4);
}
/**
* 测试两个实体的包围盒是否相交.
*/
BoundingBoxIntersectWith(en) {
let box = this.BoundingBox;
let box2 = en.BoundingBox;
return box && box2 && box.intersectsBox(box2);
}
//#region Draw
ClearDraw() {
if (this._drawObject) {
DisposeThreeObj(this._drawObject);
this._drawObject = undefined;
}
for (let [, obj] of this._CacheDrawObject)
DisposeThreeObj(obj);
this._CacheDrawObject.clear();
return this;
}
ClearDrawOfJig() {
let jig = this._CacheDrawObject.get(RenderType.Jig);
if (jig)
this._CacheDrawObject.delete(RenderType.Jig);
for (let [type, obj] of this._CacheDrawObject)
DisposeThreeObj(obj);
this._CacheDrawObject.clear();
if (jig)
this._CacheDrawObject.set(RenderType.Jig, jig);
}
get IsOnlyRender() {
return this.OnlyRenderType;
}
get CaseShadow() {
if (!this.MeshMaterial)
return true;
if (Array.isArray(this.MeshMaterial))
return true;
return !this.MeshMaterial.transparent || this.MeshMaterial.opacity === 1;
}
get ReceiveShadow() {
return this.CaseShadow;
}
get DrawObject() {
if (this.constructor.name === "Entity" || this.constructor.name === "CADObject")
throw "出错了";
if (this._drawObject)
return this._drawObject;
this._drawObject = new three.Object3D();
if (!this.IsEmbedEntity)
this._drawObject.userData.Entity = this;
if (this.IsVisible) {
this._CurRenderType = HostApplicationServices._renderType;
let obj = this.GetDrawObjectFromRenderType(HostApplicationServices._renderType);
if (obj)
this._drawObject.add(obj);
}
else
this._drawObject.visible = false;
return this._drawObject;
}
get JigObject() {
let obj = this.GetDrawObjectFromRenderType(RenderType.Jig);
if (obj && !this.IsEmbedEntity)
obj.userData.Entity = this;
return obj;
}
DestroyJigObject() {
let obj = this._CacheDrawObject.get(RenderType.Jig);
if (obj) {
this._CacheDrawObject.delete(RenderType.Jig);
DisposeThreeObj(obj);
if (obj.parent)
obj.parent.remove(obj);
}
}
UpdateRenderType(type) {
if (this._CurRenderType !== type || this.DrawObject.children.length === 0) {
this._CurRenderType = type;
if ((this.OnlyRenderType && this.DrawObject.children.length > 0) || !this._Visible)
return;
Object3DRemoveAll(this.DrawObject);
let obj = this.GetDrawObjectFromRenderType(type);
if (obj)
this.DrawObject.add(obj);
}
}
GetDrawObjectFromRenderType(renderType = RenderType.Wireframe) {
if (this.OnlyRenderType) {
if (renderType === RenderType.Jig)
return;
if (renderType < 100)
renderType = RenderType.Wireframe;
else
renderType = RenderType.WireframePrint;
}
if (renderType === RenderType.Edge && !this.HasEdgeRenderType)
renderType = RenderType.Conceptual;
if (renderType === RenderType.PlaceFace && !this.HasPlaceFaceRenderType)
renderType = RenderType.Wireframe;
if (renderType === RenderType.BigHoleFace && !this.HasBigHoleFaceRenderType)
renderType = RenderType.Wireframe;
if (this._CacheDrawObject.has(renderType)) {
return this._CacheDrawObject.get(renderType);
}
else {
let drawObj = this.InitDrawObject(renderType);
if (drawObj === undefined) {
if (renderType > 100) //如果实体没有实现打印类型,那么就使用原先的实体的渲染类型
return this.GetDrawObjectFromRenderType(renderType - 100);
return;
}
//矩阵直接使用指针,因为已经关闭自动更新,所以矩阵不会被Object3D修改.
drawObj.matrixAutoUpdate = false;
drawObj.matrix = this._Matrix;
drawObj.updateMatrixWorld(true);
drawObj.traverse(UpdateBoundingSphere);
if (!this.IsEmbedEntity)
drawObj.userData.Entity = this;
this._CacheDrawObject.set(renderType, drawObj);
return drawObj;
}
}
/**
* 初始化绘制的threejs实体,子类型重载该函数初始化绘制实体.
*/
InitDrawObject(renderType = RenderType.Wireframe) {
return undefined;
}
/**
* 当实体数据改变时,绘制的实体必须做出改变.供框架调用
*/
Update(mode = exports.UpdateDraw.All) {
this.__UpdateVersion__++;
this.NeedUpdateFlag |= mode;
if (this.AutoUpdate) {
this.DeferUpdate();
// if (this.__ReadFileIng__) //!警告
// console.error("在读取文件时更新实体的显示!");
}
}
//三维实体总是一起生成线框实体和网格实体,这个通知更新,然后统一更新就好了
//避免重复更新
UpdateDrawGeometry() { }
/** 立即更新 */
DeferUpdate() {
let mode = this.NeedUpdateFlag;
if (mode === 0)
return;
if (mode & exports.UpdateDraw.Geometry && this._CacheDrawObject.size > 0)
this.UpdateDrawGeometry();
this.UpdateVisible();
let isJigIng = this._CacheDrawObject.has(RenderType.Jig);
for (let [type, obj] of this._CacheDrawObject) {
if (isJigIng && type !== RenderType.Jig)
continue;
if (mode & exports.UpdateDraw.Geometry) {
if (obj.userData.IsClone) {
let parent = obj.parent;
DisposeThreeObj(obj);
this._CacheDrawObject.delete(type);
let newObj = this.GetDrawObjectFromRenderType(type);
if (parent) {
parent.remove(obj);
parent.add(newObj);
}
obj = newObj;
}
else
this.UpdateDrawObject(type, obj);
}
if (mode & exports.UpdateDraw.Material)
this.UpdateDrawObjectMaterial(type, obj);
if (mode & exports.UpdateDraw.Matrix || mode & exports.UpdateDraw.Geometry) {
obj.updateMatrixWorld(true);
// if (this.Id)//如果这个是Jig实体,那么我们更新这个盒子球似乎也没有意义 (虽然这在某些情况能改进性能,但是在绘制圆弧的时候,因为没有更新圆弧的盒子,导致绘制出来的圆弧无法被选中)
obj.traverse(UpdateBoundingSphere);
}
}
this.NeedUpdateFlag = exports.UpdateDraw.None;
}
/**
* 当实体需要更新时,需要重载该方法,实现实体更新
*/
UpdateDrawObject(type, en) {
}
/**
* 当实体需要被更新时,更新实体材质
*/
UpdateDrawObjectMaterial(type, obj, material) {
}
get MeshMaterial() {
if (this._MaterialId && this._MaterialId.Object)
return this._MaterialId.Object.Material;
return HostApplicationServices.DefaultMeshMaterial;
}
/**
* 更新实体Jig状态时的材质
*/
UpdateJigMaterial(color = 8) {
}
RestoreJigMaterial() {
for (let [type, en] of this._CacheDrawObject)
this.UpdateDrawObjectMaterial(type, en);
}
get Visible() {
return this._Visible;
}
set Visible(v) {
if (v !== this._Visible) {
this.WriteAllObjectRecord();
this._Visible = v;
this.UpdateVisible();
}
}
get IsVisible() {
return !this._isErase && this._Visible;
}
UpdateVisible() {
if (this._drawObject) {
this._drawObject.visible = this.IsVisible;
if (this.IsVisible)
this.UpdateRenderType(HostApplicationServices._renderType);
}
}
//#endregion
GoodBye() {
super.GoodBye();
if (this._drawObject && this._drawObject.parent)
this._drawObject.parent.remove(this._drawObject);
this.ClearDraw();
}
Erase(isErase = true) {
if (isErase === this._isErase)
return;
this.__UpdateVersion__++;
super.Erase(isErase);
this.UpdateVisible();
this.EraseEvent(isErase);
}
EraseEvent(isErase) {
}
/**
* 使用统一的方法设置对象的矩阵.
* 需要对缩放矩形进行重载.避免对象矩阵不是单位矩阵
*/
ApplyMatrix(m) {
this.WriteAllObjectRecord();
m.extractBasis(Entity_1._xa, Entity_1._ya, Entity_1._za);
if (equaln$1(Entity_1._xa.lengthSq(), 1, 1e-4) &&
equaln$1(Entity_1._ya.lengthSq(), 1, 1e-4) &&
equaln$1(Entity_1._za.lengthSq(), 1, 1e-4)) {
this._Matrix.multiplyMatrices(m, this._Matrix);
this._SpaceOCS.multiplyMatrices(m, this._SpaceOCS);
if (!equalv3(Entity_1._xa.cross(Entity_1._ya).normalize(), Entity_1._za))
this.ApplyMirrorMatrix(m);
}
else
this.ApplyScaleMatrix(m);
this.Update(exports.UpdateDraw.Matrix);
return this;
}
ApplyScaleMatrix(m) {
return this;
}
ApplyMirrorMatrix(m) {
return this;
}
/**
*
* @param snapMode 捕捉模式(单一)
* @param pickPoint const
* @param lastPoint const
* @param viewXform const 最近点捕捉需要这个变量
* @returns object snap points
*/
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform, frustum) {
return [];
}
GetGripPoints() {
return [];
}
MoveGripPoints(indexList, vec) {
}
GetStretchPoints() {
return [];
}
/**
* 拉伸夹点,用于Stretch命令
* @param {Array<number>} indexList 拉伸点索引列表.
* @param {Vector3} vec 移动向量
*/
MoveStretchPoints(indexList, vec) {
}
IntersectWith(curve, intType) { return; }
//#region -------------------------File-------------------------
Clone(cloneDraw = true) {
let ent = super.Clone();
ent._CurRenderType = this._CurRenderType;
ent.Template = undefined;
if (cloneDraw)
ent.CloneDrawObject(this);
return ent;
}
CloneDrawObject(from) {
for (let [type, obj] of from._CacheDrawObject) {
let oldUserDaata = obj.userData;
obj.traverse(o => o.userData = {});
let newObj = obj.clone();
obj.userData = oldUserDaata;
obj.userData.IsClone = true;
newObj.matrix = this._Matrix;
newObj.userData = { Entity: this };
newObj.userData.IsClone = true;
this._CacheDrawObject.set(type, newObj);
}
this.NeedUpdateFlag = exports.UpdateDraw.None;
}
get ReadFileIng() {
return this.__ReadFileIng__ || Entity_1.__ReadFileIng__;
}
/**
* 从文件读取,序列化自身,如果需要,重载_ReadFile
*/
ReadFile(file) {
this.__ReadFileIng__ = true;
this._ReadFile(file);
this.__ReadFileIng__ = false;
this.Update();
}
//对象从文件中读取数据,初始化自身
_ReadFile(file) {
let ver = file.Read();
super.ReadFile(file);
this._Color = file.Read();
this._MaterialId = file.ReadHardObjectId();
if (!this._MaterialId)
this._MaterialId = file.database?.DefaultMaterial?.Id;
this._Matrix.fromArray(file.Read());
if (ver === 2)
this.Owner = file.ReadObjectId();
if (ver > 3)
this.Template = file.ReadObjectId();
if (ver > 4)
this.GroupId = file.ReadHardObjectId();
if (ver > 5)
this._Visible = file.ReadBool();
if (ver > 6)
this._SpaceOCS.fromArray(file.Read());
if (ver > 7) {
let count = file.Read();
this.ProcessingGroupList.length = 0;
for (let i = 0; i < count; i++) {
let id = file.ReadHardObjectId();
if (id)
this.ProcessingGroupList.push(id);
}
}
if (ver > 8)
this._Freeze = file.ReadBool();
else
this._Freeze = false;
if (ver > 9)
this._VisibleInRender = file.ReadBool();
else
this._VisibleInRender = true;
}
//对象将自身数据写入到文件.
WriteFile(file) {
file.Write(10);
super.WriteFile(file);
file.Write(this._Color);
file.WriteHardObjectId(this._MaterialId);
file.Write(this._Matrix.toArray());
file.WriteObjectId(this.Template);
file.WriteHardObjectId(this.GroupId);
file.WriteBool(this._Visible);
file.Write(this._SpaceOCS.toArray());
file.Write(this.ProcessingGroupList.length);
for (let id of this.ProcessingGroupList)
file.WriteHardObjectId(id);
file.WriteBool(this._Freeze);
file.WriteBool(this._VisibleInRender);
}
//局部撤销
ApplyPartialUndo(undoData) {
super.ApplyPartialUndo(undoData);
if (undoData instanceof EntityColorHistoryRecord)
this.ColorIndex = undoData.color;
}
CopyFrom(obj) {
let templateIdBak = this.Template;
super.CopyFrom(obj);
this.Update();
this.Template = templateIdBak;
}
};
Entity._xa = new three.Vector3;
Entity._ya = new three.Vector3;
Entity._za = new three.Vector3;
__decorate([
AutoRecord
], Entity.prototype, "GroupId", void 0);
__decorate([
AutoRecord
], Entity.prototype, "Template", void 0);
__decorate([
AutoRecord
], Entity.prototype, "ProcessingGroupList", void 0);
__decorate([
xaop.iaop
], Entity.prototype, "Update", null);
__decorate([
xaop.iaop
], Entity.prototype, "EraseEvent", null);
Entity = Entity_1 = __decorate([
Factory
], Entity);
let EntityColorHistoryRecord = class EntityColorHistoryRecord {
constructor(color) {
this.color = color;
}
ReadFile(file) {
this.color = file.Read();
return this;
}
WriteFile(file) {
file.Write(this.color);
return this;
}
};
EntityColorHistoryRecord = __decorate([
Factory
], EntityColorHistoryRecord);
var StoreageKeys;
(function (StoreageKeys) {
StoreageKeys["IsLogin"] = "isLogin";
StoreageKeys["PlatSession"] = "platSession";
StoreageKeys["PlatToken"] = "platToken";
StoreageKeys["UserName"] = "userName";
StoreageKeys["UserPhone"] = "userPhone";
StoreageKeys["ShopName"] = "shopName";
StoreageKeys["ShopProps"] = "ShopProps";
StoreageKeys["AgentShopName"] = "AgentShopName";
StoreageKeys["AgentPhone"] = "AgentPhone";
StoreageKeys["VersionName"] = "VersionName";
StoreageKeys["RegisterTime"] = "RegisterTime";
StoreageKeys["RenderType"] = "renderType";
StoreageKeys["ExactDrill"] = "openExactDrill";
StoreageKeys["ConfigName"] = "configName_";
StoreageKeys["IsNewErp"] = "isNewErp";
StoreageKeys["RoomName"] = "roomName";
StoreageKeys["LastOpenFileId"] = "lastfid";
StoreageKeys["Uid"] = "uid";
StoreageKeys["Goods"] = "Goods_";
StoreageKeys["DrillTemp"] = "drilltemp_";
StoreageKeys["DrillReactor"] = "drillRreactor";
StoreageKeys["kjlConfig"] = "kjl";
StoreageKeys["HistoryWs"] = "HistoryWs";
})(StoreageKeys || (StoreageKeys = {}));
let OPERATORS = new Set(["+", "-", "*", "/"]);
/**
* eval2("+10", { L: 100 }, "L")
* @param expr
* @param [params]
* @param [defaultParam] 当输入 +10 这样的表达式时,设置默认的操作变量
* @returns 计算结果
*/
function eval2(expr, params, defaultParam) {
three.MathUtils.radToDeg;
three.MathUtils.degToRad;
three.MathUtils.RAD2DEG;
three.MathUtils.DEG2RAD;
//eval_gen_code
let abs = Math.abs;
let acos = Math.acos;
let acosh = Math.acosh;
let asin = Math.asin;
let asinh = Math.asinh;
let atan = Math.atan;
let atanh = Math.atanh;
let atan2 = Math.atan2;
let ceil = Math.ceil;
let cbrt = Math.cbrt;
let expm1 = Math.expm1;
let clz32 = Math.clz32;
let cos = Math.cos;
let cosh = Math.cosh;
let exp = Math.exp;
let floor = Math.floor;
let fround = Math.fround;
let hypot = Math.hypot;
let imul = Math.imul;
let log = Math.log;
let log1p = Math.log1p;
let log2 = Math.log2;
let log10 = Math.log10;
let max = Math.max;
let min = Math.min;
let pow = Math.pow;
let random = Math.random;
let round = Math.round;
let sign = Math.sign;
let sin = Math.sin;
let sinh = Math.sinh;
let sqrt = Math.sqrt;
let tan = Math.tan;
let tanh = Math.tanh;
let trunc = Math.trunc;
let E = Math.E;
let LN10 = Math.LN10;
let LN2 = Math.LN2;
let LOG10E = Math.LOG10E;
let LOG2E = Math.LOG2E;
let PI = Math.PI;
let SQRT1_2 = Math.SQRT1_2;
let SQRT2 = Math.SQRT2;
let clamp = CLAMP;
let eq = equaln;
let r2d = three.MathUtils.radToDeg;
let d2r = three.MathUtils.degToRad;
let R2D = three.MathUtils.RAD2DEG;
let D2R = three.MathUtils.DEG2RAD;
let fix = FixedNotZero;
//eval_gen_code
let code = "";
if (params)
for (let name in params)
code += `let ${name} = ${params[name]};`;
if (defaultParam) {
expr = expr.trimLeft();
if (expr[0] && OPERATORS.has(expr[0]))
expr = defaultParam + expr;
}
code += expr;
let result = eval(code);
if (typeof result === "function")
return result();
if (typeof result === "bigint")
return Number(result); //防止bigint乱入
return result;
}
function safeEval(expr, params, defaultParam) {
try {
return eval2(expr, params, defaultParam);
}
catch (error) {
return NaN;
}
}
const Reg_Expr = /\{[^\}]+\}/g;
/**解析大括号内的表达式 */
function ParseExpr(expr, fractionDigits = 0, params) {
let strs = expr.match(Reg_Expr);
if (!strs)
return expr;
for (let str of strs) {
let value = safeEval(str.slice(1, -1), params);
let strValue;
if (typeof value === "number")
strValue = FixedNotZero(value, fractionDigits, expr);
else
strValue = value;
expr = expr.replace(str, strValue);
}
return expr;
}
/**扣除封边是否相连和连接共用精度 */
const LINK_FUZZ = 1e-3;
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function FixIndex$1(index, arr) {
let count = (arr instanceof Array) ? arr.length : arr;
if (index < 0)
return count + index;
else if (index >= count)
return index - count;
else
return index;
}
//使用定点表示法来格式化一个数,小数点后面不尾随0. 如 FixedNotZero(1.1 , 3) => 1.1
function FixedNotZero(v, fractionDigits = 0, expr = undefined) {
if (typeof v === "string") {
if (expr?.includes("fix"))
return v; //如果表达式中有fix,则尊重表达式的格式化,不再二次格式化
v = parseFloat(v);
}
if (isNaN(v))
return "";
if (Math.abs(v) < Math.pow(0.1, fractionDigits) * 0.49)
return "0";
if (!fractionDigits)
return v.toFixed(0);
else
return v.toFixed(fractionDigits).replace(/[.]?0+$/, "");
}
const IdentityMtx4 = new three.Matrix4();
const ZeroVec = new three.Vector3();
const XAxis = new three.Vector3(1, 0, 0);
const XAxisN = new three.Vector3(-1, 0, 0);
const YAxis = new three.Vector3(0, 1, 0);
const YAxisN = new three.Vector3(0, -1, 0);
const ZAxis = new three.Vector3(0, 0, 1);
const ZAxisN = new three.Vector3(0, 0, -1);
function AsVector2(p) {
return new three.Vector2(p.x, p.y);
}
function AsVector3(p) {
return new three.Vector3(p.x, p.y, p.z);
}
/**
* 判断一维线段a和b是否存在交集
*/
function isIntersect(amin, amax, bmin, bmax, eps = 0) {
return Math.max(amin, bmin) < Math.min(amax, bmax) + eps;
}
//范围交集 [a1,a2] 与 [b1,b2] 的交集
function isIntersect2(a1, a2, b1, b2, eps = 0) {
if (a1 > a2)
[a1, a2] = [a2, a1];
if (b1 > b2)
[b1, b2] = [b2, b1];
return Math.max(a1, b1) < Math.min(a2, b2) + eps;
}
/**
* 旋转一个点,旋转中心在原点
* @param {Vector3} p 点
* @param {number} a 角度.
* @returns {Vector3} 返回pt不拷贝.
*/
function rotatePoint(p, a) {
let s = Math.sin(a);
let c = Math.cos(a);
let x = p.x * c - p.y * s;
let y = p.x * s + p.y * c;
p.x = x;
p.y = y;
return p;
}
function equaln$1(v1, v2, fuzz = 1e-5) {
return Math.abs(v1 - v2) <= fuzz;
}
function equalv3(v1, v2, fuzz = 1e-8) {
return equaln$1(v1.x, v2.x, fuzz) && equaln$1(v1.y, v2.y, fuzz) && equaln$1(v1.z, v2.z, fuzz);
}
function equalv2(v1, v2, fuzz = 1e-8) {
return equaln$1(v1.x, v2.x, fuzz) && equaln$1(v1.y, v2.y, fuzz);
}
/**
* 按照极坐标的方式移动一个点
* @param {T} v 向量(2d,3d)
* @param {number} an 角度
* @param {number} dis 距离
* @returns {T}
*/
function polar(v, an, dis) {
v.x += Math.cos(an) * dis;
v.y += Math.sin(an) * dis;
return v;
}
function angle(v) {
let angle = Math.atan2(v.y, v.x);
if (equaln$1(angle, 0, 1e-8))
return 0;
if (angle < 0)
angle += Math.PI * 2;
return angle;
}
/**
* 求两个向量的夹角,顺时针为负,逆时针为正
* @param {Vector3} v1
* @param {Vector3} v2
* @param {Vector3} [ref] 参考向量,如果为世界坐标系则为0,0,1
*/
function angleTo(v1, v2, ref = ZAxis) {
if (v1.equals(ZeroVec) || v2.equals(ZeroVec))
return 0;
v1 = v1.clone();
v2 = v2.clone();
if (ref !== ZAxis && !ref.equals(ZAxis)) {
ref = ref.clone();
//任意轴坐标系. 使用相机的构造矩阵.
ref.multiplyScalar(-1);
let up = getLoocAtUpVec(ref);
let refOcs = new three.Matrix4();
refOcs.lookAt(ZeroVec, ref, up);
let refOcsInv = new three.Matrix4().getInverse(refOcs);
v1.applyMatrix4(refOcsInv);
v2.applyMatrix4(refOcsInv);
v1.z = 0;
v2.z = 0;
}
if (v1.equals(ZeroVec) || v2.equals(ZeroVec)) //修复,这里有可能被更改为0
return 0;
//平行的向量返回0向量,不需要归一化
let cv = new three.Vector3().crossVectors(v1, v2);
if (equalv3(cv, ZeroVec))
return 0;
cv.normalize();
return equaln$1(cv.z, 0) ? v1.angleTo(v2) : v1.angleTo(v2) * cv.z;
}
function getLoocAtUpVec(dir) {
if (dir.equals(ZeroVec)) {
throw ("zero vector");
}
let norm = dir.clone().normalize();
if (norm.equals(ZAxis)) {
return new three.Vector3(0, 1, 0);
}
else if (norm.equals(ZAxis.clone().negate())) {
return new three.Vector3(0, -1, 0);
}
else {
let xv = new three.Vector3();
xv.crossVectors(ZAxis, norm);
let up = new three.Vector3();
up.crossVectors(norm, xv);
return up;
}
}
/**
* 判断2个向量是不是平行,尽量传入单位向量,才能保证计算精度
*/
function isParallelTo(v1, v2, fuzz = 1e-8) {
return v1.clone().cross(v2).lengthSq() < fuzz;
}
/**
* 垂直向量
*/
function isPerpendicularityTo(v1, v2, fuzz = 1e-8) {
return equaln$1(v1.dot(v2), 0, fuzz);
}
let tempBox = new three.Box3();
/**
* 获得Three对象的包围盒.
* @param obj
* @param [updateMatrix] 是否应该更新对象矩阵
* @returns box
*/
function GetBox(obj, updateMatrix = false) {
let box = new three.Box3();
if (updateMatrix)
obj.updateMatrixWorld(false);
if (!obj.visible)
return box;
obj.traverseVisible(o => {
//@ts-ignore
let geo = o.geometry;
if (geo) {
if (!geo.boundingBox)
geo.computeBoundingBox();
tempBox.copy(geo.boundingBox).applyMatrix4(o.matrixWorld);
box.union(tempBox);
}
});
return box;
}
function MoveMatrix(v) {
return new three.Matrix4().setPosition(v);
}
/**
* 将角度调整为0-2pi之间
*/
function clampRad(an) {
an = an % (Math.PI * 2);
if (an < 0)
an += Math.PI * 2;
return an;
}
function updateGeometry(l, geometry) {
let geo = l.geometry;
geo.dispose();
l.geometry = geometry;
geometry.computeBoundingSphere();
}
function UpdateBoundingSphere(obj) {
//@ts-ignore
let geo = obj.geometry;
if (geo) {
geo.computeBoundingSphere();
geo.computeBoundingBox(); //样条线更新位置和更新点位置后,无法被选中
}
}
const comparePointCache = new Map();
/**
* 构建返回一个用来排序的函数.根据key创建排序规则.
*
* 当key = "xyz" 时,点集按 x从小到大,y从小到大 z从小到大
* key = "X" 时,点集按 x从大到小
* 以此类推.
*
* 例子:
* let pts:Vector3[] =...;
* pts.sort(comparePoint("x")); //x从小到大排序
* pts.sort(comparePoint("zX")); //z从小到大 x从大到小
*
* @export
* @param {string} sortKey
* @returns {compareVectorFn}
*/
function ComparePointFnGenerate(sortKey, fuzz = 1e-8) {
let cacheKey = `${sortKey}${fuzz}`;
if (comparePointCache.has(cacheKey))
return comparePointCache.get(cacheKey);
let sortIndex = [];
const keys = ['x', 'X', 'y', 'Y', 'z', 'Z'];
for (let char of sortKey) {
let index = keys.indexOf(char);
let i2 = index / 2;
let ci = Math.floor(i2);
sortIndex.push([ci, i2 > ci ? 1 : -1]);
}
let compareFunction = (v1, v2) => {
if (!v1)
return -1;
if (!v2)
return 1;
for (let s of sortIndex) {
let vv1 = v1.getComponent(s[0]);
let vv2 = v2.getComponent(s[0]);
if (equaln$1(vv1, vv2, fuzz))
continue;
if (vv2 > vv1)
return s[1];
else
return -s[1];
}
return 0;
};
comparePointCache.set(sortKey, compareFunction);
return compareFunction;
}
function SelectNearP(pts, refPt) {
if (pts.length > 1) {
let dist1 = refPt.distanceToSquared(pts[0]);
let dist2 = refPt.distanceToSquared(pts[1]);
return dist1 <= dist2 ? pts[0] : pts[1];
}
return pts[0];
}
/**n是否在AB之间,fuzz 若为负的,允许相等 */
function IsBetweenA2B(n, A, B, fuzz = -1e-8) {
return n > A + fuzz && n < B - fuzz;
}
/** 矩阵是世界坐标系 */
function MatrixIsIdentityCS(mtx) {
return mtx.elements[0] === 1 && mtx.elements[5] === 1 && mtx.elements[10] === 1 && mtx.elements[12] === 0 && mtx.elements[13] === 0 && mtx.elements[14] === 0;
}
/**
* 设置矩阵的某列的向量
* @param {Matrix4} mtx 矩阵
* @param {number} col 列索引,0x 1y 2z 3org
* @param {Vector3} v 向量或点
*/
function SetMtxVector(mtx, col, v) {
let index = col * 4;
mtx.elements[index] = v.x;
mtx.elements[index + 1] = v.y;
mtx.elements[index + 2] = v.z;
}
/**
* 返回矩阵,该坐标系将坐标系与原点的坐标系映射为坐标系,
* 并将坐标系与X轴坐标系
* Y轴坐标轴以及Z轴坐标系统之间的坐标系统坐标系统的原点坐标系和原点坐标系统坐标轴的坐标系分别设置为XAxisYAxis和ZAxis
* @returns {Matrix4} 返回新的矩阵
*/
function matrixAlignCoordSys(matrixFrom, matrixTo) {
return new three.Matrix4().getInverse(matrixTo).multiply(matrixFrom);
}
/**
* 判断2个矩形共面
* @param {Matrix4} matrixFrom
* @param {Matrix4} matrixTo
* @returns {boolean} 2个矩阵共面
*/
function matrixIsCoplane(matrixFrom, matrixTo, fuzz = 1e-5) {
let nor1 = new three.Vector3().setFromMatrixColumn(matrixFrom, 2);
let nor2 = new three.Vector3().setFromMatrixColumn(matrixTo, 2);
//法线共面
if (!isParallelTo(nor1, nor2))
return false;
//高共面
let pt = new three.Vector3().setFromMatrixPosition(matrixTo);
//变换到自身对象坐标系.
pt.applyMatrix4(new three.Matrix4().getInverse(matrixFrom));
return equaln$1(pt.z, 0, fuzz);
}
/**
* 修正镜像后矩阵
*/
function reviseMirrorMatrix(mtx, index = 1) {
let cs = new CoordinateSystem().applyMatrix4(mtx);
if (index === 0)
cs.XAxis.negate();
else if (index === 1)
cs.YAxis.negate();
else
cs.ZAxis.negate();
return cs.getMatrix4(mtx);
}
let cacheVec;
function Vector2ApplyMatrix4(mtx, vec) {
if (!cacheVec)
cacheVec = new three.Vector3();
cacheVec.x = vec.x;
cacheVec.y = vec.y;
cacheVec.z = 0;
cacheVec.applyMatrix4(mtx);
vec.x = cacheVec.x;
vec.y = cacheVec.y;
}
function MakeMirrorMtx(planeNormal, pos) {
let mirrorMtx = new three.Matrix4();
let xAxis = new three.Vector3(1 - 2 * planeNormal.x ** 2, -2 * planeNormal.x * planeNormal.y, -2 * planeNormal.x * planeNormal.z);
let yAxis = new three.Vector3(-2 * planeNormal.x * planeNormal.y, 1 - 2 * planeNormal.y ** 2, -2 * planeNormal.y * planeNormal.z);
let zAxis = new three.Vector3(-2 * planeNormal.x * planeNormal.z, -2 * planeNormal.y * planeNormal.z, 1 - 2 * planeNormal.z ** 2);
mirrorMtx.makeBasis(xAxis, yAxis, zAxis);
if (pos)
mirrorMtx.setPosition(pos.clone().applyMatrix4(mirrorMtx).sub(pos).negate());
return mirrorMtx;
}
/**
* 对向量进行方向变换 (如果是pos 请使用pos.applyMatrix4)
* @param vec 向量
* @param m 矩阵
* @returns vec
*/
function TransformVector(vec, m) {
let { x, y, z } = vec;
let e = m.elements;
vec.x = e[0] * x + e[4] * y + e[8] * z;
vec.y = e[1] * x + e[5] * y + e[9] * z;
vec.z = e[2] * x + e[6] * y + e[10] * z;
return vec;
}
/**
* 把变换矩阵展平成2d矩阵,避免出现三维坐标.
*/
function MatrixPlanarizere(mtx, z0 = true) {
mtx.elements[2] = 0;
mtx.elements[6] = 0;
mtx.elements[8] = 0;
mtx.elements[9] = 0;
mtx.elements[10] = Math.sign(mtx.elements[10]);
if (z0)
mtx.elements[14] = 0;
return mtx;
}
new three.Vector3;
new three.Vector3;
new three.Quaternion;
const tempMatrix1 = new three.Matrix4;
MakeMirrorMtx(new three.Vector3(0, 0, 1));
/**
* OSMODE
*/
var ObjectSnapMode;
(function (ObjectSnapMode) {
ObjectSnapMode[ObjectSnapMode["None"] = 0] = "None";
ObjectSnapMode[ObjectSnapMode["End"] = 1] = "End";
ObjectSnapMode[ObjectSnapMode["Mid"] = 2] = "Mid";
ObjectSnapMode[ObjectSnapMode["Cen"] = 4] = "Cen";
ObjectSnapMode[ObjectSnapMode["Node"] = 8] = "Node";
ObjectSnapMode[ObjectSnapMode["Qua"] = 16] = "Qua";
ObjectSnapMode[ObjectSnapMode["Int"] = 32] = "Int";
ObjectSnapMode[ObjectSnapMode["Ins"] = 64] = "Ins";
ObjectSnapMode[ObjectSnapMode["Per"] = 128] = "Per";
ObjectSnapMode[ObjectSnapMode["Tan"] = 256] = "Tan";
ObjectSnapMode[ObjectSnapMode["Nea"] = 512] = "Nea";
ObjectSnapMode[ObjectSnapMode["NotEntitySnap"] = 1024] = "NotEntitySnap";
ObjectSnapMode[ObjectSnapMode["App"] = 2048] = "App";
ObjectSnapMode[ObjectSnapMode["Ext"] = 4096] = "Ext";
ObjectSnapMode[ObjectSnapMode["Par"] = 8192] = "Par";
ObjectSnapMode[ObjectSnapMode["Axis"] = 16384] = "Axis";
ObjectSnapMode[ObjectSnapMode["All"] = 31743] = "All";
})(ObjectSnapMode || (ObjectSnapMode = {}));
/**
* 轨道控制的数学类,观察向量和角度的互相转换
* 当x当抬头或者低头到90度时,触发万向锁.
*/
class Orbit {
constructor() {
//抬头低头 正数抬头 负数低头
this.phi = 0; //Φ
//身体旋转 0为正右边 逆时针旋转
this.theta = 0; //θ
}
get RoX() {
return this.phi;
}
set RoX(v) {
this.phi = three.MathUtils.clamp(v, Math.PI * -0.49, Math.PI * 0.49);
}
/**
* 使用旋转角度 计算观察向量
* @param [outDirection] 引用传入,如果传入,那么就不构造新的向量
* @returns 返回观察向量
*/
UpdateDirection(outDirection = new three.Vector3()) {
outDirection.z = Math.sin(this.phi);
//归一化专用.
let d = Math.abs(Math.cos(this.phi));
outDirection.x = Math.cos(this.theta) * d;
outDirection.y = Math.sin(this.theta) * d;
return outDirection;
}
/**
* 使用观察向量,计算旋转角度
* @param dir 这个向量会被修改成单位向量.
*/
SetFromDirection(dir) {
dir.normalize();
this.phi = Math.asin(dir.z);
if (equaln$1(dir.x, 0) && equaln$1(dir.y, 0))
if (dir.z > 0)
this.theta = Math.PI * -0.5;
else
this.theta = Math.PI * 0.5;
else
this.theta = Math.atan2(dir.y, dir.x);
}
/**
* 参考任意轴坐标系算法.
* http://help.autodesk.com/view/ACD/2017/CHS/?guid=GUID-E19E5B42-0CC7-4EBA-B29F-5E1D595149EE
*/
static ComputUpDirection(n, ay = new three.Vector3(), ax = new three.Vector3()) {
n.normalize();
if (Math.abs(n.x) < 0.015625 && Math.abs(n.y) < 0.015625)
ax.crossVectors(YAxis, n);
else
ax.crossVectors(ZAxis, n);
ay.crossVectors(n, ax);
ax.normalize();
ay.normalize();
return ay;
}
}
/**
* 删除数组中指定的元素,返回数组本身
* @param {Array<any>} arr 需要操作的数组
* @param {*} el 需要移除的元素
*/
function arrayRemoveOnce(arr, el) {
let index = arr.indexOf(el);
if (index !== -1)
arr.splice(index, 1);
return arr;
}
/**
* 删除通过函数校验的元素
* @param {(e: T) => boolean} checkFuntion 校验函数
*/
function arrayRemoveIf(arr, checkFuntion) {
let j = 0;
for (let i = 0, l = arr.length; i < l; i++) {
if (!checkFuntion(arr[i])) {
arr[j++] = arr[i];
}
}
arr.length = j;
return arr;
}
function arrayLast(arr) {
return arr[arr.length - 1];
}
/**
* 根据数值从小到大排序数组
* @param {Array<T>} arr
* @returns {Array<T>} 返回自身
*/
function arraySortByNumber$1(arr) {
arr.sort(sortNumberCompart$1);
return arr;
}
/**
* 对排序好的数组进行去重操作
* @param {(e1, e2) => boolean} [checkFuction] 校验对象相等函数,如果相等 则删除e2
* @returns {Array<T>} 返回自身
*/
function arrayRemoveDuplicateBySort(arr, checkFuction = checkEqual) {
if (arr.length < 2)
return arr;
let j = 1;
for (let i = 1, l = arr.length; i < l; i++)
if (!checkFuction(arr[j - 1], arr[i]))
arr[j++] = arr[i];
arr.length = j;
return arr;
}
function sortNumberCompart$1(e1, e2) {
return e1 - e2;
}
function checkEqual(e1, e2) {
return e1 === e2;
}
/**
* 改变数组的值顺序
* @param arr 需要改变初始值位置的数组
* @param index //将index位置以后的值放到起始位置
*/
function changeArrayStartIndex(arr, index) {
arr.unshift(...arr.splice(index));
return arr;
}
function equalArray(a, b, checkF = checkEqual) {
if (a === b)
return true;
if (a.length !== b.length)
return false;
for (var i = 0; i < a.length; ++i)
if (!checkF(a[i], b[i]))
return false;
return true;
}
function arrayClone(arr) {
return arr.slice();
}
//https://jsperf.com/merge-array-implementations/30
function arrayPushArray$1(arr1, arr2) {
let arr1Length = arr1.length;
let arr2Length = arr2.length;
arr1.length = arr1Length + arr2Length;
for (let i = 0; i < arr2Length; i++)
arr1[arr1Length + i] = arr2[i];
return arr1;
}
function arraySum(arr) {
let sum = 0;
for (let n of arr)
sum += n;
return sum;
}
function GetGoodShaderSimple(color = new three.Vector3, side = three.FrontSide, logBuf = false, opacity = 1) {
return {
uniforms: {
"SurfaceColor": { value: color },
"opacity": { value: opacity },
},
side,
polygonOffset: true,
polygonOffsetFactor: 1,
polygonOffsetUnits: 1
};
}
const ColorPalette = [
[0, 0, 0, 0], //----- 0 - lets make it red for an example
//[255, 255, 255, 255],//----- 0 - ByBlock - White
[255, 0, 0, 255], //----- 1 - Red
// [255, 0, 0, 255], //----- 1 - Red
[255, 255, 0, 255], //----- 2 - Yellow
[0, 255, 0, 255], //----- 3 - Green
[0, 255, 255, 255], //----- 4 - Cyan
[0, 0, 255, 255], //----- 5 - Blue
[255, 0, 255, 255], //----- 6 - Magenta
// [255, 0, 0, 255], //----- 7 - More red Red
// [255, 0, 0, 255], //----- 8 - More red Red
// [255, 0, 0, 255], //----- 9 - More red Red
[255, 255, 255, 255], //----- 7 - White
[128, 128, 128, 255], //----- 8
[192, 192, 192, 255], //----- 9
[255, 0, 0, 255], //----- 10
[255, 127, 127, 255], //----- 11
[165, 0, 0, 255], //----- 12
[165, 82, 82, 255], //----- 13
[127, 0, 0, 255], //----- 14
[127, 63, 63, 255], //----- 15
[76, 0, 0, 255], //----- 16
[76, 38, 38, 255], //----- 17
[38, 0, 0, 255], //----- 18
[38, 19, 19, 255], //----- 19
[255, 63, 0, 255], //----- 20
[255, 159, 127, 255], //----- 21
[165, 41, 0, 255], //----- 22
[165, 103, 82, 255], //----- 23
[127, 31, 0, 255], //----- 24
[127, 79, 63, 255], //----- 25
[76, 19, 0, 255], //----- 26
[76, 47, 38, 255], //----- 27
[38, 9, 0, 255], //----- 28
[38, 23, 19, 255], //----- 29
[255, 127, 0, 255], //----- 30
[255, 191, 127, 255], //----- 31
[165, 82, 0, 255], //----- 32
[165, 124, 82, 255], //----- 33
[127, 63, 0, 255], //----- 34
[127, 95, 63, 255], //----- 35
[76, 38, 0, 255], //----- 36
[76, 57, 38, 255], //----- 37
[38, 19, 0, 255], //----- 38
[38, 28, 19, 255], //----- 39
[255, 191, 0, 255], //----- 40
[255, 223, 127, 255], //----- 41
[165, 124, 0, 255], //----- 42
[165, 145, 82, 255], //----- 43
[127, 95, 0, 255], //----- 44
[127, 111, 63, 255], //----- 45
[76, 57, 0, 255], //----- 46
[76, 66, 38, 255], //----- 47
[38, 28, 0, 255], //----- 48
[38, 33, 19, 255], //----- 49
[255, 255, 0, 255], //----- 50
[255, 255, 127, 255], //----- 51
[165, 165, 0, 255], //----- 52
[165, 165, 82, 255], //----- 53
[127, 127, 0, 255], //----- 54
[127, 127, 63, 255], //----- 55
[76, 76, 0, 255], //----- 56
[76, 76, 38, 255], //----- 57
[38, 38, 0, 255], //----- 58
[38, 38, 19, 255], //----- 59
[191, 255, 0, 255], //----- 60
[223, 255, 127, 255], //----- 61
[124, 165, 0, 255], //----- 62
[145, 165, 82, 255], //----- 63
[95, 127, 0, 255], //----- 64
[111, 127, 63, 255], //----- 65
[57, 76, 0, 255], //----- 66
[66, 76, 38, 255], //----- 67
[28, 38, 0, 255], //----- 68
[33, 38, 19, 255], //----- 69
[127, 255, 0, 255], //----- 70
[191, 255, 127, 255], //----- 71
[82, 165, 0, 255], //----- 72
[124, 165, 82, 255], //----- 73
[63, 127, 0, 255], //----- 74
[95, 127, 63, 255], //----- 75
[38, 76, 0, 255], //----- 76
[57, 76, 38, 255], //----- 77
[19, 38, 0, 255], //----- 78
[28, 38, 19, 255], //----- 79
[63, 255, 0, 255], //----- 80
[159, 255, 127, 255], //----- 81
[41, 165, 0, 255], //----- 82
[103, 165, 82, 255], //----- 83
[31, 127, 0, 255], //----- 84
[79, 127, 63, 255], //----- 85
[19, 76, 0, 255], //----- 86
[47, 76, 38, 255], //----- 87
[9, 38, 0, 255], //----- 88
[23, 38, 19, 255], //----- 89
[0, 255, 0, 255], //----- 90
[127, 255, 127, 255], //----- 91
[0, 165, 0, 255], //----- 92
[82, 165, 82, 255], //----- 93
[0, 127, 0, 255], //----- 94
[63, 127, 63, 255], //----- 95
[0, 76, 0, 255], //----- 96
[38, 76, 38, 255], //----- 97
[0, 38, 0, 255], //----- 98
[19, 38, 19, 255], //----- 99
[0, 255, 63, 255], //----- 100
[127, 255, 159, 255], //----- 101
[0, 165, 41, 255], //----- 102
[82, 165, 103, 255], //----- 103
[0, 127, 31, 255], //----- 104
[63, 127, 79, 255], //----- 105
[0, 76, 19, 255], //----- 106
[38, 76, 47, 255], //----- 107
[0, 38, 9, 255], //----- 108
[19, 38, 23, 255], //----- 109
[0, 255, 127, 255], //----- 110
[127, 255, 191, 255], //----- 111
[0, 165, 82, 255], //----- 112
[82, 165, 124, 255], //----- 113
[0, 127, 63, 255], //----- 114
[63, 127, 95, 255], //----- 115
[0, 76, 38, 255], //----- 116
[38, 76, 57, 255], //----- 117
[0, 38, 19, 255], //----- 118
[19, 38, 28, 255], //----- 119
[0, 255, 191, 255], //----- 120
[127, 255, 223, 255], //----- 121
[0, 165, 124, 255], //----- 122
[82, 165, 145, 255], //----- 123
[0, 127, 95, 255], //----- 124
[63, 127, 111, 255], //----- 125
[0, 76, 57, 255], //----- 126
[38, 76, 66, 255], //----- 127
[0, 38, 28, 255], //----- 128
[19, 38, 33, 255], //----- 129
[0, 255, 255, 255], //----- 130
[127, 255, 255, 255], //----- 131
[0, 165, 165, 255], //----- 132
[82, 165, 165, 255], //----- 133
[0, 127, 127, 255], //----- 134
[63, 127, 127, 255], //----- 135
[0, 76, 76, 255], //----- 136
[38, 76, 76, 255], //----- 137
[0, 38, 38, 255], //----- 138
[19, 38, 38, 255], //----- 139
[0, 191, 255, 255], //----- 140
[127, 223, 255, 255], //----- 141
[0, 124, 165, 255], //----- 142
[82, 145, 165, 255], //----- 143
[0, 95, 127, 255], //----- 144
[63, 111, 127, 255], //----- 145
[0, 57, 76, 255], //----- 146
[38, 66, 76, 255], //----- 147
[0, 28, 38, 255], //----- 148
[19, 33, 38, 255], //----- 149
[0, 127, 255, 255], //----- 150
[127, 191, 255, 255], //----- 151
[0, 82, 165, 255], //----- 152
[82, 124, 165, 255], //----- 153
[0, 63, 127, 255], //----- 154
[63, 95, 127, 255], //----- 155
[0, 38, 76, 255], //----- 156
[38, 57, 76, 255], //----- 157
[0, 19, 38, 255], //----- 158
[19, 28, 38, 255], //----- 159
[0, 63, 255, 255], //----- 160
[127, 159, 255, 255], //----- 161
[0, 41, 165, 255], //----- 162
[82, 103, 165, 255], //----- 163
[0, 31, 127, 255], //----- 164
[63, 79, 127, 255], //----- 165
[0, 19, 76, 255], //----- 166
[38, 47, 76, 255], //----- 167
[0, 9, 38, 255], //----- 168
[19, 23, 38, 255], //----- 169
[0, 0, 255, 255], //----- 170
[127, 127, 255, 255], //----- 171
[0, 0, 165, 255], //----- 172
[82, 82, 165, 255], //----- 173
[0, 0, 127, 255], //----- 174
[63, 63, 127, 255], //----- 175
[0, 0, 76, 255], //----- 176
[38, 38, 76, 255], //----- 177
[0, 0, 38, 255], //----- 178
[19, 19, 38, 255], //----- 179
[63, 0, 255, 255], //----- 180
[159, 127, 255, 255], //----- 181
[41, 0, 165, 255], //----- 182
[103, 82, 165, 255], //----- 183
[31, 0, 127, 255], //----- 184
[79, 63, 127, 255], //----- 185
[19, 0, 76, 255], //----- 186
[47, 38, 76, 255], //----- 187
[9, 0, 38, 255], //----- 188
[23, 19, 38, 255], //----- 189
[127, 0, 255, 255], //----- 190
[191, 127, 255, 255], //----- 191
[82, 0, 165, 255], //----- 192
[124, 82, 165, 255], //----- 193
[63, 0, 127, 255], //----- 194
[95, 63, 127, 255], //----- 195
[38, 0, 76, 255], //----- 196
[57, 38, 76, 255], //----- 197
[19, 0, 38, 255], //----- 198
[28, 19, 38, 255], //----- 199
[191, 0, 255, 255], //----- 200
[223, 127, 255, 255], //----- 201
[124, 0, 165, 255], //----- 202
[145, 82, 165, 255], //----- 203
[95, 0, 127, 255], //----- 204
[111, 63, 127, 255], //----- 205
[57, 0, 76, 255], //----- 206
[66, 38, 76, 255], //----- 207
[28, 0, 38, 255], //----- 208
[33, 19, 38, 255], //----- 209
[255, 0, 255, 255], //----- 210
[255, 127, 255, 255], //----- 211
[165, 0, 165, 255], //----- 212
[165, 82, 165, 255], //----- 213
[127, 0, 127, 255], //----- 214
[127, 63, 127, 255], //----- 215
[76, 0, 76, 255], //----- 216
[76, 38, 76, 255], //----- 217
[38, 0, 38, 255], //----- 218
[38, 19, 38, 255], //----- 219
[255, 0, 191, 255], //----- 220
[255, 127, 223, 255], //----- 221
[165, 0, 124, 255], //----- 222
[165, 82, 145, 255], //----- 223
[127, 0, 95, 255], //----- 224
[127, 63, 111, 255], //----- 225
[76, 0, 57, 255], //----- 226
[76, 38, 66, 255], //----- 227
[38, 0, 28, 255], //----- 228
[38, 19, 33, 255], //----- 229
[255, 0, 127, 255], //----- 230
[255, 127, 191, 255], //----- 231
[165, 0, 82, 255], //----- 232
[165, 82, 124, 255], //----- 233
[127, 0, 63, 255], //----- 234
[127, 63, 95, 255], //----- 235
[76, 0, 38, 255], //----- 236
[76, 38, 57, 255], //----- 237
[38, 0, 19, 255], //----- 238
[38, 19, 28, 255], //----- 239
[255, 0, 63, 255], //----- 240
[255, 127, 159, 255], //----- 241
[165, 0, 41, 255], //----- 242
[165, 82, 103, 255], //----- 243
[127, 0, 31, 255], //----- 244
[127, 63, 79, 255], //----- 245
[76, 0, 19, 255], //----- 246
[76, 38, 47, 255], //----- 247
[38, 0, 9, 255], //----- 248
[38, 19, 23, 255], //----- 249
[84, 84, 84, 255], //----- 250
[118, 118, 118, 255], //----- 251
[152, 152, 152, 255], //----- 252
[186, 186, 186, 255], //----- 253
[220, 220, 220, 255], //----- 254
[255, 255, 255, 255], //----- 255
[0, 0, 0, 0], //----- ByLayer - White ----256
[135, 206, 235, 255], //-----257
];
const LINE_WIDTH = 2;
//颜色材质,对于二维图像来说可能有用,应该不对三维对象使用该材质
class ColorMaterial {
constructor() { }
static GetLineMaterial(color, freeze = false) {
if (freeze)
color = 257;
if (this._LineMaterialMap.has(color))
return this._LineMaterialMap.get(color);
let mat = new three.LineBasicMaterial({ color: this.GetColor(color), side: three.DoubleSide });
this._LineMaterialMap.set(color, mat);
return mat;
}
static GetWallLineMtl(color) {
if (this._WallLineMtlMap.has(color))
return this._WallLineMtlMap.get(color);
let mtl = new three.LineDashedMaterial({
color: this.GetColor(color),
dashSize: 10,
gapSize: 10,
});
this._WallLineMtlMap.set(color, mtl);
return mtl;
}
static GetBasicMaterial(color) {
if (this._BasicMaterialMap.has(color))
return this._BasicMaterialMap.get(color);
let mtl = new three.MeshBasicMaterial({ color: this.GetColor(color) });
this._BasicMaterialMap.set(color, mtl);
return mtl;
}
static GetBasicMaterialDoubleSide(color) {
if (this._BasicDoubleSideMaterialMap.has(color))
return this._BasicDoubleSideMaterialMap.get(color);
let mtl = new three.MeshBasicMaterial({ color: this.GetColor(color), side: three.DoubleSide });
this._BasicDoubleSideMaterialMap.set(color, mtl);
return mtl;
}
static GetConceptualMaterial(color, side = three.FrontSide, enableTransparent = false, freeze = false, opacity) {
if (freeze)
color = 257;
let key = `${color},${side},${enableTransparent ? 1 : 0}`;
if (this._ConceptualMaterial.has(key))
return this._ConceptualMaterial.get(key);
let shaderParams = GetGoodShaderSimple(new three.Vector3().fromArray(this.GetColor(color).toArray()), side, ColorMaterial.UseLogBuf);
let mtl = new three.ShaderMaterial(shaderParams);
if (enableTransparent) {
Object.defineProperty(mtl, "transparent", {
get: () => mtl.uniforms.opacity.value !== 1
});
Object.defineProperty(mtl.uniforms.opacity, "value", {
get: () => {
let conceptualOpacity = HostApplicationServices.ConceptualOpacity;
if (freeze)
conceptualOpacity = 0.5;
else if (opacity)
conceptualOpacity = opacity;
return conceptualOpacity;
}
});
}
this._ConceptualMaterial.set(key, mtl);
return mtl;
}
static UpdateConceptualMaterial(useLogBuf) {
}
static GetPrintConceptualMaterial() {
if (!this._printConceptualMaterial) {
this._printConceptualMaterial = new three.ShaderMaterial({
uniforms: {
"SurfaceColor": { value: [1.0, 1.0, 1.0] }
},
polygonOffset: true,
polygonOffsetFactor: 1,
polygonOffsetUnits: LINE_WIDTH
});
}
return this._printConceptualMaterial;
}
static GetBasicMaterialTransparent(color, opacity) {
let key = `${color},${opacity}`;
let mat = this._BasicTransparentMaterialMap.get(key);
if (mat)
return mat;
mat = new three.MeshBasicMaterial({ transparent: true, opacity: opacity, side: three.DoubleSide, color: this.GetColor(color) });
this._BasicTransparentMaterialMap.set(key, mat);
return mat;
}
static GetBasicMaterialTransparent2(color, opacity) {
let key = `${color},${opacity}`;
let mtl = this._BasicTransparentMaterialMap2.get(key);
if (mtl)
return mtl;
mtl = new three.MeshBasicMaterial({ transparent: true, opacity: opacity, color: this.GetColor(color) });
this._BasicTransparentMaterialMap2.set(key, mtl);
return mtl;
}
static GetColor(color) {
let rgb = ColorPalette[color];
if (rgb)
return new three.Color(rgb[0] / 255, rgb[1] / 255, rgb[2] / 255);
//避免无法获得到颜色而产生的错误
return new three.Color();
}
static GetConceptualEdgeMaterial() {
if (!this._ConceptualEdgeMaterial)
this._ConceptualEdgeMaterial = new three.LineBasicMaterial({ color: this.GetColor(HostApplicationServices.ConceptualEdgeColor), side: three.DoubleSide });
return this._ConceptualEdgeMaterial;
}
static UpdateConceptualEdgeMaterial() {
if (this._ConceptualEdgeMaterial)
this._ConceptualEdgeMaterial.color.copy(this.GetColor(HostApplicationServices.ConceptualEdgeColor));
}
static GetPhysical2EdgeMaterial() {
if (!this._Physical2EdgeMaterial)
this._Physical2EdgeMaterial = new three.LineBasicMaterial({ color: this.GetColor(HostApplicationServices.Physical2EdgeColor), side: three.DoubleSide });
return this._Physical2EdgeMaterial;
}
static UpdatePhysical2EdgeMaterial() {
if (this._Physical2EdgeMaterial)
this._Physical2EdgeMaterial.color.copy(this.GetColor(HostApplicationServices.Physical2EdgeColor));
}
}
ColorMaterial._LineMaterialMap = new Map();
ColorMaterial._BasicMaterialMap = new Map();
ColorMaterial.UseLogBuf = false;
ColorMaterial._WallLineMtlMap = new Map();
ColorMaterial._BasicDoubleSideMaterialMap = new Map();
ColorMaterial._ConceptualMaterial = new Map();
ColorMaterial._BasicTransparentMaterialMap = new Map();
ColorMaterial._BasicTransparentMaterialMap2 = new Map();
//橡皮筋材质: 黄色 点划线
ColorMaterial.RubberBandMaterial = new three.LineDashedMaterial({
color: 0xF0B41E,
dashSize: 20,
gapSize: 8,
});
//极轴材质: 绿色 点划线
ColorMaterial.SnapAxisMaterial = new three.LineDashedMaterial({
color: 0x008B00,
dashSize: 5,
gapSize: 5
});
//打印线条材质
ColorMaterial.PrintLineMatrial = new LineMaterial.LineMaterial({
color: 0x000000,
linewidth: LINE_WIDTH,
dashed: false,
resolution: new three.Vector2(1000, 1000),
side: three.DoubleSide,
});
//灰色半透明(板件为不拆单时候的显示样式)
ColorMaterial.GrayTransparentMeshMaterial = new three.MeshBasicMaterial({
color: 0xcccccc,
transparent: true,
opacity: 0.3,
});
ColorMaterial.TransparentLineMaterial = new three.MeshBasicMaterial({
transparent: true,
opacity: 0,
});
const BufferGeometry2GeometryCacheMap = new WeakMap();
globalThis.fuck = BufferGeometry2GeometryCacheMap;
var BufferGeometryUtils;
(function (BufferGeometryUtils) {
function CreateFromPts(pts) {
return new three.BufferGeometry().setFromPoints(pts);
}
BufferGeometryUtils.CreateFromPts = CreateFromPts;
/**
* 更新BufferGeometry的顶点
* @param geo
* @param pts
* @param ignoreBoxError 忽略更新点后盒子错误的问题
* @returns 当成功时返回true,更新失败时返回false
*/
function UpdatePts(geo, pts, ignoreBoxError = false) {
let bf = geo.getAttribute("position");
if (bf === undefined)
geo.setFromPoints(pts);
else if (bf.count === pts.length || (ignoreBoxError && bf.count > pts.length)) //现在我们只有等于的时候才更新,因为如果不是这样,那么计算盒子的时候会出错(因为盒子内部的代码用的是所有的顶点)
{
bf.copyVector3sArray(pts);
bf.needsUpdate = true;
geo.drawRange.count = pts.length;
}
else //此时这个Geometry.Dispose后在进行重新生成会比较后,否则属性没有被回收导致内存泄漏
return false;
BufferGeometry2GeometryCacheMap.delete(geo);
return true;
}
BufferGeometryUtils.UpdatePts = UpdatePts;
let arrowGeometry;
function ArrowGeometry() {
if (arrowGeometry)
return arrowGeometry;
else {
let arrowShape = new three.Shape();
arrowShape.lineTo(-0.5, -1.8);
arrowShape.lineTo(0.5, -1.8);
arrowGeometry = new three.ShapeGeometry(arrowShape);
arrowGeometry.computeBoundingBox();
return arrowGeometry;
}
}
BufferGeometryUtils.ArrowGeometry = ArrowGeometry;
function MergeBufferGeometries(geometries, useGroups = false) {
if (geometries.length === 0)
return new three.BufferGeometry();
let isIndexed = geometries[0].index !== null;
let attributesUsed = new Set(Object.keys(geometries[0].attributes));
let morphAttributesUsed = new Set(Object.keys(geometries[0].morphAttributes));
let attributes = {};
let morphAttributes = {};
let morphTargetsRelative = geometries[0].morphTargetsRelative;
let mergedGeometry = new three.BufferGeometry();
let offset = 0;
let materialOffset = 0;
for (let i = 0; i < geometries.length; ++i) {
let geometry = geometries[i];
// ensure that all geometries are indexed, or none
if (isIndexed !== (geometry.index !== null))
return null;
// gather attributes, exit early if they're different
for (let name in geometry.attributes) {
if (!attributesUsed.has(name))
continue;
if (attributes[name] === undefined)
attributes[name] = [];
attributes[name].push(geometry.attributes[name]);
}
// gather morph attributes, exit early if they're different
if (morphTargetsRelative !== geometry.morphTargetsRelative)
return null;
for (let name in geometry.morphAttributes) {
if (!morphAttributesUsed.has(name))
continue;
if (morphAttributes[name] === undefined)
morphAttributes[name] = [];
morphAttributes[name].push(geometry.morphAttributes[name]);
}
// gather .userData
mergedGeometry.userData.mergedUserData = mergedGeometry.userData.mergedUserData || [];
mergedGeometry.userData.mergedUserData.push(geometry.userData);
if (useGroups) {
let count;
if (isIndexed)
count = geometry.index.count;
else if (geometry.attributes.position !== undefined)
count = geometry.attributes.position.count;
else
return null;
if (geometry.groups.length === 0) {
mergedGeometry.addGroup(offset, count, materialOffset);
materialOffset++;
}
else {
let maxMaterialIndex = 0;
for (let g of geometry.groups) {
maxMaterialIndex = Math.max(maxMaterialIndex, g.materialIndex ?? 0);
mergedGeometry.addGroup(offset + g.start, g.count, materialOffset + g.materialIndex);
}
materialOffset += maxMaterialIndex;
}
offset += count;
}
}
// merge indices
if (isIndexed) {
let indexOffset = 0;
let mergedIndex = [];
for (let i = 0; i < geometries.length; ++i) {
let index = geometries[i].index;
for (let j = 0; j < index.count; ++j) {
mergedIndex.push(index.getX(j) + indexOffset);
}
indexOffset += geometries[i].attributes.position.count;
}
mergedGeometry.setIndex(mergedIndex);
}
// merge attributes
for (let name in attributes) {
let mergedAttribute = MergeBufferAttributes(attributes[name]);
if (!mergedAttribute)
return null;
mergedGeometry.setAttribute(name, mergedAttribute);
}
// merge morph attributes
for (let name in morphAttributes) {
let numMorphTargets = morphAttributes[name][0].length;
if (numMorphTargets === 0)
break;
mergedGeometry.morphAttributes = mergedGeometry.morphAttributes || {};
mergedGeometry.morphAttributes[name] = [];
for (let i = 0; i < numMorphTargets; ++i) {
let morphAttributesToMerge = [];
for (let j = 0; j < morphAttributes[name].length; ++j) {
morphAttributesToMerge.push(morphAttributes[name][j][i]);
}
let mergedMorphAttribute = MergeBufferAttributes(morphAttributesToMerge);
if (!mergedMorphAttribute)
return null;
mergedGeometry.morphAttributes[name].push(mergedMorphAttribute);
}
}
return mergedGeometry;
}
BufferGeometryUtils.MergeBufferGeometries = MergeBufferGeometries;
function MergeBufferAttributes(attributes) {
let TypedArray;
let itemSize;
let normalized;
let arrayLength = 0;
for (let i = 0; i < attributes.length; ++i) {
let attribute = attributes[i];
if (TypedArray === undefined)
TypedArray = attribute.array.constructor;
if (TypedArray !== attribute.array.constructor)
return null;
if (itemSize === undefined)
itemSize = attribute.itemSize;
if (itemSize !== attribute.itemSize)
return null;
if (normalized === undefined)
normalized = attribute.normalized;
if (normalized !== attribute.normalized)
return null;
arrayLength += attribute.array.length;
}
let array = new TypedArray(arrayLength);
let offset = 0;
for (let i = 0; i < attributes.length; ++i) {
array.set(attributes[i].array, offset);
offset += attributes[i].array.length;
}
return new three.BufferAttribute(array, itemSize, normalized);
}
BufferGeometryUtils.MergeBufferAttributes = MergeBufferAttributes;
})(BufferGeometryUtils || (BufferGeometryUtils = {}));
var ExtendType;
(function (ExtendType) {
/**
* 前后都不延伸
*/
ExtendType[ExtendType["None"] = 0] = "None";
/**
* 只允许延伸前面
*/
ExtendType[ExtendType["Front"] = 1] = "Front";
/**
* 只允许延伸后面
*/
ExtendType[ExtendType["Back"] = 2] = "Back";
/**
* 前后延伸
*/
ExtendType[ExtendType["Both"] = 3] = "Both";
})(ExtendType || (ExtendType = {}));
/**
* 曲线的基类,子类请实现以下方法.
*/
let Curve = class Curve extends Entity {
constructor() {
super();
//------------------绘制相关------------------
//重载
this.OnlyRenderType = true;
}
get Is2D() {
return equaln$1(this._Matrix.elements[14], 0);
}
get StartPoint() { return; }
set StartPoint(v) { return; }
get StartParam() { return; }
get EndPoint() { return; }
set EndPoint(v) { return; }
/** 曲线中点 */
get Midpoint() {
return this.GetPointAtParam(this.MidParam);
}
get MidParam() {
if (this.EndParam === 1)
return 0.5;
else
return this.GetParamAtDist(this.Length * 0.5);
}
get EndParam() { return; }
get Area() { return 0; }
/**
*获得曲线的面积,逆时针为正,顺时针为负.
*/
get Area2() { return 0; }
get Length() { return 0; }
get IsClose() { return false; }
/** 曲线为顺时针 */
get IsClockWise() { return this.Area2 < 0; }
get Shape() { throw "未实现"; }
GetPointAtParam(param) { return; }
GetPointAtDistance(distance) { return; }
GetDistAtParam(param) { return; }
GetDistAtPoint(pt) { return; }
GetParamAtPoint(pt, fuzz = 1e-6) { return; }
GetParamAtPoint2(pt, fuzz = 1e-6) { return this.GetParamAtPoint(pt, fuzz); }
GetParamAtDist(d) { return; }
/**
* 返回曲线在指定位置的一阶导数(在wcs内)
* @param {(number | Vector3)} param
*/
GetFirstDeriv(param) { return; }
GetFirstDerivAngle(param) {
let d = this.GetFirstDeriv(param);
return Math.atan2(d.y, d.x);
}
/**
* 返回切割曲线后的结果.总是从起点开始切割,并且按顺序返回曲线.
* @param {(number[] | number)} param
*/
GetSplitCurves(param) { return; }
//未完善
GetCurveAtParamRange(startParam, EndParam) { return; }
GetSplitCurvesByPts(pt) {
let pts = Array.isArray(pt) ? pt : [pt];
let pars = pts.map(p => this.GetParamAtPoint(p));
return this.GetSplitCurves(pars);
}
SplitParamSort(param) {
if (Array.isArray(param)) {
param = param.filter(p => this.ParamOnCurve(p));
if (param.length === 0)
return [];
param.push(0, this.EndParam);
arraySortByNumber$1(param);
arrayRemoveDuplicateBySort(param, (e1, e2) => equaln$1(e1, e2, 1e-7));
return param;
}
else if (this.ParamOnCurve(param))
return [0, param, this.EndParam];
else
return [];
}
Extend(newParam) { }
/**
* 连接曲线到本曲线,如果成功返回true
* @param {Curve} cu 需要连接的曲线
* @returns {boolean} 连接成功
* @memberof Curve
*/
Join(cu, allowGap = false, tolerance = 1e-4) { return exports.Status.False; }
//翻转曲线.首尾调换.
Reverse() { return this; }
//点在曲线上
PtOnCurve(pt, fuzz = 1e-5) {
return equalv3(this.StartPoint, pt, fuzz) || equalv3(this.EndPoint, pt, fuzz) || this.ParamOnCurve(this.GetParamAtPoint(pt, fuzz));
}
//点在曲线中,不在起点或者终点.
PtOnCurve2(pt) {
return !(equalv3(this.StartPoint, pt, 1e-6) || equalv3(this.EndPoint, pt, 1e-6)) && this.ParamOnCurve(this.GetParamAtPoint(pt), 0);
}
//点在曲线上,已经确定点在曲线的延伸线上
PtOnCurve3(p, fuzz = 1e-6) {
return this.PtOnCurve(p, fuzz);
}
//参数在曲线上 容差,1e-6
ParamOnCurve(param, fuzz = 1e-6) { return !isNaN(param) && param >= -fuzz && param <= this.EndParam + fuzz; }
GetOffsetCurves(offsetDist) { return; }
GetClosestPointTo(pt, extend) { return; }
/**
* 曲线相交点
*/
IntersectWith(curve, intType, tolerance = 1e-6) {
return this.IntersectWith2(curve, intType, tolerance).map(r => r.pt);
}
/**
* 曲线相交点和点的参数
*/
IntersectWith2(curve, intType, tolerance = 1e-6) { return []; }
/**
* 拽托点个数
*/
GetDragPointCount(drag) { return 0; }
//样条线重载了这个,得到了更高的绘制精度
GetDrawCount() { return 30; }
/**
* @param {RenderType} [renderType=RenderType.Wireframe]
*/
InitDrawObject(renderType = RenderType.Wireframe) {
let pts = this.Shape.getPoints(this.GetDrawCount());
if (pts.length === 0)
pts.push(new three.Vector3);
if (renderType === RenderType.WireframePrint) {
let array = [];
for (let p of pts)
array.push(p.x, p.y, 0);
let geometry = new LineGeometry.LineGeometry().setPositions(array);
return new Line2.Line2(geometry, ColorMaterial.PrintLineMatrial);
}
let geo = new three.BufferGeometry().setFromPoints(pts);
return new three.Line(geo, ColorMaterial.GetLineMaterial(this._Color));
}
/**
* 重载:更新绘制的实体
* @param {RenderType} type
* @param {Object3D} obj
*/
UpdateDrawObject(type, obj) {
let pts = this.Shape.getPoints(this.GetDrawCount());
let plObj = obj;
let geo = plObj.geometry;
if (geo instanceof LineGeometry.LineGeometry) {
let array = [];
for (let p of pts)
array.push(p.x, p.y, 0);
geo.setPositions(array);
}
else {
//@ts-ignore
for (let p of pts)
p.z = 0;
if (!BufferGeometryUtils.UpdatePts(geo, pts))
updateGeometry(plObj, new three.BufferGeometry().setFromPoints(pts));
}
}
/**
* 重载:更新实体材质
*/
UpdateDrawObjectMaterial(type, obj, material) {
if (type === RenderType.WireframePrint) ;
else {
let m = obj;
m.material = material || ColorMaterial.GetLineMaterial(this._Color);
}
}
UpdateJigMaterial(color = 8) {
for (let [type, obj] of this._CacheDrawObject) {
this.UpdateDrawObjectMaterial(type, obj, ColorMaterial.GetLineMaterial(color));
}
}
};
Curve = __decorate([
Factory
], Curve);
var DragPointType;
(function (DragPointType) {
DragPointType[DragPointType["Grip"] = 0] = "Grip";
DragPointType[DragPointType["Stretch"] = 1] = "Stretch";
})(DragPointType || (DragPointType = {}));
class Matrix2 {
constructor() {
//column-major
this.el = [1, 0, 0, 1]; //ix iy jx jy [a c b d]
}
set(ix, iy, jx, jy) {
this.el[0] = ix;
this.el[1] = iy;
this.el[2] = jx;
this.el[3] = jy;
return this;
}
applyVector(vec) {
let x = vec.x;
let y = vec.y;
let e = this.el;
vec.x = e[0] * x + e[2] * y;
vec.y = e[1] * x + e[3] * y;
return this;
}
fromMatrix4(mtx4) {
this.set(mtx4.elements[0], mtx4.elements[1], mtx4.elements[3], mtx4.elements[4]);
}
setRotate(theta) {
let c = Math.cos(theta);
let s = Math.sin(theta);
this.set(c, s, -s, c);
return this;
}
//自我求逆矩阵,返回自身
invert() {
//ref:https://www.mathsisfun.com/algebra/matrix-inverse.html
let [a, c, b, d] = this.el;
let det = 1 / (a * d - b * c);
this.set(d * det, -c * det, -b * det, a * det);
return this;
}
}
class PlaneExt extends three.Plane {
constructor(normal = new three.Vector3(0, 0, 1), constant) {
super(normal);
if (typeof constant === "number")
this.constant = constant;
else if (constant)
this.parseConstantFromPoint(constant);
}
parseConstantFromPoint(constant) {
this.constant = -this.normal.dot(constant);
}
intersectLine(line, optionalTarget = new three.Vector3(), extendLine = false) {
let v1 = new three.Vector3();
let direction = line.delta(v1);
let denominator = this.normal.dot(direction);
if (denominator === 0) {
// line is coplanar, return origin
if (this.distanceToPoint(line.start) === 0) {
return optionalTarget.copy(line.start);
}
// Unsure if this is the correct method to handle this case.
return undefined;
}
let t = -(line.start.dot(this.normal) + this.constant) / denominator;
//If you not extendLine,check intersect point in Line
if (!extendLine && (t < -1e-6 || t > 1)) {
return undefined;
}
return optionalTarget.copy(direction).multiplyScalar(t).add(line.start);
}
intersectRay(ray, optionalTarget, extendLine) {
// 从射线初始位置
let line = new three.Line3(ray.origin.clone(), ray.origin.clone().add(ray.direction));
return this.intersectLine(line, optionalTarget, extendLine);
}
}
const ROTATE_MTX2 = new Matrix2().set(0, -1, 1, 0);
var Line_1;
exports.Line = Line_1 = class Line extends Curve {
constructor(_StartPoint = new three.Vector3, _EndPoint = new three.Vector3) {
super();
this._StartPoint = _StartPoint;
this._EndPoint = _EndPoint;
}
get Is2D() {
return super.Is2D && equaln$1(this._StartPoint.z, 0) && equaln$1(this._EndPoint.z, 0);
}
get Shape() {
return new three.Shape([AsVector2(this._StartPoint), AsVector2(this._EndPoint)]);
}
Z0() {
this.WriteAllObjectRecord();
let ocsInv = this.OCSInv;
let sp = this.StartPoint.setZ(0).applyMatrix4(ocsInv);
let ep = this.EndPoint.setZ(0).applyMatrix4(ocsInv);
this._StartPoint.copy(sp);
this._EndPoint.copy(ep);
this.Update();
return this;
}
ApplyScaleMatrix(m) {
this.WriteAllObjectRecord();
this.StartPoint = this.StartPoint.applyMatrix4(m);
this.EndPoint = this.EndPoint.applyMatrix4(m);
return this;
}
ApplyMirrorMatrix(m) {
this.WriteAllObjectRecord();
let sp = this.StartPoint;
let ep = this.EndPoint;
reviseMirrorMatrix(this._Matrix);
this.StartPoint = sp;
this.EndPoint = ep;
return this;
}
InitDrawObject(renderType = RenderType.Wireframe) {
let pts = [this._StartPoint, this._EndPoint];
if (renderType === RenderType.WireframePrint) {
let array = [];
for (let p of pts)
array.push(p.x, p.y, p.z);
let geometry = new LineGeometry.LineGeometry().setPositions(array);
return new Line2.Line2(geometry, ColorMaterial.PrintLineMatrial);
}
let geo = new three.BufferGeometry().setFromPoints(pts);
return new three.Line(geo, ColorMaterial.GetLineMaterial(this._Color));
}
/**
* 重载:更新绘制的实体
* @param {RenderType} type
* @param {Object3D} obj
*/
UpdateDrawObject(type, obj) {
let pts = [this._StartPoint, this._EndPoint];
let plObj = obj;
let geo = plObj.geometry;
if (geo instanceof LineGeometry.LineGeometry) {
let array = [];
for (let p of pts)
array.push(p.x, p.y, p.z);
geo.setPositions(array);
}
else {
if (!BufferGeometryUtils.UpdatePts(geo, pts))
updateGeometry(plObj, new three.BufferGeometry().setFromPoints(pts));
}
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
switch (snapMode) {
case ObjectSnapMode.End:
return [this.StartPoint, this.EndPoint];
case ObjectSnapMode.Mid:
return [this.GetPointAtParam(0.5)];
case ObjectSnapMode.Nea:
{
let derv = this.GetFirstDeriv(0).normalize();
let viewNormal = new three.Vector3().fromArray(viewXform.elements, 2 * 3);
//平行不捕捉
if (isParallelTo(viewNormal, derv))
return [];
let fNormal = new three.Vector3().crossVectors(viewNormal, derv);
fNormal.crossVectors(derv, fNormal);
let plane = new PlaneExt(fNormal, this.StartPoint);
let plocal = plane.intersectLine(new three.Line3(pickPoint, pickPoint.clone().add(viewNormal)), new three.Vector3(), true);
let pclosest = this.GetClosestPointTo(plocal, false);
return [pclosest];
}
case ObjectSnapMode.Ext:
return [this.GetClosestPointTo(pickPoint, true)];
case ObjectSnapMode.Per:
if (lastPoint) {
let { closestPt, param } = this.GetClosestAtPoint(lastPoint, true);
if (this.ParamOnCurve(param))
return [closestPt];
}
}
return [];
}
GetGripPoints() {
return [this.StartPoint, this.GetPointAtParam(0.5), this.EndPoint];
}
MoveGripPoints(indexList, vec) {
this.WriteAllObjectRecord();
for (let index of indexList) {
if (index === 0)
this.StartPoint = this.StartPoint.add(vec);
else if (index === 2)
this.EndPoint = this.EndPoint.add(vec);
else {
let m = MoveMatrix(vec);
this.ApplyMatrix(m);
}
}
}
GetStretchPoints() {
return [this.StartPoint, this.EndPoint];
}
MoveStretchPoints(indexList, vec) {
this.WriteAllObjectRecord();
for (let index of indexList) {
if (index === 0)
this.StartPoint = this.StartPoint.add(vec);
else
this.EndPoint = this.EndPoint.add(vec);
}
}
GetFirstDeriv(param) {
return this.EndPoint.sub(this.StartPoint);
}
/**
* 需要注意的是,平行线和共线无交点
* @param curve
* @param intType
* @param [tolerance=1e-4]
* @returns
*/
IntersectWith2(curve, intType, tolerance = 1e-4) {
if (curve instanceof Line_1 || curve.constructor.name === "RoomWallLine") {
return IntersectLineAndLine(this, curve, intType, tolerance);
}
if (curve instanceof exports.Arc || curve.constructor.name === "RoomWallArc") {
return IntersectLineAndArc(this, curve, intType, tolerance);
}
if (curve instanceof exports.Circle) {
return IntersectLineAndCircle(this, curve, intType, tolerance);
}
if (curve instanceof exports.Polyline) {
return SwapParam(IntersectPolylineAndCurve(curve, this, reverseIntersectOption(intType), tolerance));
}
if (curve instanceof Ellipse)
return IntersectEllipseAndLine(this, curve, intType, tolerance);
//其他的尚未实现.
return [];
}
//Param
GetPointAtParam(param) {
return this.StartPoint.add(this.GetFirstDeriv(0).multiplyScalar(param));
}
GetParamAtPoint(pt, fuzz = 1e-5) {
let { closestPt, param } = this.GetClosestAtPoint(pt, true);
if (!equalv3(closestPt, pt, fuzz))
return NaN;
return param;
}
GetParamAtDist(d) {
return d / this.Length;
}
GetPointAtDistance(distance) {
return this.GetPointAtParam(this.GetParamAtDist(distance));
}
GetDistAtParam(param) {
return this.Length * param;
}
GetDistAtPoint(pt) {
return this.GetDistAtParam(this.GetParamAtPoint(pt));
}
GetSplitCurves(param) {
let params = this.SplitParamSort(param);
let pts = params.map(param => this.GetPointAtParam(param));
let ret = new Array();
if (pts.length >= 2) {
for (let i = 0; i < pts.length - 1; i++) {
let newLine = this.Clone();
newLine.ColorIndex = this.ColorIndex;
newLine.SetStartEndPoint(pts[i], pts[i + 1]);
ret.push(newLine);
}
}
return ret;
}
GetParamAtPoint2(pt) {
let { param } = this.GetClosestAtPoint(pt, true);
return param;
}
//点在曲线上,已经确定点在曲线的延伸线上
PtOnCurve3(p, fuzz = 1e-6) {
let { param } = this.GetClosestAtPoint(p, true);
return this.ParamOnCurve(param, fuzz);
}
GetClosestAtPoint(pt, extend) {
let sp = this.StartPoint;
let ep = this.EndPoint;
if (equalv3(pt, sp, 1e-8))
return { closestPt: sp, param: 0 };
else if (equalv3(pt, ep, 1e-8))
return { closestPt: ep, param: 1 };
let direction = this.GetFirstDeriv(0);
let length = direction.length();
if (length === 0) {
let param = NaN;
if (equalv3(pt, this.StartPoint, 1e-6))
param = 0;
return { closestPt: sp, param: param };
}
direction.divideScalar(length);
let diff = pt.clone().sub(sp);
let param = direction.dot(diff);
let closestPt;
if (extend)
closestPt = sp.add(direction.multiplyScalar(param));
else if (param < 0) {
closestPt = sp;
param = 0;
}
else if (param > length) {
closestPt = this.EndPoint;
param = length;
}
else
closestPt = sp.add(direction.multiplyScalar(param));
return {
closestPt: closestPt,
param: param / length
};
}
GetClosestPointTo(pt, extend) {
return this.GetClosestAtPoint(pt, extend).closestPt;
}
Extend(newParam) {
this.WriteAllObjectRecord();
if (newParam < this.StartParam) {
this.StartPoint = this.GetPointAtParam(newParam);
}
else if (newParam > this.EndParam) {
this.EndPoint = this.GetPointAtParam(newParam);
}
}
Join(cu, allowGap = false, tolerance = 1e-5) {
if (cu instanceof Line_1 || cu.constructor.name === "RoomWallLine") {
//平行
if (!isParallelTo(this.GetFirstDeriv(0).normalize(), cu.GetFirstDeriv(0).normalize()))
return exports.Status.False;
let sp = cu.StartPoint;
let { closestPt: cp1, param: param1 } = this.GetClosestAtPoint(sp, true);
if (!equalv3(sp, cp1, tolerance)) //点在曲线上,允许较低的精度
return exports.Status.False;
let ep = cu.EndPoint;
let { closestPt: cp2, param: param2 } = this.GetClosestAtPoint(ep, true);
if (!equalv3(ep, cp2, tolerance))
return exports.Status.False;
if (param1 > param2) {
[param1, param2] = [param2, param1];
[sp, ep] = [ep, sp];
}
if (allowGap || Math.max(0, param1) < Math.min(1, param2) + tolerance / this.Length) //这里的容差是值容差,但是我们用它来判断参数,所以进行转换
{
if (param1 < 0)
this.StartPoint = sp;
if (param2 > 1)
this.EndPoint = ep;
return exports.Status.True;
}
}
return exports.Status.False;
}
Reverse() {
this.WriteAllObjectRecord();
[this._StartPoint, this._EndPoint] = [this._EndPoint, this._StartPoint];
return this;
}
GetOffsetCurves(offsetDist) {
let offset = this._EndPoint.clone().sub(this._StartPoint).normalize().multiplyScalar(offsetDist);
ROTATE_MTX2.applyVector(offset);
let newLine = this.Clone();
newLine.ClearDraw();
newLine._StartPoint.add(offset);
newLine._EndPoint.add(offset);
return [newLine];
}
get BoundingBox() {
return new three.Box3().setFromPoints([this.StartPoint, this.EndPoint]);
}
/**
* 返回对象在自身坐标系下的Box
*/
get BoundingBoxInOCS() {
return new Box3Ext().setFromPoints([this._StartPoint, this._EndPoint]);
}
get StartParam() {
return 0;
}
get EndParam() {
return 1;
}
//属性
get Length() { return this._StartPoint.distanceTo(this._EndPoint); }
//#region -----------------------------File-----------------------------
//对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
//对象从文件中读取数据,初始化自身
_ReadFile(file) {
super._ReadFile(file);
file.Read(); //1
this._StartPoint.fromArray(file.Read());
this._EndPoint.fromArray(file.Read());
}
//对象将自身数据写入到文件.
WriteFile(file) {
super.WriteFile(file);
file.Write(1); //ver
file.Write(this._StartPoint.toArray());
file.Write(this._EndPoint.toArray());
}
//#endregion-----------------------------File End-----------------------------
//#region 属性
set StartPoint(p) {
this.WriteAllObjectRecord();
this._StartPoint.copy(p).applyMatrix4(this.OCSInv);
this.Update();
}
get StartPoint() {
return this._StartPoint.clone().applyMatrix4(this.OCSNoClone);
}
get EndPoint() {
return this._EndPoint.clone().applyMatrix4(this.OCSNoClone);
}
set EndPoint(p) {
this.WriteAllObjectRecord();
this._EndPoint.copy(p).applyMatrix4(this.OCSInv);
this.Update();
}
SetStartEndPoint(s, e) {
this.WriteAllObjectRecord();
let inv = this.OCSInv;
this._StartPoint.copy(s).applyMatrix4(inv);
this._EndPoint.copy(e).applyMatrix4(inv);
this.Update();
}
};
exports.Line = Line_1 = __decorate([
Factory
], exports.Line);
var Ellipse_1;
let Ellipse = Ellipse_1 = class Ellipse extends Curve {
constructor(center, radX = 1e-3, radY = 1e-3, angle = 0) {
super();
this._startAngle = 0;
this._endAngle = Math.PI * 2;
center && this._Matrix.setPosition(center);
this._radX = radX;
this._radY = radY;
this._rotate = angle;
}
get StartParam() {
return 0;
}
get EndParam() {
return 1;
}
get StartPoint() {
return this.GetPointAtParam(0);
}
get EndPoint() {
return this.GetPointAtParam(1);
}
get Shape() {
let sp = new three.Shape();
sp.ellipse(0, 0, this._radX, this._radY, this._startAngle, this._endAngle, false, this._rotate);
return sp;
}
get IsClose() {
return equaln$1(this.TotalAngle, Math.PI * 2);
}
get Center() {
return new three.Vector3().setFromMatrixPosition(this._Matrix);
}
set Center(v) {
this.WriteAllObjectRecord();
this._Matrix.setPosition(v);
this.Update();
}
get RadX() {
return this._radX;
}
set RadX(v) {
this.WriteAllObjectRecord();
this._radX = v;
this.Update();
}
get RadY() {
return this._radY;
}
set RadY(v) {
this.WriteAllObjectRecord();
this._radY = v;
this.Update();
}
get Rotation() {
return this._rotate;
}
set Rotation(v) {
this.WriteAllObjectRecord();
this._rotate = v;
this.Update();
}
get StartAngle() {
return this._startAngle;
}
get EndAngle() {
return this._endAngle;
}
set StartAngle(v) {
this.WriteAllObjectRecord();
this._startAngle = v;
this.Update();
}
set EndAngle(v) {
this.WriteAllObjectRecord();
this._endAngle = v;
this.Update();
}
get Length() {
let a = this._radX;
let b = this._radY;
return Math.PI * Math.abs(3 * (a + b) - Math.sqrt((3 * a + b) * (a + 3 * b))) * this.TotalAngle / Math.PI * 0.5;
}
get Area() {
let area = Math.PI * this._radX * this._radY;
let an = this._endAngle - this._startAngle;
if (an < 0)
an = Math.PI * 2 + an;
area *= an / Math.PI * 0.5;
let area2 = Math.abs(getDeterminantFor2V(AsVector2(this.StartPoint.sub(this.Center)), AsVector2(this.EndPoint.sub(this.Center)))) / 2;
if (an < Math.PI)
area -= area2;
else
area += area2;
return area;
}
get TotalAngle() {
let totolAngle = this._endAngle - this._startAngle;
if (totolAngle <= 0)
totolAngle = Math.PI * 2 + totolAngle;
return totolAngle;
}
ApplyScaleMatrix(m) {
//或许我们应该在缩放一下轴尺寸 但是先不做了
let p = this.Position;
p.applyMatrix4(m);
this.Position = p;
return this;
}
Extend(newParam) {
this.WriteAllObjectRecord();
if (newParam < 0)
this._startAngle = this.GetAngleAtParam(newParam);
else if (newParam > 1)
this._endAngle = this.GetAngleAtParam(newParam);
this.Update();
}
PtInCurve(pt) {
let p = rotatePoint(pt.clone().sub(this.Center), -this.Rotation);
return p.x ** 2 / this.RadX ** 2 + p.y ** 2 / this.RadY ** 2 < 1;
}
PtOnCurve(pt) {
return this.PtOnEllipse(pt) && this.ParamOnCurve(this.GetParamAtPoint(pt));
}
PtOnEllipse(pt) {
let p = rotatePoint(pt.clone().applyMatrix4(this.OCSInv), -this.Rotation);
return equaln$1(p.x ** 2 / this.RadX ** 2 + p.y ** 2 / this.RadY ** 2, 1, 1e-3);
}
GetPointAtParam(param) {
let an = this.TotalAngle * param + this._startAngle;
let a = this.RadX;
let b = this.RadY;
let pt = new three.Vector3(a * Math.cos(an), b * Math.sin(an), 0);
let mtx = new Matrix2().setRotate(this._rotate);
mtx.applyVector(pt);
return pt.applyMatrix4(this.OCSNoClone);
}
GetParamAtPoint(pt) {
if (!this.PtOnEllipse(pt))
return NaN;
let an = this.GetCircleAngleAtPoint(pt);
let allAngle = this.TotalAngle;
let param = an / allAngle;
if (this.IsClose)
return param;
else {
if (an >= this._startAngle)
param = (an - this._startAngle) / allAngle;
else
param = ((Math.PI * 2) - (this._startAngle - an)) / allAngle;
//剩余的参数
let remParam = Math.PI * 2 / allAngle - 1;
if (param > (remParam * 0.5 + 1)) //一半
param = (param - 1) - remParam; //返回负数
return param;
}
}
GetPointAtDistance(distance) {
let param = distance / this.Length;
return this.GetPointAtParam(param);
}
GetDistAtParam(param) {
return this.Length * param;
}
GetDistAtPoint(pt) {
let param = this.GetParamAtPoint(pt);
return this.GetDistAtParam(param);
}
GetParamAtDist(d) {
return d / this.Length;
}
GetAngleAtParam(param) {
return this._startAngle + param * this.TotalAngle;
}
GetCircleAngleAtPoint(pt) {
pt = pt.clone().applyMatrix4(this.OCSInv);
let romtx = new Matrix2().setRotate(-this._rotate);
romtx.applyVector(pt);
//https://www.petercollingridge.co.uk/tutorials/computational-geometry/finding-angle-around-ellipse/
let an = Math.atan(this.RadX * pt.y / (this.RadY * pt.x));
if (pt.x < 0)
an += Math.PI;
else if (an < 0)
an += Math.PI * 2;
return an;
}
GetFirstDeriv(pt) {
if (typeof pt === "number")
pt = this.GetPointAtParam(pt);
else
pt = pt.clone();
let refPts = this.GetGripPoints();
let p = pt.clone().applyMatrix4(this.OCSInv).applyMatrix4(new three.Matrix4().makeRotationZ(-this._rotate));
let vec = new three.Vector3();
if (equalv3(pt, refPts[0]))
vec.set(0, 1, 0);
else if (equalv3(pt, refPts[1]))
vec.set(0, -1, 0);
else if (p.y > 0) {
let k = -(this._radY ** 2 * p.x) / (this._radX ** 2 * p.y);
vec.set(-1, -k, 0);
}
else {
let k = -(this._radY ** 2 * p.x) / (this._radX ** 2 * p.y);
vec.set(1, k, 0);
}
rotatePoint(vec, this._rotate);
return vec.applyMatrix4(this.OCS.setPosition(0, 0, 0));
}
GetClosestPointTo(p, extend) {
//参考:https://wet-robots.ghost.io/simple-method-for-distance-to-ellipse/
let ro = new three.Matrix4().makeRotationZ(this._rotate);
let roInv = new three.Matrix4().getInverse(ro);
let pt = p.clone().applyMatrix4(this.OCSInv).setZ(0).applyMatrix4(roInv);
let px = pt.x;
let py = pt.y;
let t = angle(pt);
let a = this._radX;
let b = this._radY;
let x, y;
for (let i = 0; i < 3; i++) {
x = a * Math.cos(t);
y = b * Math.sin(t);
let ex = (a ** 2 - b ** 2) * Math.cos(t) ** 3 / a;
let ey = (b * b - a * a) * Math.sin(t) ** 3 / b;
let rx = x - ex;
let ry = y - ey;
let qx = px - ex;
let qy = py - ey;
let r = Math.sqrt(ry ** 2 + rx ** 2);
let q = Math.sqrt(qy ** 2 + qx ** 2);
let dc = r * Math.asin((rx * qy - ry * qx) / (r * q));
let dt = dc / Math.sqrt(a * a + b * b - x * x - y * y);
t += dt;
}
let retPt = new three.Vector3(x, y).applyMatrix4(ro).applyMatrix4(this.OCSNoClone);
if (this.IsClose || extend) {
return retPt;
}
else if (this.PtOnCurve(retPt)) {
return retPt;
}
else {
let d1 = p.distanceToSquared(this.StartPoint);
let d2 = p.distanceToSquared(this.EndPoint);
return d1 < d2 ? this.StartPoint : this.EndPoint;
}
}
GetOffsetCurves(offsetDist) {
if ((offsetDist + Math.min(this._radX, this._radY)) > 0) {
let el = this.Clone();
el.RadX = this._radX + offsetDist;
el.RadY = this._radY + offsetDist;
return [el];
}
return [];
}
GetSplitCurves(param) {
let params;
if (param instanceof Array) {
params = param.filter(p => this.ParamOnCurve(p));
params.sort((a1, a2) => a2 - a1); //从大到小
}
else
params = [param];
//补上最后一个到第一个的弧
if (this.IsClose)
params.unshift(arrayLast(params));
else {
params.unshift(1);
params.push(0);
}
arrayRemoveDuplicateBySort(params);
let anglelist = params.map(param => this.TotalAngle * param + this._startAngle);
let elllist = [];
for (let i = 0; i < anglelist.length - 1; i++) {
let sa = anglelist[i];
let ea = anglelist[i + 1];
let el = this.Clone();
if (!equaln$1(sa, ea, 1e-6)) {
el.StartAngle = ea;
el.EndAngle = equaln$1(sa, 0) ? Math.PI * 2 : sa;
elllist.push(el);
}
}
return elllist;
}
Join(el) {
if (this.IsClose || el.IsClose || !this.IsCoplaneTo(el) || !equalv3(el.Center, this.Center))
return exports.Status.False;
let status = exports.Status.False;
if (equaln$1(this._endAngle, this._startAngle)) {
this.EndAngle = this._endAngle;
status = exports.Status.True;
}
else if (equaln$1(this._startAngle, el._endAngle)) {
this.StartAngle = el._startAngle;
status = exports.Status.True;
}
if (status === exports.Status.True && !this.IsClose && equaln$1(this._startAngle, this._endAngle)) {
this.StartAngle = 0;
this.EndAngle = Math.PI * 2;
}
return status;
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
switch (snapMode) {
case ObjectSnapMode.End:
{
let pts = this.GetGripPoints();
return pts;
}
case ObjectSnapMode.Cen:
return [this.Center];
case ObjectSnapMode.Nea:
{
return getArcOrCirNearPts(this, pickPoint, viewXform);
}
case ObjectSnapMode.Per:
if (lastPoint) {
if (equaln$1(lastPoint.distanceToSquared(this.Center), 0, 1e-10))
return [];
return [this.GetClosestPointTo(lastPoint, false)];
}
case ObjectSnapMode.Tan:
{
//TODO:过某点获取椭圆全部切点
if (lastPoint) {
return getTanPtsOnEllipse();
}
}
default:
return [];
}
}
IntersectWith2(curve, intType) {
//TODO:优化椭圆和椭圆,椭圆和圆相交
if (curve instanceof exports.Line) {
return SwapParam(IntersectEllipseAndLine(curve, this, reverseIntersectOption(intType)));
}
else if (curve instanceof exports.Circle || curve instanceof exports.Arc) {
return IntersectEllipseAndCircleOrArc(this, curve, intType);
}
else if (curve instanceof exports.Polyline) {
return SwapParam(IntersectPolylineAndCurve(curve, this, intType));
}
else if (curve instanceof Ellipse_1) {
return IntersectEllipse(this, curve);
}
else
return [];
}
GetStretchPoints() {
return this.GetGripPoints();
}
GetGripPoints() {
let tmpMat4 = new three.Matrix4().makeRotationZ(this.Rotation);
let pts = [
new three.Vector3(this._radX, 0),
new three.Vector3(-this._radX, 0),
new three.Vector3(0, this._radY),
new three.Vector3(0, -this._radY)
];
for (let p of pts)
p.applyMatrix4(tmpMat4).applyMatrix4(this.OCSNoClone);
if (!equaln$1(0, this._startAngle))
pts.push(this.StartPoint);
if (!equaln$1(0, this._endAngle))
pts.push(this.EndPoint);
pts.push(this.Center);
return pts;
}
MoveStretchPoints(indexList, vec) {
this.ApplyMatrix(MoveMatrix(vec));
}
MoveGripPoints(indexList, vec) {
let pts = this.GetStretchPoints();
if (indexList.length > 0) {
let p = pts[indexList[0]].clone();
p.add(vec);
if (indexList[0] <= 1)
this.RadX = p.distanceTo(this.Center);
else if (indexList[0] <= 3)
this.RadY = p.distanceTo(this.Center);
else {
let p1 = pts[indexList[0]];
//TODO:跟cad不一致待优化
if (equalv3(p1, this.StartPoint)) {
let v1 = p1.clone().sub(this.Center);
let v2 = p.clone().sub(this.Center);
let an = angleTo(v1, v2);
this.StartAngle = this.StartAngle + an;
}
else if (equalv3(p1, this.EndPoint)) {
let v1 = p1.clone().sub(this.Center);
let v2 = p.clone().sub(this.Center);
let an = angleTo(v2, v1);
this.EndAngle = this.EndAngle + an;
}
else
this.Center = p;
}
}
}
Convert2Polyline(count = 0) {
const MIN_LEN = 80;
const par = this.TotalAngle / Math.PI * 0.5;
if (!count) {
count = Math.floor(this.Length / par / MIN_LEN);
count = three.MathUtils.clamp(count, 15, 80);
}
count = Math.floor(count * par);
if ((count & 1) === 0)
count++;
let pts = this.Shape.getPoints(count);
if (this.IsClose)
pts.pop();
let pl = Pts2Polyline(pts, this.IsClose);
pl.ColorIndex = this.ColorIndex;
pl.ApplyMatrix(this.OCS);
if (this.IsClose)
pl.CloseMark = true;
return pl;
}
_ReadFile(file) {
super._ReadFile(file);
file.Read();
this._radX = file.Read();
this._radY = file.Read();
this._rotate = file.Read();
this._startAngle = file.Read();
this._endAngle = file.Read();
this.Update();
}
//对象将自身数据写入到文件.
WriteFile(file) {
super.WriteFile(file);
file.Write(1);
file.Write(this.RadX);
file.Write(this.RadY);
file.Write(this.Rotation);
file.Write(this._startAngle);
file.Write(this._endAngle);
}
};
Ellipse = Ellipse_1 = __decorate([
Factory
], Ellipse);
var Circle_1;
let circleGeometry;
function GetCircleGeometry() {
if (!circleGeometry)
circleGeometry = BufferGeometryUtils.CreateFromPts(new three.EllipseCurve(0, 0, 1, 1, 0, 2 * Math.PI, false, 0).getPoints(360).map(AsVector3));
return circleGeometry;
}
exports.Circle = Circle_1 = class Circle extends Curve {
constructor(center, radius = 1e-6) {
super();
center && this._Matrix.setPosition(center);
this._Radius = radius;
}
get Shape() {
let sp = new Shape2();
sp.ellipse(0, 0, this._Radius, this._Radius, 0, 2 * Math.PI, false, 0);
return sp;
}
get Center() {
return new three.Vector3().setFromMatrixPosition(this._Matrix);
}
set Center(v) {
this.WriteAllObjectRecord();
this._Matrix.setPosition(v);
this.Update();
}
get Radius() {
return this._Radius;
}
set Radius(v) {
this.WriteAllObjectRecord();
this._Radius = clamp(v, 1e-9, 1e19);
this.Update();
}
ApplyScaleMatrix(m) {
this.WriteAllObjectRecord();
this.Center = this.Center.applyMatrix4(m);
this.Radius = this.Radius * m.getMaxScaleOnAxis();
return this;
}
ApplyMirrorMatrix(m) {
this.WriteAllObjectRecord();
reviseMirrorMatrix(this._Matrix);
return this;
}
//******************** Curve function start*****************//
get StartPoint() {
return this.GetPointAtParam(0);
}
get StartParam() {
return 0;
}
get EndPoint() {
return this.GetPointAtParam(0);
}
get EndParam() {
return 1;
}
PtInCurve(pt) {
return pt.distanceToSquared(this.Center) < Math.pow(this.Radius, 2);
}
get Area() {
return Math.PI * this._Radius ** 2;
}
get Area2() {
return Math.PI * this._Radius ** 2;
}
get Length() {
return Math.PI * 2 * this._Radius;
}
get IsClose() {
return true;
}
//曲线为顺时针
get IsClockWise() { return false; }
GetPointAtParam(param) {
return polar(new three.Vector3(), param * 2 * Math.PI, this._Radius).applyMatrix4(this._Matrix);
}
GetPointAtDistance(distance) {
let param = distance / (Math.PI * 2 * this._Radius);
return this.GetPointAtParam(param);
}
GetDistAtParam(param) {
return Math.PI * 2 * this._Radius * param;
}
GetDistAtPoint(pt) {
let param = this.GetParamAtPoint(pt);
return this.GetDistAtParam(param);
}
GetParamAtDist(d) {
return d / (Math.PI * 2 * this._Radius);
}
GetSplitCurves(param) {
let params;
if (param instanceof Array) {
params = param.filter(p => this.ParamOnCurve(p));
params.sort((a1, a2) => a2 - a1); //从大到小
arrayRemoveDuplicateBySort(params);
if (params.length < 2)
return [];
}
else //圆不能被单个参数切割
return [];
//补上最后一个到第一个的弧
params.unshift(arrayLast(params));
let anglelist = params.map(param => Math.PI * 2 * param);
let curvelist = new Array();
for (let i = 0; i < anglelist.length - 1; i++) {
let sa = anglelist[i];
let ea = anglelist[i + 1];
if (!equaln$1(sa, ea, 1e-6)) {
let arc = new exports.Arc(new three.Vector3(), this._Radius, ea, sa, false);
arc.ColorIndex = this.ColorIndex;
arc.ApplyMatrix(this.OCS);
curvelist.push(arc);
}
}
return curvelist;
}
GetParamAtPoint(pt) {
if (!this.PtOnCurve(pt))
return NaN;
return angle(pt.clone().applyMatrix4(this.OCSInv)) / (Math.PI * 2);
}
PtOnCurve(pt, fuzz = 1e-5) {
return equaln$1(pt.distanceToSquared(this.Center), this._Radius * this._Radius, fuzz);
}
GetOffsetCurves(offsetDist) {
if ((offsetDist + this._Radius) > 0) {
let circle = this.Clone();
circle.Radius = this._Radius + offsetDist;
return [circle];
}
return [];
}
IntersectWith2(curve, intType, tolerance = 1e-5) {
if (curve instanceof exports.Arc) {
return IntersectCircleAndArc(this, curve, intType, tolerance);
}
if (curve instanceof exports.Line) {
return SwapParam(IntersectLineAndCircle(curve, this, reverseIntersectOption(intType), tolerance));
}
if (curve instanceof Circle_1) {
return IntersectCircleAndCircle(this, curve, tolerance);
}
if (curve instanceof Ellipse) {
return SwapParam(IntersectEllipseAndCircleOrArc(curve, this, intType));
}
if (curve instanceof exports.Polyline)
return SwapParam(IntersectPolylineAndCurve(curve, this, reverseIntersectOption(intType), tolerance));
return [];
}
//******************** Curve function end*****************//
get BoundingBoxInOCS() {
return new Box3Ext(new three.Vector3(-this.Radius, -this.Radius), new three.Vector3(this.Radius, this.Radius));
}
get BoundingBox() {
let z = this.Normal;
let x = new three.Vector3;
let y = new three.Vector3;
Orbit.ComputUpDirection(z, y, x);
let m = new three.Matrix4().makeBasis(x, y, z).setPosition(this.Center);
//使用任意轴坐标系 以便我们正确的对齐世界坐标系
return this.BoundingBoxInOCS.applyMatrix4(m);
}
InitDrawObject(renderType = RenderType.Wireframe) {
let obj = new three.Object3D();
let cirGeo = GetCircleGeometry();
if (renderType === RenderType.WireframePrint) {
let geometry = new LineGeometry.LineGeometry().setPositions(cirGeo.attributes.position.array);
obj.add(new Line2.Line2(geometry, ColorMaterial.PrintLineMatrial));
}
else {
let line = new three.Line(cirGeo, ColorMaterial.GetLineMaterial(this._Color));
obj.add(line);
}
this.UpdateDrawObject(renderType, obj);
return obj;
}
UpdateDrawObject(type, obj) {
obj.children[0].scale.set(this._Radius, this._Radius, this._Radius);
obj.children[0].updateMatrix();
}
UpdateDrawObjectMaterial(type, obj, material) {
if (type === RenderType.WireframePrint) ;
else {
let m = obj.children[0];
m.material = material ? material : ColorMaterial.GetLineMaterial(this._Color);
return obj;
}
}
GetDragPointCount(drag) {
if (drag === DragPointType.Grip)
return 5;
else
return 1;
}
GetGripPoints() {
let pts = [
new three.Vector3(),
new three.Vector3(0, this._Radius),
new three.Vector3(0, -this._Radius),
new three.Vector3(-this._Radius, 0),
new three.Vector3(this._Radius, 0),
];
let ocs = this.OCS;
pts.forEach(p => p.applyMatrix4(ocs));
return pts;
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
switch (snapMode) {
case ObjectSnapMode.Nea:
{
return getArcOrCirNearPts(this, pickPoint, viewXform);
}
case ObjectSnapMode.Cen:
return [this.Center];
case ObjectSnapMode.Per:
if (lastPoint) {
if (equaln$1(lastPoint.distanceToSquared(this.Center), 0, 1e-10))
return [];
let l = new exports.Line(this.Center, lastPoint);
return l.IntersectWith(this, IntersectOption.ExtendBoth);
}
case ObjectSnapMode.Tan:
let pts = GetTanPtsOnArcOrCircle(this, lastPoint);
if (pts)
return pts;
case ObjectSnapMode.End:
{
let pts = this.GetGripPoints();
pts.shift();
return pts;
}
}
return [];
}
MoveGripPoints(indexList, vec) {
let pts = this.GetGripPoints();
if (indexList.length > 0) {
let index = indexList[0];
let p = pts[index];
if (p) {
if (index > 0) {
p.add(vec);
this.Radius = p.distanceTo(this.Center);
}
else {
this.Center = this.Center.add(vec);
}
}
}
}
GetStretchPoints() {
let pts = [new three.Vector3()];
let ocs = this.OCS;
pts.forEach(p => p.applyMatrix4(ocs));
return pts;
}
MoveStretchPoints(indexList, vec) {
if (indexList.length > 0) {
let mat = MoveMatrix(vec);
this.ApplyMatrix(mat);
}
}
GetFirstDeriv(pt) {
if (typeof pt === "number")
pt = this.GetPointAtParam(pt);
else
pt = pt.clone();
pt.applyMatrix4(this.OCSInv);
let an = angle(pt) + Math.PI * 0.5;
return polar(new three.Vector3(), an, 1).applyMatrix4(new three.Matrix4().extractRotation(this.OCS));
}
GetClosestPointTo(pt, extend) {
pt = pt.clone().applyMatrix4(this.OCSInv);
if (equalv2(pt, ZeroVec, 1e-8))
return this.GetPointAtParam(0);
let a = Math.atan2(pt.y, pt.x);
return polar(new three.Vector3, a, this._Radius).applyMatrix4(this._Matrix);
}
//#region -------------------------File-------------------------
//对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
//对象从文件中读取数据,初始化自身
_ReadFile(file) {
super._ReadFile(file);
file.Read();
this._Radius = file.Read();
}
//对象将自身数据写入到文件.
WriteFile(file) {
super.WriteFile(file);
file.Write(1);
file.Write(this._Radius);
}
};
exports.Circle = Circle_1 = __decorate([
Factory
], exports.Circle);
const ARC_DRAW_CONFIG = {
ARC_SplitLength: 0.4, //圆的分段长度
ARC_RADIUS_MIN: 2.5, //大于半径25的自动优化成36等分圆 保证绘制光滑
Arc_MinSplitCount: 8, //圆的最小分段个数
ARC_MaxSplitCount: 90, //圆的最大分段个数
};
function GetArcDrawCount(arc) {
let radius = typeof arc === "number" ? arc : arc.Radius;
let splitCount = radius / ARC_DRAW_CONFIG.ARC_SplitLength;
//保证是偶数(避免奇数和Shape2计算方式一致导致的干涉)
splitCount = clamp(Math.floor(splitCount * 0.5) * 2, ARC_DRAW_CONFIG.Arc_MinSplitCount, ARC_DRAW_CONFIG.ARC_MaxSplitCount);
if (radius > ARC_DRAW_CONFIG.ARC_RADIUS_MIN)
splitCount = Math.max(36, splitCount);
return splitCount;
}
/**
*
* @param cu
*/
function SplitCurveParams(cu) {
let xparams = [];
if (cu instanceof exports.Circle) {
let splitCount = GetArcDrawCount(cu);
for (let i = 0; i < splitCount; i++)
xparams.push(i / splitCount);
}
else
//分段1
for (let i = 0; i < cu.EndParam; i++) {
xparams.push(i);
if (!equaln$1(cu.GetBulgeAt(i), 0, BUL_IS_LINE_FUZZ)) // is arc
{
let arc = cu.GetCurveAtIndex(i);
let splitCount = GetArcDrawCount(arc);
if (splitCount === 0)
continue;
let a = Math.PI * 2 / splitCount;
let params = [];
for (let j = 0; j < splitCount; j++) {
let param = arc.GetParamAtAngle(a * j);
if (arc.ParamOnCurve(param))
params.push(param);
}
arraySortByNumber$1(params);
if (params.length === 0)
continue;
for (let p of params) {
if (p > 1e-5 && p < 0.99999)
xparams.push(p + i);
}
}
}
xparams.push(cu.EndParam);
return xparams;
}
let tempArc;
class Shape2 extends three.Shape {
getPoints(divisions = 12, optimizeArc = true) {
let points = [], last;
for (let i = 0, curves = this.curves; i < curves.length; i++) {
let curve = curves[i];
let resolution = divisions;
//@ts-ignore
if (curve && curve.isEllipseCurve) {
if (optimizeArc) {
if (!tempArc)
tempArc = new exports.Arc;
else
tempArc.ClearDraw();
tempArc.IsClockWise = curve.aClockwise;
tempArc.StartAngle = curve.aStartAngle;
tempArc.EndAngle = curve.aEndAngle;
tempArc.Radius = Math.abs(curve.xRadius);
//根据圆的半径来确定绘制个数(与SplitCurveParams一致)
let splitCount = GetArcDrawCount(tempArc);
resolution = Math.max(1, Math.ceil(Math.abs((tempArc.AllAngle * 0.5) / Math.PI) * splitCount * 0.5)) * 2;
}
else
resolution = divisions * 2;
}
else {
//@ts-ignore
resolution = (curve && (curve.isLineCurve || curve.isLineCurve3)) ? 1
//@ts-ignore
: (curve && curve.isSplineCurve) ? divisions * curve.points.length
: divisions;
}
let pts = curve.getPoints(resolution);
for (let j = 0; j < pts.length; j++) {
let point = pts[j];
if (last && equalv2(last, point, 1e-4))
continue; // ensures no consecutive points are duplicates
points.push(point);
last = point;
if (j === pts.length - 1)
point["_mask_"] = true;
}
}
if (this.autoClose
&& points.length > 1
&& !points[points.length - 1].equals(points[0])) {
points.push(points[0]);
}
return points;
}
absellipse(aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation) {
let curve = new three.EllipseCurve(aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation);
/*
if (this.curves.length > 0)
{
// if a previous curve is present, attempt to join
let firstPoint = curve.getPoint(0);
if (!equalv2(firstPoint, this.currentPoint))
{
this.lineTo(firstPoint.x, firstPoint.y);
}
}
*/
this.curves.push(curve);
let lastPoint = curve.getPoint(1);
this.currentPoint.copy(lastPoint);
return this;
}
}
//解析二维圆弧
class Arc2d {
constructor(p1, p2, bul) {
p1 = p1.clone();
p2 = p2.clone();
//a (* 2 (atan b))
let a = Math.atan(bul) * 2;
//r (/ (distance p1 p2) 2 (sin a))
let r = p1.distanceTo(p2) / 2 / Math.sin(a);
//c (polar p1 (+ (- (/ pi 2) a) (angle p1 p2)) r)
let c = polar(p1.clone(), Math.PI / 2 - a + angle(p2.clone().sub(p1)), r);
this._Radius = Math.abs(r);
this._StartAn = angle(p1.sub(c));
this._EndAn = angle(p2.sub(c));
this._Center = c;
}
}
//创建轮廓 通过点表和凸度
function CreatePolylinePath(pts, buls) {
let shape = new Shape2();
if (pts.length === 0)
return shape;
let firstPt = pts[0];
shape.moveTo(firstPt.x, firstPt.y);
for (let i = 0; i < pts.length - 1; i++) {
let prePt = pts[i];
let nextPt = pts[i + 1];
if (equaln$1(buls[i], 0, 1e-8) || equalv2(prePt, nextPt, 1e-2)) {
shape.lineTo(nextPt.x, nextPt.y);
}
else {
//参考
//http://www.dorodnic.com/blog/tag/three-js/ 绘制一个齿轮
//https://www.kirupa.com/html5/drawing_circles_canvas.htm //html5
let arc2 = new Arc2d(prePt, nextPt, buls[i]);
let cen = arc2._Center;
shape.absarc(cen.x, cen.y, arc2._Radius, arc2._StartAn, arc2._EndAn, buls[i] < 0);
}
}
return shape;
}
/*
功能:判断线段是否存在交点
ref:https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
*/
// Given three colinear points p, q, r, the function checks if
// point q lies on line segment 'pr'
function onSegment(p, q, r) {
if (q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) &&
q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y))
return true;
return false;
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
function orientation(p, q, r) {
// See https://www.geeksforgeeks.org/orientation-3-ordered-points/
// for details of below formula.
let val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val === 0)
return 0; // colinear
return (val > 0) ? 1 : 2; // clock or counterclock wise
}
/**
* 判断线段`p1q1`和线段`p2q2`是否相交.
*/
function doIntersect(p1, q1, p2, q2) {
// Find the four orientations needed for general and
// special cases
let o1 = orientation(p1, q1, p2);
let o2 = orientation(p1, q1, q2);
let o3 = orientation(p2, q2, p1);
let o4 = orientation(p2, q2, q1);
// General case
if (o1 !== o2 && o3 !== o4)
return true;
// Special Cases
// p1, q1 and p2 are colinear and p2 lies on segment p1q1
if (o1 === 0 && onSegment(p1, p2, q1))
return true;
// p1, q1 and q2 are colinear and q2 lies on segment p1q1
if (o2 === 0 && onSegment(p1, q2, q1))
return true;
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
if (o3 === 0 && onSegment(p2, p1, q2))
return true;
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
if (o4 === 0 && onSegment(p2, q1, q2))
return true;
return false; // Doesn't fall in any of the above cases
}
function SplineConver2Polyline(spl, tolerance = 0.1) {
let cu = spl.Shape;
let cacheParam = new Map();
let cacheTange = new Map();
const GetPointAtParam = (param) => {
let p = cacheParam.get(param);
if (p)
return p;
p = cu.getPoint(param);
cacheParam.set(param, p);
return p;
};
const GetTangentAtParam = (param) => {
let t = cacheTange.get(param);
if (t)
return t;
if (equaln$1(param, 1)) {
if (spl.CloseMark)
t = cu.getPoint(1e-4).sub(GetPointAtParam(param)).normalize();
else
t = GetPointAtParam(param).clone().sub(cu.getPoint(1 - 1e-4)).normalize();
}
else
t = cu.getPoint(param + 1e-4).sub(GetPointAtParam(param)).normalize();
cacheTange.set(param, t);
return t;
};
let count = spl.EndParam;
let stepx = 1 / count;
let curves = [];
for (let i = 0; i < 1;) {
let step = 0.25 * stepx; //0.5的时候也可以有不错的收敛,但是在0.25的时候会有比较贴合的效果
while (true) {
let param1 = i;
let param2 = Math.min(1, i + step);
let x = param2 - param1;
let midp1 = GetPointAtParam(param1 + x * 0.25);
let midp2 = GetPointAtParam(param1 + x * 0.75);
let p1 = GetPointAtParam(param1);
let p2 = GetPointAtParam(param2);
let t1 = GetTangentAtParam(param1);
let t2 = GetTangentAtParam(param2);
let [c1, c2] = ComputeBiarc(p1, p2, t1, t2);
// TestDraw(new Point(midp1));
// TestDraw(new Point(midp2));
// TestDraw(new Point(c1.GetPointAtParam(0.5)));
// TestDraw(new Point(c2.GetPointAtParam(0.5)));
if (c1.GetClosestPointTo(midp1, false).distanceTo(midp1) < tolerance &&
c2.GetClosestPointTo(midp2, false).distanceTo(midp2) < tolerance) {
curves.push(c1, c2);
break;
}
else
step = step * 0.5;
}
i += step;
}
let polyline = exports.Polyline.Combine(curves, 1e-3);
polyline.ApplyMatrix(spl.OCSNoClone);
polyline.ColorIndex = spl.ColorIndex;
return polyline;
}
//传入的一定是碎点,不是碎点,没有6个点请不要进来
function PointsSimplify2Polyline(pts, tolerance = 0.1) {
let tolSq = tolerance * tolerance;
let cacheTange = new Map();
let ptsCount = pts.length;
const GetIndexAtParam = (param) => {
if (param === 1)
return pts.length - 1;
return Math.floor(ptsCount * param);
};
const GetPointAtParam = (param) => {
return pts[GetIndexAtParam(param)];
};
const GetTangentAtParam = (param) => {
let t = cacheTange.get(param);
if (t)
return t;
let index = Math.floor(ptsCount * param);
if (equaln$1(param, 1) || index + 1 >= pts.length)
t = arrayLast(pts).clone().sub(pts[pts.length - 1]).normalize();
else
t = pts[index + 1].clone().sub(pts[index]).normalize();
cacheTange.set(param, t);
return t;
};
let stepx = 1;
let curves = [];
for (let i = 0; i < 1;) {
let step = 0.25 * stepx; //0.5的时候也可以有不错的收敛,但是在0.25的时候会有比较贴合的效果
while (true) {
let param1 = i;
let param2 = Math.min(1, i + step);
let index1 = GetIndexAtParam(param1);
let index2 = GetIndexAtParam(param2);
if ((index2 - index1) < 4) //当点个数等于4时,我们计算双圆弧插值必然成功.(但是也失去了意义)
{
if (index2 - index1 > 0) {
let lineD = [];
for (let i = index1; i <= index2; i++)
lineD.push({ pt: AsVector2(pts[i]), bul: 0 });
curves.push(new exports.Polyline(lineD));
}
break;
}
let x = param2 - param1;
let midp1 = GetPointAtParam(param1 + x * 0.25);
let midp2 = GetPointAtParam(param1 + x * 0.75);
let p1 = pts[index1];
let p2 = pts[index2];
let t1 = GetTangentAtParam(param1);
let t2 = GetTangentAtParam(param2);
let [c1, c2] = ComputeBiarc(p1, p2, t1, t2);
if (c1.GetClosestPointTo(midp1, false).distanceToSquared(midp1) < tolSq &&
c2.GetClosestPointTo(midp2, false).distanceToSquared(midp2) < tolSq) {
curves.push(c1, c2);
break;
}
else
step = step * 0.5;
}
i += step;
}
let polyline = exports.Polyline.Combine(curves, 1e-3);
return polyline;
}
/**
* 将碎点简化为多段线,(尝试转换为简单圆弧,或者使用双圆弧插值)
* @param pts 点表
* @param [tolerance=0.1] 容差
* @param [parseArc=true] 解析成简单圆弧
* @param [lineLengthSq=2000]
* @returns 多段线
*/
function PointsSimplify2PolylineAndParseArc(pts, buls = undefined, tolerance = 0.1, parseArc = true, lineLengthSq = 2000) {
let tolSq = tolerance * tolerance;
let ptsCount = pts.length;
const CreateLineOrArc = (startIndex, endIndex) => {
if (!buls || equaln$1(buls[startIndex], 0, 1e-4))
return new exports.Line(AsVector3(pts[startIndex]), AsVector3(pts[endIndex]));
else
return new exports.Arc().ParseFromBul(AsVector3(pts[startIndex]), AsVector3(pts[endIndex]), buls[startIndex]);
};
let retPolyline = new exports.Polyline;
let start = 0;
for (let next = start + 1; next <= ptsCount; next++) {
if (next === ptsCount || pts[next].distanceToSquared(pts[next - 1]) > lineLengthSq || (buls && !equaln$1(buls[next - 1], 0, 1e-4))) //长度大于50,我们才认为它是一条直线
{
//1.将start->next-1部分组成圆弧
if (parseArc || (next - start) < 6)
while (start < next - 1) {
if ((next - 1) - start === 1) //直线
{
retPolyline.Join(CreateLineOrArc(start, next - 1));
break;
}
//第一个三角形的方向
let firstDir = orientation(pts[start], pts[start + 1], pts[start + 2]);
let to = start;
for (; to + 3 < next; to++) {
let dir = orientation(pts[to + 1], pts[to + 2], pts[to + 3]);
if (dir !== firstDir)
break;
}
if (start === to) //三个点
{
retPolyline.Join(CreateLineOrArc(start, start + 1));
retPolyline.Join(CreateLineOrArc(start + 1, start + 2));
start = to + 2;
continue;
}
else if ((to - start) < 3) {
retPolyline.Join(PointsSimplify2Polyline(pts.slice(start, to + 2 + 1).map(AsVector3)));
}
else {
let sp = pts[start];
let ep = pts[to + 2];
let mp = pts[Math.floor((start + to + 2) / 2)];
let arc = new exports.Arc().FromThreePoint(AsVector3(sp), AsVector3(mp), AsVector3(ep));
let c = to + 2 - start;
let p1 = AsVector3(pts[start + Math.floor(c * 0.25)]);
let p2 = AsVector3(pts[start + Math.floor(c * 0.75)]);
if (arc.GetClosestPointTo(p1, false).distanceToSquared(p1) < tolSq
&& arc.GetClosestPointTo(p2, false).distanceToSquared(p2) < tolSq)
retPolyline.Join(arc);
else
retPolyline.Join(PointsSimplify2Polyline(pts.slice(start, to + 2 + 1).map(AsVector3)));
}
start = to + 2; //闪烁到圆弧终点
if (start === next - 2) {
retPolyline.Join(PointsSimplify2Polyline(pts.slice(start, next).map(AsVector3)));
start = next;
break;
}
}
else
retPolyline.Join(PointsSimplify2Polyline(pts.slice(start, next).map(AsVector3)));
//2.加入直线
if (next !== ptsCount)
retPolyline.Join(CreateLineOrArc(next - 1, next));
start = next;
}
}
return retPolyline;
}
function SmartPointsSimply2Polyline(pts, buls = undefined, tolerance = 0.1, lineLengthSq = 2000) {
let pl1 = PointsSimplify2PolylineAndParseArc(pts, buls, tolerance, true, lineLengthSq);
let pl2 = PointsSimplify2PolylineAndParseArc(pts, buls, tolerance, false, lineLengthSq);
if (pl1.EndParam < pl2.EndParam)
return pl1;
else
return pl2;
}
/**
* 简化多段线,返回结果比较好的多段线
* @param pl
* @param [tolerance=0.1]
* @param [lineLengthSq=2000]
* @returns 返回undefined时,简化失败
*/
function SmartPolylineSimply2Polyline(pl, tolerance = 0.1, lineLengthSq = 2000) {
if (pl.EndParam < 3)
return;
let ld = pl.LineData;
let pts = [ld[0].pt];
let buls = [ld[0].bul];
let prep = pts[0];
for (let i = 1; i < ld.length; i++) {
let d = ld[i];
let p = d.pt;
if (!equalv2(p, prep, 1e-3)) {
pts.push(p);
buls.push(d.bul);
prep = p;
}
}
if (pl.CloseMark && !equalv2(pts[0], arrayLast(pts)))
pts.push(pts[0].clone());
let npl = SmartPointsSimply2Polyline(pts, pl.LineData.map(p => p.bul), tolerance, lineLengthSq);
npl.ApplyMatrix(pl.OCSNoClone);
npl.ColorIndex = pl.ColorIndex;
return npl;
}
//types
function Vec2(x, y) {
this.x = x;
this.y = y;
}
let c_Epsilon = 0.0001;
// math functions
function Sqr(val) {
return val * val;
}
function IsEqualEps(lhs, rhs) {
return Math.abs(lhs - rhs) <= c_Epsilon;
}
function Vec2_Add(lhs, rhs) {
return new Vec2(lhs.x + rhs.x, lhs.y + rhs.y);
}
function Vec2_Sub(lhs, rhs) {
return new Vec2(lhs.x - rhs.x, lhs.y - rhs.y);
}
function Vec2_Scale(lhs, scale) {
return new Vec2(lhs.x * scale, lhs.y * scale);
}
function Vec2_AddScaled(lhs, rhs, scale) {
return new Vec2(lhs.x + rhs.x * scale, lhs.y + rhs.y * scale);
}
function Vec2_Dot(lhs, rhs) {
return lhs.x * rhs.x + lhs.y * rhs.y;
}
function Vec2_MagSqr(val) {
return val.x * val.x + val.y * val.y;
}
function CreateArcFromEdge(p1, t1, p2, fromP1) {
let chord = Vec2_Sub(p2, p1);
let n1 = new Vec2(-t1.y, t1.x);
let chordDotN1 = Vec2_Dot(chord, n1);
if (IsEqualEps(chordDotN1, 0))
return new exports.Line(AsVector3(p1), AsVector3(p2));
else {
let radius = Vec2_MagSqr(chord) / (2 * chordDotN1);
let center = Vec2_AddScaled(p1, n1, radius);
let p1Offset = Vec2_Sub(p1, center);
let p2Offset = Vec2_Sub(p2, center);
let p1Ang1 = Math.atan2(p1Offset.y, p1Offset.x);
let p2Ang1 = Math.atan2(p2Offset.y, p2Offset.x);
if (p1Offset.x * t1.y - p1Offset.y * t1.x > 0)
return new exports.Arc(AsVector3(center), Math.abs(radius), p1Ang1, p2Ang1, !fromP1);
else
return new exports.Arc(AsVector3(center), Math.abs(radius), p1Ang1, p2Ang1, fromP1);
}
}
/**
* 计算双圆弧插值的圆弧
* @param p1 起点
* @param p2 终点
* @param t1 起点切线
* @param t2 终点切线
* @returns 两个圆弧(或者其中一个是直线)
*/
function ComputeBiarc(p1, p2, t1, t2) {
let v = Vec2_Sub(p2, p1);
let vMagSqr = Vec2_MagSqr(v);
let vDotT1 = Vec2_Dot(v, t1);
{
let t = Vec2_Add(t1, t2);
let tMagSqr = Vec2_MagSqr(t);
let equalTangents = IsEqualEps(tMagSqr, 4.0);
let perpT1 = IsEqualEps(vDotT1, 0.0);
if (equalTangents && perpT1) //2个半圆
{
let angle = Math.atan2(v.y, v.x);
let center1 = Vec2_AddScaled(p1, v, 0.25);
let center2 = Vec2_AddScaled(p1, v, 0.75);
let radius = Math.sqrt(vMagSqr) * 0.25;
let cross = v.x * t1.y - v.y * t1.x;
return [
new exports.Arc(AsVector3(center1), radius, angle, angle + Math.PI, cross < 0),
new exports.Arc(AsVector3(center2), radius, angle, angle + Math.PI, cross > 0)
];
}
else {
let vDotT = Vec2_Dot(v, t);
let d1;
if (equalTangents)
d1 = vMagSqr / (4 * vDotT1);
else {
let denominator = 2 - 2 * Vec2_Dot(t1, t2);
let discriminant = Sqr(vDotT) + denominator * vMagSqr;
d1 = (Math.sqrt(discriminant) - vDotT) / denominator;
}
let joint = Vec2_Scale(Vec2_Sub(t1, t2), d1);
joint = Vec2_Add(joint, p1);
joint = Vec2_Add(joint, p2);
joint = Vec2_Scale(joint, 0.5);
return [
CreateArcFromEdge(p1, t1, joint, true),
CreateArcFromEdge(p2, t2, joint, false)
];
}
}
}
var Spline_1;
const DrawSplitCount = 120;
let Spline = Spline_1 = class Spline extends Curve {
constructor(_PointList = []) {
super();
this._PointList = _PointList;
this._ClosedMark = false;
}
get Shape() {
return new three.CatmullRomCurve3(this.Points, this._ClosedMark);
}
get Length() {
//TODO:这个的性能挺低的(因为还需要重新获取一遍点表(如果我们有绘制对象,应该用绘制对象的点表来计算长度))
return this.Shape.getLength();
}
get Points() {
return this._PointList;
}
set Points(pts) {
if (pts.length < 2)
return;
this.WriteAllObjectRecord();
let ocsInv = this.OCSInv;
this._PointList = pts.map(p => p.clone().applyMatrix4(ocsInv));
if (pts.length > 2 && equalv3(this._PointList[0], arrayLast(this._PointList), 1e-3)) {
this._PointList.pop();
this._ClosedMark = true;
}
this.Update();
}
//闭合标志
get CloseMark() {
return this._ClosedMark;
}
//曲线是否闭合
get IsClose() {
return this.CloseMark || (equalv3(this.StartPoint, this.EndPoint, 1e-4)) && this.EndParam > 1;
}
set CloseMark(v) {
if (this._ClosedMark === v)
return;
this.WriteAllObjectRecord();
this._ClosedMark = v;
this.Update();
}
get StartPoint() {
return this._PointList[0];
}
get EndPoint() {
return arrayLast(this._PointList);
}
get StartParam() {
return 0;
}
get EndParam() {
return this._ClosedMark ? this._PointList.length : this._PointList.length - 1;
}
GetClosestPointTo(pt, extend) {
return this.Convert2Polyline().GetClosestPointTo(pt, extend);
}
GetOffsetCurves(offsetDist) {
if (offsetDist === 0)
return [];
let pld = this._PointList.map(p => {
return { pt: AsVector2(p), bul: 0 };
});
let pl = new exports.Polyline(pld);
let pls = pl.GetOffsetCurves(offsetDist);
return pls.map(pl => {
let pts = pl.LineData.map(p => AsVector3(p.pt));
let spl = new Spline_1(pts);
spl.OCS = this._Matrix;
spl._ClosedMark = this._ClosedMark;
return spl;
});
}
GetGripPoints() {
return this._PointList.map(p => p.clone().applyMatrix4(this.OCSNoClone));
}
GetStretchPoints() {
return this.GetGripPoints();
}
MoveGripPoints(indexList, vec) {
vec = vec.clone().applyMatrix4(this.OCSInv.setPosition(0, 0, 0)).setZ(0);
this.WriteAllObjectRecord();
for (let index of indexList)
this._PointList[index].add(vec);
this.Update();
}
MoveStretchPoints(indexList, vec) {
vec = vec.clone().applyMatrix4(this.OCSInv.setPosition(0, 0, 0)).setZ(0);
this.WriteAllObjectRecord();
for (let index of indexList)
this._PointList[index].add(vec);
this.Update();
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
switch (snapMode) {
case ObjectSnapMode.End:
return this.GetStretchPoints();
case ObjectSnapMode.Mid:
case ObjectSnapMode.Nea:
case ObjectSnapMode.Ext:
case ObjectSnapMode.Cen:
case ObjectSnapMode.Per:
case ObjectSnapMode.Tan:
}
return [];
}
GetDrawCount() {
return this.EndParam * DrawSplitCount;
}
Convert2Polyline() {
return SplineConver2Polyline(this);
}
_ReadFile(file) {
super._ReadFile(file);
let ver = file.Read(); //1
let count = file.Read();
this._PointList.length = 0;
for (let i = 0; i < count; i++)
this._PointList.push(new three.Vector3().fromArray(file.Read()));
if (ver > 1)
this._ClosedMark = file.Read();
}
WriteFile(file) {
super.WriteFile(file);
file.Write(2); //ver
file.Write(this._PointList.length);
this._PointList.forEach(p => file.Write(p.toArray()));
file.Write(this._ClosedMark);
}
};
Spline = Spline_1 = __decorate([
Factory
], Spline);
/**
* 简化优化版本的曲线求交, 优化版本可以参考(算法导论33.2 确定任意一对线段是否相交 p599)
*/
class CurveIntersection {
/**
* @param {Curve[]} cus 请注意数组的顺序会被更改,如果你在意数组的顺序,请拷贝数组后传进来
* @memberof CurveIntersection
*/
constructor(cus, parseIntersectionParam = false, intType = IntersectOption.ExtendNone, fuzz = 1e-6, parseRecord = false) {
this.fuzz = fuzz;
//用来缓存的曲线包围盒
this.boxMap = new Map();
/**
* 交点数据集,key 为曲线 value 为和它相交的(曲线和交点的Map)
*/
this.intersect = new Map();
//交点参数集
this.intersect2 = new Map();
this.intersect3 = [];
this.GenBox(cus);
//按x排序
this.SortCurve(cus);
let count = cus.length;
for (let i = 0; i < count; i++) {
let c1 = cus[i];
let c1d = this.GetIntersect(c1);
let c1b = this.boxMap.get(c1);
for (let j = i + 1; j < count; j++) {
let c2 = cus[j];
//过滤掉不需要计算的曲线
let c2b = this.boxMap.get(c2);
if (c2b.min.x - c1b.max.x > fuzz)
break;
if (c2b.min.y - c1b.max.y > fuzz)
continue;
let ints = this.IntersectWith2(c1, c2, intType);
if (ints.length > 0) {
let pts = ints.map(i => i.pt);
c1d.set(c2, pts);
this.GetIntersect(c2).set(c1, pts);
if (parseRecord)
this.intersect3.push([c1, c2, pts]);
if (parseIntersectionParam) {
this.AppendIntersectionParams(c1, ints.map(i => [i.thisParam, i.pt]));
this.AppendIntersectionParams(c2, ints.map(i => [i.argParam, i.pt]));
}
}
}
}
}
IntersectWith2(c1, c2, intType) {
return c1.IntersectWith2(c2, intType);
}
AppendIntersectionParams(curve, params) {
let arr = this.intersect2.get(curve);
if (!arr) {
arr = [];
this.intersect2.set(curve, arr);
}
arrayPushArray$1(arr, params);
}
GenBox(cus) {
for (let c of cus)
this.boxMap.set(c, c.BoundingBox);
}
SortCurve(cus) {
cus.sort((c1, c2) => {
return this.boxMap.get(c1).min.x - this.boxMap.get(c2).min.x;
});
}
GetIntersect(cu) {
if (this.intersect.has(cu))
return this.intersect.get(cu);
let m = new Map();
this.intersect.set(cu, m);
return m;
}
}
class CurveIntersection2 extends CurveIntersection {
/**
* Curve2Polyline使用这个时,为了避免多余的交点导致曲线切割过度,过滤掉无关的点
*/
IntersectWith2(c1, c2, intType) {
let pts = c1.IntersectWith2(c2, intType);
return pts.filter(p => {
let inC1 = c1.ParamOnCurve(p.thisParam) || (p.thisParam < 0 ? c1.StartPoint.distanceTo(p.pt) < this.fuzz : c1.EndPoint.distanceTo(p.pt) < this.fuzz);
if (!inC1)
return false;
let inC2 = c2.ParamOnCurve(p.argParam) || (p.argParam < 0 ? c2.StartPoint.distanceTo(p.pt) < this.fuzz : c2.EndPoint.distanceTo(p.pt) < this.fuzz);
return inC2;
});
}
}
/**
* 曲线连接图
* 所有的顶点和边的关系
*/
class CurveMap {
constructor(numdimensions = 4, _RemoveSortLine = false, multiplier = 10 ** numdimensions) {
this.numdimensions = numdimensions;
this._RemoveSortLine = _RemoveSortLine;
this.multiplier = multiplier;
/*
节点图.
每个节点对应下一个路口的路线表.
路口表使用逆时针排序,起始角度使用正x轴.
*/
this._VerticeMap = new Map();
this._Vertices = [];
this._LookupTable = {};
}
/**
* 得到节点图的所有站点列表
*/
get Stands() {
return this._Vertices;
}
/**
* @param curve
* @param [isArc=curve instanceof Arc]
* @param [removeDuplicate=false]
* @returns 加入成功?
*/
AddCurveToMap(curve, isArc = curve instanceof exports.Arc, removeDuplicate = false, parseAngle = false) {
let sp = curve.StartPoint;
let ep = curve.EndPoint;
let startS = this.GetOnlyVertice(sp);
let endS = this.GetOnlyVertice(ep);
//在面域分析中,路线指向同一个顶点已经没有意义了
if (this._RemoveSortLine && startS === endS)
return false;
if (removeDuplicate) //删除重复
{
let index = startS.routes.findIndex(r => {
if (r.to === endS && r.curve.constructor.name === curve.constructor.name) {
if (isArc)
return equalv3(curve.GetPointAtParam(0.5), r.curve.GetPointAtParam(0.5));
return true;
}
});
if (index !== -1)
return false;
}
let length = curve.Length;
curve.TempData = 0;
let routeS2E = { curve, isReverse: false, length, from: startS, to: endS, s: sp, e: ep };
let routeE2S = { curve, isReverse: true, length, from: endS, to: startS, e: sp, s: ep };
if (!isArc && parseAngle) {
let an = angle(endS.position.clone().sub(startS.position));
routeS2E.an = an;
routeE2S.an = clampRad(an + Math.PI);
}
startS.routes.push(routeS2E);
endS.routes.push(routeE2S);
return true;
}
/**
* 获得唯一的顶点
*/
GetOnlyVertice(p) {
let gp = this.GenerateP(p);
if (this._VerticeMap.has(gp))
return this._VerticeMap.get(gp);
let vertice = { position: gp, routes: [] };
this._VerticeMap.set(p, vertice);
this._Vertices.push(vertice);
return vertice;
}
/**
* 生成一个唯一的向量.
*/
GenerateP(p) {
let key = "";
let els = p.toArray();
for (let n of els) {
let valueQuantized = Math.round(n * this.multiplier);
key += valueQuantized + '/';
}
if (key in this._LookupTable)
return this._LookupTable[key];
let hashparts = els.map((el) => {
let q0 = Math.floor(el * this.multiplier);
let q1 = q0 + 1;
return ['' + q0 + '/', '' + q1 + '/'];
});
let numelements = els.length;
let numhashes = 1 << numelements;
for (let hashmask = 0; hashmask < numhashes; ++hashmask) {
let hashmaskShifted = hashmask;
key = '';
for (let hashpart of hashparts) {
key += hashpart[hashmaskShifted & 1];
hashmaskShifted >>= 1;
}
this._LookupTable[key] = p;
}
return p;
}
}
const _overlap_ = "_overlap_";
/**
面域分析,基于最小循环图重新实现的版本,拓展了实现求最大轮廓。
当最大轮廓=最小轮廓时,只绘制最大轮廓(独立轮廓无分裂)。
算法只实现去重模式,业务场景应该没有非去重模式。
如果需要非去重模式那么应该获取到多个CurveMap然后对多个CurveMap进行面域分析得出多个重叠的面域。
*/
class RegionParse {
/**
* @param cuList 请不要传递圆和椭圆.
* @param [numDimensions=3] 精度:小数点后个数
* @param [removeDuplicate=true] 删除重复(现在必须是true,请不要修改它)
*/
constructor(cuList, numDimensions = 3, removeDuplicate = true) {
this.numDimensions = numDimensions;
this.removeDuplicate = removeDuplicate;
//区域列表 通常是外轮廓
this.RegionsOutline = []; //外轮廓和内轮廓重叠时,只有外轮廓有数据,可以使用RegionParse.RegionsIsOverlap来取得
//区域列表 通常是内轮廓
this.RegionsInternal = [];
//碎线 曲线进入到这里会被炸开.
this.ExpLineMap = new Map();
//需要搜索的站
let vertices = this.GenerateVerticeMap(cuList);
//移除细丝
while (true) {
let v = vertices.find(v => v.routes.length < 2);
if (v)
this.RemoveFilamentAt(v, vertices);
else
break;
}
let lowerVertice;
while (vertices.length > 0) {
lowerVertice = lowerVertice?.routes.length > 1 ? lowerVertice : this.FindLowerLeftStand(vertices);
let minWalk = ClosedWalkFrom(lowerVertice, this._CurveCount, WalkType.Min);
let maxWalk = ClosedWalkFrom(lowerVertice, this._CurveCount, WalkType.Max);
this.RemoveEdge(minWalk[0]);
this.RemoveFilamentAt(minWalk[0].from, vertices);
this.RemoveFilamentAt(minWalk[0].to, vertices);
minWalk = ReduceWalk(minWalk);
maxWalk = ReduceWalk(maxWalk);
if (maxWalk.length > 1) {
this.RegionsOutline.push(maxWalk);
if (minWalk.length === maxWalk.length && minWalk.every((w1, index) => w1 === maxWalk[index])) //大小重叠
{
//直接remove,不用计算引用个数
for (let w of minWalk) {
w.curve.TempData = 2;
this.RemoveEdge(w);
this.RemoveFilamentAt(w.from, vertices);
this.RemoveFilamentAt(w.to, vertices);
}
maxWalk[_overlap_] = true;
continue; //继续循环
}
else
for (let w of maxWalk)
w.curve.TempData = 1;
}
if (minWalk.length > 1) // && minWalk.every(w => <number>(w.curve.TempData) < 2) 没有重复线应该不会被用2次
{
this.RegionsInternal.push(minWalk);
for (let w of minWalk) {
w.curve.TempData++;
if (w.curve.TempData === 2) {
this.RemoveEdge(w);
this.RemoveFilamentAt(w.from, vertices);
this.RemoveFilamentAt(w.to, vertices);
}
}
}
}
}
//大小圈重叠
static RegionsIsOverlap(Route) {
return Boolean(Route[_overlap_]);
}
RemoveFilamentAt(v, vertices) {
let current = v;
while (current && current.routes.length < 2) {
vertices = arrayRemoveOnce(vertices, current);
let r = current.routes[0];
if (r) {
this.RemoveEdge(r);
current = r.to;
}
else
current = undefined;
}
}
RemoveEdge(r) {
let index = r.from.routes.findIndex(rr => rr.curve === r.curve);
if (index !== -1)
r.from.routes.splice(index, 1);
index = r.to.routes.findIndex(rr => rr.curve === r.curve);
if (index !== -1)
r.to.routes.splice(index, 1);
}
/**
* 找到最下方并且最左边的站 yx
*/
FindLowerLeftStand(vertices) {
return vertices.reduce((m, v) => {
let dy = v.position.y - m.position.y;
if (dy < 0)
return v;
if (dy > 0)
return m;
return v.position.x - m.position.x < 0 ? v : m;
});
}
/**
* 构造路线图. 每个节点对应下一个路口的路线表. 路口表使用逆时针排序,起始角度使用正x轴.
* @returns 所有的顶点
*/
GenerateVerticeMap(curveList) {
let curveMap = new CurveMap(this.numDimensions, true);
//将多段线炸开
let plcus = [];
arrayRemoveIf(curveList, c => {
if (c instanceof exports.Polyline) {
let cus = c.Explode();
//如果为圆弧,提前打断
let arcs = [];
arrayRemoveIf(cus, c => {
if (c.Length < 1e-5)
return true;
if (c instanceof exports.Arc) {
let arcBrs = this.BreakArc(c);
for (let arc of arcBrs)
arcs.push(arc);
}
return false;
});
//加入到计算
cus.push(...arcs);
this.ExpLineMap.set(c, cus);
plcus.push(...cus);
return true;
}
return false;
});
curveList.push(...plcus);
this._CurveCount = curveList.length;
for (let cu of curveList) {
//由于圆弧可能导致最低点计算错误的问题.
if (cu instanceof exports.Arc) {
let arcs = this.BreakArc(cu);
if (arcs.length > 1) {
arcs.forEach(a => curveMap.AddCurveToMap(a, true, this.removeDuplicate, true));
this.ExpLineMap.set(cu, arcs);
continue;
}
else
curveMap.AddCurveToMap(cu, true, this.removeDuplicate, true);
}
else
curveMap.AddCurveToMap(cu, false, this.removeDuplicate, true);
}
//排序,根据角度逆时针排序.
for (let v of curveMap._Vertices) {
let minLength = Infinity;
for (let r of v.routes)
if (r.length < minLength)
minLength = r.length;
for (let r of v.routes)
CalcRouteAngle(r, minLength * 0.2);
v.routes.sort((r1, r2) => r1.an - r2.an);
}
return curveMap.Stands;
}
BreakArc(arc) {
let underPt = arc.Center.add(new three.Vector3(0, -arc.Radius));
let param = arc.GetParamAtPoint(underPt);
if (param > 1e-4 && param < 0.9999)
return arc.GetSplitCurves(param);
else
return [arc];
}
/**
* 曲线是否已经被算法使用
*/
GetCueveUsed(cu) {
if (this.ExpLineMap.has(cu)) {
let use = this.ExpLineMap.get(cu).some(c => c.TempData > 0);
if (!use)
this.ExpLineMap.delete(cu);
return use;
}
else
return cu.TempData > 0;
}
}
function CalcRouteAngle(r, length) {
if (r.an !== undefined)
return;
let cu = r.curve;
let p = r.isReverse ?
cu.GetPointAtParam(cu.GetParamAtDist(r.length - length))
: cu.GetPointAtParam(cu.GetParamAtDist(length));
r.an = angle(p.sub(r.from.position));
}
var WalkType;
(function (WalkType) {
WalkType[WalkType["Min"] = 1] = "Min";
WalkType[WalkType["Max"] = -1] = "Max";
})(WalkType || (WalkType = {}));
function ClosedWalkFrom(startVertice, maxRoute, type = WalkType.Min) {
let walk = [];
let curVertice = startVertice;
let preRoute;
// console.log("start", type, startVertice.position.toArray());
do {
let route = GetNextRoute(curVertice, preRoute, type);
if (type === WalkType.Max && route.curve.TempData > 0)
return [];
// console.log(route.to.position.toArray());
walk.push(route);
[curVertice, preRoute] = [route.to, route];
if (walk.length > maxRoute * 2)
throw "超过计算次数限制";
} while (curVertice !== startVertice);
return walk;
}
/**
* 删除中途回路
*/
function ReduceWalk(w) {
if (w.length === 0)
return w;
//未构成回路,直接回家
if (w[0].curve === arrayLast(w).curve)
return [];
for (let i = 0; i < w.length; i++) {
let r1 = w[i];
for (let j = w.length; j--;) {
if (i === j)
break;
let r2 = w[j];
if (r1.to === r2.to) {
if (j > i)
w.splice(i + 1, j - i);
break;
}
}
}
return w;
}
function GetNextRoute(v, prev, type = WalkType.Min) {
if (!prev)
return arrayLast(v.routes); //顺时针 cw \|/ 从左往右
//逆时针 ccw 往左
let index = v.routes.findIndex(r => r.curve === prev.curve);
let newIndex = FixIndex$1(index + 1 * type, v.routes);
return v.routes[newIndex];
}
/**
* 某些时候我们不能创建轮廓,此时我们使用类似c2r的方法来构建一个外部轮廓.
*/
function CreateContours(curves, fuzz = 1e-4) {
let contours = [];
let extendsMinDistSq = fuzz * fuzz;
//炸开多段线(防止自交多段线)
let newCurves = [];
for (let cu of curves) {
if (cu instanceof exports.Circle)
contours.push(Contour.CreateContour(cu.Clone())); //避免将原始曲线传递给板,导致撤销这个圆失败
else if (cu instanceof exports.Polyline)
arrayPushArray$1(newCurves, cu.Explode());
else if (cu instanceof Spline) {
let pl = cu.Convert2Polyline();
if (pl.IsClose)
contours.push(Contour.CreateContour(pl, false));
else
newCurves.push(pl);
}
else if (cu instanceof Ellipse)
Contour.CreateContour(cu.Convert2Polyline(), false);
else
newCurves.push(cu);
}
let intersect = new CurveIntersection2(newCurves, false, IntersectOption.ExtendBoth, fuzz);
let curves2 = [];
//延伸+打断
for (let [cu, pmap] of intersect.intersect) {
let sp = cu.StartPoint;
let ep = cu.EndPoint;
let epExtend;
let epDist = Infinity;
let spExtend;
let spDist = Infinity;
let isClose = cu.IsClose;
let ipts = [];
for (let [, pts] of pmap) {
arrayPushArray$1(ipts, pts);
if (!isClose)
for (let p of pts) {
let d = p.distanceToSquared(ep);
if (d < epDist) {
epDist = d;
epExtend = p;
}
d = p.distanceToSquared(sp);
if (d < spDist) {
spDist = d;
spExtend = p;
}
}
}
if (!isClose) {
//延伸
if (epDist > 0 && epDist < extendsMinDistSq) {
let param = cu.GetParamAtPoint(epExtend);
if (param > cu.EndParam)
cu.Extend(param);
}
if (spDist > 0 && spDist < extendsMinDistSq) {
let param = cu.GetParamAtPoint(spExtend);
if (param < 0)
cu.Extend(param);
}
}
//打断
let curves;
if (ipts.length > 0)
curves = cu.GetSplitCurvesByPts(ipts);
else
curves = [cu];
let tempCus = [];
for (let c of curves) {
if (c instanceof exports.Polyline)
arrayPushArray$1(tempCus, c.Explode());
else
tempCus.push(c);
}
arrayPushArray$1(curves2, tempCus);
}
let parse = new RegionParse(curves2);
for (let rs of parse.RegionsOutline) {
let curves = rs.map(r => r.curve);
let contour = Contour.CreateContour(curves, false);
if (contour)
contours.push(contour);
}
return contours;
}
function CreateContour2(curves, fuzz = 1e-4) {
return CreateContours(curves, fuzz)[0];
}
exports.BoolOpeartionType = void 0;
(function (BoolOpeartionType) {
BoolOpeartionType[BoolOpeartionType["Intersection"] = 0] = "Intersection";
BoolOpeartionType[BoolOpeartionType["Union"] = 1] = "Union";
BoolOpeartionType[BoolOpeartionType["Subtract"] = 2] = "Subtract";
})(exports.BoolOpeartionType || (exports.BoolOpeartionType = {}));
const fuzz = 1e-3;
let fuzzV3 = new three.Vector3(fuzz, fuzz, fuzz);
//判断小曲线是不是被大曲线包含(或者重叠?)
function isTargetCurInOrOnSourceCur(bigCurve, smallCurve) {
//#fix fileid 2087494 使用二维的box来计算看起来没有问题
if (!three.Box2.prototype.containsBox.apply(bigCurve.BoundingBox.expandByVector(fuzzV3), [smallCurve.BoundingBox]))
return false;
let cus = [];
if (smallCurve instanceof exports.Polyline)
cus = smallCurve.Explode();
else
cus = [smallCurve];
return cus.every(c => {
let pts = getIntPtContextPts(bigCurve, c);
if (pts.length <= 1)
pts.push(c.StartPoint, c.EndPoint);
return IsPtsAllInOrOnReg(bigCurve, pts);
});
}
//获取交点处上下距0.01par的点
function getIntPtContextPts(sourceCur, cu, pts = []) {
let interPts = cu.IntersectWith(sourceCur, IntersectOption.ExtendNone);
if (interPts.length > 0) {
let pars = interPts.map(pt => cu.GetParamAtPoint(pt));
for (let par of pars) {
if (par >= 0.02)
pts.push(cu.GetPointAtParam(par - 0.01));
if (par <= (cu.EndParam - 0.02))
pts.push(cu.GetPointAtParam(par + 0.01));
}
}
return pts;
}
//判断点点是否全部都在封闭区域内或者在曲线上
function IsPtsAllInOrOnReg(sourceReg, pts) {
return pts.every(pt => {
//是否点在封闭曲线内
return sourceReg.PtOnCurve(pt) || sourceReg.PtInCurve(pt);
});
}
//判断点是否全部都在封闭区域外或者在曲线上
function IsPtsAllOutOrOnReg(sourceReg, pts) {
return pts.every(pt => {
//是否点在封闭曲线内
return sourceReg.PtOnCurve(pt) || !sourceReg.PtInCurve(pt);
});
}
let cache$1 = new WeakMap();
const COMBINE_FUZZ = 1e-2;
class Contour {
SetCurve(cu) {
if (cu instanceof exports.Polyline) {
if (cu.Area2 < 0)
cu.Reverse();
}
this._Curve = cu;
}
/**会将传入的闭合轮廓改为逆时针 */
static CreateContour(cus, needLink = true) {
if (cus instanceof Curve) {
if (cus.IsClose) {
let c = new Contour();
c.SetCurve(cus);
return c;
}
return;
}
let closeCurve = Contour.Combine(cus, needLink, COMBINE_FUZZ);
if (closeCurve && closeCurve.IsClose) {
if (closeCurve instanceof exports.Polyline && closeCurve.CloseMark === false) {
closeCurve.CloseMark = true;
closeCurve.RemoveVertexAt(closeCurve.NumberOfVertices - 1);
}
let c = new Contour();
c.SetCurve(closeCurve);
return c;
}
}
get Curve() {
return this._Curve;
}
get Area() {
return this._Curve.Area;
}
get BoundingBox() {
return this._Curve.BoundingBox;
}
/**
* 不等比例缩放
* @param {number} ref 缩放参考值,大于该值的点缩放
* @param {number} dist 缩放距离
* @param {string} dir x y z
*/
UnEqualProportionScale(ref, dist, dir) {
let cu = this._Curve;
if (cu instanceof exports.Polyline) {
let lineData = cu.LineData;
let length = lineData.length;
let p = cu.Position[dir];
let moveIndexs = [];
for (let i = 0; i < length; i++) {
if (lineData[i].pt[dir] + p > ref)
moveIndexs.push(i);
}
let moveVec = new three.Vector3();
moveVec[dir] = dist;
cu.MoveStretchPoints(moveIndexs, moveVec);
return true;
}
return false;
}
Clone() {
return Contour.CreateContour([this._Curve.Clone()]);
}
//交集:结果数组为空则失败
IntersectionBoolOperation(target) {
if (!IntersectBox2(this.BoundingBox, target.BoundingBox))
return [];
let resultCus = this.GetIntersetAndUnionList(target);
return Contour.GetAllContour(resultCus.intersectionList);
}
//并集:结果轮廓数组长度大于2,则失败.等于1则成功.
UnionBoolOperation(target) {
let resultCus = this.GetIntersetAndUnionList(target);
//快速
if (resultCus.unionList.every(c => c.IsClose))
return {
contours: Contour.GetAllContour(resultCus.unionList),
holes: [],
};
//并集后的线段表如果有共线的直接合并起来
let cus = [];
for (let pl of resultCus.unionList) {
if (pl instanceof exports.Polyline)
cus.push(...pl.Explode());
else
cus.push(pl);
}
let cuGroups = curveLinkGroup(cus);
for (let g of cuGroups) {
for (let i = 0; i < g.length; i++) {
let c1 = g[i];
let nextI = FixIndex$1(i + 1, g);
let c2 = g[nextI];
let status = c1.Join(c2);
if (status === exports.Status.True) {
g.splice(nextI, 1);
i--;
}
else if (status === exports.Status.ConverToCircle) {
g.length = 0;
let a = c1;
g.push(new exports.Circle(a.Center, a.Radius));
break;
}
}
}
let allContour = Contour.GetAllContour(cuGroups);
if (allContour.length < 2) {
return {
contours: allContour,
holes: [],
};
}
else {
let cache = new WeakMap();
for (let c of allContour)
cache.set(c, c.Area);
allContour.sort((a, b) => cache.get(b) - cache.get(a));
return {
contours: [allContour[0]],
holes: allContour.slice(1)
};
}
}
//差集:等于0完全被减去
SubstactBoolOperation(target) {
let subtractList = this.GetSubtractList(target);
//纯网洞
if (subtractList.every(c => c.IsClose))
return Contour.GetAllContour(subtractList);
let regParse = new RegionParse(subtractList, 2);
let contours = [];
//分析封闭包围区域
const parseRoute = (routeSet) => {
for (let routes of routeSet) {
let cs = routes.map(r => r.curve);
let c = Contour.CreateContour(cs, false);
if (c
&& !equalCurve(c.Curve, this.Curve)
&& !equalCurve(c.Curve, target.Curve)
&& c.Area > 1e-3)
contours.push(c);
}
};
parseRoute(regParse.RegionsOutline);
parseRoute(regParse.RegionsInternal);
return contours;
}
/**
* 计算与目标轮廓布尔运算后的结果曲线.
*/
GetIntersetAndUnionList(target) {
//同心圆
if (this._Curve instanceof exports.Circle && target._Curve instanceof exports.Circle && equalv2(this._Curve.Center, target._Curve.Center, 1e-3)) {
if (this._Curve.Radius > target._Curve.Radius)
return { intersectionList: [target._Curve], unionList: [this._Curve] };
else
return { intersectionList: [this._Curve], unionList: [target._Curve] };
}
let intersectionList = [];
let unionList = [];
let sourceOutline = this._Curve;
let targetOutline = target.Curve;
let isEqualNormal = equalv3(sourceOutline.Normal, targetOutline.Normal, 1e-3);
//可能会有提升,但是好像不大(并且还有更慢的趋势)
// if (!sourceOutline.BoundingBox.intersectsBox(targetOutline.BoundingBox, 1e-3))
// return { intersectionList, unionList };
let interPts = sourceOutline.IntersectWith2(targetOutline, IntersectOption.ExtendNone, COMBINE_FUZZ);
let sourceContainerTarget;
let targetContainerSource;
if (sourceOutline.Area > targetOutline.Area) {
sourceContainerTarget = CurveContainerCurve(sourceOutline, targetOutline, interPts);
targetContainerSource = false;
}
else {
sourceContainerTarget = false;
targetContainerSource = CurveContainerCurve(targetOutline, sourceOutline, interPts);
}
//包含.相交.分离(三种状态)
if (sourceContainerTarget) //源包含目标
{
intersectionList.push(targetOutline);
unionList.push(sourceOutline);
}
else if (targetContainerSource) //目标包含源
{
unionList.push(targetOutline);
intersectionList.push(sourceOutline);
}
else if (interPts.length <= 1) //分离
{
unionList.push(sourceOutline, targetOutline);
}
else //相交 interPts.length > 0
{
let pars1 = interPts.map(r => r.thisParam);
let pars2 = interPts.map(r => r.argParam);
let sourceCus = sourceOutline.GetSplitCurves(pars1);
let targetCus = targetOutline.GetSplitCurves(pars2);
for (let pl of sourceCus) {
let hasEqualCus = false;
for (let i = 0; i < targetCus.length; i++) {
let cu = targetCus[i];
hasEqualCus = fastEqualCurve(cu, pl, COMBINE_FUZZ);
if (hasEqualCus) {
//方向相同
if (equalv3(cu.GetFirstDeriv(cu.MidParam).normalize(), pl.GetFirstDeriv(pl.MidParam).normalize(), 1e-2)
=== isEqualNormal) {
unionList.push(pl);
intersectionList.push(pl);
}
targetCus.splice(i, 1);
break;
}
}
if (hasEqualCus)
continue;
if (fastCurveInCurve(targetOutline, pl))
intersectionList.push(pl);
else
unionList.push(pl);
}
for (let pl of targetCus) {
if (fastCurveInCurve(sourceOutline, pl))
intersectionList.push(pl);
else
unionList.push(pl);
}
//特殊的分离
if (intersectionList.length === 0 && unionList.length === (sourceCus.length + targetCus.length)) {
return { intersectionList, unionList: [sourceOutline, targetOutline] };
}
}
return { intersectionList, unionList };
}
GetSubtractList(target) {
let sourceOutline = this._Curve;
let targetOutline = target.Curve;
let isEqualNormal = equalv3(sourceOutline.Normal, targetOutline.Normal, 1e-3);
let interPts = sourceOutline.IntersectWith2(targetOutline, IntersectOption.ExtendNone, COMBINE_FUZZ);
if (interPts.length <= 1) {
let areaState = sourceOutline.Area > targetOutline.Area;
//反包含
if ((!areaState && fastCurveInCurve2(targetOutline, sourceOutline, interPts[0]?.pt)) || equalCurve(targetOutline, sourceOutline))
return [];
//包含
if ((areaState && fastCurveInCurve2(sourceOutline, targetOutline, interPts[0]?.pt)))
return [sourceOutline, targetOutline];
else //分离
return [sourceOutline];
}
//相交
let subtractList = [];
let sourceCus = sourceOutline.GetSplitCurves(interPts.map(r => r.thisParam));
let targetCus = targetOutline.GetSplitCurves(interPts.map(r => r.argParam));
for (let pl of sourceCus) {
let plMidParam = pl.MidParam;
let plDir = pl.GetFirstDeriv(plMidParam).normalize();
let index = targetCus.findIndex(cu => fastEqualCurve(cu, pl));
if (index !== -1) {
let cu = targetCus[index];
let cuMidParam = cu.MidParam;
let cuDir = cu.GetFirstDeriv(cuMidParam).normalize();
if (isEqualNormal === !equalv3(cuDir, plDir, 1e-3)) //不同向
subtractList.push(pl);
targetCus.splice(index, 1);
continue;
}
if (!fastCurveInCurve(targetOutline, pl))
subtractList.push(pl);
}
//源对象没有被破坏
let sourceNotBreak = subtractList.length === sourceCus.length;
for (let pl of targetCus)
if (fastCurveInCurve(sourceOutline, pl))
subtractList.push(pl);
if (sourceNotBreak && subtractList.length === sourceCus.length)
return [sourceOutline];
return subtractList;
}
GetSubtractListByMoreTargets(targets) {
let { holes, subtractList } = this.GetSubListWithCus(targets);
//纯网洞
if (subtractList.every(c => c.IsClose))
return {
holes: holes.map(h => Contour.CreateContour(h)),
outlines: Contour.GetAllContour(subtractList)
};
let regParse = new RegionParse(subtractList, 2);
let contours = [];
//分析封闭包围区域
const parseRoute = (routeSet) => {
for (let routes of routeSet) {
let cs = routes.map(r => r.curve);
let c = Contour.CreateContour(cs, false) ?? CreateContour2(cs);
if (c
&& !equalCurve(c.Curve, this.Curve)
&& targets.every(target => !equalCurve(c.Curve, target.Curve))
&& c.Area > 1e-3)
contours.push(c);
}
};
parseRoute(regParse.RegionsOutline);
parseRoute(regParse.RegionsInternal);
return {
holes: holes.map(h => Contour.CreateContour(h)),
outlines: contours
};
}
GetSubListWithCus(targets) {
let sourceOutline = this._Curve;
let subtractList = [];
let holes = [];
let intPars = [];
let curveIntParamsMap = new Map();
let outBox = sourceOutline.BoundingBox;
for (let con of targets) {
const targetOutline = con.Curve;
if (!IntersectBox2(outBox, targetOutline.BoundingBox))
continue;
let pts = sourceOutline.IntersectWith2(con.Curve, IntersectOption.ExtendNone, COMBINE_FUZZ);
if (pts.length <= 1) {
let areaState = sourceOutline.Area > targetOutline.Area;
//反包含
if ((!areaState && fastCurveInCurve2(targetOutline, sourceOutline, pts[0]?.pt)) || equalCurve(targetOutline, sourceOutline))
return { holes, subtractList };
//包含
if (areaState && fastCurveInCurve2(sourceOutline, targetOutline, pts[0]?.pt))
holes.push(targetOutline);
}
else {
intPars.push(...pts.map(r => r.thisParam));
curveIntParamsMap.set(targetOutline, pts.map(r => r.argParam));
}
}
let sourceSplitCurves = sourceOutline.GetSplitCurves(intPars);
let targetSplitCurves = [];
let targetSplitCurve_CurvesMap = new WeakMap(); //分裂后->原始曲线 映射
for (let [curve, intParams] of curveIntParamsMap) {
let splitCurves = curve.GetSplitCurves(intParams);
for (let splitCurve of splitCurves) {
targetSplitCurve_CurvesMap.set(splitCurve, curve);
targetSplitCurves.push(splitCurve);
}
}
for (let sourceSplitcu of sourceSplitCurves) {
let index = targetSplitCurves.findIndex(cu => fastEqualCurve(cu, sourceSplitcu, 0.05));
if (index !== -1) {
let targetSplitcu = targetSplitCurves[index];
let isEqualNormal = equalv3(sourceOutline.Normal, targetSplitCurve_CurvesMap.get(targetSplitcu).Normal, 1e-3);
if (isEqualNormal === !equalv3(sourceSplitcu.StartPoint, targetSplitcu.StartPoint, 0.05)) //不同向
{
subtractList.push(sourceSplitcu);
// TestDraw(sourceSplitcu.Clone(), 1);
// TestDraw(targetSplitcu.Clone(), 1);
}
// else
// TestDraw(sourceSplitcu.Clone(), 2);
targetSplitCurves.splice(index, 1);
continue;
}
if (targets.every(t => !fastCurveInCurve(t.Curve, sourceSplitcu)))
subtractList.push(sourceSplitcu);
}
//源对象没有被破坏
let sourceNotBreak = subtractList.length === sourceSplitCurves.length;
for (let pl of targetSplitCurves)
if (fastCurveInCurve(sourceOutline, pl))
subtractList.push(pl);
if (sourceNotBreak && subtractList.length === sourceSplitCurves.length)
return { subtractList: [sourceOutline], holes };
return { subtractList, holes };
}
/**
* 获得全部闭合曲线
* @若传入二维曲线数据,将默认子数组为闭合曲线段
*/
static GetAllContour(cus) {
if (cus.length === 0)
return [];
let cuGroups;
if (Array.isArray(cus[0]))
cuGroups = cus;
else
cuGroups = curveLinkGroup(cus, -Math.log10(COMBINE_FUZZ));
let contours = [];
for (let g of cuGroups)
contours.push(Contour.CreateContour(g, false));
return contours.filter(c => c !== undefined && !equaln$1(c.Area, 0, 1e-6));
}
/**
* 合并曲线组成为多段线
* @param cus 曲线组
* @param [needLink=true] 需要解析成首尾连接状态
* @returns 单一曲线,如果返回超过1个,其他的将被遗弃.
*/
static Combine(cus, needLink = true, tolerance = 1e-3) {
if (cus.length === 0)
return undefined;
let groups = needLink ? curveLinkGroup(cus, -Math.log10(tolerance)) : [cus];
for (let g of groups) {
if (g.length === 1)
return g[0].Clone();
else {
if (cache$1.has(g))
return cache$1.get(g);
let gclone = g.map(c => c.Clone());
arrayRemoveDuplicateBySort(gclone, (cu1, cu2) => cu1.Join(cu2, false, tolerance) === exports.Status.True);
if (gclone.length > 1 && gclone[0].Join(arrayLast(gclone), false, tolerance))
gclone.pop();
let pl = exports.Polyline.Combine(gclone, tolerance);
cache$1.set(g, pl);
return pl;
}
}
}
get Shape() {
return this._Curve.Shape;
}
/**
* 判断是否完全包含曲线
* @param smallCurve 传入的这个曲线不能比本轮廓还大(这个需要自己优化?)
* @returns
*/
ContainerCurve(smallCurve, isAreaCheckd = false, ipts = undefined) {
if (!isAreaCheckd && this.Area < smallCurve.Area)
return false;
return CurveContainerCurve(this._Curve, smallCurve, ipts);
}
Equal(tar) {
return equalCurve(this._Curve, tar._Curve);
}
}
/**
* 对于轮廓切割后的曲线判断相同,使用这个函数进行快速判断
*/
function fastEqualCurve(c1, c2, tolerance = 5e-3) {
let sp1 = c1.StartPoint;
let ep1 = c1.EndPoint;
let sp2 = c2.StartPoint;
let ep2 = c2.EndPoint;
if (!((equalv3(sp1, sp2, tolerance) && equalv3(ep1, ep2, tolerance))
|| (equalv3(sp1, ep2, tolerance) && equalv3(ep1, sp2, tolerance))))
return false;
return equalv3(c1.Midpoint, c2.Midpoint, tolerance);
}
/**
* 对于双多段线互相切割后的结果(或者交点个数为0),快速判断曲线是否在另一条曲线内部
* @param bigCurve
* @param smallCurve
* @returns
*/
function fastCurveInCurve(bigCurve, smallCurve) {
return bigCurve.PtInCurve(smallCurve.Midpoint);
}
//当交点小于等于1时
function fastCurveInCurve2(bigCurve, smallCurve, iPt) {
if (iPt) //提高准确性,避免使用交点去判断 导致的错误
{
let sp = smallCurve.StartPoint;
if (equalv3(sp, iPt, 1e-3))
return bigCurve.PtInCurve(smallCurve.Midpoint);
else
return bigCurve.PtInCurve(smallCurve.StartPoint);
}
else
return bigCurve.PtInCurve(smallCurve.Midpoint) || bigCurve.PtInCurve(smallCurve.StartPoint);
}
//大曲线是否完全包含小曲线(或者重合)
function CurveContainerCurve(bigCurve, smallCurve, ipts = undefined, fuzz = COMBINE_FUZZ) {
if (!ipts)
ipts = bigCurve.IntersectWith2(smallCurve, IntersectOption.ExtendNone, fuzz);
if (ipts.length === 0)
return fastCurveInCurve(bigCurve, smallCurve);
else if (ipts.length === 1)
return fastCurveInCurve2(bigCurve, smallCurve, ipts[0].pt);
else
return isTargetCurInOrOnSourceCur(bigCurve, smallCurve);
}
class CurveTreeNode {
Create(curve, box) { return new this.constructor(curve, box); }
constructor(curve, box) {
this.curve = curve;
this.box = box || curve.BoundingBox;
}
TrimBy(contour, box) {
if (IntersectsBox(box, this.box)) {
if (this.children !== undefined) {
for (let c of this.children)
c.TrimBy(contour, box);
}
else {
if (contour.Curve instanceof exports.Circle && this.curve instanceof exports.Arc) {
if (equalv3(contour.Curve.Center, this.curve.Center)) {
if (contour.Curve.Radius > this.curve.Radius + 1e-4)
this.children = [];
return;
}
}
//交点参数列表
let iParams = this.curve.IntersectWith2(contour.Curve, IntersectOption.ExtendNone)
.map(p => p.thisParam);
//#region 交点参数优化,避免出现无意义长度的曲线被移除 CurveIsFine
let length = this.curve.Length;
if (!(this.curve instanceof exports.Circle))
iParams.push(0, 1);
iParams.sort((e1, e2) => e1 - e2);
if (this.curve instanceof exports.Arc || this.curve instanceof exports.Circle) {
let allAngle = this.curve instanceof exports.Circle ? Math.PI * 2 : this.curve.AllAngle;
arrayRemoveDuplicateBySort(iParams, (p1, p2) => {
return allAngle * (p2 - p1) < ARC_MIN_ALLANGLE || length * (p2 - p1) < CURVE_MIN_LENGTH;
});
}
else {
arrayRemoveDuplicateBySort(iParams, (p1, p2) => {
return length * (p2 - p1) < CURVE_MIN_LENGTH;
});
}
if (!(this.curve instanceof exports.Circle)) {
iParams.shift(); //remove 0
iParams.pop(); //remove 1 or 0.9999
}
//#endregion
let cus = this.curve.GetSplitCurves(iParams);
if (cus.length === 0) {
let p = this.curve.GetPointAtParam(0.5);
if (box.containsPoint(p) && (contour.Curve.PtInCurve(p) && !contour.Curve.PtOnCurve(p)))
this.children = [];
}
else {
this.children = [];
for (let c of cus) {
let p = c.GetPointAtParam(0.5);
if (CurveIsFine(c) && (!(box.containsPoint(p) && contour.Curve.PtInCurve(p)) || contour.Curve.PtOnCurve(p)))
this.children.push(this.Create(c));
}
if (this.children.length === cus.length)
this.children = undefined;
}
}
}
}
get Nodes() {
if (!this.children)
return [this];
else {
let cus = [];
for (let c of this.children)
cus.push(...c.Nodes);
return cus;
}
}
}
var PolylineJoinType;
(function (PolylineJoinType) {
PolylineJoinType[PolylineJoinType["Square"] = 0] = "Square";
PolylineJoinType[PolylineJoinType["Round"] = 1] = "Round";
})(PolylineJoinType || (PolylineJoinType = {}));
class OffsetPolyline {
/**
*
* @param _Polyline
* @param _OffsetDist
* @param [_ToolPath=false] 走刀模式(在这个模式下,我们会进行圆弧过渡(或者直线过渡)避免尖角过大)
* @param [_OffsetDistSq=(_OffsetDist ** 2) * 2.1] 允许的最大尖角长度 默认值差不多是矩形的尖角大一点
* @param [_JoinType=PolylineJoinType.Round] 尖角的处理方式,默认是圆弧过渡,可以切换成直线过渡
*/
constructor(_Polyline, _OffsetDist, _ToolPath = false, _OffsetDistSq = (_OffsetDist ** 2) * 2.1, //对直角走刀不进行圆弧过度
_JoinType = PolylineJoinType.Round //仅在走刀路径时生效
) {
this._Polyline = _Polyline;
this._OffsetDist = _OffsetDist;
this._ToolPath = _ToolPath;
this._OffsetDistSq = _OffsetDistSq;
this._JoinType = _JoinType;
this._IsTopoOffset = false; //局部偏移,允许特殊延伸,参考测试用例
}
Do() {
this._OffsetDistSign = Math.sign(this._OffsetDist);
this._TrimPolylineContours = [];
this._TrimCircleContours = [];
this._TrimArcContours = [];
this._RetCurves = [];
this._CurveTreeNodes = [];
this.InitSubCurves();
if (this._SubCurves.length === 0)
return this._RetCurves;
this.GeneralCirclesAndVertexs();
this.OffsetSubCurves();
this.LinkSubCurves();
if (this._SubOffsetedCurves.length === 0) {
this._SubOffsetedCurves.push({ curve: this._Circles[0], index: 0, paddingCurve: this._Circles.slice(1) });
this._TrimPolylineContours.push(...this._Circles.map(c => Contour.CreateContour(c, false)), ...this._SubCurves.map(c => Contour.CreateContour([c, new exports.Line(c.StartPoint, c.EndPoint)], false)));
}
else
this.GeneralTrimContours();
// for (let c of this._SubOffsetedCurves)
// {
// TestDraw(c.curve.Clone(), 1);
// if (c.paddingCurve)
// for (let cc of c.paddingCurve)
// TestDraw(cc.Clone(), 2);
// }
// for (let c of this._TrimPolylineContours)
// {
// TestDraw(c.Curve.Clone(), 3);
// }
this.TrimByContours();
this.FilterInvalidCurve();
this.JoinCollinear();
this.LinkResultPolyline();
this.RepairResultPolylineClosemark();
return this._RetCurves;
}
InitSubCurves() {
this._CacheOCS = this._Polyline.OCS;
this._IsClose = this._Polyline.IsClose;
this._Polyline.OCS = IdentityMtx4;
this._SubCurves = this._Polyline.Explode().filter(c => c.Length > 1e-4); //TODO: 简化这个 并且生成圆 顶点 圆
this._Polyline.OCS = this._CacheOCS;
return this;
}
GeneralCirclesAndVertexs() {
this._Vertexs = this._SubCurves.map(c => c.StartPoint);
let lastCu = arrayLast(this._SubCurves);
if (!equalv3(lastCu.EndPoint, this._Vertexs[0], 1e-3))
this._Vertexs.push(lastCu.EndPoint);
let radius = Math.abs(this._OffsetDist);
this._Circles = this._Vertexs.map(p => new exports.Circle(p, radius));
}
OffsetSubCurves() {
this._SubOffsetedCurves = [];
for (let index = 0; index < this._SubCurves.length; index++) {
let curveOld = this._SubCurves[index];
if (curveOld.Length > 1e-6) {
let curve = curveOld.GetOffsetCurves(this._OffsetDist)[0];
if (curve)
this._SubOffsetedCurves.push({ curve, index });
else
this._TrimArcContours.push(Contour.CreateContour([curveOld, new exports.Line(curveOld.StartPoint, curveOld.EndPoint)], false));
}
}
}
//连接(延伸)曲线,或者补(圆弧,直线)
LinkSubCurves() {
let count = this._SubOffsetedCurves.length;
if (!this._IsClose)
count--;
for (let i = 0; i < count; i++) {
let curveResNow = this._SubOffsetedCurves[i];
let iNext = FixIndex$1(i + 1, this._SubOffsetedCurves);
let curveResNext = this._SubOffsetedCurves[iNext];
let curveNow = curveResNow.curve;
let curveNext = curveResNext.curve;
let isNeighbor = FixIndex$1(curveResNow.index + 1, this._SubCurves) === curveResNext.index;
if (isNeighbor) {
let sp = curveNow.EndPoint;
let ep = curveNext.StartPoint;
//直连
if (equalv3(sp, ep, 1e-3))
continue;
let iPtsP = curveNow.IntersectWith2(curveNext, IntersectOption.ExtendBoth, 1e-6);
let iPts = iPtsP.map(p => p.pt);
let tPts = iPtsP.filter(p => curveNow.ParamOnCurve(p.thisParam) && curveNext.ParamOnCurve(p.argParam)).map(p => p.pt);
let code = EntityEncode2(curveNow, curveNext);
let tp;
if (code === 1) {
if (this._IsTopoOffset && tPts.length === 0 && curveResNow.dist !== curveResNext.dist)
tPts = iPts;
if (tPts.length > 0) //不走刀或者有真交点 this._ToolPath === false ||
tp = iPts[0];
else {
if (iPts.length > 0 && iPtsP[0].thisParam > 1) {
let refP = this._Vertexs[curveResNext.index];
let distSq = iPts[0].distanceToSquared(refP);
if (this._ToolPath && distSq > this._OffsetDistSq) {
if (this._JoinType === PolylineJoinType.Round)
curveResNow.paddingCurve = [this.CreateArc(refP, sp, ep)];
else
curveResNow.paddingCurve = [this.CreateSquare(refP, curveResNow, curveResNext, code)]; //补直线
this._TrimCircleContours.push(this._Circles[curveResNext.index]);
}
else
tp = iPts[0];
}
// else
// curveResNow.paddingCurve = [new Line(sp, ep)];
}
}
else //直线和圆弧 圆弧和圆弧
{
let refP = this._Vertexs[curveResNext.index];
//在局部偏移中,当偏移距离不一致时,我们总是倾向于直接连接
if (this._IsTopoOffset && tPts.length === 0 && curveResNow.dist !== curveResNext.dist) {
if (iPts.length)
tPts = iPts;
else {
curveResNow.paddingCurve = [new exports.Line(sp, ep)]; //补直线
continue;
}
}
if (tPts.length > 0) //ipts = 1 or ipts = 2
tp = SelectNearP(tPts, refP);
else //补圆弧 或者尝试连接
{
if (iPts.length > 0 && !this._ToolPath && this.IsSharpCorner(curveResNow, curveResNext)) //非加工刀路 并且尖角化(谷)时
{
//设置新的连接点,并且备份旧点
let oldp;
if (curveResNow.sp) {
oldp = curveNow.StartPoint;
curveNow.StartPoint = curveResNow.sp;
}
let oldp2;
if (curveResNext.ep) {
oldp2 = curveNext.EndPoint;
curveNext.EndPoint = curveResNext.ep;
}
let p;
if (code === 2 && iPts.length === 2) {
let c = curveNow;
let minArc = new exports.Arc(c.Center, c.Radius, c.EndAngle, 0, c.IsClockWise);
let p1 = iPts[0];
let a1 = minArc.GetAngleAtPoint(p1);
let anAll1 = c.ParamOnCurve(c.GetParamAtAngle(a1)) ? Infinity : minArc.ComputeAnlge(a1);
let p2 = iPts[1];
let a2 = minArc.GetAngleAtPoint(p2);
let anAll2 = c.ParamOnCurve(c.GetParamAtAngle(a2)) ? Infinity : minArc.ComputeAnlge(a2);
if (anAll2 < anAll1)
p = p2;
else
p = p1;
}
else
p = SelectNearP(iPts, refP);
let onPre;
let param = curveNow.GetParamAtPoint2(p);
if (curveNow instanceof exports.Line)
onPre = param > 1;
else
onPre = param < 0 || param > 1;
let onNext = false;
if (onPre) {
let param2 = curveNext.GetParamAtPoint2(p);
if (curveNext instanceof exports.Line)
onNext = param2 < 0;
else
onNext = param2 < 0 || param2 > 1;
}
if (curveResNow.sp)
curveNow.StartPoint = oldp;
if (curveResNext.ep)
curveNext.EndPoint = oldp2;
if (onPre && onNext)
tp = p;
else
curveResNow.paddingCurve = [this.CreateArc(refP, sp, ep)]; //补圆弧
}
else {
if (this._JoinType === PolylineJoinType.Round)
curveResNow.paddingCurve = [this.CreateArc(refP, sp, ep)]; //补圆弧
else {
curveResNow.paddingCurve = [this.CreateSquare(refP, curveResNow, curveResNext, code)]; //补直线
}
}
let circle = this._Circles[curveResNext.index];
if (circle)
this._TrimCircleContours.push(circle); //因为局部偏移可能未提供圆
}
}
if (tp) {
curveResNow.ep = tp;
curveResNext.sp = tp;
curveResNow.nextCurve = curveNext;
curveResNext.preCurve = curveNow;
}
}
else {
let padCirs = [];
for (let s = FixIndex$1(curveResNow.index + 1, this._Circles);; s = FixIndex$1(s + 1, this._Circles)) {
let circle = this._Circles[s];
if (circle) //因为局部偏移可能未提供圆
{
this._TrimCircleContours.push(circle);
padCirs.push(circle);
}
if (s === curveResNext.index)
break;
}
curveResNow.paddingCurve = padCirs;
}
}
}
/**
* 判断两曲线是否尖角化
* 在
* 1.直线和圆弧,圆弧和圆弧
* 2.有交点,无真交点时
* 必须在正确的方向做出延伸动作,所以必须在尖角的情况下延伸,偏移的结果就像一个谷字去掉一个口的结果,上面的人就是偏移后的结果.
* 如果是谷,则允许连接 #I7WKKG
*/
IsSharpCorner(curveResNow, curveResNext) {
let v1 = this._SubCurves[curveResNow.index].GetFirstDeriv(1);
let v2 = this._SubCurves[curveResNext.index].GetFirstDeriv(0);
v1.cross(v2);
return Math.sign(v1.z) === this._OffsetDistSign;
}
GeneralTrimContours() {
for (let d of this._SubOffsetedCurves) {
let cu2 = d.curve;
if (d.sp && d.ep) {
let param1 = cu2.GetParamAtPoint(d.sp);
let param2 = cu2.GetParamAtPoint(d.ep);
if (cu2.ParamOnCurve(param1) && cu2.ParamOnCurve(param2) && param1 > param2)
[d.sp, d.ep] = [d.ep, d.sp];
}
if (d.sp)
cu2.StartPoint = d.sp;
if (d.ep)
cu2.EndPoint = d.ep;
//这是极端情况,圆弧被压缩成0长度圆弧,本质是空圆弧(我们会在下面判断它)(因为精度的问题)
//因为精度的问题,这种0圆心角的圆弧会被当成全圆,但是偏移算法中,应该不可能出现全圆弧的圆弧,所以我们压扁它
if (cu2 instanceof exports.Arc
&& equaln$1(cu2.StartAngle, cu2.EndAngle, 1e-6)
// && !equaln((<Arc>this._SubCurves[d.index]).AllAngle, Math.PI * 2, 1e-3) 应该不会出现
) {
if (cu2.IsClockWise)
cu2.StartAngle = cu2.EndAngle + 1e-6;
else
cu2.EndAngle = cu2.StartAngle + 1e-6;
}
}
for (let d of this._SubOffsetedCurves) {
let cu1 = this._SubCurves[d.index];
let cu2 = d.curve;
let [p1, p2, p3, p4] = [cu1.StartPoint, cu2.StartPoint, cu1.EndPoint, cu2.EndPoint];
let l1 = new exports.Line(p1, p2);
let l2 = new exports.Line(p3, p4);
let ipts = l1.IntersectWith(l2, IntersectOption.ExtendNone, 1e-8);
if (ipts.length > 0) {
let p = ipts[0];
l1.EndPoint = p;
l2.EndPoint = p;
let cus = [cu1, l1, l2];
let contour = Contour.CreateContour(cus, false);
if (contour) {
this._TrimPolylineContours.push(contour);
continue;
}
else {
console.error("未预料到的错误,构建轮廓失败" + this._OffsetDist);
}
}
//真理1:针脚线不可能同时被两个圆弧所切割
let l1Intact = true;
let l2Intact = true;
if (cu2 instanceof exports.Arc) {
if (Math.sign(cu2.Bul) !== this._OffsetDistSign) {
let ipts1 = cu2.IntersectWith(l1, IntersectOption.ExtendNone);
let ipts2 = cu2.IntersectWith(l2, IntersectOption.ExtendNone);
let sp;
let ep;
if (ipts1.length === 2)
sp = SelectNearP(ipts1, p1);
if (ipts2.length === 2)
ep = SelectNearP(ipts2, p3);
if (sp || ep)
cu2 = cu2.Clone();
if (sp) {
l1.EndPoint = sp;
cu2.StartPoint = sp;
l1Intact = false;
}
if (ep) {
l2.EndPoint = ep;
cu2.EndPoint = ep;
l2Intact = false;
}
}
}
let l1PadArc;
let l2PadArc;
//真理2:隔壁的圆弧不可能破坏当前的圆弧,只能破坏当前的针脚
if (l1Intact && d.preCurve && d.preCurve instanceof exports.Arc) {
let a = d.preCurve;
if (Math.sign(a.Bul) !== this._OffsetDistSign && a.AllAngle > 1e-6) {
let ipts = a.IntersectWith(l1, IntersectOption.ExtendNone);
if (ipts.length === 2) {
let sp = SelectNearP(ipts, p1);
l1.EndPoint = sp;
l1PadArc = a.Clone();
l1PadArc.StartPoint = sp;
}
}
}
if (l2Intact && d.nextCurve && d.nextCurve instanceof exports.Arc) {
let a = d.nextCurve;
if (Math.sign(a.Bul) !== this._OffsetDistSign && a.AllAngle > 1e-6) {
let ipts = a.IntersectWith(l2, IntersectOption.ExtendNone);
if (ipts.length === 2) {
let ep = SelectNearP(ipts, p3);
l2.EndPoint = ep;
l2PadArc = a.Clone();
l2PadArc.EndPoint = ep;
}
}
}
let pl = new exports.Polyline();
let cus = [cu1, l1];
if (l1PadArc)
cus.push(l1PadArc);
cus.push(cu2, l2);
if (l2PadArc)
cus.push(l2PadArc);
for (let c of cus)
pl.Join(c);
let contour = Contour.CreateContour(pl, false);
if (contour)
this._TrimPolylineContours.push(contour);
else
console.error("未预料到的错误,构建轮廓失败" + this._OffsetDist);
}
if (!this._IsClose) {
if (this._TrimCircleContours[0] !== this._Circles[0])
this._TrimCircleContours.push(this._Circles[0]);
let lastTrimCircle = arrayLast(this._TrimCircleContours);
let lastCircle = arrayLast(this._Circles);
if (lastTrimCircle !== lastCircle)
this._TrimCircleContours.push(lastCircle);
if (this._SubOffsetedCurves[0].index !== 0)
this._TrimCircleContours.push(this._Circles[this._SubOffsetedCurves[0].index]);
let lastIndex = this._Circles.length - 1;
let lastD = arrayLast(this._SubOffsetedCurves);
if (lastIndex !== lastD.index)
this._TrimCircleContours.push(this._Circles[lastD.index + 1]);
}
this._TrimPolylineContours.push(...this._TrimCircleContours.map(c => Contour.CreateContour(c, false)), ...this._TrimArcContours);
}
// 通过构建的轮廓对偏移曲线进行裁剪
TrimByContours() {
for (let d of this._SubOffsetedCurves) {
let c = d.curve;
if (CurveIsFine(c))
this._CurveTreeNodes.push(new CurveTreeNode(c));
if (d.paddingCurve)
this._CurveTreeNodes.push(...d.paddingCurve.map(c => new CurveTreeNode(c)));
}
for (let i = 0; i < this._TrimPolylineContours.length; i++) {
let c = this._TrimPolylineContours[i];
let cbox = c.BoundingBox;
for (let curveNode of this._CurveTreeNodes)
curveNode.TrimBy(c, cbox);
}
}
//过滤方向相反和0长度线
FilterInvalidCurve() {
this._CurveTrimedTreeNodes = [];
for (let n of this._CurveTreeNodes) {
let ns = n.Nodes;
for (let sn of ns) {
if (this.CheckPointDir(sn.curve.GetPointAtParam(0.5)))
this._CurveTrimedTreeNodes.push(sn);
}
}
}
//合并共线
JoinCollinear() {
for (let i = 0; i < this._CurveTrimedTreeNodes.length; i++) {
let n = this._CurveTrimedTreeNodes[i];
if (n.used)
continue;
let sp = n.curve.StartPoint;
for (let j = i + 1; j < this._CurveTrimedTreeNodes.length; j++) {
let n2 = this._CurveTrimedTreeNodes[j];
if (n2.used)
continue;
let status = n.curve.Join(n2.curve);
if (status === exports.Status.ConverToCircle) {
n.used = true;
n2.used = true;
let circle = new exports.Circle(n.curve.Center, n.curve.Radius);
n.curve = circle;
this._RetCurves.push(ConverCircleToPolyline$1(circle).ApplyMatrix(this._CacheOCS));
}
else if (status === exports.Status.True) {
if (equalv3(sp, n.curve.StartPoint))
n2.used = true;
else {
n.used = true;
n2.curve = n.curve;
break;
}
}
}
}
}
//连接结果曲线,返回最终多段线
LinkResultPolyline() {
let used = new Set();
let cuMap = new CurveMap(1);
for (let n of this._CurveTrimedTreeNodes) {
if (!n.used)
cuMap.AddCurveToMap(n.curve);
}
let preP;
let searchNext = (s, pl, isJoinEnd = true) => {
let minDist = Infinity;
let minR;
for (let r of s.routes) {
if (used.has(r.curve))
continue;
if (preP) {
let d = r.s.distanceToSquared(preP);
if (d < minDist) {
minR = r;
minDist = d;
}
}
else {
minR = r;
minDist = 0;
break;
}
}
if (minR && Math.sqrt(minDist) < 0.085) {
used.add(minR.curve);
preP = minR.e;
// let status = pl.Join(minR.curve, false, 8e-2);
// if (status !== Status.True)
// console.warn("连接失败");
//#region fast join (为了避免Polyline.Join 导致的顺序错误)
let cu = minR.curve;
//bul
let bul = 0;
if (cu instanceof exports.Arc) {
bul = cu.Bul;
if (minR.isReverse)
bul *= -1;
if (!isJoinEnd)
bul *= -1;
}
if (pl.LineData.length === 0) {
pl.LineData.push({
pt: AsVector2(minR.s),
bul: bul
});
}
if (isJoinEnd) {
pl.LineData[pl.LineData.length - 1].bul = bul;
pl.LineData.push({
pt: AsVector2(minR.e),
bul: 0
});
}
else {
pl.LineData.unshift({
pt: AsVector2(minR.e),
bul
});
}
//#endregion
return minR.to;
}
};
for (let s of cuMap.Stands) {
preP = undefined;
let pl = new exports.Polyline();
let ss = s;
while (ss && !pl.IsClose)
ss = searchNext(ss, pl, true);
ss = s;
preP = pl.StartPoint;
while (ss && !pl.IsClose)
ss = searchNext(ss, pl, false);
if (pl.NumberOfVertices > 1) {
//避免0长度的线
if (pl.NumberOfVertices === 2 && pl.Length < 1e-6)
continue;
let d = pl.LineData;
let ld = arrayLast(d);
if (equalv2(d[0].pt, ld.pt, 1e-2))
ld.pt.copy(d[0].pt);
this._RetCurves.push(pl.ApplyMatrix(this._CacheOCS));
}
}
}
RepairResultPolylineClosemark() {
if (!this._RetCurves.length)
return;
if (this._Polyline.CloseMark) {
if (!equalv2(this._Polyline.LineData[0].pt, arrayLast(this._Polyline.LineData).pt, 8e-2)) //缺省一个点
{
for (let pl of this._RetCurves) {
if (pl.IsClose && //封闭
equaln$1(arrayLast(pl.LineData).bul, 0, 1e-5) && //是直线
equalv2(pl.LineData[0].pt, arrayLast(pl.LineData).pt, 8e-2)) //首尾重复(一般已经是了)
{
pl.LineData.pop(); //移除最后一点
pl.CloseMark = true;
}
}
}
else {
for (let pl of this._RetCurves) {
if (pl.IsClose)
pl.CloseMark = true;
}
}
}
else if (this._IsClose) {
for (let pl of this._RetCurves) {
let firstP = pl.LineData[0].pt;
let lastP = arrayLast(pl.LineData).pt;
if (equalv2(firstP, lastP, 8e-2))
lastP.copy(firstP);
}
}
}
CheckPointDir(pt) {
return this.GetPointAtCurveDir(pt) === this._OffsetDistSign;
}
GetPointAtCurveDir(pt) {
let minIndex = Infinity;
let minDist = Infinity;
let minCp;
for (let i = 0; i < this._SubCurves.length; i++) {
let c = this._SubCurves[i];
let cp = c.GetClosestPointTo(pt, false);
if (equalv3(cp, pt, 1e-5))
return 0;
let dist = cp.distanceToSquared(pt);
if (dist < minDist) {
minDist = dist;
minIndex = i;
minCp = cp;
}
}
let c = this._SubCurves[minIndex];
let param = c.GetParamAtPoint(minCp);
if (equaln$1(param, 0) && ((minIndex === 0) ? this._IsClose : true)) {
let preIndex = FixIndex$1(minIndex - 1, this._SubCurves);
let preCurve = this._SubCurves[preIndex];
if (!equalv3(c.GetFirstDeriv(0).normalize(), preCurve.GetFirstDeriv(1).normalize())) {
let p = c.StartPoint;
let l1 = c.Length;
let l2 = preCurve.Length;
let minLength = Math.min(l1, l2) * 0.2;
let nextP;
let preP;
if (c instanceof exports.Arc)
nextP = c.GetPointAtDistance(minLength);
else
nextP = c.EndPoint;
if (preCurve instanceof exports.Arc)
preP = preCurve.GetPointAtDistance(l2 - minLength);
else
preP = preCurve.StartPoint;
let arc = new exports.Arc(p, 1, angle(preP.sub(p)), angle(nextP.sub(p)));
let dir = arc.PtOnCurve3(pt) ? -1 : 1;
return dir;
}
}
else if (equaln$1(param, 1) && ((minIndex === this._SubCurves.length - 1) ? this._IsClose : true)) {
let nextIndex = FixIndex$1(minIndex + 1, this._SubCurves);
let nextCurve = this._SubCurves[nextIndex];
if (!equalv3(c.GetFirstDeriv(1).normalize(), nextCurve.GetFirstDeriv(0).normalize())) {
let p = c.EndPoint;
let l1 = c.Length;
let l2 = nextCurve.Length;
let minLength = Math.min(l1, l2) * 0.2;
let nextP;
let preP;
if (c instanceof exports.Arc)
preP = c.GetPointAtDistance(l1 - minLength);
else
preP = c.StartPoint;
if (nextCurve instanceof exports.Arc)
nextP = nextCurve.GetPointAtDistance(minLength);
else
nextP = nextCurve.EndPoint;
let arc = new exports.Arc(p, 1, angle(preP.sub(p)), angle(nextP.sub(p)));
let dir = arc.PtOnCurve3(pt) ? -1 : 1;
return dir;
}
}
let dri = c.GetFirstDeriv(param);
let cross = dri.cross(pt.clone().sub(minCp));
return -Math.sign(cross.z);
}
CreateArc(center, startP, endP) {
let sa = angle(startP.clone().sub(center));
let ea = endP ? angle(endP.clone().sub(center)) : sa;
let arc = new exports.Arc(center, Math.abs(this._OffsetDist), sa, ea, this._OffsetDist < 0);
return arc;
}
CreateSquare(center, curveNow, curveNext, entTypeCode) {
const arc = this.CreateArc(center, curveNow.curve.EndPoint, curveNext.curve.StartPoint);
const centerPoint = arc.GetPointAtParam(0.5);
const tangentLine = new exports.Line(centerPoint, arc.GetFirstDeriv(0.5).add(centerPoint)); //切线
let ep, sp;
if (entTypeCode === 1) {
ep = tangentLine.IntersectWith(curveNow.curve, IntersectOption.ExtendBoth)[0]; //第一条线新的终点坐标
sp = centerPoint.multiplyScalar(2).sub(ep);
}
else // if (entTypeCode === 0)//全圆弧 直线和圆弧
{
ep = SelectNearP(tangentLine.IntersectWith(curveNow.curve, IntersectOption.ExtendBoth), center); //第一条线新的终点坐标
sp = SelectNearP(tangentLine.IntersectWith(curveNext.curve, IntersectOption.ExtendBoth), center);
}
curveNow.ep = ep;
curveNext.sp = sp;
return new exports.Line(ep, sp);
}
}
function EntityEncode(c) {
if (c instanceof exports.Line)
return 1;
else
return 2;
}
function EntityEncode2(c1, c2) {
return EntityEncode(c1) & EntityEncode(c2);
}
const CURVE_MIN_LENGTH = 5e-5;
const ARC_MIN_ALLANGLE = 2e-6;
//表示这个是一个正常的曲线,不是0长度的线,也不是0长度的圆弧
function CurveIsFine(curve) {
if (curve instanceof exports.Arc && curve.AllAngle < ARC_MIN_ALLANGLE)
return false;
return curve.Length > CURVE_MIN_LENGTH;
}
/**
* 判断点在多段线内外(如果点在线上 则返回false)
* @param pl 多段线
* @param pt 点
* @returns 点在多段线内部
*/
function IsPointInPolyLine(pl, pt) {
let crossings = 0;
let insLine = new exports.Line(pt, pt.clone().add(new three.Vector3(0, 10, 0)));
for (let i = 0; i < pl.EndParam; i++) {
if (equaln$1(pl.GetBulgeAt(i), 0, BUL_IS_LINE_FUZZ)) //直线
{
let sp = pl.GetPointAtParam(i);
let ep = pl.GetPointAtParam(i + 1);
if (equalv2(sp, pt, 1e-5) || equalv2(ep, pt, 1e-5)) //在起点或者终点
return false;
//点位于线上面
if (pt.y > Math.max(sp.y, ep.y))
continue;
//线垂直Y轴
let derX = ep.x - sp.x;
if (equaln$1(derX, 0, 5e-6)) {
if (equaln$1(pt.x, ep.x, 1e-5)
&& (pt.y > Math.min(sp.y, ep.y) - 1e-5 && pt.y < Math.max(sp.y, ep.y) + 1e-5))
return false; //点在线上
continue;
}
//起点
if (equaln$1(sp.x, pt.x, 5e-6)) {
if (sp.y > pt.y && derX < 0)
crossings++;
continue;
}
//终点
if (equaln$1(ep.x, pt.x, 5e-6)) {
if (ep.y > pt.y && derX > 0)
crossings++;
continue;
}
//快速求交,只验证有没有交点
let [x1, x2] = sp.x > ep.x ? [ep.x, sp.x] : [sp.x, ep.x];
if (pt.x > x1 && pt.x < x2) {
let derY = ep.y - sp.y;
let k = derY / derX;
let iptY = (pt.x - sp.x) * k + sp.y;
if (equaln$1(iptY, pt.y, 1e-5)) //点在线上 返回false
return false;
if (iptY > pt.y)
crossings++;
}
}
else //圆弧
{
let arc = pl.GetCurveAtIndex(i);
let sp = arc.StartPoint;
let ep = arc.EndPoint;
if (equalv2(sp, pt, 1e-5) || equalv2(ep, pt, 1e-5)) //在起点或者终点
return false;
let center = arc.Center;
//如果相切
if (equaln$1(Math.abs(pt.x - center.x), arc.Radius)) {
//点在线上
if (equaln$1(pt.y, center.y) && arc.ParamOnCurve(arc.GetParamAtPoint2(pt)))
return false;
//当点和起点或者终点和点相切时
if (equaln$1(sp.x, pt.x) && sp.y > pt.y) {
if (ep.x - sp.x < -1e-5)
crossings++;
}
else if (equaln$1(ep.x, pt.x) && ep.y > pt.y) {
if (ep.x - sp.x > 1e-5)
crossings++;
}
continue;
}
if (equaln$1(sp.x, pt.x) && sp.y > pt.y) {
let der = arc.GetFirstDeriv(0).normalize();
if (der.x < -1e-5)
crossings++;
}
if (equaln$1(ep.x, pt.x) && ep.y > pt.y) {
let der = arc.GetFirstDeriv(1).normalize();
if (der.x > 1e-5)
crossings++;
}
for (let pti of arc.IntersectWith(insLine, IntersectOption.ExtendArg)) {
if (equalv2(pti, pt, 1e-5)) //点在线上 返回false
return false;
//交点在点下 交点在起点? 交点在终点?
if (pti.y + 1e-5 < pt.y || equalv2(sp, pti, 1e-5) || equalv2(ep, pti, 1e-5))
continue;
crossings++;
}
}
}
return (crossings % 2) === 1;
}
var Polyline_1;
const BUL_IS_LINE_FUZZ = 1e-5;
exports.Polyline = Polyline_1 = class Polyline extends Curve {
constructor(_LineData = []) {
super();
this._LineData = _LineData;
this._ClosedMark = false;
}
UpdateOCSTo(m) {
this.WriteAllObjectRecord();
let p = new three.Vector3().setFromMatrixPosition(m);
p.applyMatrix4(this.OCSInv);
if (equaln$1(p.z, 0)) {
let dir = Math.sign(this.Area2);
let tm = matrixAlignCoordSys(this.OCSNoClone, m);
for (let p of this._LineData) {
let p3 = AsVector3(p.pt);
p3.applyMatrix4(tm);
p.pt.set(p3.x, p3.y);
}
this.OCS = m;
let newDir = Math.sign(this.Area2);
if (dir !== newDir)
for (let p of this._LineData)
p.bul *= -1;
}
}
/**
* 原地翻转,仅改变法向量
*/
Flip() {
this.WriteAllObjectRecord();
let x = new three.Vector3();
let y = new three.Vector3();
let z = new three.Vector3();
this._Matrix.extractBasis(x, y, z);
z.negate();
y.crossVectors(z, x);
let p = this.Position;
this._Matrix.makeBasis(x, y, z).setPosition(p);
for (let d of this._LineData) {
d.pt.y *= -1;
d.bul *= -1;
}
this.Update();
return this;
}
//翻转曲线,首尾调换
Reverse() {
if (this._LineData.length === 0)
return this;
this.WriteAllObjectRecord();
let pts = [];
let buls = [];
for (let data of this._LineData) {
pts.push(data.pt);
buls.push(-data.bul);
}
let lastBul = buls.pop();
buls.reverse();
buls.push(lastBul);
pts.reverse();
if (this._ClosedMark && !equalv2(pts[0], arrayLast(pts))) {
pts.unshift(pts.pop());
buls.unshift(buls.pop());
}
for (let i = 0; i < pts.length; i++) {
let d = this._LineData[i];
d.pt = pts[i];
d.bul = buls[i];
}
return this;
}
set LineData(data) {
this.WriteAllObjectRecord();
this._LineData = data;
this.Update();
}
get LineData() {
return this._LineData;
}
get NumberOfVertices() {
return this._LineData.length;
}
/**
* 在指定位置插入点.
* 例如:
* pl.AddVertexAt(pl.NumberOfVertices,p);//在末尾插入一个点
*
* @param {number} index 索引位置
* @param {Vector2} pt 点
* @returns {this}
* @memberof Polyline
*/
AddVertexAt(index, pt) {
this.WriteAllObjectRecord();
let pts;
if (Array.isArray(pt)) {
pts = pt.map(p => {
return {
pt: p.clone(),
bul: 0
};
});
}
else
pts = [{ pt: pt.clone(), bul: 0 }];
this._LineData.splice(index, 0, ...pts);
this.Update();
return this;
}
RemoveVertexAt(index) {
if (index < this._LineData.length) {
this.WriteAllObjectRecord();
this._LineData.splice(index, 1);
this.Update();
}
return this;
}
RemoveVertexIn(from, to) {
if (from + 1 < this._LineData.length && to > from) {
this.WriteAllObjectRecord();
this._LineData.splice(from + 1, to - from - 1);
this.Update();
}
return this;
}
/**
* 重设闭合多段线的起点
* @param index 起始index,如果index非整数,将用最接近的整数作为起始索引
*/
ResetStartPoint(index) {
if (!this.IsClose || index >= this.EndParam)
return false;
if (equalv2(this._LineData[0].pt, arrayLast(this._LineData).pt))
this._LineData.pop();
changeArrayStartIndex(this._LineData, Math.floor(index + 0.5));
this._LineData.push({
pt: this._LineData[0].pt.clone(),
bul: 0
});
return true;
}
GetPoint2dAt(index) {
if (index >= 0 && this._LineData.length > index)
return this._LineData[index].pt.clone();
}
/**
* 设置指定点的位置
*
* @param {number} index
* @param {Vector2} pt
* @memberof Polyline
*/
SetPointAt(index, pt) {
let d = this._LineData[index];
if (d) {
this.WriteAllObjectRecord();
d.pt.copy(pt);
this.Update();
}
return this;
}
ApplyScaleMatrix(m) {
this.WriteAllObjectRecord();
let inv = this.OCSInv;
for (let i = 0; i <= this.EndParam; i++) {
let p = this.GetPointAtParam(i);
p.applyMatrix4(m).applyMatrix4(inv);
this.SetPointAt(i, AsVector2(p));
}
return this;
}
ApplyMirrorMatrix(m) {
this.WriteAllObjectRecord();
let oldPts = this.GetStretchPoints();
reviseMirrorMatrix(this._Matrix);
let inv = this.OCSInv;
for (let i = 0; i < oldPts.length; i++) {
let newP = oldPts[i].applyMatrix4(inv);
let newBul = -this.GetBulgeAt(i);
this.SetPointAt(i, AsVector2(newP));
this.SetBulgeAt(i, newBul);
}
this.Reverse();
return this;
}
SetBulgeAt(index, bul) {
let d = this._LineData[index];
if (d) {
this.WriteAllObjectRecord();
d.bul = bul;
this.Update();
}
return this;
}
GetBulgeAt(index) {
return this._LineData[index].bul;
}
Rectangle(length, height) {
this.LineData = [
{ pt: new three.Vector2(), bul: 0 },
{ pt: new three.Vector2(length), bul: 0 },
{ pt: new three.Vector2(length, height), bul: 0 },
{ pt: new three.Vector2(0, height), bul: 0 }
];
this.CloseMark = true;
return this;
}
RectangleFrom2Pt(p1, p2) {
let box = new three.Box3();
box.setFromPoints([p2, p1].map((p) => p.clone().applyMatrix4(this.OCSInv)));
let px1 = AsVector2(box.min);
let px3 = AsVector2(box.max);
let px2 = new three.Vector2(px3.x, px1.y);
let px4 = new three.Vector2(px1.x, px3.y);
this.LineData = [
{ pt: px1, bul: 0 },
{ pt: px2, bul: 0 },
{ pt: px3, bul: 0 },
{ pt: px4, bul: 0 }
];
this.CloseMark = true;
return this;
}
FromPoints2d(pts) {
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() {
if (this._LineData.length > 0)
return AsVector3(this._LineData[0].pt).applyMatrix4(this.OCS);
return new three.Vector3();
}
set StartPoint(p) {
this.WriteAllObjectRecord();
p = p.clone().applyMatrix4(this.OCSInv);
if (this._LineData.length === 0)
this.AddVertexAt(0, AsVector2(p));
else if (this._LineData.length === 1)
this.SetPointAt(0, AsVector2(p));
else {
let bul = this.GetBulgeAt(0);
if (bul !== 0) {
let arc = this.GetCurveAtParam(0);
arc.StartPoint = p;
//前面线的凸度调整
this.SetBulgeAt(0, Math.tan(arc.AllAngle / 4) * Math.sign(bul));
}
this.SetPointAt(0, AsVector2(p));
}
}
get EndPoint() {
if (this._ClosedMark)
return this.StartPoint;
if (this._LineData.length > 0)
return AsVector3(this._LineData[this.EndParam].pt).applyMatrix4(this.OCS);
return new three.Vector3();
}
set EndPoint(p) {
if (this._LineData.length < 2 || this.CloseMark)
return;
this.WriteAllObjectRecord();
p = p.clone().applyMatrix4(this.OCSInv);
let bul = this.GetBulgeAt(this.EndParam - 1);
if (bul !== 0) {
let arc = this.GetCurveAtParam(this.EndParam - 1);
arc.ApplyMatrix(this.OCSInv);
arc.EndPoint = p;
//前面线的凸度调整
this.SetBulgeAt(this.EndParam - 1, Math.tan(arc.AllAngle / 4) * Math.sign(bul));
}
this.SetPointAt(this.EndParam, AsVector2(p));
}
get CurveCount() {
return this.EndParam;
}
get StartParam() {
return 0;
}
/**
* 表示最后一条曲线的终止参数,使用该参数可以直接遍历到多段线的所有子线段. for(i<EndParam)...
*/
get EndParam() {
if (this._LineData.length === 0)
return 0;
//闭合且起点不等于终点
if (this._ClosedMark &&
!equalv2(this._LineData[0].pt, arrayLast(this._LineData).pt))
return this._LineData.length;
return this._LineData.length - 1;
}
get Area2() {
if (this.EndParam < 2)
return 0;
let { pts, buls } = this.PtsBuls;
let area = 0;
for (let i = 0; i < pts.length - 1; i++) {
let startV = pts[i];
let endV = pts[i + 1];
let det = getDeterminantFor2V(startV, endV);
let bul = buls[i];
if (bul !== 0) {
let arc = new exports.Arc().ParseFromBul(startV, endV, bul);
let rSq = arc.Radius ** 2;
//弓形面积
let arcArea = rSq * arc.AllAngle * 0.5 - 0.5 * rSq * Math.sin(arc.AllAngle);
//我们可以知道 正的凸度总是增加逆时针的面积, 负的凸度总是增加顺时针的面积
det += arcArea * Math.sign(bul) * 2;
}
area += det;
}
return area / 2;
}
get Area() {
return Math.abs(this.Area2);
}
//闭合标志
get CloseMark() {
return this._ClosedMark;
}
//曲线是否闭合
get IsClose() {
return this.CloseMark || (this.EndParam > 1 && (equalv3(this.StartPoint, this.EndPoint, 1e-4)));
}
set CloseMark(v) {
this.WriteAllObjectRecord();
this._ClosedMark = v;
this.Update();
}
DigestionCloseMark() {
if (this._ClosedMark && this._LineData.length > 1) {
this.WriteAllObjectRecord();
this._ClosedMark = false;
if (!equalv2(this._LineData[0].pt, arrayLast(this._LineData).pt))
this._LineData.push({ pt: AsVector2(this._LineData[0].pt), bul: 0 });
}
}
/**
* 删除重复点
* @param [fuzz=0.1] 容差=0.1
*/
RemoveRepeatPos(fuzz = 0.1) {
let index = 0;
let pre = 0;
let writed = false;
for (let next = 1; next <= this._LineData.length; next++) {
if ((!this._ClosedMark && next === this._LineData.length) || //如果曲线没闭合 则总是保留最后一个点
!equalv2(this._LineData[pre].pt, this._LineData[FixIndex$1(next, this._LineData)].pt, fuzz)) {
if (!writed) {
this.WriteAllObjectRecord();
writed = true;
}
this._LineData[index] = this._LineData[pre];
index++;
}
pre++;
}
if (equalv2(this._LineData[0].pt, this._LineData[index - 1].pt, fuzz))
this._LineData[index - 1].pt.copy(this._LineData[0].pt);
this._LineData.length = index;
this.Update();
}
get Length() {
return this.Explode().reduce((l, cu) => l + cu.Length, 0);
}
/**
* 获得指定参数所在的点.
* 当曲线存在闭合标志时,参数必须在曲线内部.
* 当曲线不存在闭合标志时,参数允许延伸出曲线.
*
* @param {number} param 参数
* @returns {Vector3} 三维点,可为空
*/
GetPointAtParam(param) {
if (param === Math.floor(param) && this.ParamOnCurve(param))
return AsVector3(this.GetPoint2dAt(FixIndex$1(param, this.NumberOfVertices))).applyMatrix4(this.OCSNoClone);
let cu = this.GetCurveAtParam(param);
if (cu)
return cu.GetPointAtParam(this.GetCurveParamAtParam(param));
return undefined;
}
GetDistAtParam(param) {
if (this._ClosedMark && !this.ParamOnCurve(param))
return NaN;
//参数 整数
let paramFloor = Math.floor(param);
//需要计算的曲线个数
let cuCount = paramFloor > this.EndParam ? this.EndParam : paramFloor;
let dist = 0;
//首先计算完整曲线的长度
for (let i = 0; i < cuCount; i++) {
dist += this.GetCurveAtIndex(i).Length;
}
//参数已经大于索引,证明参数在线外.
if (paramFloor !== cuCount) {
dist += this.GetCurveAtParam(param).GetDistAtParam(param - cuCount);
}
else if (param > paramFloor) {
let lastParam = param - paramFloor;
dist += this.GetCurveAtParam(param).GetDistAtParam(lastParam);
}
return dist;
}
GetPointAtDistance(dist) {
let param = this.GetParamAtDist(dist);
return this.GetPointAtParam(param);
}
/**
* 返回参数所在的点. 如果曲线不闭合,会试图返回延伸点参数
*
* @param {Vector3} pt
* @returns {number}
* @memberof Polyline
*/
GetParamAtPoint(pt) {
let cus = this.Explode();
if (cus.length === 0)
return NaN;
for (let i = 0; i < cus.length; i++) {
let cu = cus[i];
let param = cu.GetParamAtPoint(pt);
if (cu.ParamOnCurve(param))
return i + param; //返回点在曲线内部的参数
}
//当曲线闭合时,不需要延伸首尾去判断参数
if (this._ClosedMark)
return NaN;
//起点终点参数集合
let seParams = [];
//点在第一条曲线上的参数
let startParam = cus[0].GetParamAtPoint(pt);
if (!isNaN(startParam) && startParam < 0)
seParams.push(startParam);
//点在最后一条线上的参数
let endParam = cus[cus.length - 1].GetParamAtPoint(pt);
if (!isNaN(endParam) && endParam > 0)
seParams.push(endParam + this.EndParam - 1);
if (seParams.length == 1) {
return seParams[0];
}
else if (seParams.length == 2) {
//返回较近的参数
if (pt.distanceToSquared(this.StartPoint)
< pt.distanceToSquared(this.EndPoint))
return seParams[0];
else
return seParams[1];
}
return NaN;
}
GetParamAtDist(dist) {
if (equaln$1(dist, 0))
return 0;
let cus = [];
for (let i = 0; i < this.EndParam; i++) {
let cu = this.GetCurveAtIndex(i);
let len = cu.Length;
if (len < 1e-6)
continue;
cus.push(cu);
if (dist <= len)
return i + cu.GetParamAtDist(dist);
else if (equaln$1(dist, len, 1e-8))
return i + 1;
dist -= len;
}
if (!this._ClosedMark)
return cus.length + cus[cus.length - 1].GetParamAtDist(dist);
return NaN;
}
GetDistAtPoint(pt) {
let param = this.GetParamAtPoint(pt);
if (!this.ParamOnCurve(param))
return NaN;
return this.GetDistAtParam(param);
}
/**
* 返回曲线的一阶导数.
* 当曲线闭合(标志)且点不在曲线上.
* 或者曲线不闭合(标志) 且点不在曲线上也不在延伸上
*
* @param {(number | Vector3)} param
* @returns {Vector3}
* @memberof Polyline
*/
GetFirstDeriv(param) {
if (param instanceof three.Vector3)
param = this.GetParamAtPoint(param);
if (isNaN(param))
return undefined;
let cu = this.GetCurveAtParam(param);
if (!cu)
return undefined;
return cu.GetFirstDeriv(this.GetCurveParamAtParam(param));
}
GetSplitCurves(param) {
//参数需要转化为参数数组
let params;
if (typeof param == "number")
params = [param];
else
params = param;
//校验参数在曲线中,修正参数
let endParam = this.EndParam;
params = params.filter(p => this.ParamOnCurve(p) && p > -1e-6)
.map(a => {
if (a < 0)
return 0;
if (a > endParam)
return endParam;
if (equaln$1(a, Math.floor(a + 0.5), 1e-8))
return Math.floor(a + 0.5);
return a;
});
//排序
params.sort((a, b) => a - b);
let hasEndParam = arrayLast(params) === this.EndParam;
//必须加入最后一个参数,保证切割后的曲线完整
if (!hasEndParam)
params.push(this.EndParam);
arrayRemoveDuplicateBySort(params, (e1, e2) => equaln$1(e1, e2, 1e-8));
params = params.filter(p => this.ParamOnCurve(p));
if (params.length === 0)
return [];
//判断是否存在0参数
let hasZeroParam = params[0] === 0;
if (hasZeroParam)
params.shift();
let { pts, buls } = this.PtsBuls;
//返回的多段线集合
let pls = [];
let len = 0; //已经走过的参数长度(整数)
//上一个切割参数的位置 0-1
let prePa = 0;
for (let pa of params) {
//参数所在点
let pt = AsVector2(this.GetPointAtParam(pa).applyMatrix4(this.OCSInv));
pa -= len;
let pafloor = Math.floor(pa);
len += pafloor;
let plData = [];
//添加点
for (let i = 0; i < pafloor; i++) {
if (i === 0 && !equaln$1(buls[0], 0, 1e-8)) {
buls[0] = Math.tan((1 - prePa) * Math.atan(buls[0]));
}
plData.push({ pt: pts[0], bul: buls[0] });
pts.shift();
buls.shift();
}
if (equaln$1(pa, pafloor, 1e-8)) //如果pa在点上
{
plData.push({ pt: pts[0].clone(), bul: buls[0] });
}
else //在曲线上
{
let bul = buls[0];
if (!equaln$1(bul, 0, 1e-6))
bul = Math.tan((pa - pafloor - (0 === pafloor ? prePa : 0)) * Math.atan(buls[0])); //->凸度
//加入顶点+凸度
plData.push({ pt: pts[0].clone(), bul });
//终点
plData.push({ pt, bul: 0 });
//修正剩余的点表和凸度表
pts[0].copy(pt);
}
prePa = pa - pafloor;
if (plData.length > 1) {
let pl = new Polyline_1(plData).ApplyMatrix(this.OCS);
pl.ColorIndex = this.ColorIndex;
pls.push(pl);
}
}
//当曲线为闭合曲线,并且不存在0切割参数时,首尾连接曲线
if (this._ClosedMark && !hasZeroParam && !hasEndParam) {
let lastPl = pls[pls.length - 1];
if (equalv2(arrayLast(lastPl._LineData).pt, pls[0]._LineData[0].pt))
lastPl._LineData.pop();
lastPl._LineData.push(...pls[0]._LineData);
pls.shift();
}
return pls;
}
//未完善
GetCurveAtParamRange(startParam, endParam) {
let sfloor = Math.floor(startParam + 0.5);
if (equaln$1(sfloor, startParam, 1e-8))
startParam = sfloor;
else
sfloor = Math.floor(startParam);
let efloor = Math.floor(endParam + 0.5);
if (equaln$1(efloor, endParam, 1e-8))
endParam = efloor;
else
efloor = Math.floor(efloor);
const GetCurve = (index) => {
let d = this._LineData[index];
let next = this._LineData[index + 1];
if (!equaln$1(d.bul, 0, 1e-8))
return new exports.Arc().ParseFromBul(d.pt, next.pt, d.bul);
else
return new exports.Line(AsVector3(d.pt), AsVector3(next.pt));
};
let lined = [];
if (startParam === sfloor) {
let d = this._LineData[sfloor];
lined.push({ pt: d.pt.clone(), bul: d.bul });
}
else {
let d = this._LineData[sfloor];
let cu = GetCurve(sfloor);
let remParam = startParam - sfloor;
let p = cu.GetPointAtParam(remParam);
let bul = d.bul;
if (!equaln$1(bul, 0))
bul = Math.tan(Math.atan(bul) * (1 - remParam));
lined.push({ pt: AsVector2(p), bul: bul });
}
for (let i = sfloor + 1; i < efloor; i++) {
let d = this._LineData[i];
lined.push({ pt: d.pt.clone(), bul: d.bul });
}
if (efloor !== endParam) {
let d = this.LineData[efloor];
let remParam = endParam - efloor;
let cu = GetCurve(efloor);
let p = cu.GetPointAtParam(remParam);
let bul = d.bul;
if (!equaln$1(bul, 0)) {
arrayLast(lined).bul = Math.tan(Math.atan(bul) * remParam);
bul = Math.tan(Math.atan(bul) * (1 - remParam));
}
lined.push({ pt: AsVector2(p), bul });
}
let pl = new Polyline_1(lined);
pl.OCS = this.OCSNoClone;
return;
}
Extend(newParam) {
if (this.CloseMark || this.ParamOnCurve(newParam))
return;
this.WriteAllObjectRecord();
let ptIndex;
let bulIndex;
if (newParam < 0) {
ptIndex = 0;
bulIndex = 0;
}
else if (newParam > this.EndParam) {
ptIndex = this.EndParam;
bulIndex = ptIndex - 1;
}
//修改顶点
this._LineData[ptIndex].pt = AsVector2(this.GetPointAtParam(newParam).applyMatrix4(this.OCSInv));
//修改凸度
let oldBul = this._LineData[bulIndex].bul;
if (oldBul != 0)
this._LineData[bulIndex].bul = Math.tan(Math.atan(oldBul) * (1 + newParam - ptIndex));
this.Update();
}
//const this
MatrixAlignTo2(toMatrix) {
if (!matrixIsCoplane(this._Matrix, toMatrix, 1e-4))
return this.PtsBuls;
let m = matrixAlignCoordSys(this._Matrix, toMatrix);
let z1 = this.Normal;
let z2 = new three.Vector3().setFromMatrixColumn(toMatrix, 2);
let isMirror = equalv3(z1, z2.negate());
let pts = [];
let buls = [];
for (let d of this._LineData) {
let p = AsVector2(AsVector3(d.pt).applyMatrix4(m));
pts.push(p);
buls.push(isMirror ? -d.bul : d.bul);
}
return { pts, buls };
}
Join(cu, allowGap = false, tolerance = 1e-4) {
this.WriteAllObjectRecord();
if (this._ClosedMark)
return exports.Status.False;
let [sp, ep, cuSp, cuEp] = [this.StartPoint, this.EndPoint, cu.StartPoint, cu.EndPoint];
let ocsInv = this.OCSInv;
let [cuSp2, cuEp2] = [cuSp, cuEp].map(p => AsVector2(p.clone().applyMatrix4(ocsInv)));
if (this._LineData.length === 0) {
if (cu instanceof exports.Line) {
this._LineData.push({ pt: cuSp2, bul: 0 });
this._LineData.push({ pt: cuEp2, bul: 0 });
}
else if (cu instanceof exports.Arc) {
let x = new three.Vector3().setFromMatrixColumn(cu.OCSNoClone, 0);
let y = new three.Vector3().setFromMatrixColumn(cu.OCSNoClone, 1);
let cuZ = x.cross(y);
let dir = equalv3(this.Normal, cuZ, 1e-4) ? 1 : -1;
let bul = cu.Bul * dir;
this._LineData.push({ pt: cuSp2, bul: bul });
this._LineData.push({ pt: cuEp2, bul: 0 });
}
else if (cu instanceof Polyline_1) {
let f = new CADFiler();
cu.WriteFile(f);
this.ReadFile(f);
}
else
return exports.Status.False;
}
else {
let LinkType;
(function (LinkType) {
LinkType[LinkType["None"] = 0] = "None";
LinkType[LinkType["SpSp"] = 1] = "SpSp";
LinkType[LinkType["SpEp"] = 2] = "SpEp";
LinkType[LinkType["EpSp"] = 3] = "EpSp";
LinkType[LinkType["EpEp"] = 4] = "EpEp";
})(LinkType || (LinkType = {}));
let spspDisSq = cuSp.distanceToSquared(sp);
let spepDisSq = cuSp.distanceToSquared(ep);
let epspDisSq = cuEp.distanceToSquared(sp);
let epepDisSq = cuEp.distanceToSquared(ep);
let minDis = tolerance * tolerance;
let linkType = LinkType.None;
if (epspDisSq < minDis) {
linkType = LinkType.EpSp;
minDis = epspDisSq;
}
if (epepDisSq < minDis) {
linkType = LinkType.EpEp;
minDis = epepDisSq;
}
if (spspDisSq < minDis) {
linkType = LinkType.SpSp;
minDis = spspDisSq;
}
if (spepDisSq < minDis) {
linkType = LinkType.SpEp;
minDis = spepDisSq;
}
if (linkType === LinkType.None)
return exports.Status.False;
if (cu instanceof exports.Line) {
if (linkType === LinkType.SpSp) {
this._LineData.unshift({ pt: cuEp2, bul: 0 });
}
else if (linkType === LinkType.SpEp) {
this._LineData.push({ pt: cuEp2, bul: 0 });
}
else if (linkType === LinkType.EpSp) {
this._LineData.unshift({ pt: cuSp2, bul: 0 });
}
else if (linkType === LinkType.EpEp) {
this._LineData.push({ pt: cuSp2, bul: 0 });
}
}
else if (cu instanceof exports.Arc) {
let x = new three.Vector3().setFromMatrixColumn(cu.OCSNoClone, 0);
let y = new three.Vector3().setFromMatrixColumn(cu.OCSNoClone, 1);
let cuZ = x.cross(y);
let dir = equalv3(this.Normal, cuZ, 1e-4) ? 1 : -1;
let bul = cu.Bul * dir;
if (linkType === LinkType.SpSp) {
this._LineData.unshift({ pt: cuEp2, bul: -bul });
}
else if (linkType === LinkType.SpEp) {
arrayLast(this._LineData).bul = bul;
this._LineData.push({ pt: cuEp2, bul: 0 });
}
else if (linkType === LinkType.EpSp) {
this._LineData.unshift({ pt: cuSp2, bul: bul });
}
else if (linkType === LinkType.EpEp) {
arrayLast(this._LineData).bul = -bul;
this._LineData.push({ pt: cuSp2, bul: 0 });
}
}
else if (cu instanceof Polyline_1) {
if (cu.CloseMark)
return exports.Status.False;
let { pts, buls } = this.PtsBuls;
if (linkType === LinkType.SpSp) {
cu.Reverse();
let cuPtsBul = cu.MatrixAlignTo2(this.OCS);
cuPtsBul.pts.pop();
cuPtsBul.buls.pop();
pts = cuPtsBul.pts.concat(pts);
buls = cuPtsBul.buls.concat(buls);
}
else if (linkType === LinkType.SpEp) {
pts.pop();
buls.pop();
let cuPtsBul = cu.MatrixAlignTo2(this.OCS);
pts = pts.concat(cuPtsBul.pts);
buls = buls.concat(cuPtsBul.buls);
}
else if (linkType === LinkType.EpSp) {
let cuPtsBul = cu.MatrixAlignTo2(this.OCS);
cuPtsBul.pts.pop();
cuPtsBul.buls.pop();
pts = cuPtsBul.pts.concat(pts);
buls = cuPtsBul.buls.concat(buls);
}
else if (linkType === LinkType.EpEp) {
pts.pop();
buls.pop();
cu.Reverse();
let cuPtsBul = cu.MatrixAlignTo2(this.OCS);
pts = pts.concat(cuPtsBul.pts);
buls = buls.concat(cuPtsBul.buls);
}
this._LineData.length = 0;
for (let i = 0; i < pts.length; i++) {
this._LineData.push({ pt: pts[i], bul: buls[i] });
}
}
else
return exports.Status.False;
}
//在上面的其他分支已经返回了假 所以这里直接返回真.
this.Update();
return exports.Status.True;
}
/**
* 将曲线数组组合成多段线
* @param curves 已经使用CurveLinked的数组,总是首尾相连
* @returns
*/
static Combine(curves, tolerance = 1e-5) {
if (!curves || curves.length === 0)
return;
let pl = new Polyline_1;
pl.OCS = ComputerCurvesNormalOCS(curves);
for (let cu of curves) {
pl.Join(cu, false, 0.1);
// if (status === Status.False)
// console.log();
}
let d = pl.LineData;
if (d.length > 1) {
let ld = arrayLast(d).pt;
if (equalv2(d[0].pt, ld, tolerance))
ld.copy(d[0].pt);
}
return pl;
}
/**首尾相连的曲线直接连接 */
static FastCombine(curves, tolerance = 1e-5, computOCS = true) {
if (!curves || curves.length === 0)
return;
let pl = new Polyline_1;
if (computOCS)
pl.OCS = ComputerCurvesNormalOCS(curves);
let ocsInv = pl.OCSInv;
let normal = pl.Normal;
let lineData = [];
for (let i = 0; i < curves.length; i++) {
let cu = curves[i];
let bul = 0;
if (cu instanceof exports.Arc) {
let dir = equalv3(normal, cu.Normal.negate(), 1e-3) ? -1 : 1;
bul = cu.Bul * dir;
}
lineData.push({
pt: AsVector2(cu.StartPoint.applyMatrix4(ocsInv)),
bul
});
if (i === curves.length - 1) {
lineData.push({
pt: AsVector2(cu.EndPoint.applyMatrix4(ocsInv)),
bul: 0
});
}
}
if (lineData.length > 1) {
let ld = arrayLast(lineData).pt;
if (equalv2(lineData[0].pt, ld, tolerance))
ld.copy(lineData[0].pt);
}
pl.LineData = lineData;
return pl;
}
PtOnCurve(pt, fuzz = 1e-6) {
for (let i = 0; i < this.EndParam; i++) {
let c = this.GetCurveAtIndex(i);
if (c.PtOnCurve(pt, fuzz))
return true;
}
return false;
}
//点在曲线上,已经确定点在曲线的延伸线上
PtOnCurve3(p, fuzz = 1e-6) {
for (let i = 0; i < this.EndParam; i++) {
let c = this.GetCurveAtIndex(i);
if (c.PtOnCurve3(p, fuzz))
return true;
}
return false;
}
PtInCurve(pt) {
return this.IsClose && IsPointInPolyLine(this, pt);
}
GetClosestPointTo(pt, extend) {
return this.GetClosestPointTo2(pt, extend ? ExtendType.Both : ExtendType.None);
}
GetClosestPointTo2(pt, extType) {
//当曲线空时,返回空
if (this.EndParam < 1)
return undefined;
//当有闭合标志时,曲线在任何位置都不延伸
if (this._ClosedMark)
extType = ExtendType.None;
//最近点
let ptC = undefined;
//最近点的距离
let ptCDist = Infinity;
for (let i = 0; i < this.EndParam; i++) {
let cu = this.GetCurveAtIndex(i);
//前延伸
if (i === 0 && (extType & ExtendType.Front) > 0) {
let ptCFirst = cu.GetClosestPointTo(pt, true);
if (cu.GetParamAtPoint(ptCFirst) <= 1) {
ptC = ptCFirst;
ptCDist = ptC.distanceToSquared(pt);
}
if (extType === ExtendType.Front)
continue;
}
let ptCloseNew; //新的最近点
//后延伸 (此处与前延伸分开if 如果线只有一段,那么前后延伸都能同时触发)
if (i === (this.EndParam - 1) && (extType & ExtendType.Back) > 0) {
let ptCLast = cu.GetClosestPointTo(pt, true);
if (cu.GetParamAtPoint(ptCLast) >= 0)
ptCloseNew = ptCLast;
else //如果延伸之后并不在曲线或者曲线的后延伸上
ptCloseNew = cu.EndPoint;
}
else {
ptCloseNew = cu.GetClosestPointTo(pt, false);
}
let newDist = ptCloseNew.distanceToSquared(pt);
if (newDist < ptCDist) {
ptC = ptCloseNew;
ptCDist = newDist;
}
}
return ptC;
}
//偏移
GetOffsetCurves(offsetDist) {
if (equaln$1(offsetDist, 0))
return [];
let polyOffestUtil = new OffsetPolyline(this, offsetDist);
let curves = polyOffestUtil.Do();
for (let cu of curves)
cu.ColorIndex = this.ColorIndex;
return curves;
}
GetFeedingToolPath(offsetDist, offsetDistSq = (offsetDist ** 2) * 2.1, joinType = PolylineJoinType.Round) {
if (equaln$1(offsetDist, 0))
return [];
let polyOffestUtil = new OffsetPolyline(this, offsetDist, true, offsetDistSq, joinType);
return polyOffestUtil.Do();
}
/**
* 分解
*/
Explode() {
let exportCus = [];
for (let i = 0; i < this.EndParam; i++) {
exportCus.push(this.GetCurveAtIndex(i));
}
return exportCus;
}
/**
* 根据参数得到参数所在的子曲线.
*
* 当曲线存在闭合标志时,参数必须在曲线内部,否则返回空.
*
* @param {number} param 参数值
* @returns {Curve} 曲线(直线或者圆弧) 或空
* @memberof Polyline
*/
GetCurveAtParam(param) {
if (this._ClosedMark && !this.ParamOnCurve(param))
return undefined;
if (param < 0)
return this.GetCurveAtIndex(0);
else if (param >= this.EndParam)
return this.GetCurveAtIndex(this.EndParam - 1);
else
return this.GetCurveAtIndex(Math.floor(param));
}
/**
* 得到参数在子曲线中的表示
*
* @param {number} param 参数在多段线中表示
* @returns {number} 参数在子曲线中表示
* @memberof Polyline
*/
GetCurveParamAtParam(param) {
if (param >= this.EndParam)
param -= this.EndParam - 1;
else if (param > 0)
param -= Math.floor(param);
return param;
}
/**
* 获得曲线,来自索引位置.
* @param {number} i 索引位置 整数
*/
GetCurveAtIndex(i) {
if (i >= this._LineData.length)
return undefined;
if (!this.ParamOnCurve(i))
return undefined;
if (!this._ClosedMark && i === this._LineData.length - 1)
return undefined;
let d1 = this._LineData[i];
let d2 = this._LineData[FixIndex$1(i + 1, this._LineData)];
let curve;
if (equaln$1(d1.bul, 0, BUL_IS_LINE_FUZZ))
curve = new exports.Line(AsVector3(d1.pt), AsVector3(d2.pt)).ApplyMatrix(this.OCSNoClone);
else
curve = new exports.Arc().ParseFromBul(d1.pt, d2.pt, d1.bul).ApplyMatrix(this.OCSNoClone);
curve.ColorIndex = this._Color;
return curve;
}
IntersectWith2(curve, intType, tolerance = 1e-5) {
return IntersectPolylineAndCurve(this, curve, intType, tolerance);
}
//计算自交点.
IntersectSelf() {
let cus = this.Explode();
if (cus.length === 0)
return [];
let intParams = [];
for (let i = 0; i < cus.length; i++) {
let c = cus[i];
for (let j = i + 2; j < cus.length; j++) {
let c2 = cus[j];
let pts = c.IntersectWith(c2, IntersectOption.ExtendNone);
for (let p of pts) {
intParams.push(i + c.GetParamAtPoint(p));
intParams.push(j + c2.GetParamAtPoint(p));
}
}
}
return intParams;
}
IsIntersectSelf() {
let cus = this.Explode().filter(c => !equaln$1(c.Length, 0, 1e-3));
for (let i = 0; i < cus.length - 1; i++) {
let c1 = cus[i];
let c1IsLine = c1 instanceof exports.Line;
let d1 = c1.GetFirstDeriv(c1IsLine ? 0 : 1).normalize();
for (let j = i + 1; j < cus.length; j++) {
let c2 = cus[j];
let c2IsLine = c2 instanceof exports.Line;
let d2 = c2.GetFirstDeriv(0).normalize();
if (j === i + 1) {
if (c1IsLine === c2IsLine) {
if (c1IsLine) {
if (equalv3(d1, d2.negate()))
return true;
continue;
}
else {
if (equalv3(d1, d2.negate()) && equalv3(c1.Center, c2.Center))
return true;
}
}
}
let intPts = c1.IntersectWith2(c2, 0);
let intPtsLen = intPts.length;
if (intPtsLen > 0) {
if (intPtsLen === 2 && equalv3(intPts[0].pt, intPts[1].pt, 1e-3)) {
intPtsLen = 1;
intPts.pop();
}
if (intPtsLen === 2 && j === i + 1 && cus.length === 2) {
if (intPts.every(r => equaln$1(r.thisParam, 0, 1e-3) || equaln$1(r.thisParam, 1, 1e-3)))
continue;
}
if (j === i + 1 && intPtsLen === 1)
continue;
if (this.IsClose && i === 0 && j === cus.length - 1 && intPtsLen === 1)
continue;
return true;
}
}
}
return false;
}
get BoundingBox() {
let box = new Box3Ext();
for (let i = 0; i < this.EndParam; i++) {
let cu = this.GetCurveAtIndex(i);
box.union(cu.BoundingBox);
}
return box;
}
SetPtsBuls(pts, buls) {
this.WriteAllObjectRecord();
this._LineData.length = 0;
for (let i = 0; i < pts.length; i++) {
let pt = pts[i];
let bul = buls[i];
this._LineData.push({ pt, bul });
}
this.Update();
return this;
}
/**
* 得到曲线有用的点表和凸度(闭合曲线首尾重复)
*/
get PtsBuls() {
let pts = [];
let buls = [];
if (this._LineData.length === 0)
return { pts, buls };
for (let data of this._LineData) {
pts.push(data.pt.clone());
buls.push(data.bul);
}
//闭合且起点不等于终点
if (this._ClosedMark &&
!this._LineData[0].pt.equals(arrayLast(this._LineData).pt)) {
pts.push(pts[0].clone());
buls.push(buls[0]);
}
return { pts, buls };
}
get IsBulge() {
if (!this.IsClose)
return false;
let refDir = Math.sign(this.Area2);
let c1;
let c2;
for (let i = 0; i < this.EndParam; i++) {
c1 = this.GetCurveAtIndex(i);
c2 = this.GetCurveAtIndex(FixIndex$1(i + 1, this.EndParam));
let len1 = c1.Length;
let len2 = c2.Length;
let minLen = Math.min(len1, len2) * 0.2;
let p = c1.EndPoint;
let p1;
let p2;
if (c1 instanceof exports.Arc) {
let dir = c1.IsClockWise ? -1 : 1;
if (dir !== refDir)
return false;
p1 = c1.GetPointAtDistance(len1 - minLen);
}
else
p1 = c1.StartPoint;
if (c2 instanceof exports.Arc) {
let dir = c2.IsClockWise ? -1 : 1;
if (dir !== refDir)
return false;
p2 = c2.GetPointAtDistance(minLen);
}
else
p2 = c2.EndPoint;
let vec1 = p.clone().sub(p1);
let vec2 = p2.sub(p);
let dir = Math.sign(vec1.cross(vec2).z);
if (dir !== 0 && dir !== refDir)
return false;
}
return true;
}
get Shape() {
let { pts, buls } = this.PtsBuls;
return CreatePolylinePath(pts, buls);
}
get SVG() {
let sp = this.StartPoint;
let str = `M${sp.x} ${sp.y} `;
for (let i = 1; i <= this.EndParam; i++) {
let bul = this.GetBulgeAt(i - 1);
let p = this.GetPointAtParam(i);
if (bul === 0)
str += `L${p.x} ${p.y} `;
else {
let arc = this.GetCurveAtIndex(i - 1);
str += `A ${arc.Radius} ${arc.Radius} 0 ${Math.abs(bul) >= 1 ? 1 : 0} ${arc.IsClockWise ? 0 : 1} ${p.x} ${p.y}`;
}
}
return str;
}
GetDragPointCount(drag) {
if (drag === DragPointType.Grip) {
let count = this.EndParam * 2 + 1;
if (this.CloseMark)
count--;
return count;
}
else {
return this._LineData.length;
}
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
switch (snapMode) {
case ObjectSnapMode.End:
return this.GetStretchPoints();
case ObjectSnapMode.Mid:
let midPts = [];
let enParam = this.EndParam;
for (let i = 0.5; i < enParam; i++) {
let p = this.GetPointAtParam(i);
p && midPts.push(p);
}
return midPts;
case ObjectSnapMode.Nea:
{
let nea = [];
for (let cu of this.Explode()) {
let neaa = cu.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform);
if (neaa)
nea.push(...neaa);
}
return nea;
}
case ObjectSnapMode.Ext:
{
let cp = this.GetClosestPointTo(pickPoint, true);
if (cp)
return [cp];
break;
}
case ObjectSnapMode.Cen:
let cenPts = [];
for (let i = 0; i < this._LineData.length; i++) {
let data = this._LineData[i];
if (!equaln$1(data.bul, 0)) {
let cu = this.GetCurveAtIndex(i);
if (cu) //end bul !== 0 但是并没有圆弧
cenPts.push(cu.Center);
}
}
return cenPts;
case ObjectSnapMode.Per:
if (lastPoint) {
let cp = this.GetClosestPointTo(pickPoint, false);
if (!cp)
return [];
let cparam = this.GetParamAtPoint(cp);
let cu = this.GetCurveAtParam(cparam);
if (cu) {
let closestPt = cu.GetClosestPointTo(lastPoint, true);
if (closestPt && this.PtOnCurve(closestPt))
return [closestPt];
}
}
case ObjectSnapMode.Tan:
if (lastPoint) {
let clostPt = this.GetClosestPointTo(pickPoint, false);
if (!clostPt)
return [];
let par = this.GetParamAtPoint(clostPt);
let cu = this.GetCurveAtParam(par);
if (cu instanceof exports.Arc)
return cu.GetObjectSnapPoints(snapMode, pickPoint, lastPoint);
return [];
}
}
return [];
}
GetGripPoints() {
let ptList = [];
if (this._LineData.length < 2)
return ptList;
let enParam = this.EndParam;
if (this.CloseMark)
enParam -= 0.5;
for (let i = 0; i < enParam + 0.5; i += 0.5) {
let p = this.GetPointAtParam(i);
ptList.push(p);
}
return ptList;
}
MoveGripPoints(indexList, moveVec) {
this.WriteAllObjectRecord();
let moveVLoc = AsVector2(moveVec.clone().applyMatrix4(new three.Matrix4().extractRotation(this.OCSInv)));
let calcIndexList = indexList;
if (indexList.length > 1) {
let centerIndexes = indexList.filter(i => i % 2 === 0);
if (centerIndexes.length > 0)
calcIndexList = centerIndexes;
}
for (let index of calcIndexList) {
if (index % 2 === 0) {
let cuIndex = index / 2;
let ptCount = this._LineData.length;
let frontIndex = cuIndex - 1;
if (this._ClosedMark)
frontIndex = FixIndex$1(frontIndex, ptCount);
if (frontIndex >= 0 && this.GetBulgeAt(frontIndex)) {
let arc = this.GetCurveAtIndex(frontIndex);
arc.MoveGripPoints([2], moveVec);
this._LineData[frontIndex].bul = arc.Bul;
}
if ((cuIndex !== ptCount - 1) && this.GetBulgeAt(cuIndex)) {
let arc = this.GetCurveAtIndex(cuIndex);
arc.MoveGripPoints([0], moveVec);
this._LineData[cuIndex].bul = arc.Bul;
}
this._LineData[cuIndex].pt.add(moveVLoc);
}
else {
let ptIndex = (index - 1) / 2;
let nextIndex = (FixIndex$1(ptIndex + 1, this._LineData));
let d = this._LineData[ptIndex];
if (d.bul === 0) {
this._LineData[ptIndex].pt.add(moveVLoc);
this._LineData[nextIndex].pt.add(moveVLoc);
}
else {
let arc = this.GetCurveAtIndex(ptIndex);
arc.MoveGripPoints([1], moveVec);
this._LineData[ptIndex].bul = arc.Bul;
}
}
}
this.Update();
}
GetStretchPoints() {
let iswcs = MatrixIsIdentityCS(this._Matrix);
let pts = [];
for (let data of this._LineData) {
let p = AsVector3(data.pt);
if (!iswcs)
p.applyMatrix4(this._Matrix);
pts.push(p);
}
return pts;
}
/**
* 范围拉伸(stretch),对夹点进行拉伸.
* 如果对圆弧的一侧进行拉伸,那么修改bul
*
* @param {Array<number>} indexList
* @param {Vector3} vec
*/
MoveStretchPoints(indexList, vec) {
this.WriteAllObjectRecord();
//本地坐标系移动向量
let moveVLoc = vec.clone().applyMatrix4(new three.Matrix4().extractRotation(this.OCSInv));
let ptCount = this._LineData.length;
for (let index of indexList) {
if (index >= ptCount)
throw "在拉伸多段线顶点时,尝试拉伸不存在的顶点!(通常是因为模块中的板轮廓被破坏,导致的顶点丢失!)";
let frontIndex = index - 1;
let nextIndex = index + 1;
if (this._ClosedMark) {
frontIndex = FixIndex$1(frontIndex, ptCount);
nextIndex = FixIndex$1(nextIndex, ptCount);
}
/**
* 根据新的拉伸点修改凸度.
*
* @param {number} nextIndex 隔壁点索引
* @param {number} bulIndex 需要修改凸度位置的索引
* @returns
*/
const ChangeBul = (nextIndex, bulIndex) => {
//需要修改的点的数据
let d = this._LineData[bulIndex];
if (d === undefined || d.bul === 0)
return;
//如果隔壁点不在拉伸列表中
if (indexList.indexOf(nextIndex) === -1) {
let needChangeP = this.GetPointAtParam(index);
let notChangeP = this.GetPointAtParam(nextIndex);
//原先的弦长的一半
let oldChordLengthHalf = needChangeP.distanceTo(notChangeP) * 0.5;
//弓高
let arcHeight = oldChordLengthHalf * d.bul;
needChangeP.add(vec);
let newChordLengthHalf = needChangeP.distanceTo(notChangeP) * 0.5;
d.bul = arcHeight / newChordLengthHalf;
}
};
ChangeBul(frontIndex, frontIndex);
ChangeBul(nextIndex, index);
//修改顶点
this._LineData[index].pt.add(AsVector2(moveVLoc));
}
this.Update();
}
_ReadFile(file) {
super._ReadFile(file);
let ver = file.Read();
this._LineData.length = 0;
let count = file.Read();
for (let i = 0; i < count; i++) {
let v = new three.Vector2().fromArray(file.Read());
let bul = file.Read();
this._LineData.push({ pt: v, bul: bul });
}
if (ver > 1)
this._ClosedMark = file.Read();
// 某些时候会画出这样的多段线 尚未知道是为什么画出的
// if (this._LineData.length === 0)
// this.Erase();
}
//对象将自身数据写入到文件.
WriteFile(file) {
super.WriteFile(file);
file.Write(2);
file.Write(this._LineData.length);
for (let l of this._LineData) {
file.Write(l.pt.toArray());
file.Write(l.bul);
}
file.Write(this._ClosedMark);
}
};
exports.Polyline = Polyline_1 = __decorate([
Factory
], exports.Polyline);
const TempPolyline = new exports.Polyline();
/**
* 相交延伸选项.
*
* @export
* @enum {number}
*/
var IntersectOption;
(function (IntersectOption) {
/**
* 两者都不延伸
*/
IntersectOption[IntersectOption["ExtendNone"] = 0] = "ExtendNone";
/**
* 延伸自身
*/
IntersectOption[IntersectOption["ExtendThis"] = 1] = "ExtendThis";
/**
* 延伸参数
*/
IntersectOption[IntersectOption["ExtendArg"] = 2] = "ExtendArg";
/**
* 延伸两者
*/
IntersectOption[IntersectOption["ExtendBoth"] = 3] = "ExtendBoth";
})(IntersectOption || (IntersectOption = {}));
//延伸自身还是参数反转
function reverseIntersectOption(intType) {
if (intType === IntersectOption.ExtendThis)
intType = IntersectOption.ExtendArg;
else if (intType === IntersectOption.ExtendArg)
intType = IntersectOption.ExtendThis;
return intType;
}
/**
* 校验相交点是否满足延伸选项
* 算法会计算无限延伸状态下的曲线交点,调用该方法进行校验返回校验后的点表
*
* @param {Vector3[]} intRes 相交点.曲线当作完全状态下的相交点
* @param {Curve} c1 曲线1 由this参数传入
* @param {Curve} c2 曲线2 由arg 参数传入
* @param {Intersect} extType 延伸选项.
* @returns {Array<Vector3>} 校验完成后的点表
*/
function CheckPointOnCurve(intRes, c1, c2, extType, tolerance = 1e-6) {
return intRes.filter(r => {
if (!(extType & IntersectOption.ExtendThis))
if (!c1.ParamOnCurve(r.thisParam, tolerance / c1.Length))
return false;
if (!(extType & IntersectOption.ExtendArg))
if (!c2.ParamOnCurve(r.argParam, tolerance / c2.Length))
return false;
return true;
});
}
function IntersectCircleAndCircle(cu1, cu2, tolerance = 1e-4) {
if (!cu1.IsCoplaneTo(cu2))
return [];
let c1OcsInv = cu1.OCSInv;
let c1Ocs = cu1.OCSNoClone;
let center1 = cu1.Center.applyMatrix4(c1OcsInv);
let center2 = cu2.Center.applyMatrix4(c1OcsInv);
let radius1 = cu1.Radius;
let radius2 = cu2.Radius;
let pts = [];
let dist = center2.distanceTo(center1);
if (dist < Math.abs(radius1 - radius2) - 1e-3
|| dist > (radius1 + radius2 + 1e-3))
return pts;
if (equaln$1(dist, 0, tolerance))
return pts;
let dstsqr = dist * dist;
let r1sqr = radius1 * radius1;
let r2sqr = radius2 * radius2;
let a = (dstsqr - r2sqr + r1sqr) / (2 * dist);
let h = Math.sqrt(Math.abs(r1sqr - (a * a)));
let ratio_a = a / dist;
let ratio_h = h / dist;
let dx = center2.x - center1.x;
let dy = center2.y - center1.y;
let phix = center1.x + (ratio_a * dx);
let phiy = center1.y + (ratio_a * dy);
dx *= ratio_h;
dy *= ratio_h;
let p1 = new three.Vector3(phix + dy, phiy - dx);
let p2 = new three.Vector3(phix - dy, phiy + dx);
p1.applyMatrix4(c1Ocs);
p2.applyMatrix4(c1Ocs);
pts.push({
pt: p1,
thisParam: cu1.GetParamAtPoint(p1),
argParam: cu2.GetParamAtPoint(p1),
});
if (!equalv3(p1, p2, tolerance)) //防止点重复
pts.push({
pt: p2,
thisParam: cu1.GetParamAtPoint(p2),
argParam: cu2.GetParamAtPoint(p2),
});
return pts;
}
/**
* 计算圆与圆弧的交点.
*
* @export
* @param {Circle} circle 圆
* @param {Arc} arc 圆弧
* @param {IntersectOption} extType 延伸选项
* @returns 交点集合
*/
function IntersectCircleAndArc(circle, arc, extType, tolerance = 1e-6) {
let pts = IntersectCircleAndCircle(circle, arc);
return CheckPointOnCurve(pts, circle, arc, extType | IntersectOption.ExtendThis, tolerance);
}
/**
* 计算圆弧与圆弧的交点
*
* @export
* @param {Arc} arc1 圆弧
* @param {Arc} arc2 圆弧
* @param {IntersectOption} extType 延伸选项
* @returns 交点集合
*/
function IntersectArcAndArc(arc1, arc2, extType, tolerance = 1e-5) {
let pts = IntersectCircleAndCircle(arc1, arc2, tolerance);
return CheckPointOnCurve(pts, arc1, arc2, extType, tolerance);
}
function IntersectEllipseAndLine(l, el, extType, tolerance = 1e-6) {
let pts = IntersectLineAndEllipseFor2D(l, el);
return CheckPointOnCurve(pts, l, el, extType, tolerance);
}
/**
* 通用方法:计算直线与圆的交点,默认延伸全部
*
* @export
* @param {Line} line 直线
* @param {(Circle | Arc)} circle 圆或圆弧
* @returns 交点集合
*/
function IntersectLineAndCircleOrArc(line, circle, tolerance = 1e-6) {
let lineOrg = line.StartPoint;
let lineDirection = line.EndPoint.sub(lineOrg);
let dirLen = lineDirection.length();
if (equaln$1(dirLen, 0))
return [];
lineDirection.divideScalar(dirLen);
let diff = lineOrg.clone().sub(circle.Center);
let a0 = diff.dot(diff) - circle.Radius ** 2;
let a1 = lineDirection.dot(diff);
let discr = a1 ** 2 - a0;
if (equaln$1(discr, 0, tolerance)) {
let pt = lineOrg.add(lineDirection.multiplyScalar(-a1));
return [{
pt,
thisParam: -a1 / dirLen,
argParam: circle.GetParamAtPoint(pt)
}];
}
else if (discr > 0) {
let root = Math.sqrt(discr);
let p1 = lineOrg.clone().add(lineDirection.clone().multiplyScalar(-a1 + root));
let p2 = lineOrg.add(lineDirection.multiplyScalar(-a1 - root));
return [
{
pt: p1,
thisParam: (-a1 + root) / dirLen,
argParam: circle.GetParamAtPoint(p1)
}, {
pt: p2,
thisParam: (-a1 - root) / dirLen,
argParam: circle.GetParamAtPoint(p2)
}
];
}
return [];
}
//直线和圆
function IntersectLineAndCircle(line, circle, extType, tolerance = 1e-6) {
let ptArr = IntersectLineAndCircleOrArc(line, circle, tolerance);
return CheckPointOnCurve(ptArr, line, circle, extType | IntersectOption.ExtendArg);
}
//直线和圆弧
function IntersectLineAndArc(line, arc, extType, tolerance = 1e-6) {
let ptArr = IntersectLineAndCircleOrArc(line, arc, tolerance);
return CheckPointOnCurve(ptArr, line, arc, extType, tolerance);
}
function IntersectLAndLFor2D2(p1, p2, p3, p4) {
//v1 p1->p2
//v2 p3->p4
//v3 p2->p4
let dx1 = p1.x - p2.x; //v1
let dx2 = p3.x - p4.x; //v2
let dx3 = p4.x - p2.x; //v3
let dy1 = p1.y - p2.y; //v1
let dy2 = p3.y - p4.y; //v2
let dy3 = p4.y - p2.y; //v3
let det = (dx2 * dy1) - (dy2 * dx1); //v1.cross(v2) 叉积 几何意义是平行四边形的面积
let v2Length = Math.sqrt(dx2 * dx2 + dy2 * dy2);
if (equaln$1(det / v2Length, 0.0, 1e-5)) //平行 1e-5是平行四边形的高
{
let det2 = (dx2 * dy3) - (dy2 * dx3);
if (equaln$1(det2 / v2Length, 0.0, 1e-5)) //共线 1e-5是平行四边形的高
return [p1, p2, p3, p4];
return [];
}
let pt = new three.Vector3;
let ratio = ((dx1 * dy3) - (dy1 * dx3)) / det;
pt.x = (ratio * dx2) + p4.x;
pt.y = (ratio * dy2) + p4.y;
return [pt];
}
/**
* 三维中两行之间最短的直线
* ref:https://stackoverflow.com/questions/2316490/the-algorithm-to-find-the-point-of-intersection-of-two-3d-line-segment
* ref:http://paulbourke.net/geometry/pointlineplane/
* ref:http://paulbourke.net/geometry/pointlineplane/calclineline.cs
*
* @export
* @param {Vector3} p1 l1.start
* @param {Vector3} p2 l1.end
* @param {Vector3} p3 l2.start
* @param {Vector3} p4 l2.end
* @returns 交点集合
*/
function ShortestLine3AndLine3(p1, p2, p3, p4, epsilon = 1e-6) {
let p43 = p4.clone().sub(p3);
if (p43.lengthSq() < epsilon)
return;
let p21 = p2.clone().sub(p1);
if (p21.lengthSq() < epsilon)
return;
let p13 = p1.clone().sub(p3);
let d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;
let d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
let d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
let d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
let d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;
let denom = d2121 * d4343 - d4321 * d4321;
if (Math.abs(denom) < epsilon)
return;
let numer = d1343 * d4321 - d1321 * d4343;
let mua = numer / denom;
let mub = (d1343 + d4321 * (mua)) / d4343;
let resultSegmentPoint1 = new three.Vector3();
resultSegmentPoint1.x = p1.x + mua * p21.x;
resultSegmentPoint1.y = p1.y + mua * p21.y;
resultSegmentPoint1.z = p1.z + mua * p21.z;
let resultSegmentPoint2 = new three.Vector3();
resultSegmentPoint2.x = p3.x + mub * p43.x;
resultSegmentPoint2.y = p3.y + mub * p43.y;
resultSegmentPoint2.z = p3.z + mub * p43.z;
return [resultSegmentPoint1, resultSegmentPoint2];
}
//直线和直线
function IntersectLineAndLine(l1, l2, extType, fuzz = 1e-4) {
let [pt1, pt2, pt3, pt4] = [l1.StartPoint, l1.EndPoint, l2.StartPoint, l2.EndPoint];
let ipts;
if (equaln$1(pt1.z, 0, fuzz) && equaln$1(pt2.z, 0, fuzz) && equaln$1(pt3.z, 0, fuzz) && equaln$1(pt4.z, 0, fuzz)) {
ipts = IntersectLAndLFor2D2(pt1, pt2, pt3, pt4);
ipts.sort(ComparePointFnGenerate("xy"));
arrayRemoveDuplicateBySort(ipts, (p1, p2) => equalv3(p1, p2, fuzz));
}
else {
ipts = ShortestLine3AndLine3(pt1, pt2, pt3, pt4);
if (!ipts)
return [];
if (ipts.length === 2)
ipts.pop();
}
let ints = [];
for (let pt of ipts) {
let { closestPt: p1, param: param1 } = l1.GetClosestAtPoint(pt, true);
if (!equalv3(pt, p1, fuzz))
return [];
if (!(extType & IntersectOption.ExtendThis))
if (!(l1.ParamOnCurve(param1, 0) || equalv3(pt1, pt, fuzz) || equalv3(pt2, pt, fuzz)))
return [];
let { closestPt: p2, param: param2 } = l2.GetClosestAtPoint(pt, true);
if (!equalv3(pt, p2, fuzz))
return [];
if (!(extType & IntersectOption.ExtendArg))
if (!(l2.ParamOnCurve(param2, 0) || equalv3(pt3, pt, fuzz) || equalv3(pt4, pt, fuzz)))
return [];
ints.push({ pt, thisParam: param1, argParam: param2 });
}
return ints;
}
function IntersectPolylineAndCurve(pl, cu, extType, tolerance = 1e-6) {
let cus = pl.Explode();
let cus2;
if (cu instanceof exports.Polyline)
cus2 = cu.Explode();
else
cus2 = [cu];
let intRes = [];
for (let i = 0; i < cus.length; i++) {
let cu1 = cus[i];
for (let j = 0; j < cus2.length; j++) {
let cu2 = cus2[j];
let ext = extType;
let isStart = i === 0;
let isEnd = i === cus.length - 1;
let isStart2 = j === 0;
let isEnd2 = j === cus2.length - 1;
//当曲线闭合时,或者当前的子曲线不是起始和不是结束,那么不延伸曲线.
if (pl.CloseMark || !(isStart || isEnd))
ext = ext & ~IntersectOption.ExtendThis;
if ((cu instanceof exports.Polyline && cu.CloseMark) || !(isStart2 || isEnd2))
ext = ext & ~IntersectOption.ExtendArg;
let ptPars = cu1.IntersectWith2(cu2, ext, tolerance).filter(r1 => intRes.every(r2 => !equalv3(r1.pt, r2.pt)));
//校验延伸
if (IntersectOption.ExtendThis & ext) {
//如果曲线是起始又是结束,那么不校验.
if (isStart && isEnd) ;
else if (isStart) {
//暂时没有必要
// let cu1EndPoint = cu1.EndPoint;
// for (let i = 0; i < ptPars.length; i++)
// if (equalv3(ptPars[i].pt, cu1EndPoint, tolerance))
// ptPars[i].thisParam = 1;//如果通过容差测试,我们认为它在终点上
ptPars = ptPars.filter(res => res.thisParam <= 1);
}
else if (isEnd) {
//暂时没有必要
// let cu1StartPoint = cu1.StartPoint;
// for (let i = 0; i < ptPars.length; i++)
// if (equalv3(ptPars[i].pt, cu1StartPoint, tolerance))
// ptPars[i].thisParam = 0;//如果通过容差测试,我们认为它在起点上
ptPars = ptPars.filter(res => res.thisParam >= 0);
}
}
else //当曲线不延伸时,它通过了容差测验,这时我们认为曲线在线上
for (let i = 0; i < ptPars.length; i++)
ptPars[i].thisParam = clamp(ptPars[i].thisParam, 0, 1);
if (IntersectOption.ExtendArg & ext) {
//如果曲线是起始又是结束,那么不校验.
if (isStart2 && isEnd2) ;
else if (isStart2) {
ptPars = ptPars.filter(res => res.argParam + j <= cu2.EndParam);
}
else if (isEnd2) {
ptPars = ptPars.filter(res => res.argParam + j >= 0);
}
}
else //当曲线不延伸时,它通过了容差测验,这时我们认为曲线在线上
for (let i = 0; i < ptPars.length; i++)
ptPars[i].argParam = clamp(ptPars[i].argParam, 0, 1);
intRes.push(...ptPars.map(r => {
return {
pt: r.pt,
thisParam: i + r.thisParam,
argParam: j + r.argParam,
};
}));
}
}
let fn = ComparePointFnGenerate("xyz", tolerance);
intRes.sort((p1, p2) => fn(p1.pt, p2.pt));
arrayRemoveDuplicateBySort(intRes, (p1, p2) => equalv3(p1.pt, p2.pt, tolerance));
return intRes;
}
function IntersectLineAndEllipseFor2D(l, el) {
let elInv = new three.Matrix4().makeRotationZ(-el.Rotation).multiply(el.OCSInv);
let matInv = new three.Matrix4().getInverse(elInv);
let a = el.RadX;
let b = el.RadY;
let sp = l.StartPoint.applyMatrix4(elInv);
let ep = l.EndPoint.applyMatrix4(elInv);
if (!(equaln$1(sp.z, 1e-6) && equaln$1(ep.z, 1e-6))) {
if (equalv2(sp, ep, 1e-6)) //如果与之垂直
{
let p = sp.setZ(0).applyMatrix4(matInv);
if (el.PtOnCurve(p))
return [
{
pt: p,
thisParam: l.GetParamAtPoint(p),
argParam: el.GetParamAtPoint(p)
}
];
}
//todo:求交面
return [];
}
let pts = [];
if (equaln$1(sp.x, ep.x)) {
let c = sp.x;
let j = (b ** 2) * (1 - (c ** 2) / (a ** 2));
if (equaln$1(j, 0)) {
pts = [new three.Vector3(sp.x, 0)];
}
else if (j < 0)
return [];
else {
let y1 = Math.sqrt(j);
let y2 = -Math.sqrt(j);
pts = [
new three.Vector3(c, y1),
new three.Vector3(c, y2)
];
}
}
else {
let k = (sp.y - ep.y) / (sp.x - ep.x);
let c = sp.y - sp.x * k;
let j = (2 * a * a * k * c) * (2 * a * a * k * c) - 4 * (b * b + a * a * k * k) * a * a * (c * c - b * b);
if (equaln$1(j, 0)) {
let x1 = -2 * k * c * a * a / (2 * (b * b + a * a * k * k));
let y1 = k * x1 + c;
pts = [new three.Vector3(x1, y1)];
}
else if (j < 0)
return [];
else {
let x1 = (-2 * k * c * a * a + Math.sqrt(j)) / (2 * (b * b + a * a * k * k));
let y1 = k * x1 + c;
let x2 = (-2 * k * c * a * a - Math.sqrt(j)) / (2 * (b * b + a * a * k * k));
let y2 = k * x2 + c;
pts = [
new three.Vector3(x1, y1),
new three.Vector3(x2, y2)
];
}
}
return pts.map(p => {
let pt = p.applyMatrix4(matInv);
return {
pt,
thisParam: l.GetParamAtPoint(pt),
argParam: el.GetParamAtPoint(pt)
};
});
}
function IntersectEllipseAndCircleOrArc(el, cir, type) {
if (!el.IsCoplaneTo(cir))
return [];
let a = Math.max(el.RadX, el.RadY);
let dist = el.Center.distanceTo(cir.Center);
let disVail = dist > (a + cir.Radius);
if (disVail)
return [];
if (equalv3(el.Center, cir.Center)) {
let a = el.RadX;
let b = el.RadY;
let r = cir.Radius;
let j = ((a * b) ** 2 - (b * r) ** 2) / (a ** 2 - b ** 2);
let pts = [];
if (equaln$1(j, 0) || equaln$1(j, r ** 2)) {
if (equaln$1(j, 0))
pts = [
new three.Vector3(a, 0),
new three.Vector3(-a, 0)
];
else
pts = [
new three.Vector3(0, r),
new three.Vector3(0, -r)
];
}
else if (j < 0)
return [];
else {
let y1 = Math.sqrt(j);
let y2 = -Math.sqrt(j);
let n = r ** 2 - j;
let x1 = Math.sqrt(n);
let x2 = -Math.sqrt(n);
pts = [
new three.Vector3(x1, y1),
new three.Vector3(x1, y2),
new three.Vector3(x2, y1),
new three.Vector3(x2, y2),
];
}
let ro = new three.Matrix4().makeRotationZ(el.Rotation);
let res = pts.map(p => {
let pt = p.applyMatrix4(ro).applyMatrix4(el.OCS);
return {
pt,
thisParam: el.GetParamAtPoint(pt),
argParam: cir.GetParamAtPoint(pt)
};
});
return CheckPointOnCurve(res, el, cir, type);
}
else {
let pts = el.Shape.getPoints(60);
let lineData = pts.map(p => {
return { pt: p, bul: 0 };
});
let pl = new exports.Polyline(lineData);
let cirClone = cir.Clone().ApplyMatrix(el.OCSInv);
if (type === IntersectOption.ExtendBoth)
type = IntersectOption.ExtendArg;
else if (type !== IntersectOption.ExtendArg)
type = IntersectOption.ExtendNone;
let intPts = IntersectPolylineAndCurve(pl, cirClone, type);
intPts.forEach(r => r.pt.applyMatrix4(el.OCS));
return intPts;
}
}
function IntersectEllipse(el1, el2, type) {
if (!el1.IsCoplaneTo(el2))
return [];
let isEqul = equalv3(el1.Center, el2.Center)
&& equaln$1(el1.RadX, el2.RadX)
&& equaln$1(el1.RadY, el2.RadY)
&& equalv3(el1.StartPoint, el2.StartPoint);
if (isEqul)
return [];
let a1 = Math.max(el1.RadX, el1.RadY);
let a2 = Math.max(el2.RadX, el2.RadY);
let dist = el1.Center.distanceToSquared(el2.Center);
if (dist > (a1 + a2) ** 2) {
return [];
}
if (!el1.BoundingBox.intersectsBox(el2.BoundingBox))
return [];
let diffMat = el1.OCSInv.multiply(el2.OCS);
let pts1 = el1.Shape.getPoints(60);
let pts2 = el2.Shape.getPoints(60);
let lineData1 = pts1.map(p => {
return { pt: p, bul: 0 };
});
let lineData2 = pts2.map(p => {
return { pt: p, bul: 0 };
});
let pl1 = new exports.Polyline(lineData1);
let pl2 = new exports.Polyline(lineData2).ApplyMatrix(diffMat);
let intPts = pl1.IntersectWith2(pl2, 0);
intPts.forEach(r => r.pt.applyMatrix4(el1.OCS));
return intPts;
}
var Arc_1;
/**
* 圆弧实体类
* 与ACAD不同,这个类加入了时针变量,并且默认构造的圆弧为顺时针圆弧.
*
* 关于时针圆弧:
* 起始圆弧到终止圆弧总是在0-2PI之间.(一个完整的圆).
* 圆弧的绘制从起始圆弧绘制到终止圆弧. 按照时针绘制.
* 参考计算圆弧的完整角度方法查看该计算方式.
*/
exports.Arc = Arc_1 = class Arc extends Curve {
constructor(center = ZeroVec, radius = 0.1, startAngle = 0.1, endAngle = 0, clockwise = true) {
super();
/**
* 曲线为顺时针
*/
this._Clockwise = true;
this._Matrix.setPosition(center);
this._Radius = radius;
this._StartAngle = clampRad(startAngle);
this._EndAngle = clampRad(endAngle);
this._Clockwise = clockwise;
}
get Shape() {
let sp = new Shape2();
sp.absarc(0, 0, this._Radius, this._StartAngle, this._EndAngle, this._Clockwise);
return sp;
}
get Center() {
return this.Position;
}
set Center(v) {
this.Position = v;
}
get Normal() {
return new three.Vector3().setFromMatrixColumn(this._Matrix, 2);
}
set Normal(v) {
this.WriteAllObjectRecord();
SetMtxVector(this._Matrix, 2, v);
this.Update();
}
get Area() {
return 0.5 * this.AllAngle * this.Radius * this.Radius;
}
//获得曲线的面积,逆时针为正,顺时针为负.
get Area2() {
let clockwise = this._Clockwise ? -1 : 1;
return 0.5 * this.AllAngle * this.Radius * this.Radius * clockwise;
}
get IsClose() {
return false;
}
get BoundingBoxPtsInOCS() {
let pts = [
polar(new three.Vector3(), this._StartAngle, this._Radius),
polar(new three.Vector3(), this._EndAngle, this._Radius),
];
if (this.ParamOnCurve(this.GetParamAtAngle(0)))
pts.push(new three.Vector3(this._Radius, 0));
if (this.ParamOnCurve(this.GetParamAtAngle(Math.PI / 2)))
pts.push(new three.Vector3(0, this._Radius));
if (this.ParamOnCurve(this.GetParamAtAngle(Math.PI)))
pts.push(new three.Vector3(-this._Radius, 0));
if (this.ParamOnCurve(this.GetParamAtAngle(Math.PI * 3 / 2)))
pts.push(new three.Vector3(0, -this._Radius));
return pts;
}
get BoundingBox() {
Arc_1._Z.setFromMatrixColumn(this._Matrix, 2);
Orbit.ComputUpDirection(Arc_1._Z, Arc_1._Y, Arc_1._X);
Arc_1._Mtx.makeBasis(Arc_1._X, Arc_1._Y, Arc_1._Z).setPosition(this._Matrix.elements[12], this._Matrix.elements[13], this._Matrix.elements[14]);
let pts = [
polar(new three.Vector3(), this._StartAngle, this._Radius),
polar(new three.Vector3(), this._EndAngle, this._Radius),
];
let ocsInv = this.OCSInv;
for (let p of [new three.Vector3(this._Radius), new three.Vector3(0, this._Radius), new three.Vector3(-this._Radius), new three.Vector3(0, -this._Radius)]) {
p.applyMatrix4(Arc_1._Mtx).applyMatrix4(ocsInv);
if (this.ParamOnCurve(this.GetParamAtAngle(angle(p))))
pts.push(p);
}
for (let p of pts)
p.applyMatrix4(this.OCSNoClone);
return new Box3Ext().setFromPoints(pts);
}
/**
* 返回对象在自身坐标系下的Box
*/
get BoundingBoxInOCS() {
return new Box3Ext().setFromPoints(this.BoundingBoxPtsInOCS);
}
get Radius() {
return this._Radius;
}
set Radius(v) {
this.WriteAllObjectRecord();
this._Radius = v <= 0 ? 1e-19 : v;
this.Update();
}
get IsClockWise() {
return this._Clockwise;
}
set IsClockWise(v) {
if (v !== this._Clockwise) {
this.WriteAllObjectRecord();
this._Clockwise = v;
this.Update();
}
}
get StartAngle() {
return this._StartAngle;
}
set StartAngle(v) {
// if (equaln(v, this._StartAngle)) return;//优化导致测试用例失败
this.WriteAllObjectRecord();
this._StartAngle = v;
this.Update();
}
get EndAngle() {
return this._EndAngle;
}
set EndAngle(v) {
// if (equaln(v, this._EndAngle)) return;//优化导致测试用例失败
this.WriteAllObjectRecord();
this._EndAngle = v;
this.Update();
}
//******************** Curve function start*****************//
get StartPoint() {
return polar(new three.Vector3(), this._StartAngle, this._Radius).applyMatrix4(this.OCS);
}
set StartPoint(v) {
let vTemp = v.clone().applyMatrix4(this.OCSInv);
this.StartAngle = angle(vTemp);
}
get EndPoint() {
return polar(new three.Vector3(), this._EndAngle, this._Radius).applyMatrix4(this.OCS);
}
set EndPoint(v) {
let vTemp = v.clone().applyMatrix4(this.OCSInv);
this.EndAngle = angle(vTemp);
}
get StartParam() {
return 0;
}
get EndParam() {
return 1;
}
get Length() {
return this.AllAngle * this._Radius;
}
GetParamAtPoint2(pt) {
return this.GetParamAtAngle(this.GetAngleAtPoint(pt));
}
//点在曲线上,已经确定点在曲线的延伸线上
PtOnCurve3(p, fuzz = 1e-6) {
let param = this.GetParamAtPoint2(p);
return this.ParamOnCurve(param, fuzz);
}
ApplyScaleMatrix(m) {
this.WriteAllObjectRecord();
this.Center = this.Center.applyMatrix4(m);
this.Radius = this.Radius * m.getMaxScaleOnAxis();
return this;
}
ApplyMirrorMatrix(m) {
this.WriteAllObjectRecord();
let sp = this.StartPoint;
let ep = this.EndPoint;
reviseMirrorMatrix(this._Matrix);
this._Clockwise = !this._Clockwise;
this.StartPoint = sp;
this.EndPoint = ep;
return this;
}
GetPointAtParam(param) {
let an = this.GetAngleAtParam(param);
return polar(new three.Vector3(), an, this._Radius).applyMatrix4(this.OCSNoClone);
}
GetPointAtDistance(distance) {
let len = this.Length;
if (len === 0)
return;
return this.GetPointAtParam(distance / len);
}
GetDistAtParam(param) {
return Math.abs(param * this.Length);
}
GetDistAtPoint(pt) {
let param = this.GetParamAtPoint(pt);
return this.GetDistAtParam(param);
}
GetParamAtPoint(pt) {
if (this._Radius == 0 ||
this.AllAngle == 0 ||
!equaln$1(pt.distanceTo(this.Center), this._Radius, 1e-6))
return NaN;
return this.GetParamAtAngle(this.GetAngleAtPoint(pt));
}
/**
* 利用角度计算该角度在圆弧中代表的参数.
* 如果角度在圆弧内,那么返回0-1
* 如果角度不在圆弧内,那么尝试返回离圆弧起始或者结束的较近的参数
*
* @param {number} an
* @returns
* @memberof Arc
*/
GetParamAtAngle(an) {
//如果以pt为终点,那么所有的角度为
let ptAllAn = this.ComputeAnlge(an);
let allAn = this.AllAngle;
//减去圆弧角度,剩余角度的一半
let surplusAngleHalf = Math.PI - allAn / 2;
if (ptAllAn > allAn + surplusAngleHalf) //返回负数
return ((ptAllAn - allAn) - (surplusAngleHalf * 2)) / allAn;
else //返回正数
return ptAllAn / allAn;
}
/**
* 根据角度获得参数,不过在这里我们可以指定我们是要获取前面的参数还是后面的参数(正负)
* @param an
* @param [isStart] true:返回负数false 返回正数
* @returns
*/
GetParamAtAngle2(an, isStart = true) {
//如果以pt为终点,那么所有的角度为
let ptAllAn = this.ComputeAnlge(an);
let allAn = this.AllAngle;
//减去圆弧角度,剩余角度的一半
let surplusAngleHalf = Math.PI - allAn / 2;
if (isStart) //返回负数
return ((ptAllAn - allAn) - (surplusAngleHalf * 2)) / allAn;
else //返回正数
return ptAllAn / allAn;
}
GetAngleAtPoint(pt) {
return angle(Arc_1.__PointTemp__.copy(pt).applyMatrix4(this.OCSInv));
}
GetAngleAtParam(param) {
return clampRad(this._StartAngle + param * this.AllAngle * (this._Clockwise ? -1 : 1));
}
GetSplitCurves(param) {
let params = this.SplitParamSort(param);
//角度列表
let ans = params.map(p => this.GetAngleAtParam(p));
//返回圆弧表
let arcs = [];
for (let i = 0; i < ans.length - 1; i++) {
let arc = this.Clone();
arc.ColorIndex = this.ColorIndex;
arc.StartAngle = ans[i];
arc.EndAngle = ans[i + 1];
arcs.push(arc);
}
return arcs;
}
GetOffsetCurves(offsetDist) {
if (this._Clockwise)
offsetDist *= -1;
if ((offsetDist + this._Radius) > 0) {
let arc = this.Clone();
arc.Radius = offsetDist + this._Radius;
return [arc];
}
return [];
}
Extend(newParam) {
this.WriteAllObjectRecord();
if (newParam < 0) {
this._StartAngle = this.GetAngleAtParam(newParam);
}
else if (newParam > 1) {
this._EndAngle = this.GetAngleAtParam(newParam);
}
this.Update();
}
Join(cu) {
if (cu instanceof Arc_1) {
//非常小的圆弧直接结束
if (cu.AllAngle < 5e-6)
return exports.Status.False;
if (equalv3(cu.Center, this.Center) && equaln$1(cu._Radius, this._Radius)) {
this.WriteAllObjectRecord();
let [sa, ea] = [cu.StartAngle, cu.EndAngle];
if (cu._Clockwise != this._Clockwise)
[sa, ea] = [ea, sa];
let allAn = this.AllAngle;
let saAllan = this.ComputeAnlge(sa);
let eaAllan = this.ComputeAnlge(ea);
if (equaln$1(sa, this._StartAngle)) //this起点对起点
{
if (eaAllan > allAn)
this.EndAngle = ea;
return exports.Status.True;
}
else if (equaln$1(sa, this._EndAngle)) //this终点对起点
{
if (eaAllan < allAn || equaln$1(ea, this._StartAngle))
return exports.Status.ConverToCircle;
else
this.EndAngle = ea;
return exports.Status.True;
}
else if (equaln$1(ea, this.StartAngle)) //this起点对终点
{
if (saAllan < allAn)
return exports.Status.ConverToCircle;
else
this.StartAngle = sa;
return exports.Status.True;
}
else if (equaln$1(ea, this._EndAngle)) //this终点对终点
{
if (saAllan > allAn)
this.StartAngle = sa;
return exports.Status.True;
}
else if (this.ParamOnCurve(this.GetParamAtAngle(sa))) {
if (eaAllan < saAllan)
return exports.Status.ConverToCircle;
else if (eaAllan > allAn)
this.EndAngle = ea;
return exports.Status.True;
}
else if (this.ParamOnCurve(this.GetParamAtAngle(ea))) {
this.StartAngle = sa;
return exports.Status.True;
}
//使用按负方向去计算它的参数
let saParam;
if (saAllan > allAn)
saParam = (saAllan - Math.PI * 2) / allAn;
else
saParam = saAllan / allAn;
let eaParam;
if (eaAllan > saAllan && saAllan > allAn)
eaParam = (eaAllan - Math.PI * 2) / allAn;
else
eaParam = eaAllan / allAn;
let pMin = Math.max(0, saParam);
let pMax = Math.min(1, eaParam);
if (pMin <= pMax + 1e-5) {
if (saParam < 0)
this.StartAngle = sa;
if (eaParam > 1)
this.EndAngle = ea;
return exports.Status.True;
}
}
}
return exports.Status.False;
}
Reverse() {
this.WriteAllObjectRecord();
this._Clockwise = !this._Clockwise;
[this._StartAngle, this._EndAngle] = [this._EndAngle, this._StartAngle];
return this;
}
IntersectWith2(curve, intType, tolerance = 1e-4) {
if (curve instanceof Arc_1 || curve.constructor.name === "RoomWallArc") {
return IntersectArcAndArc(this, curve, intType, tolerance);
}
if (curve instanceof exports.Line || curve.constructor.name === "RoomWallLine") {
return SwapParam(IntersectLineAndArc(curve, this, reverseIntersectOption(intType), tolerance));
}
if (curve instanceof exports.Circle) {
return SwapParam(IntersectCircleAndArc(curve, this, reverseIntersectOption(intType), tolerance));
}
if (curve instanceof exports.Polyline)
return SwapParam(IntersectPolylineAndCurve(curve, this, reverseIntersectOption(intType), tolerance));
if (curve instanceof Ellipse)
return SwapParam(IntersectEllipseAndCircleOrArc(curve, this, intType));
return [];
}
/**
* 计算出圆弧所包含的角度
*
* @readonly
* @type {number}
* @memberof Arc
*/
get AllAngle() {
return this.ComputeAnlge(this._EndAngle);
}
get Bul() {
if (equaln$1(this.AllAngle, Math.PI * 2))
return 1;
return Math.tan(this.AllAngle * 0.25) * (this.IsClockWise ? -1 : 1);
}
/**
* 计算所包含的角度
* @param {number} endAngle 结束的角度
*/
ComputeAnlge(endAngle) {
//顺时针
if (this._Clockwise) {
if (this._StartAngle > endAngle)
return this.StartAngle - endAngle;
else //越过0点绘制圆弧
return (Math.PI * 2) - (endAngle - this._StartAngle);
}
else {
if (endAngle > this._StartAngle)
return endAngle - this._StartAngle;
else
return (Math.PI * 2) - (this._StartAngle - endAngle);
}
}
/**
* 解析两点和凸度所构成的圆弧
* ref http://www.lee-mac.com/bulgeconversion.html
* @param {Vector2} p1
* @param {Vector2} p2
* @param {number} bul 凸度,在cad中,凸度为 <(四分之一圆心角)的正切值>
*/
ParseFromBul(p1, p2, bul) {
if (p1 instanceof three.Vector2)
p1 = AsVector3(p1);
if (p2 instanceof three.Vector2)
p2 = AsVector3(p2);
let ocsInv = this.OCSInv;
p1 = p1.clone().applyMatrix4(ocsInv);
p2 = p2.clone().applyMatrix4(ocsInv);
//a (* 2 (atan b))
let a = Math.atan(bul) * 2;
//r (/ (distance p1 p2) 2 (sin a))
let r = p1.distanceTo(p2) / 2 / Math.sin(a);
//c (polar p1 (+ (- (/ pi 2) a) (angle p1 p2)) r)
let c = polar(p1.clone(), Math.PI / 2 - a + angle(p2.clone().sub(p1)), r);
this._Radius = Math.abs(r);
this._StartAngle = angle(p1.sub(c));
this._EndAngle = angle(p2.sub(c));
this._Clockwise = bul < 0;
this.Center = c.applyMatrix4(this.OCSNoClone);
return this;
}
FromThreePoint(pt1, pt2, pt3) {
if (!(pt1 && pt2 && pt3))
return this;
let ocsInv = this.OCSInv;
pt1 = pt1.clone().applyMatrix4(ocsInv).setZ(0);
pt2 = pt2.clone().applyMatrix4(ocsInv).setZ(0);
pt3 = pt3.clone().applyMatrix4(ocsInv).setZ(0);
let center = getCircleCenter(pt1, pt2, pt3);
if (!center) {
this.ParseFromBul(pt1.applyMatrix4(this.OCSNoClone), pt3.applyMatrix4(this.OCSNoClone), 1e-3); //faker line
return this;
}
this.Center = center.clone().applyMatrix4(this.OCS);
//用圆心和其中一个点求距离得到半径:
this._Radius = center.distanceTo(pt1);
//起始角度 端点角度
this._StartAngle = angle(pt1.clone().sub(center));
this._EndAngle = angle(pt3.clone().sub(center));
//求出向量p1->p2,p1->p3
let p1 = pt2.clone().sub(pt1);
let p2 = pt3.clone().sub(pt1);
this._Clockwise = p1.cross(p2).z < 0;
return this;
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
switch (snapMode) {
case ObjectSnapMode.End:
return [this.StartPoint, this.EndPoint];
case ObjectSnapMode.Mid:
return [this.GetPointAtParam(0.5)];
case ObjectSnapMode.Nea:
return getArcOrCirNearPts(this, pickPoint, viewXform)
.filter(p => this.PtOnCurve(p));
case ObjectSnapMode.Ext:
return [this.GetClosestPointTo(pickPoint, true)];
case ObjectSnapMode.Cen:
return [this.Center];
case ObjectSnapMode.Per:
if (lastPoint) {
if (equaln$1(lastPoint.distanceToSquared(this.Center), 0, 1e-10))
return [];
let l = new exports.Line(this.Center, lastPoint);
return l.IntersectWith(this, IntersectOption.ExtendBoth).filter(p => this.PtOnCurve(p));
}
case ObjectSnapMode.Tan:
let pts = GetTanPtsOnArcOrCircle(this, lastPoint);
if (pts)
return pts.filter(p => this.PtOnCurve(p));
}
return [];
}
GetGripPoints() {
return [
this.StartPoint,
this.GetPointAtParam(0.5),
this.EndPoint,
this.Center.clone(),
];
}
MoveGripPoints(indexList, vec) {
if (indexList.length > 0) {
this.WriteAllObjectRecord();
let index = indexList[0];
if (index > 2)
this.Center = this.Center.add(vec);
else {
let p1 = polar(new three.Vector3, this._StartAngle, this._Radius);
let p2 = polar(new three.Vector3, this.GetAngleAtParam(0.5), this._Radius);
let p3 = polar(new three.Vector3, this._EndAngle, this._Radius);
vec = TransformVector(vec.clone(), this.OCSInv).setZ(0);
[p1, p2, p3][index].add(vec);
let center = getCircleCenter(p1, p2, p3);
if (!center) //三点共线 使用faker arc
{
this.ParseFromBul(p1.applyMatrix4(this.OCSNoClone), p3.applyMatrix4(this.OCSNoClone), 1e-3);
this.Update();
return;
}
//起始角度 端点角度
this._StartAngle = angle(p1.clone().sub(center));
this._EndAngle = angle(p3.clone().sub(center));
if (equaln$1(this._StartAngle, this._EndAngle, 1e-5)) //差不多也是三点共线,只不过逃逸了
{
this.ParseFromBul(p1.applyMatrix4(this.OCSNoClone), p3.applyMatrix4(this.OCSNoClone), 1e-3);
this.Update();
return;
}
//用圆心和其中一个点求距离得到半径:
this._Radius = center.distanceTo(p1);
this.Center = center.clone().applyMatrix4(this.OCS);
//求出向量p1->p2,p1->p3
let v1 = p2.clone().sub(p1);
let v2 = p3.clone().sub(p1);
this._Clockwise = v1.cross(v2).z < 0;
this.Update();
}
}
}
GetStretchPoints() {
return [this.StartPoint, this.EndPoint];
}
MoveStretchPoints(indexList, vec) {
if (indexList.length === 0)
return;
this.WriteAllObjectRecord();
if (indexList.length === 2)
this.ApplyMatrix(MoveMatrix(vec));
else
for (let index of indexList) {
let pts = [this.StartPoint, this.EndPoint];
let [sp, ep] = pts;
let oldChordLengthHalf = sp.distanceTo(ep) * 0.5;
let arcHeight = oldChordLengthHalf * this.Bul;
pts[index].add(vec);
let newChordLengthHalf = sp.distanceTo(ep) * 0.5;
let newBul = arcHeight / newChordLengthHalf;
//根据凸度构造新的弧
this.ParseFromBul(sp, ep, newBul);
this.Update();
}
}
GetParamAtDist(d) {
return d / this.Length;
}
GetFirstDeriv(pt) {
let an;
if (typeof pt === "number")
an = this.GetAngleAtParam(pt);
else
an = angle(pt.clone().applyMatrix4(this.OCSInv));
an += Math.PI * 0.5 * (this._Clockwise ? -1 : 1);
let ocs = new three.Matrix4().extractRotation(this.OCS);
return polar(new three.Vector3(), an, this._Radius).applyMatrix4(ocs);
}
GetClosestPointTo(pt, extend) {
pt = pt.clone().applyMatrix4(this.OCSInv);
if (equalv2(pt, ZeroVec, 1e-8))
return this.GetPointAtParam(0);
let a = angle(pt);
let param = this.GetParamAtAngle(a);
if (extend || this.ParamOnCurve(param))
return polar(new three.Vector3, a, this._Radius).applyMatrix4(this._Matrix);
if (param < 0)
return this.GetPointAtParam(0);
else
return this.GetPointAtParam(1);
}
//#region -------------------------File-------------------------
//对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
//对象从文件中读取数据,初始化自身
_ReadFile(file) {
super._ReadFile(file);
let ver = file.Read();
if (ver === 1) {
this.Center = new three.Vector3().fromArray(file.Read());
this.Normal = new three.Vector3().fromArray(file.Read());
}
this._Radius = file.Read();
this._StartAngle = file.Read();
this._EndAngle = file.Read();
this._Clockwise = file.ReadBool();
}
//对象将自身数据写入到文件.
WriteFile(file) {
super.WriteFile(file);
file.Write(2);
file.Write(this._Radius);
file.Write(this._StartAngle);
file.Write(this._EndAngle);
file.WriteBool(this._Clockwise);
}
};
exports.Arc._X = new three.Vector3;
exports.Arc._Y = new three.Vector3;
exports.Arc._Z = new three.Vector3;
exports.Arc._Mtx = new three.Matrix4;
exports.Arc.__PointTemp__ = new three.Vector3;
exports.Arc = Arc_1 = __decorate([
Factory
], exports.Arc);
/**
* 一个简单的计数器实现,本质是使用一个Map来保存元素的个数
*
* 例:
* let count = new Count();
* count.AddCount("Test", 1);
* count.GetCount("Test");//现在 Test 的个数为1
*/
class Count {
constructor() {
this.m_CountMap = new WeakMap();
}
GetCount(obj) {
let count = this.m_CountMap.get(obj);
if (!count) {
this.m_CountMap.set(obj, 0);
count = 0;
}
return count;
}
AddCount(obj, add) {
this.m_CountMap.set(obj, this.GetCount(obj) + add);
}
}
//3点获取圆心
function getCircleCenter(pt1, pt2, pt3) {
if (!(pt1 && pt2 && pt3))
return;
let A1 = pt1.x - pt2.x;
let B1 = pt1.y - pt2.y;
let C1 = (Math.pow(pt1.x, 2) - Math.pow(pt2.x, 2) + Math.pow(pt1.y, 2) - Math.pow(pt2.y, 2)) / 2;
let A2 = pt3.x - pt2.x;
let B2 = pt3.y - pt2.y;
let C2 = (Math.pow(pt3.x, 2) - Math.pow(pt2.x, 2) + Math.pow(pt3.y, 2) - Math.pow(pt2.y, 2)) / 2;
//令temp = A1*B2 - A2*B1
let temp = A1 * B2 - A2 * B1;
let center = new three.Vector3();
//判断三点是否共线
if (equaln$1(temp, 0, 1e-5)) {
return;
}
else {
//不共线则求出圆心:
center.x = (C1 * B2 - C2 * B1) / temp;
center.y = (A1 * C2 - A2 * C1) / temp;
}
return center;
}
//行列式
function getDeterminantFor2V(v1, v2) {
return v1.x * v2.y - v1.y * v2.x;
}
/**
* 曲线根据连接来分组,每组都是一条首尾相连的曲线表.
*
* @export
* @param {Curve[]} cus 传入的分组的曲线表
* @returns {Array<Array<Curve>>} 返回如下
* [
* [c1,c2,c3...],//后面的曲线的起点总是等于上一个曲线的终点
* [c1,c2,c3...],
* ]
*/
function curveLinkGroup(cus, numdimensions = 4) {
//返回的曲线组
let groupCus = new Array();
//将封闭的曲线先提取出来
cus = cus.filter(c => {
let isClose = c.IsClose;
if (isClose)
groupCus.push([c]);
return !isClose;
});
if (cus.length === 0)
return groupCus;
const fuzz = 5 * Math.pow(0.1, numdimensions);
//曲线节点图
let cuMap = new CurveMap(numdimensions);
cus.forEach(c => cuMap.AddCurveToMap(c));
//曲线站点表
let stands = cuMap.Stands;
//曲线使用计数
let cuCount = new Count();
/**
* 从站点的路线中任意取一条,加入到曲线数组中.
*
* @param {Curve[]} cus 已经连接的曲线列表
* @param {boolean} isEndSeach true:从终点搜索,false:从起点搜索
* @returns {Stand} 如果站点中存在可以取得的曲线,返回下个站点,否则返回undefined
*/
function linkCurve(stand, cus, isEndSeach) {
for (let route of stand.routes) {
let cu = route.curve;
if (cuCount.GetCount(cu) === 0) {
if (isEndSeach) {
//保证曲线总是从起点连接到终点
if (!equalv3(cu.StartPoint, stand.position, fuzz))
cu.Reverse();
cus.push(cu);
}
else {
//保证曲线总是从起点连接到终点
if (!equalv3(cu.EndPoint, stand.position, fuzz))
cu.Reverse();
cus.unshift(cu);
}
cuCount.AddCount(cu, 1);
return route.to;
}
}
}
for (let stand of stands) {
let startStand = stand;
let cus = []; //形成合并轮廓的曲线组
while (startStand)
startStand = linkCurve(startStand, cus, true);
if (cus.length > 0) {
startStand = cuMap.GetOnlyVertice(cus[0].StartPoint);
while (startStand)
startStand = linkCurve(startStand, cus, false);
}
if (cus.length > 0)
groupCus.push(cus);
}
return groupCus;
}
function equalCurve(cu1, cu2, tolerance = 1e-4) {
if ((cu1 instanceof exports.Polyline) && (cu2 instanceof exports.Polyline)) {
if (cu1.IsClose !== cu2.IsClose || !isParallelTo(cu1.Normal, cu2.Normal))
return false;
let area1 = cu1.Area2;
let area2 = cu2.Area2;
if (!equaln$1(Math.abs(area1), Math.abs(area2), 0.1))
return false;
let ptsBuls1 = cu1.PtsBuls;
let ptsBuls2 = cu2.PtsBuls;
let pts1 = ptsBuls1.pts;
let pts2 = ptsBuls2.pts;
let buls1 = ptsBuls1.buls;
let buls2 = ptsBuls2.buls;
let isEqualArea = equaln$1(area1, area2, 0.1);
if (!equalv3(cu1.Normal, cu2.Normal)) {
if (isEqualArea) {
pts2.reverse();
buls2.reverse();
buls2.push(buls2.shift());
}
else
buls2 = buls2.map(bul => -bul);
}
else if (!isEqualArea) {
pts2.reverse();
buls2.reverse();
buls2 = buls2.map(bul => -bul);
buls2.push(buls2.shift());
}
if (cu1.IsClose && equalv2(pts1[0], arrayLast(pts1), tolerance)) {
pts1.pop();
buls1.pop();
}
if (cu2.IsClose && equalv2(pts2[0], arrayLast(pts2), tolerance)) {
pts2.pop();
buls2.pop();
}
let cu1Sp = AsVector2(cu1.StartPoint.applyMatrix4(cu2.OCSInv));
let index = pts2.findIndex(p => equalv2(cu1Sp, p, tolerance));
changeArrayStartIndex(buls2, index);
changeArrayStartIndex(pts2, index);
return equalArray(buls1, buls2, equaln$1) &&
equalArray(pts1, pts2, (p1, p2) => equalv3(AsVector3(p1).applyMatrix4(cu1.OCS), AsVector3(p2).applyMatrix4(cu2.OCS), tolerance));
}
else if (cu1 instanceof exports.Circle && cu2 instanceof exports.Circle) {
return equalv3(cu1.Center, cu2.Center) && equaln$1(cu1.Radius, cu2.Radius, 1e-6);
}
else if (cu1 instanceof exports.Arc && cu2 instanceof exports.Arc) {
if (!equalv3(cu1.StartPoint, cu2.EndPoint))
cu1.Reverse();
return equalv3(cu1.Center, cu2.Center)
&& equaln$1(cu1.Radius, cu2.Radius, 1e-6)
&& equaln$1(cu1.StartAngle, cu2.StartAngle)
&& equaln$1(cu1.EndAngle, cu2.EndAngle);
}
else if (cu1 instanceof Ellipse && cu2 instanceof Ellipse) {
return equalv3(cu1.Center, cu2.Center)
&& equaln$1(cu1.RadX, cu2.RadX)
&& equaln$1(cu1.RadY, cu2.RadY)
&& equalv3(cu1.StartPoint, cu2.StartPoint);
}
else if (cu1 instanceof exports.Line && cu2 instanceof exports.Line) {
let ps1 = [cu1.StartPoint, cu1.EndPoint];
let ps2 = [cu2.StartPoint, cu2.EndPoint];
return ps1.every(p => ps2.some(p1 => equalv3(p1, p)));
}
return false;
}
function ConverCircleToPolyline$1(cir) {
//该写法不支持三维坐标系
// let pl = new Polyline();
// let bul = Math.tan(Math.PI * 0.125);
// for (let i = 0; i < 4; i++)
// {
// let p = cir.GetPointAtParam(i * 0.25);
// pl.AddVertexAt(i, Vec3DTo2D(p));
// pl.SetBulgeAt(i, bul);
// }
// pl.CloseMark = true;
// return pl;
let arcs = cir.GetSplitCurves([0, 0.5]); //注意关联封边分裂
let pl = new exports.Polyline();
pl.OCS = cir.OCSNoClone;
pl.Join(arcs[0]);
pl.Join(arcs[1]);
return pl;
}
function GetTanPtsOnArcOrCircle(cu, lastPoint) {
if (lastPoint) {
//ref:wykobi
let ocsInv = cu.OCSInv;
let v = lastPoint.clone().applyMatrix4(ocsInv);
let lengthSq = v.lengthSq();
let radiusSq = cu.Radius ** 2;
if (lengthSq >= radiusSq) {
let ratio = 1 / lengthSq;
let deltaDist = Math.sqrt(lengthSq - radiusSq);
let pts = [
new three.Vector3(cu.Radius * (cu.Radius * v.x - v.y * deltaDist) * ratio, cu.Radius * (cu.Radius * v.y + v.x * deltaDist) * ratio),
new three.Vector3(cu.Radius * (cu.Radius * v.x + v.y * deltaDist) * ratio, cu.Radius * (cu.Radius * v.y - v.x * deltaDist) * ratio),
];
for (let p of pts)
p.applyMatrix4(cu.OCS);
return pts;
}
}
}
function getArcOrCirNearPts(cu, pickPoint, viewXform) {
let viewNormal = new three.Vector3().fromArray(viewXform.elements, 2 * 3);
let plane = new PlaneExt(cu.Normal, cu.Center);
let pickLocal = plane.intersectLine(new three.Line3(pickPoint, pickPoint.clone().add(viewNormal)), new three.Vector3(), true);
if (pickLocal) {
let x = new three.Vector3().fromArray(viewXform.elements, 0).add(pickLocal);
let y = new three.Vector3().fromArray(viewXform.elements, 3).add(pickLocal);
x = plane.intersectLine(new three.Line3(x, x.clone().add(viewNormal)), new three.Vector3(), true);
y = plane.intersectLine(new three.Line3(y, y.clone().add(viewNormal)), new three.Vector3(), true);
let lx = new exports.Line(pickLocal, x);
let ly = new exports.Line(pickLocal, y);
let ins = cu.IntersectWith(lx, IntersectOption.ExtendBoth);
ins.push(...cu.IntersectWith(ly, IntersectOption.ExtendBoth));
return ins;
}
else {
let ptLocal = plane.projectPoint(pickPoint, new three.Vector3());
let lz = new exports.Line(ptLocal, ptLocal.clone().add(viewNormal));
return cu.IntersectWith(lz, IntersectOption.ExtendBoth);
}
}
function getTanPtsOnEllipse(cu, lastPoint) {
return [];
}
/**
* 判断多段线是不是矩形
* 因为用户画的垃圾图,所以我们会给容差
* 1.简化点表成4个点
* -得到x向量,构建二维旋转矩阵
* -所有的点旋转
* 2.构建box
* 3.4个点都在盒子里,面积是矩形
* @param cu
*/
function IsRect(cu) {
if (cu instanceof exports.Polyline) {
//如果不封闭(就不是矩形)
if (!cu.IsClose)
return;
//如果点个数小于4(就不是矩形)
if (cu.LineData.length < 4)
return;
//如果有圆弧(就不是矩形)
for (let i = 0; i < cu.LineData.length; i++) {
let d = cu.LineData[i];
if (equaln$1(d.bul, 0, BUL_IS_LINE_FUZZ))
continue;
let next = FixIndex$1(i + 1, cu.LineData);
if (equalv2(d.pt, cu.LineData[next].pt))
continue;
return;
}
let pts2d = cu.LineData.map(d => d.pt);
//去除重复点
arrayRemoveDuplicateBySort(pts2d, (p1, p2) => equalv2(p1, p2));
if (equalv2(pts2d[0], pts2d[pts2d.length - 1]))
pts2d.pop();
//这里我们判断它是不是有4个90度的角,并且有4个点
let preV = pts2d[0].clone().sub(pts2d[pts2d.length - 1]).negate(); //preVector
let preL = preV.length(); //preLength
let nowV = new three.Vector2; //nowVector
let crossV = 0; //永远相同方向的90度,如果不是(就不是矩形)
let pts4 = []; //简化成4个点
for (let i = 0; i < pts2d.length; i++) {
nowV.subVectors(pts2d[FixIndex$1(i + 1, pts2d.length)], pts2d[i]);
let cross = preV.cross(nowV) / preL;
let nowL = nowV.length(); //nowLength
if (equaln$1(cross, 0, 0.01)) //平行 此时的cross = 三角形的高(其中preL是三角形的底边) 我们认为它移动了0.01是可以接受的
continue; //TODOX:我们可能要合并这条线? 把preV preL更新一下?
cross /= nowL; //此时的cross = sin@
//如果不等于90度(就不是矩形)
if (!equaln$1(Math.abs(cross), 1, 1e-5))
return;
cross = Math.sign(cross);
if (!crossV)
crossV = cross;
else if (crossV !== cross) //如果方向不一致(没有绕着90度)(就不是矩形)
return;
pts4.push(pts2d[i]);
if (pts4.length > 4) //如果超过4个点(就不是矩形)
return;
preL = nowL;
preV.copy(nowV).negate(); //翻转它 以便下一次计算
}
if (pts4.length !== 4 || !crossV) //没有90度 (就不是矩形)
return;
let rectOCS;
preV.subVectors(pts4[1], pts4[0]);
let box = new three.Box2;
if (equaln$1(preV.x, 0, 1e-3) || equaln$1(preV.y, 0, 1e-3)) //判断是不是与X轴平行或者与Y轴平行,精度容差在0.001 看起来没问题
{
rectOCS = cu.OCS;
box.setFromPoints(pts4);
}
else //如果矩形不与X轴平行,我们旋转这个点表,然后变换它
{
let a = Math.atan2(preV.y, preV.x);
let r = new Matrix2().setRotate(-a);
let p0 = pts4[0];
pts4 = pts4.map(p => {
p = p.clone().sub(p0);
r.applyVector(p);
return p;
});
box.setFromPoints(pts4);
nowV.set(-preV.y, preV.x); //旋转90度
rectOCS = new three.Matrix4().makeBasis(AsVector3(preV.normalize()), AsVector3(nowV.normalize()), ZAxis).setPosition(p0.x, p0.y, 0);
rectOCS.multiplyMatrices(cu.OCSNoClone, rectOCS);
}
//4个点都在角上
if (!pts4.every(p => {
return (equaln$1(p.x, box.min.x, 0.01) || equaln$1(p.x, box.max.x, 0.01))
&& (equaln$1(p.y, box.min.y, 0.01) || equaln$1(p.y, box.max.y, 0.01));
}))
return;
let size = box.getSize(new three.Vector2);
return {
size: AsVector3(size),
box: new three.Box3(AsVector3(box.min), AsVector3(box.max)),
OCS: rectOCS,
};
}
}
function MergeCurvelist(cus) {
arrayRemoveIf(cus, c => c.Length < LINK_FUZZ);
let cir;
arrayRemoveDuplicateBySort(cus, (c1, c2) => {
if (cir)
return true;
let status = c1.Join(c2, false, LINK_FUZZ);
if (status === exports.Status.ConverToCircle) {
let arc = c1;
cir = new exports.Circle(arc.Center, arc.Radius);
return true;
}
return status === exports.Status.True;
});
if (cir) {
cus.length = 0;
cus.push(cir);
}
return cus;
}
function SwapParam(res) {
for (let r of res)
[r.thisParam, r.argParam] = [r.argParam, r.thisParam];
return res;
}
function ComputerCurvesNormalOCS(curves, allowAutoCalc = true) {
if (!curves || curves.length === 0)
return;
const IsNorZeroVector = (v) => v && !equalv3(v, ZeroVec, 1e-3);
//准备计算多段线的法向量
let normal;
let firstV;
for (let c of curves) {
if (c instanceof exports.Arc || c instanceof exports.Circle) {
normal = c.Normal;
break;
}
else if (firstV) {
let v = c.GetFirstDeriv(0);
if (IsNorZeroVector(v)) {
v.normalize().cross(firstV);
if (IsNorZeroVector(v)) //避免平行向量
{
normal = v.normalize();
break;
}
}
}
else {
let cus = c.Explode();
let ocs = ComputerCurvesNormalOCS(cus, false);
if (ocs)
return ocs;
let fv = c.GetFirstDeriv(0);
if (IsNorZeroVector(fv)) //先判断零向量
firstV = fv.normalize(); //再归一化
}
}
if (!normal && !allowAutoCalc)
return;
let x = new three.Vector3();
let y = new three.Vector3();
if (!normal) {
if (!firstV)
return curves[0].OCS;
normal = firstV.normalize();
Orbit.ComputUpDirection(normal, y, x);
[x, y, normal] = [normal, x, y];
}
else {
if (equalv3(normal, curves[0].Normal.negate(), 1e-5))
normal.negate();
Orbit.ComputUpDirection(normal, y, x);
}
return new three.Matrix4().makeBasis(x, y, normal).setPosition(curves[0].StartPoint);
}
function Pts2Polyline(pts, isClose) {
let pl = new exports.Polyline();
for (let i = 0; i < pts.length; i += 2) {
let p1 = AsVector3(pts[i]);
let arc;
let p2;
let p3;
if (isClose) {
p2 = AsVector3(pts[FixIndex$1(i + 1, pts.length)]);
p3 = AsVector3(pts[FixIndex$1(i + 2, pts.length)]);
}
else {
if (i >= pts.length - 2)
break;
p2 = AsVector3(pts[i + 1]);
p3 = AsVector3(pts[i + 2]);
}
let v1 = p1.clone().sub(p2);
let v2 = p2.clone().sub(p3);
if (equaln$1(v1.angleTo(v2), 0))
arc = new exports.Line(p1, p3);
else
arc = new exports.Arc().FromThreePoint(p1, p2, p3);
pl.Join(arc);
}
return pl;
}
/**
*勿随意更改属性名,若更改,需更改对应UI模态框的属性和检验方法的key
*
*/
//排钻类型
var DrillType;
(function (DrillType) {
DrillType["Yes"] = "\u6392";
DrillType["None"] = "\u4E0D\u6392";
DrillType["More"] = "**\u591A\u79CD**";
DrillType["Invail"] = "\u65E0\u6548\u914D\u7F6E";
})(DrillType || (DrillType = {}));
//偏心轮类型
// 左右侧板 Font朝向柜内 Back朝向柜外
// 顶底板 Font朝向柜外 Back两面朝下 Inside朝向柜内
var FaceDirection;
(function (FaceDirection) {
FaceDirection[FaceDirection["Front"] = 0] = "Front";
FaceDirection[FaceDirection["Back"] = 1] = "Back";
FaceDirection[FaceDirection["Inside"] = 2] = "Inside";
})(FaceDirection || (FaceDirection = {}));
var LogType;
(function (LogType) {
LogType["Error"] = "ERROR";
LogType["Warning"] = "WARNING";
LogType["Info"] = "INFO";
LogType["Command"] = "COMMAND";
LogType["All"] = "ALL";
})(LogType || (LogType = {}));
const _LogInjectFunctions = [];
//Log(`板:${br.Name}没有提供排钻信息!`, LogType.Warning, [br]);
function Log(message, ...optionalParams) {
for (let f of _LogInjectFunctions)
f(message, ...optionalParams);
}
const _LogInjectInteractionFunctions = [];
//InteractionLog([{ msg: "警告:" }, { msg: `板件${br.Name}`, entity: [br, cyHole] }, { msg: "侧孔与板无交点,无法加工该侧孔!" }], LogType.Warning);
function InteractionLog(message, ...optionalParams) {
for (let f of _LogInjectInteractionFunctions)
f(message, ...optionalParams);
}
let instanceMap = new Map();
/**
* 构造单例类的静态类.
* # Example:
* class A extends Singleton(){};
* //获得单例
* let a = A.GetInstance();
*/
class Singleton {
constructor() { }
//ref:https://github.com/Microsoft/TypeScript/issues/5863
static GetInstance() {
if (instanceMap.has(this))
return instanceMap.get(this);
//@ts-ignore
let __instance__ = new this.prototype.constructor();
instanceMap.set(this, __instance__);
return __instance__;
}
}
function ScaleUV(geo, scale = 1e-3) {
for (let uvsg of geo.faceVertexUvs) {
for (let uvs of uvsg) {
for (let uv of uvs) {
uv.multiplyScalar(scale);
}
}
}
}
function ScaleUV2(geo, ocs, xScale = 1e-3, yScale = 1e-3, isInvert = false) {
for (let uvsg of geo.faceVertexUvs) {
for (let uvs of uvsg) {
for (let uv of uvs) {
let p = new three.Vector3(uv.x, uv.y).applyMatrix4(ocs);
uv.x = p.x;
uv.y = p.y;
if (isInvert) {
uv.x /= yScale;
uv.y /= xScale;
}
else {
uv.x /= xScale;
uv.y /= yScale;
}
}
}
}
}
class Shape {
constructor(_Outline = new Contour, _Holes = []) {
this._Outline = _Outline;
this._Holes = _Holes;
}
get Outline() {
return this._Outline;
}
get Holes() {
return this._Holes;
}
get Area() {
let outlineArea = this._Outline.Area;
let holeArea = this._Holes.map(l => l.Area).reduce((a1, a2) => a1 + a2, 0);
return outlineArea - holeArea;
}
get BoundingBox() {
return this._Outline.BoundingBox;
}
set Outline(con) {
this._Outline = con;
}
set Holes(holes) {
this._Holes = holes;
}
get Shape() {
let shape = this.Outline.Shape;
for (let h of this._Holes) {
if (h.Curve instanceof exports.Polyline)
h.Curve.UpdateOCSTo(this.Outline.Curve.OCS);
if (h.Curve instanceof exports.Circle) {
let sp = new three.Path();
let cen = h.Curve.Center.applyMatrix4(this.Outline.Curve.OCSInv);
sp.ellipse(cen.x, cen.y, h.Curve.Radius, h.Curve.Radius, 0, 2 * Math.PI, false, 0);
shape.holes.push(sp);
}
else
shape.holes.push(h.Shape);
}
return shape;
}
get Position() {
return this._Outline.Curve.Position;
}
set Position(p) {
let vec = p.clone().sub(this._Outline.Curve.Position);
this._Outline.Curve.Position = p;
for (let h of this._Holes)
h.Curve.Position = h.Curve.Position.add(vec);
}
Z0() {
this._Outline.Curve.Z0();
for (let h of this._Holes)
h.Curve.Z0();
return this;
}
MatrixPlanarizere() {
this._Outline.Curve.MatrixPlanarizere();
for (let h of this._Holes)
h.Curve.MatrixPlanarizere();
}
ApplyMatrix(m) {
this._Outline.Curve.ApplyMatrix(m);
this._Holes.forEach(h => h.Curve.ApplyMatrix(m));
return this;
}
ApplyScaleMatrix(m) {
let cu = this.Outline.Curve;
let cus = this._Holes.map(h => h.Curve);
cus.unshift(cu);
for (let c of cus) {
c.ApplyMatrix(c.OCS);
c.ApplyMatrix(m);
c.ApplyMatrix(c.OCSInv);
}
return this;
}
Explode() {
let cus = [];
let contours = [this._Outline, ...this._Holes];
for (let con of contours) {
if (con.Curve instanceof exports.Polyline)
cus.push(...con.Curve.Explode());
else
cus.push(con.Curve.Clone());
}
return cus;
}
Clone() {
let shape = new Shape();
shape.Outline = this._Outline.Clone();
shape.Holes = this.Holes.map(h => h.Clone());
return shape;
}
SetColor(color) {
this._Outline.Curve.ColorIndex = color;
this._Holes.forEach(h => h.Curve.ColorIndex = color);
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
switch (snapMode) {
case ObjectSnapMode.End:
return this.GetStretchPoints();
case ObjectSnapMode.Mid:
case ObjectSnapMode.Cen:
case ObjectSnapMode.Nea:
case ObjectSnapMode.Ext:
case ObjectSnapMode.Per:
case ObjectSnapMode.Tan:
{
let cus = [this._Outline.Curve];
for (let h of this._Holes) {
cus.push(h.Curve);
}
let pts = [];
for (let c of cus) {
pts.push(...c.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform));
}
return pts;
}
}
return [];
}
GetGripPoints() {
let pts = this.Outline.Curve.GetGripPoints();
for (let h of this._Holes) {
pts.push(...h.Curve.GetGripPoints());
}
return pts;
}
MoveGripPoints(indexList, vec) {
let i = indexList[0];
let outlineIndex = this._Outline.Curve.GetGripPoints().length;
let cu = this._Outline.Curve;
if (i >= outlineIndex) {
for (let h of this._Holes) {
let len = h.Curve.GetGripPoints().length;
if (indexList[0] < outlineIndex + len) {
indexList = [indexList[0] - outlineIndex];
cu = h.Curve;
break;
}
outlineIndex += len;
}
}
cu.MoveGripPoints(indexList, vec);
}
GetStretchPoints() {
let pts = this.Outline.Curve.GetStretchPoints();
for (let h of this._Holes) {
pts.push(...h.Curve.GetStretchPoints());
}
return pts;
}
MoveStretchPoints(indexList, vec) {
let outlen = 0;
for (let cu of [this._Outline.Curve, ...this._Holes.map(h => h.Curve)]) {
let count = cu.GetStretchPoints().length;
let refIndex = outlen + count;
let curIndexs = [];
while (indexList.length) {
if (indexList[0] < refIndex)
curIndexs.push(indexList.shift() - outlen);
else
break;
}
cu.MoveStretchPoints(curIndexs, vec);
if (indexList.length === 0)
break;
outlen += count;
}
}
//交集 如果成功返回一个面域 失败返回0个
IntersectionBoolOperation(targetShape) {
// TestDraw(this._Outline.Curve.Clone()); //测试代码
// TestDraw(targetShape._Outline.Curve.Clone());
let resOutlines = this._Outline.IntersectionBoolOperation(targetShape._Outline);
let cus = this.targetOutlineSubHoleOutline(resOutlines, Shape.mergeContours([...this._Holes, ...targetShape._Holes]));
return Shape.pairHoleAndOutline(cus);
}
//并集,如果成功返回1个形状,不成功返回2个形状
UnionBoolOperation(targetShape, checkIntersect = false) {
if (checkIntersect && !this.BoundingBox.intersectsBox(targetShape.BoundingBox, 1e-3))
return [this, targetShape];
let { contours, holes } = this._Outline.UnionBoolOperation(targetShape._Outline);
let shapes = [];
//提取出所有的孔洞, 目标线段孔洞和原线段差,如果孔洞和目标相减后有被包围轮廓,应把这个单独提取出来作为形状
let unionHoles = [];
//合并运算时提取出运算后的孔洞和形状
const pickUpHoleOrShape = (srcHoles, tarHoles, outline) => {
srcHoles.forEach(cu => {
let tmpContours = cu.SubstactBoolOperation(outline).sort((a, b) => b.Area - a.Area); //面积从大到校
let isAllContainered = tmpContours.length > 1 && tmpContours.slice(1).every((cu, index) => tmpContours[0].ContainerCurve(cu.Curve, true));
//洞是否被最大的洞包含,是,则把被包含的洞都提取出来加入形状数组
if (isAllContainered) {
shapes.push(...this.targetOutlinesSubHoles(tmpContours.slice(1).map(c => new Shape(c)), tarHoles.map(c => new Shape(c))));
}
else
unionHoles.push(...tmpContours);
});
};
pickUpHoleOrShape(targetShape._Holes, this._Holes, this._Outline);
pickUpHoleOrShape(this._Holes, targetShape._Holes, targetShape._Outline);
targetShape._Holes.forEach(cu => {
this._Holes.forEach(c => {
unionHoles.push(...c.IntersectionBoolOperation(cu));
});
});
shapes.push(...this.targetOutlinesSubHoles(contours.map(c => new Shape(c, holes)), unionHoles.map(c => new Shape(c))));
return shapes;
}
/**
* 如果完全被减掉,就返回0个.其他的返回1个或者n个
* @param targetShapes 已经是合并后的形状数组
*/
SubstactBoolOperation(targetShapes) {
let originOutline = this.Outline;
let targetOutlines = targetShapes.map(s => s.Outline);
const { holes, outlines } = originOutline.GetSubtractListByMoreTargets(targetOutlines);
holes.push(...this.Holes);
let newShapes = [];
if (outlines.length === 1 && equaln$1(outlines[0].Area, originOutline.Area)) {
newShapes = [new Shape(outlines[0], Shape.mergeContours(holes))];
}
else if (holes.length === 0) {
newShapes = outlines.map(o => new Shape(o));
}
else {
for (let outline of outlines)
newShapes.push(...new Shape(outline).SubstactBoolOperation(holes.map(h => new Shape(h))));
}
let holeShape = this.Holes.map(h => new Shape(h));
for (let target of targetShapes) {
let tmpInterList = [];
if (target.Holes.length === 0)
continue;
for (let hole of target.Holes) {
let list = hole.IntersectionBoolOperation(originOutline);
tmpInterList.push(...list);
}
for (let ot of tmpInterList) {
let subShapes = [];
subShapes.push(...holeShape);
for (let t of targetShapes) {
if (t !== target)
subShapes.push(new Shape(t.Outline));
}
newShapes.push(...new Shape(ot).SubstactBoolOperation(subShapes));
}
}
return newShapes;
}
Equal(targetShape) {
if (this._Outline.Equal(targetShape._Outline)) {
return this._Holes.length === targetShape._Holes.length
&& this._Holes.every(h1 => targetShape._Holes.some(h2 => h1.Equal(h2)));
}
return false;
}
targetOutlinesSubHoles(targetShapes, holeShapes) {
let resultShapes = [];
for (let ts of targetShapes) {
let res = ts.SubstactBoolOperation(holeShapes);
resultShapes.push(...res);
}
return resultShapes;
}
/**
* 目标轮廓减去洞
*
* @private
* @param {Contour[]} tarContours 轮廓列表
* @param {Contour[]} holes 洞列表
* @returns {Contour[]} 新的轮廓列表
* @memberof Shape
*/
targetOutlineSubHoleOutline(tarContours, holes) {
if (!holes.length)
return tarContours;
let resultContours = [];
for (let minuendContour of tarContours) {
//需要被差集的形状列表
let tmpContour = [minuendContour];
for (let hole of holes) {
//缓存差集生成的轮廓
let tmps = [];
tmpContour.forEach(r => {
let cus = r.SubstactBoolOperation(hole);
tmps.push(...cus);
});
tmpContour = tmps; //使用新生成的进行下一轮计算
}
resultContours.push(...tmpContour);
}
return resultContours;
}
//整理轮廓数组,匹配洞和外轮廓
static pairHoleAndOutline(contours) {
let shapes = [];
contours.sort((a, b) => b.Area - a.Area);
while (contours.length) {
//洞列表
let tmpHoles = [];
let outline = contours.shift();
//取出包含的洞
arrayRemoveIf(contours, (con) => {
let bisIn = outline.ContainerCurve(con.Curve, true);
if (bisIn)
tmpHoles.push(con);
return bisIn;
});
let holes = Shape.removeBeContaineredHoles(tmpHoles);
shapes.push(new Shape(outline, holes));
}
return shapes;
}
/**
* 合并洞,本质是使用(并集算法)将可以并集的洞合并在一起,减少洞的数量.
* canSidewipe 用于走刀,擦边的,包含的,是否合并
*/
static mergeContours(holes, canSidewipe = true) {
if (holes.length <= 1)
return holes;
let rets = []; //返回的合并轮廓
let cache = new Map();
while (holes.length > 0) {
let c = holes.shift(); //取第一个
let b1 = cache.get(c);
if (!b1) {
b1 = c.BoundingBox;
cache.set(c, b1);
}
while (true) {
//剩余的 不相交的形状表 remaining
let remHoles = holes.filter(ic => {
let b2 = cache.get(ic);
if (!b2) {
b2 = ic.BoundingBox;
cache.set(ic, b2);
}
if (!IntersectBox2(b1, b2))
return true;
let unions = c.UnionBoolOperation(ic);
if (unions.holes.length > 0) {
console.warn("未知情况"); //qiannianzhou_lvzhijia.test.ts触发这个 本质是轮廓+轮廓会产生新的洞!
//TODO: 这是个BUG
// let f = new CADFiler;
// f.Write(3);
// c.Curve.ColorIndex = 1;
// f.WriteObject(c.Curve);
// f.WriteObject(ic.Curve);
// ic.Curve.ColorIndex = 2;
// f.WriteObject(unions.holes[0].Curve);
// unions.holes[0].Curve.ColorIndex = 3;
// copyTextToClipboard(f.ToString());
}
if (unions.contours.length === 1) //并集成功
{
if (!canSidewipe) {
if (equaln$1(c.Area + ic.Area, unions.contours[0].Area, 0.1))
return true;
if (equaln$1(unions.contours[0].Area, Math.max(c.Area, ic.Area), 0.1))
return true;
}
c = unions.contours[0]; //更新c
b1 = c.BoundingBox;
cache.set(c, b1);
}
return unions.contours.length !== 1; //过滤出并集失败的形状
});
//如果c和剩余的轮廓都不相交,那么退出
if (remHoles.length === holes.length) {
rets.push(c); //c已经是一个独立的轮廓,不和任意轮廓相交(不能合并了)
break; //退出循环.下一个
}
else
holes = remHoles; //更新为剩下的轮廓列表
}
}
return rets;
}
/**
* 移除被包含的洞.(移除无效的洞,已经被更大的洞包含)
*
* @private
* @param {Contour[]} tmpHoles 洞列表
* @returns {Contour[]} 返回的洞列表都不会互相包含.
* @memberof Shape
*/
static removeBeContaineredHoles(tmpHoles) {
let holes = [];
if (tmpHoles.length <= 1)
return tmpHoles;
tmpHoles.sort((a, b) => b.Area - a.Area); //面积从大到小排序
while (tmpHoles.length) {
let srcHole = tmpHoles.shift();
holes.push(srcHole);
//移除包含的洞
arrayRemoveIf(tmpHoles, h => srcHole.ContainerCurve(h.Curve, true));
}
return holes;
}
//读写文件
ReadFile(file) {
file.Read(); //1
this._Outline = Contour.CreateContour([file.ReadObject()]);
let count = file.Read();
for (let i = 0; i < count; i++) {
this._Holes.push(Contour.CreateContour([file.ReadObject()]));
}
}
//对象将自身数据写入到文件.
WriteFile(file) {
file.Write(1); //ver
file.WriteObject(this._Outline.Curve);
file.Write(this._Holes.length);
this._Holes.forEach(h => file.WriteObject(h.Curve));
}
}
class ShapeManager {
constructor(_ShapeList = []) {
this._ShapeList = _ShapeList;
}
get ShapeList() {
return this._ShapeList.slice();
}
get ShapeCount() {
return this._ShapeList.length;
}
get ShapeArea() {
return this._ShapeList.map(s => s.Area).reduce((a1, a2) => a1 + a2, 0);
}
AppendShapeList(shapes) {
Array.isArray(shapes) ? this._ShapeList.push(...shapes) : this._ShapeList.push(shapes);
return this;
}
Clear() {
this._ShapeList.length = 0;
}
BoolOper(otherMg, booltype) {
switch (booltype) {
case exports.BoolOpeartionType.Intersection:
return this.IntersectionBoolOperation(otherMg);
case exports.BoolOpeartionType.Union:
return this.UnionBoolOperation(otherMg);
case exports.BoolOpeartionType.Subtract:
return this.SubstactBoolOperation(otherMg);
}
}
//交集 如果成功返回一个面域 失败返回0个
IntersectionBoolOperation(target) {
let shapes = [];
for (let srcShape of this._ShapeList) {
for (let tarShape of target._ShapeList) {
let tmpShapes = srcShape.IntersectionBoolOperation(tarShape);
shapes.push(...tmpShapes);
}
}
this.Clear();
this._ShapeList = shapes;
return this._ShapeList.length > 0;
}
//并集,如果有一个形状并集成功,就成功
UnionBoolOperation(targetMg) {
let isSuccess = false;
let srcShapes = this._ShapeList;
let tarShapes = targetMg._ShapeList;
let alones = []; //孤立的形状
const boxCache = new WeakMap();
for (let src of srcShapes) {
let notUnions = []; //未被合并的形状列表 来自tarShapes
let srcBox = src.BoundingBox;
for (let tar of tarShapes) {
let tarBox = boxCache.get(tar);
if (!tarBox) {
tarBox = tar.BoundingBox;
boxCache.set(tar, tarBox);
}
if (!IntersectBox2(srcBox, tarBox)) {
notUnions.push(tar);
continue;
}
let unions = src.UnionBoolOperation(tar);
if (unions.length === 1) //并集成功
{
isSuccess = true;
src = unions[0]; //src设置为 合并完的形状
}
else //并集失败
notUnions.push(tar); //设置为未计算
}
//如果发现src和任意一个形状并集成功,那么
if (notUnions.length !== tarShapes.length) {
notUnions.push(src); //加入src 进行下一轮
tarShapes = notUnions;
}
else
alones.push(src); //它是孤独的一个形状
}
this._ShapeList = alones.concat(tarShapes);
return isSuccess;
}
SubstactBoolOperation(target) {
let newShapes = [];
for (let s of this._ShapeList) {
let ss = s.SubstactBoolOperation(target.ShapeList);
newShapes.push(...ss);
}
this._ShapeList = newShapes;
return true;
}
/**
* 与region.ApplyMatrix不同的是,这个是直接操作内部对象.
* 通常用来计算布尔运算时需要真实的移动这个位置.
* 并且将不会刷新显示
*
* @param {Matrix4} mat4
* @memberof ShapeManager
*/
ApplyMatrix(mat4) {
for (let s of this._ShapeList) {
s.Outline.Curve.ApplyMatrix(mat4);
s.Holes.forEach(o => o.Curve.ApplyMatrix(mat4));
}
}
ReadFile(file) {
file.Read(); //1
let cout = file.Read();
for (let i = 0; i < cout; i++) {
let obj = new Shape();
obj.ReadFile(file);
this._ShapeList.push(obj);
}
}
WriteFile(file) {
file.Write(1); //ver
file.Write(this.ShapeList.length);
for (let s of this.ShapeList) {
s.WriteFile(file);
}
}
}
var Region_1;
let Region = Region_1 = class Region extends Entity {
static CreateFromCurves(cus) {
let shapes = Contour.GetAllContour(cus).map(out => new Shape(out));
if (shapes.length > 0) {
let reg = new Region_1();
//MarkX:曲线同面域一起移动
reg.ApplyMatrix(shapes[0].Outline.Curve.OCS);
reg.ShapeManager.AppendShapeList(shapes);
return reg;
}
}
constructor(_ShapeManager = new ShapeManager()) {
super();
this._ShapeManager = _ShapeManager;
}
//如果需要修改获取到的属性,需要Clone后进行操作,否则会对原实体进行破坏
get ShapeManager() {
return this._ShapeManager;
}
get Area() {
return this.ShapeManager.ShapeArea;
}
get BoundingBox() {
let box = new three.Box3();
for (let s of this._ShapeManager.ShapeList)
box.union(s.BoundingBox);
return box;
}
Explode() {
let shapeList = this._ShapeManager.ShapeList;
if (shapeList.length <= 1) {
return shapeList[0].Explode();
}
else {
let regs = [];
shapeList.forEach(s => {
let reg = new Region_1().ApplyMatrix(this.OCS);
reg.ShapeManager.AppendShapeList(s);
regs.push(reg);
});
return regs;
}
}
/**
* 对于布尔操作,这个将会变换内部轮廓到对方坐标系.
* 并且这个变换不会更新图形绘制.
* @param {Matrix4} m
* @memberof Region
*/
ShapeApplyMatrix(m) {
this.WriteAllObjectRecord();
this._ShapeManager.ApplyMatrix(m);
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
switch (snapMode) {
case ObjectSnapMode.End:
return this.GetGripPoints();
case ObjectSnapMode.Mid:
case ObjectSnapMode.Cen:
case ObjectSnapMode.Nea:
case ObjectSnapMode.Ext:
case ObjectSnapMode.Per:
case ObjectSnapMode.Tan:
{
let pts = [];
for (let s of this._ShapeManager.ShapeList) {
pts.push(...s.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform));
}
return pts;
}
}
return [];
}
GetGripPoints() {
let pts = [];
for (let s of this._ShapeManager.ShapeList)
pts.push(...s.GetStretchPoints());
return pts;
}
MoveGripPoints(indexList, moveVec) {
this.WriteAllObjectRecord();
let moveVLoc = moveVec.clone().applyMatrix4(new three.Matrix4().extractRotation(this.OCSInv));
this.ApplyMatrix(MoveMatrix(moveVLoc));
}
ApplyMatrix(m) {
this.WriteAllObjectRecord();
//面域移动,组成面域的曲线也要移动 MarkX:曲线同面域一起移动
this._ShapeManager.ShapeList.forEach(s => s.ApplyMatrix(m));
return super.ApplyMatrix(m);
}
get Position() {
return super.Position;
}
set Position(pt) {
this.WriteAllObjectRecord();
let moveX = pt.x - this._Matrix.elements[12];
let moveY = pt.y - this._Matrix.elements[13];
let moveZ = pt.z - this._Matrix.elements[14];
this._Matrix.setPosition(pt);
this._SpaceOCS.elements[12] += moveX;
this._SpaceOCS.elements[13] += moveY;
this._SpaceOCS.elements[14] += moveZ;
let m = new three.Matrix4().setPosition(moveX, moveY, moveZ);
for (let s of this.ShapeManager.ShapeList)
s.ApplyMatrix(m);
this.Update(exports.UpdateDraw.Matrix);
}
ApplyScaleMatrix(m) {
this.WriteAllObjectRecord();
for (let s of this._ShapeManager.ShapeList)
s.ApplyScaleMatrix(m);
this.Update(exports.UpdateDraw.Geometry);
return this;
}
//Z轴归0
Z0() {
super.Z0();
for (let s of this._ShapeManager.ShapeList)
s.Z0();
return this;
}
MatrixPlanarizere() {
super.MatrixPlanarizere();
for (let s of this._ShapeManager.ShapeList)
s.MatrixPlanarizere();
return this;
}
ApplyMirrorMatrix(m) {
return this;
}
/**
* 请注意:该计算会操作otherRegion的矩阵
* @param {Region} otherRegion
* @param {BoolOpeartionType} boolType
*/
BooleanOper(otherRegion, boolType) {
if (this.IsCoplaneTo(otherRegion)) {
this.WriteAllObjectRecord();
let oldOcs = this.OCS;
//把形状曲线转移到二维屏幕计算后还原回来
this.ShapeApplyMatrix(this.OCSInv);
otherRegion.ShapeApplyMatrix(this.OCSInv);
let isSuccess = this._ShapeManager.BoolOper(otherRegion._ShapeManager, boolType);
this.ShapeApplyMatrix(oldOcs);
this.Update();
return isSuccess;
}
return false;
}
get MeshGeometry() {
if (this._MeshGeometry)
return this._MeshGeometry;
this.UpdateGeometry();
return this._MeshGeometry;
}
get EdgeGeometry() {
if (this._EdgeGeometry)
return this._EdgeGeometry;
this.UpdateGeometry();
return this._EdgeGeometry;
}
UpdateGeometry() {
let shapeList = this._ShapeManager.ShapeList;
let edgePts = [];
let meshGeoms = [];
const AddEdgePts = (pts, diffMat) => {
for (let i = 0; i < pts.length; i++) {
let p = AsVector3(pts[i]);
p.applyMatrix4(diffMat);
edgePts.push(p);
if (i !== 0 && i !== pts.length - 1)
edgePts.push(p);
}
};
for (let i = 0; i < shapeList.length; i++) {
let shape = shapeList[i];
let geometry = new three.ShapeGeometry(shape.Shape, 60); //60 可以优化.
let diffMat = this.OCSInv.clone().multiply(shape.Outline.Curve.OCSNoClone);
geometry.applyMatrix4(diffMat);
ScaleUV(geometry);
meshGeoms.push(new three.BufferGeometry().fromGeometry(geometry));
let shapeInfo = shape.Shape.extractPoints(60);
let pts = shapeInfo.shape;
AddEdgePts(pts, diffMat);
let holePtss = shapeInfo.holes;
for (let holePts of holePtss)
AddEdgePts(holePts, diffMat);
}
this._EdgeGeometry = BufferGeometryUtils.CreateFromPts(edgePts);
this._MeshGeometry = BufferGeometryUtils.MergeBufferGeometries(meshGeoms);
this._MeshGeometry["IsMesh"] = true;
this._MeshGeometry.computeVertexNormals();
}
UpdateDrawGeometry() {
if (this._EdgeGeometry)
this._EdgeGeometry.dispose();
this._EdgeGeometry = undefined;
if (this._MeshGeometry)
this._MeshGeometry.dispose();
this._MeshGeometry = undefined;
}
ClearDraw() {
this.UpdateDrawGeometry();
return super.ClearDraw();
}
InitDrawObject(renderType = RenderType.Wireframe) {
if (renderType === RenderType.Wireframe) {
return new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(this.ColorIndex));
}
else if (renderType === RenderType.Conceptual) {
return new three.Object3D().add(new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(this.ColorIndex)), new three.Mesh(this.MeshGeometry, ColorMaterial.GetConceptualMaterial(this.ColorIndex)));
}
else if (renderType === RenderType.Physical) {
let mesh = new three.Mesh(this.MeshGeometry, this.MeshMaterial);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
else if (renderType === RenderType.Print) {
return new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(0));
}
else if (renderType === RenderType.Physical2) {
let mesh = new three.Mesh(this.MeshGeometry, this.MeshMaterial);
mesh.castShadow = true;
mesh.receiveShadow = true;
return new three.Object3D().add(new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(this.ColorIndex)), mesh);
}
}
UpdateDrawObject(renderType, obj) {
DisposeThreeObj(obj);
Object3DRemoveAll(obj);
if (renderType === RenderType.Wireframe) {
let l = obj;
l.geometry = this.EdgeGeometry;
l.material = ColorMaterial.GetLineMaterial(this.ColorIndex);
}
else if (renderType === RenderType.Conceptual) {
return obj.add(new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(this.ColorIndex)), new three.Mesh(this.MeshGeometry, ColorMaterial.GetConceptualMaterial(this.ColorIndex)));
}
else if (renderType === RenderType.Physical) {
let mesh = obj;
mesh.geometry = this.MeshGeometry;
mesh.material = this.MeshMaterial;
}
else if (renderType === RenderType.Physical2) {
let mesh = new three.Mesh(this.MeshGeometry, this.MeshMaterial);
mesh.castShadow = true;
mesh.receiveShadow = true;
return obj.add(new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(this.ColorIndex)), mesh);
}
else if (renderType === RenderType.Print) {
let l = obj;
l.geometry = this.EdgeGeometry;
l.material = ColorMaterial.GetLineMaterial(0);
}
}
/**
* 当实体需要被更新时,更新实体材质
*/
UpdateDrawObjectMaterial(type, obj, material) {
if (type === RenderType.Wireframe || type === RenderType.Print) {
let line = obj;
line.material = ColorMaterial.GetLineMaterial(this.ColorIndex);
}
else if (type === RenderType.Conceptual) {
for (let i = 0; i < obj.children.length; i++) {
if (i % 2 === 0) {
let l = obj.children[i];
l.material = ColorMaterial.GetLineMaterial(this.ColorIndex);
}
else {
let mesh = obj.children[i];
mesh.material = ColorMaterial.GetConceptualMaterial(this.ColorIndex);
}
}
}
else if (type === RenderType.Physical) {
let mesh = obj;
mesh.material = this.MeshMaterial;
}
}
_ReadFile(file) {
super._ReadFile(file);
file.Read(); //1
this._ShapeManager.Clear();
this._ShapeManager.ReadFile(file);
}
WriteFile(file) {
super.WriteFile(file);
file.Write(1); //ver
this._ShapeManager.WriteFile(file);
}
};
Region = Region_1 = __decorate([
Factory
], Region);
/**
* 把板件炸开成面域,0,1为正反面,其余的为边面(没有圆弧面)
*/
function Board2Regions(br) {
let ocs = br.OCS;
let cu = br.ContourCurve.Clone();
if (cu instanceof exports.Circle)
cu = ConverCircleToPolyline$1(cu);
let frontReg = Region.CreateFromCurves([cu.Clone()]);
let regFrontOcs = ocs.clone();
regFrontOcs.setPosition(br.Position.add(br.Normal.multiplyScalar(br.Thickness)));
frontReg.ApplyMatrix(regFrontOcs);
let backReg = Region.CreateFromCurves([cu.Flip()]);
backReg.ApplyMatrix(ocs);
let resultRegs = [frontReg, backReg];
//edges
let lines = cu.Explode().filter(c => c instanceof exports.Line);
for (let l of lines) {
let rectPl = new exports.Polyline().Rectangle(l.Length, br.Thickness);
let reg = Region.CreateFromCurves([rectPl]);
if (!reg)
continue;
let p = l.StartPoint.applyMatrix4(ocs);
let x = l.GetFirstDeriv(0).transformDirection(ocs);
let y = br.Normal;
let z = new three.Vector3().crossVectors(x, y);
let mtx = new three.Matrix4().makeBasis(x, y, z).setPosition(p);
reg.ApplyMatrix(mtx);
resultRegs.push(reg);
}
return resultRegs;
}
/**统一板件属性key的命名修改值会导致无法 .xxx该属性 */
var EBoardKeyList;
(function (EBoardKeyList) {
EBoardKeyList["Height"] = "height";
EBoardKeyList["Width"] = "width";
EBoardKeyList["Thick"] = "thickness";
EBoardKeyList["RoomName"] = "roomName";
EBoardKeyList["CabinetName"] = "cabinetName";
EBoardKeyList["PaperName"] = "paperName";
EBoardKeyList["BrName"] = "brName";
EBoardKeyList["BrMat"] = "boardName";
EBoardKeyList["Mat"] = "material";
EBoardKeyList["Color"] = "color";
EBoardKeyList["Lines"] = "lines";
EBoardKeyList["ProcessGroup"] = "ProcessGroup";
EBoardKeyList["BigHole"] = "bigHoleDir";
/**
* 排钻类型,当没有定义每个边的排钻数据时,使用统一的排钻类型
*/
EBoardKeyList["DrillType"] = "drillType";
EBoardKeyList["ComposingFace"] = "composingFace";
/**
* 封边数组,定义每个边的封边信息
*/
EBoardKeyList["HighSealed"] = "highSealed";
EBoardKeyList["UpSealed"] = "sealedUp";
EBoardKeyList["DownSealed"] = "sealedDown";
EBoardKeyList["LeftSealed"] = "sealedLeft";
EBoardKeyList["RightSealed"] = "sealedRight";
EBoardKeyList["KnifeRad"] = "knifeRadius";
EBoardKeyList["SpliteHeight"] = "spliteHeight";
EBoardKeyList["SpliteWidth"] = "spliteWidth";
EBoardKeyList["SpliteThickness"] = "spliteThickness";
EBoardKeyList["DrawNumber"] = "drawNumber";
EBoardKeyList["Remarks"] = "remarks";
EBoardKeyList["ExtraRemarks"] = "extraRemarks";
EBoardKeyList["OpenDir"] = "openDir";
EBoardKeyList["GroovesAddLength"] = "GroovesAddLength";
EBoardKeyList["GroovesAddWidth"] = "GroovesAddWidth";
EBoardKeyList["GroovesAddDepth"] = "GroovesAddDepth";
EBoardKeyList["FrontDrill"] = "frontDrill";
EBoardKeyList["BackDrill"] = "backDrill";
})(EBoardKeyList || (EBoardKeyList = {}));
/**序列化板件数据 */
function serializeBoardData(file, processData) {
file.Write(processData[EBoardKeyList.RoomName]);
file.Write(processData[EBoardKeyList.CabinetName]);
file.Write(processData[EBoardKeyList.BrMat]);
file.Write(processData[EBoardKeyList.Mat]);
file.Write(processData[EBoardKeyList.Color]);
file.Write(processData[EBoardKeyList.Lines]);
file.Write(processData[EBoardKeyList.BigHole]);
file.Write(processData[EBoardKeyList.DrillType]);
file.Write(processData[EBoardKeyList.ComposingFace]);
file.Write(processData[EBoardKeyList.HighSealed].length);
for (let n of processData[EBoardKeyList.HighSealed]) {
file.Write(n.size);
}
file.Write(processData[EBoardKeyList.UpSealed]);
file.Write(processData[EBoardKeyList.DownSealed]);
file.Write(processData[EBoardKeyList.LeftSealed]);
file.Write(processData[EBoardKeyList.RightSealed]);
file.Write(processData.spliteHeight);
file.Write(processData.spliteWidth);
file.Write(processData.spliteThickness);
file.Write(processData.highDrill.length);
for (let n of processData.highDrill)
file.Write(n);
file.Write(processData.frontDrill);
file.Write(processData.backDrill);
file.Write(processData.remarks.length);
for (let d of processData.remarks) {
file.Write(d[0]);
file.Write(d[1]);
}
}
//反序列化板件数据
function deserializationBoardData(file, processData, ver) {
processData[EBoardKeyList.RoomName] = file.Read();
processData[EBoardKeyList.CabinetName] = file.Read();
processData[EBoardKeyList.BrMat] = file.Read();
processData[EBoardKeyList.Mat] = file.Read();
processData[EBoardKeyList.Color] = file.Read();
processData[EBoardKeyList.Lines] = file.Read();
processData[EBoardKeyList.BigHole] = file.Read();
processData[EBoardKeyList.DrillType] = file.Read();
processData[EBoardKeyList.ComposingFace] = file.Read();
let count = file.Read();
processData[EBoardKeyList.HighSealed].length = 0;
for (let i = 0; i < count; i++) {
let size = file.Read();
if (ver < 4) {
file.Read();
}
processData[EBoardKeyList.HighSealed].push({ size });
}
processData[EBoardKeyList.UpSealed] = file.Read();
processData[EBoardKeyList.DownSealed] = file.Read();
processData[EBoardKeyList.LeftSealed] = file.Read();
processData[EBoardKeyList.RightSealed] = file.Read();
processData.spliteHeight = file.Read();
processData.spliteWidth = file.Read();
processData.spliteThickness = file.Read();
count = file.Read();
processData.highDrill = file.ReadArray(count);
processData.frontDrill = file.Read();
processData.backDrill = file.Read();
if (ver >= 7) {
let count = file.Read();
processData.remarks.length = 0;
for (let i = 0; i < count; i++) {
let d = ["", ""];
d[0] = file.Read();
d[1] = file.Read();
processData.remarks.push(d);
}
}
}
function SerializeBoard2DModeingData(file, modelList) {
file.Write(modelList.length);
for (let data of modelList) {
file.WriteObject(data.path);
file.Write(data.dir);
file.Write(data.items.length);
for (let item of data.items) {
file.Write(item.depth);
file.Write(item.offset);
file.Write(item.knife.id);
file.Write(item.knife.radius);
file.Write(item.knife.angle);
file.Write(item.knife.name);
}
}
}
function SerializeBoard3DModeingData(file, modelList) {
file.Write(modelList.length);
for (let item of modelList) {
file.Write(item.path.length);
for (let d of item.path) {
file.Write(d.pt.toArray());
file.Write(d.bul);
}
file.Write(item.dir);
file.Write(item.knife.id);
file.Write(item.knife.radius);
file.Write(item.knife.angle);
file.Write(item.knife.name);
}
}
//反序列化板件数据
function DeserializationBoard2DModeingData(file, data, ver) {
data.length = 0;
const count = file.Read();
for (let i = 0; i < count; i++) {
let path = file.ReadObject();
let dir = file.Read();
let m = {
path,
dir,
items: []
};
const itemCount = file.Read();
for (let j = 0; j < itemCount; j++) {
let depth = file.Read();
let offset = file.Read();
let knifeId = file.Read();
let knifeRad = file.Read();
let knifeAngle = file.Read();
let knifeName = file.Read();
m.items.push({
depth, offset, knife: { id: knifeId, radius: knifeRad, angle: knifeAngle, name: knifeName }
});
}
data.push(m);
}
}
//反序列化板件数据
function DeserializationBoard3DModeingData(file, data, ver) {
data.length = 0;
const count = file.Read();
for (let i = 0; i < count; i++) {
let pathCount = file.Read();
let path = [];
for (let i = 0; i < pathCount; i++) {
let pt = new three.Vector3().fromArray(file.Read());
let bul = file.Read();
path.push({ pt, bul });
}
let dir = file.Read();
let knifeId = file.Read();
let knifeRad = file.Read();
let knifeAngle = file.Read();
let knifeName = file.Read();
data.push({
path, dir, knife: { id: knifeId, radius: knifeRad, angle: knifeAngle, name: knifeName }
});
}
}
let Hole = class Hole extends Entity {
constructor() {
super(...arguments);
this.type = GangDrillType.Pxl;
}
get Height() {
return this._Height;
}
set Height(v) {
if (this._Height !== v) {
this.WriteAllObjectRecord();
this._Height = v;
this.Update();
}
}
get Type() {
return this.type;
}
set Type(t) {
if (this.type !== t) {
this.WriteAllObjectRecord();
this.type = t;
}
}
Clone() {
let ent = super.Clone();
ent.OtherHalfTongKong = null;
return ent;
}
_ReadFile(file) {
super._ReadFile(file);
let ver = file.Read(); //1
if (ver <= 4) {
//临时兼容旧图纸排钻,更新旧图纸后去掉兼容代码
file['readIndex']--;
}
else {
this._Height = file.Read();
this.FId = file.ReadSoftObjectId();
this.MId = file.ReadSoftObjectId();
}
if (ver >= 6) {
this.OtherHalfTongKong = file.ReadSoftObjectId();
}
}
WriteFile(file) {
super.WriteFile(file);
file.Write(6); //ver
file.Write(this._Height);
file.WriteSoftObjectId(this.FId);
file.WriteSoftObjectId(this.MId);
file.WriteSoftObjectId(this.OtherHalfTongKong);
}
};
__decorate([
AutoRecord
], Hole.prototype, "FId", void 0);
__decorate([
AutoRecord
], Hole.prototype, "MId", void 0);
__decorate([
AutoRecord
], Hole.prototype, "OtherHalfTongKong", void 0);
Hole = __decorate([
Factory
], Hole);
var CylinderHole_1;
var GangDrillType;
(function (GangDrillType) {
/**偏心轮 */
GangDrillType[GangDrillType["Pxl"] = 0] = "Pxl";
/**连接杆 */
GangDrillType[GangDrillType["Ljg"] = 1] = "Ljg";
/**预埋件 */
GangDrillType[GangDrillType["Ymj"] = 2] = "Ymj";
/**层板钉 */
GangDrillType[GangDrillType["Nail"] = 3] = "Nail";
/** 木销 */
GangDrillType[GangDrillType["Wood"] = 4] = "Wood";
/** 通孔 */
GangDrillType[GangDrillType["TK"] = 5] = "TK";
GangDrillType[GangDrillType["WoodPXL"] = 6] = "WoodPXL";
})(GangDrillType || (GangDrillType = {}));
let TempCircle1 = new exports.Circle();
let TempCircle2 = new exports.Circle();
let CylinderHole = CylinderHole_1 = class CylinderHole extends Hole {
constructor() {
super();
this._Radius = 1;
this._Color = 1;
}
static CreateCylHole(radius, height, type) {
let drill = new CylinderHole_1();
drill.Height = height;
drill._Radius = radius;
drill.type = type;
return drill;
}
set Radius(r) {
if (r !== this._Radius) {
this.WriteAllObjectRecord();
this._MeshGeometry = null;
this._EdgeGeometry = null;
this._Radius = r;
this.Update();
}
}
get Height() {
return super.Height;
}
set Height(v) {
if (this._Height !== v) {
this._MeshGeometry = null;
this._EdgeGeometry = null;
super.Height = v;
}
}
get Radius() {
return this._Radius;
}
get BoundingBox() {
return this.BoundingBoxInOCS.applyMatrix4(this._Matrix);
}
/**
* 返回对象在自身坐标系下的Box
*/
get BoundingBoxInOCS() {
return new Box3Ext(new three.Vector3(-this._Radius, -this._Radius, 0), new three.Vector3(this._Radius, this._Radius, this._Height));
}
get MeshGeometry() {
if (this._MeshGeometry)
return this._MeshGeometry;
this._MeshGeometry = FastDrillingMeshGeometry(this.Radius, this.Height);
return this._MeshGeometry;
}
get EdgeGeometry() {
if (this._EdgeGeometry)
return this._EdgeGeometry;
this._EdgeGeometry = FastDrillingEdgeGeometry(this._Radius, this.Height);
return this._EdgeGeometry;
}
GetGripPoints() {
let cir = new exports.Circle(new three.Vector3(), this._Radius);
let pts = cir.GetGripPoints();
pts.push(...pts.map(p => p.clone().add(new three.Vector3(0, 0, this.Height))));
return pts.map(p => p.applyMatrix4(this.OCS));
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
let pts = [];
TempCircle1.Radius = this.Radius;
TempCircle1.OCS = this._Matrix;
TempCircle2.Radius = this.Radius;
TempCircle2.OCS = this._Matrix;
TempCircle2.Position = TempCircle2.Position.add(this.Normal.multiplyScalar(this.Height));
for (let c of [TempCircle2, TempCircle1]) {
pts.push(...c.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform));
}
return pts;
}
Erase(isErase = true) {
if (isErase === this.IsErase)
return;
super.Erase(isErase);
if (!isErase)
return;
if (this.OtherHalfTongKong && !this.OtherHalfTongKong.IsErase) {
let cy = this.OtherHalfTongKong.Object;
cy.Type = GangDrillType.Ymj;
cy.OtherHalfTongKong = null;
}
}
InitDrawObject(renderType) {
return this.GetObject3DByRenderType(renderType);
}
GetObject3DByRenderType(renderType) {
if (renderType === RenderType.Wireframe)
return new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(this.ColorIndex));
else if (renderType === RenderType.CustomNumber || renderType === RenderType.ModelGroove)
return; //不绘制了
// return new Mesh(this.MeshGeometry, ColorMaterial.GetConceptualMaterial(this.ColorIndex, FrontSide, true));
else
return new three.Mesh(this.MeshGeometry, ColorMaterial.GetConceptualMaterial(this.ColorIndex));
}
UpdateDrawObject(type, obj) {
DisposeThreeObj(obj);
Object3DRemoveAll(obj);
obj.add(this.GetObject3DByRenderType(type));
}
UpdateDrawObjectMaterial(type, obj) {
if (type === RenderType.Wireframe) {
let l = obj;
l.material = ColorMaterial.GetLineMaterial(this.ColorIndex);
}
else {
let mesh = obj;
if (type === RenderType.CustomNumber || type === RenderType.ModelGroove)
mesh.material = ColorMaterial.GetConceptualMaterial(this.ColorIndex, three.FrontSide, true);
else
mesh.material = ColorMaterial.GetConceptualMaterial(this.ColorIndex);
}
}
ClearDraw() {
if (this._EdgeGeometry)
this._EdgeGeometry.dispose();
this._EdgeGeometry = undefined;
if (this._MeshGeometry)
this._MeshGeometry.dispose();
this._MeshGeometry = undefined;
return super.ClearDraw();
}
_ReadFile(file) {
super._ReadFile(file);
let ver = file.Read(); //1
this._Radius = file.Read();
if (ver <= 4) {
//临时兼容旧排钻
this._Height = file.Read();
this.type = file.Read();
this.FId = file.ReadSoftObjectId();
this.MId = file.ReadSoftObjectId();
}
else {
this.type = file.Read();
}
}
WriteFile(file) {
super.WriteFile(file);
file.Write(5); //ver
file.Write(this._Radius);
file.Write(this.type);
}
};
CylinderHole = CylinderHole_1 = __decorate([
Factory
], CylinderHole);
let cache = new Map();
let ro = new three.Matrix4();
ro.makeRotationX(Math.PI / 2);
function FastDrillingMeshGeometry(radius, height) {
let key = `${radius},${height}`;
if (cache.has(key))
return cache.get(key);
let geo = new three.CylinderBufferGeometry(radius, radius, height, 8, 1);
geo.applyMatrix4(ro);
geo.translate(0, 0, height / 2);
cache.set(key, geo);
return geo;
}
let cache2 = new Map();
function FastDrillingEdgeGeometry(radius, height) {
let key = `${radius},${height}`;
if (cache2.has(key))
return cache2.get(key);
let sp = new three.Shape();
sp.ellipse(0, 0, radius, radius, 0, 2 * Math.PI, false, 0);
let pts = sp.getPoints(4);
let geo = new three.BufferGeometry();
let coords = [];
for (let i = 0; i < pts.length; i++) {
let p = pts[i];
let np = pts[FixIndex$1(i + 1, pts.length)];
coords.push(p.x, p.y, 0, np.x, np.y, 0); //bottom
coords.push(p.x, p.y, height, np.x, np.y, height); //top
coords.push(p.x, p.y, 0, p.x, p.y, height); //edge
}
geo.setAttribute('position', new three.Float32BufferAttribute(coords, 3));
cache2.set(key, geo);
return geo;
}
CADFactory.RegisterObjectAlias(CylinderHole, "GangDrill");
const SCALAR = 0.1;
function CyHoleInBoard(cys, br, ocs, checkAll = false, allowPxl = false) {
if (!checkAll && cys.length === 1 && cys[0].Type === GangDrillType.Ymj)
return true;
const outline = br.ContourCurve;
let box = new Box3Ext();
let pxl;
let ljg;
let ymj;
let wood;
let woodPXL;
let pxl2;
for (let cy of cys) {
box.union(cy.BoundingBox);
if (cy.Type === GangDrillType.Pxl) {
if (pxl)
pxl2 = cy;
else
pxl = cy;
}
else if (cy.Type === GangDrillType.Ljg)
ljg = cy;
else if (cy.Type === GangDrillType.Wood)
wood = cy;
else if (cy.Type === GangDrillType.WoodPXL)
woodPXL = cy;
else
ymj = cy;
}
box.applyMatrix4(ocs);
let outlineBox = outline.BoundingBox;
outlineBox.max.setZ(br.Thickness);
if (!box.intersectsBox(outlineBox, 1e-5))
return false;
let nor = new three.Vector3();
if (ljg)
nor.copy(ljg.Normal);
else if (ymj)
nor.copy(ymj.Normal);
else if (wood)
nor.copy(wood.Normal);
nor.applyMatrix4(ocs.clone().setPosition(new three.Vector3));
if (isParallelTo(nor, ZAxis)) {
if (ymj) {
let center = ymj.Position.applyMatrix4(ocs).setZ(0);
let cir = new exports.Circle(center, ymj.Radius - SCALAR);
return outline.IntersectWith(cir, 0).length === 0 && outline.PtInCurve(center);
}
}
else {
if (pxl) {
let plxs = [pxl];
if (pxl2)
plxs.push(pxl2);
if (plxs.every(cy => {
let center = cy.Position.applyMatrix4(ocs).setZ(0);
let cir = new exports.Circle(center, pxl.Radius - SCALAR);
if (allowPxl || !HostApplicationServices.forceFilterPxl)
return outline.IntersectWith(cir, 0).length <= 1 && !outline.PtInCurve(center);
else
return outline.IntersectWith(cir, 0).length > 0 || !outline.PtInCurve(center);
}))
return false;
}
if (woodPXL) {
let center = woodPXL.Position.applyMatrix4(ocs).setZ(0);
let cir = new exports.Circle(center, woodPXL.Radius - SCALAR);
if (outline.IntersectWith(cir, 0).length > 0 || !outline.PtInCurve(center))
return false;
}
if (ljg) {
let c1 = ljg.Position.applyMatrix4(ocs).setZ(0);
let minPt = c1.clone().add(nor.clone().multiplyScalar(ljg.Height / 2));
let c2 = c1.clone().add(nor.clone().multiplyScalar(ljg.Height - SCALAR));
c1.add(nor.clone().multiplyScalar(SCALAR));
rotatePoint(nor, Math.PI / 2);
c1.add(nor.multiplyScalar(ljg.Radius));
c2.add(nor.negate());
let rect = new exports.Polyline().RectangleFrom2Pt(c1, c2);
let intPtsLen = outline.IntersectWith(rect, 0).length;
if (intPtsLen > 2 || (intPtsLen === 0 && !outline.PtInCurve(minPt)))
return false;
}
if (wood) {
let c1 = wood.Position.applyMatrix4(ocs).setZ(0);
let c2 = c1.clone().add(nor.clone().multiplyScalar(wood.Height));
rotatePoint(nor, Math.PI / 2);
let dir = nor.multiplyScalar(wood.Radius);
let p1 = c1.clone().add(dir);
let p2 = c2.clone().add(dir);
let p3 = c1.clone().add(dir.negate());
let p4 = c2.clone().add(dir);
let l1 = new exports.Line(p1, p2);
let l2 = new exports.Line(p3, p4);
if (l1.IntersectWith(outline, 0).length !== 1 || l2.IntersectWith(outline, 0).length !== 1)
return false;
}
}
return true;
}
/**分析上下左右排钻 */
function ParseBoardRectHoleType(br, outBrRectHoleType = {}) {
let dir = Math.sign(br.ContourCurve.Area2);
let hightDrill = br.BoardProcessOption.highDrill;
let cus = br.ContourCurve.Explode();
for (let i = 0; i < cus.length; i++) {
let c = cus[i];
let derv = c.GetFirstDeriv(0).multiplyScalar(dir);
if (Math.abs(derv.x) > Math.abs(derv.y)) {
if (derv.x > 0)
outBrRectHoleType.down = hightDrill[i];
else
outBrRectHoleType.up = hightDrill[i];
}
else {
if (derv.y > 0)
outBrRectHoleType.right = hightDrill[i];
else
outBrRectHoleType.left = hightDrill[i];
}
}
return outBrRectHoleType;
}
function ExtureHoleInBoard(holes, board, ocs) {
//TODO:自定义排钻判断
return true;
}
function HoleInBoard(holes, br, ocs) {
if (holes.length === 0)
return false;
if (holes[0] instanceof CylinderHole) {
return CyHoleInBoard(holes, br, ocs ?? br.OCSInv);
}
else {
return ExtureHoleInBoard(holes, br, ocs ?? br.OCSInv);
}
}
/**上下左右排钻写入到板件的高级排钻中 */
function SetBrHighHoleTypeFromRectHoleType(br, brRectHoleType) {
let dir = Math.sign(br.ContourCurve.Area2);
let highDrill = br.BoardProcessOption.highDrill;
let cus = br.ContourCurve.Explode();
highDrill.length = 0;
for (let i = 0; i < cus.length; i++) {
let c = cus[i];
let derv = c.GetFirstDeriv(0).multiplyScalar(dir);
if (Math.abs(derv.x) > Math.abs(derv.y)) {
if (derv.x > 0)
highDrill.push(brRectHoleType.down);
else
highDrill.push(brRectHoleType.up);
}
else {
if (derv.y > 0)
highDrill.push(brRectHoleType.right);
else
highDrill.push(brRectHoleType.left);
}
}
let types = new Set(highDrill);
if (types.size === 1 && highDrill[0] !== DrillType.None)
br.BoardProcessOption[EBoardKeyList.DrillType] = highDrill[0];
else if (types.size > 1)
br.BoardProcessOption[EBoardKeyList.DrillType] = DrillType.More;
}
class BoardUVGenerator {
generateTopUV(geometry, vertices, indexA, indexB, indexC) {
var a_x = vertices[indexA * 3];
var a_y = vertices[indexA * 3 + 1];
var b_x = vertices[indexB * 3];
var b_y = vertices[indexB * 3 + 1];
var c_x = vertices[indexC * 3];
var c_y = vertices[indexC * 3 + 1];
return [
new three.Vector2(a_x, a_y),
new three.Vector2(b_x, b_y),
new three.Vector2(c_x, c_y)
];
}
generateSideWallUV(geometry, vertices, indexA, indexB, indexC, indexD) {
var a_x = vertices[indexA * 3];
var a_y = vertices[indexA * 3 + 1];
var a_z = vertices[indexA * 3 + 2];
var b_x = vertices[indexB * 3];
var b_y = vertices[indexB * 3 + 1];
var b_z = vertices[indexB * 3 + 2];
var c_x = vertices[indexC * 3];
var c_y = vertices[indexC * 3 + 1];
var c_z = vertices[indexC * 3 + 2];
var d_x = vertices[indexD * 3];
var d_y = vertices[indexD * 3 + 1];
var d_z = vertices[indexD * 3 + 2];
let pts;
if (Math.abs(a_y - b_y) < 0.01) {
pts = [
new three.Vector2(a_z - 1, a_x),
new three.Vector2(b_z - 1, b_x),
new three.Vector2(c_z - 1, c_x),
new three.Vector2(d_z - 1, d_x)
];
}
else {
pts = [
new three.Vector2(a_z - 1, a_y),
new three.Vector2(b_z - 1, b_y),
new three.Vector2(c_z - 1, c_y),
new three.Vector2(d_z - 1, d_y)
];
}
return pts;
}
}
class BoardUVGenerator2 extends BoardUVGenerator {
generateTopUV(geometry, vertices, indexA, indexB, indexC) {
var a_x = vertices[indexA * 3];
var a_y = vertices[indexA * 3 + 1];
var b_x = vertices[indexB * 3];
var b_y = vertices[indexB * 3 + 1];
var c_x = vertices[indexC * 3];
var c_y = vertices[indexC * 3 + 1];
return [
new three.Vector2(a_y, a_x),
new three.Vector2(b_y, b_x),
new three.Vector2(c_y, c_x)
];
}
}
let boardUVGenerator = new BoardUVGenerator();
let boardUVGenerator2 = new BoardUVGenerator2();
const _TerminateTaskInjectInteractionFunctions = [];
function TerminateCSGTask(task) {
for (let f of _TerminateTaskInjectInteractionFunctions)
f(task);
}
var BoardType;
(function (BoardType) {
BoardType[BoardType["Layer"] = 0] = "Layer";
BoardType[BoardType["Vertical"] = 1] = "Vertical";
BoardType[BoardType["Behind"] = 2] = "Behind"; //背板
})(BoardType || (BoardType = {}));
//纹路类型
var LinesType;
(function (LinesType) {
/** 正纹 */
LinesType[LinesType["Positive"] = 0] = "Positive";
/** 反纹 */
LinesType[LinesType["Reverse"] = 1] = "Reverse";
/** 可翻转 */
LinesType[LinesType["CanReversal"] = 2] = "CanReversal";
})(LinesType || (LinesType = {}));
// 排版面
var ComposingType;
(function (ComposingType) {
ComposingType[ComposingType["Positive"] = 0] = "Positive";
ComposingType[ComposingType["Reverse"] = 1] = "Reverse";
ComposingType[ComposingType["Arbitrary"] = 2] = "Arbitrary"; //任意
})(ComposingType || (ComposingType = {}));
var BoardOpenDir;
(function (BoardOpenDir) {
BoardOpenDir[BoardOpenDir["Left"] = 1] = "Left";
BoardOpenDir[BoardOpenDir["Right"] = 2] = "Right";
BoardOpenDir[BoardOpenDir["Up"] = 3] = "Up";
BoardOpenDir[BoardOpenDir["Down"] = 4] = "Down";
BoardOpenDir[BoardOpenDir["None"] = 0] = "None";
})(BoardOpenDir || (BoardOpenDir = {}));
var BrRelativePos;
(function (BrRelativePos) {
BrRelativePos["Front"] = "front";
BrRelativePos["Back"] = "back";
BrRelativePos["Top"] = "top";
BrRelativePos["Bottom"] = "bottom";
BrRelativePos["Left"] = "left";
BrRelativePos["Right"] = "right";
BrRelativePos["Div"] = "div";
})(BrRelativePos || (BrRelativePos = {}));
class PointShapeUtils {
//方形点表
static SquarePts(size) {
return [
new three.Vector3(-size, -size),
new three.Vector3(size, -size),
new three.Vector3(size, size),
new three.Vector3(-size, size),
new three.Vector3(-size, -size),
];
}
//方形外圈十字直线点表
static OutsideLinePts(squareSize, lineLength) {
return [
//-X
new three.Vector3(-squareSize, 0),
new three.Vector3(-lineLength, 0),
//X
new three.Vector3(squareSize, 0),
new three.Vector3(lineLength, 0),
//Y
new three.Vector3(0, squareSize),
new three.Vector3(0, lineLength),
//-Y
new three.Vector3(0, -squareSize),
new three.Vector3(0, -lineLength),
];
}
//十字直线点表
static CrossLinePts(lineLength) {
return [
new three.Vector3(0, -lineLength),
new three.Vector3(0, lineLength),
new three.Vector3(lineLength, 0),
new three.Vector3(-lineLength, 0),
];
}
static CrossLine3DPts(lineLength) {
return [
[new three.Vector3(lineLength, 0),
new three.Vector3(-lineLength / 2, 0)],
[new three.Vector3(0, -lineLength / 2),
new three.Vector3(0, lineLength)],
[new three.Vector3(0, 0, -lineLength / 2),
new three.Vector3(0, 0, lineLength)],
];
}
static TrianglePts(size) {
return [
new three.Vector3(size, -size),
new three.Vector3(0, size),
new three.Vector3(-size, -size),
new three.Vector3(size, -size),
];
}
static CirclePts(size) {
let pts = [];
let a = Math.PI * 2 / 8;
for (let i = 0; i < 9; i++)
pts.push(new three.Vector3(Math.sin(a * i) * size, Math.cos(a * i) * size));
return pts;
}
static ObliqueCrossPts(size) {
return [new three.Vector3(-size, size), new three.Vector3(size, -size), new three.Vector3(-size, -size), new three.Vector3(size, size)];
}
static ObliqueCrossLinePts(size) {
return [new three.Vector3(-size, size), new three.Vector3(size, -size), new three.Vector3(), new three.Vector3(-size, -size), new three.Vector3(size, size)];
}
static SandClockPts(size) {
return [
new three.Vector3(size, size),
new three.Vector3(-size, size),
new three.Vector3(size, -size),
new three.Vector3(-size, -size),
new three.Vector3(size, size),
];
}
static TangentPts(size) {
let pts = [
new three.Vector3(-size, size),
new three.Vector3(size, size),
new three.Vector3(size / 2, size),
];
let a = Math.PI * 2 / 8;
for (let i = 0; i < 9; i++)
pts.push(new three.Vector3(Math.sin(a * i + Math.PI / 2) * size, Math.cos(a * i + Math.PI / 2) * size));
return pts;
}
static PerPts(size) {
return [
new three.Vector3(-size, size),
new three.Vector3(-size, -size),
new three.Vector3(size, -size),
new three.Vector3(0, -size),
new three.Vector3(0, 0),
new three.Vector3(-size, 0),
];
}
static LinesDirPts(len, width, lineType) {
if (lineType === LinesType.Reverse) {
return [
new three.Vector3(-len / 2), new three.Vector3(-len / 2 + width / 2, width / 2),
new three.Vector3(-len / 2), new three.Vector3(-len / 2 + width / 2, -width / 2),
new three.Vector3(-len / 2), new three.Vector3(len / 2),
new three.Vector3(len / 2), new three.Vector3(len / 2 - width / 2, width / 2),
new three.Vector3(len / 2), new three.Vector3(len / 2 - width / 2, -width / 2),
];
}
else if (lineType === LinesType.Positive)
return [
new three.Vector3(0, -len / 2), new three.Vector3(-width / 2, -len / 2 + width / 2),
new three.Vector3(0, -len / 2), new three.Vector3(width / 2, -len / 2 + width / 2),
new three.Vector3(0, -len / 2), new three.Vector3(0, len / 2),
new three.Vector3(0, len / 2), new three.Vector3(-width / 2, len / 2 - width / 2),
new three.Vector3(0, len / 2), new three.Vector3(width / 2, len / 2 - width / 2),
];
else {
let w1 = Math.min(len, width) / 5;
return [
new three.Vector3(0, len / 2), new three.Vector3(0, -len / 2),
new three.Vector3(-width / 2), new three.Vector3(width / 2),
new three.Vector3(-width / 2), new three.Vector3(-width / 2 + w1, w1),
new three.Vector3(-width / 2), new three.Vector3(-width / 2 + w1, -w1),
new three.Vector3(width / 2), new three.Vector3(width / 2 - w1, w1),
new three.Vector3(width / 2), new three.Vector3(width / 2 - w1, -w1),
new three.Vector3(0, len / 2), new three.Vector3(-w1, len / 2 - w1),
new three.Vector3(0, len / 2), new three.Vector3(w1, len / 2 - w1),
new three.Vector3(0, -len / 2), new three.Vector3(-w1, -len / 2 + w1),
new three.Vector3(0, -len / 2), new three.Vector3(w1, -len / 2 + w1),
];
}
}
//开门方向纹路
static LinesOpenDirPts(len, width, openDir) {
if (openDir === BoardOpenDir.Right) {
return [
new three.Vector3(-width / 2, -len / 2), new three.Vector3(width / 2, 0),
new three.Vector3(width / 2, 0), new three.Vector3(-width / 2, len / 2)
];
}
else if (openDir === BoardOpenDir.Left) {
return [
new three.Vector3(width / 2, -len / 2), new three.Vector3(-width / 2, 0),
new three.Vector3(-width / 2, 0), new three.Vector3(width / 2, len / 2)
];
}
else if (openDir === BoardOpenDir.Up) {
return [
new three.Vector3(-width / 2, -len / 2), new three.Vector3(0, len / 2),
new three.Vector3(0, len / 2), new three.Vector3(width / 2, -len / 2)
];
}
else if (openDir === BoardOpenDir.Down) {
return [
new three.Vector3(-width / 2, len / 2), new three.Vector3(0, -len / 2),
new three.Vector3(0, -len / 2), new three.Vector3(width / 2, len / 2)
];
}
}
}
//为了避免Core对UI库的依赖,导致测试用例失败,导致外部项目引用失败,我们分离了这个函数
var Intent;
(function (Intent) {
Intent["NONE"] = "none";
Intent["PRIMARY"] = "primary";
Intent["SUCCESS"] = "success";
Intent["WARNING"] = "warning";
Intent["DANGER"] = "danger";
})(Intent || (Intent = {}));
const ToasterInjectFunctions = [];
function Toaster(option) {
for (let f of ToasterInjectFunctions)
f(option);
}
const ToasterShowEntityMsgInjectFunctions = [];
function ToasterShowEntityMsg(option) {
for (let f of ToasterShowEntityMsgInjectFunctions)
f(option);
let logMsgs = [{ msg: option.msg }];
if (option.ent)
logMsgs.push({ msg: "点击查看", entity: Array.isArray(option.ent) ? option.ent : [option.ent] });
InteractionLog(logMsgs, LogType.Warning);
}
function equaln(v1, v2, fuzz = 1e-5) {
return Math.abs(v1 - v2) <= fuzz;
}
function FixIndex(index, arr) {
let count = (arr instanceof Array) ? arr.length : arr;
if (index < 0)
return count + index;
else if (index >= count)
return index - count;
else
return index;
}
/**
* @param compart true => t2 , false => t1
* @returns 索引
*/
function Max(arr, compart) {
let best = arr[0];
let bestIndex = 0;
for (let i = 1; i < arr.length; i++) {
let t1 = arr[i];
if (compart(best, t1)) {
best = t1;
bestIndex = i;
}
}
return bestIndex;
}
var EMetalsType;
(function (EMetalsType) {
EMetalsType["Metals"] = "\u4E94\u91D1";
EMetalsType["Comp"] = "\u7EC4\u4EF6";
})(EMetalsType || (EMetalsType = {}));
[EBoardKeyList.UpSealed, EBoardKeyList.DownSealed, EBoardKeyList.RightSealed, EBoardKeyList.LeftSealed];
var EFindType;
(function (EFindType) {
EFindType[EFindType["Find"] = 0] = "Find";
EFindType[EFindType["Modify"] = 1] = "Modify";
EFindType[EFindType["FindMaxSize"] = 2] = "FindMaxSize";
EFindType[EFindType["FindSplite"] = 3] = "FindSplite";
EFindType[EFindType["GetOption"] = 4] = "GetOption";
EFindType[EFindType["RemoveModeling"] = 5] = "RemoveModeling";
EFindType[EFindType["RemoveSpecialShape"] = 6] = "RemoveSpecialShape";
EFindType[EFindType["RemoveModelingAndSpecial"] = 7] = "RemoveModelingAndSpecial";
EFindType[EFindType["ModifyHardware"] = 8] = "ModifyHardware";
EFindType[EFindType["FindMinSize"] = 9] = "FindMinSize";
EFindType[EFindType["GetHardWareOption"] = 10] = "GetHardWareOption";
EFindType[EFindType["RemoveSplitSize"] = 11] = "RemoveSplitSize";
})(EFindType || (EFindType = {}));
var ECompareType;
(function (ECompareType) {
ECompareType["Equal"] = "=";
ECompareType["UnEqual"] = "!=";
ECompareType["Greater"] = ">=";
ECompareType["Less"] = "<=";
ECompareType["Include"] = "//";
})(ECompareType || (ECompareType = {}));
//门板位置类型
var DoorPosType;
(function (DoorPosType) {
DoorPosType[DoorPosType["Out"] = 0] = "Out";
DoorPosType[DoorPosType["In"] = 1] = "In";
})(DoorPosType || (DoorPosType = {}));
var HandleHorPos;
(function (HandleHorPos) {
HandleHorPos[HandleHorPos["Left"] = 0] = "Left";
HandleHorPos[HandleHorPos["Right"] = 1] = "Right";
HandleHorPos[HandleHorPos["Mid"] = 2] = "Mid";
})(HandleHorPos || (HandleHorPos = {}));
var HandleVePos;
(function (HandleVePos) {
HandleVePos[HandleVePos["Top"] = 0] = "Top";
HandleVePos[HandleVePos["Bottom"] = 1] = "Bottom";
HandleVePos[HandleVePos["Mid"] = 2] = "Mid";
})(HandleVePos || (HandleVePos = {}));
//门板开门类型
var DoorOpenDir;
(function (DoorOpenDir) {
DoorOpenDir["Left"] = "lf";
DoorOpenDir["Right"] = "rt";
DoorOpenDir["Top"] = "tp";
DoorOpenDir["Bottom"] = "bm";
DoorOpenDir["None"] = "none";
})(DoorOpenDir || (DoorOpenDir = {}));
var ELatticeArrayType;
(function (ELatticeArrayType) {
ELatticeArrayType[ELatticeArrayType["ByWidth"] = 0] = "ByWidth";
ELatticeArrayType[ELatticeArrayType["ByCount"] = 1] = "ByCount";
})(ELatticeArrayType || (ELatticeArrayType = {}));
var ForBoardNameType;
(function (ForBoardNameType) {
ForBoardNameType["Same"] = "same";
ForBoardNameType["NoSame"] = "nosame";
ForBoardNameType["Include"] = "include";
ForBoardNameType["NoInclude"] = "noinclude";
})(ForBoardNameType || (ForBoardNameType = {}));
var CurtailType;
(function (CurtailType) {
CurtailType["PerBr"] = "0";
CurtailType["Total"] = "1";
CurtailType["OCS"] = "2";
})(CurtailType || (CurtailType = {}));
var StripType;
(function (StripType) {
StripType["H"] = "h";
StripType["V"] = "v";
})(StripType || (StripType = {}));
/**
*背板靠上还是靠下
*
* @export
* @enum {number}
*/
var BehindHeightPositon;
(function (BehindHeightPositon) {
BehindHeightPositon["ForTop"] = "top";
BehindHeightPositon["ForBottom"] = "bottom";
BehindHeightPositon["AllHeight"] = "all"; //总高
})(BehindHeightPositon || (BehindHeightPositon = {}));
/**
*板件相对位置
*
* @export
* @enum {number}
*/
var ViewDirection;
(function (ViewDirection) {
ViewDirection[ViewDirection["Left"] = 1] = "Left";
ViewDirection[ViewDirection["Right"] = 2] = "Right";
ViewDirection[ViewDirection["Up"] = 3] = "Up";
ViewDirection[ViewDirection["Front"] = 4] = "Front";
ViewDirection[ViewDirection["Bottom"] = 5] = "Bottom";
ViewDirection[ViewDirection["Back"] = 6] = "Back";
ViewDirection[ViewDirection["Southwest"] = 7] = "Southwest";
})(ViewDirection || (ViewDirection = {}));
var ViewportPosition;
(function (ViewportPosition) {
ViewportPosition["Vertical"] = "vertical";
ViewportPosition["Horizontal"] = "horizontal";
ViewportPosition["Left"] = "left";
ViewportPosition["Right"] = "right";
ViewportPosition["Bottom"] = "bottom";
ViewportPosition["Top"] = "top";
})(ViewportPosition || (ViewportPosition = {}));
var RadioType;
(function (RadioType) {
RadioType["lefttop"] = "1";
RadioType["leftbottom"] = "2";
RadioType["righttop"] = "3";
RadioType["rightbottom"] = "4";
})(RadioType || (RadioType = {}));
var EWineRackType;
(function (EWineRackType) {
EWineRackType[EWineRackType["Oblique"] = 0] = "Oblique";
EWineRackType[EWineRackType["Upright"] = 1] = "Upright";
})(EWineRackType || (EWineRackType = {}));
/**酒格样式 */
var EWineRackStyle;
(function (EWineRackStyle) {
EWineRackStyle[EWineRackStyle["WholeLattice"] = 0] = "WholeLattice";
EWineRackStyle[EWineRackStyle["Semilattice"] = 1] = "Semilattice";
})(EWineRackStyle || (EWineRackStyle = {}));
var EWRackArrayType;
(function (EWRackArrayType) {
EWRackArrayType[EWRackArrayType["ByWidth"] = 0] = "ByWidth";
EWRackArrayType[EWRackArrayType["ByCount"] = 1] = "ByCount";
EWRackArrayType[EWRackArrayType["Fixed"] = 2] = "Fixed";
})(EWRackArrayType || (EWRackArrayType = {}));
/**铺满方式 */
var EFullType;
(function (EFullType) {
EFullType[EFullType["ByHeight"] = 0] = "ByHeight";
EFullType[EFullType["ByWidth"] = 1] = "ByWidth";
EFullType[EFullType["Symmetry"] = 2] = "Symmetry";
})(EFullType || (EFullType = {}));
/**高度优先时靠左还是靠右 */
var EFullDir;
(function (EFullDir) {
EFullDir[EFullDir["Left"] = 0] = "Left";
EFullDir[EFullDir["Right"] = 1] = "Right";
})(EFullDir || (EFullDir = {}));
var EOrderType;
(function (EOrderType) {
EOrderType["ByCreate"] = "create_date desc";
EOrderType["ByCreate2"] = "create_date";
EOrderType["ByUpdate"] = "update_date desc";
EOrderType["ByUpdate2"] = "update_date";
EOrderType["ByName"] = "name";
EOrderType["ByName2"] = "name desc";
})(EOrderType || (EOrderType = {}));
const DefaultLayerBoardConfig = {
version: 3,
type: BoardType.Layer,
name: "层板",
frontShrink: 0,
leftShrink: 0,
rightShrink: 0,
calcHeight: "W",
isTotalLength: true,
boardRelative: BrRelativePos.Div,
thickness: 18,
count: 1,
spaceSize: 300,
isActive: false,
calcSpaceSize: "0",
calcFrontShrink: "0",
calcLeftShrink: "0",
calcRightShrink: "0",
};
Object.freeze(DefaultLayerBoardConfig);
const DefaultVerticalBoardConfig = {
version: 3,
type: BoardType.Vertical,
name: "立板",
frontShrink: 0,
bottomShrink: 0,
calcWidth: "W",
calcHeight: "H",
isTotalLength: true,
isTotalWidth: true,
boardRelative: BrRelativePos.Div,
thickness: 18,
count: 1,
spaceSize: 0,
calcSpaceSize: "0",
calcBottomShrink: "0",
calcFrontShrink: "0",
};
Object.freeze(DefaultVerticalBoardConfig);
const DefaultBehindBoardConfig = {
version: 2,
type: BoardType.Behind,
name: "背板",
leftExt: 0,
rightExt: 0,
topExt: 0,
bottomExt: 0,
thickness: 18,
boardPosition: BehindHeightPositon.AllHeight,
calcHeight: "H",
moveDist: 0,
boardRelative: BrRelativePos.Back,
spaceSize: 0,
count: 1,
calcSpaceSize: "0",
calcMoveDist: "0",
};
Object.freeze(DefaultBehindBoardConfig);
const DefaultWineRackConfig = {
version: 5,
type: EWineRackType.Oblique,
wineRackStyle: EWineRackStyle.WholeLattice,
arrayType: EWRackArrayType.ByWidth,
fullType: EFullType.ByWidth,
isFull: false,
isLock: false,
fullDir: EFullDir.Left,
heightCount: 3.5,
widthCount: 3.5,
isTotalDepth: true,
depth: 0,
gripWidth: 100,
calcDepth: "W",
boardThick: 18,
grooveWidthAdd: 0,
leftEdge: 1,
rightEdge: 1,
topEdge: 1,
bottomEdge: 1,
frontCut: 0,
leftCut: 0,
rightCut: 0,
topCut: 0,
grooveLengthAdd: 3,
isDrawLy: false,
isDrawVer: false,
brThick2: 18,
isExtendsBH2: false,
followNarrow: false,
useBoardProcessOption: true
};
Object.freeze(DefaultWineRackConfig);
const DefaultTopBoardOption = {
version: 2,
type: BoardType.Layer,
name: "顶板",
isDraw: true,
thickness: 18,
frontDist: 0,
behindDistance: 0,
isWrapSide: false,
useLFData: true,
leftExt: 0,
rightExt: 0,
offset: 0,
};
Object.freeze(DefaultTopBoardOption);
const DefaultBottomBoardOption = {
version: 2,
type: BoardType.Layer,
name: "底板",
isDraw: true,
thickness: 18,
frontDist: 0,
behindDistance: 0,
isWrapSide: false,
useLFData: true,
leftExt: 0,
rightExt: 0,
offset: 80,
footThickness: 18,
isDrawFooter: true,
footBehindShrink: 0,
isDrawBackFooter: false,
isDrawStrengthenStrip: false,
footerOffset: 0,
divCount: 1,
};
Object.freeze(DefaultBottomBoardOption);
const DefaultSideBoardOption = {
version: 2,
type: BoardType.Vertical,
name: "",
height: 2000,
width: 600,
thickness: 18,
spaceSize: 1200,
leftShrink: 0,
rightShrink: 0,
};
Object.freeze(DefaultSideBoardOption);
const DefaultViewportConfigOption = {
view: ViewDirection.Up,
renderType: RenderType.Print,
revertRotation: false
};
Object.freeze(DefaultViewportConfigOption);
const DefaultViewport2ConfigOption = {
viewportPosition: ViewportPosition.Vertical,
renderType: [RenderType.Print, RenderType.Print],
view1: ViewDirection.Left,
view2: ViewDirection.Up,
revertRotation: false
};
Object.freeze(DefaultViewport2ConfigOption);
const DefaultViewport3ConfigOption = {
viewportPosition: ViewportPosition.Vertical,
renderType: [RenderType.Print, RenderType.Print, RenderType.Print],
view: [ViewDirection.Front, ViewDirection.Up, ViewDirection.Southwest],
revertRotation: false
};
Object.freeze(DefaultViewport3ConfigOption);
const DefaultViewport4ConfigOption = {
view: [ViewDirection.Front, ViewDirection.Left, ViewDirection.Up, ViewDirection.Southwest],
renderType: [RenderType.Print, RenderType.Print, RenderType.Print, RenderType.Print],
revertRotation: false
};
Object.freeze(DefaultViewport4ConfigOption);
const DefaultModifyTextsOption = {
changeTexts: Array.from({ length: 5 }, () => ["", ""]),
};
Object.freeze(DefaultModifyTextsOption);
const DefaultPointLightOption = {
version: 1,
lightColor: "#FFFFFF",
temperature: 6500,
Intensity: 100,
IndirectLightingIntensity: 1,
SpecularScale: 1,
SourceRadius: 10,
SoftSourceRadius: 0,
SourceLength: 0,
CaseShadow: true,
};
Object.freeze(DefaultPointLightOption);
const DefaultSpotLightOption = {
version: 1,
lightColor: "#FFFFFF",
temperature: 6500,
Intensity: 100,
IndirectLightingIntensity: 1,
SpecularScale: 1,
SourceRadius: 0,
SoftSourceRadius: 30,
SourceLength: 0,
Angle: 45,
InnerConeAngle: 0,
AttenuationRadius: 300,
CaseShadow: true,
ShowHelper: true,
};
Object.freeze(DefaultSpotLightOption);
const DefaultRectAreaLightOption = {
version: 1,
lightColor: "#FFFFFF",
temperature: 6500,
Intensity: 100,
IndirectLightingIntensity: 1,
SpecularScale: 1,
AttenuationRadius: 300,
Width: 150,
Height: 150,
BarnDoorAngle: 90,
BarnDoorLength: 20,
CaseShadow: true,
ShowHelper: true,
};
Object.freeze(DefaultRectAreaLightOption);
const DefaultRightPlaneLightOption = {
version: 2,
ShowHemiLight: true,
ShowSunLight: true,
SkyLightColor: "#FFFFFF",
SkyLightIntensity: 1,
SkyLightIndirectLightingIntensity: 1,
SunLightIntensity: 50,
SunLightIndirectLightingIntensity: 1,
SunLightColor: "#FFFFFF",
SunLightTemperature: 6500,
SunLightElevationDeg: 60,
SunLightRotateDeg: 300,
ShowExposure: true,
AutoExposure: false,
ExposureCompensation: 0,
SunTime: "默认",
};
Object.freeze(DefaultRightPlaneLightOption);
const DefaultSingleBoardOption = {
version: 1,
name: "层板",
type: BoardType.Layer,
height: 1200,
width: 600,
thickness: 18,
rotateX: 0,
rotateY: 0,
rotateZ: 0,
drawNumber: 1
};
Object.freeze(DefaultSingleBoardOption);
const DefaultClosingStripOption = {
version: 3,
type: BoardType.Vertical,
name: "收口条",
striptype: StripType.H,
boardRelative: BrRelativePos.Left,
width: 54,
thickness: 18,
frontShrink: 0,
isDrawFuZhu: true,
fzWidth: 80,
fzThickness: 18,
addSKTCabinetName: true
};
Object.freeze(DefaultClosingStripOption);
const DefaultBoardFindOption = {
version: 9,
condition: {
// layer: false, //这个KEY看起来没用了
height: false,
width: false,
thickness: false,
useWood: false,
useDrill: false,
useNail: false,
useDoor: false,
useDim: false,
useSpecial: false,
useModeling: false,
roomName: false,
hardwareName: false,
cabinetName: false,
brName: false,
material: false,
lines: false,
bigHoleDir: false,
drillType: false,
useKeyWord: false,
addRemarks: false,
composingFace: false,
openDir: false,
sealedUp: false,
sealedDown: false,
sealedLeft: false,
sealedRight: false,
upDrill: false,
downDrill: false,
leftDrill: false,
rightDrill: false,
useZhengFanDrill: false,
useChaidan: false,
[EBoardKeyList.KnifeRad]: false,
edgeRemarkUp: false,
edgeRemarkDown: false,
edgeRemarkLeft: false,
edgeRemarkRight: false,
},
compareType: {
height: ECompareType.Equal,
width: ECompareType.Equal,
thickness: ECompareType.Equal,
roomName: ECompareType.Equal,
cabinetName: ECompareType.Equal,
brName: ECompareType.Equal,
hardwareName: ECompareType.Equal,
[EBoardKeyList.Mat]: ECompareType.Equal,
[EBoardKeyList.Color]: ECompareType.Equal,
[EBoardKeyList.BrMat]: ECompareType.Equal,
lines: ECompareType.Equal,
bigHoleDir: ECompareType.Equal,
drillType: ECompareType.Equal,
composingFace: ECompareType.Equal,
openDir: ECompareType.Equal,
[EBoardKeyList.KnifeRad]: ECompareType.Equal,
},
tolerance: {
height: "",
width: "",
thickness: "",
[EBoardKeyList.KnifeRad]: "",
},
// layer: "0", 删除无用的key
height: "",
width: "",
thickness: "",
roomName: "",
cabinetName: "",
brName: "",
hardwareName: "",
[EBoardKeyList.BrMat]: "",
material: "",
color: "",
lines: LinesType.Positive,
bigHoleDir: FaceDirection.Front,
drillType: "",
composingFace: ComposingType.Positive,
openDir: BoardOpenDir.None,
hardwareDoorName: "",
sealedUp: "",
sealedDown: "",
sealedLeft: "",
sealedRight: "",
highDrill: [],
upDownDrill: [true, true],
isClose: false,
remarks: Array.from({ length: 10 }, () => ["", ""]),
extraRemarks: Array.from({ length: 10 }, () => ["", ""]),
isChaidan: false,
[EBoardKeyList.KnifeRad]: "",
edgeRemarkUp: "",
edgeRemarkDown: "",
edgeRemarkLeft: "",
edgeRemarkRight: "",
};
Object.freeze(DefaultBoardFindOption);
const DefaultLatticOption = {
version: 3,
arrayType: ELatticeArrayType.ByWidth,
gripWidth: 100,
gripDepth: 100,
widthCount: 3,
depthCount: 4,
knifeRad: 3,
thickness: 18,
arcLen: 50,
downDist: 0,
space: 0.2,
grooveAddWidth: 0.2,
upSealed: 1,
downSealed: 1,
leftSealed: 1,
rightSealed: 1,
isAuto: true,
isChange: true,
isOpenCut: false,
upCut: 0,
downCut: 4,
linesType: LinesType.Reverse,
useBoardProcessOption: true,
};
Object.freeze(DefaultLatticOption);
const DefaultDoorOption = {
version: 9,
col: 2,
row: 1,
isAllSelect: true,
topOffset: 0,
bottomOffset: 0,
doorPosType: DoorPosType.Out,
offset: 0,
topExt: 18,
bottomExt: 18,
leftExt: 18,
rightExt: 18,
topSpace: 2,
bottomSpace: 2,
leftSpace: 2,
rightSpace: 2,
midSpace: 2,
thickness: 18,
depth: 0,
isAuto: true,
boardName: "",
doorThickness: 18,
topBrSeal: 1,
bottomBrSeal: 1,
leftBrSeal: 1,
rightBrSeal: 1,
topDoorSeal: 1,
bottomDoorSeal: 1,
cbHightDrillOption: { up: "", down: "", left: "", right: "" },
leftDoorSeal: 1,
rightDoorSeal: 1,
handleAngle: 0,
handleHorPos: HandleHorPos.Right,
horSpacing: 50,
handleVePos: HandleVePos.Mid,
veSpacing: 10,
hingeCount: 0,
hindeTopDist: 0,
hindeBottomDist: 0,
downOffsetExpr: "0",
upOffsetExpr: "0",
useRule: false,
changeTemplateBoardNameOfOpenDir: true,
frontAndBackDrill: false,
verticalBoardName: "立板",
layerBoardName: "层板",
lbSealedUp: 1,
lbSealedDown: 1,
lbSealedLeft: 1,
lbSealedRight: 1,
lbHightDrillOption: { up: "", down: "", left: "", right: "" },
useBoardProcessOption: true,
isModifyHardwareMaterial: false,
deviation: 100 //偏移量
};
Object.freeze(DefaultDoorOption);
const DefaultHingeOption = {
hingeCount: 0,
hindeTopDist: 0,
hindeBottomDist: 0,
useRule: false,
deviation: 100
};
Object.freeze(DefaultHingeOption);
const DefaultDrawerOption = {
version: 7,
col: 1,
row: 1,
isAllSelect: true,
topOffset: 0,
bottomOffset: 0,
doorPosType: DoorPosType.Out,
offset: 0,
topExt: 18,
bottomExt: 18,
leftExt: 18,
rightExt: 18,
topSpace: 2,
bottomSpace: 2,
leftSpace: 2,
rightSpace: 2,
midSpace: 2,
thickness: 18,
depth: 0,
isAuto: true,
boardName: "",
handleAngle: 90,
handleHorPos: HandleHorPos.Mid,
horSpacing: 10,
handleVePos: HandleVePos.Mid,
veSpacing: 10,
drawerTotalDepth: 0,
trackDepth: 0,
isAutoSelectTrack: true,
isLockTopOffset: false,
isLockBottomOffset: false,
downOffsetExpr: "0",
upOffsetExpr: "0",
verticalBoardName: "立板",
lbSealedUp: 1,
lbSealedDown: 1,
lbSealedLeft: 1,
lbSealedRight: 1,
lbHightDrillOption: { up: "", down: "", left: "", right: "" },
useBoardProcessOption: true,
isModifyHardwareMaterial: false,
};
Object.freeze(DefaultDrawerOption);
const DefaultBoardBatchCurtailOption = {
version: 1,
type: CurtailType.Total,
front: 0,
back: 0,
left: 0,
right: 0,
moveBrs: false,
};
Object.freeze(DefaultBoardBatchCurtailOption);
const DefaultBatchModifyPanelOption = {
version: 1,
length: "L",
width: "W",
thick: "H",
position: RadioType.lefttop,
};
Object.freeze(DefaultBatchModifyPanelOption);
const DefaultLatticeConfig = {
version: 1,
arrayType: ELatticeArrayType.ByWidth,
gripWidth: 100,
gripDepth: 100,
widthCount: 3,
depthCount: 4,
knifeRad: 3,
thickness: 18,
arcLen: 50,
downDist: 0,
space: 0.5,
grooveAddWidth: 0,
upSealed: 1,
downSealed: 0,
leftSealed: 0,
rightSealed: 0,
isAuto: true,
isChange: true,
isOpenCut: false,
upCut: 0,
downCut: 4,
linesType: LinesType.Reverse,
useBoardProcessOption: true
};
Object.freeze(DefaultLatticeConfig);
const DefaultNailOption = {
version: 1,
isDraw: true,
addCount: 0,
dist: 50,
isGroup: false,
isInBack: false,
front: 50,
behind: 50,
count: 2,
rad: 2.5,
length: 34,
depth: 11
};
Object.freeze(DefaultNailOption);
const DefaultCylinederMetalsOption = {
version: 2,
rad: 50,
height: 200,
name: "圆柱体",
unit: "",
roomName: "",
cabinetName: "",
costExpr: "L*R*R*3.14",
actualExpr: "L*R*R*3.14*3",
model: "X-1",
factory: "晨丰",
brand: "晨丰",
spec: "个",
count: "1",
comments: "",
isHole: true,
};
Object.freeze(DefaultCylinederMetalsOption);
const DefaultExtruderMetalsOption = {
version: 1,
thickness: 100,
knifeRad: 0,
isHole: true,
addLen: 0,
name: "拉伸实体",
unit: "",
roomName: "",
cabinetName: "",
costExpr: "L*W*H*100",
actualExpr: "L*W*H*200",
model: "X-1",
factory: "晨丰",
brand: "晨丰",
spec: "个",
count: "1",
comments: "",
};
Object.freeze(DefaultExtruderMetalsOption);
const DefaultCompositeMetalsOption = {
version: 2,
type: EMetalsType.Metals,
isSplite: false,
isSplitePrice: false,
name: "复合实体",
unit: "",
roomName: "",
cabinetName: "",
costExpr: "L*W*H*100",
actualExpr: "L*W*H*300",
model: "X-1",
factory: "晨丰",
brand: "晨丰",
spec: "个",
count: "1",
color: "",
material: "",
comments: "",
isHole: true,
};
Object.freeze(DefaultCompositeMetalsOption);
const DefaultToplineMetalsOption = {
version: 3,
name: "顶线",
unit: "毫米",
roomName: "",
cabinetName: "",
costExpr: "",
actualExpr: "",
model: "",
factory: "",
brand: "",
spec: "",
comments: "",
addLen: "0",
isHole: false,
};
Object.freeze(DefaultToplineMetalsOption);
const DefaultBoardProcessOption = {
version: 4,
roomName: "",
cabinetName: "",
boardName: "",
material: "",
color: "",
lines: LinesType.Positive,
bigHoleDir: FaceDirection.Front,
drillType: "",
composingFace: ComposingType.Arbitrary,
highSealed: [],
sealedUp: "1",
sealedDown: "1",
sealedLeft: "1",
sealedRight: "1",
spliteHeight: "",
spliteWidth: "",
spliteThickness: "",
highDrill: [],
frontDrill: true,
backDrill: true,
remarks: [],
useBoardProcessOption: true,
edgeRemarkUp: "",
edgeRemarkDown: "",
edgeRemarkLeft: "",
edgeRemarkRight: "",
highBoardEdgeRemark: [],
};
Object.freeze(DefaultBoardProcessOption);
const DefaultCurve2RecOption = {
version: 1,
isSaveMax: false,
isSaveSmall: true,
width: 90,
isAnaly: true,
gap: 3,
forceUseUCS: false,
ForceUseFrontViewCS: false,
};
Object.freeze(DefaultCurve2RecOption);
const DefaultUpdateInfoOption = {
version: 3,
[EBoardKeyList.BrName]: "",
[EBoardKeyList.RoomName]: "",
[EBoardKeyList.CabinetName]: "",
[EBoardKeyList.Lines]: LinesType.Positive,
[EBoardKeyList.BigHole]: FaceDirection.Front,
[EBoardKeyList.DrillType]: "",
[EBoardKeyList.ComposingFace]: ComposingType.Arbitrary,
upDownDrill: [true, true],
[EBoardKeyList.UpSealed]: "1", //封边上下左右
[EBoardKeyList.DownSealed]: "1",
[EBoardKeyList.LeftSealed]: "1",
[EBoardKeyList.RightSealed]: "1",
[EBoardKeyList.KnifeRad]: "3",
remarks: Array.from({ length: 10 }, () => ["", ""]),
[EBoardKeyList.BrMat]: "",
[EBoardKeyList.Mat]: "",
[EBoardKeyList.Color]: "",
grooveAddDepth: "0",
grooveAddLength: "0",
grooveAddWidth: "0",
highDrill: [],
isChaiDan: true,
edgeRemarkUp: "",
edgeRemarkDown: "",
edgeRemarkLeft: "",
edgeRemarkRight: "",
condition: {
[EBoardKeyList.BrName]: false,
[EBoardKeyList.RoomName]: false,
[EBoardKeyList.CabinetName]: false,
[EBoardKeyList.Lines]: true,
[EBoardKeyList.BigHole]: true,
[EBoardKeyList.DrillType]: true,
[EBoardKeyList.ComposingFace]: true,
[EBoardKeyList.UpSealed]: true,
[EBoardKeyList.DownSealed]: true,
[EBoardKeyList.LeftSealed]: true,
[EBoardKeyList.RightSealed]: true,
useZhengFanDrill: true,
remarks: true,
[EBoardKeyList.KnifeRad]: true,
[EBoardKeyList.Mat]: true,
grooveAddDepth: true,
grooveAddLength: true,
grooveAddWidth: true,
upDrill: true,
downDrill: true,
leftDrill: true,
rightDrill: true,
isChaiDan: true,
autoCutOption: { isAutoCut: false, isRelevance: false },
edgeRemarkUp: false,
edgeRemarkDown: false,
edgeRemarkLeft: false,
edgeRemarkRight: false,
}
};
Object.freeze(DefaultUpdateInfoOption);
const DefaultKuGanOption = {
insertMode: "0",
minSpacing: 50,
count: 1,
isHor: false,
depth: 0,
isDefault: true,
leftDist: 0,
rightDist: 0,
upDist: 0,
downDist: 0,
};
Object.freeze(DefaultKuGanOption);
const DefaultParseBoardNameOPtion = {
version: 3,
verticalBrShrink: 0,
layerBrShrink: 0,
topBrShrink: 0,
bottomBrShrink: 0,
groundLineBrShrink: 0,
farLeftVerticalBrName: "左侧板",
farRightVerticalBrName: "右侧板",
middleVerticalBrName: "立板",
topMostLayerBrName: "顶板",
middleLayerBrName: "层板",
bottomMostLayerBrName: "底板",
bottomMostBackBrName: "地脚线",
stripeBrName: "收口条",
cabinetName: "",
isfarLeftVerticalBrName: true, //最左侧立板名称
isModifyMiddleVerticalBrName: false, //是否修改中间立板名称
isfarRightVerticalBrName: true,
istopMostLayerBrName: true,
isModifyMiddleLayerBrName: false, //是否修改中间层板名称
isbottomMostLayerBrName: true,
isbottomMostBackBrName: true,
isstripeBrName: true,
iscabinetName: false, //修改柜名
isModifyRoomName: true, //修改房名
isMultiBackBr: false,
isBack: true,
backName: "背板",
isAloneStripName: true, //收口条名字独立
};
Object.freeze(DefaultParseBoardNameOPtion);
const DefaultR2bOption = {
version: 8,
cabinetDeep: 400,
cabinetBrThick: 18,
cabinetCurtail: 0,
backBrThick: 18,
backBrBiggerThanHeight: 200,
backBrBiggerThanWidth: 200,
backBrFrontMove: 0,
backBrLeftExtend: 0,
backBrRightExtend: 0,
backBrUpExtend: 0,
backBrDownExtend: 0,
...DefaultParseBoardNameOPtion,
grooveOption: {
grooveAddLength: "0",
grooveAddWidth: "0",
grooveAddDepth: "0",
knifeRadius: "3",
},
roomName: "",
boardMatName: "",
material: "",
color: "",
drillType: "",
sealedDown: "1",
sealedLeft: "1",
sealedRight: "1",
sealedUp: "1",
backBrUseTemplate: false,
backBrTemplate: null,
backBrTemplateId: "",
remarks: Array.from({ length: 12 }, () => ["", ""]),
maxThickness: 20,
useBrName: true,
configName: "",
backBrName: "背板",
behindIsRelative: false,
footerThickness: 18,
closeStripThickness: 18,
useSktTemplate: false,
sktTemplate: null,
sktTemplateId: "",
rightSktTemplateId: "",
topSktTemplateId: "",
};
Object.freeze(DefaultR2bOption);
const DefaultR2b2Option = {
version: 1,
depthExpr: "W",
drillType: "",
sealedDown: "1",
sealedLeft: "1",
sealedRight: "1",
sealedUp: "1",
remarks: Array.from({ length: 12 }, () => ["", ""]),
maxThickness: 20,
layerShrink: 0,
vertialShrink: 0,
};
Object.freeze(DefaultR2b2Option);
({
version: 5,
isAll: true,
isHide: true,
isDelete: false,
behind: false,
layer: false,
vertial: false,
footer: false,
tbBoard: false,
specialShape: false,
hole: false,
door: false,
drawer: false,
closingStrip: false,
noChaiDan: false,
winerack: false,
wood: false,
nails: false,
topline: false,
handle: false,
hinge: false,
hwComposity: false,
lattice: false,
dim: false,
visual: false,
curve: false,
line: false,
polyline: false,
circle: false,
arc: false,
custom: false,
customBoardName: "",
matchType: ECompareType.Equal
});
const DefaultCommonPanelOption = {
version: 1,
orderMap: {},
orderType: EOrderType.ByUpdate,
};
Object.freeze(DefaultCommonPanelOption);
const DefaultDatalistOption = {
resizeUI: {}
};
Object.freeze(DefaultDatalistOption);
const DefaultAutoDimBrsOption = {
version: 1,
total: true,
out: true,
inW: false,
inH: false,
noRepeat: false,
noSmSize: false,
noAppointSize: false,
noInSize: false,
noShowMinSize: 20,
noShowMinInSize: 300,
noShowAppointSizes: "",
useParseGroups: "0",
forBoardName: false,
forBoardNameStr: "",
forBoardNameType: ForBoardNameType.Same,
};
Object.freeze(DefaultAutoDimBrsOption);
const DefaultWindowPanelOption = {
Length: 1100,
Height: 1500,
Thick: 280,
WindowOffGround: 900,
IsBayWindow: false,
BayLeftIsWall: false,
BayRightIsWall: false,
BayDist: 500,
BayLeftDist: 600,
BayMiddleDist: 600,
BayRightDist: 600,
HasWindowStone: false,
StoneThick: 50,
StoneBulge: 50,
StoneLeftRightBulge: 50,
};
Object.freeze(DefaultWindowPanelOption);
const DefaultDimStyleOption = {
dimFXLON: true,
dimFXL: 100,
dimALTD: 2,
dimASZ: 10,
dimGAP: 10,
dimEXE: 20,
dimTXT: 60,
dimTAD: 2,
dimADEC: 2,
};
Object.freeze(DefaultDimStyleOption);
const DefaultChangeColorByBoardMaterialOption = {
accordThickness: false,
accordMaterialColor: true,
accordMaterial: true,
accordMaterialName: true
};
Object.freeze(DefaultChangeColorByBoardMaterialOption);
const DefaultShareBoardInfConfigurationOption = {
Physical2EdgeColor: 7,
VisualStyle: RenderType.Conceptual,
Viewport: ViewDirection.Southwest,
IsExportBoard: true,
IsExportHardware: true,
showBom: true,
};
Object.freeze(DefaultShareBoardInfConfigurationOption);
const DefaultBulkheadCeilingOption = {
Item: []
};
Object.freeze(DefaultBulkheadCeilingOption);
const DefaultChangeColorByRoomOrCabinetOption = {
accordCabinetName: true,
accordRoomName: true
};
Object.freeze(DefaultChangeColorByRoomOrCabinetOption);
const DefaultDoorRelatesInfoOption = {
version: 1,
hingeOption: []
};
Object.freeze(DefaultDoorRelatesInfoOption);
({
filterSmallSize: false,
filterSmallSizeValue: 0,
filterAppointSize: false,
filterAppointSizeValues: "",
filterAppointForBoardName: false,
filterAppointForBoardNameValues: "",
conditionType: ForBoardNameType.Same,
});
/**
* 使用轮廓和扫描路径构建扫描几何体,实现衣柜中的顶线或者地脚线之类的实体.
* 该几何体需要轮廓和路径的起始截面垂直,否则构造的实体将会错误.
*/
class SweepGeometry extends three.Geometry {
constructor(contour, path, ShapeMaterialSlotData) {
super();
this.edgePts = [];
this.polygonIndexes = [];
this.ShapeMaterialSlotData = ShapeMaterialSlotData;
if (Array.isArray(path))
this.AddShape2(contour, path);
else
this.AddShape(contour, path);
this.computeVertexNormals();
this.computeFaceNormals();
}
get LineGeom() {
let lineGeo = new LineGeometry.LineGeometry();
let lineSegments = new Float32Array(this.edgePts);
var instanceBuffer = new three.InstancedInterleavedBuffer(lineSegments, 6, 1);
lineGeo.setAttribute('instanceStart', new three.InterleavedBufferAttribute(instanceBuffer, 3, 0));
lineGeo.setAttribute('instanceEnd', new three.InterleavedBufferAttribute(instanceBuffer, 3, 3));
return lineGeo;
}
get EdgeGeom() {
return new three.BufferGeometry().setAttribute('position', new three.Float32BufferAttribute(this.edgePts, 3));
}
AddShape(contour, path) {
//路径点表
let pathPts2d = path.Shape.getPoints(4);
let pathPts = pathPts2d.map(AsVector3);
arrayRemoveDuplicateBySort(pathPts, equalv3);
for (let p of pathPts)
p.applyMatrix4(path.OCSNoClone);
let shapePts2d = contour.Shape.getPoints(4);
if (!three.ShapeUtils.isClockWise(shapePts2d))
shapePts2d.reverse();
//轮廓点表
let shapePts3d = shapePts2d.map(AsVector3);
for (let p of shapePts3d)
p.applyMatrix4(contour.OCSNoClone);
let isClosePath = path.IsClose;
let verts = []; //所有路径上的轮廓点
let pathNormal = path.Normal;
//计算所有需要的几何点,本质是不断的投影
if (!isClosePath)
verts.push(ProjectionToPlane(shapePts3d, pathNormal, pathPts[0], undefined, pathPts[1]));
//遍历所有的路径节点进行顶点投射
for (let i = 1; i < pathPts.length; i++) {
if (i === pathPts.length - 1) {
if (isClosePath)
verts.push(ProjectionToPlane(shapePts3d, pathNormal, pathPts[i], pathPts[i - 1], pathPts[1]));
else
verts.push(ProjectionToPlane(shapePts3d, pathNormal, pathPts[i], pathPts[i - 1]));
}
else {
verts.push(ProjectionToPlane(shapePts3d, pathNormal, pathPts[i], pathPts[i - 1], pathPts[i + 1]));
}
}
if (isClosePath)
verts.unshift(verts[verts.length - 1]);
this.BuildSideFaces(shapePts2d, pathPts2d, pathPts, verts);
if (!isClosePath)
this.BuildLid(shapePts2d, verts);
}
AddShape2(contour, paths) {
let pathPts = [];
let pathNormals = [];
//路径点表
for (let path of paths) {
let pathPts2d = path.Shape.getPoints(4);
arrayRemoveDuplicateBySort(pathPts2d, (p1, p2) => {
if (equalv2(p1, p2)) {
p2["_mask_"] = p1["_mask_"];
return true;
}
return false;
});
if (path !== paths[0])
pathPts2d.shift();
let pNormal = path.Normal;
for (let p of pathPts2d) {
let p3 = AsVector3(p).applyMatrix4(path.OCSNoClone);
p3["_mask_"] = p["_mask_"];
pathPts.push(p3);
pathNormals.push(pNormal);
}
}
let shapePts2d = contour.Shape.getPoints(4);
if (!three.ShapeUtils.isClockWise(shapePts2d))
shapePts2d.reverse();
//轮廓点表
let shapePts3d = shapePts2d.map(AsVector3);
for (let p of shapePts3d)
p.applyMatrix4(contour.OCSNoClone);
let isClosePath = equalv3(pathPts[0], pathPts[pathPts.length - 1], 1e-3);
let verts = []; //所有路径上的轮廓点
//计算所有需要的几何点,本质是不断的投影
if (!isClosePath)
verts.push(ProjectionToPlane(shapePts3d, pathNormals[0], pathPts[0], undefined, pathPts[1]));
//遍历所有的路径节点进行顶点投射
for (let i = 1; i < pathPts.length; i++) {
if (i === pathPts.length - 1) {
if (isClosePath)
verts.push(ProjectionToPlane(shapePts3d, pathNormals[i], pathPts[i], pathPts[i - 1], pathPts[1]));
else
verts.push(ProjectionToPlane(shapePts3d, pathNormals[i], pathPts[i], pathPts[i - 1]));
}
else {
verts.push(ProjectionToPlane(shapePts3d, pathNormals[i], pathPts[i], pathPts[i - 1], pathPts[i + 1]));
}
}
if (isClosePath)
verts.unshift(verts[verts.length - 1]);
this.BuildSideFaces(shapePts2d, pathPts, pathPts, verts);
if (!isClosePath)
this.BuildLid(shapePts2d, verts);
}
/**
* 使用4点构建面
* @param a 左下
* @param b 右下
* @param c 左上
* @param d 右上
* @param uvs
* @param [materialIndex]
*/
BuildFace4(a, b, c, d, uvs, materialIndex) {
let f1 = new three.Face3(a, b, c, undefined, undefined, materialIndex);
let f2 = new three.Face3(b, d, c, undefined, undefined, materialIndex);
this.faces.push(f1, f2);
this.faceVertexUvs[0].push([uvs[0].clone(), uvs[1].clone(), uvs[2].clone()], [uvs[1].clone(), uvs[3].clone(), uvs[2].clone()]);
}
/**
* 构造边缘面开始标记
* @param dir 前进方向(单位向量)
*/
SideStartMark(dir) { }
BuildSideFaces(shapePts2d, pathPts2d, pathPts, verts) {
let addCount = 0; //补充个数
shapePts2d[0]["_mask_"] = true;
for (let p of shapePts2d)
if (p["_mask_"]) {
addCount++;
if (this.ShapeMaterialSlotData)
p["_material_index_"] = this.ShapeMaterialSlotData[addCount - 1];
}
let sumCount = addCount + shapePts2d.length; //实际个数
let vs = [0]; //vs 对应 y轴
for (let i = 1; i < shapePts2d.length; i++)
vs.push((vs[i - 1] + shapePts2d[i].distanceTo(shapePts2d[i - 1]) * 1e-3));
let lastStartX = 0;
for (let pathIndex = 0; pathIndex < verts.length; pathIndex++) {
let pts = verts[pathIndex];
let pts2 = verts[FixIndex(pathIndex + 1, verts)];
let startIndex = this.vertices.length;
let pBase = pts[0];
let p1 = pathPts[pathIndex];
let p2 = pathPts[FixIndex(pathIndex + 1, pathPts.length)];
let p1Dir = p2.clone().sub(p1).normalize();
this.SideStartMark(p1Dir);
let tempStartX = 0;
let lastMaterialIndex = undefined;
for (let contourIndex = 0; contourIndex < shapePts2d.length; contourIndex++) {
let p1 = pts[contourIndex];
let p2 = pts2[contourIndex];
let p2d = shapePts2d[contourIndex];
if (p2d["_mask_"])
lastMaterialIndex = p2d["_material_index_"] ?? lastMaterialIndex;
if (pathIndex !== verts.length - 1)
if (contourIndex === 0 || p2d["_mask_"])
this.edgePts.push(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z);
if (contourIndex === 0 || p2d["_mask_"])
this.vertices.push(p1); //补点
if (pathIndex !== verts.length - 1) {
let curIndex = this.vertices.length;
let nextIndex = startIndex + FixIndex(curIndex - startIndex + 1, sumCount);
let curIndex2 = curIndex + sumCount;
let nextIndex2 = nextIndex + sumCount;
let x1 = lastStartX + p1.clone().sub(pBase).dot(p1Dir) * 1e-3;
let x2 = lastStartX + pts[FixIndex(contourIndex + 1, shapePts2d)].clone().sub(pBase).dot(p1Dir) * 1e-3;
let x3 = lastStartX + p2.clone().sub(pBase).dot(p1Dir) * 1e-3;
let x4 = lastStartX + pts2[FixIndex(contourIndex + 1, shapePts2d)].clone().sub(pBase).dot(p1Dir) * 1e-3;
if (contourIndex === 0)
tempStartX = x3;
let v1 = vs[contourIndex];
let v2 = vs[FixIndex(contourIndex + 1, vs)];
let uvs = [
new three.Vector2(v1, x1),
new three.Vector2(v2, x2),
new three.Vector2(v1, x3),
new three.Vector2(v2, x4),
];
this.BuildFace4(curIndex, nextIndex, curIndex2, nextIndex2, uvs, lastMaterialIndex);
}
this.vertices.push(p1);
}
lastStartX = tempStartX;
if (pathPts2d[FixIndex(pathIndex + 1, verts)]["_mask_"]) {
for (let contourIndex = 0; contourIndex < shapePts2d.length; contourIndex++) {
let p1 = pts2[contourIndex];
let p2d = shapePts2d[contourIndex];
if (contourIndex === 0 || p2d["_mask_"])
this.vertices.push(p1); //补点
this.vertices.push(p1);
let p2 = pts2[FixIndex(contourIndex + 1, pts2)];
this.edgePts.push(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z);
}
}
}
}
BuildLid(shapePts2d, verts) {
//轮廓三角网格索引
let faces = three.ShapeUtils.triangulateShape(shapePts2d, []);
for (let v of shapePts2d)
v.multiplyScalar(1e-3); //作为uvs
let lastIndex = this.vertices.length;
this.vertices.push(...verts[0].map(p => p.clone()));
this.vertices.push(...verts[verts.length - 1].map(p => p.clone()));
for (let i = 0; i < faces.length; i++) {
let [a, b, c] = faces[i];
this.faces.push(new three.Face3(lastIndex + a, lastIndex + b, lastIndex + c));
let uvs = faces[i].map(index => shapePts2d[index].clone());
this.faceVertexUvs[0].push(uvs);
this.faces.push(new three.Face3(lastIndex + verts[0].length + c, lastIndex + verts[0].length + b, lastIndex + verts[0].length + a));
this.faceVertexUvs[0].push(uvs.concat().reverse().map(v => v.clone()));
}
//构建线框
for (let i = 0; i < shapePts2d.length; i++) {
let nextIndex = FixIndex(i + 1, shapePts2d);
let pts1 = verts[0];
let p0 = pts1[i];
let p1 = pts1[nextIndex];
this.edgePts.push(p0.x, p0.y, p0.z, p1.x, p1.y, p1.z);
let pts2 = verts[verts.length - 1];
p0 = pts2[i];
p1 = pts2[nextIndex];
this.edgePts.push(p0.x, p0.y, p0.z, p1.x, p1.y, p1.z);
}
}
}
/**
* 将轮廓变换到`路径上某个点`.
*
* @param {Vector3[]} contourPts 原始的轮廓点(在世界坐标系)
* @param {Vector3} normal 路径法向量
* @param {Vector3} curP 路径上当前点
* @param {Vector3} [preP] 路径的前一个点
* @param {Vector3} [nextP] 路径下一个点
* @returns 变换后的轮廓点表
*/
function ProjectionToPlane(contourPts, normal, curP, preP, nextP) {
let pts;
if (!preP && nextP) {
let mtx = ContourTransfromToPath(curP, normal, nextP.clone().sub(curP));
pts = contourPts.map(p => p.clone().applyMatrix4(mtx));
}
else if (!nextP && preP) {
let mtx = ContourTransfromToPath(curP, normal, curP.clone().sub(preP));
pts = contourPts.map(p => p.clone().applyMatrix4(mtx));
}
else if (nextP && preP) {
let dir = curP.clone().sub(preP).normalize();
let v2 = nextP.clone().sub(curP).normalize();
//角平分线向量
let v = dir.clone().sub(v2);
//v1v2pm向量
let nv1v2 = dir.clone().cross(v2);
let norm = nv1v2.cross(v);
if (equalv2(v, ZeroVec))
norm.copy(dir);
//角平分线的平面
let plane = new PlaneExt(norm, curP);
let mtx = ContourTransfromToPath(preP, normal, dir);
pts = contourPts.map(p => p.clone().applyMatrix4(mtx));
pts = pts.map(p => plane.intersectLine(new three.Line3(p, p.clone().add(dir)), new three.Vector3(), true));
}
return pts;
}
/**
* 计算轮廓变换到`路径上某个点`的矩阵
*
* @param {Vector3} pt 路径上的点
* @param {Vector3} norm 曲线法向量
* @param {Vector3} dir 点前进的方向.
* @returns {Matrix4}
*/
function ContourTransfromToPath(pt, norm, dir) {
let vy = norm;
let vz = dir.normalize();
let vx = vz.clone().cross(vy);
let mat = new three.Matrix4();
mat.makeBasis(vx, vy, vz);
mat.setPosition(pt);
return mat;
}
// Quote from:
// https://github.com/Mugen87/yume/blob/master/src/javascript/engine/etc/OBB.js
// 即obb.js(本项目中已存在)
// Reference material:
//https://stackoverflow.com/questions/28499800/oriented-box-intersection-in-threejs
//http://www.cnblogs.com/iamzhanglei/archive/2012/06/07/2539751.html
//https://github.com/Mugen87/yume/blob/master/src/javascript/engine/etc/OBB.js
class OBB {
constructor(ocs, halfSizes) {
this.ocs = ocs;
this.halfSizes = halfSizes;
this._EPSILON = 1e-3;
this.center = halfSizes.clone().applyMatrix4(ocs);
}
intersectsOBB(obb, is2D, ucsInv) {
let newCenter;
let newObbCenter;
let cs;
let obbcs;
if (is2D) {
let mtx1 = new three.Matrix4().multiplyMatrices(ucsInv, this.ocs);
let mtx2 = new three.Matrix4().multiplyMatrices(ucsInv, obb.ocs);
cs = mtx1;
obbcs = mtx2;
cs.elements[14] = 0;
obbcs.elements[14] = 0;
newCenter = this.halfSizes.clone().applyMatrix4(cs);
newObbCenter = obb.halfSizes.clone().applyMatrix4(obbcs);
}
let xAxisA = new three.Vector3();
let yAxisA = new three.Vector3();
let zAxisA = new three.Vector3();
let xAxisB = new three.Vector3();
let yAxisB = new three.Vector3();
let zAxisB = new three.Vector3();
let translation = new three.Vector3();
let vector = new three.Vector3();
let axisA = [];
let axisB = [];
let rotationMatrix = [[], [], []];
let rotationMatrixAbs = [[], [], []];
let halfSizeA, halfSizeB;
let t, i;
// extract each axis
(cs ?? this.ocs).extractBasis(xAxisA, yAxisA, zAxisA);
(obbcs ?? obb.ocs).extractBasis(xAxisB, yAxisB, zAxisB);
// push basis vectors into arrays, so you can access them via indices
axisA.push(xAxisA, yAxisA, zAxisA);
axisB.push(xAxisB, yAxisB, zAxisB);
// get displacement vector
vector.subVectors(newObbCenter ?? obb.center, newCenter ?? this.center);
// express the translation vector in the coordinate frame of the current
// OBB (this)
for (i = 0; i < 3; i++) {
translation.setComponent(i, vector.dot(axisA[i]));
}
// generate a rotation matrix that transforms from world space to the
// OBB's coordinate space
for (i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
rotationMatrix[i][j] = axisA[i].dot(axisB[j]);
rotationMatrixAbs[i][j] = Math.abs(rotationMatrix[i][j]) + this._EPSILON;
}
}
// test the three major axes of this OBB
for (i = 0; i < 3; i++) {
vector.set(rotationMatrixAbs[i][0], rotationMatrixAbs[i][1], rotationMatrixAbs[i][2]);
halfSizeA = this.halfSizes.getComponent(i);
halfSizeB = obb.halfSizes.dot(vector);
if (Math.abs(translation.getComponent(i)) > halfSizeA + halfSizeB) {
return false;
}
}
// test the three major axes of other OBB
for (i = 0; i < 3; i++) {
vector.set(rotationMatrixAbs[0][i], rotationMatrixAbs[1][i], rotationMatrixAbs[2][i]);
halfSizeA = this.halfSizes.dot(vector);
halfSizeB = obb.halfSizes.getComponent(i);
vector.set(rotationMatrix[0][i], rotationMatrix[1][i], rotationMatrix[2][i]);
t = translation.dot(vector);
if (Math.abs(t) > halfSizeA + halfSizeB) {
return false;
}
}
// test the 9 different cross-axes
// A.x <cross> B.x
halfSizeA = this.halfSizes.y * rotationMatrixAbs[2][0] + this.halfSizes.z * rotationMatrixAbs[1][0];
halfSizeB = obb.halfSizes.y * rotationMatrixAbs[0][2] + obb.halfSizes.z * rotationMatrixAbs[0][1];
t = translation.z * rotationMatrix[1][0] - translation.y * rotationMatrix[2][0];
if (Math.abs(t) > halfSizeA + halfSizeB) {
return false;
}
// A.x < cross> B.y
halfSizeA = this.halfSizes.y * rotationMatrixAbs[2][1] + this.halfSizes.z * rotationMatrixAbs[1][1];
halfSizeB = obb.halfSizes.x * rotationMatrixAbs[0][2] + obb.halfSizes.z * rotationMatrixAbs[0][0];
t = translation.z * rotationMatrix[1][1] - translation.y * rotationMatrix[2][1];
if (Math.abs(t) > halfSizeA + halfSizeB) {
return false;
}
// A.x <cross> B.z
halfSizeA = this.halfSizes.y * rotationMatrixAbs[2][2] + this.halfSizes.z * rotationMatrixAbs[1][2];
halfSizeB = obb.halfSizes.x * rotationMatrixAbs[0][1] + obb.halfSizes.y * rotationMatrixAbs[0][0];
t = translation.z * rotationMatrix[1][2] - translation.y * rotationMatrix[2][2];
if (Math.abs(t) > halfSizeA + halfSizeB) {
return false;
}
// A.y <cross> B.x
halfSizeA = this.halfSizes.x * rotationMatrixAbs[2][0] + this.halfSizes.z * rotationMatrixAbs[0][0];
halfSizeB = obb.halfSizes.y * rotationMatrixAbs[1][2] + obb.halfSizes.z * rotationMatrixAbs[1][1];
t = translation.x * rotationMatrix[2][0] - translation.z * rotationMatrix[0][0];
if (Math.abs(t) > halfSizeA + halfSizeB) {
return false;
}
// A.y <cross> B.y
halfSizeA = this.halfSizes.x * rotationMatrixAbs[2][1] + this.halfSizes.z * rotationMatrixAbs[0][1];
halfSizeB = obb.halfSizes.x * rotationMatrixAbs[1][2] + obb.halfSizes.z * rotationMatrixAbs[1][0];
t = translation.x * rotationMatrix[2][1] - translation.z * rotationMatrix[0][1];
if (Math.abs(t) > halfSizeA + halfSizeB) {
return false;
}
// A.y <cross> B.z
halfSizeA = this.halfSizes.x * rotationMatrixAbs[2][2] + this.halfSizes.z * rotationMatrixAbs[0][2];
halfSizeB = obb.halfSizes.x * rotationMatrixAbs[1][1] + obb.halfSizes.y * rotationMatrixAbs[1][0];
t = translation.x * rotationMatrix[2][2] - translation.z * rotationMatrix[0][2];
if (Math.abs(t) > halfSizeA + halfSizeB) {
return false;
}
// A.z <cross> B.x
halfSizeA = this.halfSizes.x * rotationMatrixAbs[1][0] + this.halfSizes.y * rotationMatrixAbs[0][0];
halfSizeB = obb.halfSizes.y * rotationMatrixAbs[2][2] + obb.halfSizes.z * rotationMatrixAbs[2][1];
t = translation.y * rotationMatrix[0][0] - translation.x * rotationMatrix[1][0];
if (Math.abs(t) > halfSizeA + halfSizeB) {
return false;
}
// A.z <cross> B.y
halfSizeA = this.halfSizes.x * rotationMatrixAbs[1][1] + this.halfSizes.y * rotationMatrixAbs[0][1];
halfSizeB = obb.halfSizes.x * rotationMatrixAbs[2][2] + obb.halfSizes.z * rotationMatrixAbs[2][0];
t = translation.y * rotationMatrix[0][1] - translation.x * rotationMatrix[1][1];
if (Math.abs(t) > halfSizeA + halfSizeB) {
return false;
}
// A.z <cross> B.z
halfSizeA = this.halfSizes.x * rotationMatrixAbs[1][2] + this.halfSizes.y * rotationMatrixAbs[0][2];
halfSizeB = obb.halfSizes.x * rotationMatrixAbs[2][1] + obb.halfSizes.y * rotationMatrixAbs[2][0];
t = translation.y * rotationMatrix[0][2] - translation.x * rotationMatrix[1][2];
if (Math.abs(t) > halfSizeA + halfSizeB) {
return false;
}
// no separating axis exists, so the two OBB don't intersect
return true;
}
}
var SweepSolid_1;
let SweepSolid = SweepSolid_1 = class SweepSolid extends Entity {
constructor(contour, pathCurve) {
super();
this._Contour = contour;
this._PathCurve = pathCurve;
if (this._Contour && this._Contour.Id)
this._Contour = this._Contour.Clone();
if (this._Contour && this._PathCurve) {
this.TransfromPathToWCS();
//将OCS变换成第一个路径的OCS(合理一点)
let paths = this.Paths;
let path = paths[0];
this.OCS = path.OCSNoClone;
this._SpaceOCS.copy(path.OCSNoClone);
let ocsInv = this.OCSInv;
for (let p of paths)
p.ApplyMatrix(ocsInv);
}
}
Explode() {
if (Array.isArray(this._PathCurve)) {
const explode = [this._Contour.Clone().ApplyMatrix(this._Matrix)];
for (let path of this._PathCurve) {
explode.push(path.Clone().ApplyMatrix(this._Matrix));
}
return explode;
}
return [this._Contour.Clone().ApplyMatrix(this._Matrix), this._PathCurve.Clone().ApplyMatrix(this._Matrix)];
}
get Contour() {
return this._Contour;
}
get Path() {
return this._PathCurve;
}
//单纯的返回数组
get Paths() {
return Array.isArray(this._PathCurve) ? this._PathCurve : [this._PathCurve];
}
Reverse() {
this.WriteAllObjectRecord();
if (Array.isArray(this._PathCurve)) {
for (let path of this._PathCurve)
path.Reverse();
this._PathCurve.reverse();
}
else
this._PathCurve.Reverse();
this.Update();
}
/**
* 将轮廓变换到wcs空间,当用户选定某个与扫描线起点相切的轮廓时.
*/
TransfromPathToWCS() {
if (Array.isArray(this._PathCurve))
return;
if (equalv3(this._Contour.Normal, ZAxis))
return;
let fDir = this._PathCurve.GetFirstDeriv(0);
if (isParallelTo(fDir, this._Contour.Normal)) {
//构建回家的矩阵
let toWcsMat4Inv = new three.Matrix4();
let zv = fDir.normalize();
let yv = this._PathCurve.Normal;
let xv = zv.clone().cross(yv);
toWcsMat4Inv.makeBasis(xv, yv, zv);
toWcsMat4Inv.setPosition(this._PathCurve.StartPoint);
let toWcsMat4 = new three.Matrix4().getInverse(toWcsMat4Inv);
this._Contour.ApplyMatrix(toWcsMat4);
let z = this._Contour.StartPoint.z;
if (IsPointInPolyLine(this._Contour, new three.Vector3(0, 0, z))) {
let z = this._Contour.StartPoint.z;
this._Contour.ApplyMatrix(MoveMatrix(new three.Vector3(0, 0, -z)));
return;
}
else
this._Contour.ApplyMatrix(toWcsMat4Inv);
}
let lDir = this._PathCurve.GetFirstDeriv(this._PathCurve.EndParam);
if (isParallelTo(lDir, this._Contour.Normal)) {
//再次构建回家的矩阵
let toWcsMat4Inv = new three.Matrix4();
let zv = lDir.negate().normalize();
let yv = this._PathCurve.Normal;
let xv = zv.clone().cross(yv);
toWcsMat4Inv.makeBasis(xv, yv, zv);
toWcsMat4Inv.setPosition(this._PathCurve.EndPoint);
let toWcsMat4 = new three.Matrix4().getInverse(toWcsMat4Inv);
this._Contour.ApplyMatrix(toWcsMat4);
let z = this._Contour.StartPoint.z;
if (IsPointInPolyLine(this._Contour, new three.Vector3(0, 0, z))) {
let z = this._Contour.StartPoint.z;
this._Contour.ApplyMatrix(MoveMatrix(new three.Vector3(0, 0, -z)));
this._PathCurve.Reverse();
return;
}
else
this._Contour.ApplyMatrix(toWcsMat4);
}
Log("错误:提供的轮廓没有和路径垂直!", LogType.Error);
}
get MeshGeometry() {
if (this._MeshGeometry)
return this._MeshGeometry;
try {
let contour = this._Contour;
if (SweepSolid_1.UseRectFakerContour && contour.EndParam > 10) {
let box = contour.BoundingBox;
contour = new exports.Polyline().RectangleFrom2Pt(box.min, box.max);
}
this._MeshGeometry = new SweepGeometry(contour, this._PathCurve);
this._EdgeGeometry = this._MeshGeometry.EdgeGeom;
this._LineGeom = this._MeshGeometry.LineGeom;
this._MeshGeometry.edgePts = undefined;
return this._MeshGeometry;
}
catch (error) {
return new three.BoxBufferGeometry(1000, 1000, 1000);
}
}
get EdgeGeometry() {
if (this._EdgeGeometry)
return this._EdgeGeometry;
this.MeshGeometry;
return this._EdgeGeometry;
}
InitDrawObject(renderType) {
if (renderType === RenderType.Wireframe || renderType === RenderType.Edge) {
let line = new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(this.ColorIndex));
// for (let p of this.Paths)
// {
// p.IsEmbedEntity = true;
// let lineObj = p.GetDrawObjectFromRenderType(RenderType.Wireframe) as TLine;
// lineObj.material = ColorMaterial.GetWallLineMtl(1);
// lineObj.computeLineDistances();
// lineObj.matrix.copy(p.OCSNoClone);
// line.add(lineObj);
// }
return line;
}
else if (renderType === RenderType.Conceptual) {
return new three.Object3D().add(new three.Mesh(this.MeshGeometry, ColorMaterial.GetConceptualMaterial(this.ColorIndex)), new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetConceptualEdgeMaterial()));
}
else if (renderType === RenderType.Physical)
return new three.Mesh(this.MeshGeometry, this.MeshMaterial);
else if (renderType === RenderType.Print) {
let mat2 = ColorMaterial.GetPrintConceptualMaterial();
let meshGeo = this.MeshGeometry;
let mesh = new three.Mesh(meshGeo, mat2);
let line = new Line2.Line2(this._LineGeom, ColorMaterial.PrintLineMatrial);
return new three.Object3D().add(line, mesh);
}
else if (renderType === RenderType.Jig) {
if (Array.isArray(this._PathCurve)) {
const object3d = new three.Object3D();
for (let path of this._PathCurve)
object3d.add(path.DrawObject);
return object3d;
}
return new three.Object3D().add(this._PathCurve.DrawObject);
}
else if (renderType === RenderType.Physical2) {
return new three.Object3D().add(new three.Mesh(this.MeshGeometry, this.MeshMaterial), new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetPhysical2EdgeMaterial()));
}
}
UpdateDrawGeometry() {
if (this._EdgeGeometry)
this._EdgeGeometry.dispose();
this._EdgeGeometry = undefined;
if (this._MeshGeometry)
this._MeshGeometry.dispose();
this._MeshGeometry = undefined;
}
ClearDraw() {
this.UpdateDrawGeometry();
return super.ClearDraw();
}
UpdateDrawObject(renderType, obj) {
DisposeThreeObj(obj);
if (renderType === RenderType.Wireframe || renderType === RenderType.Edge) {
let l = obj;
l.geometry = this.EdgeGeometry;
l.material = ColorMaterial.GetLineMaterial(this.ColorIndex);
// Object3DRemoveAll(l);
// for (let p of this.Paths)
// {
// p.IsEmbedEntity = true;
// let lineObj = p.GetDrawObjectFromRenderType(RenderType.Wireframe) as TLine;
// lineObj.material = ColorMaterial.GetWallLineMtl(1);
// lineObj.computeLineDistances();
// l.add(lineObj);
// }
}
else if (renderType === RenderType.Conceptual) {
Object3DRemoveAll(obj);
return obj.add(new three.Mesh(this.MeshGeometry, ColorMaterial.GetConceptualMaterial(this.ColorIndex)), new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetConceptualEdgeMaterial()));
}
else if (renderType === RenderType.Physical) {
let mesh = obj;
mesh.geometry = this.MeshGeometry;
mesh.material = this.MeshMaterial;
}
else if (renderType === RenderType.Jig) {
Object3DRemoveAll(obj);
if (Array.isArray(this._PathCurve))
for (let path of this._PathCurve)
obj.add(path.DrawObject);
else
obj.add((this._PathCurve.DrawObject));
}
else if (renderType === RenderType.Physical2) {
Object3DRemoveAll(obj);
return obj.add(new three.Mesh(this.MeshGeometry, this.MeshMaterial), new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetPhysical2EdgeMaterial()));
}
}
/**
* 当实体需要被更新时,更新实体材质
*/
UpdateDrawObjectMaterial(type, obj) {
if (type === RenderType.Wireframe) {
let l = obj;
l.material = ColorMaterial.GetLineMaterial(this.ColorIndex);
}
else if (type === RenderType.Conceptual) {
let mesh = obj.children[0];
mesh.material = ColorMaterial.GetConceptualMaterial(this.ColorIndex);
}
else if (type === RenderType.Physical2) {
let mesh = obj.children[0];
mesh.material = this.MeshMaterial;
}
else {
let mesh = obj;
mesh.material = this.MeshMaterial;
}
}
get BoundingBox() {
let geom = this.MeshGeometry;
if (!geom) {
console.error("SweepSolid无法建模");
return new three.Box3;
}
if (!geom.boundingBox)
geom.computeBoundingBox();
return geom.boundingBox.clone().applyMatrix4(this._Matrix);
}
get OBB() {
let box = this.BoundingBox;
let size = box.getSize(new three.Vector3);
return new OBB(MoveMatrix(box.min), size.multiplyScalar(0.5));
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
switch (snapMode) {
case ObjectSnapMode.End:
return this.GetEndPoint();
case ObjectSnapMode.Mid:
case ObjectSnapMode.Cen:
case ObjectSnapMode.Nea:
case ObjectSnapMode.Ext:
case ObjectSnapMode.Per:
case ObjectSnapMode.Tan:
{
if (Array.isArray(this._PathCurve)) {
const points = [];
for (let path of this._PathCurve) {
let contour = path.Clone();
contour.ApplyMatrix(this.OCS);
let pts = contour.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform);
if (snapMode === ObjectSnapMode.Mid)
points.push(...pts, ...this.GetMidPoints());
else
points.push(...pts);
}
return points;
}
else {
let contour = this._PathCurve.Clone();
contour.ApplyMatrix(this.OCS);
let pts = contour.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform);
if (snapMode === ObjectSnapMode.Mid)
return [...pts, ...this.GetMidPoints()];
return pts;
}
}
}
return [];
}
/** 获取夹点与子实体的索引 */
GetGripSubIndexMap() {
if (this._GripSubIndexMap)
return this._GripSubIndexMap;
this.GetGripPoints();
return this._GripSubIndexMap;
}
GetGripPoints() {
this._GripSubIndexMap = undefined;
if (Array.isArray(this._PathCurve)) {
this._GripSubIndexMap = new Map;
const points = [];
for (let path of this._PathCurve) {
let pts = path.GetGripPoints();
for (let p of pts) {
this._GripSubIndexMap.set(points.length, path);
p.applyMatrix4(this._Matrix);
points.push(p);
}
}
return points;
}
else {
let pts = this._PathCurve.GetGripPoints();
for (let p of pts)
p.applyMatrix4(this._Matrix);
return pts;
}
}
GetStretchPoints() {
if (Array.isArray(this._PathCurve)) {
const points = [];
for (let path of this._PathCurve) {
let pts = path.GetStretchPoints();
for (let p of pts) {
p.applyMatrix4(this._Matrix);
points.push(p);
}
}
return points;
}
else {
let pts = this._PathCurve.GetStretchPoints();
for (let p of pts)
p.applyMatrix4(this._Matrix);
return pts;
}
}
//端点捕捉时提供端点
GetEndPoint() {
let pathPts = [];
let pathNormals = [];
//路径点表
if (Array.isArray(this._PathCurve)) {
for (let path of this._PathCurve) {
pathPts = path.GetStretchPoints();
arrayRemoveDuplicateBySort(pathPts, (p1, p2) => {
if (equalv3(p1, p2)) {
p2["_mask_"] = p1["_mask_"];
return true;
}
return false;
});
if (path !== this._PathCurve[0])
pathPts.shift();
let pNormal = path.Normal;
for (let p of pathPts)
pathNormals.push(pNormal);
}
}
else {
const path = this._PathCurve;
//路径点表
pathPts = path.GetStretchPoints();
if (path.IsClose && !equalv3(pathPts[0], pathPts[pathPts.length - 1], 1e-3))
pathPts.push(pathPts[0]);
arrayRemoveDuplicateBySort(pathPts, equalv3);
let pNormal = path.Normal;
for (let p of pathPts)
pathNormals.push(pNormal);
}
let shapePts2d = this.Contour.GetStretchPoints();
//轮廓点表
let shapePts3d = shapePts2d.map(AsVector3);
let isClosePath = equalv3(pathPts[0], pathPts[pathPts.length - 1], 1e-3);
let pts = []; //端点
//遍历所有的路径节点进行顶点投射
for (let i = 1; i < pathPts.length; i++) {
if (i === pathPts.length - 1) {
if (isClosePath)
pts.push(...ProjectionToPlane(shapePts3d, pathNormals[i], pathPts[i], pathPts[i - 1], pathPts[1]));
else
pts.push(...ProjectionToPlane(shapePts3d, pathNormals[i], pathPts[i], pathPts[i - 1]));
}
else {
pts.push(...ProjectionToPlane(shapePts3d, pathNormals[i], pathPts[i], pathPts[i - 1], pathPts[i + 1]));
}
}
for (let pt of pts)
pt.applyMatrix4(this.OCSNoClone);
return pts;
}
GetMidPoints() {
let conPts = this._Contour.GetStretchPoints();
const pts = [];
const UpdateEndMtx = (path) => {
for (let i = 0.5; i < path.EndParam; i++) {
let pos = path.GetPointAtParam(i);
let dir = path.GetFirstDeriv(i).normalize();
let y = path.Normal;
let roMat = new three.Matrix4().extractRotation(this.OCS);
let z = dir.applyMatrix4(roMat);
let x = z.clone().cross(y);
tempMatrix1.makeBasis(x, y, z);
tempMatrix1.setPosition(pos.applyMatrix4(this.OCS));
pts.push(...conPts.map(p => p.clone().applyMatrix4(tempMatrix1)));
}
};
if (Array.isArray(this._PathCurve))
for (let path of this._PathCurve)
UpdateEndMtx(path);
else
UpdateEndMtx(this._PathCurve);
return pts;
}
MoveGripPoints(indexList, vec) {
if (equalv3(vec, ZeroVec))
return;
this.WriteAllObjectRecord();
vec = vec.clone().applyMatrix4(this.OCSInv.setPosition(0, 0, 0));
if (Array.isArray(this._PathCurve)) {
let ptsLengths = 0;
for (let i = 0; i < this._PathCurve.length; i += 1) {
const path = this._PathCurve[i];
const pathGripPts = path.GetGripPoints();
const idxList = indexList.filter(v => v >= ptsLengths && v < (ptsLengths + pathGripPts.length)).map((v) => v - ptsLengths);
ptsLengths += pathGripPts.length;
if (idxList.length === 0)
continue;
let isMoveLine = (path instanceof exports.Line && idxList.length === 1 && idxList[0] === 1);
let isMovePolylineStart = (path instanceof exports.Polyline) && idxList.length === 1 && idxList.includes(1);
let isMovePolylineEnd = (path instanceof exports.Polyline) && idxList.length === 1 && idxList.includes(pathGripPts.length - 2);
//如果不是整体移动,那么vec被限制在CURVE OCS Z0
let isMove = isMoveLine || idxList.length === pathGripPts.length;
if (!isMove) {
vec = vec.clone()
.applyMatrix4(path.OCSInv.setPosition(0, 0, 0))
.setZ(0)
.applyMatrix4(path.OCS.setPosition(0, 0, 0));
}
//我们校验它的前后曲线支不支持它移动这个点
let prePath = this._PathCurve[FixIndex$1(i - 1, this._PathCurve)];
let nextPath = this._PathCurve[FixIndex$1(i + 1, this._PathCurve)];
if ((isMoveLine || idxList.includes(0) || isMovePolylineStart) //(move line ) or (move start) or(move pl start)
&& (i || equalv3(prePath.EndPoint, path.StartPoint, 1e-3))) //连接到下一段
{
//vec限制在上一段的坐标系内 无z
vec = vec.clone()
.applyMatrix4(prePath.OCSInv.setPosition(0, 0, 0))
.setZ(0)
.applyMatrix4(prePath.OCS.setPosition(0, 0, 0));
if (isMoveLine || isMovePolylineStart) //顺带移动上一段
prePath.MoveGripPoints([prePath.GetGripPoints().length - 1], vec);
}
if ((isMoveLine || idxList.includes(pathGripPts.length - 1) || isMovePolylineEnd) //(move line ) or (move end) or(move pl end)
&& (i < this._PathCurve.length - 2 || equalv3(path.EndPoint, nextPath.StartPoint, 1e-3))) //连接到上一段
{
//vec限制在下一段的坐标系内 无z
vec = vec.clone()
.applyMatrix4(nextPath.OCSInv.setPosition(0, 0, 0))
.setZ(0)
.applyMatrix4(nextPath.OCS.setPosition(0, 0, 0));
if (isMoveLine || isMovePolylineEnd) //顺带移动下一段
nextPath.MoveGripPoints([0], vec);
}
if (isMove)
path.Move(vec);
else
path.MoveGripPoints(idxList, vec);
}
}
else {
this._PathCurve.MoveGripPoints(indexList, vec);
}
this.Update();
}
MoveStretchPoints(indexList, vec) {
if (equalv3(vec, ZeroVec))
return;
this.WriteAllObjectRecord();
vec = vec.clone().applyMatrix4(this.OCSInv.setPosition(0, 0, 0));
if (Array.isArray(this._PathCurve)) {
let ptsLengths = 0;
let pathIndexMap = new Map();
//1.parse
for (let i = 0; i < this._PathCurve.length; i++) {
let path = this._PathCurve[i];
const pts = path.GetStretchPoints();
const idxList = indexList.filter(v => v >= ptsLengths && v < (ptsLengths + pts.length)).map((v) => v - ptsLengths);
ptsLengths += pts.length;
pathIndexMap.set(path, {
idx: idxList,
count: pts.length,
isMove: pts.length === idxList.length,
});
}
//2.change vec
for (let i = 0; i < this._PathCurve.length; i++) {
let path = this._PathCurve[i];
let { idx: idxList, count: ptsCount, isMove } = pathIndexMap.get(path);
if (idxList.length === 0)
continue;
let isMoveStart = idxList.includes(0);
let isMoveEnd = idxList.includes(ptsCount - 1);
if (!isMove) //如果不是移动 限制在本OCS内 NO Z
{
vec.applyMatrix4(path.OCSInv.setPosition(0, 0, 0))
.setZ(0)
.applyMatrix4(path.OCS.setPosition(0, 0, 0));
}
//我们校验它的前后曲线支不支持它移动这个点
let prePath = this._PathCurve[FixIndex$1(i - 1, this._PathCurve)];
let nextPath = this._PathCurve[FixIndex$1(i + 1, this._PathCurve)];
//如果pre是move 则不需要过滤z
if ((isMove || isMoveStart) //(move line ) or (move start) or(move pl start)
&& (i || equalv3(prePath.EndPoint, path.StartPoint, 1e-3)) //连接到下一段
&& (!pathIndexMap.get(prePath).isMove) //非移动
) {
//vec限制在上一段的坐标系内 无z
vec.applyMatrix4(prePath.OCSInv.setPosition(0, 0, 0))
.setZ(0)
.applyMatrix4(prePath.OCS.setPosition(0, 0, 0));
}
if (isMove || isMoveEnd
&& (i < this._PathCurve.length - 2 || equalv3(path.EndPoint, nextPath.StartPoint, 1e-3)) //连接到上一段
&& (!pathIndexMap.get(nextPath).isMove) //非移动
) {
//vec限制在下一段的坐标系内 无z
vec.applyMatrix4(nextPath.OCSInv.setPosition(0, 0, 0))
.setZ(0)
.applyMatrix4(nextPath.OCS.setPosition(0, 0, 0));
}
}
//3 move
for (let i = 0; i < this._PathCurve.length; i++) {
let path = this._PathCurve[i];
let { idx: idxList, isMove } = pathIndexMap.get(path);
if (isMove)
path.Move(vec);
else
path.MoveStretchPoints(idxList, vec);
}
}
else {
this._PathCurve.MoveStretchPoints(indexList, vec);
}
this.Update();
}
ApplyMatrix(m) {
this.WriteAllObjectRecord();
if (equaln$1(m.getMaxScaleOnAxis(), 1)) {
let xA = new three.Vector3();
let yA = new three.Vector3();
let zA = new three.Vector3();
m.extractBasis(xA, yA, zA);
if (!equalv3(xA.clone().cross(yA).normalize(), zA))
this.ApplyMirrorMatrix(m);
else {
this._Matrix.multiplyMatrices(m, this._Matrix);
this._SpaceOCS.multiplyMatrices(m, this._SpaceOCS);
this.Update(exports.UpdateDraw.Matrix);
}
}
else {
this.ApplyScaleMatrix(m);
}
return this;
}
ApplyMirrorMatrix(m) {
if (!this.Id) {
this._Matrix.multiplyMatrices(m, this._Matrix);
return this;
}
let ocsInv = this.OCSInv;
if (Array.isArray(this._PathCurve)) {
for (let p of this._PathCurve)
p.ApplyMatrix(this.OCSNoClone).ApplyMatrix(m).ApplyMatrix(ocsInv);
let group = curveLinkGroup(this._PathCurve);
this._PathCurve = group[0];
}
else
this._PathCurve.ApplyMatrix(this.OCSNoClone).ApplyMatrix(m).ApplyMatrix(ocsInv);
this.Update(exports.UpdateDraw.Geometry);
return this;
}
_ReadFile(file) {
super._ReadFile(file);
let ver = file.Read(); //ver
this._Contour = file.ReadObject();
if (ver === 1) {
this._PathCurve = file.ReadObject();
if (this._Contour instanceof Spline || this._PathCurve instanceof Spline) {
this._isErase = true;
Log("放样实体是样条线生成的,自动删除它!", LogType.Info);
}
}
else if (ver > 1) {
const pathCurveCount = file.Read();
if (pathCurveCount === 1)
this._PathCurve = file.ReadObject();
else {
this._PathCurve = [];
for (let i = 0; i < pathCurveCount; i++) {
this._PathCurve.push(file.ReadObject());
}
}
}
}
WriteFile(file) {
super.WriteFile(file);
file.Write(2); //ver
file.WriteObject(this._Contour);
if (Array.isArray(this._PathCurve)) {
file.Write(this._PathCurve.length);
for (let c of this._PathCurve)
file.WriteObject(c);
}
else {
file.Write(1);
file.WriteObject(this._PathCurve);
}
}
};
SweepSolid.UseRectFakerContour = false;
SweepSolid = SweepSolid_1 = __decorate([
Factory
], SweepSolid);
let HardwareTopline = class HardwareTopline extends SweepSolid {
constructor() {
super(...arguments);
this.HardwareOption = { ...DefaultToplineMetalsOption };
this.DataList = [];
this._contourRotation = 0;
}
get ContourRotation() {
return this._contourRotation;
}
/**
*延伸取最大最小轮廓每段首尾到前面线段,取最长线段作为分段长
*/
get Segmentations() {
//轮廓点表
let contourPts = this._Contour.Shape.getPoints(4).map(AsVector3);
for (let p of contourPts)
p.applyMatrix4(this._Contour.OCSNoClone);
//收集所有的点
let pathCurves = [];
if (Array.isArray(this._PathCurve)) {
for (let p of this._PathCurve) {
if (p instanceof exports.Polyline)
pathCurves.push(...p.Explode());
else
pathCurves.push(p.Clone());
}
}
else if (this._PathCurve instanceof exports.Polyline)
pathCurves.push(...this._PathCurve.Explode());
else
pathCurves.push(this._PathCurve.Clone());
let isClosePath = equalv3(pathCurves[0].StartPoint, pathCurves[pathCurves.length - 1].EndPoint, 1e-3);
let radiusMap = new Map();
function ExtendsCurve(path, pts) {
let params = pts.map(p => path.GetParamAtPoint2(path.GetClosestPointTo(p, true)));
let min = Math.min(0, params[Max(params, (p1, p2) => p1 > p2)]);
let max = Math.max(1, params[Max(params, (p1, p2) => p1 < p2)]);
let sp = path.GetPointAtParam(min);
let ep = path.GetPointAtParam(max);
path.StartPoint = sp;
path.EndPoint = ep;
}
//遍历所有的路径节点进行顶点投射
for (let i = 0; i < pathCurves.length; i++) {
let path = pathCurves[i];
if (isClosePath || i !== pathCurves.length - 1) //与下一段
{
let ep = path.EndPoint;
let nextPath = pathCurves[FixIndex$1(i + 1, pathCurves)];
let preP;
if (path instanceof exports.Line)
preP = ep.clone().sub(path.GetFirstDeriv(1).normalize());
else {
let pts = path.Shape.getPoints(4);
preP = AsVector3(pts[pts.length - 2]).applyMatrix4(path.OCSNoClone);
}
let nextP;
if (nextPath instanceof exports.Line)
nextP = ep.clone().add(nextPath.GetFirstDeriv(0).normalize());
else {
let pts = nextPath.Shape.getPoints(4);
nextP = AsVector3(pts[1]).applyMatrix4(nextPath.OCSNoClone);
}
//投射的点表
let pts = ProjectionToPlane(contourPts, path.Normal, ep, preP, nextP);
// for (let j = 0; j < pts.length - 1; j++)
// TestDraw(new Line(pts[j].clone(), pts[j + 1].clone()).ApplyMatrix(this.OCSNoClone), i + 1);
//针对圆弧 修改它的半径
if (path instanceof exports.Arc) {
let mp = path.GetPointAtParam(0.5);
let arcPts = ProjectionToPlane(contourPts, path.Normal, mp, mp.clone().sub(path.GetFirstDeriv(0.5).normalize()));
let r = radiusMap.get(path);
let ocsInv = path.OCSInv;
let radius = arcPts.map(p => p.applyMatrix4(ocsInv).setZ(0).distanceTo(ZeroVec));
if (r)
radius.push(r);
let maxRadius = radius[Max(radius, (r1, r2) => r1 < r2)];
radiusMap.set(path, maxRadius);
}
ExtendsCurve(path, pts);
ExtendsCurve(nextPath, pts);
}
}
for (let [arc, rad] of radiusMap)
arc.Radius = rad;
return pathCurves;
}
get MaxLength() {
return this.Segmentations.reduce((len, c) => len + c.Length, 0);
}
set ContourRotation(ro) {
if (ro === this._contourRotation)
return;
this.WriteAllObjectRecord();
let diffRo = ro - this._contourRotation;
this._contourRotation = ro;
let mat = new three.Matrix4().makeRotationZ(diffRo);
this.Contour.ApplyMatrix(mat);
this.Update();
}
_ReadFile(file) {
super._ReadFile(file);
file.Read(); //1
this._contourRotation = file.Read();
this.HardwareOption.addLen = file.Read();
this.HardwareOption.name = file.Read();
this.HardwareOption.roomName = file.Read();
this.HardwareOption.cabinetName = file.Read();
this.HardwareOption.costExpr = file.Read();
this.HardwareOption.actualExpr = file.Read();
this.HardwareOption.model = file.Read();
this.HardwareOption.factory = file.Read();
this.HardwareOption.brand = file.Read();
this.HardwareOption.spec = file.Read();
this.HardwareOption.comments = file.Read();
let count = file.Read();
this.DataList.length = 0;
for (let i = 0; i < count; i++) {
let d = ["", ""];
d[0] = file.Read();
d[1] = file.Read();
this.DataList.push(d);
}
}
WriteFile(file) {
super.WriteFile(file);
file.Write(1); //ver
file.Write(this._contourRotation);
file.Write(this.HardwareOption.addLen);
file.Write(this.HardwareOption.name);
file.Write(this.HardwareOption.roomName);
file.Write(this.HardwareOption.cabinetName);
file.Write(this.HardwareOption.costExpr);
file.Write(this.HardwareOption.actualExpr);
file.Write(this.HardwareOption.model);
file.Write(this.HardwareOption.factory);
file.Write(this.HardwareOption.brand);
file.Write(this.HardwareOption.spec);
file.Write(this.HardwareOption.comments);
file.Write(this.DataList.length);
for (let data of this.DataList) {
file.Write(data[0]);
file.Write(data[1]);
}
}
};
__decorate([
AutoRecordObject
], HardwareTopline.prototype, "HardwareOption", void 0);
__decorate([
AutoRecord
], HardwareTopline.prototype, "DataList", void 0);
HardwareTopline = __decorate([
Factory
], HardwareTopline);
class LookOverBoardInfosTool {
constructor() {
this.drillTypeMap = new Map();
this.sealMap = new Map();
this.boardMap = new Map();
this.ParseProcessingGroupTags = (entity) => {
return [...entity.ProcessingGroupList].map((g) => {
let obj = g?.Object;
if (obj)
return obj.Name;
});
};
}
GetCount(brs, options = null, IsBbsCountChaidan) {
let drillCount = [];
let sealCount = [];
let hardwareCount = [];
let areaCount = [];
this.drillTypeMap.clear();
this.sealMap.clear();
this.Update(brs, options, IsBbsCountChaidan);
if (this.drillTypeMap.size > 0)
for (let [k, v] of this.drillTypeMap) {
if (v[0] instanceof Hole)
if (k === "木销")
drillCount.push({ name: k, count: v.length });
else if (k === "层板钉")
drillCount.push({ name: k, count: v.length });
else
drillCount.push({ name: k, count: v.length });
else {
this.ParseHardwareCount(k, v, hardwareCount);
}
}
hardwareCount.sort((h1, h2) => h1.name.localeCompare(h2.name));
//加入封边信息
for (let [k, v] of this.sealMap) {
sealCount.push({ name: k, count: v / 1000, unit: "米" });
}
for (let [k, bs] of this.boardMap) {
const chaiDan = IsBbsCountChaidan ? bs : bs.filter(b => b.IsChaiDan);
areaCount.push({
entity: bs[0],
count: chaiDan.length,
count2: this.GetBoardsArea(chaiDan)
});
}
return { drillCount, hardwareCount, sealCount, areaCount };
}
;
Update(ens, options = null, IsBbsCountChaidan) {
//计算排钻个数
const addDrillToMap = (spiteName, d) => {
if (!this.drillTypeMap.has(spiteName))
this.drillTypeMap.set(spiteName, [d]);
else {
let ds = this.drillTypeMap.get(spiteName);
if (!ds.includes(d))
ds.push(d);
}
};
const brsProps = [];
const hardwares = [];
for (let e of ens) {
if (e instanceof Board)
brsProps.push(e);
else
hardwares.push(e);
}
for (let h of hardwares) {
let { name, unit, factory, spec, model, brand } = h.HardwareOption;
let tags = this.ParseProcessingGroupTags(h);
// :230行 代码对关联复合实体又进行一遍分析 需同步修改
addDrillToMap(`${name},${unit},${factory},${this.ParseSpec(h, spec)},${this.ParseSpec(h, model)},${brand},${tags.join(",")}`, h);
}
this.UpdateBoardMap(brsProps);
//统计 排钻 封边 关联的五金(排钻?)
for (let br of brsProps) {
if (!IsBbsCountChaidan && !br.IsChaiDan) //非拆单板我们不统计
continue;
//排钻 层板钉
let dlist = br.DrillList;
if (equaln$1(br.ContourCurve.Area, 0)) {
ToasterShowEntityMsg({
msg: `${br.BoardProcessOption.roomName} ${br.BoardProcessOption.cabinetName} ${br.Name}轮廓有有问题,请检查`,
timeout: 5000,
intent: Intent.DANGER,
ent: br
});
continue;
}
for (let [id, idList] of dlist) {
for (let ids of idList) {
let holes = ids.map(id => id.Object).filter(h => h);
if (!holes[0] || !HoleInBoard(holes, br))
continue;
let isTk = false;
let spliteName;
let hole;
let pxlCount = 0;
findHole: for (let objId of ids) {
let gd = objId.Object;
if (!gd || gd.IsErase)
break;
const group = gd.GroupId?.Object;
if (!group) {
Toaster({ message: `柜名:${br.BoardProcessOption.cabinetName} 房间名:${br.BoardProcessOption.roomName} 板名:${br.Name} 的排钻的编组丢失,统计排钻个数时会丢失该个数!`, timeout: 5000, intent: Intent.DANGER });
Log(`柜名:${br.BoardProcessOption.cabinetName} 房间名:${br.BoardProcessOption.roomName} 板名:${br.Name} 的排钻的编组丢失,统计排钻个数时会丢失该个数!`, LogType.Error, [br, gd]);
break;
}
if (gd instanceof CylinderHole) {
switch (gd.Type) {
case GangDrillType.Pxl:
pxlCount++;
break;
case GangDrillType.Ljg:
case GangDrillType.Ymj:
break;
case GangDrillType.TK:
isTk = true;
break;
case GangDrillType.Wood:
case GangDrillType.WoodPXL:
spliteName = "木销";
break;
default:
break findHole;
}
options?.getHoles && options?.getHoles(spliteName || group.Name || "未命名", gd);
}
else {
if (gd.isThrough)
isTk = true;
}
if (!spliteName)
spliteName = group.Name || "未命名";
if (!hole)
hole = gd;
}
if (spliteName && hole) {
if (isTk && HostApplicationServices.chaidanOption.statTk) {
addDrillToMap("通孔" + spliteName, hole);
}
else if (pxlCount === 2 && HostApplicationServices.chaidanOption.statSt) {
addDrillToMap("双头" + spliteName, hole);
}
else {
addDrillToMap(spliteName, hole);
}
}
}
}
// 被复制的层板钉暂未加入LayerNails数组 等做好关联后解除注释
if (br.LayerNails.length > 0)
for (let objId of br.LayerNails) {
if (!objId?.IsErase)
addDrillToMap("层板钉", objId.Object);
}
//分析五金
for (const mId of br.RelativeHardware) {
let metal = mId?.Object;
if (metal && !metal.IsErase && metal.HardwareOption) {
let { name, unit, factory, spec, model, brand } = metal.HardwareOption;
let tags = this.ParseProcessingGroupTags(metal);
// :113行 代码对关联复合实体又进行一遍分析 需同步修改
addDrillToMap(`${name},${unit},${factory},${this.ParseSpec(metal, spec)},${this.ParseSpec(metal, model)},${brand},${tags.join(",")}`, metal);
}
}
//封边
let sealdData = GetSealedBoardContour(br);
if (!sealdData) {
ToasterShowEntityMsg({
intent: Intent.DANGER,
msg: "板件扣封边失败,请检查板件轮廓!",
timeout: 10000,
ent: br
});
throw "错误:板扣除封边失败!";
}
let sealData = exports.Production.ParseSealData(sealdData);
let color = br.BoardProcessOption[EBoardKeyList.Color];
//封边留头量
let sealReserve = HostApplicationServices.sealReserve * 2;
let thickness = this.GetBoardThickness(br);
for (let data of sealData) {
if (equaln$1(0, data.size))
continue;
data.length += sealReserve;
let k = `${data.size}-${FixedNotZero(thickness, 2)}-${color}`;
if (options && options.sealGruopKey) {
options.sealGruopKey(k, br, data.size);
}
let len = this.sealMap.get(k);
if (!len)
this.sealMap.set(k, data.length);
else
this.sealMap.set(k, len += data.length);
}
}
}
;
ParseSpec(en, spec, len) {
let size = en instanceof three.Vector3 ? en : en.BoundingBoxInOCS.getSize(new three.Vector3);
const accuracy = HostApplicationServices.chaidanOption.hardwareExpressionFormattingAccuracy;
return ParseExpr(spec, accuracy, { L: len ?? size.x, W: size.y, H: size.z }) || "[ 无 ]";
}
ParseHardwareCount(k, v, hardwareCount) {
const accuracy = HostApplicationServices.chaidanOption.hardwareExpressionFormattingAccuracy;
if (v.length > 0) {
if (!(v[0] instanceof HardwareTopline)) {
let count2 = v.reduce((v, d) => {
let size = d.BoundingBoxInOCS.getSize(new three.Vector3);
let c = safeEval(d.HardwareOption.count, { L: size.x, W: size.y, H: size.z }) ?? 0;
return v + c;
}, 0);
hardwareCount.push({ name: k.split(",")[0], count: v.length, entity: v[0], count2: FixedNotZero(count2, accuracy) });
}
else {
let map = new Map();
let name = k.split(",")[0];
let addLen = v[0].HardwareOption.addLen;
for (let d of v) {
let e = d;
let cus = e.Segmentations;
for (let cu of cus) {
let len = parseFloat(FixedNotZero(cu.Length, 2));
if (map.has(len)) {
map.set(len, map.get(len) + 1);
}
else {
map.set(len, 1);
}
}
}
for (let [len, count] of map) {
let count2 = parseFloat(FixedNotZero(len + parseFloat(addLen), accuracy));
hardwareCount.push({ name, count, entity: v[0], count2, length: count2 });
}
}
}
}
UpdateBoardMap(brs) {
this.boardMap.clear();
for (let b of brs) {
let thickness = this.GetBoardThickness(b);
let brMat = b.BoardProcessOption[EBoardKeyList.BrMat];
let mat = b.BoardProcessOption[EBoardKeyList.Mat];
let color = b.BoardProcessOption[EBoardKeyList.Color];
let key = `${thickness}-${brMat}-${mat}-${color}`;
let list = this.boardMap.get(key);
if (!list) {
list = [];
this.boardMap.set(key, list);
}
list.push(b);
}
}
GetBoardThickness(br) {
let size = exports.Production.GetSpiteSize(br);
if (size)
return FixedNotZero(size.spliteThickness, 2);
else
return FixedNotZero(br.Thickness, 2);
}
GetBoardsArea(brs) {
return brs.reduce((area, b) => {
let size = exports.Production.GetSpiteSize(b);
let ar;
if (size)
ar = size.spliteHeight * size.spliteWidth / 1e6;
else
ar = b.Width * b.Height / 1e6;
ar = parseFloat(ar.toFixed(3));
return area + ar;
}, 0).toFixed(2);
}
}
const lookOverBoardInfosTool = new LookOverBoardInfosTool();
let GroupRecord = class GroupRecord extends SymbolTableRecord {
constructor() {
super();
this.Entitys = new Proxy([], {
set: (target, key, value, receiver) => {
if (Reflect.get(target, key, receiver) !== value) {
if (this.WriteAllObjectRecord()) {
if (value instanceof ObjectId && value.Object instanceof Entity) {
if (!this.Id)
console.warn("请先添加到Database后在进行操作!");
else
value.Object.GroupId = this.Id;
}
}
}
return Reflect.set(target, key, value, receiver);
},
get: (target, key, receiver) => {
if (key === ISPROXYKEY)
return true;
return Reflect.get(target, key, receiver);
}
});
}
get Name() {
return this.name;
}
set Name(name) {
if (name !== this.name) {
this.WriteAllObjectRecord();
this.name = name;
}
}
Purge() {
arrayRemoveIf(this.Entitys, id => !id || id.IsErase);
}
//对象从文件中读取数据,初始化自身
ReadFile(file) {
file.Read();
super.ReadFile(file);
let count = file.Read();
this.Entitys.length = 0;
for (let i = 0; i < count; i++) {
let id = file.ReadObjectId();
if (id)
this.Entitys.push(id);
}
}
//对象将自身数据写入到文件.
WriteFile(file) {
file.Write(1);
super.WriteFile(file);
file.Write(this.Entitys.length);
for (let id of this.Entitys)
file.WriteObjectId(id);
}
};
__decorate([
AutoRecord
], GroupRecord.prototype, "Entitys", void 0);
GroupRecord = __decorate([
Factory
], GroupRecord);
//将嵌入的实体绘制对象添加到当前的绘制对象(由于内嵌的实体可能被重复引用)
function AddEntityDrawObject(obj, embedEntity, renderType = RenderType.Wireframe) {
let embedObject = embedEntity.GetDrawObjectFromRenderType(renderType);
if (embedObject.parent)
obj.children.push(embedObject); //为了避免这个内嵌实体加入到不同的Object中(因为我们有PrintObject),这个写法能行,是因为我们会在其他地方更新它的矩阵
else
obj.add(embedObject);
}
/** Epsilon used during determination of near zero distances.
* @default
*/
const EPS = 5e-2;
// //////////////////////////////
// tolerance: The maximum difference for each parameter allowed to be considered a match
class FuzzyFactory {
constructor(numdimensions = 3, tolerance = EPS) {
this.lookuptable = {};
this.multiplier = 1.0 / tolerance;
}
// let obj = f.lookupOrCreate([el1, el2, el3], function(elements) {/* create the new object */});
// Performs a fuzzy lookup of the object with the specified elements.
// If found, returns the existing object
// If not found, calls the supplied callback function which should create a new object with
// the specified properties. This object is inserted in the lookup database.
lookupOrCreate(els, object) {
let hash = "";
let multiplier = this.multiplier;
for (let el of els) {
let valueQuantized = Math.round(el * multiplier);
hash += valueQuantized + "/";
}
if (hash in this.lookuptable)
return this.lookuptable[hash];
else {
let hashparts = els.map(el => {
let q0 = Math.floor(el * multiplier);
let q1 = q0 + 1;
return ["" + q0 + "/", "" + q1 + "/"];
});
let numelements = els.length;
let numhashes = 1 << numelements;
for (let hashmask = 0; hashmask < numhashes; ++hashmask) {
let hashmaskShifted = hashmask;
hash = "";
hashparts.forEach(hashpart => {
hash += hashpart[hashmaskShifted & 1];
hashmaskShifted >>= 1;
});
this.lookuptable[hash] = object;
}
return object;
}
}
}
var DepthType;
(function (DepthType) {
DepthType[DepthType["Front"] = 1] = "Front";
DepthType[DepthType["Back"] = 2] = "Back";
DepthType[DepthType["All"] = 3] = "All";
})(DepthType || (DepthType = {}));
const ExtrudeBuildConfig = { bevel: false };
/**
* 槽的几何数据,包括槽的墙面和槽的盖子
*/
class Groove {
constructor(contour, holes, depthType, depth, allDepth, box = contour.BoundingBox) {
this.depthType = depthType;
this.depth = depth;
this.allDepth = allDepth;
this.box = box;
this.holeWalls = []; //槽的网洞的墙
this.contourWall = new ExtudeWall(contour.Curve, depthType, depth, allDepth, DirectionType.Inner);
for (let h of holes)
this.holeWalls.push(new ExtudeWall(h.Curve, depthType, depth, allDepth, DirectionType.Outer));
this.lid = new CurveTapeShape(contour, holes);
}
/**
* @param groove this - groove
* @param [eachOther=true] 相互裁剪
*/
ClipTo(groove, eachOther = true) {
//相同深度和面不用操作
if (groove.depthType === this.depthType && groove.depth === this.depth)
return;
if (!IntersectsBox(this.box, groove.box))
return;
this.ClipLid(groove);
groove.ClipLid(this);
//一正一反,不交集
if (this.depthType + groove.depthType === 3 && this.depth + groove.depth < this.allDepth)
return;
this.contourWall.ClipTo(groove, true);
for (let wall of this.holeWalls)
wall.ClipTo(groove, true);
if (eachOther) {
groove.contourWall.ClipTo(this, false);
for (let wall of groove.holeWalls)
wall.ClipTo(this, false);
}
}
ClipLid(groove) {
if (this.depthType === DepthType.All)
return;
// if (groove.depthType === DepthType.All) return;
if (this.depthType === groove.depthType) {
if (groove.depth > this.depth)
this.lid.ClipTo(groove.lid, true);
else
this.lid.SplitTo(groove.lid);
}
else {
if (this.depth + groove.depth >= this.allDepth)
this.lid.ClipTo(groove.lid, true);
else
this.lid.SplitTo(groove.lid);
}
}
Draw(verticesArray, uvArray, edgeBuild, rotateUv) {
this.contourWall.Draw(verticesArray, uvArray, edgeBuild);
for (let wall of this.holeWalls)
wall.Draw(verticesArray, uvArray, edgeBuild);
if (this.depthType === DepthType.All)
return;
let isFront = this.depthType === DepthType.Front;
this.lid.Draw(verticesArray, uvArray, isFront, isFront ? this.allDepth - this.depth : this.depth, rotateUv, this.allDepth);
}
}
function CreateTape(faceType, startParam, endParam, depth, allDepth) {
if (faceType === DepthType.Front)
return new Tape(startParam, endParam, allDepth - depth, allDepth);
else
return new Tape(startParam, endParam, 0, depth);
}
//朝向类型
var DirectionType;
(function (DirectionType) {
DirectionType[DirectionType["Outer"] = 0] = "Outer";
DirectionType[DirectionType["Inner"] = 1] = "Inner"; //内墙
})(DirectionType || (DirectionType = {}));
//轮廓树节点,用于重新确认外墙和网洞的关系
class ContourTreeNode {
constructor(contour, children = []) {
this.contour = contour;
this.children = children;
}
SetParent(node) {
if (this.parent)
throw "ContourTreeNode重复设置父对象";
this.parent = node;
node.children.push(this);
}
get Depth() {
let depth = 0;
let parent = this.parent;
while (parent) {
depth++;
parent = parent.parent;
if (depth > 10)
throw "ContourTreeNode嵌套超过10层";
}
return depth;
}
get IsHole() {
return this.Depth % 2 === 1;
}
Draw(verticesArray, uvArray, front, z, rotateUv, allDepth) {
// TestDraw(this.contour.Curve, depth);
let pts = this.contour.Curve.GetStretchPoints();
let vertices = pts.concat();
let holes = this.children.map(h => {
// TestDraw(h.contour.Curve, depth + 1);
let pts = h.contour.Curve.GetStretchPoints();
arrayPushArray$1(vertices, pts);
return pts;
});
let faces = three.ShapeUtils.triangulateShape(pts, holes);
for (let f of faces) {
if (front) {
AddVertice(vertices[f[0]]);
AddVertice(vertices[f[1]]);
AddVertice(vertices[f[2]]);
}
else {
AddVertice(vertices[f[0]]);
AddVertice(vertices[f[2]]);
AddVertice(vertices[f[1]]);
}
}
function AddVertice(v, inz = z) {
verticesArray.push(v.x, v.y, inz);
if (rotateUv)
uvArray.push(v.y * 1e-3, v.x * 1e-3);
else
uvArray.push(v.x * 1e-3, v.y * 1e-3);
}
for (let hole of this.children) {
for (let h of hole.children) {
h.Draw(verticesArray, uvArray, front, z, rotateUv, allDepth); //, depth + 2
}
}
return; //如果不倒角 就不执行下面的代码
}
static ParseContourTree(contourNodes) {
if (contourNodes.length < 2)
return;
let fb = new Flatbush__default["default"](contourNodes.length);
for (let node of contourNodes) {
node.box = node.contour.BoundingBox;
node.area = node.contour.Area;
fb.add(node.box.min.x, node.box.min.y, node.box.max.x, node.box.max.y);
}
fb.finish();
for (let i = 0; i < contourNodes.length; i++) {
const node1 = contourNodes[i];
let p = node1.contour.Curve.StartPoint;
let ids = fb.search(node1.box.min.x, node1.box.min.y, node1.box.max.x, node1.box.max.y);
ids.sort((i1, i2) => contourNodes[i1].area - contourNodes[i2].area);
for (let id of ids) {
if (id === i)
continue;
let node2 = contourNodes[id];
if (node2.parent === node1 || node2.area < node1.area)
continue; //避免自己的儿子成为自己的父亲
if (node2.contour.Curve.PtInCurve(p)) {
node1.SetParent(node2);
break;
}
}
}
}
}
class EdgeGeometryBuild {
constructor(allDepth) {
this.allDepth = allDepth;
this.lineVerticesArray = [];
this.frontLines = [];
this.backLines = [];
}
AddLidLine(p1, p2, depth) {
if (depth === 0) {
p1 = p1.clone().setZ(0);
p2 = p2.clone().setZ(0);
let line = new exports.Line(p1, p2);
this.backLines.push(line);
}
else if (depth === this.allDepth) {
p1 = p1.clone().setZ(0);
p2 = p2.clone().setZ(0);
let line = new exports.Line(p1, p2);
this.frontLines.push(line);
}
}
BuildLid(verticesArray, uvArray, rotateUv) {
let arr = [this.backLines, this.frontLines];
for (let index = 0; index < 2; index++) {
let lines = arr[index];
let parse = new RegionParse(lines, 2);
let contourNodes = [];
//对于未被面域分析出来的线,我们进行join操作
let unusedLines = [];
for (let l of lines)
if (!parse.GetCueveUsed(l))
unusedLines.push(l);
if (unusedLines.length > 2) {
let groups = curveLinkGroup(unusedLines, 1);
for (let g of groups) {
if (g.length < 2)
continue;
let pl = new exports.Polyline();
pl.ColorIndex = g[0].ColorIndex;
pl.OCS = ComputerCurvesNormalOCS(g);
for (let cu of g) {
if (pl.Join(cu, false, 0.1) === exports.Status.True)
cu.Erase();
}
pl.CloseMark = true;
let c = Contour.CreateContour(pl, false) ?? CreateContour2(pl.Explode(), 0.1);
if (c && c.Area > 0.1)
contourNodes.push(new ContourTreeNode(c));
else
console.error("错误:构建板件的MeshGeometry的盖子生成轮廓失败了!");
}
}
for (let routes of parse.RegionsOutline) {
let cs = routes.map(r => r.curve);
let c = Contour.CreateContour(cs, false) ?? CreateContour2(cs) ?? CreateContour2(cs, 0.1);
if (c)
contourNodes.push(new ContourTreeNode(c));
else
console.error("错误:构建板件的MeshGeometry的盖子生成轮廓失败了!");
}
ContourTreeNode.ParseContourTree(contourNodes);
for (let j = contourNodes.length; j--;) {
let node = contourNodes[j];
if (node.parent)
continue;
node.Draw(verticesArray, uvArray, index === 1, this.allDepth * index, rotateUv, this.allDepth);
}
}
}
}
/**
* 胶带
*/
class Tape {
constructor(start, end, bottom, top) {
this.start = start;
this.end = end;
this.bottom = bottom;
this.top = top;
}
//用于测试
get Curve() {
return new exports.Polyline().RectangleFrom2Pt(new three.Vector3(this.start, this.bottom), new three.Vector3(this.end, this.top));
}
Clip(t) {
let yr = IntersectRange(this.bottom, this.top, t.bottom, t.top, 1e5);
if (yr === undefined)
return [this];
let xr = IntersectRange(this.start, this.end, t.start, t.end, 1e5);
if (xr === undefined)
return [this];
let rem = SubtractRange(this.start, this.end, t.start, t.end, 1e5).map(r => {
return new Tape(r[0], r[1], this.bottom, this.top);
});
let remR = SubtractRange(this.bottom, this.top, t.bottom, t.top, 1e5);
for (let hr of remR) {
rem.push(new Tape(xr[0], xr[1], hr[0], hr[1]));
}
return rem;
}
Split(xlst) {
let ret = [];
let pre = this.start;
for (let x of xlst) {
if (x > pre) {
if (x >= this.end)
x = this.end;
if (equaln$1(pre, x))
continue;
ret.push(new Tape(pre, x, this.bottom, this.top));
pre = x;
if (x === this.end)
break;
}
}
if (pre < this.end) //避免最后一个切割元素小于终点时没有补上最后一个
ret.push(new Tape(pre, this.end, this.bottom, this.top));
return ret;
}
}
/**
* 二维形状,内部用曲线胶带表示(用来计算盖子差集算法)
*/
class CurveTapeShape {
constructor(contour, holes) {
this.children = [];
this.contour = new CurveTape(contour, DirectionType.Outer);
this.holes = holes.map(h => new CurveTape(h, DirectionType.Inner));
}
CloneNew() {
let s = new CurveTapeShape(this.contour.contour, this.holes.map(h => h.contour));
return s;
}
/**
* 删除包含,同向
*/
ClipTo(s, append = false) {
for (let c of [this.contour, ...this.holes])
if (c.tapes.length > 0)
c.ClipTo(s);
if (append) {
let sn = s.CloneNew();
sn.ReverseClipTo(this);
this.children.push(sn);
}
}
//合理打断(以保证三维网格对齐(否则圆弧点将无法正确的对齐))
SplitTo(s) {
for (let c of [this.contour, ...this.holes]) {
for (let c2 of [s.contour, ...s.holes]) {
let int = GetIntersection(c.contour.Curve, c2.contour.Curve);
c.splitParams.push(...int.map(i => i.thisParam));
}
}
}
/**
* 只保留被包含部分
*/
ReverseClipTo(s) {
for (let c of [this.contour, ...this.holes])
if (c.tapes.length > 0)
c.ReverseClipTo(s);
return this;
}
ChildrenClip() {
for (let i = 0; i < this.children.length; i++) {
let s1 = this.children[i];
for (let j = i + 1; j < this.children.length; j++) {
let s2 = this.children[j];
s1.ClipTo(s2, false);
s2.ClipTo(s1, false);
}
}
}
Draw(verticesArray, uvArray, front, z, rotateUv, allDepth) {
this.ChildrenClip();
let polylines = this.contour.Curves;
for (let h of this.holes)
polylines.push(...h.Curves);
for (let s of this.children) {
polylines.push(...s.contour.Curves);
for (let h of s.holes)
polylines.push(...h.Curves);
}
// TestDraw(polylines, z);
let groups = curveLinkGroup(polylines);
let contourNodes = [];
for (let cus of groups) {
let c = Contour.CreateContour(cus, false);
if (c)
contourNodes.push(new ContourTreeNode(c));
else
console.error("出错");
}
ContourTreeNode.ParseContourTree(contourNodes);
for (let j = contourNodes.length; j--;) {
let node = contourNodes[j];
// TestDraw(s.contour.Curve.Clone(), z);
if (node.parent)
continue;
node.Draw(verticesArray, uvArray, front, z, rotateUv, allDepth);
}
}
}
/**
* 曲线胶带(一维)
*/
class CurveTape {
constructor(contour, wallType) {
this.contour = contour;
this.wallType = wallType;
this.splitParams = [];
this.tapes = [[0, this.contour.Curve.EndParam]];
}
get Curves() {
let xparams = SplitCurveParams(this.contour.Curve);
if (this.splitParams.length > 0) {
xparams.push(...this.splitParams);
arraySortByNumber$1(xparams);
arrayRemoveDuplicateBySort(xparams, (p1, p2) => equaln$1(p1, p2));
}
let polylines = [];
function TD(p) {
return { pt: AsVector2(p), bul: 0 };
}
const addPolyline = (t) => {
let pts = [TD(this.contour.Curve.GetPointAtParam(t[0]))];
for (let x of xparams) {
if (x <= t[0])
continue;
if (x >= t[1])
break;
pts.push(TD(this.contour.Curve.GetPointAtParam(x)));
}
pts.push(TD(this.contour.Curve.GetPointAtParam(t[1])));
let pl = new exports.Polyline(pts);
polylines.push(pl);
};
for (let t of this.tapes) {
if (t[0] > t[1]) {
addPolyline([0, t[1]]);
addPolyline([t[0], this.contour.Curve.EndParam]);
}
else
addPolyline(t);
}
return polylines;
}
/**
* 分析与另一个形状的包含关系
*/
Parse(s) {
let [res1] = ParseCurveParamRangeRelation(this.contour.Curve, s.contour.contour.Curve);
if (this.wallType === DirectionType.Inner)
[res1.syntropy, res1.reverse] = [res1.reverse, res1.syntropy];
if (res1.container.length > 0) {
for (let h of s.holes) {
let [res2] = ParseCurveParamRangeRelation(this.contour.Curve, h.contour.Curve);
if (this.wallType === DirectionType.Outer)
[res2.syntropy, res2.reverse] = [res2.reverse, res2.syntropy];
res1.syntropy.push(...res2.syntropy);
res1.reverse.push(...res2.reverse);
res1.container = SubtractRanges(res1.container, res2.container, this.contour.Curve.EndParam);
res1.container = SubtractRanges(res1.container, res2.syntropy, this.contour.Curve.EndParam);
res1.container = SubtractRanges(res1.container, res2.reverse, this.contour.Curve.EndParam);
}
}
return res1;
}
/**
* 删除包含,同向面
*/
ClipTo(s) {
let d = this.Parse(s);
this.tapes = SubtractRanges(this.tapes, d.container, this.contour.Curve.EndParam);
this.tapes = SubtractRanges(this.tapes, d.syntropy, this.contour.Curve.EndParam);
return this;
}
/**
* 保留被包含的部分
*/
ReverseClipTo(s) {
this.tapes = this.Parse(s).container;
return this;
}
}
class ExtudeWall {
constructor(curve, depthType, depth, allDepth, wallType) {
this.curve = curve;
this.depthType = depthType;
this.depth = depth;
this.allDepth = allDepth;
this.wallType = wallType;
//一整段
this.Tape = [CreateTape(depthType, 0, this.curve.EndParam, depth, allDepth)];
}
/**
* 减去在另一个groove内的部分
* @param groove this - groove
* @param [clipSyntropy=false] 删除同向的面
*/
ClipTo(groove, clipSyntropy = false) {
let [res1] = ParseCurveParamRangeRelation(this.curve, groove.contourWall.curve);
if (this.wallType !== groove.contourWall.wallType)
[res1.syntropy, res1.reverse] = [res1.reverse, res1.syntropy];
if (res1.container.length > 0) {
for (let h of groove.holeWalls) {
let [resh1] = ParseCurveParamRangeRelation(this.curve, h.curve);
//翻转
if (this.wallType !== h.wallType)
[resh1.syntropy, resh1.reverse] = [resh1.reverse, resh1.syntropy];
//删除在网洞内的
let subParams;
if (clipSyntropy)
subParams = resh1.container; //删除共面,
else
subParams = [...resh1.container, ...resh1.syntropy]; //保留共面部分
for (let i of subParams) {
let rems = [];
for (let r of res1.container)
rems.push(...SubtractRange(r[0], r[1], i[0], i[1], this.curve.EndParam));
res1.container = rems;
}
}
}
let params = [...res1.container, ...res1.reverse];
if (clipSyntropy)
params.push(...res1.syntropy);
for (let c of params)
this.ClipFromParam(c[0], c[1], groove.depthType, groove.depth);
}
ClipReverse(wall) {
let [res1] = ParseCurveParamRangeRelation(this.curve, wall.curve);
for (let c of res1.syntropy)
this.ClipFromParam(c[0], c[1], wall.depthType, wall.depth);
}
/**
* 当起始参数大于终止参数时,裁剪的区域经过终点
*
* @param startParam 起始参数
* @param endParam 终止参数
* @param faceType 裁剪面朝向
* @param depth 裁剪面的深度
*/
ClipFromParam(startParam, endParam, faceType, depth) {
if (equaln$1(startParam, endParam))
return;
if (startParam > endParam) {
this.ClipFromParam(startParam, this.curve.EndParam, faceType, depth);
this.ClipFromParam(0, endParam, faceType, depth);
return this;
}
let subTape = CreateTape(faceType, startParam, endParam, depth, this.allDepth);
let taps = [];
for (let t of this.Tape)
taps.push(...t.Clip(subTape));
this.Tape = taps;
return this;
}
Draw(verticesArray, uvArray, edgeBuild) {
let xparams = SplitCurveParams(this.curve);
let isOuter = this.wallType === DirectionType.Outer;
let allDepth = this.allDepth;
function AddVertice(v) {
verticesArray.push(v.x);
verticesArray.push(v.y);
if (isOuter && ExtrudeBuildConfig.bevel) //如果倒角,则执行下面的代码
{
if (v.z === 0)
verticesArray.push(1);
else if (v.z === allDepth)
verticesArray.push(allDepth - 1);
else
verticesArray.push(v.z);
}
else
verticesArray.push(v.z);
}
let tapes = [];
this.Tape.sort((t1, t2) => t1.start - t2.start);
for (let tape of this.Tape)
tapes.push(...tape.Split(xparams));
for (let i = 0; i < tapes.length; i++) {
let preIndex = FixIndex$1(i - 1, tapes);
let nextIndex = FixIndex$1(i + 1, tapes);
let tape = tapes[i];
let preTape = tapes[preIndex];
let nextTape = tapes[nextIndex];
let p1 = this.curve.GetPointAtParam(tape.start).setZ(tape.bottom);
let p2 = this.curve.GetPointAtParam(tape.end).setZ(tape.bottom);
let vs = [p1, p2, p2.clone().setZ(tape.top), p1.clone().setZ(tape.top), p1];
edgeBuild.AddLidLine(p1, p2, tape.bottom);
edgeBuild.AddLidLine(p1, p2, tape.top);
//#region 构造线框
{
let leftRanges;
let rightRange;
const IsInteger = (n) => equaln$1(n, Math.round(n), 1e-8);
if (!IsInteger(tape.start) && equaln$1(tape.start, preTape.end))
leftRanges = SubtractRange(tape.bottom, tape.top, preTape.bottom, preTape.top, this.allDepth);
else
leftRanges = [[tape.bottom, tape.top]];
if (equaln$1(tape.end, nextTape.start))
rightRange = SubtractRange(tape.bottom, tape.top, nextTape.bottom, nextTape.top, this.allDepth);
else
rightRange = [[tape.bottom, tape.top]];
//上下两条线
edgeBuild.lineVerticesArray.push(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z, p1.x, p1.y, tape.top, p2.x, p2.y, tape.top);
//左右线
for (let range of leftRanges) {
edgeBuild.lineVerticesArray.push(p1.x, p1.y, range[0], p1.x, p1.y, range[1]);
}
for (let range of rightRange) {
edgeBuild.lineVerticesArray.push(p2.x, p2.y, range[0], p2.x, p2.y, range[1]);
}
}
//#endregion
//和X平行平行
let isXPar = equaln$1(vs[0].x, vs[1].x, 1e-2);
function AddUv(p) {
if (isXPar)
uvArray.push((p.z - 1) * 1e-3, p.y * 1e-3);
else
uvArray.push((p.z - 1) * 1e-3, p.x * 1e-3);
}
if (this.wallType === DirectionType.Outer) {
AddVertice(vs[0]);
AddUv(vs[0]);
AddVertice(vs[1]);
AddUv(vs[1]);
AddVertice(vs[2]);
AddUv(vs[2]);
AddVertice(vs[0]);
AddUv(vs[0]);
AddVertice(vs[2]);
AddUv(vs[2]);
AddVertice(vs[3]);
AddUv(vs[3]);
}
else {
AddVertice(vs[0]);
AddUv(vs[0]);
AddVertice(vs[2]);
AddUv(vs[2]);
AddVertice(vs[1]);
AddUv(vs[1]);
AddVertice(vs[0]);
AddUv(vs[0]);
AddVertice(vs[3]);
AddUv(vs[3]);
AddVertice(vs[2]);
AddUv(vs[2]);
}
}
}
}
/**
* 分析两个曲线关系(包含,分离,同向共线,反向共线)(用参数范围表示)
*/
function ParseCurveParamRangeRelation(cu1, cu2, reverseParse = false) {
let ins = GetIntersection(cu1, cu2);
ins.sort((a1, a2) => a1.thisParam - a2.thisParam);
//点重复->下方ins会sort,导致交点对应不上,导致错误
arrayRemoveDuplicateBySort(ins, (i1, i2) => equalv3(i1.pt, i2.pt, 1e-4));
if (ins.length > 1 && equalv3(ins[0].pt, ins[ins.length - 1].pt, 1e-4))
ins.pop();
let c1Res = { container: [], syntropy: [], reverse: [], outer: [] };
let c2Res = { container: [], syntropy: [], reverse: [], outer: [] };
if (ins.length === 0) {
if (cu1 instanceof exports.Circle && cu2 instanceof exports.Circle && equaln$1(cu1.Radius, cu2.Radius, 1e-4) && equalv2(cu1.Center, cu2.Center, 1e-4)) {
c1Res.syntropy.push([0, 1]);
c2Res.syntropy.push([0, 1]);
return [c1Res, c2Res];
}
let a1 = cu1.Area, a2 = cu2.Area;
if (a2 > a1 && cu2.PtInCurve(cu1.StartPoint)) //cu2包含cu1
c1Res.container.push([0, cu1.EndParam]);
else
c1Res.outer.push([0, cu1.EndParam]);
if (a1 > a2 && cu1.PtInCurve(cu2.StartPoint)) //cu1包含cu2
c2Res.container.push([0, cu2.EndParam]);
else
c2Res.outer.push([0, cu2.EndParam]);
return [c1Res, c2Res];
}
if (ins.length === 1) {
let a1 = cu1.Area, a2 = cu2.Area;
if (a2 > a1 && fastCurveInCurve2(cu2, cu1)) //cu2包含cu1
c1Res.container.push([0, cu1.EndParam]);
else
c1Res.outer.push([0, cu1.EndParam]);
if (a1 > a2 && fastCurveInCurve2(cu1, cu2)) //cu1包含cu2
c2Res.container.push([0, cu2.EndParam]);
else
c2Res.outer.push([0, cu2.EndParam]);
return [c1Res, c2Res];
}
//解析出线段列表
let c1Curves = [];
let c2Curves = [];
for (let i = 0; i < ins.length; i++) {
let n1 = ins[i];
let n2 = ins[FixIndex$1(i + 1, ins)];
c1Curves.push({ startParam: n1.thisParam, endParam: n2.thisParam, startPoint: n1.pt, endPoint: n2.pt });
}
ins.sort((a1, a2) => a1.argParam - a2.argParam);
for (let i = 0; i < ins.length; i++) {
let n1 = ins[i];
let n2 = ins[FixIndex$1(i + 1, ins)];
c2Curves.push({ startParam: n1.argParam, endParam: n2.argParam, startPoint: n1.pt, endPoint: n2.pt });
}
//分析共边关系和包含关系
for (let c of c1Curves) {
let c1MidPoint = CenterPoint(cu1, c.startParam, c.endParam);
for (let c2 of c2Curves) {
if (c2.used)
continue;
let c2MidPoint = CenterPoint(cu2, c2.startParam, c2.endParam);
if (!equalv3(c1MidPoint, c2MidPoint, 1e-4))
continue;
c.used = true;
if (c.startPoint === c2.startPoint
&& c.endPoint === c2.endPoint) {
c1Res.syntropy.push([c.startParam, c.endParam]);
c2Res.syntropy.push([c2.startParam, c2.endParam]);
c2.used = true;
break;
}
else if (c.startPoint === c2.endPoint
&& c.endPoint === c2.startPoint) {
c1Res.reverse.push([c.startParam, c.endParam]);
c2Res.reverse.push([c2.startParam, c2.endParam]);
c2.used = true;
break;
}
else
c.used = false;
}
if (!c.used) {
if (cu2.PtInCurve(c1MidPoint))
c1Res.container.push([c.startParam, c.endParam]);
else
c1Res.outer.push([c.startParam, c.endParam]);
}
}
//只分析包含关系
if (reverseParse)
for (let c of c2Curves) {
if (c.used)
continue;
let p = CenterPoint(cu2, c.startParam, c.endParam);
if (cu1.PtInCurve(p))
c2Res.container.push([c.startParam, c.endParam]);
else
c2Res.outer.push([c.startParam, c.endParam]);
}
return [c1Res, c2Res];
}
function CenterPoint(cu, start, end) {
let lenStart = cu.GetDistAtParam(start);
let lenEnd = cu.GetDistAtParam(end);
if (end > start)
return cu.GetPointAtDistance((lenEnd + lenStart) * 0.5);
let lenAll = cu.Length;
let lenDiv = ((lenAll - lenStart) + lenEnd) * 0.5;
if (lenStart + lenDiv >= lenAll)
return cu.GetPointAtDistance(lenStart + lenDiv - lenAll);
else
return cu.GetPointAtDistance(lenStart + lenDiv);
}
/**
*
* @param orgStart 被裁剪范围的起点(如果起点大于终点,那么表示 s->end + 0->e)
* @param orgEnd
* @param clipStart
* @param clipEnd
* @param end
* @returns
*/
function SubtractRange(orgStart, orgEnd, clipStart, clipEnd, end) {
if (orgStart < 0
|| orgEnd < 0
|| orgEnd > end
|| orgStart > end
|| clipStart < 0
|| clipEnd < 0
|| clipStart > end
|| clipEnd > end)
return [];
if (orgStart > orgEnd)
return SubtractRange(orgStart, end, clipStart, clipEnd, end).concat(SubtractRange(0, orgEnd, clipStart, clipEnd, end));
if (clipStart > clipEnd) {
let arr = SubtractRange(orgStart, orgEnd, clipStart, end, end);
let rem = [];
for (let s of arr)
arrayPushArray$1(rem, SubtractRange(s[0], s[1], 0, clipEnd, end));
return rem;
}
if (clipStart >= orgEnd || clipEnd <= orgStart)
return [[orgStart, orgEnd]];
if (clipStart <= orgStart) // c1 a1 b1
{
if (clipEnd >= orgEnd)
return [];
return [[clipEnd, orgEnd]];
}
if (clipEnd < orgEnd)
return [[orgStart, clipStart], [clipEnd, orgEnd]];
return [[orgStart, clipStart]];
}
function SubtractRange2(r, sr, end) {
return SubtractRange(r[0], r[1], sr[0], sr[1], end);
}
function SubtractRanges(ranges, subRanges, end) {
let rets = ranges;
for (let sr of subRanges) {
let temps = [];
for (let r of rets)
arrayPushArray$1(temps, SubtractRange2(r, sr, end));
rets = temps;
}
return rets;
}
function IntersectRange(a, b, c, d, end) {
let b1 = b < a ? b + end : b;
let d1 = d < c ? d + end : d;
let a1 = a;
let c1 = c;
if (c < a)
[a1, b1, c1, d1] = [c1, d1, a1, b1];
if (c1 > b1)
return;
return [c1, Math.min(b1, d1)];
}
const alMatrix4 = new three.Matrix4;
class ExtrudeGeometryBuilder {
constructor(br, rotateUv = false) {
this.br = br;
this.verticesArray = []; //用于构建三维网格
this.uvArray = []; //uv
this.GenerateMeshData(br, rotateUv);
}
GenerateMeshData(br, rotateUv) {
this.edgeAndLidBuilder = new EdgeGeometryBuild(this.br.Thickness);
rotateUv = rotateUv || (br instanceof Board && br.BoardProcessOption.lines === LinesType.Reverse);
//计算墙(创建轮廓取出,为了得到正确的轮廓曲线(逆时针之类的))
let outerWall = new ExtudeWall(Contour.CreateContour(br.ContourCurve.Clone()).Curve, DepthType.All, br.Thickness, br.Thickness, DirectionType.Outer);
let grooves = this.ParseGrooves();
if (grooves.length < MaxDrawGrooveCount) //只能绘制1000个以下的造型
for (let i = 0; i < grooves.length; i++) {
let s1 = grooves[i];
outerWall.ClipTo(s1, false);
s1.contourWall.ClipReverse(outerWall);
for (let j = i + 1; j < grooves.length; j++) {
let s2 = grooves[j];
s1.ClipTo(s2, true);
}
s1.Draw(this.verticesArray, this.uvArray, this.edgeAndLidBuilder, rotateUv);
}
outerWall.Draw(this.verticesArray, this.uvArray, this.edgeAndLidBuilder);
//这里构建盖子
this.edgeAndLidBuilder.BuildLid(this.verticesArray, this.uvArray, rotateUv);
intCache.clear();
}
get MeshGeometry() {
let geo = new three.BufferGeometry();
geo.setAttribute('position', new three.Float32BufferAttribute(this.verticesArray, 3));
geo.setAttribute('uv', new three.Float32BufferAttribute(this.uvArray, 2));
geo.computeVertexNormals();
return geo;
}
get EdgeGeometry() {
let geo = new three.BufferGeometry();
geo.setAttribute('position', new three.Float32BufferAttribute(this.edgeAndLidBuilder.lineVerticesArray, 3));
return geo;
}
ParseGrooves() {
let br = this.br;
const brOcsInv = br.OCSInv;
let grooves = [];
//备份原始的槽,下面的代码为了合并槽,会将板的槽先清空,后续我们在还原它 (未来我们可能不强制合并板的槽 而是在这里合并? 保证板的槽的纯粹?例如矩形)
let bakGrooves;
{
//合并深度相同 但是半径不相同的槽
let thicknessKnifsMap = new Map(); //深度->刀半径Set
let thicknessGroovesMap = new Map(); //深度->刀半径Set
let fuzz = new FuzzyFactory(2, 1e-2);
for (let g of br.Grooves) //准备数据 map
{
let thick = fuzz.lookupOrCreate([g.Thickness], g.Thickness);
let knifsSet = thicknessKnifsMap.get(thick);
let grooveArr = thicknessGroovesMap.get(thick);
if (!knifsSet) {
knifsSet = new Set();
grooveArr = [];
thicknessKnifsMap.set(thick, knifsSet);
thicknessGroovesMap.set(thick, grooveArr);
}
grooveArr.push(g);
knifsSet.add(g.KnifeRadius);
}
let mergeGrooves = [];
for (let [thick, set] of thicknessKnifsMap) {
if (set.size > 1) //如果深度相同的大于1
{
if (!bakGrooves)
bakGrooves = br.Grooves.concat(); //复制一个
let arr = thicknessGroovesMap.get(thick).map(g => {
let newg = g.Clone();
newg.KnifeRadius = 0;
return newg;
});
br.Grooves.length = 0;
br.Grooves.push(...arr);
br.GrooveCheckMerge(false);
mergeGrooves.push(...br.Grooves);
}
else {
mergeGrooves.push(...thicknessGroovesMap.get(thick));
}
}
if (bakGrooves) {
br.Grooves.length = 0;
br.Grooves.push(...mergeGrooves);
}
}
for (let groove of br.Grooves) {
//判断槽正反面
let type;
if (equaln$1(groove.Thickness, br.Thickness))
type = DepthType.All;
else {
if (equaln$1(groove.Position.applyMatrix4(brOcsInv).z, 0))
type = DepthType.Back;
else
type = DepthType.Front;
}
alMatrix4.multiplyMatrices(brOcsInv, groove.OCSNoClone);
//槽轮廓
let grooveContourCurve = groove.ContourCurve.Clone();
grooveContourCurve.ApplyMatrix(alMatrix4);
grooveContourCurve.Z0();
if (grooveContourCurve instanceof exports.Polyline)
grooveContourCurve.UpdateOCSTo(IdentityMtx4); //不可能改变这个
let grooveContour = Contour.CreateContour(grooveContourCurve);
let grooveHoleContours = [];
//孤岛
for (let grooveChild of groove.Grooves) {
let grooveChildContourCurve = grooveChild.ContourCurve.Clone();
alMatrix4.multiplyMatrices(brOcsInv, grooveChild.OCSNoClone);
grooveChildContourCurve.ApplyMatrix(alMatrix4).Z0();
if (grooveChildContourCurve instanceof exports.Polyline)
grooveChildContourCurve.UpdateOCSTo(IdentityMtx4);
let grooveChildContour = Contour.CreateContour(grooveChildContourCurve);
grooveHoleContours.push(grooveChildContour);
}
grooves.push(new Groove(grooveContour, grooveHoleContours, type, groove.Thickness, br.Thickness));
}
if (bakGrooves) {
br.Grooves.length = 0;
br.Grooves.push(...bakGrooves);
}
return grooves;
}
}
let intCache = new Map();
function GetIntersection(cu1, cu2) {
let m = intCache.get(cu1);
if (m) {
let r = m.get(cu2);
if (r)
return r;
}
else
m = new Map();
intCache.set(cu1, m);
let r = cu1.IntersectWith2(cu2, IntersectOption.ExtendNone, 1e-4);
let cu1EndParam = cu1.EndParam;
let cu2EndParam = cu2.EndParam;
for (let d of r) {
d.thisParam = three.MathUtils.clamp(d.thisParam, 0, cu1EndParam);
d.argParam = three.MathUtils.clamp(d.argParam, 0, cu2EndParam);
}
m.set(cu2, r);
let r2 = r.map(r => {
return { thisParam: r.argParam, argParam: r.thisParam, pt: r.pt };
});
let m2 = intCache.get(cu2);
if (!m2) {
m2 = new Map();
intCache.set(cu2, m2);
}
m2.set(cu1, r2);
return r;
}
const DIRS = [XAxis, YAxis, ZAxis, XAxisN, YAxisN, ZAxisN];
function GetFaceDir(direction) {
let absx = Math.abs(direction.x);
let absy = Math.abs(direction.y);
let absz = Math.abs(direction.z);
let face = -1.0;
if (absx > absz) {
if (absx > absy)
face = direction.x > 0 ? 0 : 3;
else
face = direction.y > 0 ? 1 : 4;
}
else {
if (absz > absy)
face = direction.z > 0 ? 2 : 5;
else
face = direction.y > 0 ? 1 : 4;
}
return DIRS[face];
}
class GenUVForWorld {
constructor() {
this.InvMtxMap = new Map();
this._Z = new three.Vector3;
this._X = new three.Vector3;
this._Y = new three.Vector3;
this._Box = new three.Box3;
this._Box2 = new three.Box3;
}
GetMtxInv(normalX, normalY, normalZ) {
this._Z.set(normalX, normalY, normalZ);
let n = GetFaceDir(this._Z);
let mtx = this.InvMtxMap.get(n);
if (mtx)
return mtx;
this._Z.copy(n);
Orbit.ComputUpDirection(this._Z, this._Y, this._X);
mtx = new three.Matrix4().makeBasis(this._X, this._Y, this._Z);
mtx.getInverse(mtx);
this.InvMtxMap.set(n, mtx);
return mtx;
}
GenUV(mesh) {
if (Array.isArray(mesh.material)) {
let geo = mesh.geometry;
if (!geo.boundingBox)
geo.computeBoundingBox();
let normals = geo.getAttribute("normal");
let pos = geo.getAttribute("position");
let uvs = geo.getAttribute("uv");
for (let i = 0; i < mesh.material.length; i++) {
let mtl = mesh.material[i];
if (mtl[USE_WORLD_UV]) {
this._Box.makeEmpty();
let g = mesh.geometry.groups[i];
for (let y = 0; y < g.count; y++) {
let index = (y + g.start) * 3;
this._X.set(pos.array[index], pos.array[index + 1], pos.array[index + 2]);
this._Box.expandByPoint(this._X);
}
for (let y = 0; y < g.count; y++) {
let index = (y + g.start) * 3;
let mtx = this.GetMtxInv(normals.array[index], normals.array[index + 1], normals.array[index + 2]);
this._X.set(pos.array[index], pos.array[index + 1], pos.array[index + 2]);
this._X.applyMatrix4(mtx);
this._Box2.copy(this._Box).applyMatrix4(mtx);
//@ts-ignore
uvs.array[(y + g.start) * 2] = (((this._X.x - (this._Box2.min.x + this._Box2.max.x) * 0.5)) * 1e-2 + mtl[U_WORLD_MOVE]) * mtl[U_WORLD_REP] + 0.5;
//@ts-ignore
uvs.array[(y + g.start) * 2 + 1] = (((this._X.y - (this._Box2.min.y + this._Box2.max.y) * 0.5)) * 1e-2 - mtl[V_WORLD_MOVE]) * mtl[V_WORLD_REP] + 0.5;
}
uvs.needsUpdate = true;
}
}
}
else {
let mtl = mesh.material;
if (mtl[USE_WORLD_UV])
this.GenGeoUV(mesh.geometry, mtl, 1e-2);
}
}
GenGeoUV(geo, mtl, scale = 1e-3) {
if (!geo.boundingBox)
geo.computeBoundingBox();
let normals = geo.getAttribute("normal");
let pos = geo.getAttribute("position");
let uvs = geo.getAttribute("uv");
if (!pos || !uvs || pos.count === 0)
return;
for (let y = 0; y < pos.count; y++) {
let index = y * 3;
let mtx = this.GetMtxInv(normals.array[index], normals.array[index + 1], normals.array[index + 2]);
this._X.set(pos.array[index], pos.array[index + 1], pos.array[index + 2]);
this._X.applyMatrix4(mtx);
this._Box.copy(geo.boundingBox);
this._Box.applyMatrix4(mtx);
//@ts-ignore
uvs.array[y * 2] = (((this._X.x - (this._Box.min.x + this._Box.max.x) * 0.5)) * scale + mtl[U_WORLD_MOVE]) * mtl[U_WORLD_REP] + 0.5;
//@ts-ignore
uvs.array[y * 2 + 1] = (((this._X.y - (this._Box.min.y + this._Box.max.y) * 0.5)) * scale + mtl[V_WORLD_MOVE]) * mtl[V_WORLD_REP] + 0.5;
}
uvs.needsUpdate = true;
}
}
var ExtrudeSolid_1;
const MaxDrawGrooveCount = 1000; //最大的绘制槽个数(但是还是会绘制线)
let ExtrudeSolid = ExtrudeSolid_1 = class ExtrudeSolid extends Entity {
constructor() {
super();
/*
y-----------
^ |
| ↑ |
| | | height
| ↓ |
| |
0---width->x
*/
this.height = 1; //y
this.width = 1; //x
/**
* 拉伸实体的厚度
* 我们允许它是一个负数,但是这个时候这个实体已经是一个无效的拉伸实体了.
* 允许负数,用来校验凹槽的合理性.
*/
this.thickness = 1;
this.isRect = true;
this.bevelEnabled = false;
this.IsKnife = false;
/**
* 正面和反面的凹槽造型
*/
this.grooves = [];
this.knifeRadius = 3;
this.groovesAddLength = 0;
this.groovesAddWidth = 0;
this.groovesAddDepth = 0;
this.RelevanceKnifs = this.CreateProxyArray((v) => {
//可以更新自己,但是不建议,建议手动更新
});
this.RelevanceMeats = this.CreateProxyArray((v) => {
//可以更新肉,简单是不建议,建议手动更新
});
}
set Material(materialId) {
let oldMaterial = this.Material ?? this._db?.DefaultMaterial.objectId;
super.Material = materialId;
let isf_old = Boolean(oldMaterial?.Object?.IsFull);
let isf_new = Boolean(materialId?.Object?.IsFull);
if (isf_old !== isf_new)
this.Update();
else if (materialId?.Object?.UseWorldUV) {
if (this._MeshGeometry)
this.GenWorldUV(this._MeshGeometry);
}
}
get Material() { return super.Material; }
get KnifeRadius() {
return this.knifeRadius;
}
set KnifeRadius(v) {
if (!equaln$1(v, this.knifeRadius)) {
this.WriteAllObjectRecord();
this.knifeRadius = v;
//在双击板修改的时候,我们希望它能修改绘制版本,这样它的关联实体也会被刷新
this.__UpdateVersion__++;
}
}
get BoundingBox() {
return this.BoundingBoxInOCS.applyMatrix4(this.OCS);
}
get BoundingBoxInOCS() {
if (this.width > 0 && this.height > 0 && this.thickness > 0)
return new Box3Ext(new three.Vector3, new three.Vector3(this.width, this.height, this.thickness));
else
return new Box3Ext().setFromPoints([new three.Vector3, new three.Vector3(this.width, this.height, this.thickness)]);
}
get OBB() {
return new OBB(this.OCS, new three.Vector3(this.width, this.height, this.thickness).multiplyScalar(0.5));
}
get GroovesAddLength() {
return this.groovesAddLength;
}
set GroovesAddLength(v) {
if (!equaln$1(v, this.groovesAddLength)) {
this.WriteAllObjectRecord();
this.groovesAddLength = v;
//更改它的时候,关联切割被更新,拆单的时候才会正确,否则使用缓存将不正确
this.__UpdateVersion__++;
}
}
get GroovesAddWidth() {
return this.groovesAddWidth;
}
set GroovesAddWidth(v) {
if (!equaln$1(v, this.groovesAddWidth)) {
this.WriteAllObjectRecord();
this.groovesAddWidth = v;
//更改它的时候,关联切割被更新,拆单的时候才会正确,否则使用缓存将不正确
this.__UpdateVersion__++;
}
}
get GroovesAddDepth() {
return this.groovesAddDepth;
}
set GroovesAddDepth(v) {
if (!equaln$1(v, this.groovesAddDepth)) {
this.WriteAllObjectRecord();
this.groovesAddDepth = v;
//更改它的时候,关联切割被更新,拆单的时候才会正确,否则使用缓存将不正确
this.__UpdateVersion__++;
}
}
Clone() {
let en = super.Clone();
return en;
}
ApplyMatrix(m) {
//暂时关闭更新,避免内部实体还没有更新位置时,先更新了实体的Geometry,导致后续没有进行更新
let updateBak = this.AutoUpdate;
this.AutoUpdate = false;
super.ApplyMatrix(m);
let normal = this.Normal.negate();
for (let g of this.grooves) {
g._SpaceOCS.copy(this._SpaceOCS); //因为在镜像(Extrude.ApplyMirrorMatrix)的时候 没有设置这个会导致错误(参考该函数的代码,似乎是为了实现柜子镜像) 所以拷贝一下
g.objectId = new ObjectId;
g.ApplyMatrix(m);
//如果是镜像,如果槽的方向相反了,那么需要修正
let gNormal = g.Normal;
if (equalv3(normal, gNormal, 1e-3)) {
let p = g.Position.add(gNormal.multiplyScalar(g.thickness * 0.5));
g.ApplyMatrix(MakeMirrorMtx(normal, p));
}
g.objectId = undefined;
}
this.AutoUpdate = updateBak;
let te = m.elements;
let scaleXSq = te[0] * te[0] + te[1] * te[1] + te[2] * te[2];
let scaleYSq = te[4] * te[4] + te[5] * te[5] + te[6] * te[6];
let scaleZSq = te[8] * te[8] + te[9] * te[9] + te[10] * te[10];
if (!equaln$1(scaleXSq, 1, 1e-4) || !equaln$1(scaleYSq, 1, 1e-4) || !equaln$1(scaleZSq, 1, 1e-4))
this.Update(exports.UpdateDraw.Geometry);
else if (this.AutoUpdate)
this.DeferUpdate();
return this;
}
ApplyScaleMatrix(m) {
this.WriteAllObjectRecord();
let cu = this.ContourCurve;
cu.ApplyMatrix(this.OCSNoClone);
cu.ApplyMatrix(m);
cu.ApplyMatrix(this.OCSInv);
this.CheckContourCurve();
return this;
}
//实现了这个函数后 实现了柜子的镜像
ApplyMirrorMatrix(m) {
this.WriteAllObjectRecord();
const curve = this.ContourCurve;
if (curve instanceof exports.Polyline && !equalv3(curve.Position, ZeroVec)) //移除多段线的OCS(目前只判断了基点)
{
let pts = curve.LineData;
if (equalv2(pts[0].pt, arrayLast(pts).pt))
pts.pop();
let ocs = curve.OCSNoClone;
for (let p of pts)
Vector2ApplyMatrix4(ocs, p.pt);
curve.OCS = IdentityMtx4;
}
let nor = this.Normal.applyMatrix4(this.SpaceOCSInv.setPosition(ZeroVec)); //法向量在柜子坐标系中的表示
if (equaln$1(Math.abs(nor.z), 1)) //在柜子中是一个层板
{
reviseMirrorMatrix(this._Matrix, 1);
if (curve instanceof exports.Circle)
curve.ApplyMatrix(new three.Matrix4().makeRotationX(Math.PI));
else
reviseMirrorMatrix(curve.OCSNoClone, 1);
this.SetContourCurve(curve);
}
else if (equaln$1(Math.abs(nor.x), 1)) //立板
{
reviseMirrorMatrix(this._Matrix, 2);
this._Matrix.setPosition(this.Position.add(this.Normal.multiplyScalar(-this.Thickness)));
}
else //背板?
{
reviseMirrorMatrix(this._Matrix, 0);
if (curve instanceof exports.Circle)
curve.ApplyMatrix(new three.Matrix4().makeRotationY(Math.PI));
else
reviseMirrorMatrix(curve.OCSNoClone, 0);
this.SetContourCurve(curve);
}
if (this.grooves.length)
this.Update(exports.UpdateDraw.Geometry);
return this;
}
get Width() {
return this.width;
}
get Height() {
return this.height;
}
get Thickness() {
return this.thickness;
}
set Thickness(thickness) {
if (!equaln$1(thickness, this.thickness, 1e-4)) //避免18.0009 无法改成 18
{
this.WriteAllObjectRecord();
if (this.grooves.length > 0) {
let inv = this.OCSInv;
let v = this.Normal.multiplyScalar(thickness - this.thickness);
let m = new three.Matrix4().setPosition(v);
for (let g of this.grooves) {
let p = g.Position.applyMatrix4(inv);
if (equaln$1(g.thickness, this.thickness))
g.Thickness = thickness;
else if (!equaln$1(p.z, 0))
g.ApplyMatrix(m);
}
}
this.thickness = thickness;
this.Update(exports.UpdateDraw.Geometry);
}
}
//修复#I7CBHO的过程中 直接修改了这个属性
get Grooves() {
return this.grooves;
}
/**
* 返回未拷贝的轮廓曲线
*/
get ContourCurve() {
if (!this.contourCurve)
this.GeneralRectContour();
return this.contourCurve;
}
set ContourCurve(cu) {
this.SetContourCurve(cu);
}
/**
* 生成矩形轮廓(强制)
*/
GeneralRectContour() {
if (!this.contourCurve || !(this.contourCurve instanceof exports.Polyline))
this.contourCurve = new exports.Polyline();
this.contourCurve.Rectangle(this.width, this.height);
this.contourCurve.OCS = IdentityMtx4;
this.ContourCurve = this.contourCurve;
}
/**
* 转换成矩形拉伸实体
*/
ConverToRectSolid(width = this.width, height = this.height, thickness = this.thickness) {
this.WriteAllObjectRecord();
this.height = height;
this.width = width;
this.thickness = thickness;
this.isRect = true;
this.GeneralRectContour();
return this;
}
/**
* 更新拉伸实体的轮廓
* @param curve 曲线已经存在WCS坐标系0点
*/
SetContourCurve(curve) {
if (!curve.IsClose)
return;
let area = curve.Area;
if (!area || equaln$1(area, 0))
return;
if (curve instanceof Spline || curve instanceof Ellipse)
curve = curve.Convert2Polyline();
if (curve instanceof exports.Polyline) {
curve.CloseMark = true;
let pts = curve.LineData;
if (equalv2(pts[0].pt, arrayLast(pts).pt))
pts.pop();
//如果曲线被旋转了,那么修正它的旋转矩阵,避免纹路错误
let ocs = curve.OCS;
let x = new three.Vector3().setFromMatrixColumn(ocs, 0);
let y = new three.Vector3().setFromMatrixColumn(ocs, 1);
let z = new three.Vector3().setFromMatrixColumn(ocs, 2);
let z1 = x.cross(y);
let isMirror = equaln$1(ocs.elements[10], -1, 1e-4) || !equalv3(z, z1);
let isRotate = !equaln$1(ocs.elements[0], 1);
if (isMirror || isRotate) // || ocs.elements[9] || ocs.elements[10]
{
for (let p of pts) {
Vector2ApplyMatrix4(ocs, p.pt);
if (isMirror)
p.bul *= -1;
}
curve.OCS = IdentityMtx4;
}
}
else {
curve.OCS = new three.Matrix4().setPosition(curve.Position);
}
curve.ClearDraw();
this.WriteAllObjectRecord();
this.contourCurve = curve;
this.CheckContourCurve();
this.Update();
}
/**
* 在不改变Normal和实体显示的情况下,修改X轴的指向
* @param xAxis
*/
SetXAxis(xAxis) {
let ocsInv = this.OCSInv;
let x = TransformVector(xAxis.clone(), ocsInv).setZ(0).normalize();
if (equalv3(ZeroVec, x, 1e-5))
return this;
this.WriteAllObjectRecord();
let a = Math.atan2(x.y, x.x);
x.transformDirection(this._Matrix);
let z = this.Normal;
let y = z.cross(x);
this._Matrix.elements[0] = x.x;
this._Matrix.elements[1] = x.y;
this._Matrix.elements[2] = x.z;
this._Matrix.elements[4] = y.x;
this._Matrix.elements[5] = y.y;
this._Matrix.elements[6] = y.z;
this.ContourCurve.ApplyMatrix(tempMatrix1.makeRotationZ(-a)); //复用了这个矩阵
this.CheckContourCurve();
if (this.contourCurve instanceof exports.Polyline)
this.contourCurve.UpdateOCSTo(IdentityMtx4);
this.Update();
return this;
}
/**
* 检验轮廓曲线,通常当轮廓曲线被修改时,都需要检验轮廓曲线,并更新实体大小和轮廓位置.
* >计算轮廓大小
* >判断是否矩形
* >修正轮廓基点
* >保证轮廓是逆时针(不再设置)
*/
CheckContourCurve() {
let box = this.ContourCurve.BoundingBox;
let size = box.getSize(new three.Vector3());
this.width = size.x;
this.height = size.y;
if (equaln$1(size.x, 0) || equaln$1(size.y, 0))
Log(`注意!!该板件尺寸为0!`, LogType.Warning);
this.isRect = equaln$1(this.width * this.height, this.ContourCurve.Area, 0.1);
// if (area2 < 0)
// this.contourCurve.Reverse();//矩形板的封边与排钻不需要考虑 异形板的排钻封边可能会错误(无所谓了)
//修正轮廓基点
if (!equalv3(box.min, ZeroVec)) {
this.contourCurve.Position = this.contourCurve.Position.sub(box.min);
let v = box.min.applyMatrix4(this.OCS.setPosition(ZeroVec));
this._Matrix.setPosition(this.Position.add(v));
}
}
get IsRect() {
return this.isRect;
}
/**
* 这个拉伸实体的面域形状
*/
get Shape() { return this.GetShape(false); }
GetShape(filterSmallGroove = false) {
let contour = Contour.CreateContour(this.ContourCurve.Clone(), false);
let holes = [];
for (let g of this.grooves) {
if (equaln$1(g.thickness, this.thickness, 1e-3) && (!filterSmallGroove || g.ContourCurve.Area > 900))
holes.push(Contour.CreateContour(g.ContourCurve.Clone().ApplyMatrix(this.OCSInv.multiply(g.OCSNoClone)), false));
}
return new Shape(contour, holes);
}
/**
* 实体合并(不会删除target)
*/
Join(target) {
let [n, tn] = [this.Normal, target.Normal];
if (!isParallelTo(n, tn))
return exports.Status.False;
let isEqualNorm = equalv3(n, tn);
let targetZMin = target.Position.applyMatrix4(this.OCSInv).z;
let targetZMax = targetZMin + target.Thickness * (isEqualNorm ? 1 : -1);
[targetZMin, targetZMax] = arraySortByNumber$1([targetZMin, targetZMax]);
const MergeRelevance = () => {
if (!this.Id || !target.Id)
return;
for (let kf of target.RelevanceKnifs) {
let kfBr = kf.Object;
if (!kfBr)
continue;
if (!kfBr.RelevanceMeats.includes(this.Id))
kfBr.RelevanceMeats.push(this.Id);
if (!this.RelevanceKnifs.includes(kf))
this.RelevanceKnifs.push(kf);
}
for (let meat of target.RelevanceMeats) {
let meatBr = meat.Object;
if (!meatBr)
continue;
if (!meatBr.RelevanceKnifs.includes(this.Id))
meatBr.RelevanceKnifs.push(this.Id);
if (!this.RelevanceMeats.includes(meat))
this.RelevanceMeats.push(meat);
}
};
if (equaln$1(this.thickness, target.thickness)
&& equaln$1(0, targetZMin)) {
let matrixToLocal = this.OCSInv.multiply(target.OCS);
let thisShape = this.Shape;
let targetShape = target.Shape.ApplyMatrix(matrixToLocal).Z0();
let unionShapes = thisShape.UnionBoolOperation(targetShape, true);
if (unionShapes.length === 1) {
this.WriteAllObjectRecord();
// [ + ] 产生网洞.
for (let hole of unionShapes[0].Holes) {
let g = new ExtrudeSolid_1();
g.thickness = this.thickness;
g.ContourCurve = hole.Curve;
g.ApplyMatrix(this.OCS);
this.AppendGroove(g);
}
this.ContourCurve = unionShapes[0].Outline.Curve;
this.grooves.push(...target.grooves.map(g => g.Clone()));
MergeRelevance();
this.GrooveCheckMerge();
this.Update();
return exports.Status.True;
}
}
else {
if (!isIntersect(0, this.thickness, targetZMin, targetZMax, 1e-5))
return exports.Status.False;
let matrixToLocal = this.OCSInv.multiply(target.OCS);
let thisCurve = this.ContourCurve;
let targetCurve = target.ContourCurve.Clone().ApplyMatrix(matrixToLocal);
targetCurve.Position = targetCurve.Position.setZ(0);
if (equalCurve(thisCurve, targetCurve)) {
this.WriteAllObjectRecord();
if (targetZMin < 0)
this.Position = this.Position.add(n.multiplyScalar(targetZMin));
this.Thickness = Math.max(this.Thickness, targetZMax) - Math.min(0, targetZMin);
this.grooves.push(...target.grooves.map(g => g.Clone()));
MergeRelevance();
this.GrooveCheckMerge();
this.Update();
return exports.Status.True;
}
}
return exports.Status.False;
}
get Volume() {
let sum = this.ContourCurve.Area * this.thickness;
for (let g of this.grooves)
sum -= g.Volume;
return sum;
}
/**
* 被切割
* @param extrudes 切割刀
* @param [output=undefined] 如果实体被分裂,那么输出分裂的其他实体(如果为空,则尝试入当前实体的容器中)
* @param [checkIntersect=true] 检查相交,性能优化
* @returns 切割是否成功
*/
Subtract(extrudes, output = undefined, checkIntersect = true) {
return false;
}
RelevanceSubtract(knif, check = false) {
if (!this.Id || !knif.Id)
return;
//判断是否已经存在
if (check) {
let index = this.RelevanceKnifs.findIndex(id => id.Index === knif.Id.Index);
if (index !== -1)
return;
}
this.RelevanceKnifs.push(knif.Id);
knif.RelevanceMeats.push(this.Id);
}
/**
* 当实体被分裂后,加入新图纸时,需要修复关联拉槽
*/
RepairRelevance() {
if (!this.Id) {
console.error("不能修复未加入到图纸的板件!");
return;
}
for (let id of this.RelevanceKnifs) {
if (id && !id.IsErase) {
let br = id.Object;
br.RelevanceMeats.push(this.Id);
}
}
for (let id of this.RelevanceMeats) {
if (id && !id.IsErase) {
let br = id.Object;
br.RelevanceKnifs.push(this.Id);
}
}
}
AppendGroove(groove) {
this.WriteAllObjectRecord();
this.grooves.push(groove);
}
/** 添加槽进板件,并且自动分裂.
* 通常槽已经校验过准确性,所以不在校验
*/
AppendGrooves(grooves, output = undefined) {
if (grooves.length === 0)
return;
this.WriteAllObjectRecord();
this.grooves.push(...grooves);
this.GrooveCheckAllAutoSplit(output);
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform, frustum) {
switch (snapMode) {
case ObjectSnapMode.End:
return this.GetStretchPoints();
case ObjectSnapMode.Mid:
case ObjectSnapMode.Cen:
case ObjectSnapMode.Nea:
case ObjectSnapMode.Ext:
case ObjectSnapMode.Per:
case ObjectSnapMode.Tan:
{
let contour = this.ContourCurve.Clone();
contour.ApplyMatrix(this.OCSNoClone);
let pts = contour.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform);
contour.Position = contour.Position.add(this.Normal.multiplyScalar(this.thickness));
pts.push(...contour.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform));
let ps = this.contourCurve.GetStretchPoints();
for (let p of ps) {
let l = new exports.Line(p, p.clone().setZ(this.thickness));
l.ApplyMatrix(this.OCSNoClone);
pts.push(...l.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform));
}
if (this.grooves.length < 100)
for (let g of this.grooves) {
if (!frustum || frustum.intersectsBox(g.BoundingBox))
pts.push(...g.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform, frustum));
}
return pts;
}
}
return [];
}
//#region Stretch
GetStrectchPointCountList(dragType) {
let counts = [this.ContourCurve.GetDragPointCount(dragType) * 2];
for (let g of this.grooves) {
let c = g.ContourCurve.GetDragPointCount(dragType) * 2;
for (let g1 of g.grooves)
c += g1.contourCurve.GetDragPointCount(dragType) * 2;
counts.push(c);
}
return counts;
}
GetGripOrStretchPoints(dragType) {
let isGrip = dragType === DragPointType.Grip;
let pts = isGrip ? this.ContourCurve.GetGripPoints() : this.ContourCurve.GetStretchPoints();
let v = new three.Vector3(0, 0, this.thickness);
pts.push(...pts.map(p => p.clone().add(v)));
pts.forEach(p => { p.applyMatrix4(this.OCSNoClone); });
for (let g of this.grooves) {
let gpts = g.GetGripOrStretchPoints(dragType);
pts.push(...gpts);
}
return pts;
}
MoveGripOrStretchPoints(indexList, vec, dragType) {
this.WriteAllObjectRecord();
let counts = this.GetStrectchPointCountList(dragType);
if (dragType === DragPointType.Stretch && indexList.length === arraySum(counts)) {
this.Position = this.Position.add(vec);
return;
}
arraySortByNumber$1(indexList);
let updateBak = this.AutoUpdate;
this.AutoUpdate = false;
if (this.grooves.length === 0) {
this.MoveGripOrStretchPointsOnly(indexList, vec, dragType);
}
else {
let i = 0;
let icount = indexList.length;
let offset = 0;
let grooveIndex = -1;
for (let count of counts) {
offset += count;
let ilist = [];
for (; i < icount; i++) {
if (indexList[i] < offset)
ilist.push(indexList[i] - offset + count);
else
break;
}
if (ilist.length > 0) {
if (grooveIndex === -1)
this.MoveGripOrStretchPointsOnly(ilist, vec, dragType);
else
this.grooves[grooveIndex].MoveGripOrStretchPoints(ilist, vec, dragType);
}
grooveIndex++;
}
}
if (this.objectId) {
this.CheckContourCurve();
let splitEntitys = [];
this.GrooveCheckAll(splitEntitys);
if (splitEntitys.length > 0 && this.Owner) {
let ms = this.Owner.Object;
for (let e of splitEntitys)
ms.Append(e);
}
}
this.AutoUpdate = updateBak;
this.Update();
}
GetGripPoints() {
return this.GetGripOrStretchPoints(DragPointType.Grip);
}
MoveGripPoints(indexList, vec) {
this.MoveGripOrStretchPoints(indexList, vec, DragPointType.Grip);
}
GetStretchPoints() {
return this.GetGripOrStretchPoints(DragPointType.Stretch);
}
MoveStretchPoints(indexList, vec) {
this.MoveGripOrStretchPoints(indexList, vec, DragPointType.Stretch);
}
/**
* 只对自身的轮廓和厚度进行拉伸,忽略子实体
*/
MoveGripOrStretchPointsOnly(indexList, vec, dragType) {
let stretchCount = this.ContourCurve.GetDragPointCount(dragType);
if (dragType === DragPointType.Stretch) {
//Move
if (indexList.length === stretchCount * 2) {
this.Position = this.Position.add(vec);
return;
}
//判断是否拉伸厚度
if (this.IsStretchThickness(indexList)) {
let isFront = indexList[0] < stretchCount;
if (indexList.every(v => v < stretchCount === isFront)) {
//Change thickness
let lvec = vec.clone().applyMatrix4(this.OCSInv.setPosition(ZeroVec));
if (isFront) {
// if (lvec.z >= this.thickness) return;
this.thickness -= lvec.z;
//移动位置而不改变内部拉槽
let v = this.Normal.multiplyScalar(lvec.z);
this._Matrix.elements[12] += v.x;
this._Matrix.elements[13] += v.y;
this._Matrix.elements[14] += v.z;
}
else {
// if (-lvec.z > this.thickness) return;
this.thickness += lvec.z;
}
return;
}
}
indexList = arrayClone(indexList);
}
//修正点的索引
for (let i = 0; i < indexList.length; i++) {
let index = indexList[i];
if (index >= stretchCount) {
index -= stretchCount;
indexList[i] = index;
}
}
indexList = [...new Set(indexList)];
let localVec = vec.clone().applyMatrix4(this.OCSInv.setPosition(ZeroVec));
if (dragType === DragPointType.Grip) {
if (this.ContourCurve instanceof exports.Polyline
&& indexList.length === 1
&& indexList[0] % 2 === 1) {
let param = indexList[0] / 2;
if (this.ContourCurve.GetBulgeAt(Math.floor(param)) === 0) {
let der = this.ContourCurve.GetFirstDeriv(param).normalize();
[der.x, der.y] = [der.y, -der.x];
let d = localVec.dot(der);
localVec.copy(der).multiplyScalar(d);
}
}
this.ContourCurve.MoveGripPoints(indexList, localVec);
}
else
this.ContourCurve.MoveStretchPoints(indexList, localVec);
}
IsStretchThickness(indexs) {
let count = this.ContourCurve.GetStretchPoints().length;
if (indexs.length === count) {
let isF = indexs[0] < count;
return indexs.every(i => isF === (i < count));
}
return false;
}
//#endregion
//#region groove(凹槽(造型))操作
/*
添加凹槽一般会经过以下几个步骤
1.凹槽转换成和本实体法线平行的凹槽实体
->调用第二步
2.校验凹槽的Z轴位置是否存在交集
->{不存在Z轴交集} 或 {交集异常(凹槽被本实体包含不见光)} 的凹槽被移除
->凹槽的Z轴位置和厚度被更新
3.凹槽合并
4.修正轮廓.
->{本实体}轮廓被修正(当实体被凹槽破坏形状时)
-->修正成功后,凹槽被移除
-->本实体可能分裂
->{凹槽}轮廓被修正({凹槽的轮廓}被约束在{实体轮廓}内部)
-->凹槽可能分裂
*/
/**
* (步骤2)
* 更新凹槽位置和厚度(校验凹槽的Z轴位置是否存在交集)
*/
GrooveCheckPosition(target) {
if (target.Width < 1e-1 || target.Height < 1e-1 || target.Thickness < 1e-1)
return exports.Status.False;
let tp = target.Position.applyMatrix4(this.OCSInv);
let minZ = tp.z;
let maxZ = tp.z + target.thickness;
if (minZ <= 1e-2) //背面
{
target.Thickness = Math.min(maxZ, this.thickness);
if (!(equaln$1(minZ, 0)))
target.ApplyMatrix(MoveMatrix(this.Normal.multiplyScalar(-minZ)));
}
else if (maxZ >= (this.thickness - 1e-2) && minZ > 0) //正面
target.Thickness = this.thickness - minZ;
else
return exports.Status.False;
if (equaln$1(target.thickness, this.thickness, 1e-2))
target.thickness = this.thickness;
return target.thickness > 1e-2 ? exports.Status.True : exports.Status.False;
}
/**
* (步骤3)
* 计算凹槽合并
*/
GrooveCheckMerge(checkGrooveContourCurve = true) {
if (this.grooves.length < 2)
return;
//构建二维空间索引
let ocsInv = this.OCSInv;
let mtx = new three.Matrix4;
let fb = new Flatbush__default["default"](this.grooves.length);
for (let i = 0; i < this.grooves.length; i++) {
let g = this.grooves[i];
mtx.multiplyMatrices(ocsInv, g.OCSNoClone);
let cu = g.ContourCurve.Clone().ApplyMatrix(mtx);
let box = cu.BoundingBox;
g.TempData = { index: i, used: false, box };
fb.add(box.min.x, box.min.y, box.max.x, box.max.y);
}
fb.finish();
let retGs = []; //新的槽列表
for (let i = 0; i < this.grooves.length; i++) {
let startG = this.grooves[i];
if (startG.TempData === undefined) //已经被使用
continue;
retGs.push(startG);
let stack = [startG];
for (let j = 0; j < stack.length; j++) {
let g = stack[j];
let gd = g.TempData; //能入栈的都是未被使用的
let ids = fb.search(gd.box.min.x - 1e-2, gd.box.min.y - 1e-2, gd.box.max.x + 1e-2, gd.box.max.y + 1e-2, (id => {
if (id <= i)
return false; //(id比它小(如果能合并 早就合并了))
let gd = this.grooves[id].TempData;
return gd && !gd.used; //判断被使用
}));
for (let id of ids) {
let ng = this.grooves[id];
if (equaln$1(startG.knifeRadius, ng.knifeRadius, 1e-3) && startG.Join(ng) === exports.Status.True) {
ng.TempData.used = true;
stack.push(ng);
}
}
g.TempData = undefined; //总是保证被使用的造型这个数据为空
}
}
if (checkGrooveContourCurve && retGs.length !== this.grooves.length) {
this.grooves = retGs;
for (let g of this.grooves)
g.CheckContourCurve();
}
}
/**
* (步骤4.1)
* 计算凹槽轮廓在本实体中的约束状态(可能分裂)
* @param groove 凹槽(不拷贝,直接被修改)
* @returns this[] 凹槽在本实体中正确的约束状态.(可能分裂成为多个)
*/
GrooveCheckContour(groove) {
let matrixToTarget = groove.OCSInv.multiply(this.OCS);
matrixToTarget.elements[14] = 0; //z->0
//理论上只有大洞才需要优化,小洞无所谓了
let thisShape = this.GetShape(true).ApplyMatrix(matrixToTarget);
let targetShape = new Shape(Contour.CreateContour([groove.ContourCurve.Clone()], false));
let inters = thisShape.IntersectionBoolOperation(targetShape);
if (inters.length === 1) {
groove.ContourCurve = inters[0].Outline.Curve;
let grooves = [groove];
groove.GrooveCheckAll(grooves);
return grooves;
}
else {
let grooves = [];
for (let contour of inters) {
let ext = groove.Clone().ClearDraw();
ext.ContourCurve = contour.Outline.Curve;
ext.GrooveCheckAll(grooves);
grooves.push(ext);
}
return grooves;
}
}
/**
* (步骤4.2)
* 计算本实体被全身的凹槽差集后正确的实体轮廓,和有可能的分裂实体
* @param splitEntitys 分裂出来的实体
* @returns [Status] Status : 消失不见
*/
ContourCheckSubtract(splitEntitys) {
let shapeManager = new ShapeManager();
shapeManager.AppendShapeList(new Shape(Contour.CreateContour(this.ContourCurve.Clone(), false)));
let subtractShape = new ShapeManager();
let allDepthGrooves = []; //全深槽
arrayRemoveIf(this.grooves, groove => {
if (equaln$1(groove.thickness, this.thickness)) {
let grooveCurve = groove.ContourCurve.Clone();
let matrixToLocal = this.OCSInv.multiply(groove.OCS);
grooveCurve.ApplyMatrix(matrixToLocal);
subtractShape.AppendShapeList(new Shape(Contour.CreateContour([grooveCurve], false)));
allDepthGrooves.push(groove);
return true;
}
return false;
});
shapeManager.SubstactBoolOperation(subtractShape);
let shapes = shapeManager.ShapeList;
//不做任何改变
if (shapeManager.ShapeCount === 1 && shapes[0].Holes.length === allDepthGrooves.length) {
//在拉伸夹点后,全深槽如果不改原始的板的信息,那么它无法在递归检查,这个时候如果不校验,那么它将不会在被校验
for (let g of allDepthGrooves)
g.CheckContourCurve();
this.grooves.push(...allDepthGrooves);
return true;
}
//分裂
for (let i = 1; i < shapeManager.ShapeCount; i++) {
let ext = this.Clone();
let shape = shapes[i];
for (let hole of shape.Holes) {
let groove = new ExtrudeSolid_1();
//#endregion 保持旧的槽数据 (必须在这里写代码 因为下面会修改hole.Curve)
let p = hole.Curve.StartPoint;
let oldGrooveIndex = subtractShape.ShapeList.findIndex(s => s.Outline.Curve.PtOnCurve(p));
let oldGroove = allDepthGrooves[Math.max(oldGrooveIndex, 0)];
groove.knifeRadius = oldGroove.knifeRadius;
groove.groovesAddWidth = oldGroove.groovesAddWidth;
groove.groovesAddDepth = oldGroove.groovesAddDepth;
groove.groovesAddLength = oldGroove.groovesAddLength;
//#endregion
groove.OCS = this.OCSNoClone;
groove.ContourCurve = hole.Curve;
groove.thickness = this.thickness;
ext.grooves.push(groove);
}
ext.ContourCurve = shape.Outline.Curve;
ext.GrooveCheckAll(splitEntitys);
ext.Update();
splitEntitys.push(ext);
}
if (shapes.length > 0) {
let shape = shapes[0];
for (let hole of shape.Holes) {
let groove = new ExtrudeSolid_1();
//#endregion 保持旧的槽数据 (必须在这里写代码 因为下面会修改hole.Curve)
let p = hole.Curve.StartPoint;
let oldGrooveIndex = subtractShape.ShapeList.findIndex(s => s.Outline.Curve.PtOnCurve(p));
let oldGroove = allDepthGrooves[Math.max(oldGrooveIndex, 0)];
groove.knifeRadius = oldGroove.knifeRadius;
groove.groovesAddWidth = oldGroove.groovesAddWidth;
groove.groovesAddDepth = oldGroove.groovesAddDepth;
groove.groovesAddLength = oldGroove.groovesAddLength;
//#endregion
groove.OCS = this.OCSNoClone;
groove.ContourCurve = hole.Curve;
groove.thickness = this.thickness;
this.grooves.push(groove);
}
if (!equaln$1(this.contourCurve.Area, shape.Outline.Area))
this.ContourCurve = shape.Outline.Curve;
return true;
}
else
return false;
}
/**
* 无法知道修改了轮廓是否为更新到内部凹槽.
* 无法知道修改了内部凹槽之后是否会更新到轮廓.
* 所以默认全部校验内部的凹槽
*/
GrooveCheckAll(splitEntitys) {
if (this.IsLazyGrooveCheck) {
this.IsNeedGrooveCheck = true;
return;
}
this.IsNeedGrooveCheck = false;
this.WriteAllObjectRecord();
//校验Z轴位置
arrayRemoveIf(this.grooves, g => {
return this.GrooveCheckPosition(g) === exports.Status.False;
});
//清除全深洞的子槽
for (let g of this.grooves) {
if (equaln$1(g.thickness, this.thickness, 1e-3)) {
/*
此刻我们直接将它的子槽清空,虽然子槽可能将这个槽分裂成2个,
但是这样的情况只能在造型应用中才会产生
*/
g.grooves.length = 0;
}
else
arrayRemoveIf(g.grooves, subg => !equaln$1(g.thickness, subg.thickness, 1e-3));
}
//合并
this.GrooveCheckMerge();
//修改本实体轮廓
if (this.grooves.some(g => equaln$1(g.thickness, this.thickness, 1e-3))) {
if (!this.ContourCheckSubtract(splitEntitys)) {
this.Erase();
return;
}
}
//修正凹槽轮廓
let splitGrooves = [];
let thisArea = this.contourCurve.Area;
for (let i = 0; i < this.grooves.length; i++) {
let g = this.grooves[i];
if (equaln$1(g.thickness, this.thickness, 1e-3))
splitGrooves.push(g);
else {
let gs = this.GrooveCheckContour(g);
if (gs.length === 1) {
let gg = gs[0];
if (gg.grooves.length === 0 && equaln$1(gg.contourCurve.Area, thisArea)) {
//判断正反面
let p = gg.Position.applyMatrix4(this.OCSInv);
if (equaln$1(p.z, 0)) {
this.thickness -= gg.thickness;
let n = this.Normal;
n.multiplyScalar(gg.thickness);
this._Matrix.elements[12] += n.x;
this._Matrix.elements[13] += n.y;
this._Matrix.elements[14] += n.z;
}
else {
this.thickness -= gg.thickness;
}
this.grooves.splice(i, 1);
this.GrooveCheckAll(splitEntitys);
return;
}
}
splitGrooves.push(...gs);
}
}
this.grooves = splitGrooves;
this.Update();
}
/** 校验内部槽并且自动分裂 */
GrooveCheckAllAutoSplit(output = undefined) {
let splitEntitys = [];
this.GrooveCheckAll(splitEntitys);
if (output)
output.push(...splitEntitys);
else if (this._Owner) {
let record = this._Owner.Object;
for (let e of splitEntitys) {
record.Add(e);
e.RepairRelevance();
}
this.HandleSpliteEntitys(splitEntitys);
}
}
//分裂后重新将排钻实体设置给不同的实体
HandleSpliteEntitys(splitEntitys) { }
LazyGrooveCheckAll() {
this.IsLazyGrooveCheck = false;
if (this.IsNeedGrooveCheck)
this.GrooveCheckAllAutoSplit();
}
//#endregion
//#region Draw
GetPrintObject3D() {
let geometry = new LineGeometry.LineGeometry();
let lineSegments = new Float32Array(this.EdgeGeometry.attributes.position.array);
let instanceBuffer = new three.InstancedInterleavedBuffer(lineSegments, 6, 1);
geometry.setAttribute('instanceStart', new three.InterleavedBufferAttribute(instanceBuffer, 3, 0));
geometry.setAttribute('instanceEnd', new three.InterleavedBufferAttribute(instanceBuffer, 3, 3));
let line = new Line2.Line2(geometry, ColorMaterial.PrintLineMatrial);
let mesh = new three.Mesh(this.MeshGeometry, ColorMaterial.GetPrintConceptualMaterial());
return [line, mesh];
}
InitDrawObject(renderType = RenderType.Wireframe) {
if (renderType === RenderType.Wireframe) {
return new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(this.ColorIndex, this.Freeze));
}
else if (renderType === RenderType.Conceptual) {
return new three.Object3D().add(new three.Mesh(this.MeshGeometry, ColorMaterial.GetConceptualMaterial(this.ColorIndex, three.FrontSide, true, this.Freeze)), new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetConceptualEdgeMaterial()));
}
else if (renderType === RenderType.Physical) {
let mesh = new three.Mesh(this.MeshGeometry, this.MeshMaterial);
Object.defineProperty(mesh, "castShadow", {
get: () => this.CaseShadow
});
Object.defineProperty(mesh, "receiveShadow", {
get: () => this.CaseShadow
});
return mesh;
}
else if (renderType === RenderType.Jig) {
return new three.Object3D().add(...FastWireframe(this));
}
else if (renderType === RenderType.Print) {
return new three.Object3D().add(...this.GetPrintObject3D());
}
else if (renderType === RenderType.Physical2) {
let mesh = new three.Mesh(this.MeshGeometry, this.MeshMaterial);
Object.defineProperty(mesh, "castShadow", {
get: () => this.CaseShadow
});
Object.defineProperty(mesh, "receiveShadow", {
get: () => this.CaseShadow
});
return new three.Object3D().add(mesh, new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetPhysical2EdgeMaterial()));
}
}
get UCGenerator() {
return boardUVGenerator;
}
get NeedUpdateRelevanceGroove() {
//在同步反应器中,当存在关联拉槽的实体和本实体一起被删除后,会更新本实体,然后导致同步认为它没有子实体
if (this._isErase)
return false;
if (!this.__CacheKnifVersion__)
return true;
for (let k of this.RelevanceKnifs) {
if (!k || !k.Object)
continue;
if (this.__CacheKnifVersion__[k.Index] !== (k.Object).__UpdateVersion__)
return true;
}
return false;
}
/**
* 计算关联拉槽,更新绘制对象(MeshGeometry和EdgeGeometry)
*/
CalcRelevanceGroove() {
//避免Jig实体更新,导致性能暴跌.
if (!this.Id)
return;
this.__CacheKnifVersion__ = {};
let knifs = [];
this.GetRelevanceKnifes(knifs);
if (knifs.length > 0) {
for (let k of knifs) //复合实体(五金)的子实体没有id
this.__CacheKnifVersion__[k.Id?.Index ?? k.__TempIndexVersion__?.Index] = k.__TempIndexVersion__?.Version ?? k.__UpdateVersion__;
let tempExtrude = this.Clone();
tempExtrude.RelevanceKnifs.length = 0; //避免递归
if (!this.ReadFileIng)
tempExtrude.__OriginalId__ = this.Id; //在读取文件时不打印日志
let output = [tempExtrude];
let ok = tempExtrude.Subtract(knifs, output);
this.__CacheSplitExtrudes = output;
if (ok) {
this.__CacheVolume__ = tempExtrude.Volume;
let meshs = [];
let edges = [];
let inv = this.OCSInv;
let diff = new three.Matrix4;
for (let e2 of output) {
diff.multiplyMatrices(inv, e2._Matrix);
meshs.push(e2.MeshGeometry.applyMatrix4(diff));
edges.push(e2.EdgeGeometry.applyMatrix4(diff));
this.__CacheVolume__ += e2.Volume;
}
if (output.length === 1) {
this._MeshGeometry = tempExtrude.MeshGeometry;
this._EdgeGeometry = tempExtrude.EdgeGeometry;
}
else {
this._MeshGeometry = BufferGeometryUtils.MergeBufferGeometries(meshs);
this._MeshGeometry["IsMesh"] = true;
this._EdgeGeometry = BufferGeometryUtils.MergeBufferGeometries(edges);
}
//因为这里更新了mesh geom 所以我们必须更新它.
this._MeshGeometry = this.UpdateMeshGeom(this._MeshGeometry);
this._MeshGeometry["IsMesh"] = true;
//我们加入一些拓展信息,以便排钻能够使用(或者其他的,比如发送到效果图?,BBS)(布局视口会直接添加实体到场景,所以我们只在这里设置OriginEntity)
for (let i = 0; i < this.__CacheSplitExtrudes.length; i++) {
this.__CacheSplitExtrudes[i].objectId = new ObjectId(this.Id.Index * -100 - i);
this.__CacheSplitExtrudes[i].__OriginalEnt__ = this;
}
}
else {
let id = this.Id ?? this.__OriginalId__;
if (!this.ReadFileIng &&
id &&
this instanceof Board &&
this.__CacheVolume__ !== undefined &&
!equaln$1(this.__CacheVolume__, this.Volume))
InteractionLog([{ msg: `${this.Name}(${id.Index})`, entity: [this] }, { msg: "关联槽已逃离!" }], LogType.Warning);
this.__CacheVolume__ = undefined;
this.__CacheSplitExtrudes = [this];
}
}
else {
if (!this.ReadFileIng &&
this.Id &&
this instanceof Board &&
this.__CacheVolume__ !== undefined &&
!equaln$1(this.__CacheVolume__, this.Volume))
InteractionLog([{ msg: `${this.Name}(${this.Id.Index})`, entity: [this] }, { msg: "关联槽已逃离或者被清除!" }], LogType.Warning);
this.__CacheSplitExtrudes = [this];
this.__CacheVolume__ = undefined;
}
}
/**
* 如果实体被切割,那么将返回分裂的实体数组,否则返回自身
*/
get SplitExtrudes() {
if (this.NeedUpdateRelevanceGroove)
this.Update(exports.UpdateDraw.Geometry); //我们先直接更新绘制
if (this.NeedUpdateRelevanceGroove) //如果更新失败,那么我们更新这个槽(似乎也证明了我们没有绘制实体)
this.CalcRelevanceGroove(); //注意,这也将更新绘制的实体(EdgeGeo,MeshGeo)(如果拆单也用这个,可能会带来性能损耗)
if (this.__CacheSplitExtrudes)
for (let e of this.__CacheSplitExtrudes)
e._MaterialId = this._MaterialId;
return this.__CacheSplitExtrudes;
}
GetRelevanceKnifes(knifs) {
for (let e of this.RelevanceKnifs) {
if (!e.IsErase)
knifs.push(e.Object);
else if (this.__CacheKnifVersion__)
this.__CacheKnifVersion__[e.Index] = e?.Object?.__UpdateVersion__;
}
}
ClearRelevance(en) {
if (en) {
let oldLen = this.RelevanceKnifs.length;
arrayRemoveIf(this.RelevanceKnifs, id => !id?.Object || id.Index === en.Id.Index);
if (this.RelevanceKnifs.length !== oldLen)
arrayRemoveIf(en.RelevanceMeats, id => !id?.Object || id.Index === this.Id.Index);
oldLen = this.RelevanceMeats.length;
arrayRemoveIf(this.RelevanceMeats, id => !id?.Object || id.Index === en.Id.Index);
if (oldLen !== this.RelevanceMeats.length)
arrayRemoveIf(en.RelevanceKnifs, id => !id?.Object || id.Index === this.Id.Index);
}
else {
for (let id of this.RelevanceKnifs) {
let en = id.Object;
if (en)
arrayRemoveIf(en.RelevanceMeats, i => !i?.Object || i.Index === this.Id.Index);
}
for (let id of this.RelevanceMeats) {
let en = id.Object;
if (en) {
arrayRemoveIf(en.RelevanceKnifs, i => !i?.Object || i.Index === this.Id.Index);
en.Update();
}
}
this.RelevanceMeats.length = 0;
this.RelevanceKnifs.length = 0;
}
this.Update();
}
get MeshGeometry() {
if (this._MeshGeometry)
return this._MeshGeometry;
if (this.thickness <= 0)
return new three.BufferGeometry();
this.CalcRelevanceGroove();
if (this._MeshGeometry)
return this._MeshGeometry;
this._EdgeGeometry = undefined; //清理掉(以便在被csg切割后得到edgegeom)
let grooves = this.Grooves;
if (grooves.every(g => equaln$1(g.thickness, this.thickness)) || grooves.length === 0) {
let contour = this.ContourCurve.Clone();
let holes = [];
let ocsInv = this.OCSInv;
let alMatrix4 = new three.Matrix4();
if (grooves.length < MaxDrawGrooveCount)
for (let g of grooves) {
alMatrix4.multiplyMatrices(ocsInv, g.OCSNoClone);
let gContour = g.ContourCurve.Clone();
gContour.ApplyMatrix(alMatrix4);
holes.push(Contour.CreateContour(gContour));
}
let shape = new Shape(Contour.CreateContour(contour), holes);
let extrudeSettings = {
steps: 1,
UVGenerator: this.UCGenerator,
depth: this.bevelEnabled ? this.thickness - 4 : this.thickness,
bevelEnabled: this.bevelEnabled,
bevelThickness: 2,
bevelSize: 2,
bevelOffset: -2,
bevelSegments: 1,
};
let geo = new three.ExtrudeGeometry(shape.Shape, extrudeSettings);
geo.applyMatrix4(contour.OCSNoClone);
if (this.bevelEnabled)
geo.translate(0, 0, 2);
this.UpdateUV(geo, contour.OCSNoClone);
//板在某些时候需要布尔运算(二维刀路)
this._MeshGeometry = this.UpdateMeshGeom(geo);
this._MeshGeometry["IsMesh"] = true;
this.GenWorldUV(this._MeshGeometry);
//edge geometry
if (grooves.length < MaxDrawGrooveCount && !this._EdgeGeometry) //这个代码保证线框和概念对齐
{
let coords = FastExtrudeEdgeGeometryOfShape(shape.Shape, 0, this.thickness, 12, true);
let edgeGeo = new three.BufferGeometry();
edgeGeo.setAttribute('position', new three.Float32BufferAttribute(coords, 3));
edgeGeo.applyMatrix4(contour.OCSNoClone);
this._EdgeGeometry = edgeGeo;
}
return this._MeshGeometry;
}
let builder = new ExtrudeGeometryBuilder(this);
this._MeshGeometry = builder.MeshGeometry;
this._MeshGeometry = this.UpdateMeshGeom(this._MeshGeometry);
if (grooves.length < MaxDrawGrooveCount && !this._EdgeGeometry)
this._EdgeGeometry = builder.EdgeGeometry;
this.UpdateUV(null, null);
this.GenWorldUV(this._MeshGeometry);
return this._MeshGeometry;
}
//子类重载,用于二次计算几何体(例如二维刀路布尔运算)
UpdateMeshGeom(geo) {
return geo;
}
get Has2DPath() { return false; }
get EdgeGeometry() {
if (this._EdgeGeometry)
return this._EdgeGeometry;
this.CalcRelevanceGroove();
if (this._EdgeGeometry)
return this._EdgeGeometry;
//这里我们超过100就用这个,为了性能 和MaxDrawGrooveCount不一致
if (this.grooves.length > 100 || (!this.Has2DPath && (this.grooves.length === 0 || this.grooves.every(g => equaln$1(g.thickness, this.thickness))))) {
let coords = FastExtrudeEdgeGeometry(this, this.ColorIndex, 12, true);
let edgeGeo = new three.BufferGeometry();
edgeGeo.setAttribute('position', new three.Float32BufferAttribute(coords, 3));
this._EdgeGeometry = edgeGeo;
return this._EdgeGeometry;
}
if (this._MeshGeometry) {
this._MeshGeometry.dispose();
this._MeshGeometry = undefined;
}
this.MeshGeometry;
return this._EdgeGeometry;
}
UpdateUV(geo, ocs, isRev = false) {
let mtl = this.Material?.Object ?? this.Db?.DefaultMaterial;
if (mtl?.IsFull) {
if (geo)
ScaleUV2(geo, ocs, this.width, this.height, isRev);
else
this.UpdateBufferGeometryUvs(isRev);
}
else {
if (geo)
ScaleUV(geo);
}
}
GenWorldUV(geo) {
let mtl = this.Material?.Object ?? this.Db?.DefaultMaterial;
if (mtl?.UseWorldUV) {
let gen = new GenUVForWorld;
gen.GenGeoUV(geo, mtl.Material);
}
}
UpdateBufferGeometryUvs(isRev) {
let uvs = this._MeshGeometry.attributes.uv;
for (let i = 0; i < uvs.count; i++) {
let x = uvs.getX(i) * 1e3;
let y = uvs.getY(i) * 1e3;
if (isRev)
uvs.setXY(i, x / this.height, y / this.width);
else
uvs.setXY(i, x / this.width, y / this.height);
}
}
DeferUpdate() {
if (this.NeedUpdateFlag & exports.UpdateDraw.Matrix) {
//如果是Jig实体,那么就算它有关联切割,我们也不更新实体(因为似乎没必要?)
if (this.Id && this.RelevanceKnifs.some(id => !id.IsErase))
this.NeedUpdateFlag |= exports.UpdateDraw.Geometry;
}
super.DeferUpdate();
}
ClearDraw() {
this.UpdateDrawGeometry();
return super.ClearDraw();
}
UpdateDrawObject(renderType, obj) {
DisposeThreeObj(obj);
Object3DRemoveAll(obj);
if (renderType === RenderType.Wireframe) {
let l = obj;
l.geometry = this.EdgeGeometry;
l.material = ColorMaterial.GetLineMaterial(this.ColorIndex, this.Freeze);
}
else if (renderType === RenderType.Conceptual) {
return obj.add(new three.Mesh(this.MeshGeometry, ColorMaterial.GetConceptualMaterial(this.ColorIndex, three.FrontSide, true, this.Freeze)), new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetConceptualEdgeMaterial()));
}
else if (renderType === RenderType.ModelGroove) {
obj.add(
// new Mesh(this.MeshGeometry, ColorMaterial.GetConceptualMaterial(9, FrontSide, true, this.Freeze)),
new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetConceptualEdgeMaterial()));
obj.add(this.GetModelGroove());
}
else if (renderType === RenderType.Physical) {
let mesh = obj;
mesh.geometry = this.MeshGeometry;
mesh.material = this.MeshMaterial;
}
else if (renderType === RenderType.Jig) {
obj.add(...FastWireframe(this));
}
else if (renderType === RenderType.Print) {
return obj.add(...this.GetPrintObject3D());
}
else if (renderType === RenderType.Physical2) {
let mesh = new three.Mesh(this.MeshGeometry, this.MeshMaterial);
Object.defineProperty(mesh, "castShadow", {
get: () => this.CaseShadow
});
Object.defineProperty(mesh, "receiveShadow", {
get: () => this.CaseShadow
});
return obj.add(mesh, new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetPhysical2EdgeMaterial()));
}
}
UpdateDrawObjectMaterial(renderType, obj) {
if (renderType === RenderType.Wireframe) {
let l = obj;
l.material = ColorMaterial.GetLineMaterial(this.ColorIndex, this.Freeze);
}
else if (renderType === RenderType.Conceptual) {
let mesh = obj.children[0];
mesh.material = ColorMaterial.GetConceptualMaterial(this.ColorIndex, three.FrontSide, true, this.Freeze);
}
else if (renderType === RenderType.Physical2) {
let mesh = obj.children[0];
mesh.material = this.MeshMaterial;
}
else {
let mesh = obj;
mesh.material = this.MeshMaterial;
}
}
GetModelGroove() {
const grooves = [];
for (const sp of this.__CacheSplitExtrudes ?? []) {
grooves.push(...sp.grooves);
}
const group = new three.Group();
for (const groove of grooves) {
const grooveClone = groove.Clone();
grooveClone.UpdateDrawGeometry();
const mtx = new three.Matrix4().premultiply(grooveClone.OCSNoClone).premultiply(this.OCSInv);
const edgeGeo = grooveClone.EdgeGeometry;
edgeGeo.applyMatrix4(mtx);
const line = new three.LineSegments(edgeGeo, ColorMaterial.GetLineMaterial(1, this.Freeze));
const meshGeo = grooveClone.MeshGeometry;
meshGeo.applyMatrix4(mtx);
const mesh = new three.Mesh(meshGeo, ColorMaterial.GetConceptualMaterial(1, three.FrontSide, true, this.Freeze, 0.6));
group.add(mesh, line);
}
return group;
}
UpdateJigMaterial(color = 8) {
}
//#endregion
//#region -------------------------File-------------------------
/**
* 简化的文件读取和写入,只写入必要的数据,没有id,没有其他版本号
*/
ReadFileLite(file) {
this.ReadFileOnly(file);
this._Matrix.fromArray(file.Read());
}
WriteFileLite(file) {
this.WriteFileOnly(file);
file.Write(this._Matrix.toArray());
}
ReadFileOnly(file) {
let ver = file.Read();
this.height = Number(file.Read());
this.width = Number(file.Read());
this.thickness = Number(file.Read());
this.isRect = file.Read();
this.contourCurve = file.ReadObject();
let grooveCount = file.Read();
this.grooves.length = 0;
for (let i = 0; i < grooveCount; i++) {
if (this.grooves[i] === undefined)
this.grooves[i] = new ExtrudeSolid_1();
this.grooves[i].ReadFileLite(file);
}
this.knifeRadius = file.Read();
this.groovesAddLength = file.Read();
if (ver > 1) {
this.groovesAddWidth = file.Read();
this.groovesAddDepth = file.Read();
}
if (ver > 2) {
this.RelevanceMeats.length = 0;
this.RelevanceKnifs.length = 0;
let count = file.Read();
for (let index = 0; index < count; index++) {
let id = file.ReadSoftObjectId();
if (id)
this.RelevanceMeats.push(id);
}
count = file.Read();
for (let index = 0; index < count; index++) {
let id = file.ReadSoftObjectId();
if (id)
this.RelevanceKnifs.push(id);
}
}
}
WriteFileOnly(file) {
file.Write(3);
file.Write(this.height);
file.Write(this.width);
file.Write(this.thickness);
file.Write(this.isRect);
file.WriteObject(this.ContourCurve);
file.Write(this.grooves.length);
for (let groove of this.grooves)
groove.WriteFileLite(file);
file.Write(this.knifeRadius);
file.Write(this.groovesAddLength);
file.Write(this.groovesAddWidth);
file.Write(this.groovesAddDepth);
//3
file.Write(this.RelevanceMeats.length);
for (let id of this.RelevanceMeats)
file.WriteSoftObjectId(id);
file.Write(this.RelevanceKnifs.length);
for (let id of this.RelevanceKnifs)
file.WriteSoftObjectId(id);
}
//对象从文件中读取数据,初始化自身
_ReadFile(file) {
super._ReadFile(file);
this.ReadFileOnly(file);
}
//对象将自身数据写入到文件.
WriteFile(file) {
super.WriteFile(file);
this.WriteFileOnly(file);
}
};
ExtrudeSolid = ExtrudeSolid_1 = __decorate([
Factory
], ExtrudeSolid);
CADFactory.RegisterObjectAlias(ExtrudeSolid, "ExtureSolid");
//用于翻转绘制出来的槽
new three.Matrix4().makeBasis(YAxis, ZAxis, XAxis);
var CompositeEntity_1;
let CompositeEntity = CompositeEntity_1 = class CompositeEntity extends Entity {
constructor() {
super();
//如果你需要修改内部实体,则需要写入记录
this.Entitys = [];
}
/**
* 返回对象在自身坐标系下的Box
*/
get BoundingBoxInOCS() {
//这个代码可能是错误的. 当复合实体是子实体时,复合实体的矩阵和世界坐标垂直,但是父实体没有垂直时,此时的结果是错误的 所以注释掉这个代码
// if (
// (equaln(this._Matrix.elements[0], 1, 1e-5) ||
// equaln(this._Matrix.elements[1], 1, 1e-5) ||
// equaln(this._Matrix.elements[2], 1, 1e-5)
// //3
// )
// &&
// (equaln(this._Matrix.elements[4], 1, 1e-5) ||
// equaln(this._Matrix.elements[5], 1, 1e-5) ||
// equaln(this._Matrix.elements[6], 1, 1e-5)
// //7
// )
// &&
// (equaln(this._Matrix.elements[8], 1, 1e-5) ||
// equaln(this._Matrix.elements[9], 1, 1e-5) ||
// equaln(this._Matrix.elements[10], 1, 1e-5)
// )
// )
// return this.GetBoundingBoxInMtx(this.OCSInv);
// else
{
let box = new Box3Ext;
for (let e of this.Entitys)
box.union(e.BoundingBoxInOCS.applyMatrix4(e.OCSNoClone));
return box;
}
}
//#region 绘制
// OnlyRenderType = true; //我们现在不需要这样,因为我们每个绘制类型的Object的子实体都有子实体的渲染类型(唯一的缺点可能是渲染速度变慢了?)
/**
* 初始化绘制的threejs实体,子类型重载该函数初始化绘制实体.
*/
Explode() {
return this.Entitys.map(e => {
let cloneE = e.Clone();
cloneE.Material = e.Material;
return cloneE.ApplyMatrix(this.OCSNoClone);
});
}
Traverse(callback) {
callback(this);
for (let en of this.Entitys) {
if (en instanceof CompositeEntity_1)
en.Traverse(callback);
else
callback(en);
}
}
//实体在被内嵌时,它绘制对象的世界矩阵会被影响,所以这里我们不直接计算它的盒子,而是用绘制对象的盒子来计算包围盒,避免错误
//例如 复合扫略实体 的ZoomObject在这个实现下是错误的(因为扫略实体也是直接取绘制对象的包围盒)
// get BoundingBox()
// {
// let box = new Box3Ext();
// for (let e of this.Entitys)
// box.union(e.BoundingBox);
// return box.applyMatrix4(this.OCSNoClone);
// }
InitDrawObject(renderType = RenderType.Wireframe) {
/**
* 如果复合实体里面有圆,并且使用了拉伸夹点功能,在UpdateDrawObject时,会因为无法得到Jig对象而导致的错误.
* 索性我们去掉Jig实体的功能.
*/
if (renderType === RenderType.Jig)
return;
let object = new three.Object3D();
this.UpdateDrawObject(renderType, object);
return object;
}
UpdateDrawObject(renderType, obj) {
Object3DRemoveAll(obj);
if (renderType === RenderType.ModelGroove)
return;
for (let e of this.Entitys) {
e.IsEmbedEntity = true;
// //内嵌实体在某些时候可能被清理,修复它
// if (e.DrawObject.children.length === 0)
// e.ClearDraw();
let rtype = renderType;
if (renderType === RenderType.Print && e.IsOnlyRender)
rtype += 100;
let o = e.GetDrawObjectFromRenderType(rtype);
if (o) {
o.traverse(obj => obj.userData = {});
AddEntityDrawObject(obj, e, rtype);
}
}
}
get ColorIndex() {
return super.ColorIndex;
}
set ColorIndex(color) {
if (color !== this._Color) {
this.WriteAllObjectRecord();
this._Color = color;
this.Traverse(e => {
if (e === this)
return;
// if (e instanceof CompositeEntity) //有点奇怪
// e._Color = color;
// else
e.ColorIndex = color;
});
}
}
get Material() {
return super.Material;
}
set Material(id) {
super.Material = id;
for (let e of this.Entitys) {
if (!e.Db)
e.SetDatabase(this.Db);
e.Material = id;
}
}
UpdateDrawObjectMaterial(renderType, obj) {
this.UpdateDrawObject(renderType, obj);
}
RestoreJigMaterial() {
//我们不做任何事情,避免更新材质引起的重绘,因为我们没有实现Jig材质,所以我们也不需要还原它
}
//#endregion
//#region 交互操作
/**
*
* @param snapMode 捕捉模式(单一)
* @param pickPoint const
* @param lastPoint const
* @param viewXform const 最近点捕捉需要这个变量
* @returns object snap points
*/
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
let pts = [];
for (let e of this.Entitys)
arrayPushArray$1(pts, e.Clone().ApplyMatrix(this.OCSNoClone).GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform));
return pts;
}
GetGripPoints() {
return this.GetGripOrStretchPoints(DragPointType.Grip);
}
MoveGripPoints(indexList, vec) {
this.WriteAllObjectRecord();
this.MoveGripOrStretchPoints(indexList, vec, DragPointType.Grip);
}
GetStretchPoints() {
return this.GetGripOrStretchPoints(DragPointType.Stretch);
}
/**
* 拉伸夹点,用于Stretch命令
*
* @param {Array<number>} indexList 拉伸点索引列表.
* @param {Vector3} vec 移动向量
* @memberof Entity
*/
MoveStretchPoints(indexList, vec) {
this.WriteAllObjectRecord();
this.MoveGripOrStretchPoints(indexList, vec, DragPointType.Stretch);
}
GetGripOrStretchPoints(type) {
let pts = [];
for (let e of this.Entitys)
pts.push(...(type === DragPointType.Grip ? e.GetGripPoints() : e.GetStretchPoints()));
for (let p of pts)
p.applyMatrix4(this._Matrix);
return pts;
}
GetStrectchPointCountList(dragType) {
let counts = this.Entitys.map(e => {
return (dragType === DragPointType.Grip ? e.GetGripPoints() : e.GetStretchPoints()).length;
});
return counts;
}
MoveGripOrStretchPoints(indexList, vec, dragType) {
this.WriteAllObjectRecord();
let counts = this.GetStrectchPointCountList(dragType);
if (dragType === DragPointType.Stretch && indexList.length === arraySum(counts)) {
this.Position = this.Position.add(vec);
return;
}
vec = vec.clone().applyMatrix4(this.OCSInv.setPosition(0, 0, 0));
arraySortByNumber$1(indexList);
let i = 0;
let j = 0;
let icount = indexList.length;
let offset = 0;
for (let count of counts) {
offset += count;
let ilist = [];
for (; i < icount; i++) {
if (indexList[i] < offset)
ilist.push(indexList[i] - offset + count);
else
break;
}
let ent = this.Entitys[j];
dragType === DragPointType.Grip ? ent.MoveGripPoints(ilist, vec) : ent.MoveStretchPoints(ilist, vec);
if (ent instanceof ExtrudeSolid) //取消优化判断this.Objectid,因为这个实体可能被复合在另一个实体中,导致这个id是不存在的,所以我们无法判断它在拽拖.
ent.CheckContourCurve();
ent.Update();
j++;
}
this.__UpdateVersion__++;
//如何绘制对象是克隆的,那么我们将重绘它(避免无法更新)
//我们也不大需要下面的判断,我们如果持续的更新它,其实并不会有多大的问题,因为我们总是从缓存里面拿绘制对象
// if (this._drawObject && this._drawObject.children[0]?.userData.IsClone)
this.Update();
}
CloneDrawObject(from) {
for (let [type, obj] of from._CacheDrawObject) {
let oldUserDaata = obj.userData;
obj.userData = {};
let newObj = obj.clone(true);
obj.userData = oldUserDaata;
obj.userData.IsClone = true;
newObj.matrix = this._Matrix;
newObj.userData = { Entity: this };
newObj.userData.IsClone = true;
this._CacheDrawObject.set(type, newObj);
}
this.NeedUpdateFlag = exports.UpdateDraw.None;
}
ApplyMirrorMatrix(m) {
if (this.Id)
this.Update(exports.UpdateDraw.Geometry);
return this;
}
//#endregion
//#region 文件序列化
_ReadFile(file) {
file.Read();
super._ReadFile(file);
let count = file.Read();
this.Entitys.length = 0;
for (let i = 0; i < count; i++) {
let ent = file.ReadObject();
if (ent)
this.Entitys.push(ent);
}
}
//对象将自身数据写入到文件.
WriteFile(file) {
file.Write(1);
super.WriteFile(file);
file.Write(this.Entitys.length);
for (let e of this.Entitys)
file.WriteObject(e);
}
};
__decorate([
AutoRecord
], CompositeEntity.prototype, "Entitys", void 0);
CompositeEntity = CompositeEntity_1 = __decorate([
Factory
], CompositeEntity);
var HardwareCompositeEntity_1;
let HardwareCompositeEntity = HardwareCompositeEntity_1 = class HardwareCompositeEntity extends CompositeEntity {
constructor() {
super(...arguments);
this.HardwareOption = { ...DefaultCompositeMetalsOption };
/**
* 备注
*/
this.DataList = [];
this.RelevanceBoards = [];
this.RelevanceHardware = []; //当这个实体为复合板时,关联五金的信息
this.RelevanceHandle = []; //关联拉手
}
/**
*
* @param [checkIsHole=false] true:只获取是孔的实体 false:返回所有实体
* @param [checkFilter] 过滤函数
* @returns
*/
GetAllEntity(checkIsHole = false, checkFilter) {
let holes = [];
for (let e of this.Entitys) {
if (e instanceof HardwareCompositeEntity_1) {
if (!checkIsHole || e.HardwareOption.isHole)
holes.push(...e.GetAllEntity(checkIsHole, checkFilter).map(h => h.ApplyMatrix(this.OCSNoClone)));
}
else {
if (!checkFilter || checkFilter(e))
holes.push(e.Clone().ApplyMatrix(this.OCSNoClone));
}
}
return holes;
}
ApplyMirrorMatrix(m) {
super.ApplyMirrorMatrix(m);
//"左","右"互换
if (this.HardwareOption.model.includes("左"))
this.HardwareOption.model = this.HardwareOption.model.replace("左", "右");
else if (this.HardwareOption.model.includes("右"))
this.HardwareOption.model = this.HardwareOption.model.replace("右", "左");
return this;
}
_ReadFile(file) {
super._ReadFile(file);
let v = file.Read();
this.HardwareOption.type = file.Read();
this.HardwareOption.isSplite = file.Read();
this.HardwareOption.isSplitePrice = file.Read();
this.HardwareOption.color = file.Read();
this.HardwareOption.material = file.Read();
this.HardwareOption.name = file.Read();
this.HardwareOption.roomName = file.Read();
this.HardwareOption.cabinetName = file.Read();
this.HardwareOption.costExpr = file.Read();
this.HardwareOption.actualExpr = file.Read();
this.HardwareOption.model = file.Read();
this.HardwareOption.factory = file.Read();
this.HardwareOption.brand = file.Read();
this.HardwareOption.spec = file.Read();
this.HardwareOption.count = file.Read();
this.HardwareOption.comments = file.Read();
this.HardwareOption.unit = file.Read();
let count = file.Read();
this.DataList.length = 0;
for (let i = 0; i < count; i++) {
let d = [
file.Read(),
file.Read()
];
this.DataList.push(d);
}
if (v > 1)
this.HardwareOption.isHole = file.Read();
if (v > 2) {
let count = file.Read();
this.RelevanceBoards.length = 0;
for (let i = 0; i < count; i++)
this.RelevanceBoards.push(file.ReadSoftObjectId());
}
if (v > 3) {
let count = file.Read();
this.RelevanceHardware.length = 0;
for (let i = 0; i < count; i++)
this.RelevanceHardware.push(file.ReadSoftObjectId());
}
if (v > 4) {
let count = file.Read();
this.RelevanceHandle.length = 0;
for (let i = 0; i < count; i++) {
let objId = file.ReadObjectId();
if (objId)
this.RelevanceHandle.push(objId);
}
}
}
//对象将自身数据写入到文件.
WriteFile(file) {
super.WriteFile(file);
file.Write(5);
file.Write(this.HardwareOption.type);
file.Write(this.HardwareOption.isSplite);
file.Write(this.HardwareOption.isSplitePrice);
file.Write(this.HardwareOption.color);
file.Write(this.HardwareOption.material);
file.Write(this.HardwareOption.name);
file.Write(this.HardwareOption.roomName);
file.Write(this.HardwareOption.cabinetName);
file.Write(this.HardwareOption.costExpr);
file.Write(this.HardwareOption.actualExpr);
file.Write(this.HardwareOption.model);
file.Write(this.HardwareOption.factory);
file.Write(this.HardwareOption.brand);
file.Write(this.HardwareOption.spec);
file.Write(this.HardwareOption.count);
file.Write(this.HardwareOption.comments);
file.Write(this.HardwareOption.unit);
file.Write(this.DataList.length);
for (let data of this.DataList) {
file.Write(data[0]);
file.Write(data[1]);
}
file.Write(this.HardwareOption.isHole);
file.Write(this.RelevanceBoards.length);
for (let id of this.RelevanceBoards)
file.WriteSoftObjectId(id);
file.Write(this.RelevanceHardware.length);
for (let id of this.RelevanceHardware)
file.WriteSoftObjectId(id);
file.Write(this.RelevanceHandle.length);
for (let id of this.RelevanceHandle)
file.WriteSoftObjectId(id);
}
};
__decorate([
AutoRecordObject
], HardwareCompositeEntity.prototype, "HardwareOption", void 0);
__decorate([
AutoRecord
], HardwareCompositeEntity.prototype, "DataList", void 0);
__decorate([
AutoRecord
], HardwareCompositeEntity.prototype, "RelevanceBoards", void 0);
__decorate([
AutoRecord
], HardwareCompositeEntity.prototype, "RelevanceHardware", void 0);
__decorate([
AutoRecord
], HardwareCompositeEntity.prototype, "RelevanceHandle", void 0);
HardwareCompositeEntity = HardwareCompositeEntity_1 = __decorate([
Factory
], HardwareCompositeEntity);
const CanDrawHoleFuzz = 0.1;
var BoardFaceType;
(function (BoardFaceType) {
BoardFaceType[BoardFaceType["Side"] = 0] = "Side";
BoardFaceType[BoardFaceType["NoSide"] = 1] = "NoSide";
})(BoardFaceType || (BoardFaceType = {}));
function GetSideFaceMtx(cu, inverseZ = false) {
let x = cu.GetFirstDeriv(0).normalize();
let y = ZAxis;
let z = x.clone().cross(y);
if (inverseZ)
z.negate();
let basePt;
if ((equaln$1(x.x, 0, 1e-5) && x.y > 0) || x.x < -1e-5) {
x.negate();
basePt = cu.EndPoint;
}
else
basePt = cu.StartPoint;
//构建面矩阵
return new three.Matrix4()
.makeBasis(x, y, z)
.setPosition(basePt);
}
const x = new three.Vector3;
const y = new three.Vector3;
let tempPolyline;
/**
* 转换成多段线点表(pts bul)
* @param cu
* @param [isOutline=true] 如果是外部轮廓,则返回逆时针点表
* @returns pts buls
*/
function ConverToPtsBul(cu, isOutline = true) {
let ptsBuls;
if (cu instanceof exports.Circle) {
let pl = ConverCircleToPolyline$1(cu);
ptsBuls = pl.PtsBuls;
}
else
ptsBuls = cu.PtsBuls;
let ocs = cu.OCSNoClone;
//判断是不是被镜像了
x.setFromMatrixColumn(ocs, 0);
y.setFromMatrixColumn(ocs, 1);
let z1 = x.cross(y);
let isMirror = !equaln$1(z1.z, 1, 1e-3);
for (let i = 0; i < ptsBuls.pts.length; i++) {
Vector2ApplyMatrix4(ocs, ptsBuls.pts[i]);
if (isMirror)
ptsBuls.buls[i] *= -1;
}
if (isOutline && cu.IsClose) {
tempPolyline = tempPolyline || new exports.Polyline;
tempPolyline.LineData.length = 0;
for (let i = 0; i < ptsBuls.pts.length; i++)
tempPolyline.LineData.push({ pt: ptsBuls.pts[i], bul: ptsBuls.buls[i] });
if (cu.Area2 < 0) {
tempPolyline.Reverse();
ptsBuls = tempPolyline.PtsBuls;
}
tempPolyline.LineData.length = 0;
}
return ptsBuls;
}
//转换成多段线点表(pts bul)
function ConverArcToPtsBul(arc, hasEnd = false) {
let result = { pts: [], buls: [] };
let bul = arc.Bul;
result.pts.push(arc.StartPoint);
result.buls.push(bul);
if (hasEnd) {
result.pts.push(arc.EndPoint);
result.buls.push(0);
}
return result;
}
exports.Production = void 0;
(function (Production) {
/**获取板件拆单数据 */
function GetBoardSplitOrderData(br, redundancyKnif = 0) {
let sealedData = GetSealedBoardContour(br);
if (!sealedData) {
ToasterShowEntityMsg({
msg: br.Name + " 轮廓错误,可能存在轮廓自交,请检查后重新拆单!(错误的板已经选中,您可以按住鼠标中键查看该板!)(使用FISC命令可以修复自交轮廓!)",
timeout: 8000,
intent: Intent.DANGER,
ent: br.__OriginalEnt__ ?? br
});
return undefined;
}
let { brContour: orgContour, sealedContour: sealedOutline } = sealedData;
let offsetTanslation = sealedOutline.BoundingBox.min;
sealedOutline.Position = sealedOutline.Position.sub(offsetTanslation);
let sealedOutlinePtsBul = ConverToPtsBul(sealedOutline); //不分裂圆弧转点表
//外轮廓去掉最后的闭合点
sealedOutlinePtsBul.pts.pop();
sealedOutlinePtsBul.buls.pop();
let size = sealedOutline.BoundingBox.getSize(new three.Vector3);
//不扣除封边的轮廓信息
let originOutlinePtsBul = ConverToPtsBul(orgContour);
originOutlinePtsBul.pts.pop();
originOutlinePtsBul.buls.pop();
let { modeling, sideModeling } = GetBoardModelingData(br, offsetTanslation, redundancyKnif);
let boardContour;
if (GetSpiteSize(br))
boardContour = ConverToPtsBul(br.ContourCurve); //不分裂圆弧转点表
//每段封边信息
let perSealData = ParseSealData(sealedData);
let perBoardEdgeRemarkData = br.IsSpecialShape ? ParseBoardEdgeRemarkData(sealedData, br.BoardProcessOption.highBoardEdgeRemark) : [];
//因为传递给拆单软件的数据是逆时针,所以我们翻转它
if (orgContour.Area2 < 0)
perSealData.reverse();
return {
info: GetBoardInfo(br, size),
originOutlin: originOutlinePtsBul, //拼错了 未扣封边的点表
outline: sealedOutlinePtsBul, //扣完封边的点表
sealing: perSealData, //每段曲线的封边信息
boardEdgeRemark: perBoardEdgeRemarkData, //每段曲线的板边备注信息
modeling,
holes: GetBoardHolesData(br, offsetTanslation, orgContour),
sideModeling,
offsetTanslation,
metalsData: GetBoardMetals(br),
boardContour,
modeling2D: Get2DModeing(br, offsetTanslation),
modeling3D: Get3DModeing(br, offsetTanslation),
};
}
Production.GetBoardSplitOrderData = GetBoardSplitOrderData;
//生产那边需要一一对应的数据
function ParseSealData(sealData) {
let seals = [];
for (let i = 0; i < sealData.brCurves.length; i++) {
let curve = sealData.brCurves[i];
let sealD = sealData.highSeals[i];
if (curve instanceof exports.Circle) {
let seal2 = {
length: curve.Length * 0.5,
...sealD
};
seals.push(seal2);
seals.push({ ...seal2 });
return seals;
}
else {
if (curve instanceof exports.Polyline) //多段线炸开
{
for (let subC of curve.Explode()) {
let seal2 = {
length: subC.Length,
...sealD
};
seals.push(seal2);
}
}
else //直线 圆弧直接加
{
let seal2 = {
length: curve.Length,
...sealD
};
seals.push(seal2);
}
}
}
return seals;
}
Production.ParseSealData = ParseSealData;
function ParseBoardEdgeRemarkData(sealData, highBoardEdgeRemark) {
let remarks = [];
for (let i = 0; i < sealData.brCurves.length; i++) {
let curve = sealData.brCurves[i];
let remarkData = highBoardEdgeRemark[i];
if (curve instanceof exports.Circle) {
remarks.push(remarkData);
remarks.push({ ...remarkData });
return remarks;
}
else {
if (curve instanceof exports.Polyline) //多段线炸开
{
for (let subC of curve.Explode()) {
remarks.push(remarkData);
}
}
else //直线 圆弧直接加
{
remarks.push(remarkData);
}
}
}
return remarks;
}
Production.ParseBoardEdgeRemarkData = ParseBoardEdgeRemarkData;
function GetBoardInfo(br, size) {
let data = br.BoardProcessOption;
let spliteSize = Production.GetSpiteSize(br);
let isRect = !!spliteSize || !br.IsSpecialShape;
return {
id: br.Id.Index,
name: br.Name,
[EBoardKeyList.RoomName]: data[EBoardKeyList.RoomName],
[EBoardKeyList.CabinetName]: data[EBoardKeyList.CabinetName],
[EBoardKeyList.Mat]: data[EBoardKeyList.Mat],
[EBoardKeyList.BrMat]: data[EBoardKeyList.BrMat],
[EBoardKeyList.Color]: data[EBoardKeyList.Color],
[EBoardKeyList.Lines]: data[EBoardKeyList.Lines],
[EBoardKeyList.DrillType]: data[EBoardKeyList.DrillType],
spliteHeight: spliteSize ? spliteSize.spliteHeight.toString() : "",
spliteThickness: spliteSize ? spliteSize.spliteThickness.toString() : "",
spliteWidth: spliteSize ? spliteSize.spliteWidth.toString() : "",
isRect,
remarks: data.remarks.slice(),
kaiLiaoWidth: size.x,
kaiLiaoHeight: size.y,
openDir: br.OpenDir,
};
}
Production.GetBoardInfo = GetBoardInfo;
function GetMetalTotalEntitys(md, isHole = false, filter) {
let holes = [];
if (isHole && !md.HardwareOption.isHole)
return [];
for (let e of md.Entitys) {
if (e instanceof HardwareCompositeEntity) {
if (!isHole || md.HardwareOption.isHole)
holes.push(...GetMetalTotalEntitys(e, isHole, filter).map(h => h.ApplyMatrix(md.OCS)));
}
else {
if (!filter || filter(e)) {
holes.push(e.Clone().ApplyMatrix(md.OCS));
}
}
}
return holes;
}
Production.GetMetalTotalEntitys = GetMetalTotalEntitys;
function GetOriginBoardModelingData(br) {
const getModelings = (ms) => {
let data = [];
for (let m of ms) {
let cu = m.shape.Outline.Curve;
if (m.shape.Holes.length === 0 && cu instanceof exports.Circle && cu.Radius < HostApplicationServices.chaidanOption.modeling2HoleRad + 1e-6)
continue;
if (HostApplicationServices.chaidanOption.useDefaultRad)
m.knifeRadius = HostApplicationServices.chaidanOption.radius;
data.push({
outline: ConverToPtsBul(cu, false),
holes: m.shape.Holes.map(h => ConverToPtsBul(h.Curve, false)),
thickness: m.thickness + (m.addDepth ?? 0),
dir: m.dir,
knifeRadius: m.knifeRadius,
});
}
return data;
};
let allModeling = GetModelingFromCustomDrill(br);
let modeling = getModelings([...br.BoardModeling, ...allModeling.modeling]);
let sideModeling = getModelings(allModeling.sideModeling);
return { modeling, sideModeling };
}
Production.GetOriginBoardModelingData = GetOriginBoardModelingData;
/**
* 计算板的造型走刀数据(包括自定义排钻的走刀 侧面走刀)
* @param br
* @param offsetTanslation
*/
function GetBoardModelingData(br, offsetTanslation, redundancyKnif = 0) {
const tool = FeedingToolPath.GetInstance();
const tMtx = MoveMatrix(offsetTanslation.clone().negate());
const getModelings = (ms, isSide) => {
let data = [];
for (let m of ms) {
let cu = m.shape.Outline.Curve;
if (m.shape.Holes.length === 0 && cu instanceof exports.Circle && cu.Radius < HostApplicationServices.chaidanOption.modeling2HoleRad + 1e-6)
continue;
if (HostApplicationServices.chaidanOption.useDefaultRad)
m.knifeRadius = HostApplicationServices.chaidanOption.radius;
let paths = tool.GetModelFeedPath(br, m, redundancyKnif); //走刀路径
if (!isSide)
paths.forEach(path => path.ApplyMatrix(tMtx));
//走刀的ptsbuls
let feeding = paths.map((c) => ConverToPtsBul(c, false));
if (feeding.length > 0)
data.push({
feeding,
thickness: m.thickness + (m.addDepth ?? 0),
dir: m.dir,
knifeRadius: m.knifeRadius,
origin: {
outline: ConverToPtsBul(cu, false),
holes: m.shape.Holes.map(h => ConverToPtsBul(h.Curve, false)),
addLen: m.addLen,
addWidth: m.addWidth,
addDepth: m.addDepth,
}
});
else {
Toaster({
message: "板件有造型或者自定义排钻无法加工,请运行造型检测命令<checkmodeing>确认",
timeout: 5000,
intent: Intent.DANGER,
key: "造型加工错误"
});
}
}
return data;
};
let allModeling = GetModelingFromCustomDrill(br);
let modeling = getModelings([...br.BoardModeling, ...allModeling.modeling], false).filter(f => f.feeding.length > 0);
let sideModeling = getModelings(allModeling.sideModeling, true).filter(f => f.feeding.length > 0);
return { modeling, sideModeling };
}
Production.GetBoardModelingData = GetBoardModelingData;
//获得拆单尺寸
function GetSpiteSize(br) {
let [spHeight, spWidth, spThickness] = [br.BoardProcessOption.spliteHeight, br.BoardProcessOption.spliteWidth, br.BoardProcessOption.spliteThickness];
const isEffect = HostApplicationServices.chaidanOption.partialSplitValueCanTakesEffect;
const param = { L: br.Height, W: br.Width, H: br.Thickness };
if (isEffect ? (spHeight || spWidth || spThickness) : (spHeight && spWidth && spThickness)) {
spHeight = spHeight || br.Height.toString();
spWidth = spWidth || br.Width.toString();
spThickness = spThickness || br.Thickness.toString();
const spliteHeight = safeEval(spHeight, param, "L");
const spliteWidth = safeEval(spWidth, param, "W");
const spliteThickness = safeEval(spThickness, param, "H");
if (spliteHeight && spliteWidth && spliteThickness) {
return {
spliteHeight,
spliteWidth,
spliteThickness
};
}
}
}
Production.GetSpiteSize = GetSpiteSize;
//获得拆单轮廓(如果没有,那么将返回空,如果有,返回多段线)
function GetSpliteOutlineBySpliteSize(br) {
let size = GetSpiteSize(br);
if (size)
return new exports.Polyline().Rectangle(size.spliteWidth, size.spliteHeight);
return null;
}
Production.GetSpliteOutlineBySpliteSize = GetSpliteOutlineBySpliteSize;
/**孔信息,侧孔的z 均为 从上到下距离 */
function GetBoardHolesData(br, offsetTanslation, sealedContour) {
let data = {
frontBackHoles: [],
sideHoles: []
};
let brNormal = br.Normal;
// 性能优化的解析板件网洞类
// new ParseBoardHoleData(br, offsetTanslation, sealedContour);
for (let [, driss] of br.DrillList) {
for (let dris of driss) {
for (let dId of dris) {
if (!dId || dId.IsErase)
continue;
let d = dId.Object;
if (d instanceof ExtrudeHole)
ParseExtrudeHoles(d, br, offsetTanslation, data, sealedContour);
else
ParseCylHoles(d, br, offsetTanslation, data, sealedContour);
}
}
}
if (br.RelativeHardware) {
for (let dId of br.RelativeHardware) {
if (dId.IsErase)
continue;
let d = dId.Object;
let holes = [];
if (d instanceof HardwareCompositeEntity) {
holes.push(...GetMetalTotalEntitys(d, true, (e) => e instanceof Hole));
}
for (let h of holes) {
if (h instanceof ExtrudeHole)
ParseExtrudeHoles(h, br, offsetTanslation, data, sealedContour, true, true);
else
ParseCylHoles(h, br, offsetTanslation, data, sealedContour);
}
}
}
let modelings = br.BoardModeling;
for (let nid of br.LayerNails) {
if (!nid || !nid.Object || nid.IsErase)
continue;
let nail = nid.Object;
if (!isParallelTo(nail.Normal, brNormal, CanDrawHoleFuzz))
continue;
let sp = nail.Position.applyMatrix4(br.OCSInv);
let nor = nail.Normal.multiplyScalar(nail.Height);
let ep = nail.Position.add(nor).applyMatrix4(br.OCSInv);
let [z0, z1] = sp.z < ep.z ? [sp.z, ep.z] : [ep.z, sp.z];
let face = !equalv3(nail.Normal, brNormal, CanDrawHoleFuzz) ? FaceDirection.Front : FaceDirection.Back;
let depth = Math.min(z1, br.Thickness) - Math.max(z0, 0);
if (Math.max(z0, 0) < Math.min(z1, br.Thickness) - 1e-6
&& br.ContourCurve.PtInCurve(sp.setZ(0))
//层板钉中心点不在造型槽内
&& modelings.every(m => {
if (m.dir !== face && (depth + m.thickness) < br.Thickness) //不相交
return true;
// if (m.shape.Holes.some(h => h.Curve.PtInCurve(sp))) return true; //层板钉在孤岛内 现实中应该不会有
return !m.shape.Outline.Curve.PtInCurve(sp);
})) {
data.frontBackHoles.push({
type: nail.Type,
position: sp.sub(offsetTanslation),
radius: nail.Radius,
depth,
face,
name: '层板钉'
});
}
}
for (let m of modelings) {
let cu = m.shape.Outline.Curve;
if (m.shape.Holes.length === 0 && cu instanceof exports.Circle && cu.Radius < HostApplicationServices.chaidanOption.modeling2HoleRad + 1e-6) {
let center = cu.Center.setZ(0).sub(offsetTanslation);
data.frontBackHoles.push({
type: GangDrillType.Ljg, //设置为偏心轮导致我们分析大孔面方向和板的大孔面一致时错误,所以改成连接杆
position: center,
radius: cu.Radius,
depth: m.thickness,
face: m.dir,
name: GetGroupName(cu)
});
}
}
return data;
}
Production.GetBoardHolesData = GetBoardHolesData;
/**拆单那边需要把侧孔 z 坐标转换为从上到下 */
function InvertPosition(pos, thickness) {
pos.z = thickness - pos.z;
}
function HoleInBoard(center, radius, outline, isYMJ = false) {
let cir = new exports.Circle(center, radius - SCALAR);
if (isYMJ) {
return outline.IntersectWith(cir, 0).length === 0 && outline.PtInCurve(center);
}
else {
if (HostApplicationServices.forceFilterPxl)
return outline.IntersectWith(cir, 0).length === 0 && outline.PtInCurve(center);
else
return outline.IntersectWith(cir, 0).length > 1 || outline.PtInCurve(center);
}
}
/**分析常规排钻 */
function ParseCylHoles(cyHole, br, offsetTanslation, data, outline) {
let brInv = br.OCSInv;
let brNormal = br.Normal;
let brInvRo = new three.Matrix4().extractRotation(brInv);
let position = cyHole.Position.applyMatrix4(brInv);
let holes = data.frontBackHoles;
let face; //孔面方向
let isPush = false;
let endPt;
let depth = cyHole.Height;
let diffMat = brInv.clone().multiply(cyHole.OCSNoClone);
let x = new three.Vector3().setFromMatrixColumn(diffMat, 0);
let angle = angleTo(XAxis, x);
let cyNormal = cyHole.Normal.applyMatrix4(brInvRo);
let pos2 = position.clone().add(cyNormal.multiplyScalar(depth));
if (cyHole.Type === GangDrillType.Pxl || cyHole.Type === GangDrillType.WoodPXL) {
if (isParallelTo(cyHole.Normal, brNormal, CanDrawHoleFuzz)) {
if (!IsBetweenA2B(position.x, -cyHole.Radius, br.Width + cyHole.Radius, 1e-6)
|| !IsBetweenA2B(position.y, -cyHole.Radius, br.Height + cyHole.Radius, 1e-6)
|| !HoleInBoard(position.clone().setZ(0), cyHole.Radius, outline))
return;
position.sub(offsetTanslation);
//#region 求得真实的求交范围
let z0 = position.z;
let z1 = pos2.z;
if (z0 > z1)
[z0, z1] = [z1, z0];
let i1 = Math.max(z0, 0);
let i2 = Math.min(z1, br.Thickness);
if (i2 - i1 < CanDrawHoleFuzz)
return; //相交范围小于0.1
if (equaln$1(i1, 0, CanDrawHoleFuzz))
face = FaceDirection.Back;
else if (equaln$1(i2, br.Thickness, CanDrawHoleFuzz))
face = FaceDirection.Front;
else
return; //不在正面 也不在反面
depth = i2 - i1; //真实的相交范围
//#endregion
isPush = true;
}
}
else if (cyHole.Type === GangDrillType.Ljg || cyHole.Type === GangDrillType.Wood) {
if (isPerpendicularityTo(cyHole.Normal, brNormal, CanDrawHoleFuzz)) //侧孔
{
let z = position.z;
if (!IsBetweenA2B(z, -cyHole.Radius, br.Thickness + cyHole.Radius, 1e-6))
return;
let sp = position.clone().setZ(0); //真实数据
let ep = position.clone().add(cyHole.Normal.multiplyScalar(cyHole.Height).applyMatrix4(brInvRo)).setZ(0); //真实数据
let testLine = new exports.Line(sp, ep);
let iPt = outline.IntersectWith(testLine, 0, CanDrawHoleFuzz)[0];
if (!iPt) {
InteractionLog([{ msg: "警告:" }, { msg: `板件${br.Name}`, entity: [br, cyHole] }, { msg: "侧孔与板无交点,无法加工该侧孔!" }], LogType.Warning);
return;
}
position = iPt.clone().setZ(z); //排钻开始的位置
for (let p of [sp, ep]) {
if (outline.PtInCurve(p)) {
endPt = p.setZ(z); //排钻插入后的位置
break;
}
}
if (!endPt) {
InteractionLog([{ msg: "警告:" }, { msg: `板件${br.Name}`, entity: [br, cyHole] }, { msg: "侧孔位置有问题,排钻不在板轮廓内!" }], LogType.Warning);
return;
}
holes = data.sideHoles;
face = Math.floor(outline.GetParamAtPoint(iPt)); //板在那个边上
isPush = true;
depth = position.distanceTo(endPt);
angle = undefined;
InvertPosition(position, br.Thickness);
InvertPosition(endPt, br.Thickness);
}
else if (cyHole.Type === GangDrillType.Wood) //木销
{
if (!outline.PtInCurve(position.clone().setZ(0)))
return;
position.sub(offsetTanslation);
//#region 求得真实的求交范围
let z0 = position.z;
let z1 = pos2.z;
if (z0 > z1)
[z0, z1] = [z1, z0];
let i1 = Math.max(z0, 0);
let i2 = Math.min(z1, br.Thickness);
if (i2 - i1 < CanDrawHoleFuzz)
return; //相交范围小于0.1
if (equaln$1(i1, 0, CanDrawHoleFuzz))
face = FaceDirection.Back;
else if (equaln$1(i2, br.Thickness, CanDrawHoleFuzz))
face = FaceDirection.Front;
else
return; //不在正面 也不在反面
depth = i2 - i1; //真实的相交范围
//#endregion
holes = data.frontBackHoles;
isPush = true;
}
}
else {
if (isParallelTo(cyHole.Normal, brNormal, CanDrawHoleFuzz)) {
if (!IsBetweenA2B(position.x, -cyHole.Radius, br.Width + cyHole.Radius, CanDrawHoleFuzz)
|| !IsBetweenA2B(position.y, -cyHole.Radius, br.Height + cyHole.Radius, CanDrawHoleFuzz)
|| !isIntersect2(0, br.Thickness, position.z, pos2.z, -CanDrawHoleFuzz)
|| !HoleInBoard(position.clone().setZ(0), cyHole.Radius, outline, true))
return;
position.sub(offsetTanslation);
holes = data.frontBackHoles;
face = !equalv3(cyHole.Normal, brNormal, CanDrawHoleFuzz) ? FaceDirection.Front : FaceDirection.Back;
isPush = true;
}
}
isPush && holes.push({
type: cyHole.Type,
position, //排钻开始位置
endPt, //排钻结束的位置(在板的坐标系)
radius: cyHole.Radius, //排钻半径
depth, //排钻的插入深度
face, //板在哪个边上
angle,
name: GetGroupName(cyHole)
});
}
/**分析自定义圆柱排钻
*
* isRelativeHardware 关联的五金使用此类型误差
* CanDrawHoleFuzz)//区间交集必须大于CanDrawHoleFuzz
* CanDrawHoleFuzz)//区间交集必须大于CanDrawHoleFuzz
*
*/
function ParseExtrudeHoles(d, br, offsetTanslation, data, outline, isCheckGroove = false, isRelativeHardware = false) {
if (!d.isHole)
return;
let brNormal = br.Normal;
let cir = d.ContourCurve;
if (cir instanceof exports.Circle) {
let diffMtx = br.OCSInv.multiply(d.OCS);
let nor = d.Normal;
let sp = cir.Center.applyMatrix4(diffMtx);
let ep = cir.Center.add(new three.Vector3(0, 0, d.Height)).applyMatrix4(diffMtx);
let x = new three.Vector3().setFromMatrixColumn(diffMtx, 0);
//#I2DPFO 在挖穿造型内的五金不加工
const grooves = br.Grooves.filter(g => equaln$1(g.Thickness, br.Thickness));
const groovesOutlines = isCheckGroove ? grooves.map(g => g.ContourCurve.Clone().ApplyMatrix(g.OCS).ApplyMatrix(br.OCSInv).Z0()) : [];
if (isParallelTo(nor, brNormal, CanDrawHoleFuzz)) //正反面孔
{
let z0 = Math.min(sp.z, ep.z);
let z1 = Math.max(sp.z, ep.z);
let p = sp.clone().setZ(0).sub(offsetTanslation);
//区间没有交集
if (!(Math.max(z0, 0) < (Math.min(z1, br.Thickness) - CanDrawHoleFuzz)))
return;
if (!(z0 < CanDrawHoleFuzz || z1 > (br.Thickness - CanDrawHoleFuzz))) //禁止在中间挖洞
{
InteractionLog([{ msg: "警告:" }, { msg: `板件${br.Name}`, entity: [br, d] }, { msg: "的孔嵌在板中间,无法加工,已经跳过!" }], LogType.Warning);
return;
}
if (!(outline.PtInCurve(p))) //在轮廓内
{
InteractionLog([{ msg: "警告:" }, { msg: `板件${br.Name}`, entity: [br, d] }, { msg: "的孔不在板轮廓内,无法加工,已经跳过!" }], LogType.Warning);
return;
}
if (groovesOutlines.some(g => g.PtInCurve(p))) //在洞内
{
InteractionLog([{ msg: "警告:" }, { msg: `板件${br.Name}`, entity: [br, d] }, { msg: "的孔在造型内,无法加工,已经跳过!" }], LogType.Warning);
return;
}
let depth = z0 < CanDrawHoleFuzz ? z1 : br.Thickness - z0;
let angle = angleTo(XAxis, x);
if (equaln$1(angle, Math.PI))
angle = 0;
if (depth > CanDrawHoleFuzz)
data.frontBackHoles.push({
type: d.isThrough ? GangDrillType.TK : (isRelativeHardware ? GangDrillType.Ljg : d.Type),
position: z0 < CanDrawHoleFuzz ? p : p.setZ(br.Thickness),
radius: cir.Radius,
depth,
face: z0 < CanDrawHoleFuzz ? FaceDirection.Back : FaceDirection.Front,
angle: angle,
name: GetGroupName(d)
});
}
else //侧孔
{
let oldZ = sp.z;
let [minX, maxX] = sp.x < ep.x ? [sp.x, ep.x] : [ep.x, sp.x];
let [minY, maxY] = sp.y < ep.y ? [sp.y, ep.y] : [ep.y, sp.y];
if (sp.z > -cir.Radius
&& sp.z < br.Thickness + cir.Radius
&& Math.max(minX, 0) < (Math.min(br.Width, maxX) + (isRelativeHardware ? -CanDrawHoleFuzz : 1e-6)) //区间交集必须大于CanDrawHoleFuzz
&& Math.max(minY, 0) < (Math.min(br.Height, maxY) + (isRelativeHardware ? -CanDrawHoleFuzz : 1e-6)) //区间交集必须大于CanDrawHoleFuzz
) {
sp.setZ(0);
ep.setZ(0);
let line = new exports.Line(sp, ep);
let pt = outline.IntersectWith(line, 0)[0];
if (!pt) {
InteractionLog([{ msg: "警告:" }, { msg: `板件${br.Name}`, entity: [br, d] }, { msg: `${isRelativeHardware ? "五金" : "排钻"}嵌在板件内部,已经跳过!` }], LogType.Warning);
return;
}
let position = pt.clone().setZ(oldZ);
let endPt;
let face = Math.floor(outline.GetParamAtPoint(pt));
for (let p of [line.StartPoint, line.EndPoint]) {
if (!equalv2(p, position) && outline.PtInCurve(p)) {
endPt = p.setZ(oldZ);
break;
}
}
if (!endPt)
return;
let depth = position.distanceTo(endPt);
if (equaln$1(depth, 0, CanDrawHoleFuzz))
return;
InvertPosition(position, br.Thickness);
InvertPosition(endPt, br.Thickness);
data.sideHoles.push({
type: GangDrillType.Ljg,
endPt,
position,
radius: cir.Radius,
depth,
face,
name: GetGroupName(d)
});
}
}
}
}
function GetBoardMetals(br) {
let mids = br.RelativeHardware;
let metalsData = {
metals: 0,
comp: 0
};
for (let id of mids) {
if (!id || id.IsErase)
continue;
let metals = id.Object;
if (!metals.HardwareOption)
continue;
if (metals.HardwareOption.type === EMetalsType.Metals) {
metalsData.metals++;
}
else {
metalsData.comp++;
}
}
return metalsData;
}
function GetHardwareCompositeData(en) {
const accuracy = HostApplicationServices.chaidanOption.hardwareExpressionFormattingAccuracy;
let size = en.BoundingBoxInOCS.getSize(new three.Vector3);
let data = { ...en.HardwareOption };
const actualVal = safeEval(data.actualExpr, { L: size.x, W: size.y, H: size.z });
data.actualExpr = actualVal ? actualVal.toString() : data.actualExpr;
data.spec = ParseExpr(data.spec, accuracy, { L: size.x, W: size.y, H: size.z });
data.model = ParseExpr(data.model, accuracy, { L: size.x, W: size.y, H: size.z });
data.factory = ParseExpr(data.factory, accuracy, { L: size.x, W: size.y, H: size.z });
data.brand = ParseExpr(data.brand, accuracy, { L: size.x, W: size.y, H: size.z });
data.count = (safeEval(data.count, { L: size.x, W: size.y, H: size.z }) || 0).toString();
let metalData = {
metalsOption: data,
dataList: en.DataList,
children: [],
size: size
};
if (en instanceof HardwareCompositeEntity && (en.HardwareOption.isSplite || en.HardwareOption.isSplitePrice)) {
if (en.Entitys.every(e => !(e instanceof HardwareCompositeEntity || e instanceof HardwareTopline)))
return metalData;
for (let e of en.Entitys) {
if (e instanceof HardwareCompositeEntity) {
let d = GetHardwareCompositeData(e);
metalData.children.push(d);
}
else if (e instanceof HardwareTopline) {
metalData.children.push(...GetHardwareToplineData(e));
}
}
}
return metalData;
}
Production.GetHardwareCompositeData = GetHardwareCompositeData;
function GetHardwareToplineData(en) {
let data = { ...en.HardwareOption };
let datas = [];
let map = new Map();
let addLen = en.HardwareOption.addLen;
let cus = en.Segmentations;
let size = en.BoundingBoxInOCS.getSize(new three.Vector3);
for (let cu of cus) {
let len = parseFloat(FixedNotZero(cu.Length, 2));
if (map.has(len)) {
map.set(len, map.get(len) + 1);
}
else {
map.set(len, 1);
}
}
for (let [len, count] of map) {
let totalLength = parseFloat(FixedNotZero(len + parseFloat(addLen), 2));
let width = parseFloat(FixedNotZero(size.y, 2));
let height = parseFloat(FixedNotZero(size.z, 2));
for (let i = 0; i < count; i++) {
let d = { ...en.HardwareOption };
const actualVal = safeEval(data.actualExpr, { L: totalLength, W: width, H: height });
d.actualExpr = actualVal ? actualVal.toString() : d.actualExpr;
const accuracy = HostApplicationServices.chaidanOption.hardwareExpressionFormattingAccuracy;
d.spec = ParseExpr(data.spec, accuracy, { L: totalLength, W: width, H: height });
datas.push({
metalsOption: d,
dataList: en.DataList,
length: totalLength,
children: [],
size
});
}
}
return datas;
}
Production.GetHardwareToplineData = GetHardwareToplineData;
/**获取排钻数量 */
function GetTotalDrillCount(brs) {
return lookOverBoardInfosTool.GetCount(brs);
}
Production.GetTotalDrillCount = GetTotalDrillCount;
function GetCabSize(brList) {
let brMap = new Map();
//根据柜名房名分类
for (let b of brList) {
let k = b.BoardProcessOption[EBoardKeyList.RoomName] + '-' + b.BoardProcessOption[EBoardKeyList.CabinetName];
if (brMap.has(k))
brMap.get(k).push(b);
else
brMap.set(k, [b]);
}
let sizeData = new Map();
for (let [k, brs] of brMap) {
let ocsInv = brs[0].SpaceOCSInv;
let box = new three.Box3();
let size = new three.Vector3();
for (let b of brs) {
sizeData.set(b, size);
box.union(b.GetBoundingBoxInMtx(ocsInv));
}
box.getSize(size);
}
return sizeData;
}
Production.GetCabSize = GetCabSize;
function Data2Polyline(data, isClose = true) {
let pl = new exports.Polyline(data.pts.map((p, i) => ({ pt: new three.Vector2(p.x, p.y), bul: data.buls[i] })));
if (isClose)
pl.CloseMark = true;
return pl;
}
Production.Data2Polyline = Data2Polyline;
function Get2DModeing(br, offset) {
let res = [];
let tmtx = MoveMatrix(offset.clone().negate());
for (let m of br.Modeling2D) {
let path = m.path.Clone().ApplyMatrix(tmtx);
res.push({
path: ConverToPtsBul(path, false),
dir: m.dir,
items: m.items.map(item => ({ ...item }))
});
}
return res;
}
Production.Get2DModeing = Get2DModeing;
function Get3DModeing(br, offset) {
let res = [];
for (let m of br.Modeling3D) {
let d = {
path: { pts: [], buls: [] },
knife: { ...m.knife },
dir: m.dir
};
for (let i = 0; i < m.path.length - 1; i++) {
let d1 = m.path[i];
let d2 = m.path[i + 1];
if (equaln$1(d1.bul, 0)) {
let p = d1.pt.clone();
InvertPosition(p, br.Thickness);
p.sub(offset);
d.path.pts.push(p);
d.path.buls.push(0);
}
else {
let arc = new exports.Arc().ParseFromBul(d1.pt.clone().sub(offset), d2.pt.clone().sub(offset), d1.bul);
let r = ConverArcToPtsBul(arc, false);
r.pts.forEach(p => InvertPosition(p, br.Thickness));
d.path.pts.push(...r.pts);
d.path.buls.push(...r.buls);
}
if (i === m.path.length - 2) {
let p = d2.pt.clone();
InvertPosition(p, br.Thickness);
p.sub(offset);
d.path.pts.push(p);
d.path.buls.push(0);
}
}
res.push(d);
}
return res;
}
Production.Get3DModeing = Get3DModeing;
function GetChaiDanFeedingPath(data, redundancyKnif = 0) {
const { thickness, boardContour, dir, addLen, addWidth, addDepth, knifeRadius, brThickness } = data;
let brContour = Data2Polyline(boardContour);
const tool = FeedingToolPath.GetInstance();
const outline = Contour.CreateContour(Data2Polyline(data.outline));
const holes = data.holes.map(h => Contour.CreateContour(Data2Polyline(h)));
let shape = new Shape(outline, holes);
const paths = tool.GetModelFeedPath({ Thickness: brThickness, ContourCurve: brContour }, {
shape,
thickness,
dir, knifeRadius, addLen, addWidth, addDepth
}, redundancyKnif);
return paths.map((c) => ConverToPtsBul(c, false));
}
Production.GetChaiDanFeedingPath = GetChaiDanFeedingPath;
function GetGroupName(ent) {
let group = ent.GroupId?.Object;
if (!group || !(group instanceof GroupRecord))
return "未知(无拆单名称)";
return group.Name;
}
Production.GetGroupName = GetGroupName;
})(exports.Production || (exports.Production = {}));
/**
* 分析曲线的上下左右位置的线
* @param curves
*/
function ParseEdgeSealDir(curves) {
let boxAll = new three.Box3;
let fb = new Flatbush__default["default"](curves.length);
for (let c of curves) {
let box = c.BoundingBox;
boxAll.union(box);
fb.add(box.min.x, box.min.y, box.max.x, box.max.y);
}
fb.finish();
let leftids = fb.search(boxAll.min.x - 1, boxAll.min.y - 1, boxAll.min.x + 1, boxAll.max.y + 1);
let rightids = fb.search(boxAll.max.x - 1, boxAll.min.y - 1, boxAll.max.x + 1, boxAll.max.y + 1);
let topids = fb.search(boxAll.min.x - 1, boxAll.max.y - 1, boxAll.max.x + 1, boxAll.max.y + 1);
let bottomids = fb.search(boxAll.min.x - 1, boxAll.min.y - 1, boxAll.max.x + 1, boxAll.min.y + 1);
const FindBestCurveIndex = (ids, dirRef) => {
if (ids.length === 1)
return ids[0];
let maxLength = -Infinity;
let bestIndex = -1;
for (let id of ids) {
let c = curves[id];
let dir = c.EndPoint.sub(c.StartPoint).normalize();
let length = Math.abs(dir.dot(dirRef)); //取模(模越长 表示和dirRef越平行(接近))
if (length > maxLength) {
bestIndex = id;
maxLength = length;
}
}
return bestIndex;
};
let left = FindBestCurveIndex(leftids, YAxis);
let right = FindBestCurveIndex(rightids, YAxis);
let top = FindBestCurveIndex(topids, XAxis);
let bottom = FindBestCurveIndex(bottomids, XAxis);
return [left, right, top, bottom];
}
/**
* 将曲线分段(根据高级封边的特性 (因为圆弧无法单独使用封边,所以和圆弧在一起的曲线必须和圆弧一样的封边,否则偏移失败))
* @l-arc-l,l-arc-arc-l,l-arc-l-arc-l....
* @param in_out_curves 曲线组( 函数结束后 这个数组被改变 )
* @returns 返回编组 curveGroups
*/
function SubsectionCurvesOfHightSeal(in_out_curves) {
let curveGroups = [];
let usedCu = new WeakSet();
//归类曲线,返回归类是否成功
const paragraph = (nextCurve, curCurve, curvesGroup, isBack) => {
const curIsLine = curCurve instanceof exports.Line;
const nextIsLine = nextCurve instanceof exports.Line;
if (usedCu.has(nextCurve))
return false;
if (curIsLine !== nextIsLine) //直线和圆弧
{
if (curIsLine) {
if (isBack) {
if (!isParallelTo(curCurve.GetFirstDeriv(0).normalize(), nextCurve.GetFirstDeriv(0).normalize()))
return false;
}
else {
if (!isParallelTo(curCurve.GetFirstDeriv(0).normalize(), nextCurve.GetFirstDeriv(1).normalize()))
return false;
}
}
if (nextIsLine) {
if (isBack) {
if (!isParallelTo(curCurve.GetFirstDeriv(1).normalize(), nextCurve.GetFirstDeriv(0).normalize()))
return false;
}
else {
if (!isParallelTo(curCurve.GetFirstDeriv(0).normalize(), nextCurve.GetFirstDeriv(0).normalize()))
return false;
}
}
}
else if (nextIsLine) //都是直线
{
//共线且相连的直线分为一组 #I11T1Z
if (!isParallelTo(nextCurve.GetFirstDeriv(0).normalize(), curCurve.GetFirstDeriv(0).normalize()))
return false;
let pts = [curCurve.StartPoint, curCurve.EndPoint];
let pts2 = [nextCurve.StartPoint, nextCurve.EndPoint];
if (pts.every(p => pts2.every(p2 => !equalv3(p, p2, 1e-6)))) //2条线完全分离 没有共同点
return false;
}
//else 都是圆弧 必然成组
if (isBack)
curvesGroup.push(nextCurve);
else
curvesGroup.unshift(nextCurve);
usedCu.add(nextCurve);
return true;
};
let caclCus = in_out_curves.filter(c => !equaln$1(c.Length, 0));
while (caclCus.length > 0) {
let curCurve = caclCus.shift();
if (usedCu.has(curCurve))
continue;
let curvesGroup = [curCurve]; //编组
usedCu.add(curCurve);
//往后搜索
for (let i = 0; i < caclCus.length; i++) {
if (!paragraph(caclCus[i], curCurve, curvesGroup, true))
break;
curCurve = caclCus[i];
}
//只有第一条才需要往前搜索
if (caclCus.length === in_out_curves.length - 1) {
curCurve = curvesGroup[0];
//往前搜索
for (let i = caclCus.length - 1; i >= 0; i--) {
if (!paragraph(caclCus[i], curCurve, curvesGroup, false))
break;
curCurve = caclCus[i];
}
}
curveGroups.push(curvesGroup);
}
in_out_curves.length = 0;
//同组多条曲线连接为多段线
for (let g of curveGroups) {
if (g.length === 1)
in_out_curves.push(g[0]);
else {
let pl = new exports.Polyline();
for (let c of g)
pl.Join(c);
in_out_curves.push(pl);
}
}
return curveGroups;
}
//与GetBoardSealingCurves相关
function GetBoardHighSeal(br, sealcus) {
if (br.BoardProcessOption[EBoardKeyList.SpliteHeight]
&& br.BoardProcessOption[EBoardKeyList.SpliteWidth]
&& br.BoardProcessOption[EBoardKeyList.SpliteThickness]) {
return [
{ size: parseFloat(br.BoardProcessOption.sealedDown) },
{ size: parseFloat(br.BoardProcessOption.sealedRight) },
{ size: parseFloat(br.BoardProcessOption.sealedUp) },
{ size: parseFloat(br.BoardProcessOption.sealedLeft) },
];
}
let highSeals = [];
for (let d of br.BoardProcessOption.highSealed)
if (d.size != null)
highSeals.push({ ...d });
//若未设置高级封边,把上下左右封边存入高级封边
if (sealcus.length !== highSeals.length || !br.IsSpecialShape) {
let sealDown = parseFloat(br.BoardProcessOption[EBoardKeyList.DownSealed]);
let sealUp = parseFloat(br.BoardProcessOption[EBoardKeyList.UpSealed]);
let sealLeft = parseFloat(br.BoardProcessOption[EBoardKeyList.LeftSealed]);
let sealRight = parseFloat(br.BoardProcessOption[EBoardKeyList.RightSealed]);
highSeals.length = 0;
let dir = Math.sign(br.ContourCurve.Area2);
for (let c of sealcus) {
let derv = c.GetFirstDeriv(0).multiplyScalar(dir);
if (Math.abs(derv.x) > Math.abs(derv.y)) {
if (derv.x > 0)
highSeals.push({ size: sealDown });
else
highSeals.push({ size: sealUp });
}
else {
if (derv.y > 0)
highSeals.push({ size: sealRight });
else
highSeals.push({ size: sealLeft });
}
}
}
return highSeals;
}
function GetHighBoardEdgeRemark(br, sealcus) {
const opt = br.BoardProcessOption;
//顺序和封边一样
if (opt[EBoardKeyList.SpliteHeight]
&& opt[EBoardKeyList.SpliteWidth]
&& opt[EBoardKeyList.SpliteThickness]) {
return [
{ description: opt.edgeRemarkDown },
{ description: opt.edgeRemarkRight },
{ description: opt.edgeRemarkUp },
{ description: opt.edgeRemarkLeft },
];
}
const highBoardEdgeRemark = [];
for (const d of opt.highBoardEdgeRemark)
if (d.description != null)
highBoardEdgeRemark.push({ ...d });
//若未设置高级板边备注,把上下左右板边备注存入高级板边备注
if (sealcus.length !== highBoardEdgeRemark.length || !br.IsSpecialShape) {
const edgeRemarkDown = opt.edgeRemarkDown;
const edgeRemarkRight = opt.edgeRemarkRight;
const edgeRemarkUp = opt.edgeRemarkUp;
const edgeRemarkLeft = opt.edgeRemarkLeft;
highBoardEdgeRemark.length = 0;
const dir = Math.sign(br.ContourCurve.Area2);
for (const c of sealcus) {
const derv = c.GetFirstDeriv(0).multiplyScalar(dir);
if (Math.abs(derv.x) > Math.abs(derv.y)) {
if (derv.x > 0)
highBoardEdgeRemark.push({ description: edgeRemarkDown });
else
highBoardEdgeRemark.push({ description: edgeRemarkUp });
}
else {
if (derv.y > 0)
highBoardEdgeRemark.push({ description: edgeRemarkRight });
else
highBoardEdgeRemark.push({ description: edgeRemarkLeft });
}
}
}
return highBoardEdgeRemark;
}
/**偏移前后曲线起点没改变 */
function OffsetOutlineSpNotChange(oldcu, newCu) {
if (!newCu)
return false;
if (oldcu.EndParam !== newCu.EndParam)
return false;
let sDerv = oldcu.GetFirstDeriv(0).normalize();
let eDerv = oldcu.GetFirstDeriv(oldcu.EndParam).normalize().negate();
sDerv.add(eDerv).normalize();
let mDerv = newCu.StartPoint.sub(oldcu.StartPoint).normalize();
return isParallelTo(mDerv, sDerv);
}
/**
* 获取板件的轮廓曲线数组(合并共线了 已经对封边进行合并了 尊重原始时针)
* GetBrContourCurvesBySealGrouped
* @param offsetInside 向内偏移1(为了编辑封边)
* */
function GetBoardSealingCurves(br, offsetInside = false) {
let brContour = exports.Production.GetSpliteOutlineBySpliteSize(br);
if (brContour)
return brContour.Explode(); //如果是拆单板 则直接炸开返回
brContour = br.ContourCurve;
if (brContour instanceof exports.Circle)
return [brContour.Clone()];
if (offsetInside) {
let dir = Math.sign(brContour.Area2);
let offsetedCurve = brContour.GetOffsetCurves(-1 * dir)[0];
if (OffsetOutlineSpNotChange(brContour, offsetedCurve))
brContour = offsetedCurve;
}
//避免共线导致的侧面数据对应错误
let curves = brContour.Explode();
MergeCurvelist(curves);
if (curves.length === 1 && curves[0] instanceof exports.Circle) //变成一个圆
return curves;
if (br.IsSpecialShape) //是异形板(其实矩形板也需要分组 避免共线导致的错误) (但是如果删了这个 之前的数据会出现不兼容的问题)
SubsectionCurvesOfHightSeal(curves); //分组 join 改变cus
return curves;
}
class OffsetPolyline2 extends OffsetPolyline {
InitSubCurves() {
this._IsTopoOffset = true;
return this;
}
OffsetSubCurves() { }
GeneralCirclesAndVertexs() { }
GeneralTrimContours() {
super.GeneralTrimContours();
arrayRemoveIf(this._TrimPolylineContours, con => con.Curve.Area < 0.01); //因为局部偏移可能为0,产生0面积的轮廓
}
CheckPointDir(pt) {
let dir = this.GetPointAtCurveDir(pt);
return dir === 0 || dir === this._OffsetDistSign; //因为局部偏移可能为0,所以在线上的我们也保留了
}
}
/**
* 获取板件的轮廓(没有扣封边)(拆单时表现)
* 在拆单的时候 我们用这个轮廓(为了数据对应准确性)
* 修改时 请注意函数 GetSealedBoardContour
*/
function GetBoardContour(br) {
if (Math.abs(br.ContourCurve.Area) < 10)
return;
let curves = GetBoardSealingCurves(br);
if (curves.length === 1 && curves[0] instanceof exports.Circle)
return curves[0];
let allCurvs = [];
for (let c of curves)
if (c instanceof exports.Polyline)
allCurvs.push(...c.Explode());
else
allCurvs.push(c);
let brContour = exports.Polyline.FastCombine(allCurvs, 1e-4, false);
return brContour;
}
/**
* 获取板件(扣封边后的)轮廓(拆单时)
* 修改时 请注意函数 GetBoardContour
*/
function GetSealedBoardContour(br) {
if (Math.abs(br.ContourCurve.Area) < 10)
return;
let curves = GetBoardSealingCurves(br);
let highSeals = GetBoardHighSeal(br, curves);
if (curves.length === 1 && curves[0] instanceof exports.Circle) {
let res = {
brContour: curves[0],
sealedContour: curves[0].GetOffsetCurves(-highSeals[0].size)[0],
brCurves: curves,
highSeals,
};
return res;
}
let allCurvs = [];
for (let c of curves)
if (c instanceof exports.Polyline)
allCurvs.push(...c.Explode());
else
allCurvs.push(c);
let brContour = exports.Polyline.FastCombine(allCurvs, 1e-4, false);
let dir = Math.sign(brContour.Area2);
let sealedContours;
{
//局部偏移
let polylineOffset = new OffsetPolyline2(brContour, dir * -Math.max(...highSeals.map(s => s.size)));
let subIndex = 0;
polylineOffset._TrimCircleContours = [];
polylineOffset._TrimArcContours = [];
polylineOffset._SubOffsetedCurves = [];
polylineOffset._SubCurves = [];
polylineOffset._Circles = [];
polylineOffset._CacheOCS = IdentityMtx4;
polylineOffset._IsClose = true;
for (let i = 0; i < curves.length; i++) {
let curve = curves[i]; //曲线组
let seal = highSeals[i]; //封边
let preSeal = highSeals[FixIndex$1(i - 1, curves)];
if (curve instanceof exports.Polyline) {
let curveExpds = curve.Explode().filter(c => c.Length >= 1e-4);
for (let j = 0; j < curveExpds.length; j++) {
let c = curveExpds[j];
polylineOffset._SubCurves.push(c); //sub
//trim Circle
if (seal.size && (j || seal.size === preSeal.size))
polylineOffset._Circles.push(new exports.Circle(c.StartPoint, seal.size));
else
polylineOffset._Circles.push(undefined);
//offset
let offsetC = c.GetOffsetCurves(dir * -seal.size)[0];
if (offsetC)
polylineOffset._SubOffsetedCurves.push({
index: subIndex,
curve: offsetC,
dist: seal.size,
});
else
polylineOffset._TrimArcContours.push(Contour.CreateContour([c, new exports.Line(c.StartPoint, c.EndPoint)], false));
subIndex++;
}
}
else {
if (curve.Length < 1e-4)
continue;
polylineOffset._SubCurves.push(curve); //sub
//trim Circle
if (seal.size && seal.size === preSeal.size)
polylineOffset._Circles.push(new exports.Circle(curve.StartPoint, seal.size));
else
polylineOffset._Circles.push(undefined);
//offset
let offsetC = curve.GetOffsetCurves(dir * -seal.size)[0];
if (offsetC)
polylineOffset._SubOffsetedCurves.push({
index: subIndex,
curve: offsetC,
dist: seal.size,
});
else
polylineOffset._TrimArcContours.push(Contour.CreateContour([curve, new exports.Line(curve.StartPoint, curve.EndPoint)], false));
subIndex++;
}
}
polylineOffset._Vertexs = polylineOffset._SubCurves.map(c => c.StartPoint);
sealedContours = polylineOffset.Do();
}
//如果有多个 取最大
if (sealedContours.length > 1) {
Toaster({
message: `有板计算封边异常,请检查!(点击左下角提示可以查看该板)`,
timeout: 15000,
intent: Intent.WARNING,
key: "sealerror"
});
InteractionLog([{ msg: "警告:" }, { msg: `板:${br.Name}`, entity: [br] }, { msg: `在扣除封边计算中,得到了${sealedContours.length}条轮廓,请检查!` }], LogType.Warning);
let areas = sealedContours.map(p => p.Area);
let maxIndex = Max(areas, (a1, a2) => a2 > a1);
sealedContours = [sealedContours[maxIndex]];
}
//如果不闭合 则尝试闭合
let sealedContour = sealedContours[0];
if (sealedContour && !sealedContour.IsClose) {
if (sealedContour.StartPoint.distanceTo(sealedContour.EndPoint) < 0.1)
sealedContour.CloseMark = true;
else
sealedContour = CreateContour2([sealedContour])?.Curve;
}
if (!sealedContour)
return;
//逆时针
if (sealedContour && sealedContour.Area2 < 0)
sealedContour.Reverse();
let res = {
brContour,
sealedContour,
brCurves: curves,
highSeals,
};
return res;
}
/**
* 设置板的上下左右封边(解析来自高级封边)
* @param br
* @param sealDatas 封边数据
* @param [sealCurves] 封边的曲线
* @param [brContourCurve] 传递封边的曲线轮廓
*/
function SetBoardTopDownLeftRightSealData(br, sealDatas, sealCurves, brContourCurve) {
let dir = Math.sign((brContourCurve ?? br.ContourCurve).Area2);
sealCurves = sealCurves ?? GetBoardSealingCurves(br);
//现在我们不管是否有拆单尺寸,我们总是关系封边值
// let param = { L: br.Height, W: br.Width, H: br.Thickness };
// let spliteHeight = safeEval(br.BoardProcessOption.spliteHeight, param, "L");
// let spliteWidth = safeEval(br.BoardProcessOption.spliteWidth, param, "W");
// let spliteThickness = safeEval(br.BoardProcessOption.spliteThickness, param, "H");
// if ((spliteHeight && spliteWidth && spliteThickness) || !br.IsSpecialShape && cus.length === 4)
if (br.IsRect && sealCurves.length === 4) {
for (let i = 0; i < 4; i++) {
let derv = sealCurves[i].GetFirstDeriv(0).normalize();
if (isParallelTo(derv, XAxis, 1e-4)) {
if (derv.x * dir > 0)
br.BoardProcessOption[EBoardKeyList.DownSealed] = sealDatas[i].size.toString();
else
br.BoardProcessOption[EBoardKeyList.UpSealed] = sealDatas[i].size.toString();
}
else {
if (derv.y * dir > 0)
br.BoardProcessOption[EBoardKeyList.RightSealed] = sealDatas[i].size.toString();
else
br.BoardProcessOption[EBoardKeyList.LeftSealed] = sealDatas[i].size.toString();
}
}
}
else {
let [left, right, top, bottom] = ParseEdgeSealDir(sealCurves);
br.BoardProcessOption[EBoardKeyList.LeftSealed] = sealDatas[left].size.toString();
br.BoardProcessOption[EBoardKeyList.RightSealed] = sealDatas[right].size.toString();
br.BoardProcessOption[EBoardKeyList.UpSealed] = sealDatas[top].size.toString();
br.BoardProcessOption[EBoardKeyList.DownSealed] = sealDatas[bottom].size.toString();
}
}
function SetBoardEdgeRemarkData(br, remarkDatas, sealCurves, brContourCurve) {
let dir = Math.sign((brContourCurve ?? br.ContourCurve).Area2);
sealCurves = sealCurves ?? GetBoardSealingCurves(br);
const opt = br.BoardProcessOption;
if (br.IsRect && sealCurves.length === 4) {
for (let i = 0; i < 4; i++) {
let derv = sealCurves[i].GetFirstDeriv(0).normalize();
if (isParallelTo(derv, XAxis, 1e-4)) {
if (derv.x * dir > 0)
opt.edgeRemarkDown = remarkDatas[i].description;
else
opt.edgeRemarkUp = remarkDatas[i].description;
}
else {
if (derv.y * dir > 0)
opt.edgeRemarkRight = remarkDatas[i].description;
else
opt.edgeRemarkLeft = remarkDatas[i].description;
}
}
}
else {
let [left, right, top, bottom] = ParseEdgeSealDir(sealCurves);
opt.edgeRemarkLeft = remarkDatas[left].description;
opt.edgeRemarkRight = remarkDatas[right].description;
opt.edgeRemarkUp = remarkDatas[top].description;
opt.edgeRemarkDown = remarkDatas[bottom].description;
}
}
var Board_1;
//排钻配置名是合法的 可用的
function IsValidDriName(name) {
return name === DrillType.None || HostApplicationServices.DrillConfigs.has(name);
}
/**
* 板件实体
*/
let Board = Board_1 = class Board extends ExtrudeSolid {
constructor() {
super();
this.HasEdgeRenderType = true;
this.HasPlaceFaceRenderType = true;
this.HasBigHoleFaceRenderType = true;
this._Rotation = {
x: 0,
y: 0,
z: 0
};
this._Name = "";
//板件排钻表,与之碰撞板件为key
this._DrillList = new Map();
this._LayerNails = [];
this.RelativeHardware = [];
this._OpenDir = BoardOpenDir.None;
this.RelativeHandle = [];
this._IsChaiDan = true;
this._2DModelingList = [];
this._3DModelingList = [];
this._CustomNumber = null; //自定义编号
this._DrillLock = false; //排钻独立锁
this._DrillAssociationLock = new Set(); //排钻关联锁
//仅在渲染器中使用倒角
this.bevelEnabled = true;
//二维刀路 id -> polyline
this._KnifePolylineMap = new Map();
this._workerCalcedGeom = null; //worker计算后,暂时存入到这里
this._async2DPathIng = false;
//偏移缓存
this.OffsetPathCache = new Map();
this.InitBoardData();
}
/**
* 创建一个代理数组,数组改变时被监听
*/
CreateArray() {
return new Proxy([], {
set: (target, key, value, receiver) => {
if (Reflect.get(target, key, receiver) !== value)
this.WriteAllObjectRecord();
return Reflect.set(target, key, value, receiver);
}
});
}
InitBoardData() {
let defaultData = {
roomName: "",
cabinetName: "",
boardName: "",
material: "",
color: "",
lines: LinesType.Positive,
bigHoleDir: FaceDirection.Front,
composingFace: ComposingType.Arbitrary,
highSealed: this.CreateArray(),
sealedUp: "1",
sealedDown: "1",
sealedLeft: "1",
sealedRight: "1",
spliteHeight: "",
spliteWidth: "",
spliteThickness: "",
highDrill: this.CreateArray(),
frontDrill: true,
backDrill: true,
drillType: "",
remarks: this.CreateArray(),
edgeRemarkUp: "",
edgeRemarkDown: "",
edgeRemarkLeft: "",
edgeRemarkRight: "",
highBoardEdgeRemark: this.CreateArray(),
};
this._BoardProcessOption = new Proxy(defaultData, {
get: function (target, key, receiver) {
return Reflect.get(target, key, receiver);
},
set: (target, key, value, receiver) => {
if (Reflect.get(target, key, receiver) !== value) {
this.WriteAllObjectRecord();
if (key === "highDrill" || key === EBoardKeyList.HighSealed || key === "highBoardEdgeRemark") {
let arr = this.CreateArray();
arr.push(...value);
target[key] = arr;
//更新封边检查的显示
if (!this.__ReadFileIng__ && key === EBoardKeyList.HighSealed) {
let obj = this.CacheDrawObject.get(RenderType.Edge);
if (obj) {
this.UpdateDrawObject(RenderType.Edge, obj);
obj.updateMatrixWorld(true);
}
}
return true;
}
let result = Reflect.set(target, key, value, receiver);
if (!this.__ReadFileIng__) //看起来使用 this.ReadFileIng 似乎也是没问题的
{
if (key === EBoardKeyList.Lines)
this.Update(exports.UpdateDraw.Geometry);
else if (key === EBoardKeyList.ComposingFace) {
let obj = this.CacheDrawObject.get(RenderType.PlaceFace);
if (obj) {
this.UpdateDrawObject(RenderType.PlaceFace, obj);
obj.updateMatrixWorld(true);
}
}
else if (key === EBoardKeyList.BigHole) {
let obj = this.CacheDrawObject.get(RenderType.BigHoleFace);
if (obj) {
this.UpdateDrawObject(RenderType.BigHoleFace, obj);
obj.updateMatrixWorld(true);
}
}
}
return result;
}
return true;
}
});
}
//初始化板件 来自长宽高
InitBoard(length, width, thickness, boardType = BoardType.Layer) {
if (!length || !width || !thickness)
throw `无法使用该尺寸构建板,长:${length},宽:${width},厚:${thickness}`;
this.WriteAllObjectRecord();
this._BoardType = boardType;
if (boardType === BoardType.Layer) {
this.ColorIndex = 2;
this._Name = "层板";
}
else if (boardType === BoardType.Vertical) {
this.ColorIndex = 11;
this._Name = "立板";
}
else {
this.ColorIndex = 3;
this._Name = "背板";
}
let types = [...HostApplicationServices.DrillConfigs.keys(), "不排"];
let type = types.includes(this.BoardProcessOption.drillType) ? this.BoardProcessOption.drillType : types[0];
this._BoardProcessOption.drillType = type;
this._BoardProcessOption.highDrill = Array(4).fill(type);
this.ConverToRectSolid(width, length, thickness);
this.Update(exports.UpdateDraw.Geometry);
}
static CreateBoard(length, width, thickness, boardType = BoardType.Layer) {
let board = new Board_1();
board.InitBoard(length, width, thickness, boardType);
board.ApplyMatrix(board.RotateMat);
board._SpaceOCS.identity();
return board;
}
get DrillList() {
return this._DrillList;
}
get DrillLock() {
return this._DrillLock;
}
set DrillLock(v) {
this.WriteAllObjectRecord();
this._DrillLock = v;
}
get isDrillAssociationLock() {
//有一块关联的板就返回true
for (const o of this._DrillAssociationLock) {
if (o.Object && !o.Object.IsErase)
return true;
}
return false;
}
get DrillAssociationLock() {
return this._DrillAssociationLock;
}
AppendDrillAssociationLock(o) {
this.WriteAllObjectRecord();
this._DrillAssociationLock.add(o);
}
ClearDrillAssociationLock(o) {
this.WriteAllObjectRecord();
this._DrillAssociationLock.delete(o);
}
ClearAllAssociDrillLock() {
this.WriteAllObjectRecord();
for (const o of this._DrillAssociationLock) {
this.ClearDrillAssociationLock(o);
if (o && o.Object) {
let br = o.Object;
br.ClearDrillAssociationLock(this.Id);
}
}
}
get LayerNails() {
return this._LayerNails;
}
AppendNails(ids) {
this.WriteAllObjectRecord();
this._LayerNails.push(...ids);
}
ClearLayerNails() {
this.WriteAllObjectRecord();
for (let nail of this._LayerNails) {
if (nail.Object && !nail.IsErase)
nail.Object.Erase();
}
this._LayerNails.length = 0;
}
get CustomNumber() { return this._CustomNumber; }
set CustomNumber(n) {
if (n !== this._CustomNumber) {
this.WriteAllObjectRecord();
this._CustomNumber = n;
if (this._CustomNumberTextEntity) {
this._CustomNumberTextEntity.TextString = n?.toString() ?? "";
this._CustomNumberTextEntity.Visible = this._CustomNumberTextEntity.TextString !== "";
this._CustomNumberTextEntity.Height = HostApplicationServices.boardCustomNumberTextHeight;
}
}
}
/**
* 你可以安心的修改它,这样会直接影响到板件,因为板件对这个对象添加了代理.
*/
get BoardProcessOption() {
return this._BoardProcessOption;
}
set BoardProcessOption(obj) {
Object.assign(this._BoardProcessOption, obj, {
[EBoardKeyList.HighSealed]: obj[EBoardKeyList.HighSealed].slice(),
highBoardEdgeRemark: obj.highBoardEdgeRemark.slice(),
});
}
get NeedUpdateRelevanceGroove() {
if (super.NeedUpdateRelevanceGroove)
return true;
for (let k of this.RelativeHardware) {
if (!k || !k.Object)
continue;
if (this.__CacheKnifVersion__[k.Index] !== (k.Object).__UpdateVersion__)
return true;
}
return false;
}
GetRelevanceKnifes(knifs) {
super.GetRelevanceKnifes(knifs);
for (let e of this.RelativeHardware) {
if (e.IsErase)
continue;
let ent = e.Object;
if (ent instanceof HardwareCompositeEntity) {
if (ent.HardwareOption.isHole) {
let holes = ent.GetAllEntity(true, e => e instanceof ExtrudeHole || e instanceof ExtrudeSolid);
for (let i = 0; i < holes.length; i++) {
let h = holes[i];
let g = h instanceof ExtrudeHole ? h.Convert2ExtrudeSolid() : h;
g.__TempIndexVersion__ = { Index: ent.Id.Index, Version: ent.__UpdateVersion__ };
knifs.push(g);
}
}
}
}
}
ClearRelevance(en) {
for (let id of this.RelativeHardware) {
let e = id.Object;
if (e instanceof HardwareCompositeEntity) {
arrayRemoveIf(e.RelevanceBoards, i => !i || i.Index === this.Id.Index);
}
}
this.RelativeHardware.length = 0;
super.ClearRelevance(en);
}
get SplitBoards() {
let brs = this.SplitExtrudes;
let highDrills;
let ocsInv;
if (brs.some(br => br.__OriginalEnt__)) {
if (this._BoardProcessOption.highDrill
&& this._BoardProcessOption.highDrill.length > 1
&& !this._BoardProcessOption.highDrill.every(d => d === this._BoardProcessOption.drillType)) {
highDrills = this._BoardProcessOption.highDrill;
}
ocsInv = this.OCSInv;
}
let oldContour = this.ContourCurve; //旧的轮廓
let oldHightSealCurves = GetBoardSealingCurves(this); //旧的封边轮廓
let oldHightSealDatas = GetBoardHighSeal(this, oldHightSealCurves); //旧的封边数据
//拆单或者bbs的时候会重新加入最新的原板件的排钻和层板钉数据
for (let br of brs) {
if (br.__OriginalEnt__) {
br._Name = this._Name;
br._DrillList = new Map(this._DrillList.entries());
br._LayerNails = [...this._LayerNails];
br.ProcessingGroupList = [...this.ProcessingGroupList];
br._BoardProcessOption = { ...this._BoardProcessOption };
br._CustomNumber = this._CustomNumber; //因为CustomNumber不会刷新绘制,所以拷贝这个
let new2old_edgeMap;
//修正排钻边的数据
if (highDrills) {
br.BoardProcessOption.highDrill = []; //因为上面用了拷贝,所以这里不能直接改它的数据(我们新建一个数组来改它,否则原始板件的数据就被改掉了)
new2old_edgeMap = ParseNewBr2OldBr_EdgeMap(br, this, ocsInv);
for (let index of new2old_edgeMap) {
let dri = highDrills[index];
if (dri !== undefined)
br._BoardProcessOption.highDrill.push(dri);
else
br._BoardProcessOption.highDrill.push(br._BoardProcessOption.drillType);
}
}
else //填充默认类型就好了
br._BoardProcessOption.highDrill = Array(br.contourCurve.EndParam).fill(br._BoardProcessOption.drillType);
//保持封边属性(代码来自SetContourCurve)
br._BoardProcessOption.highSealed = [];
if (br.isRect) {
SetBoardTopDownLeftRightSealData(br, oldHightSealDatas, oldHightSealCurves, oldContour);
}
else //变成了异形
{
let newhightSealCurves = GetBoardSealingCurves(br);
for (let i = 0; i < newhightSealCurves.length; i++) {
let newC = newhightSealCurves[i];
let p = newC.GetPointAtParam(newC.EndParam * 0.5);
let closesIndex = 0;
let closesDistance = Infinity;
for (let j = 0; j < oldHightSealCurves.length; j++) {
let oldC = oldHightSealCurves[j];
let d = oldC.GetClosestPointTo(p, false).distanceTo(p);
if (d < closesDistance) {
closesIndex = j;
closesDistance = d;
}
}
br._BoardProcessOption.highSealed.push(oldHightSealDatas[closesIndex]);
}
}
}
}
return brs;
}
get BoardModeling() {
let models = [];
for (let g of this.grooves) {
let cu = g.ContourCurve.Clone().ApplyMatrix(this.OCSInv.multiply(g.OCSNoClone));
cu.Erase(false); //当内部造型超过100个时,不校验时,这个曲线就是erase的状态,所以设置这个状态,避免无法绘制出来
if (cu instanceof exports.Circle) //这里保证这个圆是正常坐标系 I3BUSY#note_4525213
cu.OCS = new three.Matrix4().setPosition(cu.Position);
let outline = Contour.CreateContour(cu, false);
let holes = [];
for (let subG of g.Grooves) {
let holeCu = subG.ContourCurve.Clone().ApplyMatrix(this.OCSInv.multiply(subG.OCSNoClone));
holes.push(Contour.CreateContour(holeCu, false));
}
let s = new Shape(outline, holes);
models.push({
shape: s,
thickness: g.Thickness,
dir: equaln$1(g.Position.applyMatrix4(this.OCSInv).z, 0) && g.Thickness < this.thickness - 1e-6 ? FaceDirection.Back : FaceDirection.Front,
knifeRadius: g.KnifeRadius,
addLen: g.GroovesAddLength,
addWidth: g.GroovesAddWidth,
addDepth: g.GroovesAddDepth,
});
}
return models;
}
set BoardModeling(models) {
this.WriteAllObjectRecord();
this.grooves.length = 0;
for (let model of models) {
let g = new ExtrudeSolid();
g.ContourCurve = model.shape.Outline.Curve;
g.Thickness = model.thickness;
g.GroovesAddLength = model.addLen;
g.KnifeRadius = model.knifeRadius;
for (let hole of model.shape.Holes) {
let subG = new ExtrudeSolid();
subG.ContourCurve = hole.Curve;
subG.Thickness = model.thickness;
g.AppendGroove(subG);
}
//不需要处理正反面的原因是因为Get时保留了曲线的高度,导致生成的槽位置已经在指定位置
g.ApplyMatrix(this.OCSNoClone);
this.grooves.push(g);
}
this.GrooveCheckAllAutoSplit();
this.Update();
}
//二维刀路
get Modeling2D() {
return [...this._2DModelingList];
}
set Modeling2D(ms) {
this.WriteAllObjectRecord();
this._2DModelingList = ms;
this.Clear2DPathCache();
this.Update(exports.UpdateDraw.Geometry);
}
get Modeling3D() {
return [...this._3DModelingList];
}
set Modeling3D(ms) {
this.WriteAllObjectRecord();
this.Clear3DPathCache();
this._3DModelingList = ms;
this.Update(exports.UpdateDraw.Geometry);
}
ClearModeling2DList() {
if (this._2DModelingList.length === 0)
return;
this.WriteAllObjectRecord();
this._2DModelingList.length = 0;
this.Clear2DPathCache();
this.Update(exports.UpdateDraw.Geometry);
}
ClearModeling3DList() {
if (this._3DModelingList.length === 0)
return;
this.WriteAllObjectRecord();
this._3DModelingList.length = 0;
this.Clear3DPathCache();
this.Update(exports.UpdateDraw.Geometry);
}
get IsChaiDan() {
return this._IsChaiDan;
}
set IsChaiDan(v) {
if (this._IsChaiDan !== v) {
this.WriteAllObjectRecord();
this._IsChaiDan = v;
this.Update(exports.UpdateDraw.Geometry);
}
}
get OpenDir() {
return this._OpenDir;
}
set OpenDir(v) {
if (this._OpenDir !== v) {
this.WriteAllObjectRecord();
this._OpenDir = v;
//开门方向改变更新
this.Update(exports.UpdateDraw.Geometry);
}
}
ClearBoardModeling() {
if (this.grooves.length === 0)
return;
this.WriteAllObjectRecord();
this.grooves.length = 0;
this.Update(exports.UpdateDraw.Geometry);
}
/**
* 注意传入的排钻列表,避免指针被引用
*/
AppendDrillList(k, drs) {
this.WriteAllObjectRecord();
let oldDrs = this._DrillList.get(k);
if (oldDrs)
oldDrs.push(...drs); //同类型板件时,会触发这里.
else
this._DrillList.set(k, drs);
}
ClearDrillList(k) {
let drids = this._DrillList.get(k);
if (drids) {
this.WriteAllObjectRecord();
for (let drillents of drids) {
for (let objId of drillents) {
if (objId && !objId.IsErase)
objId.Object.Erase();
}
}
this._DrillList.delete(k);
if (k && k.Object) {
//必须在这里删除
let br = k.Object;
br.ClearDrillList(this.Id);
}
}
}
ClearAllDrillList() {
for (const [id] of this._DrillList) {
this.ClearDrillList(id);
}
}
Erase(isErase = true) {
if (isErase === this.IsErase)
return;
super.Erase(isErase);
if (!isErase)
return;
//记录数据,避免下面记录的时候,排钻已经被删除,导致排钻数据被优化掉.
this.WriteAllObjectRecord();
for (const [, driss] of this._DrillList) {
for (let dris of driss)
for (let d of dris)
if (d && d.Object)
d.Object.Erase();
}
this.ClearLayerNails();
}
get RotateMat() {
let roMat = new three.Matrix4();
switch (this._BoardType) {
case BoardType.Layer:
roMat.makeBasis(YAxis, XAxisN, ZAxis);
break;
case BoardType.Vertical:
roMat.makeBasis(YAxis, ZAxis, XAxis);
break;
case BoardType.Behind:
roMat.makeBasis(XAxis, ZAxis, YAxisN);
}
return roMat;
}
get Height() {
return this.height;
}
set Height(v) {
if (this.ContourCurve instanceof exports.Circle)
return;
if (!equaln$1(v, this.height, 1e-2)) {
this.WriteAllObjectRecord();
let refHeight = this.height / 2;
let dist = v - this.height;
let contour = Contour.CreateContour(this.ContourCurve, false);
let isSuccess = contour.UnEqualProportionScale(refHeight, dist, "y");
if (isSuccess) {
this.height = v;
this.GrooveCheckAllAutoSplit();
this.Update();
}
}
}
get Width() {
return this.width;
}
set Width(v) {
if (this.ContourCurve instanceof exports.Circle)
return;
if (!equaln$1(v, this.width, 1e-2)) {
this.WriteAllObjectRecord();
let refDist = this.width / 2;
let dist = v - this.width;
let contour = Contour.CreateContour(this.ContourCurve, false);
let isSuccess = contour.UnEqualProportionScale(refDist, dist, "x");
if (isSuccess) {
this.width = v;
this.GrooveCheckAllAutoSplit();
this.Update();
}
}
}
get BoardType() {
return this._BoardType;
}
set BoardType(type) {
this.WriteAllObjectRecord();
if (type !== this._BoardType) {
let spaceCS = this._SpaceOCS.clone();
this._BoardType = type;
this.ApplyMatrix(this.OCSInv);
this.ApplyMatrix(this.RotateMat);
this._SpaceOCS.identity();
this.ApplyMatrix(spaceCS);
this.Update();
}
}
//设置板件类型并且不做任何的事情
SetBoardType(type) {
if (type === this._BoardType)
return;
this.WriteAllObjectRecord();
this._BoardType = type;
}
//最左下角的点
get MinPoint() {
switch (this._BoardType) {
case BoardType.Layer:
return new three.Vector3(0, this.height).applyMatrix4(this.OCS);
case BoardType.Vertical:
return this.Position;
case BoardType.Behind:
return new three.Vector3(0, 0, this.thickness).applyMatrix4(this.OCS);
}
}
get MaxPoint() {
let pt = new three.Vector3(this.width, this.height, -this.thickness);
pt.applyMatrix4(this.OCS);
return pt;
}
get IsRect() {
return this.isRect;
}
get IsSpecialShape() {
return !this.isRect;
}
get HasGroove() {
return this.grooves.length > 0;
}
get Name() {
return this._Name;
}
set Name(n) {
if (n === this._Name)
return;
this.WriteAllObjectRecord();
this._Name = n;
}
/**
* 板件的轮廓,在板件坐标系中的表现方式.
*/
get ContourCurve() {
return super.ContourCurve;
}
//设置新的板件轮廓,这里重载为了保持正确的排钻封边映射
set ContourCurve(newContour) {
/**
* 保持排钻边和封边数据对应正确性
* (2x2种可能性)
* 矩形->矩形
* 矩形->异形
* 异形->异形
* 异形->矩形
*/
//上下左右排钻属性(在矩形时生效)
let rectHoleOpt = {};
ParseBoardRectHoleType(this, rectHoleOpt); //分析旧的上下左右排钻
let oldHightSealCurves = GetBoardSealingCurves(this); //旧的封边轮廓
let oldHightSealDatas = GetBoardHighSeal(this, oldHightSealCurves); //旧的封边数据
let oldHighBoardEdgeRemarkDatas = GetHighBoardEdgeRemark(this, oldHightSealCurves); //旧的封边数据
let oldContour = this.ContourCurve; //旧的轮廓
let defaultDrillType = this._BoardProcessOption.drillType;
if (!IsValidDriName(defaultDrillType) && this._BoardProcessOption.highDrill)
for (let name of this._BoardProcessOption.highDrill)
if (IsValidDriName(name)) {
defaultDrillType = name;
break;
}
if (!IsValidDriName(defaultDrillType))
defaultDrillType = HostApplicationServices.DrillConfigs.size ? HostApplicationServices.DrillConfigs.entries().next().value[0] : DrillType.None;
super.ContourCurve = newContour; //设置新的轮廓
//保持排钻边属性
if (this.isRect && rectHoleOpt.up) //矩形->矩形
SetBrHighHoleTypeFromRectHoleType(this, rectHoleOpt); //直接应用旧的矩形数据
else //异形->矩形 矩形->异形 异形->异形
{
let indexMap = [];
for (let i = 0; i < newContour.EndParam; i++) {
let p = newContour.GetPointAtParam(i + 0.5);
let cp = oldContour.GetClosestPointTo(p, false);
let cparam = oldContour.GetParamAtPoint2(cp);
indexMap.push(Math.floor(cparam));
}
let highDrill = [];
for (let index of indexMap)
highDrill.push(this._BoardProcessOption.highDrill[index] ?? defaultDrillType);
this._BoardProcessOption.highDrill = highDrill;
}
this._BoardProcessOption.highSealed.length = 0;
this._BoardProcessOption.highBoardEdgeRemark.length = 0;
//保持封边属性
if (this.isRect) {
SetBoardTopDownLeftRightSealData(this, oldHightSealDatas, oldHightSealCurves, oldContour);
SetBoardEdgeRemarkData(this, oldHighBoardEdgeRemarkDatas, oldHightSealCurves, oldContour);
}
else //变成了异形
{
let newhightSealCurves = GetBoardSealingCurves(this);
for (let i = 0; i < newhightSealCurves.length; i++) {
let newC = newhightSealCurves[i];
let p = newC.GetPointAtParam(newC.EndParam * 0.5);
let closesIndex = 0;
let closesDistance = Infinity;
for (let j = 0; j < oldHightSealCurves.length; j++) {
let oldC = oldHightSealCurves[j];
let d = oldC.GetClosestPointTo(p, false).distanceTo(p);
if (d < closesDistance) {
closesIndex = j;
closesDistance = d;
}
}
this._BoardProcessOption.highSealed.push(oldHightSealDatas[closesIndex]);
this._BoardProcessOption.highBoardEdgeRemark.push(oldHighBoardEdgeRemarkDatas[closesIndex]);
}
}
}
Explode() {
return Board2Regions(this);
}
/**
* 在不改变Normal和实体显示的情况下,修改X轴的指向
* @param xAxis
*/
SetXAxis(xAxis, isKeepLines = false) {
let ocsInv = this.OCSInv;
let x = TransformVector(xAxis.clone(), ocsInv).setZ(0).normalize();
if (equalv3(ZeroVec, x, 1e-5))
return this;
this.WriteAllObjectRecord();
let highSeals = GetBoardHighSeal(this, GetBoardSealingCurves(this));
let a = Math.atan2(x.y, x.x);
if (isKeepLines && this.BoardProcessOption.lines !== LinesType.CanReversal && (equaln$1(x.y, 1, 1e-5) || equaln$1(x.y, -1, 1e-5)))
this.BoardProcessOption.lines = 1 - this.BoardProcessOption.lines; //翻转纹路 1=>0 0=>1
x.transformDirection(this._Matrix);
let z = this.Normal;
let y = z.cross(x);
this._Matrix.elements[0] = x.x;
this._Matrix.elements[1] = x.y;
this._Matrix.elements[2] = x.z;
this._Matrix.elements[4] = y.x;
this._Matrix.elements[5] = y.y;
this._Matrix.elements[6] = y.z;
this.ContourCurve.ApplyMatrix(tempMatrix1.makeRotationZ(-a)); //复用了这个矩阵
this.CheckContourCurve();
if (this.contourCurve instanceof exports.Polyline)
this.contourCurve.UpdateOCSTo(IdentityMtx4);
SetBoardTopDownLeftRightSealData(this, highSeals); //这里不可以用缓存的曲线 否则分析错误,必须重新开始分析曲线
this.Update();
return this;
}
RotateBoard(rox, roy, roz) {
this.WriteAllObjectRecord();
this._Rotation.x = rox;
this._Rotation.y = roy;
this._Rotation.z = roz;
let spcocs = this.SpaceOCS;
let roMatX = new three.Matrix4().makeRotationX(rox);
let roMatY = new three.Matrix4().makeRotationY(roy);
let roMatZ = new three.Matrix4().makeRotationZ(roz);
this.ApplyMatrix(this.OCSInv)
.ApplyMatrix(this.RotateMat)
.ApplyMatrix(roMatX)
.ApplyMatrix(roMatY)
.ApplyMatrix(roMatZ)
.ApplyMatrix(spcocs);
this._SpaceOCS.copy(spcocs);
this.Update();
}
/**实际上这个值可能被改变,应该适当的去校验它(仅在重新设计模块时,这个值会被改变!) */
get Rotation() {
let roMtx = this.RotateMat;
let roMtxInv = roMtx.getInverse(roMtx);
let csInSpace = this.SpaceOCSInv.multiply(this.OCSNoClone); //逆到模块坐标系
csInSpace.multiply(roMtxInv); //(正确)
csInSpace.setPosition(0, 0, 0);
let euRoMtx = new three.Matrix4().makeRotationFromEuler(new three.Euler(this._Rotation.x, this._Rotation.y, this._Rotation.z, "ZYX"));
if (euRoMtx.elements.every((v, i) => equaln$1(v, csInSpace.elements[i])))
return this._Rotation;
else {
let eu = new three.Euler(0, 0, 0, "ZYX").setFromRotationMatrix(csInSpace);
return eu;
}
}
ApplyMirrorMatrix(m) {
if (!this.Id) {
// super.ApplyMirrorMatrix(m); //这个变更导致镜像错误 因为实体没有正常的被更新(更新下面的属性?). 所以需要注意的是,如果需要镜像变更,需要给实体一个id!!!
return this; //为了优化性能,在jig模式下不去计算封边排钻等属性,不重绘板
}
this.ContourCurve; //因为下面翻转孔面的代码,所以必须初始化这个
let hasSplitSize = (this.BoardProcessOption[EBoardKeyList.SpliteHeight]
&& this.BoardProcessOption[EBoardKeyList.SpliteWidth]
&& this.BoardProcessOption[EBoardKeyList.SpliteThickness]);
let highSealsCurves = GetBoardSealingCurves(this);
let highSeals = GetBoardHighSeal(this, highSealsCurves);
let isStartSealToBack = !equalv3(highSealsCurves[0].StartPoint, this.contourCurve.StartPoint, 1e-4); //第一段封边往后搜索了
super.ApplyMirrorMatrix(m);
if (this.contourCurve.Area2 < 0) {
this.contourCurve.Reverse();
highSeals.reverse();
if (isStartSealToBack) //如果第一段封边往后搜索了,那么封边在镜像后 第一段封边保持不变
highSeals.unshift(highSeals.pop());
this.BoardProcessOption.highDrill?.reverse();
if (hasSplitSize) {
[
this.BoardProcessOption.sealedUp,
this.BoardProcessOption.sealedLeft,
this.BoardProcessOption.sealedDown,
this.BoardProcessOption.sealedRight,
]
=
[
this.BoardProcessOption.sealedUp,
this.BoardProcessOption.sealedRight,
this.BoardProcessOption.sealedDown,
this.BoardProcessOption.sealedLeft,
];
}
}
else
this.BoardProcessOption[EBoardKeyList.BigHole] = 1 - this.BoardProcessOption[EBoardKeyList.BigHole]; //反转大孔面
this.BoardProcessOption.highSealed = highSeals;
if (!hasSplitSize) //&& this.isRect
SetBoardTopDownLeftRightSealData(this, highSeals);
//重新构建SpaceOCS
this._SpaceOCS.multiplyMatrices(this._Matrix, new three.Matrix4().getInverse(this.RotateMat));
//"左","右"互换
if (this.Name.includes("左"))
this.Name = this.Name.replace("左", "右");
else if (this.Name.includes("右"))
this.Name = this.Name.replace("右", "左");
//开门方向"左","右"互换
if (this.OpenDir === BoardOpenDir.Left)
this.OpenDir = BoardOpenDir.Right;
else if (this.OpenDir === BoardOpenDir.Right)
this.OpenDir = BoardOpenDir.Left;
return this;
}
get UCGenerator() {
if (this.BoardProcessOption.lines === LinesType.Positive)
return boardUVGenerator;
else
return boardUVGenerator2;
}
UpdateUV(geo, ocs, isRev = false) {
super.UpdateUV(geo, ocs, this.BoardProcessOption.lines === LinesType.Reverse);
}
//从一个实体拷贝数据,实体类型必须相同.
CopyFrom(obj) {
this.WriteAllObjectRecord();
let drillBak = this._DrillList;
this._DrillList = new Map();
let layerNailsBak = this._LayerNails;
this._LayerNails = [];
super.CopyFrom(obj);
this._DrillList = drillBak;
this._LayerNails = layerNailsBak;
}
Clone() {
let br = super.Clone();
br._DrillAssociationLock.clear();
br._DrillList.clear();
br._LayerNails.length = 0;
br.RelativeHardware.length = 0;
return br;
}
Join(target) {
let res = super.Join(target);
if (res && target.RelativeHardware) {
for (let hw of target.RelativeHardware) {
if (!this.RelativeHardware.includes(hw))
this.RelativeHardware.push(hw);
}
}
return res;
}
GetLinesDir() {
let l;
let len;
let width;
switch (this.BoardProcessOption.lines) {
case LinesType.Positive:
len = this.height / 3;
width = Math.min(this.width, this.height) / 8;
break;
case LinesType.Reverse:
len = this.width / 2;
width = Math.min(this.width, this.height) / 8;
break;
case LinesType.CanReversal:
len = this.height / 3;
width = this.width / 2;
}
l = new three.LineSegments(BufferGeometryUtils.CreateFromPts(PointShapeUtils.LinesDirPts(len, width, this.BoardProcessOption.lines)), ColorMaterial.GetLineMaterial(8));
let l1 = l.clone();
l1.material = ColorMaterial.GetLineMaterial(7);
l.position.set(this.width / 2, this.height / 2, 0);
l1.position.set(this.width / 2, this.height / 2, this.thickness);
l.updateMatrix();
l1.updateMatrix();
return [l, l1];
}
GetLinesOpenDir() {
const l = new three.Line(BufferGeometryUtils.CreateFromPts(PointShapeUtils.LinesOpenDirPts(this.height, this.width, this.OpenDir)), ColorMaterial.GetWallLineMtl(9));
l.computeLineDistances();
let l1 = l.clone();
l.position.set(this.width / 2, this.height / 2, 0);
l1.position.set(this.width / 2, this.height / 2, this.thickness);
l.updateMatrix();
l1.updateMatrix();
return [l, l1];
}
get KnifePolylineMap() {
return new Map(this._KnifePolylineMap);
}
set KnifePolylineMap(map) {
if (map.size === 0 && this._KnifePolylineMap.size === 0)
return;
this.WriteAllObjectRecord();
this._KnifePolylineMap = map;
//不进行更新 通常由其他的方法更新
}
Clear3DPathCache() {
if (!this._3DPathObject)
return;
DisposeThreeObj(this._3DPathObject);
this._3DPathObject = undefined;
}
//获取三维刀路的绘制对象
Get3DPathDrawObject() {
if (this._3DPathObject)
return this._3DPathObject;
this._3DPathObject = new three.Object3D();
let tempIndex = 1;
let tempMap = new Map();
for (let vm of this._3DModelingList) {
let key = `${vm.dir}-${vm.knife.id}`;
let color = tempMap.get(key);
if (!color) {
color = tempIndex;
tempIndex++;
tempMap.set(key, color);
}
for (let i = 0; i < vm.path.length - 1; i++) {
let d1 = vm.path[i];
let d2 = vm.path[i + 1];
if (equaln$1(d1.bul, 0)) {
let geo = BufferGeometryUtils.CreateFromPts([d1.pt, d2.pt]);
this._3DPathObject.add(new three.Line(geo, ColorMaterial.GetLineMaterial(color)));
}
else {
let arc = new exports.Arc().ParseFromBul(d1.pt, d2.pt, d1.bul);
arc.ColorIndex = color;
this._3DPathObject.add(arc.GetDrawObjectFromRenderType(RenderType.Wireframe));
}
}
}
return this._3DPathObject;
}
//#region 二维刀路缓存
//清除二维刀路的缓存
Clear2DPathCache() {
this._2DPathCsgs = undefined;
if (this._2DPathDrawObject) {
DisposeThreeObj(this._2DPathDrawObject);
this._2DPathDrawObject = undefined;
}
this.OffsetPathCache.clear();
}
/**
* 这个函数生成了二维刀路的csg数组,并且同时生成了_2DPathDrawObject(二维刀路提刀线框显示对象)
*/
Get2DPathCsgs() {
if (this._2DPathCsgs)
return this._2DPathCsgs;
this._2DPathCsgs = [];
return this._2DPathCsgs;
}
//#endregion
get Has2DPath() { return this._2DModelingList.length > 0; }
get Async2DPathing() { return this._async2DPathIng; }
GoodBye() {
super.GoodBye();
TerminateCSGTask({ key: this });
}
async Load2DPathIng() {
return new Promise((res) => {
this.promise2DPath = res;
});
}
//分裂后重新将排钻实体设置给不同的实体
HandleSpliteEntitys(splitEntitys) {
if (!splitEntitys.length)
return;
this.WriteAllObjectRecord();
//层板钉
let nails = [];
for (let nail of this._LayerNails) {
if (nail?.Object && !nail.IsErase)
nails.push(nail);
}
//如果没有开排钻反应器,那么需要重新关联排钻
//排钻列表
let dris = [];
if (!HostApplicationServices.openDrillingReactor || this.DrillLock || this.isDrillAssociationLock) {
for (let d of this._DrillList) {
dris.push(d);
//离婚,分割财产
let fbr = d[0]?.Object;
if (fbr) {
fbr.WriteAllObjectRecord();
fbr._DrillList.delete(this.Id);
}
}
this._DrillList.clear(); //开启反应器时,这个行为由排钻重排控制,没有开启时,我们暂时清空,下面会重新计算关联
}
//清除所有层板钉(因为下面会重新关联)(这个和排钻反应器没有关联,必须全部清除)
this._LayerNails.length = 0;
//所有的实体,包括自己
let ents = [this].concat(splitEntitys);
for (let en of ents) {
let ocsInv = en.OCSInv;
//把层板钉送给有缘人
nails = nails.filter(id => {
let n = id.Object;
let position = n.Position.applyMatrix4(ocsInv).setZ(0);
if (en.contourCurve.PtInCurve(position)) {
if (en === this) {
this._LayerNails.push(id);
return false;
}
//异形换位,把排钻给别人
if (n.MId === this.Id)
n.MId = en.Id;
else if (n.FId === this.Id)
n.FId = en.Id;
//新的板需要关联这个id
en._LayerNails.push(id);
return false;
}
return true;
});
//如果没有开排钻反应器,那么需要重新关联排钻
if (!HostApplicationServices.openDrillingReactor || this.DrillLock || this.isDrillAssociationLock) {
for (let d of dris) {
let [bid, drIdss] = d;
let board = bid?.Object; //另一个父亲
drIdss = drIdss.filter(ids => {
if (!ids[0]?.Object || ids[0].IsErase)
return false;
let holes = ids.map(i => i.Object);
if (holes[0] instanceof CylinderHole) {
let isInBoard = CyHoleInBoard(holes, en, en.OCSInv);
if (isInBoard) {
if (board) //重新拥有父亲的身份.
board.AppendDrillList(en.Id, [ids]);
en.AppendDrillList(bid, [ids]); //拥有新的母亲的身份
for (let h of holes) {
//成了别人的新娘
if (h.FId === this.Id)
h.FId = en.Id;
if (h.MId === this.Id)
h.MId = en.Id;
}
return false;
}
}
else {
//直接删除,毫不留情
for (let id of ids) {
let object = id?.Object;
if (object && !object.IsErase)
object.Erase();
}
return false;
}
return true;
});
d[1] = drIdss;
}
}
}
//删除无父母的排钻
for (let d of dris) {
for (let ids of d[1])
for (let id of ids) {
let object = id?.Object;
if (object && !object.IsErase)
object.Erase();
}
}
}
//绘制排版面时使用的颜色
get PlaceColor() { return this._Color === 8 ? 9 : this._Color; }
//排版面网格
GetPlaceFace() {
let shapeGeom = new three.ShapeGeometry(this.contourCurve.Shape);
let isBack = this._BoardProcessOption[EBoardKeyList.ComposingFace] !== ComposingType.Positive;
shapeGeom.applyMatrix4(this.contourCurve.OCSNoClone);
shapeGeom.translate(0, 0, isBack ? -1e-3 : this.thickness + 1e-3);
let mesh = new three.Mesh(shapeGeom, ColorMaterial.GetBasicMaterialDoubleSide(this.PlaceColor));
return mesh;
}
//大孔面网格
GetBigHoleFace() {
let shapeGeom = new three.ShapeGeometry(this.contourCurve.Shape);
let isBack = this._BoardProcessOption[EBoardKeyList.BigHole] === FaceDirection.Back;
shapeGeom.applyMatrix4(this.contourCurve.OCSNoClone);
shapeGeom.translate(0, 0, isBack ? -1e-3 : this.thickness + 1e-3);
let mesh = new three.Mesh(shapeGeom, ColorMaterial.GetBasicMaterialDoubleSide(this.PlaceColor));
return mesh;
}
/**
* 根据板的信息修改绘制的实体
* 1.非拆单板 灰色
* 2.纹路 开门方向纹路
* 3.二维 三维刀路
*/
UpdateDrawObjectByBoardInfo(renderType, obj) {
if (!obj)
return;
if (!this.IsChaiDan) //非拆单板绘制更新
{
if (renderType === RenderType.Conceptual || renderType === RenderType.Physical2) {
obj.children.length = 1;
obj.children[0].material = ColorMaterial.GrayTransparentMeshMaterial; //灰色半透明
}
else if (renderType !== RenderType.Wireframe) // renderType === RenderType.Physical(理论上只有这个)
obj.material = ColorMaterial.GrayTransparentMeshMaterial; //灰色半透明(会不会有bug?)
}
//线框及概念且可拆单
if (([RenderType.Wireframe, RenderType.Conceptual, RenderType.Print].includes(renderType)) && this.IsChaiDan) {
//绘制纹路
if (HostApplicationServices.showLines && renderType !== RenderType.Print)
obj.add(...this.GetLinesDir());
//绘制开门方向纹路
if (HostApplicationServices.showOpenDirLines && this.OpenDir !== BoardOpenDir.None)
obj.add(...this.GetLinesOpenDir());
}
if (this.objectId) //二维和三维刀路
{
//三维刀路
let path3d = this.Get3DPathDrawObject();
if (path3d.parent)
obj.children.push(path3d);
else
obj.add(path3d);
//二维刀路
this.Get2DPathCsgs();
let path2d = this._2DPathDrawObject;
if (path2d && HostApplicationServices.show2DPathLine) {
if (path2d.parent)
obj.children.push(path2d);
else
obj.add(path2d);
}
}
}
UpdateDrawObjectMaterial(renderType, obj) {
super.UpdateDrawObjectMaterial(renderType, obj);
if (renderType === RenderType.PlaceFace || renderType === RenderType.BigHoleFace) {
let face = obj.children[2];
if (!face)
return;
face.material = ColorMaterial.GetBasicMaterialDoubleSide(this.PlaceColor);
}
if (!this.IsChaiDan) {
if (renderType === RenderType.Conceptual || renderType === RenderType.Physical2) {
obj.children[0].material = ColorMaterial.GrayTransparentMeshMaterial;
}
else if (renderType !== RenderType.Wireframe) {
obj.material = ColorMaterial.GrayTransparentMeshMaterial;
}
}
}
//封边检查(为obj 对象添加封边检查的曲线)
CheckSealing(obj) {
let sealingInfo = new Map(HostApplicationServices.sealingColorMap.filter(d => d[0] && d[1]));
if (sealingInfo.size === 0)
return;
let cus = GetBoardSealingCurves(this);
let highSeals = GetBoardHighSeal(this, cus);
for (let i = 0; i < cus.length; i++) {
let size = highSeals[i].size.toString();
let color = sealingInfo.get(size);
if (color) {
cus[i].Position = cus[i].Position.add(new three.Vector3(0, 0, this.thickness / 2));
cus[i].ColorIndex = parseInt(color) ?? 7;
let l = cus[i].GetDrawObjectFromRenderType(RenderType.Wireframe);
obj.add(l);
}
}
}
GetStretchPoints() {
let pts = this.GetGripOrStretchPoints(DragPointType.Stretch);
for (let m of this._2DModelingList) {
pts.push(...m.path.GetStretchPoints().map(p => p.add(new three.Vector3(0, 0, m.dir === FaceDirection.Front ? this.thickness : 0)).applyMatrix4(this.OCS)));
}
return pts;
}
MoveStretchPoints(indexList, vec) {
let exCount = arraySum(this.GetStrectchPointCountList(DragPointType.Stretch));
let originIndexList = [];
let mIndexList = [];
for (let i of indexList) {
if (i < exCount)
originIndexList.push(i);
else
mIndexList.push(i - exCount);
}
let oldOcs = this.OCS;
super.MoveStretchPoints(originIndexList, vec);
if (!this.Id)
return;
arraySortByNumber$1(mIndexList);
let ocsInv = this.OCSInv;
let localVec = TransformVector(vec.clone(), ocsInv);
let offset = 0;
let icount = mIndexList.length;
let i = 0;
for (let m of this._2DModelingList) {
let count = m.path.GetDragPointCount(DragPointType.Stretch);
offset += count;
let iList = [];
for (; i < icount; i++) {
if (mIndexList[i] < offset)
iList.push(mIndexList[i] - offset + count);
else
break;
}
if (iList.length > 0) {
m.path.MoveStretchPoints(iList, localVec);
}
m.path.ApplyMatrix(oldOcs).ApplyMatrix(ocsInv);
}
this.Clear2DPathCache();
this.Clear3DPathCache();
this.Update(exports.UpdateDraw.Geometry);
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform, frustum) {
let pts = super.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform, frustum);
if (snapMode === ObjectSnapMode.End) {
for (let vm of this._2DModelingList) {
if (!this.OffsetPathCache.has(vm.path))
continue;
for (let item of vm.items) {
if (item.offset === 0)
continue;
let paths = this.OffsetPathCache.get(vm.path);
let polyline = paths[item.offset.toString()];
if (!polyline)
continue; //多段线可能偏移失败
let ps = polyline.GetStretchPoints();
for (let p of ps) {
if (vm.dir === FaceDirection.Front)
p.add(new three.Vector3(0, 0, this.thickness));
p.applyMatrix4(this.OCS);
}
pts.push(...ps);
}
}
}
else if (snapMode === ObjectSnapMode.Mid) {
for (let vm of this._2DModelingList) {
let ps = vm.path.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform);
for (let p of ps) {
if (vm.dir === FaceDirection.Front)
p.add(new three.Vector3(0, 0, this.thickness));
p.applyMatrix4(this.OCS);
}
pts.push(...ps);
}
}
return pts;
}
DeferUpdate() {
if (this.NeedUpdateFlag & exports.UpdateDraw.Matrix) {
if (this.RelativeHardware.some(id => !id.IsErase))
this.NeedUpdateFlag |= exports.UpdateDraw.Geometry;
}
super.DeferUpdate();
}
_ReadFile(file) {
super._ReadFile(file);
let ver = file.Read();
if (ver < 6)
this._SpaceOCS.fromArray(file.Read());
this._BoardType = file.Read();
this._Name = file.Read();
//兼容旧版本
if (ver > 2) {
deserializationBoardData(file, this._BoardProcessOption, ver);
}
else {
let opt = file.Read();
this._BoardProcessOption = Object.assign(this._BoardProcessOption, typeof opt === "string" ? JSON.parse(opt) : opt);
}
//读取排钻列表
this._DrillList.clear();
let size = file.Read();
//没有与任何板件关联的排钻
let noRelevancyDrillings = [];
for (let i = 0; i < size; i++) {
let id = file.ReadObjectId();
let drIdList = [];
let count = file.Read();
for (let i = 0; i < count; i++) {
let drIDs = [];
let count1 = file.Read();
for (let j = 0; j < count1; j++) {
let fileId = file.ReadObjectId();
fileId && drIDs.push(fileId);
}
if (drIDs.length > 0)
drIdList.push(drIDs);
}
if (drIdList.length === 0)
continue;
if (!id)
noRelevancyDrillings.push(...drIdList);
else
this._DrillList.set(id, drIdList);
}
if (noRelevancyDrillings.length > 0)
this._DrillList.set(undefined, noRelevancyDrillings);
if (ver > 1) {
this._LayerNails.length = 0;
let nailsCount = file.Read();
for (let i = 0; i < nailsCount; i++) {
let objId = file.ReadObjectId();
if (objId)
this._LayerNails.push(objId);
}
}
if (ver > 4)
this._Rotation = { x: file.Read(), y: file.Read(), z: file.Read() };
if (ver >= 7) {
let count = file.Read();
this.RelativeHardware.length = 0;
for (let i = 0; i < count; i++) {
let objId = file.ReadObjectId();
if (objId)
this.RelativeHardware.push(objId);
}
}
if (ver >= 8)
this.OpenDir = file.Read();
if (ver >= 9)
this._IsChaiDan = file.Read();
if (ver >= 10) {
DeserializationBoard2DModeingData(file, this._2DModelingList);
DeserializationBoard3DModeingData(file, this._3DModelingList);
}
this.Clear2DPathCache();
this.Clear3DPathCache();
if (ver > 10)
this._CustomNumber = file.Read();
else
this._CustomNumber = null;
this._KnifePolylineMap.clear();
if (ver > 11) {
let size = file.Read();
for (let i = 0; i < size; i++) {
let id = file.Read();
let pl = file.ReadObject();
this._KnifePolylineMap.set(id, pl);
}
}
// if (this.width === 0 || this.height === 0) //板件变成0长度,无法绘制
// this._isErase = true;
if (ver > 12) {
let count = file.Read();
this.RelativeHandle.length = 0;
for (let i = 0; i < count; i++) {
let objId = file.ReadObjectId();
if (objId)
this.RelativeHandle.push(objId);
}
}
const processData = this._BoardProcessOption;
if (ver > 13) {
processData.edgeRemarkUp = file.Read();
processData.edgeRemarkDown = file.Read();
processData.edgeRemarkLeft = file.Read();
processData.edgeRemarkRight = file.Read();
let count = file.Read();
processData.highBoardEdgeRemark = file.ReadArray(count);
}
this._DrillAssociationLock.clear();
if (ver > 14) {
this._DrillLock = file.Read();
//读取关联排钻锁映射
const size = file.Read();
for (let i = 0; i < size; i++) {
const id = file.ReadObjectId();
if (id)
this._DrillAssociationLock.add(id);
}
}
}
WriteFile(file) {
super.WriteFile(file);
file.Write(15);
// file.Write(this._SpaceOCS.toArray()); ver < 6
file.Write(this._BoardType);
file.Write(this._Name);
serializeBoardData(file, this._BoardProcessOption);
file.Write(this._DrillList.size);
for (let [id, idList] of this._DrillList) {
file.WriteObjectId(id);
file.Write(idList.length);
for (let ids of idList) {
file.Write(ids.length);
for (let id of ids)
file.WriteObjectId(id);
}
}
file.Write(this._LayerNails.length);
for (let nail of this._LayerNails)
file.WriteHardObjectId(nail);
file.Write(this._Rotation.x);
file.Write(this._Rotation.y);
file.Write(this._Rotation.z);
file.Write(this.RelativeHardware.length);
for (let id of this.RelativeHardware)
file.WriteObjectId(id);
file.Write(this.OpenDir);
file.Write(this._IsChaiDan);
SerializeBoard2DModeingData(file, this._2DModelingList);
SerializeBoard3DModeingData(file, this._3DModelingList);
file.Write(this._CustomNumber);
file.Write(this._KnifePolylineMap.size);
for (let [id, pl] of this._KnifePolylineMap) {
file.Write(id);
file.WriteObject(pl);
}
file.Write(this.RelativeHandle.length);
for (let id of this.RelativeHandle)
file.WriteObjectId(id);
const processData = this._BoardProcessOption;
file.Write(processData.edgeRemarkUp);
file.Write(processData.edgeRemarkDown);
file.Write(processData.edgeRemarkLeft);
file.Write(processData.edgeRemarkRight);
file.Write(processData.highBoardEdgeRemark.length);
for (let r of processData.highBoardEdgeRemark) {
file.Write(r);
}
//ver>14
{
file.Write(this._DrillLock);
const count = this._DrillAssociationLock.size;
file.Write(count);
for (const id of this._DrillAssociationLock) {
file.WriteObjectId(id);
}
}
}
};
__decorate([
AutoRecord
], Board.prototype, "RelativeHardware", void 0);
__decorate([
AutoRecord
], Board.prototype, "RelativeHandle", void 0);
Board = Board_1 = __decorate([
Factory
], Board);
//解析新的板的边映射到旧板边的映射情况
function ParseNewBr2OldBr_EdgeMap(newBr, oldBr, oldBrOcsInv) {
let newBrCu = newBr.ContourCurve;
let oldBrCu = oldBr.ContourCurve;
let indexMap = [];
//矩阵对齐
let m = newBr.OCS.premultiply(oldBrOcsInv);
for (let i = 0; i < newBrCu.EndParam; i++) {
let p = newBrCu.GetPointAtParam(i + 0.5).applyMatrix4(m);
let cp = oldBrCu.GetClosestPointTo(p, false);
let cparam = oldBrCu.GetParamAtPoint2(cp);
indexMap.push(Math.floor(cparam));
}
return indexMap;
}
//使用lineseg来生成拉伸的边框,避免生成过多的实体导致的drawcall次数增多
function FastWireframe(br, color = 0, divCount = 6, optArc = true) {
color = color || br.ColorIndex;
let material = ColorMaterial.GetLineMaterial(color);
let thickness = br.Thickness;
let cu = br.ContourCurve;
let pts = cu.Shape.getPoints(divCount, optArc);
let geo = new three.BufferGeometry();
let coords = [];
let edgeCoords = [];
for (let p of pts) {
coords.push(p.x, p.y, 0);
if (p["_mask_"])
edgeCoords.push(p.x, p.y, 0, p.x, p.y, thickness);
}
for (let p of pts)
coords.push(p.x, p.y, thickness);
let edgeGeo = new three.BufferGeometry();
edgeGeo.setAttribute('position', new three.Float32BufferAttribute(edgeCoords, 3));
geo.setAttribute('position', new three.Float32BufferAttribute(coords, 3));
let line = new three.Line(geo, material);
line.applyMatrix4(cu.OCSNoClone);
let edge = new three.LineSegments(edgeGeo, material);
edge.applyMatrix4(cu.OCSNoClone);
let result = [line, edge];
let ocsInv = br.OCSInv;
if (br.Grooves.length < 100)
for (let g of br.Grooves) {
let m = ocsInv.clone().multiply(g.OCSNoClone);
let lines = FastWireframe(g, color, 3, false);
for (let l of lines) {
l.applyMatrix4(m);
result.push(l);
}
}
return result;
}
function FastExtrudeEdgeGeometryOfShape(shape, z0, z1, divCount = 6, optArc = true, coords = []) {
let ptss = [shape.getPoints(divCount, optArc)];
for (let hole of shape.holes)
ptss.push(hole.getPoints(divCount, optArc));
for (let pts of ptss)
for (let i = 0; i < pts.length; i++) {
let p = pts[i];
let nextp = pts[FixIndex$1(i + 1, pts)];
//底面
coords.push(p.x, p.y, z0, nextp.x, nextp.y, z0);
//顶面
coords.push(p.x, p.y, z1, nextp.x, nextp.y, z1);
if (p["_mask_"]) //侧面
coords.push(p.x, p.y, z0, p.x, p.y, z1);
}
return coords;
}
let tempP = new three.Vector3;
//这个代码天生不和Mesh对齐,因为独立坐标系的原因,槽的坐标系可能和主题的坐标系不一致导致的
function FastExtrudeEdgeGeometry(ext, color = 0, divCount = 6, optArc = true, coords = [], inv = undefined) {
color = color || ext.ColorIndex;
let thickness = ext.Thickness;
let cu = ext.ContourCurve;
let pts = cu.Shape.getPoints(divCount, optArc);
let startIndex = coords.length / 3;
for (let i = 0; i < pts.length; i++) {
let p = pts[i];
let nextp = pts[FixIndex$1(i + 1, pts)];
//底面
coords.push(p.x, p.y, 0, nextp.x, nextp.y, 0);
//顶面
coords.push(p.x, p.y, thickness, nextp.x, nextp.y, thickness);
if (p["_mask_"]) //侧面
coords.push(p.x, p.y, 0, p.x, p.y, thickness);
}
let m = inv ? (ext.Grooves.length ? inv.clone() : inv).multiply(cu.OCSNoClone) : cu.OCSNoClone;
if (!MatrixIsIdentityCS(m)) {
let count = coords.length / 3;
for (let i = startIndex; i < count; i++) {
let a = i * 3;
let b = a + 1;
let c = a + 2;
tempP.set(coords[a], coords[b], coords[c]);
tempP.applyMatrix4(m);
coords[a] = tempP.x;
coords[b] = tempP.y;
coords[c] = tempP.z;
}
}
let ocsInv = inv ? (inv.multiply(ext.OCSInv)) : ext.OCSInv;
optArc = ext.Grooves.length < 100;
for (let g of ext.Grooves) {
FastExtrudeEdgeGeometry(g, color, divCount, optArc, coords, ocsInv.clone().multiply(g.OCSNoClone));
}
return coords;
}
function FastWireframe2(dr, color = 0) {
color = color || dr.ColorIndex;
let material = ColorMaterial.GetLineMaterial(color);
let height = dr.Height;
let cu = dr.ContourCurve;
let pts = cu.Shape.getPoints(6);
let geo = new three.BufferGeometry();
let coords = [];
let edgeCoords = [];
for (let p of pts) {
coords.push(p.x, p.y, 0);
if (p["_mask_"])
edgeCoords.push(p.x, p.y, 0, p.x, p.y, height);
}
for (let p of pts)
coords.push(p.x, p.y, height);
let edgeGeo = new three.BufferGeometry();
edgeGeo.setAttribute('position', new three.Float32BufferAttribute(edgeCoords, 3));
geo.setAttribute('position', new three.Float32BufferAttribute(coords, 3));
let line = new three.Line(geo, material);
line.applyMatrix4(cu.OCS);
let edge = new three.LineSegments(edgeGeo, material);
edge.applyMatrix4(cu.OCS);
let result = [line, edge];
return result;
}
//使用轮廓点表快速拉伸
function GenerateExtrudeEdgeGeometry(contourPoints, height) {
let pts = [];
for (let cs of contourPoints)
arrayPushArray$1(pts, GenerateExtrudeEdgeGeometryPoints(cs, height));
let geo = new three.BufferGeometry().setFromPoints(pts);
return geo;
}
//拉伸点表成为Geom
function GenerateExtrudeEdgeGeometryPoints(contourPoints, height) {
if (contourPoints.length < 3)
return [];
if (equalv3(contourPoints[0], arrayLast(contourPoints)))
contourPoints.pop();
let pts = [];
let hpts = contourPoints.map(p => new three.Vector3(p.x, p.y, height));
let count = contourPoints.length;
for (let i = 0; i < count; i++) {
pts.push(contourPoints[i], contourPoints[FixIndex$1(i + 1, count)], hpts[i], hpts[FixIndex$1(i + 1, count)], contourPoints[i], hpts[i]);
}
return pts;
}
let ExtrudeHole = class ExtrudeHole extends Hole {
constructor() {
super(...arguments);
this._contourCurve = new exports.Polyline();
this._knifeRadius = 3;
this.isHole = true;
this.isThrough = false;
}
get KnifeRadius() {
return this._knifeRadius;
}
set KnifeRadius(v) {
if (!equaln$1(v, this._knifeRadius)) {
this.WriteAllObjectRecord();
this._knifeRadius = v;
}
}
Explode() {
return [this.ContourCurve.Clone().ApplyMatrix(this.OCS)];
}
get ContourCurve() {
return this._contourCurve;
}
set ContourCurve(curve) {
if (!curve.IsClose)
return;
if (curve instanceof exports.Polyline) {
curve.CloseMark = true;
let pts = curve.LineData;
if (equalv2(pts[0].pt, arrayLast(pts).pt))
pts.pop();
//如果曲线被旋转了,那么修正它的旋转矩阵,避免纹路错误
let ocs = curve.OCS;
if (!equaln$1(ocs.elements[0], 1)) // || ocs.elements[9] || ocs.elements[10]
{
for (let p of pts)
Vector2ApplyMatrix4(ocs, p.pt);
curve.OCS = new three.Matrix4();
}
curve.ClearDraw();
}
this.WriteAllObjectRecord();
this._contourCurve = curve;
this.CheckContourCurve();
this.Update();
}
CheckContourCurve() {
let box = this._contourCurve.BoundingBox;
//修正轮廓基点
if (!equalv3(box.min, ZeroVec)) {
this._contourCurve.Position =
this._contourCurve.Position.sub(box.min);
let v = box.min.applyMatrix4(this.OCS.setPosition(ZeroVec));
this._Matrix.setPosition(this.Position.add(v));
}
}
Erase(isErase = true) {
if (isErase === this.IsErase)
return;
super.Erase(isErase);
if (!isErase)
return;
if (this.OtherHalfTongKong && !this.OtherHalfTongKong.IsErase) {
let cy = this.OtherHalfTongKong.Object;
cy.isThrough = false;
cy.OtherHalfTongKong = null;
}
}
ApplyScaleMatrix(m) {
this.WriteAllObjectRecord();
let cu = this.ContourCurve;
cu.ApplyMatrix(this.OCS);
cu.ApplyMatrix(m);
cu.ApplyMatrix(this.OCSInv);
this.CheckContourCurve();
this.Update();
return this;
}
GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform) {
switch (snapMode) {
case ObjectSnapMode.End:
return this.GetStretchPoints();
case ObjectSnapMode.Mid:
case ObjectSnapMode.Cen:
case ObjectSnapMode.Nea:
case ObjectSnapMode.Ext:
case ObjectSnapMode.Per:
case ObjectSnapMode.Tan:
{
let contour = this.ContourCurve.Clone();
contour.ApplyMatrix(this.OCS);
let pts = contour.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform);
contour.Position = contour.Position.add(this.Normal.multiplyScalar(this.Height));
pts.push(...contour.GetObjectSnapPoints(snapMode, pickPoint, lastPoint, viewXform));
if (snapMode === ObjectSnapMode.Mid)
pts.push(...contour.GetStretchPoints().map(p => p.add(this.Normal.multiplyScalar(-this.Height / 2))));
return pts;
}
}
return [];
}
get Shape() {
let contour = Contour.CreateContour(this.ContourCurve.Clone(), false);
return new Shape(contour);
}
get BoundingBoxInOCS() {
let box = new Box3Ext().copy(this.ContourCurve.BoundingBox);
box.max.add(new three.Vector3(0, 0, this.Height));
return box;
}
get BoundingBox() {
let box = this.ContourCurve.BoundingBox;
box.max.add(new three.Vector3(0, 0, this.Height));
box.applyMatrix4(this.OCSNoClone);
return box;
}
get EdgeGeometry() {
if (this._EdgeGeometry)
return this._EdgeGeometry;
let pts = [this.ContourCurve.Shape.getPoints(6).map(AsVector3)];
this._EdgeGeometry = GenerateExtrudeEdgeGeometry(pts, this.Height).applyMatrix4(this._contourCurve.OCSNoClone);
return this._EdgeGeometry;
}
get MeshGeometry() {
if (this._MeshGeometry)
return this._MeshGeometry;
this._MeshGeometry = this.GeneralMeshGeometry();
return this._MeshGeometry;
}
GeneralMeshGeometry() {
let extrudeSettings = {
curveSegments: 6,
steps: 1,
bevelEnabled: false,
depth: this.Height,
};
let geo = new three.ExtrudeGeometry(this.ContourCurve.Shape, extrudeSettings);
geo.applyMatrix4(this._contourCurve.OCSNoClone);
let mtl = this.Material?.Object ?? this.Db?.DefaultMaterial;
if (mtl?.UseWorldUV) {
let bgeo = new three.BufferGeometry().fromGeometry(geo);
let gen = new GenUVForWorld();
gen.GenGeoUV(bgeo, mtl.Material);
return bgeo;
}
else
ScaleUV(geo);
return geo;
}
GetGripOrStretchPoints(dragType) {
let isGrip = dragType === DragPointType.Grip;
let pts = isGrip ? this.ContourCurve.GetGripPoints() : this.ContourCurve.GetStretchPoints();
let v = new three.Vector3(0, 0, this.Height);
pts.push(...pts.map(p => p.clone().add(v)));
pts.forEach(p => { p.applyMatrix4(this.OCSNoClone); });
return pts;
}
GetStrectchPointCountList(dragType) {
return this.ContourCurve.GetDragPointCount(dragType) * 2;
}
MoveGripOrStretchPoints(indexList, vec, dragType) {
this.WriteAllObjectRecord();
if (dragType === DragPointType.Stretch && indexList.length === this.GetStrectchPointCountList(dragType)) {
this.Position = this.Position.add(vec);
return;
}
arraySortByNumber$1(indexList);
this.MoveGripOrStretchPointsOnly(indexList, vec, dragType);
this.CheckContourCurve();
this.Update();
}
IsStretchHeight(indexs) {
let count = this.ContourCurve.GetStretchPoints().length;
if (indexs.length === count) {
let isF = indexs[0] < count;
return indexs.every(i => isF === (i < count));
}
return false;
}
MoveGripOrStretchPointsOnly(indexList, vec, dragType) {
let stretchCount = this.ContourCurve.GetDragPointCount(dragType);
if (dragType === DragPointType.Stretch) {
//Move
if (indexList.length === stretchCount * 2) {
this.Position = this.Position.add(vec);
return;
}
//判断是否拉伸厚度
if (this.IsStretchHeight(indexList)) {
let isFront = indexList[0] < stretchCount;
if (indexList.every(v => v < stretchCount === isFront)) {
//Change thickness
let lvec = vec.clone().applyMatrix4(this.OCSInv.setPosition(ZeroVec));
if (isFront) {
this.Height -= lvec.z;
//移动位置而不改变内部拉槽
let v = this.Normal.multiplyScalar(lvec.z);
this._Matrix.elements[12] += v.x;
this._Matrix.elements[13] += v.y;
this._Matrix.elements[14] += v.z;
}
else {
this.Height += lvec.z;
}
return;
}
}
indexList = arrayClone(indexList);
}
//修正点的索引
for (let i = 0; i < indexList.length; i++) {
let index = indexList[i];
if (index >= stretchCount) {
index -= stretchCount;
indexList[i] = index;
}
}
indexList = [...new Set(indexList)];
let localVec = vec.clone().applyMatrix4(this.OCSInv.setPosition(ZeroVec));
if (dragType === DragPointType.Grip) {
if (this.ContourCurve instanceof exports.Polyline
&& indexList.length === 1
&& indexList[0] % 2 === 1) {
let param = indexList[0] / 2;
if (this.ContourCurve.GetBulgeAt(Math.floor(param)) === 0) {
let der = this.ContourCurve.GetFirstDeriv(param).normalize();
[der.x, der.y] = [der.y, -der.x];
let d = localVec.dot(der);
localVec.copy(der).multiplyScalar(d);
}
}
this.ContourCurve.MoveGripPoints(indexList, localVec);
}
else
this.ContourCurve.MoveStretchPoints(indexList, localVec);
}
GetGripPoints() {
return this.GetGripOrStretchPoints(DragPointType.Grip);
}
GetStretchPoints() {
return this.GetGripOrStretchPoints(DragPointType.Stretch);
}
MoveGripPoints(indexList, vec) {
this.MoveGripOrStretchPoints(indexList, vec, DragPointType.Grip);
}
MoveStretchPoints(indexList, vec) {
this.MoveGripOrStretchPoints(indexList, vec, DragPointType.Stretch);
}
Convert2ExtrudeSolid() {
let g = new ExtrudeSolid();
g.KnifeRadius = this.KnifeRadius;
g.SetContourCurve(this.ContourCurve);
g.Thickness = this.Height;
g.ApplyMatrix(this.OCSNoClone);
return g;
}
GetPrintObject3D() {
let geometry = new LineGeometry.LineGeometry();
let lineSegments = new Float32Array(this.EdgeGeometry.attributes.position.array);
let instanceBuffer = new three.InstancedInterleavedBuffer(lineSegments, 6, 1);
geometry.setAttribute('instanceStart', new three.InterleavedBufferAttribute(instanceBuffer, 3, 0));
geometry.setAttribute('instanceEnd', new three.InterleavedBufferAttribute(instanceBuffer, 3, 3));
let line = new Line2.Line2(geometry, ColorMaterial.PrintLineMatrial);
let mesh = new three.Mesh(this.MeshGeometry, ColorMaterial.GetPrintConceptualMaterial());
return [line, mesh];
}
InitDrawObject(renderType = RenderType.Wireframe) {
if (renderType === RenderType.Wireframe || renderType === RenderType.Edge) {
return new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(this.ColorIndex));
}
else if (renderType === RenderType.Conceptual || renderType === RenderType.Physical2) {
return new three.Object3D().add(new three.Mesh(this.MeshGeometry, ColorMaterial.GetConceptualMaterial(this.ColorIndex)), new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(7)));
}
else if (renderType === RenderType.Physical) {
let mesh = new three.Mesh(this.MeshGeometry, this.MeshMaterial);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
else if (renderType === RenderType.Jig) {
return new three.Object3D().add(...FastWireframe2(this));
}
else if (renderType === RenderType.Print) {
return new three.Object3D().add(...this.GetPrintObject3D());
}
}
UpdateDrawObject(renderType, obj) {
DisposeThreeObj(obj);
if (renderType !== RenderType.Wireframe)
Object3DRemoveAll(obj);
this._EdgeGeometry = undefined;
this._MeshGeometry = undefined;
this.MeshGeometry;
if (renderType === RenderType.Wireframe || renderType === RenderType.Edge) {
let l = obj;
l.geometry = this.EdgeGeometry;
l.material = ColorMaterial.GetLineMaterial(this.ColorIndex);
}
else if (renderType === RenderType.Print) {
obj.add(...this.GetPrintObject3D());
}
else if (renderType === RenderType.Physical) {
let mesh = obj;
mesh.geometry = this.MeshGeometry;
mesh.material = this.MeshMaterial;
}
else if (renderType === RenderType.Conceptual || renderType === RenderType.Physical2) {
obj.add(new three.Mesh(this.MeshGeometry, ColorMaterial.GetConceptualMaterial(this.ColorIndex)), new three.LineSegments(this.EdgeGeometry, ColorMaterial.GetLineMaterial(7)));
}
else if (renderType === RenderType.Jig)
obj.add(...FastWireframe2(this));
return obj;
}
UpdateDrawObjectMaterial(renderType, obj) {
if (renderType === RenderType.Wireframe || renderType === RenderType.Edge) {
let l = obj;
l.material = ColorMaterial.GetLineMaterial(this.ColorIndex);
}
else if (renderType === RenderType.Physical) {
let mesh = obj;
mesh.material = this.MeshMaterial;
}
else if (renderType !== RenderType.Jig && renderType !== RenderType.Print) {
let mesh = obj.children[0];
mesh.material = ColorMaterial.GetConceptualMaterial(this.ColorIndex);
}
}
ClearDraw() {
if (this._EdgeGeometry)
this._EdgeGeometry.dispose();
this._EdgeGeometry = undefined;
if (this._MeshGeometry)
this._MeshGeometry.dispose();
this._MeshGeometry = undefined;
return super.ClearDraw();
}
get OBB() {
let size = this.ContourCurve.BoundingBox.getSize(new three.Vector3).setZ(this.Height);
return new OBB(this.OCS, size.multiplyScalar(0.5));
}
ReadFile(file) {
super.ReadFile(file);
let ver = file.Read();
this._contourCurve = file.ReadObject();
this._knifeRadius = file.Read();
if (ver > 1) {
this.isHole = file.Read();
}
if (ver > 2)
this.isThrough = file.Read();
if (ver > 3)
this.type = file.Read();
else
this.type = GangDrillType.Ljg;
//回退版本5弃用
if (ver > 4) {
let count = file.Read();
for (let i = 0; i < count; i++)
file.ReadSoftObjectId();
}
this.Update();
}
//对象将自身数据写入到文件.
WriteFile(file) {
super.WriteFile(file);
file.Write(5);
file.WriteObject(this._contourCurve);
file.Write(this._knifeRadius);
file.Write(this.isHole);
file.Write(this.isThrough);
file.Write(this.type);
//ver= 5 弃用
file.Write(0);
}
};
__decorate([
AutoRecord
], ExtrudeHole.prototype, "isHole", void 0);
__decorate([
AutoRecord
], ExtrudeHole.prototype, "isThrough", void 0);
ExtrudeHole = __decorate([
Factory
], ExtrudeHole);
/**
* 优化走刀路径,连接偏移后的曲线数组
* @param offsetCus 偏移后的曲线组
* @param originShape 原始走刀形状
* @param rad 刀半径
* @returns tool path
*/
function OptimizeToolPath(offsetCus, originShape, rad) {
//去掉最外轮廓
let outline = offsetCus.shift();
let plList = [];
let noCloseCus = [];
for (let cu of offsetCus) {
if (!cu.IsClose) {
noCloseCus.push(cu);
continue;
}
if (cu instanceof exports.Polyline) {
//轮廓朝下的逆时针轮廓需要翻转
//第一刀为顺时针,其余为逆时针
if (cu.IsClose) {
if ((cu.Normal.z * cu.Area2 < 0) === (cu !== offsetCus[0]))
cu.Reverse();
}
plList.push(cu);
}
else if (cu instanceof exports.Circle) {
let c = ConverCircleToPolyline$1(cu);
if (offsetCus.length > 1 && cu === offsetCus[0])
c.Reverse();
c.ColorIndex = cu.ColorIndex;
plList.push(c);
}
else
console.warn("错误形状");
}
if (noCloseCus.length > 0) {
let culist = [];
noCloseCus.forEach(c => {
if (c instanceof exports.Polyline)
culist.push(...c.Explode());
else
culist.push(c);
});
//移除相等的曲线避免重复走刀
RempveEqualCurves(culist);
let groups = curveLinkGroup(culist);
for (let g of groups) {
let pl = exports.Polyline.Combine(g);
pl.ColorIndex = noCloseCus[0].ColorIndex;
plList.push(pl);
}
}
let dir = GetCurveToInDir(outline);
let cantIntCur = [outline];
cantIntCur.push(...GetOffsetCurves(outline, rad * dir));
//洞的外圈走一刀
for (let h of originShape.Holes) {
let dir = Math.sign(h.Curve.Area2);
if (h.Curve instanceof exports.Circle)
dir = 1;
cantIntCur.push(h.Curve, ...GetOffsetCurves(h.Curve, rad * dir));
}
//曲线统一起点
ChangePlListStartPt(plList);
//对多段线进行排序,按最起始点远近排序
SortPlByStartPt(plList);
let result = [];
let firstPl = plList[0];
firstPl.CloseMark = false;
for (let i = 1; i < plList.length; i++) {
let ePt = firstPl.EndPoint;
let isDisVail;
if (plList[i].TempData?.isOut && !equalv3(ePt, plList[i].StartPoint))
isDisVail = true;
else {
let refLine = new exports.Line(ePt, plList[i].StartPoint);
isDisVail = cantIntCur.some(c => c.IntersectWith(refLine, 0).length > 1);
}
if (isDisVail) {
result.push(firstPl);
firstPl = plList[i];
firstPl.CloseMark = false;
}
else {
let alMat = matrixAlignCoordSys(plList[i].OCS, firstPl.OCS);
let cuPtsBul = plList[i].PtsBuls;
for (let j = 0; j < cuPtsBul.pts.length; j++) {
//坐标系对齐
let p = cuPtsBul.pts[j];
p.copy(AsVector2(AsVector3(p).applyMatrix4(alMat)));
firstPl.LineData.push({ pt: p, bul: cuPtsBul.buls[j] });
}
}
}
result.push(firstPl);
for (let pl of result)
pl.RemoveRepeatPos(1e-3);
return result;
}
/**
* 设定走刀路径起始点
* 为了统一刀路起点,最外轮廓左左点为起始点,其余轮廓以最接近最外轮廓起始点的点左起始点
* @param plList
*/
function ChangePlListStartPt(plList) {
let firstPl = plList[0];
if (firstPl.IsClose) {
let minP = undefined;
let compare = ComparePointFnGenerate("xy");
for (let p of firstPl.GetStretchPoints()) {
if (!minP)
minP = p;
else if (compare(minP, p) === 1)
minP = p;
}
let par = firstPl.GetParamAtPoint(minP);
firstPl.ResetStartPoint(par);
}
let firstSpt = firstPl.StartPoint;
for (let i = 1; i < plList.length; i++) {
let pl = plList[i];
if (pl.IsClose) {
let pts = pl.GetStretchPoints().sort((p1, p2) => {
let dist1 = p1.distanceToSquared(firstSpt);
let dist2 = p2.distanceToSquared(firstSpt);
return dist1 - dist2;
});
let par = pl.GetParamAtPoint(pts[0]);
pl.ResetStartPoint(par);
}
else {
let sPt = pl.StartPoint;
let ePt = pl.EndPoint;
let dist1 = sPt.distanceToSquared(firstSpt);
let dist2 = ePt.distanceToSquared(firstSpt);
if (dist1 > dist2)
pl.Reverse();
}
}
}
/**
* 排序多段线数组,按照起点之间的距离
*/
function SortPlByStartPt(pls) {
if (pls.length <= 1)
return pls;
let result = [pls[0]];
let usedPl = new WeakSet([pls[0]]);
let p = pls[0].StartPoint;
while (true) {
if (pls.length === result.length)
break;
let vaildPl;
let minDist = Infinity;
for (let pl of pls) {
if (usedPl.has(pl))
continue;
let dist = pl.StartPoint.distanceToSquared(p);
if (dist < minDist) {
minDist = dist;
vaildPl = pl;
}
}
p = vaildPl.StartPoint;
result.push(vaildPl);
usedPl.add(vaildPl);
}
pls.length = 0;
pls.push(...result);
}
function RempveEqualCurves(cus) {
let needRemoveCurve = new Set();
for (let i = 0; i < cus.length; i++) {
let cu1 = cus[i];
if (needRemoveCurve.has(cu1))
continue;
for (let j = i + 1; j < cus.length; j++) {
let cu2 = cus[j];
if (needRemoveCurve.has(cu2))
continue;
if (equalCurve(cu1, cu2)) {
needRemoveCurve.add(cu2);
}
}
}
arrayRemoveIf(cus, (c) => needRemoveCurve.has(c));
}
/**获取内偏移的轮廓 */
function GetOffsetCurves(cu, dist, rectInfo) {
if (cu instanceof exports.Polyline) {
if (rectInfo) {
let r = RectOffset(cu, rectInfo, Math.abs(dist));
return r ? [r] : [];
}
return cu.GetFeedingToolPath(dist).filter(c => !equaln$1(c.Length, 0, 1e-5));
}
else
return cu.GetOffsetCurves(dist);
}
/** 获得曲线内偏移方向*/
function GetCurveToInDir(cu) {
return cu.IsClockWise ? 1 : -1;
}
/**矩形偏移,正为内偏移 */
function RectOffset(rect, rectInfo, dist) {
if (!rectInfo || equaln$1(dist, 0))
return;
let box = rectInfo.box;
let size = rectInfo.size;
let min = box.min.clone();
let max = box.max.clone();
if (dist > Math.min(size.x, size.y) / 2 + 1e-2)
return;
if (equaln$1(size.x / 2, dist, 1e-5)) {
let x = (box.min.x + box.max.x) * 0.5;
let sPt = new three.Vector3(x, box.min.y + dist);
let ePt = new three.Vector3(x, box.max.y - dist);
return new exports.Polyline([{ pt: AsVector2(sPt), bul: 0 }, { pt: AsVector2(ePt), bul: 0 }]).ApplyMatrix(rectInfo.OCS);
}
else if (equaln$1(size.y / 2, dist, 1e-5)) {
let y = (box.min.y + box.max.y) * 0.5;
let sPt = new three.Vector3(box.min.x + dist, y);
let ePt = new three.Vector3(box.max.x - dist, y);
return new exports.Polyline([{ pt: AsVector2(sPt), bul: 0 }, { pt: AsVector2(ePt), bul: 0 }]).ApplyMatrix(rectInfo.OCS);
}
else {
min.add(new three.Vector3(dist, dist));
max.add(new three.Vector3(-dist, -dist));
return new exports.Polyline().RectangleFrom2Pt(min, max).ApplyMatrix(rectInfo.OCS);
}
}
/**
*计算走刀工具类
*/
class FeedingToolPath extends Singleton {
/**
* 处理形状,内偏移
* @param shape 造型Shape
* @param knifRadius 刀半径/偏移距离
* @param [isOut=true] 是否是最外轮廓,如果是,洞需要外偏移一个刀半径,多段线偏移保留不闭合轮廓
*/
HandleShape(shape, knifRadius, isOut, redundancyKnif = 0) {
let outline = shape.Outline.Curve;
if (isOut)
outline = outline.Clone();
let dir = GetCurveToInDir(outline);
let offsetCus = [outline];
//获得形状外孔轮廓
let holes = [];
/**用于判断孤岛是否与外轮廓相交 */
let holeOffsetCus = [];
for (let h of shape.Holes) {
if (!isOut)
holes.push(h.Clone());
else {
let dir = -GetCurveToInDir(h.Curve);
let cus;
if (h.Curve instanceof exports.Circle)
cus = h.Curve.GetOffsetCurves(knifRadius * dir);
else
cus = h.Curve.GetFeedingToolPath(knifRadius * dir);
holeOffsetCus.push(...h.Curve.GetOffsetCurves(knifRadius * dir).filter(c => c.IsClose));
holes.push(...this.GetContours(cus, offsetCus));
}
}
let offsetDist = 0;
let rectInfo = IsRect(outline);
while (true) {
if ((!isOut || offsetDist >= knifRadius) && rectInfo) {
offsetDist += knifRadius * 2 - redundancyKnif;
}
else
offsetDist += knifRadius;
//理论上最大的宽度为1220,所以2000已经是种仁慈.
//更好的算法应该是, 如果线不在outline里面, 那么已经算是错误的,但是理论上如果线已经往外偏太多了,就一定会使得这个判断生效
if (offsetDist > 2000)
throw "无法加工的造型!已经超过了最大的走刀范围2000!";
let retCus = [];
let tempOffsetCus = GetOffsetCurves(outline, offsetDist * dir, rectInfo);
retCus.push(...tempOffsetCus);
//最后一次内偏移如果是矩形
if (retCus.length === 0 && rectInfo && offsetDist > knifRadius) {
const rectMinLengthHalf = Math.min(rectInfo.size.x, rectInfo.size.y) / 2;
//如果最后一个矩形最小边区间一半小于刀半径减去冗余值的一半,则偏移到中心处切一次
if (!equaln$1(offsetDist, rectMinLengthHalf, 1e-5) && offsetDist - rectMinLengthHalf - 1e-5 < knifRadius - redundancyKnif / 2) {
retCus.push(...GetOffsetCurves(outline, rectMinLengthHalf * dir, rectInfo));
}
}
if (retCus.length === 0)
break;
//是否和孤岛相交
let isInt = false;
for (let c of retCus) {
if (holes.length > 0) {
isInt = holes.some(h => {
let ipts = h.Curve.IntersectWith2(c, 0);
return ipts.length > 0 || h.ContainerCurve(c, false, ipts);
});
if (isInt)
break;
}
if (isOut && offsetDist === knifRadius)
c.TempData = { isOut: true };
offsetCus.push(c);
}
if (isInt) {
//洞形状管理器
let holesMg = new ShapeManager();
if (isOut)
holes = Shape.mergeContours(holes, false); //#I1MUQD 正好擦边的孔不合并
holesMg.AppendShapeList(holes.map(h => new Shape(h)));
let shapeMg = new ShapeManager();
let cons = this.GetContours(retCus, offsetCus);
shapeMg.AppendShapeList(cons.map(c => new Shape(c)));
//testcode
// let reg = new Region(shapeMg).Clone();
// let reg2 = new Region(holesMg).Clone();
// TestDraw(reg, 1);
// TestDraw(reg2, 2);
shapeMg.BoolOper(holesMg, exports.BoolOpeartionType.Subtract);
// let reg3 = new Region(shapeMg).Clone();
// TestDraw(reg3, 3);
for (let s of shapeMg.ShapeList) {
if (isOut && tempOffsetCus.length > 1)
s.Outline.Curve.TempData = { isOut: true };
offsetCus.push(...this.HandleShape(s, knifRadius, false));
}
break;
}
}
let vailHoles = [];
//内偏(走刀方式)
let outlineOffsetCusOfTool = GetOffsetCurves(outline, dir * knifRadius).filter(c => c.IsClose);
let maxArea = Math.max(...(outlineOffsetCusOfTool.map(c => c.Area)));
for (let i = 0; i < holes.length; i++) {
let h = holes[i];
//如果加工洞外圈和最外轮廓相交,则去掉
if (h.Curve.IntersectWith(outline, 0).length > 0)
continue;
let isVail = true;
//若最外轮廓内偏移一个刀半径的曲线 和最内轮廓相交或者被包含,则去掉.且不与洞曲线相等
if (isOut) {
let holeOffsetCurve = holeOffsetCus[i]; //网洞走刀曲线
for (let j = 0; j < outlineOffsetCusOfTool.length; j++) {
let outlineCurveOffsetInside = outlineOffsetCusOfTool[j];
if (h.Curve.IntersectWith(outlineCurveOffsetInside, 0).length > 0) {
if (!(equalCurve(holeOffsetCurve, outlineCurveOffsetInside) || isTargetCurInOrOnSourceCur(outlineCurveOffsetInside, h.Curve))) {
isVail = false;
break;
}
else if (isTargetCurInOrOnSourceCur(h.Curve, outlineCurveOffsetInside)) {
offsetCus.push(outlineCurveOffsetInside);
isVail = false;
break;
}
}
else if (holeOffsetCurve.Area > maxArea) {
isVail = false;
break;
}
}
}
if (isVail)
vailHoles.push(h);
}
offsetCus.push(...vailHoles.map(h => h.Curve));
return offsetCus;
}
/**用于测试走刀路径 */
TestCalcPath(br, isCd = false, rk = 0) {
let modelings = br.BoardModeling;
let allModeling = GetModelingFromCustomDrill(br);
modelings.push(...allModeling.modeling);
if (isCd && HostApplicationServices.chaidanOption.useDefaultRad)
modelings.forEach(m => m.knifeRadius = HostApplicationServices.chaidanOption.radius);
if (isCd)
arrayRemoveIf(modelings, m => {
let c = m.shape.Outline.Curve;
if (m.shape.Holes.length === 0 && c instanceof exports.Circle && c.Radius < HostApplicationServices.chaidanOption.modeling2HoleRad + 1e-6)
return true;
return false;
});
return this.CalcPath(modelings, br, rk);
}
/**
* 计算走刀路径
*/
CalcPath(modelings, br, rk = 0) {
let cus = [];
for (let m of modelings) {
cus.push(...this.GetModelFeedPath(br, m, rk));
}
return cus;
}
//获取造型走刀
GetModelFeedPath(br, modeling, redundancyKnif = 0) {
const brThickness = br.Thickness;
let cus = []; //返回走刀路径
let { shape, thickness, knifeRadius, addLen, addWidth, addDepth } = modeling;
if (!knifeRadius)
knifeRadius = 3;
if (addDepth)
thickness += addDepth;
if (thickness < 1e-5)
return cus;
shape = shape.Clone();
shape.Z0();
this.GrooveAddSize(shape, addLen, addWidth);
this.HandleThoughGroove(br.ContourCurve, shape, knifeRadius);
//造型半径和刀半径相等,返回重合点的线
let outline = shape.Outline.Curve;
if (outline instanceof exports.Circle && equaln$1(outline.Radius, modeling.knifeRadius))
return [new exports.Polyline([{ pt: AsVector2(outline.Center), bul: 0 }, { pt: AsVector2(outline.Center), bul: 0 }])];
if (thickness >= brThickness) //通孔只切一刀
{
let dir = GetCurveToInDir(outline);
let paths;
if (outline instanceof exports.Circle)
outline = ConverCircleToPolyline$1(outline);
paths = outline.GetFeedingToolPath(dir * knifeRadius);
for (let path of paths) {
if (dir < 0)
path.Reverse();
// 有些走刀会变成一条线,或者某些地方退化成线,这个时候这个判断是错误的
// if (!path.IsClockWise)
// throw "程序错误:全深网洞加工数据并不为逆时针!";
}
cus.push(...paths);
}
else {
let offsetCus = this.HandleShape(shape, knifeRadius, true, redundancyKnif);
if (offsetCus.length > 1)
cus.push(...OptimizeToolPath(offsetCus, shape, knifeRadius));
}
return cus;
}
GrooveAddSize(shape, addLen, addWidth) {
shape.Outline.Curve.Position = shape.Outline.Curve.Position.setZ(0);
//若是矩形,应用槽加长
if (addLen > 0 || addWidth > 0) {
let rectInfo = IsRect(shape.Outline.Curve);
if (rectInfo) {
let box = rectInfo.box;
let size = rectInfo.size;
if (size.x > size.y) {
box.max.add(new three.Vector3(addLen / 2, addWidth / 2));
box.min.add(new three.Vector3(-addLen / 2, -addWidth / 2));
}
else {
box.max.add(new three.Vector3(addWidth / 2, addLen / 2));
box.min.add(new three.Vector3(-addWidth / 2, -addLen / 2));
}
let pl = new exports.Polyline().RectangleFrom2Pt(box.min, box.max).ApplyMatrix(rectInfo.OCS);
shape.Outline = Contour.CreateContour(pl);
}
}
}
/**
* 获取所有的轮廓
* @param cus
* @param retCus 不能组成轮廓的线被添加到这里
* @returns
*/
GetContours(cus, retCus) {
let cons = [];
for (let c of cus) {
if (c.IsClose) {
cons.push(Contour.CreateContour(c));
}
else {
let expCus = c.Explode();
let regParse = new RegionParse(expCus);
//分析封闭包围区域
const parseRoute = (routeSet) => {
for (let routes of routeSet) {
let cs = routes.map(r => r.curve);
let c = Contour.CreateContour(cs, false);
if (c && c.Area > 1e-3)
cons.push(c);
}
};
parseRoute(regParse.RegionsOutline);
parseRoute(regParse.RegionsInternal);
for (let c of expCus) {
if (!regParse.GetCueveUsed(c)) {
retCus.push(c);
}
}
}
}
return cons;
}
CheckModeling(br) {
let errorIndexs = [];
let modelings = br.BoardModeling;
for (let i = 0; i < modelings.length; i++) {
if (HostApplicationServices.chaidanOption.useDefaultRad)
modelings[i].knifeRadius = HostApplicationServices.chaidanOption.radius;
let m = modelings[i];
let cu = m.shape.Outline.Curve;
if (m.shape.Holes.length === 0 && cu instanceof exports.Circle && cu.Radius < HostApplicationServices.chaidanOption.modeling2HoleRad + 1e-6)
continue;
let cus = this.GetModelFeedPath(br, m);
if (cus.length === 0)
errorIndexs.push(i);
}
return errorIndexs;
}
CheckCustomHole(br) {
let { modeling, sideModeling } = GetModelingFromCustomDrill(br);
let errHoles = [];
for (let m of [...modeling, ...sideModeling]) {
let cu = m.shape.Outline.Curve;
if (m.shape.Holes.length === 0 && cu instanceof exports.Circle && cu.Radius < HostApplicationServices.chaidanOption.modeling2HoleRad + 1e-6)
continue;
if (HostApplicationServices.chaidanOption.useDefaultRad)
m.knifeRadius = HostApplicationServices.chaidanOption.radius;
let cus = this.GetModelFeedPath(br, m);
if (cus.length === 0)
errHoles.push(m.originEn);
}
return errHoles;
}
//当槽与板的边缘重叠时,因为刀的圆形加工特性,这里将造型加大,使得走刀的路径和板边缘平行
HandleThoughGroove(brCon, shape, knifeRadius) {
let outline = shape.Outline.Curve;
if (outline instanceof exports.Circle)
return;
let cus = outline.Explode();
MergeCurvelist(cus);
let hasChange = false;
let curveBak;
for (let i = 0; i < cus.length; i++) {
let c = cus[i];
if (c instanceof exports.Line) {
let mp = (curveBak ?? c).Midpoint;
curveBak = undefined;
let param = brCon.GetParamAtPoint(mp);
let curveOnContour = false;
if (brCon.ParamOnCurve(param)) {
//#I7MYN9 在长的槽里面,我们防止它加长,短的槽就无所谓了
if (c.Length > 100) {
let curve = brCon.GetCurveAtParam(param);
curveOnContour = curve.PtOnCurve(c.GetPointAtParam(0.2), 0.1) && curve.PtOnCurve(c.GetPointAtParam(0.8), 0.1);
}
else
curveOnContour = true;
}
if (curveOnContour) {
hasChange = true;
let cs = c.GetOffsetCurves(knifeRadius);
cus[i] = cs[0];
let fline = cus[FixIndex(i - 1, cus.length)];
let isAddLine = false;
if (fline instanceof exports.Line) {
let intPts = fline.IntersectWith2(cs[0], 3);
if (intPts.length === 0) {
console.error("未知错误情况");
return;
}
if (intPts[0].thisParam >= 0 && intPts[0].argParam <= 1) {
fline.EndPoint = intPts[0].pt;
cs[0].StartPoint = intPts[0].pt;
}
else {
isAddLine = true;
}
}
else {
isAddLine = true;
}
if (isAddLine) {
let newLine = new exports.Line(fline.EndPoint, cs[0].StartPoint);
if (i === 0) {
cus.push(newLine);
}
else {
cus.splice(i, 0, newLine);
i++;
}
}
let backLine = cus[FixIndex(i + 1, cus.length)];
isAddLine = false;
if (backLine instanceof exports.Line) {
let intPts = backLine.IntersectWith2(cs[0], 3);
if (intPts.length === 0) {
return;
}
if (intPts[0].thisParam <= 1 && intPts[0].argParam >= 0) {
curveBak = backLine.Clone();
backLine.StartPoint = intPts[0].pt;
cs[0].EndPoint = intPts[0].pt;
}
else {
isAddLine = true;
}
}
else {
isAddLine = true;
}
if (isAddLine) {
let newLine = new exports.Line(cs[0].EndPoint, backLine.StartPoint);
if (i + 1 === cus.length) {
cus.unshift(newLine);
}
else {
cus.splice(i + 1, 0, newLine);
i++;
}
}
}
}
}
if (hasChange) {
let con = Contour.CreateContour(exports.Polyline.Combine(cus));
if (con)
shape.Outline = con;
else
console.error("在造型走刀时构建轮廓失败了!(未知情况,未处理)");
}
}
}
function GetModelingFromCustomDrill(br) {
let normal = br.Normal;
let outline = GetBoardContour(br);
let modeling = [];
let sideModeling = [];
const holes = [];
let bbox = br.BoundingBoxInOCS;
let holeBoxMap = new WeakMap();
for (let [, idss] of br.DrillList) {
for (let ids of idss) {
for (let id of ids) {
if (id?.Object && !id.Object.IsErase && id.Object instanceof ExtrudeHole && id.Object.isHole) {
if (!(id.Object.ContourCurve instanceof exports.Circle)) {
let en = id.Object;
let enBox = en.GetBoundingBoxInMtx(br.OCSInv);
holeBoxMap.set(en, enBox);
if (enBox.clone().intersect(bbox).isSolid(0.1))
holes.push(id.Object);
}
}
else
break;
}
}
}
for (let en of holes) {
let box = holeBoxMap.get(en);
let max = box.max;
let min = box.min;
let dir;
let shape = en.Shape;
let diff = br.OCSInv.multiply(en.OCS);
shape.ApplyMatrix(diff);
let thickness;
if (isParallelTo(normal, en.Normal)) {
if (min.z > br.Thickness - 1e-6)
continue;
//在板件的世界,0.01的误差应该不能被看出来,所以我们允许0.01的容差(这样应该是没问题的)
//也避免了一些二维转三维出现的缝隙排钻不能被拆解的问题
if (max.z >= br.Thickness - CanDrawHoleFuzz) {
dir = FaceDirection.Front;
shape.Position = shape.Position.setZ(min.z);
thickness = br.Thickness - min.z;
}
else if (min.z < CanDrawHoleFuzz) {
dir = FaceDirection.Back;
thickness = max.z;
}
else
continue;
if (thickness > 1e-6 && isTargetCurInOrOnSourceCur(outline, shape.Outline.Curve.Clone().Z0())) {
modeling.push({
shape,
thickness,
dir,
knifeRadius: en.KnifeRadius,
addLen: 0,
originEn: en,
});
}
}
else {
if (min.z <= 0 || max.z >= br.Thickness)
continue;
let spt = en.Position.applyMatrix4(br.OCSInv).setZ(0);
if (outline.PtOnCurve(spt))
continue;
let line = new exports.Line(spt, en.Position.add(en.Normal.multiplyScalar(en.Height)).applyMatrix4(br.OCSInv).setZ(0));
let pt = outline.IntersectWith(line, 0)[0];
if (!pt)
continue;
let index = Math.floor(outline.GetParamAtPoint(pt));
let thickness = line.StartPoint.distanceTo(pt);
let shape = en.Shape.ApplyMatrix(en.OCS).ApplyMatrix(br.OCSInv);
let vec = line.GetFirstDeriv(0).normalize().multiplyScalar(thickness);
shape.Position = shape.Position.add(vec);
//侧面造型仅在多段线直线上
let cu = outline.GetCurveAtIndex(index);
shape.ApplyMatrix(new three.Matrix4().getInverse(GetSideFaceMtx(cu)));
sideModeling.push({
shape,
thickness,
dir: index,
knifeRadius: en.KnifeRadius,
addLen: 0,
originEn: en,
});
}
}
return { modeling, sideModeling };
}
/**
* V型刀走刀数据,第一刀直接扎进去,最后一刀提刀
* @param polyline
* @param feedingDepth 走刀深度
* @param knifAngle 通常为60度.按弧度表示
*/
function VKnifToolPath(polyline, feedingDepth, knifAngle) {
let x = Math.abs(feedingDepth * Math.tan(knifAngle));
let cus = polyline.Explode();
arrayRemoveIf(cus, c => c.Length < 0.01);
let ptsbul = [];
let isClose = polyline.IsClose;
for (let i = 0; i < cus.length; i++) {
let nextIndex = FixIndex$1(i + 1, cus.length);
let c1 = cus[i];
let c2 = cus[nextIndex];
let d = { pt: c1.StartPoint, bul: 0 };
let curP = c1.EndPoint;
if (c1 instanceof exports.Arc) {
d.bul = c1.Bul;
c1 = new exports.Line(curP.clone().sub(c1.GetFirstDeriv(1).multiplyScalar(100)), curP.clone());
}
if (c2 instanceof exports.Arc)
c2 = new exports.Line(curP.clone(), curP.clone().add(c2.GetFirstDeriv(0).multiplyScalar(100)));
ptsbul.push(d);
if (!isClose && i === cus.length - 1) //最后一条
{
ptsbul.push({ pt: c1.EndPoint, bul: 0 });
break;
}
//圆弧与直线相切,此时不要提刀
if (isParallelTo(c1.GetFirstDeriv(0), c2.GetFirstDeriv(0)))
continue;
//计算提刀部分:
//向量与平分线,参照倒角代码
let derv1 = c1.GetFirstDeriv(0).normalize();
let derv2 = c2.GetFirstDeriv(0).normalize();
let bisectorVec = derv1.clone().negate().add(derv2).multiplyScalar(0.5);
let co1 = c1.GetOffsetCurves(x * Math.sign(derv1.cross(bisectorVec).z))[0];
let co2 = c2.GetOffsetCurves(x * Math.sign(derv2.cross(bisectorVec).z))[0];
if (!co1 || !co2)
continue;
let ipts = co1.IntersectWith(co2, IntersectOption.ExtendBoth);
if (ipts.length === 0)
continue;
if (co1.PtOnCurve(ipts[0]))
continue;
//抬刀路径
ptsbul.push({ pt: curP, bul: 0 });
ptsbul.push({ pt: ipts[0].setZ(feedingDepth), bul: 0 });
}
if (isClose) {
//第一刀
ptsbul.unshift(ptsbul[ptsbul.length - 1]); //, ptsbul[ptsbul.length - 2]
}
return ptsbul;
}
function VData2Curve(data) {
let curves = [];
for (let i = 0; i < data.length - 1; i++) {
let p1 = new three.Vector3(data[i].pt.x, data[i].pt.y, data[i].pt.z);
let p2 = new three.Vector3(data[i + 1].pt.x, data[i + 1].pt.y, data[i + 1].pt.z);
if (equaln$1(data[i].bul, 0)) {
curves.push(new exports.Line(p1, p2));
}
else {
curves.push(new exports.Arc().ParseFromBul(p1, p2, data[i].bul));
}
}
return curves;
}
/**
* 转换成多段线点表,并且将圆弧打断(大于1/4的话) API仅用于开料生产
* @param cu 多段线或者圆弧
* @param [isOutline=true] 如果为外部轮廓,则将其转换为逆时针
* @param [isSplite=true] 如果为true,则对大圆弧进行切割
* @returns 点表+凸度表
*/
function ConverToPolylineAndSplitArc(cu, isOutline = true, isSplite = true) {
let ptsBuls;
if (cu instanceof exports.Circle) {
let pl = ConverCircleToPolyline(cu);
ptsBuls = pl.PtsBuls;
}
else {
if (isOutline && cu.IsClose && cu.Normal.z * cu.Area2 < 0)
cu.Reverse();
if (isSplite)
ptsBuls = SplitePolylineAtArc(cu);
else
ptsBuls = cu.PtsBuls;
}
let ocs = cu.OCSNoClone;
if (!equaln$1(ocs.elements[0], 1)
|| !equaln$1(ocs.elements[9], 0)
|| !equaln$1(ocs.elements[10], 0)) {
for (let i = 0; i < ptsBuls.pts.length; i++) {
Vector2ApplyMatrix4(ocs, ptsBuls.pts[i]);
ptsBuls.buls[i] *= cu.Normal.z;
}
}
return ptsBuls;
}
function ConverCircleToPolyline(cir) {
let arcs = cir.GetSplitCurves([0, 0.25, 0.5, 0.75]);
let pl = new exports.Polyline();
pl.OCS = cir.OCSNoClone;
for (let arc of arcs)
pl.Join(arc);
return pl;
}
const SPLITBUL = Math.tan(Math.PI / 8);
function GetSpliteCount(allAngle) {
return Math.ceil(Math.abs(allAngle) / Math.PI * 2);
}
/** 打断多段线超过1/4圆的圆弧*/
function SplitePolylineAtArc(cu) {
let ptsBuls = cu.PtsBuls;
let ocsInv = cu.OCSInv;
let result = { pts: [], buls: [] };
if (ptsBuls.pts.length === 0)
return result;
for (let i = 0; i < ptsBuls.buls.length - 1; i++) {
let bul = ptsBuls.buls[i];
if (Math.abs(bul) > SPLITBUL + 1e-8) {
let allAngle = Math.atan(bul) * 4;
let splitCount = GetSpliteCount(allAngle);
let arc = cu.GetCurveAtIndex(i);
let paramDiv = 1 / splitCount;
let newBul = Math.tan((allAngle / splitCount) / 4);
for (let i = 0; i < splitCount; i++) {
let param = i * paramDiv;
let p = arc.GetPointAtParam(param).applyMatrix4(ocsInv);
let p2 = AsVector2(p);
//暂时不处理0长度段
{
result.pts.push(p2);
result.buls.push(newBul);
}
}
}
else {
//暂时不处理0长度段
{
result.pts.push(ptsBuls.pts[i]);
result.buls.push(ptsBuls.buls[i]);
}
}
}
result.pts.push(arrayLast(ptsBuls.pts));
result.buls.push(arrayLast(ptsBuls.buls));
return result;
}
//点表面积
function Area(pts) {
let cnt = pts.length;
if (cnt < 3)
return 0;
let a = 0;
for (let i = 0, j = cnt - 1; i < cnt; ++i) {
a += (pts[j].x + pts[i].x) * (pts[j].y - pts[i].y);
j = i;
}
return -a * 0.5;
}
function Path2Polyline(path) {
let pl = new exports.Polyline();
pl.LineData = path.map(p => {
return { pt: AsVector2(p), bul: 0 };
});
pl.CloseMark = true;
return pl;
}
class Vector2 {
constructor(x = 0, y = 0) {
this.isVector2 = true;
this.x = x;
this.y = y;
}
get width() { return this.x; }
set width(value) { this.x = value; }
get height() { return this.y; }
set height(value) { this.y = value; }
set(x, y) {
this.x = x;
this.y = y;
return this;
}
setScalar(scalar) {
this.x = scalar;
this.y = scalar;
return this;
}
setX(x) {
this.x = x;
return this;
}
setY(y) {
this.y = y;
return this;
}
setComponent(index, value) {
switch (index) {
case 0:
this.x = value;
break;
case 1:
this.y = value;
break;
default: throw new Error('index is out of range: ' + index);
}
return this;
}
getComponent(index) {
switch (index) {
case 0: return this.x;
case 1: return this.y;
default: throw new Error('index is out of range: ' + index);
}
}
clone() {
return new this.constructor().copy(this);
}
copy(v) {
this.x = v.x;
this.y = v.y;
return this;
}
add(v) {
this.x += v.x;
this.y += v.y;
return this;
}
addScalar(s) {
this.x += s;
this.y += s;
return this;
}
addVectors(a, b) {
this.x = a.x + b.x;
this.y = a.y + b.y;
return this;
}
addScaledVector(v, s) {
this.x += v.x * s;
this.y += v.y * s;
return this;
}
sub(v) {
this.x -= v.x;
this.y -= v.y;
return this;
}
subScalar(s) {
this.x -= s;
this.y -= s;
return this;
}
subVectors(a, b) {
this.x = a.x - b.x;
this.y = a.y - b.y;
return this;
}
multiply(v) {
this.x *= v.x;
this.y *= v.y;
return this;
}
multiplyScalar(scalar) {
if (isFinite(scalar)) {
this.x *= scalar;
this.y *= scalar;
}
else {
this.x = 0;
this.y = 0;
}
return this;
}
divide(v) {
this.x /= v.x;
this.y /= v.y;
return this;
}
divideScalar(scalar) {
return this.multiplyScalar(1 / scalar);
}
min(v) {
this.x = Math.min(this.x, v.x);
this.y = Math.min(this.y, v.y);
return this;
}
max(v) {
this.x = Math.max(this.x, v.x);
this.y = Math.max(this.y, v.y);
return this;
}
clamp(min, max) {
// This function assumes min < max, if this assumption isn't true it will not operate correctly
this.x = Math.max(min.x, Math.min(max.x, this.x));
this.y = Math.max(min.y, Math.min(max.y, this.y));
return this;
}
clampScalar(minVal, maxVal) {
const min = Vector2.clampScalar_min.set(minVal, minVal);
const max = Vector2.clampScalar_max.set(maxVal, maxVal);
return this.clamp(min, max);
}
clampLength(min, max) {
const length = this.length();
return this.multiplyScalar(Math.max(min, Math.min(max, length)) / length);
}
floor() {
this.x = Math.floor(this.x);
this.y = Math.floor(this.y);
return this;
}
ceil() {
this.x = Math.ceil(this.x);
this.y = Math.ceil(this.y);
return this;
}
round() {
this.x = Math.round(this.x);
this.y = Math.round(this.y);
return this;
}
roundToZero() {
this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
return this;
}
negate() {
this.x = -this.x;
this.y = -this.y;
return this;
}
dot(v) {
return this.x * v.x + this.y * v.y;
}
lengthSq() {
return this.x * this.x + this.y * this.y;
}
length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
lengthManhattan() {
return Math.abs(this.x) + Math.abs(this.y);
}
normalize() {
return this.divideScalar(this.length());
}
angle() {
// computes the angle in radians with respect to the positive x-axis
let angle = Math.atan2(this.y, this.x);
if (angle < 0)
angle += 2 * Math.PI;
return angle;
}
distanceTo(v) {
return Math.sqrt(this.distanceToSquared(v));
}
distanceToSquared(v) {
const dx = this.x - v.x, dy = this.y - v.y;
return dx * dx + dy * dy;
}
distanceToManhattan(v) {
return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
}
setLength(length) {
return this.multiplyScalar(length / this.length());
}
lerp(v, alpha) {
this.x += (v.x - this.x) * alpha;
this.y += (v.y - this.y) * alpha;
return this;
}
lerpVectors(v1, v2, alpha) {
return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
}
equals(v) {
return ((v.x === this.x) && (v.y === this.y));
}
fromArray(array, offset = 0) {
this.x = array[offset];
this.y = array[offset + 1];
return this;
}
toArray(array = [], offset = 0) {
array[offset] = this.x;
array[offset + 1] = this.y;
return array;
}
fromAttribute(attribute, index, offset = 0) {
index = index * attribute.itemSize + offset;
this.x = attribute.array[index];
this.y = attribute.array[index + 1];
return this;
}
rotateAround(center, angle) {
const c = Math.cos(angle), s = Math.sin(angle);
const x = this.x - center.x;
const y = this.y - center.y;
this.x = x * c - y * s + center.x;
this.y = x * s + y * c + center.y;
return this;
}
}
Vector2.clampScalar_min = new Vector2();
Vector2.clampScalar_max = new Vector2();
class Box2 {
constructor(min = new Vector2(+Infinity, +Infinity), max = new Vector2(-Infinity, -Infinity)) {
this.min = min;
this.max = max;
}
get area() {
return (this.max.x - this.min.x) * (this.max.y - this.min.y);
}
set(min, max) {
this.min.copy(min);
this.max.copy(max);
return this;
}
setFromPoints(points) {
this.makeEmpty();
for (let p of points) {
this.expandByPoint(p);
}
return this;
}
setFromCenterAndSize(center, size) {
const v1 = Box2._setFromCenterAndSize_v1;
const halfSize = v1.copy(size).multiplyScalar(0.5);
this.min.copy(center).sub(halfSize);
this.max.copy(center).add(halfSize);
return this;
}
clone() {
return new this.constructor().copy(this);
}
copy(box) {
this.min.copy(box.min);
this.max.copy(box.max);
return this;
}
makeEmpty() {
this.min.x = this.min.y = +Infinity;
this.max.x = this.max.y = -Infinity;
return this;
}
isEmpty() {
// this is a more robust check for empty than (volume <= 0) because volume can get positive with two negative axes
return (this.max.x < this.min.x) || (this.max.y < this.min.y);
}
getCenter(result = new Vector2()) {
return this.isEmpty() ? result.set(0, 0) : result.addVectors(this.min, this.max).multiplyScalar(0.5);
}
getSize(result = new Vector2()) {
return this.isEmpty() ? result.set(0, 0) : result.subVectors(this.max, this.min);
}
expandByPoint(point) {
this.min.min(point);
this.max.max(point);
return this;
}
expandByVector(vector) {
this.min.sub(vector);
this.max.add(vector);
return this;
}
expandByScalar(scalar) {
this.min.addScalar(-scalar);
this.max.addScalar(scalar);
return this;
}
containsPoint(point) {
if (point.x < this.min.x || point.x > this.max.x ||
point.y < this.min.y || point.y > this.max.y) {
return false;
}
return true;
}
containsBox(box) {
if ((this.min.x <= box.min.x) && (box.max.x <= this.max.x) &&
(this.min.y <= box.min.y) && (box.max.y <= this.max.y)) {
return true;
}
return false;
}
getParameter(point, result = new Vector2()) {
// This can potentially have a divide by zero if the box
// has a size dimension of 0.
return result.set((point.x - this.min.x) / (this.max.x - this.min.x), (point.y - this.min.y) / (this.max.y - this.min.y));
}
intersectsBox(box) {
// using 6 splitting planes to rule out intersections.
if (box.max.x < this.min.x || box.min.x > this.max.x ||
box.max.y < this.min.y || box.min.y > this.max.y) {
return false;
}
return true;
}
clampPoint(point, result = new Vector2()) {
return result.copy(point).clamp(this.min, this.max);
}
distanceToPoint(point) {
const v1 = Box2._distanceToPoint_v1;
const clampedPoint = v1.copy(point).clamp(this.min, this.max);
return clampedPoint.sub(point).length();
}
intersect(box) {
this.min.max(box.min);
this.max.min(box.max);
return this;
}
union(box) {
this.min.min(box.min);
this.max.max(box.max);
return this;
}
translate(offset) {
this.min.add(offset);
this.max.add(offset);
return this;
}
equals(box) {
return box.min.equals(this.min) && box.max.equals(this.max);
}
}
Box2._setFromCenterAndSize_v1 = new Vector2();
Box2._distanceToPoint_v1 = new Vector2();
let clipperCpp = {};
function InitClipperCpp() {
if (clipperCpp.lib)
return;
if (!globalThis.document)
globalThis.document = {};
return new Promise((res, rej) => {
clipperLib__namespace.loadNativeClipperLibInstanceAsync(
// let it autodetect which one to use, but also available WasmOnly and AsmJsOnly
//del_ue_exp_start
clipperLib__namespace.NativeClipperLibRequestedFormat.WasmOnly
//del_ue_exp_end
//del_ue_exp_start
//del_ue_exp_end clipperLib.NativeClipperLibRequestedFormat.AsmJsOnly
).then(c => {
clipperCpp.lib = c;
res();
// console.log("载入成功!");//不再需要
});
});
}
/**
* 轮廓路径类
* 可以求NFP和保存NFPCahce
* 因为NFP结果是按照最低点移动的,所以将点旋转后,按照盒子将点移动到0点.
*/
class Path {
constructor(OrigionPoints, rotation = 0) {
this.OrigionPoints = OrigionPoints;
this.OutsideNFPCache = {};
this.InsideNFPCache = {};
if (OrigionPoints)
this.Init(OrigionPoints, rotation);
}
Init(origionPoints, rotation) {
this.Rotation = rotation;
if (rotation === 0)
this.Points = origionPoints.map(p => { return { ...p }; });
else {
let c = Math.cos(rotation);
let s = Math.sin(rotation);
let npts = [];
for (let p of origionPoints) {
let x = p.x;
let y = p.y;
const x1 = x * c - y * s;
const y1 = x * s + y * c;
npts.push({ x: x1, y: y1 });
}
this.Points = npts;
}
let box = new Box2();
let v2 = new Vector2();
for (let p of this.Points) {
v2.x = p.x;
v2.y = p.y;
box.expandByPoint(v2);
}
this.OrigionMinPoint = box.min;
this.Size = box.max.sub(box.min);
for (let p of this.Points) {
p.x -= box.min.x;
p.y -= box.min.y;
}
}
GetNFPs(path, outside) {
// 寻找内轮廓时,面积应该比本path小,这个判断移交给使用者自己判断
// if (!outside && this.Area < path.Area) return [];
let nfps = clipperCpp.lib.minkowskiSumPath(this.BigIntPoints, path.MirrorPoints, true);
//必须删除自交,否则将会出错
nfps = clipperCpp.lib.simplifyPolygons(nfps);
nfps = nfps.filter((nfp) => {
let area = Area(nfp);
// if (area > 1) return outside;//第一个不一定是外轮廓,但是面积为正时肯定为外轮廓 (因为使用了简化多段线,所以这个代码已经不能有了)
if (Math.abs(area) < 10)
return false; //应该不用在移除这个了
let { x, y } = nfp[0];
if (outside) {
if (this.Area > path.Area) {
let p = { x: path.InPoint.x + x, y: path.InPoint.y + y };
if (p.x < 0 || p.y < 0 || p.x > this.BigSize.x || p.y > this.BigSize.y)
return true;
let dir = clipperCpp.lib.pointInPolygon(p, this.BigIntPoints);
return dir === 0;
}
else {
let p = { x: this.InPoint.x - x, y: this.InPoint.y - y };
if (p.x < 0 || p.y < 0 || p.x > path.BigSize.x || p.y > path.BigSize.y)
return true;
let dir = clipperCpp.lib.pointInPolygon(p, path.BigIntPoints);
return dir === 0;
}
}
else {
let p = { x: path.InPoint.x + x, y: path.InPoint.y + y };
if (p.x < 0 || p.y < 0 || p.x > this.BigSize.x || p.y > this.BigSize.y)
return false;
let dir = clipperCpp.lib.pointInPolygon(p, this.BigIntPoints);
return dir === 1;
}
});
return nfps;
}
GetOutsideNFP(path) {
let nfps = this.OutsideNFPCache[path.Id];
if (nfps)
return nfps;
if (this.IsRect && path.IsRect) {
let [ax, ay] = [this.Size.x * 1e4, this.Size.y * 1e4];
let [bx, by] = [path.Size.x * 1e4, path.Size.y * 1e4];
nfps = [[
{ x: -bx, y: -by },
{ x: ax, y: -by },
{ x: ax, y: ay },
{ x: -bx, y: ay },
]];
}
else
nfps = this.GetNFPs(path, true);
this.OutsideNFPCache[path.Id] = nfps;
//虽然有这种神奇的特性,但是好像并不会提高性能。
// path.OutsideNFPCache[this.id] = (this, nfps.map(nfp =>
// {
// return nfp.map(p =>
// {
// return { x: -p.x, y: -p.y };
// });
// }));
return nfps;
}
GetInsideNFP(path) {
if (path.Area > this.Area)
return;
let nfp = this.InsideNFPCache[path.Id];
if (nfp)
return nfp;
let nfps;
if (this.IsRect) {
let [ax, ay] = [this.Size.x * 1e4, this.Size.y * 1e4];
let [bx, by] = [path.Size.x * 1e4, path.Size.y * 1e4];
let l = ax - bx;
let h = ay - by;
const MinNumber = 200; //清理的数值是100,所以200是可以接受的, 200=0.020问题不大(过盈配合)
if (l < -MinNumber || h < -MinNumber)
return;
if (l < MinNumber)
l = MinNumber;
else
l += MinNumber;
if (h < MinNumber)
h = MinNumber;
else
h += MinNumber;
nfps = [[
{ x: 0, y: 0 },
{ x: l, y: 0 },
{ x: l, y: h },
{ x: 0, y: h }
]];
}
else
nfps = this.GetNFPs(path, false);
if (path.Id !== undefined)
this.InsideNFPCache[path.Id] = nfps;
return nfps;
}
/**
* 用这个点来检测是否在Path内部
*/
get InPoint() {
if (this._InPoint)
return this._InPoint;
let mp = { x: (this.Points[0].x + this.Points[1].x) / 2, y: (this.Points[0].y + this.Points[1].y) / 2 };
let normal = new Vector2(this.Points[1].x - this.Points[0].x, this.Points[1].y - this.Points[0].y).normalize();
// [normal.x, normal.y] = [normal.y, -normal.x];
mp.x -= normal.y;
mp.y += normal.x;
mp.x *= 1e4;
mp.y *= 1e4;
this._InPoint = mp;
return mp;
}
get BigIntPoints() {
if (this._BigIntPoints)
return this._BigIntPoints;
this._BigIntPoints = this.Points.map(p => {
return {
x: Math.round(p.x * 1e4),
y: Math.round(p.y * 1e4),
};
});
return this._BigIntPoints;
}
get BigSize() {
if (this._BigSize)
return this._BigSize;
this._BigSize = new Vector2(this.Size.x * 1e4, this.Size.y * 1e4);
return this._BigSize;
}
get MirrorPoints() {
if (!this._MirrorPoints)
this._MirrorPoints = this.BigIntPoints.map(p => {
return { x: -p.x, y: -p.y };
});
return this._MirrorPoints;
}
get BoundingBox() {
if (!this._BoundingBox)
this._BoundingBox = new Box2(new Vector2, this.Size);
return this._BoundingBox;
}
get Area() {
if (this._Area === undefined)
this._Area = Area(this.Points);
return this._Area;
}
set Area(a) {
this._Area = a;
}
get IsRect() {
if (this._IsRect === undefined) {
let s = this.BoundingBox.getSize(new Vector2);
this._IsRect = equaln(this.Area, s.x * s.y, 1);
}
return this._IsRect;
}
ReadFile(file) {
file.Read();
this.Id = file.Read();
let arr = file.Read();
this.Points = [];
for (let i = 0; i < arr.length; i += 2) {
let p = { x: arr[i], y: arr[i + 1] };
this.Points.push(p);
}
this.Size = new Vector2(file.Read(), file.Read());
this._Area = file.Read();
let id = file.Read();
if (id !== -1) {
this.Origion = id;
this.Rotation = file.Read();
this.OrigionMinPoint = new Vector2(file.Read(), file.Read());
}
}
WriteFile(file) {
file.Write(1); //ver
file.Write(this.Id);
let arr = [];
for (let p of this.Points)
arr.push(p.x, p.y);
file.Write(arr);
file.Write(this.Size.x);
file.Write(this.Size.y);
file.Write(this._Area);
if (this.Origion && this.Origion.Id) {
//如果有原始的id,则传递它,以便后续进行NFP复用.
file.Write(this.Origion.Id);
file.Write(this.Rotation);
file.Write(this.OrigionMinPoint.x);
file.Write(this.OrigionMinPoint.y);
}
else
file.Write(-1);
}
}
function PathTranslate_Self(pts, mx, my) {
for (let pt of pts) {
pt.x += mx;
pt.y += my;
}
return pts;
}
//缩放点表,返回原始点表
function PathScale(pts, scale) {
for (let p of pts) {
p.x *= scale;
p.y *= scale;
}
return pts;
}
class NestCache {
/**
* 用于创建原点在0点的矩形路径
*/
static CreatePath(x, y, knifRadius = 3.5) {
let minX = -knifRadius;
let maxX = x + knifRadius;
let minY = -knifRadius;
let maxY = y + knifRadius;
return new Path([
{ x: minX, y: minY },
{ x: maxX, y: minY },
{ x: maxX, y: maxY },
{ x: minX, y: maxY },
]);
}
static Clear() {
this.CachePartPosCount = 0;
this.CacheNoSetCount = 0;
this.CacheRect.clear();
this.PositionCache = {};
}
}
//放置零件时,命中缓存的次数
NestCache.CachePartPosCount = 0;
//放置零件时,命中无法放置缓存的次数
NestCache.CacheNoSetCount = 0; //noset
NestCache.PositionCache = {};
NestCache.NoPutCache = {};
NestCache.CacheRect = new Map();
const TEXT_BOX = NestCache.CreatePath(570, 110);
/**
* 分析文字放置位置
* @param contour 轮廓点表
* @param holes 网洞点表
* @param [textBox=TEXT_BOX] 标签盒子
* @param [allowReturnNullPos=false] 允许返回null 当没有找到合适的位置返回null
* @returns Vector3
*/
function ParseRegionTextPos(contour, holes, textBox = TEXT_BOX, allowReturnNullPos = false) {
let hasTextBox = true;
let path = new Path(contour);
let nfps = path.GetInsideNFP(textBox)?.map(nfp => Path2Polyline(PathTranslate_Self(PathScale(nfp, 1e-4), path.OrigionMinPoint.x, path.OrigionMinPoint.y))); //可能无法获得
if (!nfps || nfps.length === 0) {
if (allowReturnNullPos)
return;
nfps = [Path2Polyline(contour)];
hasTextBox = false;
}
let holeNFPs = [];
for (let hole of holes) {
let hpath = new Path(hole);
let nfps = hpath.GetOutsideNFP(textBox);
let nfp = nfps[Max(nfps, (n1, n2) => Area(n2) > Area(n1))];
let pl = Path2Polyline(PathTranslate_Self(PathScale(nfp, 1e-4), hpath.OrigionMinPoint.x, hpath.OrigionMinPoint.y));
let box = pl.BoundingBox;
let boxpl = new exports.Polyline().RectangleFrom2Pt(new three.Vector3(box.min.x - 1e5, box.min.y - 1), new three.Vector3(box.max.x + 1e5, box.min.y + 1));
let con1 = Contour.CreateContour(pl, false);
let con2 = Contour.CreateContour(boxpl, false);
holeNFPs.push(...con1.UnionBoolOperation(con2).contours);
}
let shapes = nfps.map(pl => new Shape(Contour.CreateContour(pl, false)));
let subShapes = new ShapeManager;
holeNFPs.forEach(pl => {
subShapes.UnionBoolOperation(new ShapeManager([new Shape(pl)]));
});
let resShapes = [];
for (let shape of shapes) {
// TestDraw(shape.Outline.Curve, 6);
resShapes.push(...shape.SubstactBoolOperation(subShapes.ShapeList)); //可能减完丢了
}
if (resShapes.length === 0) {
//允许返回空的点 因为无法放置
if (allowReturnNullPos)
return;
resShapes = shapes;
}
let maxDist = -Infinity;
let minp;
for (let shape of resShapes) {
let pl = shape.Outline.Curve;
if (pl.Area < 1) {
if (!minp)
minp = pl.BoundingBox.getCenter(new three.Vector3).toArray();
continue;
}
// TestDraw(pl, 3); //绘制裁剪后的线
let p = polylabel__default["default"]([pl.LineData.map(p => p.pt.toArray())], 1.0); //这里不再需要转换 因为我们传递进来的就是没有凸度的点表
let dist = p["distance"];
if (dist > maxDist) {
maxDist = dist;
let pos = pl.Position;
minp = p;
minp[0] += pos.x;
minp[1] += pos.y;
}
}
let p = new three.Vector3(minp[0], minp[1]);
//左右均分
// TestDraw(new Point(p));
// let line = new Line(p, p.clone().setX(minp[0] + 1));
// let pts = curve.IntersectWith(line, IntersectOption.ExtendArg);
// pts.push(p);
// pts.sort(ComparePoint("xyz"));
// let index = pts.indexOf(p);
// p = midPoint(pts[index - 1], pts[index + 1]);
// TestDraw(new Point(p));
// //上下居中
// line = new Line(p, p.clone().setY(p.y + 1));
// pts = curve.IntersectWith(line, IntersectOption.ExtendArg);
// pts.push(p);
// pts.sort(ComparePoint("xyz"));
// index = pts.indexOf(p);
// p = midPoint(pts[index - 1], pts[index + 1]);
// TestDraw(new Point(p));
if (hasTextBox) {
p.x += textBox.Size.x * 0.5;
p.y += textBox.Size.y * 0.5;
}
return p;
}
//返回可以插入的位置
function InsertSortedIndex(array, element, comparefunc) {
let leftbound = 0;
let rightbound = array.length;
while (rightbound > leftbound) {
let testindex = Math.floor((leftbound + rightbound) / 2);
let testelement = array[testindex];
let compareresult = comparefunc(element, testelement);
if (compareresult > 0) // element > testelement
leftbound = testindex + 1;
else if (compareresult === 0) //因为函数 FindBestRange 会取index-1 来取范围
leftbound = testindex + 1;
else
rightbound = testindex;
}
return leftbound;
}
//范围并集
function RangeUnion(ranges) {
arrayRemoveDuplicateBySort(ranges, (r1, r2) => {
if (r1[1] >= r2[1])
return true;
if (r1[1] >= r2[0]) {
r1[1] = r2[1];
return true;
}
return false;
});
return ranges;
}
const LEFT = 1, RIGHT = 2, DOWN = 4, TOP = 8;
// Compute the bit code for a point (x, y) using the clip rectangle
// bounded diagonally by (xmin, ymin), and (xmax, ymax)
// ASSUME THAT xmax, xmin, ymax and ymin are global constants.
function ComputeOutCode(x, y, box) {
let code = 0;
if (x < box.min.x) // to the left of clip window
code |= LEFT;
else if (x > box.max.x) // to the right of clip window
code |= RIGHT;
if (y < box.min.y) // below the clip window
code |= DOWN;
else if (y > box.max.y) // above the clip window
code |= TOP;
return code;
}
/**
* 盒子相交测试,快速判断盒子和直线或者圆是否有相交
*/
class BoxCheckIntersect {
constructor(box) {
this.p1 = box.min;
this.p3 = box.max;
this.p2 = new three.Vector2(this.p3.x, this.p1.y);
this.p4 = new three.Vector2(this.p1.x, this.p3.y);
this.box = box;
}
//直线与盒子相交,或者被盒子包含. CohenSutherland裁剪算法
IsIntersectLine(p1, p2) {
let code1 = ComputeOutCode(p1.x, p1.y, this.box);
let code2 = ComputeOutCode(p2.x, p2.y, this.box);
//按位AND不为0:两个点共享一个外部区域(LEFT,RIGHT,TOP或BOTTOM),因此两个点都必须在窗口外部
if (code1 & code2)
return false;
let code = code1 | code2;
if (code1 === 0 || code2 === 0 || code === 3 || code === 12) //点1点2在矩形内,或者竖直贯穿,水平贯穿
return true;
if ((code & TOP) && doIntersect(p1, p2, this.p3, this.p4))
return true;
if ((code & LEFT) && doIntersect(p1, p2, this.p1, this.p4))
return true;
if ((code & RIGHT) && doIntersect(p1, p2, this.p2, this.p3))
return true;
if ((code & DOWN) && doIntersect(p1, p2, this.p3, this.p1))
return true;
return false;
}
//ref https://yal.cc/rectangle-circle-intersection-test/
IsIntersectCircle(cen, radius) {
let nearestX = three.MathUtils.clamp(cen.x, this.box.min.x, this.box.max.x);
let nearestY = three.MathUtils.clamp(cen.y, this.box.min.y, this.box.max.y);
return ((nearestX - cen.x) ** 2 + (nearestY - cen.y) ** 2) <= radius ** 2;
}
}
/**
* 删除数组中指定的元素,返回数组本身
* @param {Array<any>} arr 需要操作的数组
* @param {*} el 需要移除的元素
*/
/**
* 根据数值从小到大排序数组
* @param {Array<T>} arr
* @returns {Array<T>} 返回自身
*/
function arraySortByNumber(arr) {
arr.sort(sortNumberCompart);
return arr;
}
function sortNumberCompart(e1, e2) {
return e1 - e2;
}
//https://jsperf.com/merge-array-implementations/30
function arrayPushArray(arr1, arr2) {
let arr1Length = arr1.length;
let arr2Length = arr2.length;
arr1.length = arr1Length + arr2Length;
for (let i = 0; i < arr2Length; i++)
arr1[arr1Length + i] = arr2[i];
return arr1;
}
/**
* 获取面积最大的矩形
* @param rects
* @returns
*/
function GetMaxAreaFn(rects) {
return Max(rects, (t1, t2) => t2[0] > t1[0]);
}
function GetMaxWidthFn(rects) {
return Max(rects, (t1, t2) => t2[5] === t1[5] ? t2[6] > t1[6] : t2[5] > t1[5]);
}
function GetMaxHeightFn(rects) {
return Max(rects, (t1, t2) => t2[6] === t1[6] ? t2[6] > t1[6] : t2[5] > t1[5]);
}
//最大内接矩形
//1.分析盒子
// 1.默认我们使用顶点坐标进行一维展开
// 2.当存在斜线时, 我们精确展开(当跨度大于(100/50)时,我们精确产开另一条轴)
// (1).获取需要精确展开的区间 随后合并区间
// (2).对区间进行展开数值,使用二分搜索,如果在已有的顶点附近,则不进行增项
//2.标记轮廓内的矩形
// (1).我们收集多边形所有的斜线,与矩形盒子求交,如果相交,则矩形盒子无效化
// (2).矩形终点在轮廓内(使用单条射线检测,因为我们始终在端点增加锚点的关系 我们似乎不会在顶点位置得到(交点? 稳妥起见 实现标准的射线检测算法)
//3.获取最大内接矩形
// 迭代开始
// 动态规划 存储每个方块得到的最大矩形
// 获取最大的(根据规则)
// 对池子里的结果进行生存选择(如果被占用了方块,则该结果已经不可用)
// 取剩下可用的最大的(因为我们已经是在合理的情况下拿最大的了,所以不需要在重新迭代)
// 重复迭代
/**
* @example
*
* //1. 设置过滤条件(舍弃掉一些无用的矩形)
* let lir = new LargestInteriorRectangle;
* lir.MinWidth = 200;//设置最小可以接受的矩形宽度
* lir.MinHeight = 200;//设置最小可以接受的矩形宽度
* lir.MinArea = 200 * 200;//设置最小可以接受的矩形面积
*
* //除了变量 也可以设置过滤函数
* //例如:
* lir.FilterRectFn = (w,h)=> !(w>500 || h>500) //这样将对长或者宽不足500的矩形板过滤(舍弃)
*
* //2. 可以设置优先模式,默认提供了面积优先,你也可以使用宽度优先或者高度优先,或许默认规则可能不好用,可以自定义传入自定义函数.
* lir.GetMaxRectIndexFn = LargestInteriorRectangle.GetMaxAreaFn; //或者 LargestInteriorRectangle.GetMaxWidthFn / LargestInteriorRectangle.GetMaxHeightFn
*
* //3. 你已经设置好解析器,你现在可以进行解析了
* let rects = lir.ParseLir([ new Vector3(0,0,0) , new Vector3(100,0,0) , new Vector3(50,100,0)]) //首尾不需要重复,首尾重复可能造成错误,程序不再校验
*
*/
class LargestInteriorRectangle {
constructor() {
this.MinWidth = 100;
this.MinHeight = 100;
this.MinArea = 200 * 200;
this.GetMaxRectIndexFn = GetMaxAreaFn;
}
/**
* 分析最大内接矩形
* @param polygonPts 多边形点表,首尾不要相等
*/
ParseLIR(polygonPts) {
//端点列表
let xs = [];
let ys = [];
let xset = new Set();
let yset = new Set();
//需要展开的范围
let xranges = [];
let yranges = [];
let polygonBox = new three.Box2().setFromPoints(polygonPts);
let vec = new three.Vector2;
let klines = []; //所有的斜线
// let linesP: [Vector2, Vector2][] = []; //所有的线点
// let linesFb = new Flatbush(pts.length);//所有的线的索引
let fuzzX = new FuzzyFactory(1, 0.1);
let fuzzY = new FuzzyFactory(1, 0.1);
for (let i = 0; i < polygonPts.length; i++) {
let p = polygonPts[i];
let p2 = polygonPts[FixIndex$1(i + 1, polygonPts)];
// linesP.push([p, p2]);
// let box = new Box2().setFromPoints([p, p2]);
// linesFb.add(box.min.x, box.min.y, box.max.x, box.max.y);
vec.subVectors(p2, p);
//收集端点
let x = fuzzX.lookupOrCreate([p.x], p.x);
if (!xset.has(x)) {
xs.push(x);
xset.add(x);
}
let y = fuzzY.lookupOrCreate([p.y], p.y);
if (!yset.has(y)) {
ys.push(y);
yset.add(y);
}
//展开斜线 每20分段
if (Math.abs(vec.x) > 20 && Math.abs(vec.y) > 20) {
if (Math.abs(vec.x) > 20)
yranges.push(vec.y > 0 ? [p.y, p2.y] : [p2.y, p.y]);
if (Math.abs(vec.y) > 20)
xranges.push(vec.x > 0 ? [p.x, p2.x] : [p2.x, p.x]);
}
//收集所有的斜线
if (!equaln$1(vec.x, 0, 0.1) && !equaln$1(vec.y, 0, 0.1))
klines.push([p, p2]);
}
// linesFb.finish();
//合并展开区间
xranges.sort((a, b) => a[0] - b[0]);
yranges.sort((a, b) => a[0] - b[0]);
xranges = RangeUnion(xranges);
yranges = RangeUnion(yranges);
arraySortByNumber(xs);
arraySortByNumber(ys);
//展开
RangesAdd(xranges, xs);
RangesAdd(yranges, ys);
//最大化
xs[0] = polygonBox.min.x;
xs[xs.length - 1] = polygonBox.max.x;
ys[0] = polygonBox.min.y;
ys[ys.length - 1] = polygonBox.max.y;
//所有网格的索引
let gridFb = new Flatbush__default["default"]((xs.length - 1) * (ys.length - 1));
// let pls: Polyline[] = [];
for (let i = 0; i < xs.length - 1; i++) {
for (let j = 0; j < ys.length - 1; j++) {
gridFb.add(xs[i], ys[j], xs[i + 1], ys[j + 1]);
// let pl = new Polyline().RectangleFrom2Pt(new Vector3(xs[i], ys[j]), new Vector3(xs[i + 1], ys[j + 1]));
// if (false) TestDraw(pl);
// pls.push(pl);
}
}
gridFb.finish();
const matrix = new Array(xs.length - 1).fill(1).map(() => new Array(ys.length - 1).fill(1));
//矩形与斜线相交
let checks = [];
for (let line of klines) {
let box = new three.Box2().setFromPoints(line);
let ids = gridFb.search(box.min.x, box.min.y, box.max.x, box.max.y);
for (let id of ids) {
let check = checks[id];
let i = Math.floor((id) / (ys.length - 1));
let j = id - (i * (ys.length - 1));
if (!check) {
check = new BoxCheckIntersect(new three.Box2(new three.Vector2(xs[i] + 0.01, ys[j] + 0.01), new three.Vector2(xs[i + 1] - 0.01, ys[j + 1] - 0.01)));
checks[id] = check;
// let pl = new Polyline().RectangleFrom2Pt(new Vector3(xs[i], ys[j]), new Vector3(xs[i + 1], ys[j + 1]));
// TestDraw(pl, 2);
}
if (check.IsIntersectLine(line[0], line[1])) {
// pls[id].ColorIndex = 1;
// pls[id].Erase();
matrix[i][j] = 0;
}
}
}
//y轴扫描线(矩形在多边形外)
// if (xs.length < ys.length)
{
for (let i = 0; i < xs.length - 1; i++) {
let x = (xs[i + 1] + xs[i]) * 0.5;
let iPtYs = IsPointInPolygon(polygonPts, new three.Vector3(x, polygonBox.min.y - 0.1, 0));
arraySortByNumber(iPtYs);
for (let j = 0; j < ys.length - 1; j++) {
let y = (ys[j + 1] + ys[j]) * 0.5;
while (iPtYs.length && iPtYs[0] < y)
iPtYs.shift();
if (iPtYs.length % 2 !== 1) {
// pls[i * (ys.length - 1) + j].ColorIndex = 3;
// pls[i * (ys.length - 1) + j].Erase();
matrix[i][j] = 0;
}
}
}
}
// else//x轴扫描线
//ref https://leetcode.cn/problems/maximal-rectangle/solutions/535672/zui-da-ju-xing-by-leetcode-solution-bjlu/
const maximalRectangle = () => {
const m = matrix.length; //m个竖条 xlist
if (m === 0)
return 0;
const n = matrix[0].length; //n个横条 ylist
if (n === 0)
return;
const left = new Array(m).fill(0).map(() => new Array(n).fill(0));
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (matrix[i][j]) {
left[i][j] = (j === 0 ? 0 : left[i][j - 1]) + 1;
}
}
}
//area maxXIndex maxYIndex xcount ycount,width,height
let rects = [];
for (let j = 0; j < n; j++) // 对于每一列,使用基于柱状图的方法
{
const up = new Array(m).fill(0);
const down = new Array(m).fill(0);
let stack = [];
for (let i = 0; i < m; i++) {
while (stack.length && left[stack[stack.length - 1]][j] >= left[i][j]) {
stack.pop();
}
up[i] = stack.length === 0 ? -1 : stack[stack.length - 1];
stack.push(i);
}
stack = [];
for (let i = m - 1; i >= 0; i--) {
while (stack.length && left[stack[stack.length - 1]][j] >= left[i][j]) {
stack.pop();
}
down[i] = stack.length === 0 ? m : stack[stack.length - 1];
stack.push(i);
}
for (let i = 0; i < m; i++) {
const xCount = down[i] - up[i] - 1;
const yCount = left[i][j];
if (!yCount || !xCount)
continue;
let width = xs[down[i]] - xs[down[i] - xCount];
let height = ys[j + 1] - ys[j + 1 - yCount];
if (width < this.MinWidth || height < this.MinHeight)
continue;
//自定义过滤函数
if (this.FilterRectFn && this.FilterRectFn(width, height))
continue;
let area = width * height;
//面积小于最小允许面积
if (area < this.MinArea)
continue;
rects.push([area, down[i], j, xCount, yCount, width, height]);
}
}
// while (rects.length)
if (rects.length) {
let maxIndex = this.GetMaxRectIndexFn(rects);
let [area, maxXIndex, maxYIndex, xCount, yCount] = rects[maxIndex];
let xMax = xs[maxXIndex];
let yMax = ys[maxYIndex + 1];
let xMin = xs[maxXIndex - xCount];
let yMin = ys[maxYIndex + 1 - yCount];
maxRects.push(new three.Box2(new three.Vector2(xMin, yMin), new three.Vector2(xMax, yMax)));
rects.splice(maxIndex, 1);
//对方块进行标记
for (let i = 0; i < xCount; i++) {
for (let j = 0; j < yCount; j++) {
matrix[maxXIndex - 1 - i][maxYIndex - j] = 0;
}
}
//如果有被标记的方块,则删除它
//某些情况不适合这个算法,移除了它,保证结果正确性
// arrayRemoveIf(rects, rect =>
// {
// let [area, maxX, maxY, xCount, yCount] = rect;
// for (let i = 0; i < xCount; i++)
// {
// for (let j = 0; j < yCount; j++)
// {
// if (!matrix[maxX - 1 - i][maxY - j])
// return true;
// }
// }
// return false;
// });
}
};
let maxRects = [];
while (true) {
let count = maxRects.length;
maximalRectangle();
if (count === maxRects.length)
break;
}
return maxRects;
}
}
LargestInteriorRectangle.GetMaxAreaFn = GetMaxAreaFn;
LargestInteriorRectangle.GetMaxWidthFn = GetMaxWidthFn;
LargestInteriorRectangle.GetMaxHeightFn = GetMaxHeightFn;
function RangesAdd(ranges, vList) {
let adds = [];
for (let range of ranges) {
let dist = range[1] - range[0];
let count = Math.floor(dist / 20);
let divDist = Math.floor(dist / count);
for (let i = 1; i < count - 1; i++) {
let d = Math.floor(range[0] + divDist * i);
let index = InsertSortedIndex(vList, d, (a, b) => a - b);
if (Math.abs(vList[index] - d) < 5)
continue;
if (index !== vList.length - 1 && Math.abs(vList[index + 1] - d) < 5)
continue;
adds.push(d);
}
}
if (adds.length) {
arrayPushArray(vList, adds);
arraySortByNumber(vList);
}
}
/**
* 判断点在多段线内外 这是为了LIR实现的优化算法,返回交点的Y轴列表
*/
function IsPointInPolygon(polyPts, pt) {
// let crossings = 0;
// let insLine = new Line(pt, p2);
let iPtYs = [];
for (let i = 0; i < polyPts.length; i++) {
let sp = polyPts[i];
let ep = polyPts[FixIndex$1(i + 1, polyPts)];
// if (equalv2(sp, pt, 1e-5) || equalv2(ep, pt, 1e-5))//在起点或者终点
// return false;
//点位于线上面
// if (pt.y > Math.max(sp.y, ep.y))
// continue;
//线垂直Y轴
let derX = ep.x - sp.x;
if (equaln$1(derX, 0, 5e-6)) {
// if (equaln(pt.x, ep.x, 1e-5)
// && (pt.y > Math.min(sp.y, ep.y) - 1e-5 && pt.y < Math.max(sp.y, ep.y) + 1e-5))
// return false;//点在线上
continue;
}
//起点
if (equaln$1(sp.x, pt.x, 5e-6)) {
// if (sp.y > pt.y && derX < 0)
if (derX < 0) {
// crossings++;
iPtYs.push(sp.y);
}
continue;
}
//终点
if (equaln$1(ep.x, pt.x, 5e-6)) {
// if (ep.y > pt.y && derX > 0)
if (derX > 0) {
// crossings++;
iPtYs.push(ep.y);
}
continue;
}
//快速求交,只验证有没有交点
let [x1, x2] = sp.x > ep.x ? [ep.x, sp.x] : [sp.x, ep.x];
if (pt.x > x1 && pt.x < x2) {
let derY = ep.y - sp.y;
let k = derY / derX;
let iptY = (pt.x - sp.x) * k + sp.y;
// if (equaln(iptY, pt.y, 1e-5))//点在线上 返回false
// return false;
if (iptY > pt.y) {
// crossings++;
iPtYs.push(iptY);
}
}
}
return iPtYs;
}
/** 内外接多边形 */
function Circle2Points(circle, knifRadius, splitSize = 10, outside = false) {
let radius = circle.Radius;
let an = Math.PI * 2 / splitSize;
if (outside)
radius = radius / Math.cos(an / 2) + knifRadius;
else
radius -= knifRadius;
let cenP = circle.Center;
let pts = [];
for (let i = 0; i < splitSize; i++) {
pts.push(polar(cenP.clone(), an * i, radius));
}
return pts;
}
/**
* 针对板件的曲线变点表做的特殊优化
*/
function Curves2Points(cu, outside, knifeRadius) {
if (cu instanceof exports.Circle)
return [cu.Clone(), Circle2Points(cu, knifeRadius, 8, outside)];
else
return Polyline2Points(cu, outside, knifeRadius);
}
function Polyline2Points(pl, outside, knifeRadius) {
let pts = [];
if (!outside)
knifeRadius = -knifeRadius;
if (pl.IsClockWise)
pl.Reverse();
for (let i = 0; i < pl.EndParam; i++) {
pts.push(pl.GetPointAtParam(i));
let bul = pl.GetBulgeAt(i);
if (bul !== 0) {
let arc = pl.GetCurveAtIndex(i);
// //小圆弧内嵌时忽略小圆(直线连接) 有可能产生自交 放弃这个
// if (!outside && arc.Radius < 30 && bul > 0) continue;
// if (outside && arc.Radius < 30 && bul < 0) continue;
let allAngle = arc.AllAngle;
let arcLength = arc.Length;
let minCount = Math.floor(allAngle * 4 / Math.PI);
let splitCount = Math.round(allAngle / 0.4);
if (arcLength < 300)
splitCount = Math.max(2, minCount);
else
splitCount = Math.max(Math.floor(arcLength / 200), splitCount, 2, minCount);
let radius = arc.Radius;
if (outside === bul > 0)
radius = radius / Math.cos(allAngle / (splitCount * 2));
let cp = arc.Center;
for (let j = 0.5; j < splitCount; j++) {
let a = arc.GetAngleAtParam(j * (1 / splitCount));
let p = polar(cp.clone(), a, radius);
pts.push(p);
}
}
}
if (knifeRadius !== 0) {
pts = clipperCpp.lib.offsetToPaths({
delta: knifeRadius * 1e4,
offsetInputs: [{ data: PathScale(pts, 1e4), joinType: clipperLib.JoinType.Miter, endType: clipperLib.EndType.ClosedPolygon }]
})[0];
PathScale(pts, 1e-4);
}
return [pl, pts];
}
/**
* 移除小圆弧,使用尖角直连(有可能产生自交 概率不大)
* @param pl 请传入逆时针多段线(我们将直接修改这个多段线,如果你不想被修改 你应该拷贝一个)
* @param [radius=30]
*/
function PolylineFilletMinArc(pl, radius = 30) {
let ocsInv = pl.OCSInv;
for (let i = 0; i < pl.EndParam; i++) {
let bul = pl.LineData[i].bul;
if (equaln(bul, BUL_IS_LINE_FUZZ))
continue;
let arc = pl.GetCurveAtIndex(i);
if (arc.Radius > radius)
continue;
let preCurve = pl.GetCurveAtIndex(FixIndex(i - 1, pl.EndParam));
if (!(preCurve instanceof exports.Line))
continue;
let nextCurve = pl.GetCurveAtIndex(FixIndex(i + 1, pl.EndParam));
if (!(nextCurve instanceof exports.Line))
continue;
if (preCurve.IntersectWith2(arc, IntersectOption.ExtendThis).length === 2)
continue;
let ipt = nextCurve.IntersectWith(preCurve, IntersectOption.ExtendBoth)[0];
if (!ipt)
continue;
if (ipt.distanceTo(arc.Midpoint) > 50)
continue;
pl.LineData.splice(i, 1);
pl.SetPointAt(i, ipt.applyMatrix4(ocsInv));
}
}
/** 点p到线段P1P2 的最短距离的平方,线段不延伸 */
function GetSqSegDist(p, p1, p2) {
let x = p1.x;
let y = p1.y;
let dx = p2.x - x;
let dy = p2.y - y;
if (dx !== 0 || dy !== 0) //不是0长度线
{
let t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = p2.x;
y = p2.y;
}
else if (t > 0) {
x += dx * t;
y += dy * t;
}
}
dx = p.x - x;
dy = p.y - y;
return dx * dx + dy * dy;
}
function CrossVector2(a, b) {
return a.x * b.y - a.y * b.x;
}
//Ramer-Douglas-Peucker algorithm
function SimplifyDPStep(points, first, last, sqTolerance, simplified, offset) {
let maxSqDist = 0;
let index;
let fp = points[first];
let lp = points[last];
for (let i = first + 1; i < last; i++) {
let p = points[i];
let sqDist = GetSqSegDist(p, fp, lp);
if (sqDist > maxSqDist) {
index = i;
maxSqDist = sqDist;
}
}
if (maxSqDist > sqTolerance) {
if (index - first > 1)
SimplifyDPStep(points, first, index, sqTolerance, simplified, offset);
simplified.push(points[index]);
if (last - index > 1)
SimplifyDPStep(points, index, last, sqTolerance, simplified, offset);
}
else {
//记录偏移
let v = new Vector2(lp.x - fp.x, lp.y - fp.y).normalize();
for (let i = first + 1; i < last; i++) {
let p = points[i];
let offsetDist = -CrossVector2(v, { x: p.x - fp.x, y: p.y - fp.y });
offset.positiveOffset = Math.max(offset.positiveOffset, offsetDist);
offset.negativeOffset = Math.min(offset.negativeOffset, offsetDist);
}
}
}
// Ramer-Douglas-Peucker 算法
function SimplifyDouglasPeucker(points, sqTolerance) {
let last = points.length - 1;
let simplified = [points[0]];
let offset = { negativeOffset: 0, positiveOffset: 0 };
SimplifyDPStep(points, 0, last, sqTolerance, simplified, offset);
simplified.push(points[last]);
return [simplified, offset];
}
/**
* 运用此代码将曲线转换为点,并且精简它.
* @class CurveWrap
*/
class CurveWrap {
constructor(Curve, KnifRadius = 3, IsOutside = true) {
this.Curve = Curve;
this.KnifRadius = KnifRadius;
this.IsOutside = IsOutside;
this.Used = false;
this.Holes = [];
this._OrgCurve = Curve;
this.BoundingBox = Curve.BoundingBox;
if (Curve instanceof exports.Polyline) {
let pts = Polyline2Points(Curve, IsOutside, 0)[1];
let [spts, offset] = SimplifyDouglasPeucker(pts, KnifRadius ** 2 + KnifRadius);
if (spts.length !== pts.length) {
this.SimplyOffset = offset;
this.SimplyPolyline = Path2Polyline(spts);
this.Curve = this.SimplyPolyline; //保险起见,也更新它
this.Area = this.SimplyPolyline.Area;
}
else //此处更新多段线
this.Curve = Path2Polyline(pts);
this.Points = spts;
}
if (this.Area === undefined)
this.Area = this.Curve.Area;
}
ContainsCurve(curve) {
if (this.SimplyPolyline)
return this.SimplyPolyline.PtInCurve(curve.Curve.StartPoint);
return this.Curve.PtInCurve(curve.Curve.StartPoint);
}
GetOutsidePoints() {
if (this.Curve instanceof exports.Circle) {
let pts = Circle2Points(this.Curve, this.KnifRadius, 10, true);
return pts;
}
else {
let pl = this.SimplyPolyline || this.Curve;
let offset = this.KnifRadius;
if (this.SimplyOffset)
offset += this.SimplyOffset.positiveOffset;
if (offset > 0) {
let pts = pl.GetStretchPoints();
pts = clipperCpp.lib.offsetToPaths({
delta: offset * 10000,
offsetInputs: [{ data: PathScale(pts, 10000), joinType: clipperLib.JoinType.Miter, endType: clipperLib.EndType.ClosedPolygon }]
})[0];
PathScale(pts, 0.0001);
return pts;
}
else
return this.Points;
}
}
GetInsidePoints() {
if (this.Curve instanceof exports.Circle) {
let pts = Circle2Points(this.Curve, this.KnifRadius, 10, false);
return pts;
}
else {
let pl = this.SimplyPolyline || this.Curve;
let offset = -this.KnifRadius;
if (this.SimplyOffset) {
offset += this.SimplyOffset.negativeOffset;
}
if (offset < -0.01) {
let pls = pl.GetOffsetCurves(offset);
if (pls.length)
return pls[0].GetStretchPoints();
}
else
return this.Points;
}
}
}
exports.BUL_IS_LINE_FUZZ = BUL_IS_LINE_FUZZ;
exports.CADFiler = CADFiler;
exports.Circle2Points = Circle2Points;
exports.ComputeBiarc = ComputeBiarc;
exports.Contour = Contour;
exports.ConverToPolylineAndSplitArc = ConverToPolylineAndSplitArc;
exports.CurveContainerCurve = CurveContainerCurve;
exports.CurveWrap = CurveWrap;
exports.Curves2Points = Curves2Points;
exports.FeedingToolPath = FeedingToolPath;
exports.InitClipperCpp = InitClipperCpp;
exports.IsPtsAllOutOrOnReg = IsPtsAllOutOrOnReg;
exports.IsRect = IsRect;
exports.LargestInteriorRectangle = LargestInteriorRectangle;
exports.ParseRegionTextPos = ParseRegionTextPos;
exports.PointsSimplify2PolylineAndParseArc = PointsSimplify2PolylineAndParseArc;
exports.Polyline2Points = Polyline2Points;
exports.PolylineFilletMinArc = PolylineFilletMinArc;
exports.Shape = Shape;
exports.ShapeManager = ShapeManager;
exports.SmartPointsSimply2Polyline = SmartPointsSimply2Polyline;
exports.SmartPolylineSimply2Polyline = SmartPolylineSimply2Polyline;
exports.SplineConver2Polyline = SplineConver2Polyline;
exports.TempPolyline = TempPolyline;
exports.VData2Curve = VData2Curve;
exports.VKnifToolPath = VKnifToolPath;
exports.clipperCpp = clipperCpp;
exports.fastCurveInCurve2 = fastCurveInCurve2;
exports.isTargetCurInOrOnSourceCur = isTargetCurInOrOnSourceCur;
//# sourceMappingURL=api.cjs.js.map