在线cad绘制门和窗(网页端cad sdk开发室内设计软件) | vue.js 技术论坛-大发黄金版app下载
mxcad是使用typescript、c 语言开发的一个网页cad底层平台,它为用户提供了丰富的开发接口,此框架功能丰富、使用简易高效,可帮助大家在网页二开与自己专业相关的网页cad应用。我们以家装行业为例,介绍mxcad如何快速实现墙体、单开门、标准窗等实体,并实现这些实体之间的联动。
mxcad 相关开发文档地址:
在线mxcad demo地址:
在线查看效果:(位置:工具=>家装设置=>示例户型图,命令:mx_planview)
在本文介绍的所有实体,都将通过mxcad的自定义实体实现,如果不清楚mxcad的自定义实体实现原理和使用方法,请参考、。下面我们将以实现标准窗为例,详细介绍如何通过自定义实体实现标准窗实体。
绘制示例:(位置:工具=》家装设计=》标准窗;命令:bzc)
3.1 实体形状
标准窗形状为一个矩形框与两条平分矩形内部的直线组成,因此可以使用 mxcad 中的 、 绘制标准窗。
3.2 实体交互方式
绘制标准窗实体时,应先确定窗体宽度,若用户未输入窗体宽度值则设置一个默认值;再确定窗体长度,用户可再图纸中绘制线段指定长度,又可直接输入窗体长度;最后再在图纸中选择放置实体的位置。因此,我们可以使用 和 实现交互。
3.3 实体夹点
标准窗有三个夹点,分别位于标准窗实体开始端、中心、结束端。移动首尾两端的夹点,可以修改标准窗的长度和方向,中心夹点也应该在首尾夹点移动后重新计算;移动中心夹点,整个标准窗保持方向不变,位置随中心夹点移动,因此首尾两夹点位置跟随中心夹点移动。
3.4 实体关联(句柄关联)
标准窗在实现后应与图纸中的墙体联动,当标准窗靠近目标墙体时应该自动旋转角度与墙体适配,并自动识别墙体宽度,调整标准窗宽度与墙体宽度一致。
若关联位置的墙体有交叉、拐点、墙体长度不够等情况则只绘制实体,不与墙体关联。若与墙体关联后又离开墙体,则需要取消与墙体的关联。因此,我们可以监听图纸中的夹点编辑事件,当标准窗夹点移动后进行相关操作。
4.1 标准窗
实现mcdbteststandardwindow自定义实体窗
// 自定义标准窗实体类
class mcdbteststandardwindow extends mcdbcustomentity {
/** 标准窗开始点 */
private startpt: mcgepoint3d = new mcgepoint3d();
/** 标准窗结束点 */
private endpt: mcgepoint3d = new mcgepoint3d();
/** 标准窗中心点 */
private windowposition: mcgepoint3d = new mcgepoint3d();
/** 窗宽 */
private _windowwidth: number = 30;
/** 窗旋转角度 */
private _windowangle: number = 0;
/** 窗长 */
private _windowlength: number = 0;
/** 关联墙体句柄 */
private _wallhandle: string = "";
/**自身句柄 */
private _entityhandle: string = "";
/** 之前关联的墙体句柄 */
private _oldwallhandle: string = "";
constructor(imp?: any) {
super(imp);
}
public create(imp: any) {
return new mcdbteststandardwindow(imp)
}
/** 获取类名 */
public gettypename(): string {
return "mcdbteststandardwindow";
}
//设置或获取窗宽
public set windowwidth(val: number) {
this._windowwidth = val;
}
public get windowwidth(): number {
return this._windowwidth;
}
/** 获取或设置窗旋转角度 */
public set windowangle(val: number) {
this._windowangle = val;
this.resetpoint();
}
public get windowrangle(): number {
return this._windowangle;
}
/** 获取或设置窗长 */
public set width(val: number) {
this._windowlength = val;
}
public get width(): number {
return this._windowlength;
}
/** 获取关联墙体句柄 */
public get wallhandle(): string {
return this._wallhandle;
}
/** 设置关联墙体句柄 */
public set wallhandle(val: string) {
this._wallhandle = val;
}
/** 获取自身句柄 */
public get entityhandle(): string {
return this._entityhandle;
}
/** 设置自身句柄 */
public set entityhandle(val: string) {
this._entityhandle = val;
}
/** 获取之前关联的墙体句柄 */
public get oldwallhandle(): string {
return this._oldwallhandle;
}
/** 设置之前关联的墙体句柄 */
public set oldwallhandle(val: string) {
this._oldwallhandle = val;
}
/** 读取数据 */
public dwginfields(filter: imcdbdwgfiler): boolean {
this.startpt = filter.readpoint('satrtpt').val;
this.endpt = filter.readpoint('endpt').val;
this.windowposition = filter.readpoint('windowposition').val;
this._windowwidth = filter.readdouble('windowwidth').val;
this._windowangle = filter.readdouble('windowangle').val;
this._windowlength = filter.readdouble('windowlength').val;
this._wallhandle = filter.readstring('wallhandle').val;
this._entityhandle = filter.readstring('entityhandle').val;
this._oldwallhandle = filter.readstring('oldwallhandle').val;
return true;
}
/** 写入数据 */
public dwgoutfields(filter: imcdbdwgfiler): boolean {
filter.writepoint("satrtpt", this.startpt);
filter.writepoint("endpt", this.endpt);
filter.writepoint("windowposition", this.windowposition);
filter.writedouble("windowwidth", this._windowwidth);
filter.writedouble("windowangle", this._windowangle);
filter.writedouble("windowlength", this._windowlength);
filter.writestring("wallhandle", this._wallhandle);
filter.writestring("entityhandle", this._entityhandle);
filter.writestring("oldwallhandle", this._oldwallhandle);
return true;
}
/** 移动夹点 */
public movegrippointsat(iindex: number, dxoffset: number, dyoffset: number, dzoffset: number) {
this.assertwrite();
if (iindex === 0) {
this.startpt.x = dxoffset;
this.startpt.y = dyoffset;
this.startpt.z = dzoffset;
} else if (iindex === 1) {
this.endpt.x = dxoffset;
this.endpt.y = dyoffset;
this.endpt.z = dzoffset;
} else if (iindex === 2) {
this.startpt.x = dxoffset;
this.startpt.y = dyoffset;
this.startpt.z = dzoffset;
this.endpt.x = dxoffset;
this.endpt.y = dyoffset;
this.endpt.z = dzoffset;
}
this._windowlength = this.startpt.distanceto(this.endpt);
this.windowposition = this.startpt.clone().addvec(this.endpt.sub(this.startpt).mult(1 / 2));
// 移动标准窗
const { wallarr, angle, position } = getwallpts(this.windowposition, this._windowlength);
this._oldwallhandle = this._wallhandle;
if (wallarr.length > 0) {
// 有墙体与门窗相关联
this._windowangle = angle;
this.windowposition = position;
this.resetpoint();
const id = wallarr[0];
const newwall = id.getmcdbentity() as mcdbtestwall;
this._windowwidth = newwall.wallwidth;
this._wallhandle = newwall.gethandle();
} else {
// 没有墙体与门窗关联
this._wallhandle = "";
}
};
private resetpoint() {
const v = mcgevector3d.kxaxis.clone().rotateby(this._windowangle);
this.startpt = this.windowposition.clone().addvec(v.clone().mult(this._windowlength / 2));
this.endpt = this.windowposition.clone().addvec(v.clone().negate().mult(this._windowlength / 2));
}
/** 获取夹点 */
public getgrippoints(): mcgepoint3darray {
let ret = new mcgepoint3darray();
ret.append(this.startpt);
ret.append(this.endpt);
ret.append(this.windowposition)
return ret;
};
/** 动态绘制 */
public worlddraw(draw: mxcadworlddraw): void {
const v = this.endpt.sub(this.startpt).normalize().perpvector();
const pt1 = this.startpt.clone().addvec(v.clone().mult(this._windowwidth / 2));
const pt2 = this.endpt.clone().addvec(v.clone().mult(this._windowwidth / 2));
const pt3 = this.endpt.clone().addvec(v.clone().negate().mult(this._windowwidth / 2));
const pt4 = this.startpt.clone().addvec(v.clone().negate().mult(this._windowwidth / 2));
const pl = new mcdbpolyline();
pl.isclosed = true;
pl.addvertexat(pt1);
pl.addvertexat(pt2);
pl.addvertexat(pt3);
pl.addvertexat(pt4);
draw.drawentity(pl);
const line = new mcdbline(this.startpt, this.endpt);
const line1 = line.clone() as mcdbline;
line1.move(this.windowposition, this.windowposition.clone().addvec(v.clone().mult(this._windowwidth / 6)));
const line2 = line.clone() as mcdbline;
line2.move(this.windowposition, this.windowposition.clone().addvec(v.clone().negate().mult(this._windowwidth / 6)));
draw.drawentity(line1);
draw.drawentity(line2);
}
/** 设置窗所在位置 */
public setposition(pt: mcgepoint3d) {
this.windowposition = pt.clone();
this.resetpoint();
}
/** 获取窗所在位置 */
public getposition(): mcgepoint3d {
return this.windowposition;
}
public transformby(_mat: mcgematrix3d): boolean {
this.startpt.transformby(_mat);
this.endpt.transformby(_mat);
this.windowposition.transformby(_mat);
return true;
}
}
关联墙体信息,代码如下:
/**
* 获取门窗关联墙体信息
* @param pt 门窗的位置
* @param width 门窗宽
* @param inserttype 插入方式
* @returns { wallarr: 门窗所在墙体id数组, angle: 门窗的旋转角度, position: 门的位置, isred: 放置位置是否合理 }
*/
function getwallpts(pt: mcgepoint3d, width: number, inserttype?: string): { wallarr: mcobjectid[], angle: number, position: mcgepoint3d, isred: boolean } {
let wallarr: mcobjectid[] = [];
let angle: number = 0;
let isred: boolean = false;
const dol = 0.00001;
const startpt = pt.clone().addvec(mcgevector3d.kxaxis.clone().mult(width));
const endpt = pt.clone().addvec(mcgevector3d.kxaxis.clone().negate().mult(width));
const line = new mcdbline(startpt, endpt);
line.rotate(pt, math.pi / 4);
// 查找符合与单开门关联的墙体
const filter = new mxcadresbuf();
filter.addstring("mcdbcustomentity", 5020);
const ss = new mxcadselectionset();
ss.crossingselect(line.startpoint.x, line.startpoint.y, line.endpoint.x, line.endpoint.y, filter);
if (ss.count() > 0) {
ss.foreach(id => {
const entity = id.getmcdbentity() as mcdbcustomentity;
if (entity.gettypename() === "mcdbtestwall") {
const wall = entity.clone() as mcdbtestwall;
const pt1 = wall.getstartpoint();
const pt2 = wall.getendpoint();
const _line = new mcdbline(pt1, pt2);
const closepos = _line.getclosestpointto(pt, false).val;
const v = pt1.sub(pt2).normalize().mult(width / 2);
const doorstart = closepos.clone().addvec(v);
const doorend = closepos.clone().addvec(v.negate());
const point1 = _line.getclosestpointto(doorstart, false).val;
const point2 = _line.getclosestpointto(doorend, false).val;
if (point1.distanceto(doorstart) < dol && point2.distanceto(doorend) < dol && closepos.distanceto(pt) < wall.wallwidth / 2) {
// 判断门所在墙体位置是否还和其他墙体相交
let res = wall.insterpoints.filter(pt => {
const doorline = new mcdbline(doorstart, doorend);
const point = doorline.getclosestpointto(pt, false).val;
return point.distanceto(pt) < dol;
})
// 有符合条件的相交墙体
if (res.length === 0) {
const _v = startpt.sub(endpt);
const angle1 = v.angleto2(mcgevector3d.kxaxis, mcgevector3d.knegatezaxis);// 墙体的角度
const angle2 = _v.angleto2(mcgevector3d.kxaxis, mcgevector3d.knegatezaxis);// 门的初始角度
angle = angle1 - angle2;
wallarr.push(id);
if (inserttype === 'e' || !inserttype) {
pt = closepos.clone();
} else if (inserttype === 'm') {
pt = _line.getpointatdist(_line.getlength().val / 2).val;
}
} else {
isred = true;
}
}
}
})
};
return { wallarr, angle, position: pt, isred };
}
设置监听事件,参考代码:
const mxcad: mcobject = mxcpp.getcurrentmxcad();
// 监听wall夹点变化
mxcad.mxdraw.on("objectgripedit", (grips) => {
grips.foreach((grip) => {
const id = new mcobjectid(grip.id, grip.type === "mxcad" ? mcobjectidtype.kmxcad : mcobjectidtype.kmxdraw);
if (id.iserase()) return;
const ent = id.getmcdbentity() as mcdbentity;
if (!ent) return;
if (ent.objectname === "mcdbcustomentity") {
const typename = (ent as mcdbcustomentity).gettypename();
if (typename === "mcdbteststandardwindow") {
// 单开门夹点变换,更新相关墙体
const door = ent as mcdbtestsingledoor | mcdbteststandardwindow;
let newwall: mcdbtestwall | null = null;
let oldwall: mcdbtestwall | null = null;
const database: mcdbdatabase = mxcpp.getcurrentmxcad().getdatabase();
if (door.wallhandle) {
newwall = database.handletoidindex(door.wallhandle).getmcdbentity() as mcdbtestwall;
}
if (door.oldwallhandle) {
oldwall = database.handletoidindex(door.oldwallhandle).getmcdbentity() as mcdbtestwall;
}
if (door.wallhandle === door.oldwallhandle) {
// 新墙体句柄与老墙体句柄相等,则更新墙体
if (newwall && oldwall) {
// 重新计算门窗在墙体中的相关信息
newwall.countdoordata();
// 更新墙体
newwall.assertobjectmodification(true);
}
} else {
// 新老关联墙体不相等,新墙体添加句柄,旧墙体删除句柄并更新
if (newwall) {
newwall.doorhandles = [...newwall.doorhandles, door.entityhandle];
newwall.assertobjectmodification(true);
}
if (oldwall) {
oldwall.doorhandles = oldwall.doorhandles.filter(handle => handle !== door.entityhandle);
oldwall.assertobjectmodification(true);
}
}
};
mxcad.updatedisplay();
};
})
})
// 监听mxcad选择,若门窗被删除则触发更新
const olddoorselectids: mcobjectid[] = [];
mxcad.on("selectchange", (ids: mcobjectid[]) => {
if (ids.length > 0) {
olddoorselectids.length = 0;
ids.foreach(id => {
const entity = id.getmcdbentity();
if (entity && entity.objectname === "mcdbcustomentity") {
if ((entity as mcdbcustomentity).gettypename() === "mcdbteststandardwindow") {
olddoorselectids.push(id)
}
}
})
} else {
settimeout(() => {
if (olddoorselectids.length > 0) {
olddoorselectids.foreach(id => {
if (id.iserase()) {
const door = id.getmcdbentity() as mcdbtestsingledoor | mcdbteststandardwindow;
let newwall: mcdbtestwall | null = null;
const database: mcdbdatabase = mxcpp.getcurrentmxcad().getdatabase();
if (door.wallhandle) {
newwall = database.handletoidindex(door.wallhandle).getmcdbentity() as mcdbtestwall;
newwall.doorhandles = newwall.doorhandles.filter(handle => handle !== door.entityhandle);
newwall.assertobjectmodification(true);
}
}
});
mxcad.updatedisplay();
};
}, 0)
}
})
实现交互绘制方法:
// 绘制标准窗
async function drawstandardwindow() {
// 设置窗的长度
const getlength = new mxcaduiprdist();
getlength.setmessage(`${t('请设置标准窗的长度')}`)
let windowlength = await getlength.go();
if (!windowlength) windowlength = 30;
const window = new mcdbteststandardwindow();
window.width = windowlength;
// 设置窗的插入方式
const getinserttype = new mxcaduiprkeyword();
getinserttype.setmessage(`${t('请选择插入方式' )}`);
getinserttype.setkeywords(`[${t("任意位置")}(e)/${t("中心位置")}(m)]`)
let inserttype = await getinserttype.go();
if (!inserttype) inserttype = 'e'
// 设置窗的位置
const getdoorpos = new mxcaduiprpoint();
getdoorpos.setmessage(`${t('请选择窗的位置')}`);
let _wallarr: mcobjectid[] = [];
getdoorpos.setuserdraw((pt, pw) => {
const { isred, angle, position, wallarr } = getwallpts(pt, windowlength, inserttype);
window.setposition(position);
if (wallarr.length > 0) {
_wallarr = wallarr;
const wall = wallarr[0].getmcdbentity() as mcdbtestwall;
window.windowwidth = wall.wallwidth;
}
if (!isred) {
pw.setcolor(0x00ffff);
window.windowangle = angle;
pw.drawmcdbentity(window)
} else {
pw.setcolor(0xff0000);
pw.drawmcdbentity(window)
}
})
const windowpos = await getdoorpos.go();
if (!windowpos) return;
if (_wallarr.length > 0) {
// 关联门窗和墙
const id = _wallarr[0];
const wall = id.getmcdbentity() as mcdbtestwall;
const windowid = mxcpp.getcurrentmxcad().drawentity(window);
const _window = windowid.getmcdbentity() as mcdbteststandardwindow;
const handle: string = _window.gethandle();
wall.doorhandles = [...wall.doorhandles, handle];
wall.assertobjectmodification(true)
_window.wallhandle = wall.gethandle();
_window.entityhandle = handle;
} else {
// 绘制单开门
const id = mxcpp.getcurrentmxcad().drawentity(window);
const _window = id.getmcdbentity() as mcdbteststandardwindow;
const handle: string = _window.gethandle();
_window.entityhandle = handle;
}
}
4.2 实现效果
可在 中查看完整绘制效果:
根据上述实现家装设计实体的思路,我们可以扩展实现更多的家装设计实体,下面我们将参照上文中的基础实体实现思路去实现家装设计中的单开门实体,单开门实体样式示例:
5.1 单开门实体
a.实体形状
单卡开门实体形状为一个非闭合的矩形框与一段圆弧组成,因此,我们可以使用 mxcad 中的、 绘制单开门。
b.实体夹点
单开门有四个夹点,分别位于单开门实体开始端、中心、结束端和单开门内部。移动首尾两端的夹点,可以修改单开门实体的长度和方向,中心夹点和单开门内部的夹点也应该在首尾夹点移动后重新计算;移动中心夹点,整个单开门实体保持方向不变,位置随中心夹点移动,因此首尾两夹点和单开门内部的夹点也跟随中心夹点移动;移动单开门内部夹点,改变单开门的圆弧方向,其他三个夹点位置保持不变。
c.预留与墙体关联的接口
根据上述对实体的分析,我们就可以通过 实现一个单开门的自定义实体 mcdbtestsingledoor ,示例代码如下:
class mcdbtestsingledoor extends mcdbcustomentity {
/** 门开始点 */
private startpt: mcgepoint3d = new mcgepoint3d();
/** 门结束点 */
private endpt: mcgepoint3d = new mcgepoint3d();
/** 门中心点 */
private doorposition: mcgepoint3d = new mcgepoint3d();
/** 门宽 */
private _doorwidth: number;
/** 门方向点 */
private directpt: mcgepoint3d = new mcgepoint3d();
/** 门所在象限 */
private quadrant: number = 3;
/** 门旋转角度 */
private _doorangle: number = 0;
/** 门所在墙体的句柄 */
private _wallhandle: string = '';
/** 门句柄 */
private _entityhandle: string = '';
/** 移动前关联墙体句柄 */
private _oldwallhandle: string = '';
constructor(imp?: any) {
super(imp);
}
public create(imp: any) {
return new mcdbtestsingledoor(imp)
}
/** 获取类名 */
public gettypename(): string {
return "mcdbtestsingledoor";
}
//设置或获取门宽
public set width(val: number) {
this._doorwidth = val;
}
public get width(): number {
return this._doorwidth;
}
/** 获取或设置门旋转角度 */
public set doorangle(val: number) {
this._doorangle = val;
}
public get doorangle(): number {
return this._doorangle;
}
/** 获取或设置门所在墙体的句柄 */
public set wallhandle(val: string) {
this._wallhandle = val;
}
public get wallhandle(): string {
return this._wallhandle;
}
/** 获取或设置门句柄 */
public set entityhandle(val: string) {
this._entityhandle = val;
}
public get entityhandle(): string {
return this._entityhandle;
}
/** 获取移动前关联墙体句柄 */
public set oldwallhandle(val: string) {
this._oldwallhandle = val;
}
public get oldwallhandle(): string {
return this._oldwallhandle;
}
/** 读取数据 */
public dwginfields(filter: imcdbdwgfiler): boolean {
this.startpt = filter.readpoint('satrtpt').val;
this.endpt = filter.readpoint('endpt').val;
this.doorposition = filter.readpoint('doorposition').val;
this.directpt = filter.readpoint('directpt').val;
this._doorwidth = filter.readdouble('doorwidth').val;
this.quadrant = filter.readlong('quadrant').val;
this._doorangle = filter.readdouble('doorangle').val;
this._wallhandle = filter.readstring('wallhandle').val;
this._entityhandle = filter.readstring('entityhandle').val;
this._oldwallhandle = filter.readstring('oldwallhandle').val;
return true;
}
/** 写入数据 */
public dwgoutfields(filter: imcdbdwgfiler): boolean {
filter.writepoint("satrtpt", this.startpt);
filter.writepoint("endpt", this.endpt);
filter.writepoint("doorposition", this.doorposition);
filter.writepoint("directpt", this.directpt);
filter.writedouble("doorwidth", this._doorwidth);
filter.writelong("quadrant", this.quadrant);
filter.writedouble("doorangle", this._doorangle);
filter.writestring("wallhandle", this._wallhandle);
filter.writestring("entityhandle", this._entityhandle);
filter.writestring("oldwallhandle", this._oldwallhandle);
return true;
}
/** 移动夹点 */
public movegrippointsat(iindex: number, dxoffset: number, dyoffset: number, dzoffset: number) {
this.assertwrite();
if (iindex === 0) {
this.startpt.x = dxoffset;
this.startpt.y = dyoffset;
this.startpt.z = dzoffset;
this.doorposition = this.startpt.clone().addvec(this.endpt.sub(this.startpt).mult(0.5));
this.getquadrant();
} else if (iindex === 1) {
this.endpt.x = dxoffset;
this.endpt.y = dyoffset;
this.endpt.z = dzoffset;
this.doorposition = this.startpt.clone().addvec(this.endpt.sub(this.startpt).mult(0.5));
this.getquadrant();
} else if (iindex === 2) {
this.doorposition.x = dxoffset;
this.doorposition.y = dyoffset;
this.doorposition.z = dzoffset;
this.endpt.x = dxoffset;
this.endpt.y = dyoffset;
this.endpt.z = dzoffset;
this.startpt.x = dxoffset;
this.startpt.y = dyoffset;
this.startpt.z = dzoffset;
this.directpt.x = dxoffset;
this.directpt.y = dyoffset;
this.directpt.z = dzoffset;
} else if (iindex === 3) {
const v = this.endpt.sub(this.startpt);
const pt = this.directpt.clone();
pt.x = dxoffset;
pt.y = dyoffset;
pt.z = dzoffset;
const _v = pt.sub(this.doorposition);
const angle = _v.angleto2(v, mcgevector3d.knegatezaxis);
if (0 < angle && angle < math.pi / 2) {
this.quadrant = 1;
} else if (math.pi / 2 <= angle && angle < math.pi) {
this.quadrant = 2;
} else if (math.pi <= angle && angle < math.pi * 3 / 2) {
this.quadrant = 3;
} else {
this.quadrant = 4;
}
this.getquadrant();
};
this._doorwidth = this.startpt.distanceto(this.endpt);
this._oldwallhandle = this._wallhandle;
if (iindex !== 3) {
// 移动单开门
const { wallarr, angle, position } = getwallpts(this.doorposition, this._doorwidth);
if (wallarr.length > 0) {
// 有墙体与单开门相关联
this._doorangle = angle;
this.doorposition = position;
const v = mcgevector3d.kxaxis.clone().rotateby(this._doorangle);
this.resetpoint(v, this._doorwidth);
const id = wallarr[0];
const newwall = id.getmcdbentity() as mcdbtestwall;
this._wallhandle = newwall.gethandle();
} else {
// 没有墙体与单开门关联
this._wallhandle = "";
}
}
};
private resetpoint(v: mcgevector3d, width: number) {
this.startpt = this.doorposition.clone().subvec(v.clone().mult(width / 2));
this.endpt = this.doorposition.clone().addvec(v.clone().mult(width / 2));
this.directpt = this.startpt.clone().addvec(v.clone().mult(width / 5)).subvec(v.clone().perpvector().mult(width / 3));
this.quadrant = 3;
}
private getquadrant() {
const v = this.endpt.sub(this.startpt);
const angleorigin = v.angleto2(mcgevector3d.kxaxis, mcgevector3d.knegatezaxis);
const width = this.startpt.distanceto(this.endpt);
let v1, v2, point;
if (this.quadrant === 1) {
point = this.endpt.clone();
v1 = mcgevector3d.kxaxis.clone().negate().mult(width / 5);
v2 = mcgevector3d.kyaxis.clone().mult(width / 3);
} else if (this.quadrant === 2) {
point = this.startpt.clone();
v1 = mcgevector3d.kxaxis.clone().mult(width / 5);
v2 = mcgevector3d.kyaxis.clone().mult(width / 3);
} else if (this.quadrant === 3) {
point = this.startpt.clone();
v1 = mcgevector3d.kxaxis.clone().mult(width / 5);
v2 = mcgevector3d.kyaxis.clone().negate().mult(width / 3);
} else if (this.quadrant === 4) {
point = this.endpt.clone();
v1 = mcgevector3d.kxaxis.clone().negate().mult(width / 5);
v2 = mcgevector3d.kyaxis.clone().negate().mult(width / 3);
}
this.directpt = point.clone().addvec(v1.rotateby(angleorigin)).addvec(v2.rotateby(angleorigin));
}
/** 获取夹点 */
public getgrippoints(): mcgepoint3darray {
let ret = new mcgepoint3darray();
ret.append(this.startpt);
ret.append(this.endpt);
ret.append(this.doorposition);
ret.append(this.directpt);
return ret;
};
/** 动态绘制 */
public worlddraw(draw: mxcadworlddraw): void {
const dist1 = this.directpt.distanceto(this.startpt);
const dist2 = this.directpt.distanceto(this.endpt);
const width = this.startpt.distanceto(this.endpt);
let startpoint, endpoint;
if (dist1 < dist2) {
startpoint = this.startpt.clone();
endpoint = this.endpt.clone();
} else {
startpoint = this.endpt.clone();
endpoint = this.startpt.clone();
}
// 绘制圆弧
const directv = endpoint.sub(startpoint);
const arc = new mcdbarc();
arc.setcenter(startpoint.x, startpoint.y, startpoint.z);
arc.radius = width;
let startangle, endangle, v1, v2, angle;
if (this.quadrant === 1) {
startangle = math.pi / 2;
endangle = math.pi;
v1 = mcgevector3d.kyaxis.clone().mult(width);
v2 = mcgevector3d.kxaxis.clone().negate().mult(width / 10);
angle = directv.angleto2(mcgevector3d.kxaxis.clone().negate(), mcgevector3d.knegatezaxis)
} else if (this.quadrant === 2) {
endangle = math.pi / 2;
startangle = 0;
v1 = mcgevector3d.kyaxis.clone().mult(width);
v2 = mcgevector3d.kxaxis.clone().mult(width / 10);
angle = directv.angleto2(mcgevector3d.kxaxis, mcgevector3d.knegatezaxis)
} else if (this.quadrant === 3) {
startangle = math.pi * (3 / 2);
endangle = 0;
v1 = mcgevector3d.kyaxis.clone().negate().mult(width);
v2 = mcgevector3d.kxaxis.clone().mult(width / 10);
angle = directv.angleto2(mcgevector3d.kxaxis, mcgevector3d.knegatezaxis)
} else if (this.quadrant === 4) {
endangle = math.pi * (3 / 2);
startangle = math.pi;
v1 = mcgevector3d.kyaxis.clone().negate().mult(width);
v2 = mcgevector3d.kxaxis.clone().negate().mult(width / 10);
angle = directv.angleto2(mcgevector3d.kxaxis.clone().negate(), mcgevector3d.knegatezaxis)
}
arc.startangle = startangle;
arc.endangle = endangle;
arc.rotate(startpoint, angle);
draw.drawentity(arc);
// 绘制直线
const pl = new mcdbpolyline();
pl.addvertexat(startpoint.clone().addvec(v1))
pl.addvertexat(startpoint)
pl.addvertexat(startpoint.clone().addvec(v2));
pl.addvertexat(startpoint.clone().addvec(v1).addvec(v2));
pl.rotate(startpoint, angle);
draw.drawentity(pl);
}
/** 设置门所在位置 */
public setposition(pt: mcgepoint3d) {
this.doorposition = pt.clone();
let v: mcgevector3d;
if (this._doorangle === 0) {
v = mcgevector3d.kxaxis.clone();
} else {
v = mcgevector3d.kxaxis.clone().rotateby(this._doorangle);
}
this.resetpoint(v, this._doorwidth);
}
/** 获取门所在位置 */
public getposition(): mcgepoint3d {
return this.doorposition;
}
/**
* 获取自定义对象矩阵坐标变换
*/
public transformby(_mat: mcgematrix3d): boolean {
this.startpt.transformby(_mat);
this.endpt.transformby(_mat);
this.doorposition.transformby(_mat);
this.directpt.transformby(_mat);
return true;
};
}
5.2 实体关联(句柄关联)
单开门实体在实现后应与图纸中的墙体联动,当标准窗靠近目标墙体时应该自动旋转角度与墙体适配,并自动识别墙体的中心线,单开门放置位置应与墙体中心线对齐。
若关联位置的墙体有交叉、拐点、墙体长度不够等情况则只绘制实体,不与墙体关联。若与墙体关联后又离开墙体,则需要取消与墙体的关联。因此,我们可以监听图纸中的夹点编辑事件,当单开门夹点(首尾两夹点和中心点)移动后进行相关操作。(同上述标准窗一样的操作,识别自定义实体类别中添加上mcdbtestsingledoor)
5.3实体交互方式
绘制单开门实体时,应先确定单开门宽度,若用户未输入单开门宽度值则设置一个默认值,然后在在图纸中指定单开门的位置。因此,我们可以使用 和 实现交互。
根据其交互方式就可以设置出相应的绘制方法,下面为其示例代码:
// 绘制单开门
async function drawsingledoor() {
// 设置门的宽度
const getwidth = new mxcaduiprdist();
getwidth.setmessage(`${t('请输入门的宽度')}`)
let doorwidth = await getwidth.go();
if (!doorwidth) doorwidth = 30;
const door = new mcdbtestsingledoor();
door.width = doorwidth;
// 设置门的插入方式
const getinserttype = new mxcaduiprkeyword();
getinserttype.setmessage(`${t('请选择插入方式' )}`);
getinserttype.setkeywords(`[${t("任意位置")}(e)/${t("中心位置")}(m)]`)
let inserttype = await getinserttype.go();
if (!inserttype) inserttype = 'e'
// 设置门的位置
const getdoorpos = new mxcaduiprpoint();
getdoorpos.setmessage(`${t('请选择门的位置')}`);
let _wallarr: mcobjectid[] = [];
getdoorpos.setuserdraw((pt, pw) => {
const { isred, angle, position, wallarr } = getwallpts(pt, doorwidth, inserttype);
door.setposition(position);
_wallarr = wallarr;
if (!isred) {
pw.setcolor(0x00ffff);
door.doorangle = angle;
pw.drawmcdbentity(door)
} else {
pw.setcolor(0xff0000);
pw.drawmcdbentity(door)
}
})
const doorpos = await getdoorpos.go();
if (!doorpos) return;
if (_wallarr.length > 0) {
// 关联门窗和墙
const id = _wallarr[0];
const wall = id.getmcdbentity() as mcdbtestwall;
const doorid = mxcpp.getcurrentmxcad().drawentity(door);
const _door = doorid.getmcdbentity() as mcdbtestsingledoor;
const handle: string = _door.gethandle();
wall.doorhandles = [...wall.doorhandles, handle];
wall.assertobjectmodification(true)
_door.wallhandle = wall.gethandle();
_door.entityhandle = handle;
} else {
// 绘制单开门
const id = mxcpp.getcurrentmxcad().drawentity(door);
const _door = id.getmcdbentity() as mcdbtestsingledoor;
const handle: string = _door.gethandle();
_door.entityhandle = handle;
}
}
5.4 运行效果
可在 中查看完整绘制效果:
本作品采用《cc 协议》,转载必须注明作者和本文链接