明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 569|回复: 0

在线CAD绘制墙体(网页中开发室内设计软件)

[复制链接]
发表于 2024-12-26 14:00:58 | 显示全部楼层 |阅读模式
本帖最后由 MxDraw 于 2024-12-26 14:03 编辑

前言
室内平面图中墙体是最重要的图形之一,其中砖墙、混凝土墙、钢架墙、隔墙、隔热墙类型的墙在设计图中均有不同的表现方式,墙体的用途一般可以分为一般墙、虚墙、卫生隔断、阳台挡板、矮墙等,根据不同的需求绘制对应的墙体能够增强建筑设计的专业性和准确性。下面我们将介绍如何使用mxcad实现基础墙体功能,并展示其实践运用效果。
下述的墙体功能为一个基于mxcad开发的demo示例,因此存在无法百分百适配用户实际使用需求的情况,用户可在下述源码的基础上基于mxcad实现二次开发一次来适配实际需求。

功能开发
mxcad墙体功能的核心思想是通过继承mxcad中的自定义实体[McDbCustomEntity],自己实现一个独立的墙体对象,以及通过监测墙体相交的变化实现自主计算墙体绘制的一系列逻辑。如果你对mxcad中的自定义实体还不熟悉,点击[自定义实体开发文档链接]了解自定义实体是什么,内部存在的方法以及如何通过该实体实现自定义的功能实体。

1.封装计算墙体对应的多段线方法
在计算墙体多段线的方法中,我们需要传入墙体开始点、墙体结束点和墙体宽度,如果目标墙体存在与其他墙体相交产生拐点的情况还需要传入目标墙体生成的拐点。为了方便后续与其他墙体之间的比对和计算,我们将返回墙体的四个断点(pt1,pt2,pt3,pt4)、整体多段线(pl)、以及所在的中心直线(line),参考代码:
  1.    // 计算多段线相关数据
  2.    const getPolyline = (startPoint: McGePoint3d, endPoint: McGePoint3d, width: number, turnPoints?: McGePoint3d[]): any => {
  3.        const _startPoint = startPoint.clone();
  4.        const _endPoint = endPoint.clone();
  5.        if (startPoint.x > endPoint.x) {
  6.            startPoint = _endPoint;
  7.            endPoint = _startPoint;
  8.        }
  9.        const pl = new McDbPolyline();
  10.        // 计算初始墙体四个端点  
  11.        const v = endPoint.sub(startPoint).normalize().perpVector();
  12.        let pt1 = startPoint.clone().addvec(v.clone().mult(width / 2));
  13.        let pt2 = startPoint.clone().addvec(v.clone().negate().mult(width / 2));
  14.        let pt3 = endPoint.clone().addvec(v.clone().negate().mult(width / 2));
  15.        let pt4 = endPoint.clone().addvec(v.clone().mult(width / 2));
  16.        const pointArr = {
  17.            pt1Arr: [],
  18.            pt2Arr: [],
  19.            pt3Arr: [],
  20.            pt4Arr: []
  21.        }
  22.        /**
  23.         *有拐点则计算出墙体变换后的四个端点
  24.         */
  25.        if (turnPoints && turnPoints.length > 0) {
  26.            turnPoints.forEach(pt => {
  27.                const dist1 = pt.distanceTo(startPoint);
  28.                const dist2 = pt.distanceTo(endPoint);
  29.                if (dist1 < dist2) {
  30.                    // 开始端
  31.                    const _dist1 = pt.distanceTo(pt1);
  32.                    const _dist2 = pt.distanceTo(pt2);
  33.                    if (_dist1 < _dist2) {
  34.                        // pt1 = pt;
  35.                        pointArr.pt1Arr.push(pt)
  36.                    } else {
  37.                        // pt2 = pt;
  38.                        pointArr.pt2Arr.push(pt)
  39.                    }
  40.                } else {
  41.                    // 结束端
  42.                    const _dist1 = pt.distanceTo(pt3);
  43.                    const _dist2 = pt.distanceTo(pt4);
  44.                    if (_dist1 < _dist2) {
  45.                        // pt3 = pt;
  46.                        pointArr.pt3Arr.push(pt)
  47.                    } else {
  48.                        // pt4 = pt;
  49.                        pointArr.pt4Arr.push(pt);
  50.                    }
  51.                };
  52.            });
  53.         // 拐点变换后新的墙体端点      
  54.            for (let i = 0; i < 4; i++) {
  55.                if (pointArr[`pt${i + 1}Arr`].length !== 0) {
  56.                    let dist = null;
  57.                    let point = new McGePoint3d();
  58.                    switch (i + 1) {
  59.                        case 1:
  60.                            point = pt4;
  61.                            break;
  62.                        case 2:
  63.                            point = pt3;
  64.                            break;
  65.                        case 3:
  66.                            point = pt2;
  67.                            break;
  68.                        case 4:
  69.                            point = pt1;
  70.                            break;
  71.                    }
  72.                    pointArr[`pt${i + 1}Arr`].forEach(pt => {
  73.                        const _dist = pt.distanceTo(point);
  74.                        if (!dist || _dist < dist) {
  75.                            dist = _dist;
  76.                            switch (i + 1) {
  77.                                case 1:
  78.                                    pt1 = pt;
  79.                                    break;
  80.                                case 2:
  81.                                    pt2 = pt;
  82.                                    break;
  83.                                case 3:
  84.                                    pt3 = pt;
  85.                                    break;
  86.                                case 4:
  87.                                    pt4 = pt;
  88.                                    break;
  89.                            }
  90.                        }
  91.                    })
  92.                }
  93.            }
  94.        } else {
  95.            pl.addVertexAt(pt1);
  96.            pl.addVertexAt(pt2);
  97.            pl.addVertexAt(pt3);
  98.            pl.addVertexAt(pt4);
  99.        }
  100.        pl.isClosed = true;
  101.        const line = new McDbLine(startPoint, endPoint);
  102.        return { pl, line, pt1, pt2, pt3, pt4 };
  103.    }
复制代码

2.实现自定义墙体类:McDbTestWall
  1.    // 墙体类
  2.    class McDbTestWall extends McDbCustomEntity {
  3.        // 定义McDbTestWall内部的点对象
  4.        // 墙体开始点
  5.        private startPoint: McGePoint3d = new McGePoint3d();
  6.        // 墙体夹点移动前结束点位置
  7.        private _oldEndPoint: McGePoint3d = new McGePoint3d();
  8.        // 墙体夹点移动前开始点位置
  9.        private _oldStartPoint: McGePoint3d = new McGePoint3d();
  10.        // 墙体结束点
  11.        private endPoint: McGePoint3d = new McGePoint3d();
  12.        // 墙体宽
  13.        private _wallWidth: number = 30;
  14.        // 墙体断点
  15.        private _breakPoints: McGePoint3d[] = [];
  16.        // 墙体拐点
  17.        private _turnPoints: McGePoint3d[] = [];
  18.        // 墙体相交点
  19.        private _insterPoints: McGePoint3d[] = [];
  20.        // 每一个断点组的长度记录
  21.        private breakPtsCount: number[] = [];
  22.        // 构造函数
  23.        constructor(imp?: any) {
  24.            super(imp);
  25.        }
  26.        // 创建函数
  27.        public create(imp: any) {
  28.            return new McDbTestWall(imp)
  29.        }
  30.        // 获取类名
  31.        public getTypeName(): string {
  32.            return "McDbTestWall";
  33.        }
  34.        //设置或获取墙体宽
  35.        public set wallWidth(val: number) {
  36.            this._wallWidth = val;
  37.        }
  38.        public get wallWidth(): number {
  39.            return this._wallWidth;
  40.        }
  41.        //设置或获取墙体断点
  42.        public set breakPoints(val: McGePoint3d[]) {
  43.            this._breakPoints = val;
  44.        }
  45.        public get breakPoints(): McGePoint3d[] {
  46.            return this._breakPoints;
  47.        }
  48.        //设置或获取墙体拐点
  49.        public set turnPoints(val: McGePoint3d[]) {
  50.            this._turnPoints = val;
  51.        }
  52.        public get turnPoints(): McGePoint3d[] {
  53.            return this._turnPoints;
  54.        }
  55.        //设置或获取墙体相交点
  56.        public set insterPoints(val: McGePoint3d[]) {
  57.            this._insterPoints = val;
  58.        }
  59.        public get insterPoints(): McGePoint3d[] {
  60.            return this._insterPoints;
  61.        }
  62.        //设置或获取墙体单次相交断点数
  63.        public set breakPtsCounts(val: number[]) {
  64.            this.breakPtsCount = val;
  65.        }
  66.        public get breakPtsCounts(): number[] {
  67.            return this.breakPtsCount;
  68.        }
  69.        // 获取墙体移动前开始点
  70.        public get oldStartPoint(): McGePoint3d {
  71.            return this._oldStartPoint;
  72.        }
  73.        // 获取墙体移动前结束点
  74.        public get oldEndPoint(): McGePoint3d {
  75.            return this._oldEndPoint;
  76.        }
  77.        // 读取自定义实体数据
  78.        public dwgInFields(filter: IMcDbDwgFiler): boolean {
  79.            this.startPoint = filter.readPoint("startPoint").val;
  80.            this.endPoint = filter.readPoint("endPoint").val;
  81.            this._oldEndPoint = filter.readPoint("oldEndPoint").val;
  82.            this._oldStartPoint = filter.readPoint("oldStartPoint").val;
  83.            this._breakPoints = filter.readPoints("breakPoint").val;
  84.            this._insterPoints = filter.readPoints("insterPoints").val;
  85.            this._turnPoints = filter.readPoints("turnPoint").val;
  86.            this._wallWidth = filter.readDouble("wallWidth").val;
  87.            const _breakPtsCount = filter.readString("breakPtsCount").val;
  88.            if (_breakPtsCount) {
  89.                this.breakPtsCount = _breakPtsCount.split(',').map(Number);
  90.            }
  91.            return true;
  92.        }
  93.        // 写入自定义实体数据
  94.        public dwgOutFields(filter: IMcDbDwgFiler): boolean {
  95.            filter.writePoint("endPoint", this.endPoint);
  96.            filter.writePoint("startPoint", this.startPoint);
  97.            filter.writePoint("oldStartPoint", this._oldStartPoint);
  98.            filter.writePoint("oldEndPoint", this._oldEndPoint);
  99.            filter.writePoints("breakPoint", this._breakPoints);
  100.            filter.writePoints("insterPoints", this._insterPoints);
  101.            filter.writePoints("turnPoint", this._turnPoints);
  102.            filter.writeDouble("wallWidth", this._wallWidth);
  103.            if (this.breakPtsCount.length > 0) {
  104.                filter.writeString("breakPtsCount", this.breakPtsCount.join(','));
  105.            }
  106.            return true;
  107.        }
  108.    
  109.        // 移动自定义对象的夹点
  110.        public moveGripPointsAt(iIndex: number, dXOffset: number, dYOffset: number, dZOffset: number) {
  111.            this.assertWrite();
  112.            if (iIndex === 0) {
  113.                this._oldStartPoint = this.startPoint.clone();
  114.                this.startPoint.x += dXOffset;
  115.                this.startPoint.y += dYOffset;
  116.                this.startPoint.z += dZOffset;
  117.            } else if (iIndex === 1) {
  118.                this._oldEndPoint = this.endPoint.clone();
  119.                this.endPoint.x += dXOffset;
  120.                this.endPoint.y += dYOffset;
  121.                this.endPoint.z += dZOffset;
  122.            };
  123.        };
  124.        // 获取自定义对象的夹点
  125.        public getGripPoints(): McGePoint3dArray {
  126.            let ret = new McGePoint3dArray()
  127.            ret.append(this.startPoint);
  128.            ret.append(this.endPoint);
  129.            return ret;
  130.        };
  131.        // 绘制实体
  132.        public worldDraw(draw: MxCADWorldDraw): void {
  133.            const { pl, pt1, pt2, pt3, pt4 } = getPolyline(this.startPoint.clone(), this.endPoint.clone(), this._wallWidth, this._turnPoints);
  134.            const dol = 0.00001;
  135.            if (this.turnPoints.length > 0 || this.breakPoints.length > 0) {
  136.                // 有拐点,有断点
  137.                const lineArr: McDbLine[] = [];
  138.                const line1 = new McDbLine(pt1, pt4);
  139.                const line2 = new McDbLine(pt2, pt3);
  140.                lineArr.push(line2, line1)
  141.                const dist1 = pt1.distanceTo(pt2);
  142.                const dist2 = pt4.distanceTo(pt3);
  143.                if (dist1 - this._wallWidth < 0.00001) {
  144.                    const line3 = new McDbLine(pt2, pt1);
  145.                    if (this._turnPoints.length > 0) {
  146.                        let r = this._turnPoints.filter(pt => pt2.distanceTo(pt) < dol || pt1.distanceTo(pt) < dol);
  147.                        if (r.length === 0) lineArr.push(line3)
  148.                    } else {
  149.                        lineArr.push(line3);
  150.                    }
  151.                }
  152.                if (dist2 - this._wallWidth < 0.00001) {
  153.                    const line4 = new McDbLine(pt3, pt4);
  154.                    if (this._turnPoints.length > 0) {
  155.                        let r = this._turnPoints.filter(pt => pt3.distanceTo(pt) < dol || pt4.distanceTo(pt) < dol);
  156.                        if (r.length === 0) lineArr.push(line4)
  157.                    } else {
  158.                        lineArr.push(line4)
  159.                    }
  160.                };
  161.                if (this.breakPoints.length > 0) {
  162.                    /**
  163.                     * 有断点
  164.                     * 找出每一个断点所在的直线
  165.                     * 以每一组断点去切割直线,再绘制切割好的直线
  166.                     */
  167.                    let count = 0;
  168.                    const breakPlArr: McDbPolyline[] = []; // 存储每一组断点多段线
  169.                    const breakPts = [];
  170.                    this.breakPtsCount.forEach((item) => {
  171.                        count += item;
  172.                        const pl = new McDbPolyline()
  173.                        if (count > 0) {
  174.                            // 一组断点
  175.                            const breakPoint = this.breakPoints.filter((pt, i) => i < count && i >= count - item);
  176.                            breakPoint.forEach(pt => {
  177.                                pl.addVertexAt(pt);
  178.                            })
  179.                            breakPts.push(breakPoint)
  180.                        };
  181.                        breakPlArr.push(pl);
  182.                    });
  183.                    lineArr.forEach((l) => {
  184.                        // 直线上的断点集合
  185.                        const plArr: McDbPolyline[] = []; // 直线上的断点连线
  186.                        const r: McGePoint3d[] = []; // 直线上的断点集合
  187.                        const _r: McGePoint3d[] = []; // 直线上的单断点集合
  188.                        breakPts.forEach((item) => {
  189.                            const points = item.filter(pt => {
  190.                                const point = l.getClosestPointTo(pt, false).val;
  191.                                const dist = point.distanceTo(pt);
  192.                                return dist < dol
  193.                            });
  194.                            if (points.length > 0) {
  195.                                r.push(...points)
  196.                                if (points.length % 2 === 0) {
  197.                                    points.forEach((pt, index) => {
  198.                                        if (index % 2 == 0) {
  199.                                            const pl = new McDbPolyline();
  200.                                            pl.addVertexAt(pt);
  201.                                            pl.addVertexAt(points[index + 1]);
  202.                                            plArr.push(pl);
  203.                                        }
  204.                                    });
  205.                                } else {
  206.                                    _r.push(...points)
  207.                                }
  208.                            }
  209.                        })
  210.                        if (r.length > 0) {
  211.                            // 直线上含有断点
  212.                            l.splitCurves(r).forEach((obj) => {
  213.                                const _line = obj.clone() as McDbLine;
  214.                                const midPt = _line.startPoint.clone().addvec(_line.endPoint.sub(_line.startPoint).mult(1 / 2))
  215.                                const res1 = r.filter(pt => _line.startPoint.distanceTo(pt) < dol);
  216.                                const res2 = r.filter(pt => _line.endPoint.distanceTo(pt) < dol);
  217.                                // 起始点只有一个是断点
  218.                                if (res1.length === 0 || res2.length === 0) {
  219.                                    if (_r.length === 0) {
  220.                                        // 无单断点直接绘制
  221.                                        draw.drawEntity(_line)
  222.                                    } else {
  223.                                        // 筛选出在切断后直线的单断点
  224.                                        const singlePts = _r.filter(pt => _line.getDistAtPoint(pt).ret);//切割后线段上的单断点
  225.                                        if (singlePts.length === 0) {
  226.                                            // 若单断点不在这条直线上,则直接绘制
  227.                                            draw.drawEntity(_line)
  228.                                        } else {
  229.                                            // 若单断点在这条直线上,则判断是否与对应的交点同向
  230.                                            singlePts.forEach(pt => {
  231.                                                if (breakPlArr.length > 0) {
  232.                                                    const _plNum = breakPlArr.filter((pl, index) => {
  233.                                                        if (pl.numVerts() === 2) {
  234.                                                            return this.countLineToPl(pl, index, pt, midPt);
  235.                                                        } else {
  236.                                                            const num = pl.numVerts();
  237.                                                            let flag = false;
  238.                                                            for (let i = 0; i < num; i++) {
  239.                                                                if (i % 2 == 0) {
  240.                                                                    const _pl = new McDbPolyline();
  241.                                                                    _pl.addVertexAt(pl.getPointAt(i).val);
  242.                                                                    _pl.addVertexAt(pl.getPointAt(i + 1).val);
  243.                                                                    _pl.trueColor = new McCmColor(255, 0, 0);
  244.                                                                    const r = this.countLineToPl(_pl, index, pt, midPt)
  245.                                                                    if (r) flag = true;
  246.                                                                }
  247.                                                            };
  248.                                                            return flag
  249.                                                        }
  250.    
  251.                                                    });
  252.                                                    if (_plNum.length === singlePts.length) {
  253.                                                        draw.drawEntity(_line)
  254.                                                    }
  255.                                                }
  256.                                            })
  257.                                        }
  258.    
  259.                                    }
  260.                                }
  261.                                // 起始点都是断点
  262.                                if (res1.length !== 0 && res2.length !== 0) {
  263.                                    const _r1 = plArr.filter((pl) => {
  264.                                        const pt = pl.getClosestPointTo(midPt, false).val;
  265.                                        return pt.distanceTo(midPt) < dol
  266.                                    });
  267.                                    if (_r1.length === 0) {
  268.                                        draw.drawEntity(_line)
  269.                                    }
  270.                                }
  271.                            })
  272.                        } else {
  273.                            // 线段上无断点
  274.                            const length = l.getLength().val;
  275.                            if (Math.abs(length - this._wallWidth) < dol) {
  276.                                const midPt = l.getPointAtDist(l.getLength().val / 2).val;
  277.                                const distArr = this.insterPoints.map(pt => pt.distanceTo(midPt));
  278.                                const index = distArr.findIndex(element => element === Math.min(...distArr));
  279.                                const pl = breakPlArr[index];
  280.                                const insterPt = this._insterPoints[index];
  281.                                const p = l.getClosestPointTo(insterPt, true).val;
  282.                                if (p.distanceTo(insterPt) > dol) {
  283.                                    const closePt = pl.getClosestPointTo(insterPt, true).val;
  284.                                    const insterWallWidth = closePt.distanceTo(insterPt);
  285.                                    const insterPtToLine = l.getClosestPointTo(insterPt, true).val.distanceTo(insterPt);
  286.                                    if (insterPtToLine > insterWallWidth) {
  287.                                        draw.drawEntity(l)
  288.                                    } else {
  289.                                        const midVec = midPt.sub(insterPt);
  290.                                        const num = pl.numVerts();
  291.                                        if (num === 2) {
  292.                                            const vec = pl.getPointAt(0).val.sub(pl.getPointAt(1).val);
  293.                                            const angle = Number((midVec.angleTo1(vec) * (180 / Math.PI)).toFixed(4));
  294.                                            if (angle === 0 || angle === 180) {
  295.                                                draw.drawEntity(l)
  296.                                            }
  297.                                        } else {
  298.                                            let count = 0;
  299.                                            for (let i = 0; i < num; i++) {
  300.                                                if (i % 2 == 0) {
  301.                                                    const _pl = new McDbPolyline();
  302.                                                    _pl.addVertexAt(pl.getPointAt(i).val);
  303.                                                    _pl.addVertexAt(pl.getPointAt(i + 1).val);
  304.                                                    _pl.trueColor = new McCmColor(255, 0, 0);
  305.                                                    const vec = _pl.getPointAt(0).val.sub(_pl.getPointAt(1).val);
  306.                                                    const angle = Number((midVec.angleTo1(vec) * (180 / Math.PI)).toFixed(4));
  307.                                                    if (angle === 0 || angle === 180) {
  308.                                                        count += 1;
  309.                                                    }
  310.                                                }
  311.                                            }
  312.                                            if (count === num / 2) draw.drawEntity(l);
  313.                                        }
  314.                                    }
  315.                                }
  316.    
  317.                            } else {
  318.                                draw.drawEntity(l)
  319.                            }
  320.                        }
  321.                    });
  322.                } else {
  323.                    // 无断点,全是拐点
  324.                    lineArr.forEach((l, index) => {
  325.                        if (index < 2) {
  326.                            draw.drawEntity(l)
  327.                        } else {
  328.                            const length = l.getLength().val;
  329.                            const r = this._turnPoints.filter(pt => pt.distanceTo(l.startPoint) < dol || pt.distanceTo(l.endPoint) < dol)
  330.                            if (Math.abs(length - this._wallWidth) < dol && r.length === 0) {
  331.                                draw.drawEntity(l)
  332.                            }
  333.                        }
  334.                    })
  335.                }
  336.            }
  337.            if (!this.breakPoints.length && !this.turnPoints.length) {
  338.                draw.drawEntity(pl);
  339.            }
  340.            const line = new McDbLine(this.startPoint, this.endPoint)
  341.            draw.drawOsnapEntity(line);
  342.        }
  343.        private countLineToPl(pl: McDbPolyline, index: number, pt: McGePoint3d, midPt: McGePoint3d): Boolean {
  344.            const dol = 0.00001;
  345.            const dist1 = pl.getPointAt(0).val.distanceTo(pt);
  346.            const dist2 = pl.getPointAt(1).val.distanceTo(pt);
  347.            const insterPt = this._insterPoints[index];
  348.            if (dist1 < dol || dist2 < dol) {
  349.                const _closePt = pl.getClosestPointTo(insterPt, true).val;
  350.                const v = insterPt.sub(_closePt);
  351.                const _v = midPt.sub(_closePt);
  352.                let angle1 = v.angleTo2(McGeVector3d.kXAxis, McGeVector3d.kNegateZAxis);
  353.                let angle2 = _v.angleTo2(McGeVector3d.kXAxis, McGeVector3d.kNegateZAxis);
  354.                const angle = Math.abs(angle1 - angle2);
  355.                if (Number((angle * (180 / Math.PI)).toFixed(4)) >= 90 && Number((angle * (180 / Math.PI)).toFixed(4)) <= 270) {
  356.                    return true
  357.                } else {
  358.                    return false
  359.                }
  360.            } else {
  361.                return false
  362.            }
  363.        }
  364.        // 设置墙体起点
  365.        public setStartPoint(pt: McGePoint3d) {
  366.            this.startPoint = pt.clone()
  367.            this._oldStartPoint = pt.clone()
  368.        }
  369.        // 获取墙体起点
  370.        public getStartPoint(): McGePoint3d {
  371.            return this.startPoint;
  372.        }
  373.        // 设置墙体结束点
  374.        public setEndPoint(pt: McGePoint3d) {
  375.            this.endPoint = pt.clone()
  376.            this._oldEndPoint = pt.clone()
  377.        }
  378.        // 获取墙体结束点
  379.        public getEndPoint(): McGePoint3d {
  380.            return this.endPoint.clone();
  381.        }
  382.        // 增加墙体断点
  383.        public addBreakPoints(pts: McGePoint3d[]) {
  384.            this.breakPoints.push(...pts);
  385.        }
  386.        // 增加墙体交点
  387.        public addInsterPoints(pts: McGePoint3d[]) {
  388.            this._insterPoints.push(...pts);
  389.        }
  390.        // 增加墙体单次相交的断点数
  391.        public addBreakPtsCount(val: number[]) {
  392.            if (val.length > 0) {
  393.                this.breakPtsCount.push(...val);
  394.            }
  395.        }
  396.    };

3.计算墙体相交后的断点和拐点。
计算与目标墙体相交的墙体,参考代码:
  1.      //相交墙体集合
  2.      const intersectingWalls: McObjectId[] = [];
  3.      /**
  4.       * startPoint:墙体开始点
  5.       * endPoint:墙体结束点
  6.       * wallWidth:墙体宽度
  7.       */
  8.      const { pt1, pt3, line } = getPolyline(startPoint, endPoint, wallWidth);
  9.      const length = line.getLength().val;
  10.      // 设置过滤器,过滤出自定义实体
  11.      let filter = new MxCADResbuf();
  12.      filter.AddString("McDbCustomEntity", 5020);
  13.      const ss = new MxCADSelectionSet();
  14.      ss.crossingSelect(pt1.x - length, pt1.y - length, pt3.x + length, pt3.y + length, filter);
  15.      // 与其他墙体相交
  16.      if (ss.count() !== 0) {
  17.          ss.forEach(id => {
  18.              const ent = id.getMcDbEntity();
  19.              if ((ent as McDbCustomEntity).getTypeName() === "McDbTestWall") {
  20.                  intersectingWalls.push(id);
  21.              }
  22.          });
  23.      }
复制代码
根据相交墙体信息获取墙体断点与拐点,参考代码:
  1.      // 处理相交墙体得到断点与拐点
  2.      /**
  3.       * this.startPoint:墙体开始点
  4.       * this.endPoint:墙体结束点
  5.       * this.wallWidth:墙体宽度
  6.       * this.pointArr:断点集合
  7.       * this.breakPtsCount:单次断点数集合
  8.       * this.turnPointArr:拐点集合
  9.       * this.insterPointArr:交点集合
  10.       * this.dol:精度
  11.       */
  12.      function dealingWithWalls(wallObjIds: McObjectId[]) {
  13.          const { pl, pt1, pt2, pt3, pt4, line } = getPolyline(this.startPoint, this.endPoint, this.wallWidth);
  14.          if (wallObjIds.length != 0) {
  15.              // 与新墙体有交点的墙体集合
  16.              const wallArr = [];
  17.              // 与其他墙体相交
  18.              wallObjIds.forEach(id => {
  19.                  const entity = id.getMcDbEntity();
  20.                  if ((entity as McDbCustomEntity).getTypeName() === "McDbTestWall") {
  21.                      const wall = entity.clone() as McDbTestWall
  22.                      const { pl: _pl, pt1: _pt1, pt2: _pt2, pt3: _pt3, pt4: _pt4, line: _line } = getPolyline(wall.getStartPoint(), wall.getEndPoint(), wall.wallWidth);
  23.                      const _pointArr: McGePoint3d[] = [];
  24.                      _pl.IntersectWith(pl, McDb.Intersect.kOnBothOperands).forEach(item => _pointArr.push(item));
  25.                      if (_pointArr.length > 0) {
  26.                          wallArr.push({ id, wall, pl: _pl, pt1: _pt1, pt2: _pt2, pt3: _pt3, pt4: _pt4, line: _line, pointArr: _pointArr })
  27.                      }
  28.                  }
  29.              });
  30.              if (wallArr.length != 0) {
  31.                  wallArr.forEach(item => {
  32.                      const { id, wall, pl: _pl, pt1: _pt1, pt2: _pt2, pt3: _pt3, pt4: _pt4, line: _line, pointArr: _pointArr } = item;
  33.                      /**
  34.                           * 根据交点位置判断是否是拐点
  35.                           * 中心相交的交点与比对中心线端点距离小于1/2墙宽的都是拐点
  36.                           */
  37.                      const insterPointArr = (line as McDbLine).IntersectWith(_line as McDbLine, McDb.Intersect.kExtendBoth);
  38.                      if (insterPointArr.length() > 0) {
  39.                          const insterPt = insterPointArr.at(0);
  40.                          this.insterPointArr.push(insterPt);
  41.                          const dist1 = insterPt.distanceTo(_line.startPoint);
  42.                          const dist2 = insterPt.distanceTo(_line.endPoint);
  43.                          const dist3 = insterPt.distanceTo(line.startPoint);
  44.                          const dist4 = insterPt.distanceTo(line.endPoint);
  45.                          let vec, _vec;
  46.                          dist3 < dist4 ? vec = (line as McDbLine).endPoint.sub(insterPt).normalize() :
  47.                          vec = (line as McDbLine).startPoint.sub(insterPt).normalize();
  48.                          dist1 < dist2 ? _vec = (_line as McDbLine).endPoint.sub(insterPt).normalize() :
  49.                          _vec = (_line as McDbLine).startPoint.sub(insterPt).normalize();
  50.                          const angle1 = vec.angleTo2(McGeVector3d.kXAxis, McGeVector3d.kNegateZAxis);
  51.                          const angle2 = _vec.angleTo2(McGeVector3d.kXAxis, McGeVector3d.kNegateZAxis);
  52.                          if ((dist1 <= wall.wallWidth / 2 || dist2 <= wall.wallWidth / 2) &&
  53.                              (dist3 <= this.wallWidth / 2 || dist4 <= this.wallWidth / 2)
  54.                             ) {
  55.                              // 绘制拐角
  56.                              const turnArr = []
  57.                              const line1 = new McDbLine(pt1, pt4);
  58.                              const line2 = new McDbLine(pt2, pt3);
  59.                              const _line1 = new McDbLine(_pt1, _pt4);
  60.                              const _line2 = new McDbLine(_pt2, _pt3);
  61.                              let line1ClosePt, line2ClosePt;
  62.                              pt1.distanceTo(insterPt) < pt4.distanceTo(insterPt) ? line1ClosePt = pt1.clone() : line1ClosePt = pt4.clone();
  63.                              pt2.distanceTo(insterPt) < pt3.distanceTo(insterPt) ? line2ClosePt = pt2.clone() : line2ClosePt = pt3.clone();
  64.                              const lineInsterPts1 = line1.IntersectWith(_line1, McDb.Intersect.kExtendBoth)
  65.                              const lineInsterPts2 = line1.IntersectWith(_line2, McDb.Intersect.kExtendBoth)
  66.                              const lineInsterPts3 = line2.IntersectWith(_line1, McDb.Intersect.kExtendBoth)
  67.                              const lineInsterPts4 = line2.IntersectWith(_line2, McDb.Intersect.kExtendBoth)
  68.                              const _point1 = lineInsterPts1.length() > 0 ? lineInsterPts1.at(0) : line1ClosePt;
  69.                              const _point2 = lineInsterPts2.length() > 0 ? lineInsterPts2.at(0) : line1ClosePt;
  70.                              const _point3 = lineInsterPts3.length() > 0 ? lineInsterPts3.at(0) : line2ClosePt;
  71.                              const _point4 = lineInsterPts4.length() > 0 ? lineInsterPts4.at(0) : line2ClosePt;
  72.                              const inflectioPoints = [_point1, _point2, _point3, _point4];
  73.                              const r = inflectioPoints.filter((pt) => {
  74.                                  const v = pt.sub(insterPt);
  75.                                  const angle3 = v.angleTo2(McGeVector3d.kXAxis, McGeVector3d.kNegateZAxis);
  76.                                  return this.isAngleBetween(angle3, angle1, angle2)
  77.                              })
  78.                              if (r.length) {
  79.                                  turnArr.push(r[0])
  80.                                  const point = r[0];
  81.                                  const _r = inflectioPoints.filter((pt) => {
  82.                                      const v = pt.sub(insterPt);
  83.                                      const _v = point.sub(insterPt)
  84.                                      const angle = v.angleTo2(_v, McGeVector3d.kNegateZAxis);
  85.                                      return Math.abs(angle - Math.PI) < this.dol
  86.                                  })
  87.                                  if (_r.length) turnArr.push(_r[0])
  88.                              }
  89.                              this.turnPointArr.push(...turnArr);
  90.                              this.breakPtsCount.push(0);
  91.                              const _wall = wall.clone() as McDbTestWall;
  92.                              _wall.turnPoints = [..._wall.turnPoints, ...turnArr];
  93.                              _wall.addInsterPoints([insterPt]);
  94.                              _wall.addBreakPtsCount([0]);
  95.                              MxCpp.getCurrentMxCAD().drawEntity(_wall);
  96.                              id.erase();
  97.                          } else {
  98.                              // 没有拐角直接根据断点绘制
  99.                              this.pointArr.push(..._pointArr);
  100.                              this.breakPtsCount.push(_pointArr.length);
  101.                              const _wall = wall.clone() as McDbTestWall;
  102.                              _wall.addBreakPoints(_pointArr);
  103.                              _wall.addInsterPoints([insterPt]);
  104.                              _wall.addBreakPtsCount([_pointArr.length]);
  105.                              MxCpp.getCurrentMxCAD().drawEntity(_wall);
  106.                              id.erase();
  107.                          }
  108.                      }
  109.      
  110.                  });
  111.              }
  112.          }
  113.      }
  114.      // 计算角度方向是否在指定角度范围内
  115.      function isAngleBetween(angleRad: number, startRad: number, endRad: number) {
  116.              let minRad, maxRad
  117.              if (startRad - endRad > 0) {
  118.                  minRad = endRad;
  119.                  maxRad = startRad;
  120.              } else {
  121.                  minRad = startRad;
  122.                  maxRad = endRad;
  123.              }
  124.              if (maxRad - minRad > Math.PI) {
  125.                  return angleRad <= minRad || angleRad >= maxRad;
  126.              } else {
  127.                  return angleRad >= minRad && angleRad <= maxRad;
  128.              }
  129.          }
复制代码

4.整合绘制墙体方法:MxdrawWalls
  1. // 绘制墙体类
  2.    class MxdrawWalls {
  3.        // 墙体开始点
  4.        private startPoint: McGePoint3d = new McGePoint3d();
  5.        // 墙体结束点
  6.        private endPoint: McGePoint3d = new McGePoint3d();
  7.        // 墙体宽度
  8.        private wallWidth: number = 10;
  9.        // 断点集合
  10.        private pointArr: McGePoint3d[] = [];
  11.        // 拐点集合
  12.        private turnPointArr: McGePoint3d[] = [];
  13.        // 交点集合
  14.        private insterPointArr: McGePoint3d[] = [];
  15.        // 单次断点数集合
  16.        private breakPtsCount: number[] = [];
  17.        // 精度
  18.        private dol = 0.00001;
  19.        // 获取相交墙体
  20.        public getIntersectingWalls(startPoint: McGePoint3d, endPoint: McGePoint3d, wallWidth: number): McObjectId[] {
  21.            this.startPoint = startPoint.clone();
  22.            this.endPoint = endPoint.clone();
  23.            this.wallWidth = wallWidth;
  24.            const intersectingWalls: McObjectId[] = [];
  25.            const { pt1, pt3, line } = getPolyline(startPoint, endPoint, wallWidth);
  26.            const length = line.getLength().val;
  27.            // 设置过滤器,过滤出自定义实体
  28.            let filter = new MxCADResbuf();
  29.            filter.AddString("McDbCustomEntity", 5020);
  30.            const ss = new MxCADSelectionSet();
  31.            ss.crossingSelect(pt1.x - length, pt1.y - length, pt3.x + length, pt3.y + length, filter);
  32.            // 与其他墙体相交
  33.            if (ss.count() !== 0) {
  34.                ss.forEach(id => {
  35.                    const ent = id.getMcDbEntity();
  36.                    if ((ent as McDbCustomEntity).getTypeName() === "McDbTestWall") {
  37.                        intersectingWalls.push(id);
  38.                    }
  39.                });
  40.            }
  41.            return intersectingWalls;
  42.        }
  43.        // 初始绘制墙体
  44.        public drawWall(startPoint: McGePoint3d, endPoint: McGePoint3d, wallWidth: number): McObjectId {
  45.            const wallObjIds = this.getIntersectingWalls(startPoint, endPoint, wallWidth);
  46.            this.dealingWithWalls(wallObjIds);
  47.            const wallId = this.draw();
  48.            MxCpp.getCurrentMxCAD().updateDisplay();
  49.            return wallId
  50.        }
  51.        private isAngleBetween(angleRad: number, startRad: number, endRad: number) {
  52.            let minRad, maxRad
  53.            if (startRad - endRad > 0) {
  54.                minRad = endRad;
  55.                maxRad = startRad;
  56.            } else {
  57.                minRad = startRad;
  58.                maxRad = endRad;
  59.            }
  60.            if (maxRad - minRad > Math.PI) {
  61.                return angleRad <= minRad || angleRad >= maxRad;
  62.            } else {
  63.                return angleRad >= minRad && angleRad <= maxRad;
  64.            }
  65.        }
  66.        // 处理相交墙体得到断点与拐点
  67.        private dealingWithWalls(wallObjIds: McObjectId[]) {
  68.            const { pl, pt1, pt2, pt3, pt4, line } = getPolyline(this.startPoint, this.endPoint, this.wallWidth);
  69.            if (wallObjIds.length != 0) {
  70.                // 与新墙体有交点的墙体集合
  71.                const wallArr = [];
  72.                // 与其他墙体相交
  73.                wallObjIds.forEach(id => {
  74.                    const entity = id.getMcDbEntity();
  75.                    if ((entity as McDbCustomEntity).getTypeName() === "McDbTestWall") {
  76.                        const wall = entity.clone() as McDbTestWall
  77.                        const { pl: _pl, pt1: _pt1, pt2: _pt2, pt3: _pt3, pt4: _pt4, line: _line } = getPolyline(wall.getStartPoint(), wall.getEndPoint(), wall.wallWidth);
  78.                        const _pointArr: McGePoint3d[] = [];
  79.                        _pl.IntersectWith(pl, McDb.Intersect.kOnBothOperands).forEach(item => _pointArr.push(item));
  80.                        if (_pointArr.length > 0) {
  81.                            wallArr.push({ id, wall, pl: _pl, pt1: _pt1, pt2: _pt2, pt3: _pt3, pt4: _pt4, line: _line, pointArr: _pointArr })
  82.                        }
  83.                    }
  84.                });
  85.                if (wallArr.length != 0) {
  86.                    wallArr.forEach(item => {
  87.                        const { id, wall, pl: _pl, pt1: _pt1, pt2: _pt2, pt3: _pt3, pt4: _pt4, line: _line, pointArr: _pointArr } = item;
  88.                        /**
  89.                         * 根据交点位置判断是否是拐点
  90.                         * 中心相交的交点与比对中心线端点距离小于1/2墙宽的都是拐点
  91.                         */
  92.                        const insterPointArr = (line as McDbLine).IntersectWith(_line as McDbLine, McDb.Intersect.kExtendBoth);
  93.                        if (insterPointArr.length() > 0) {
  94.                            const insterPt = insterPointArr.at(0);
  95.                            this.insterPointArr.push(insterPt);
  96.                            const dist1 = insterPt.distanceTo(_line.startPoint);
  97.                            const dist2 = insterPt.distanceTo(_line.endPoint);
  98.                            const dist3 = insterPt.distanceTo(line.startPoint);
  99.                            const dist4 = insterPt.distanceTo(line.endPoint);
  100.                            let vec, _vec;
  101.                            dist3 < dist4 ? vec = (line as McDbLine).endPoint.sub(insterPt).normalize() :
  102.                                vec = (line as McDbLine).startPoint.sub(insterPt).normalize();
  103.                            dist1 < dist2 ? _vec = (_line as McDbLine).endPoint.sub(insterPt).normalize() :
  104.                                _vec = (_line as McDbLine).startPoint.sub(insterPt).normalize();
  105.                            const angle1 = vec.angleTo2(McGeVector3d.kXAxis, McGeVector3d.kNegateZAxis);
  106.                            const angle2 = _vec.angleTo2(McGeVector3d.kXAxis, McGeVector3d.kNegateZAxis);
  107.                            if ((dist1 <= wall.wallWidth / 2 || dist2 <= wall.wallWidth / 2) &&
  108.                                (dist3 <= this.wallWidth / 2 || dist4 <= this.wallWidth / 2)
  109.                            ) {
  110.                                // 绘制拐角
  111.                                const turnArr = []
  112.                                const line1 = new McDbLine(pt1, pt4);
  113.                                const line2 = new McDbLine(pt2, pt3);
  114.                                const _line1 = new McDbLine(_pt1, _pt4);
  115.                                const _line2 = new McDbLine(_pt2, _pt3);
  116.                                let line1ClosePt, line2ClosePt;
  117.                                pt1.distanceTo(insterPt) < pt4.distanceTo(insterPt) ? line1ClosePt = pt1.clone() : line1ClosePt = pt4.clone();
  118.                                pt2.distanceTo(insterPt) < pt3.distanceTo(insterPt) ? line2ClosePt = pt2.clone() : line2ClosePt = pt3.clone();
  119.                                const lineInsterPts1 = line1.IntersectWith(_line1, McDb.Intersect.kExtendBoth)
  120.                                const lineInsterPts2 = line1.IntersectWith(_line2, McDb.Intersect.kExtendBoth)
  121.                                const lineInsterPts3 = line2.IntersectWith(_line1, McDb.Intersect.kExtendBoth)
  122.                                const lineInsterPts4 = line2.IntersectWith(_line2, McDb.Intersect.kExtendBoth)
  123.                                const _point1 = lineInsterPts1.length() > 0 ? lineInsterPts1.at(0) : line1ClosePt;
  124.                                const _point2 = lineInsterPts2.length() > 0 ? lineInsterPts2.at(0) : line1ClosePt;
  125.                                const _point3 = lineInsterPts3.length() > 0 ? lineInsterPts3.at(0) : line2ClosePt;
  126.                                const _point4 = lineInsterPts4.length() > 0 ? lineInsterPts4.at(0) : line2ClosePt;
  127.                                const inflectioPoints = [_point1, _point2, _point3, _point4];
  128.                                const r = inflectioPoints.filter((pt) => {
  129.                                    const v = pt.sub(insterPt);
  130.                                    const angle3 = v.angleTo2(McGeVector3d.kXAxis, McGeVector3d.kNegateZAxis);
  131.                                    return this.isAngleBetween(angle3, angle1, angle2)
  132.                                })
  133.                                if (r.length) {
  134.                                    turnArr.push(r[0])
  135.                                    const point = r[0];
  136.                                    const _r = inflectioPoints.filter((pt) => {
  137.                                        const v = pt.sub(insterPt);
  138.                                        const _v = point.sub(insterPt)
  139.                                        const angle = v.angleTo2(_v, McGeVector3d.kNegateZAxis);
  140.                                        return Math.abs(angle - Math.PI) < this.dol
  141.                                    })
  142.                                    if (_r.length) turnArr.push(_r[0])
  143.                                }
  144.                                this.turnPointArr.push(...turnArr);
  145.                                this.breakPtsCount.push(0);
  146.                                const _wall = wall.clone() as McDbTestWall;
  147.                                _wall.turnPoints = [..._wall.turnPoints, ...turnArr];
  148.                                _wall.addInsterPoints([insterPt]);
  149.                                _wall.addBreakPtsCount([0]);
  150.                                MxCpp.getCurrentMxCAD().drawEntity(_wall);
  151.                                id.erase();
  152.                            } else {
  153.                                // 没有拐角直接根据断点绘制
  154.                                this.pointArr.push(..._pointArr);
  155.                                this.breakPtsCount.push(_pointArr.length);
  156.                                const _wall = wall.clone() as McDbTestWall;
  157.                                _wall.addBreakPoints(_pointArr);
  158.                                _wall.addInsterPoints([insterPt]);
  159.                                _wall.addBreakPtsCount([_pointArr.length]);
  160.                                MxCpp.getCurrentMxCAD().drawEntity(_wall);
  161.                                id.erase();
  162.                            }
  163.                        }
  164.    
  165.                    });
  166.                }
  167.            }
  168.        }
  169.        // 绘制墙体
  170.        private draw(): McObjectId {
  171.            const mxcad = MxCpp.getCurrentMxCAD();
  172.            const wall = new McDbTestWall();
  173.            wall.setEndPoint(this.endPoint);
  174.            wall.setStartPoint(this.startPoint);
  175.            wall.wallWidth = this.wallWidth;
  176.            if (this.pointArr && this.pointArr.length) {
  177.                wall.addBreakPoints(this.pointArr)
  178.            }
  179.            wall.turnPoints = this.turnPointArr ? this.turnPointArr : [];
  180.            if (this.insterPointArr && this.insterPointArr.length) {
  181.                wall.addInsterPoints(this.insterPointArr)
  182.            }
  183.            if (this.breakPtsCount && this.breakPtsCount.length) {
  184.                wall.addBreakPtsCount(this.breakPtsCount)
  185.            }
  186.            return mxcad.drawEntity(wall);
  187.        }
  188.       // 更新墙体
  189.        public updateWall(wallId: McObjectId, isRedraw: Boolean) {
  190.            const wall = wallId.getMcDbEntity() as McDbTestWall;
  191.            if (wall.objectName === "McDbCustomEntity" && (wall as McDbCustomEntity).getTypeName() === "McDbTestWall") {
  192.                const wallClone = wall.clone() as McDbTestWall;
  193.                // 先修改移动前相交墙体里的断点和拐点
  194.                const oldWallObjIds = this.getIntersectingWalls(wall.oldStartPoint, wall.oldEndPoint, wall.wallWidth);
  195.                // 更新与墙体相交的墙
  196.                oldWallObjIds.forEach(id => {
  197.                    const _wall = (id.getMcDbEntity()) as McDbTestWall;
  198.                    _wall.breakPoints = _wall.breakPoints.filter(pt => {
  199.                        const r = wallClone.breakPoints.filter(point => point.distanceTo(pt) < this.dol);
  200.                        return r.length === 0;
  201.                    });
  202.                    _wall.turnPoints = _wall.turnPoints.filter(pt => {
  203.                        const r = wallClone.turnPoints.filter(point => point.distanceTo(pt) < this.dol);
  204.                        return r.length === 0;
  205.                    });
  206.                    _wall.insterPoints = _wall.insterPoints.filter((pt, index) => {
  207.                        const r = wallClone.insterPoints.filter(point => point.distanceTo(pt) < this.dol);
  208.                        if (r.length === 0) {
  209.                            return true
  210.                        } else {
  211.                            _wall.breakPtsCounts.splice(index, 1);
  212.                        }
  213.                    });
  214.                    _wall.assertObjectModification(true);
  215.                });
  216.                // 是否重绘墙体,重新计算墙体点位
  217.                if (isRedraw) {
  218.                    wallId.erase();
  219.                    setTimeout(() => {
  220.                        this.drawWall(wall.getStartPoint(), wall.getEndPoint(), wall.wallWidth);
  221.                    }, 0)
  222.                };
  223.            } else {
  224.                return
  225.            }
  226.        }

5.调用MxdrawWalls绘制墙体
  1. // 绘制墙体
  2.    async function drawWall() {
  3.        const getWallWidth = new MxCADUiPrDist();
  4.        getWallWidth.setMessage(`\n${t('请设置墙体宽度')}:`);
  5.        let wallWidth = await getWallWidth.go();
  6.        if (!wallWidth) wallWidth = 30;
  7.        while (true) {
  8.            const getPoint1 = new MxCADUiPrPoint();
  9.            getPoint1.setMessage(`\n${t('请设置墙体起点')}:`);
  10.            const pt1 = await getPoint1.go();
  11.            if (!pt1) break;
  12.    
  13.            const getDist = new MxCADUiPrDist();
  14.            getDist.setBasePt(pt1);
  15.            getDist.setMessage(`\n${t('请设置墙体长度')}:`);
  16.            let pt2 = new McGePoint3d();
  17.            getDist.setUserDraw((pt, pw) => {
  18.                const v = pt1.sub(pt).normalize().perpVector().mult(wallWidth / 2);
  19.                const line1 = new McDbLine(pt1.clone().addvec(v), pt.clone().addvec(v));
  20.                pw.setColor(0x0000FF);
  21.                pw.drawMcDbEntity(line1);
  22.    
  23.                const line2 = new McDbLine(pt1.clone().addvec(v.clone().negate()), pt.clone().addvec(v.clone().negate()));
  24.                pw.setColor(0xFF0000);
  25.                pw.drawMcDbEntity(line2);
  26.                pt2 = pt.clone();
  27.            });
  28.            const dist = await getDist.go();
  29.            if (!dist) break;
  30.            const v = pt2.sub(pt1).normalize().mult(dist);
  31.            const endPt = pt1.clone().addvec(v)
  32.            const wall = new MxdrawWalls()
  33.            wall.drawWall(pt1, endPt, wallWidth);
  34.        }
  35.    }
复制代码

6.夹点编辑、实体删除情况处理
调用夹点编辑监听事件,监听墙体变化,当墙体位置发生改变则触发墙体更新方法,参考代码:
  1. const mxcad: McObject = MxCpp.getCurrentMxCAD();
  2.      // 监听wall夹点变化
  3.      mxcad.mxdraw.on("objectGripEdit", (grips) => {
  4.          grips.forEach((grip) => {
  5.              const id = new McObjectId(grip.id, grip.type === "mxcad" ? McObjectIdType.kMxCAD : McObjectIdType.kMxDraw);
  6.              if (id.isErase()) return;
  7.              const wall = id.getMcDbEntity() as McDbTestWall;
  8.              if(!wall) return;
  9.             
  10.              if (wall.objectName === "McDbCustomEntity" && (wall as McDbCustomEntity).getTypeName() === "McDbTestWall") {
  11.                  // 更新绘制wall
  12.                  const newWall = new MxdrawWalls();
  13.                  newWall.updateWall(id, true);
  14.                  mxcad.clearMxCurrentSelect();
  15.              };
  16.          })
  17.      })
复制代码
调用实体选择监听事件,监听实体选择,若监听到选择的墙体被删除则触法墙体更新方法,参考代码:
  1.      // 监听mxcad选择,若wall被删除则触发更新
  2.      const oldSelectIds: McObjectId[] = [];
  3.      mxcad.on("selectChange", (ids: McObjectId[]) => {
  4.          if (ids.length > 0) {
  5.              ids.forEach(id => {
  6.                  const entity = id.getMcDbEntity();
  7.                  if (entity && entity.objectName === "McDbCustomEntity" && (entity as McDbCustomEntity).getTypeName() === "McDbTestWall") {
  8.                      oldSelectIds.push(id)
  9.                  }
  10.              })
  11.          } else {
  12.              setTimeout(()=>{
  13.                  oldSelectIds.forEach(id => {
  14.                      if (id.isErase()) {
  15.                          const newWall = new MxdrawWalls();
  16.                          newWall.updateWall(id, false);
  17.                      }
  18.                  });
  19.                  oldSelectIds.length = 0;
  20.              },0)
  21.          }
  22.      })
复制代码

功能实践
在线示例demo地址:https://demo.mxdraw3d.com:3000/mxcad/墙体绘制效果展示:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-2-22 02:05 , Processed in 0.193958 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表