一、弁言五月桃色网
本章将先容如何期骗mxcad插件完了在CAD图纸中测量圆和测量面积的功能,用户点击绸缪圆对象将自动象征出这个圆的半径、面积值和周长值,同期不错自界说遴荐标注翰墨的位置,测量圆功能简略快速掌合手绸缪圆对象的数据信息,节略统计工程量。测量面积功能(多边形、矩形)以多边形的每一个过火来详情多边形的相貌和位置,通过点击矩形的量角点来详情矩形的大小和位置,也不错自界说标注翰墨的位置。
二、测量圆的功能完了
欧美性色图2.1.完了自界说圆标注类
为了节略后期管束与修改标注,不错通过接收McDbCustomEntity自界说实体类来彭胀完了圆标注类。其中在 mxcad 中圆对象对应的实体类为McDbCircle,该类提供了取得或开导圆干系信息的属性或相貌,咱们不错左证功能需求去遴荐调用。
在测量圆功能中需要取得圆对象的半径、面积与周长,因此不错调用McDbCircle.radius属性取得圆半径,McDbCircle.getArea()相貌或奏凯计较来取得圆面积,以及McDbCircle.getLength()相貌取得圆周长。然后期骗McDbMText构造测量信息多文本对象,将圆的标注信息画图在页面中,参考代码如下:
// 自界说圆标注类class McDbTestMeasuringCircle extends McDbCustomEntity {// 界说McDbTestMeasuringCircle里面的点对象// 圆心private center: McGePoint3d = new McGePoint3d();// 标注点private position: McGePoint3d = new McGePoint3d();// 圆半径private radius: number;// 构造函数constructor(imp?: any) {super(imp);}// 创建函数public create(imp: any) {return new McDbTestMeasuringCircle(imp)}// 取得类名public getTypeName(): string {return "McDbTestMeasuringCircle";}//开导或取得圆半径public set circleRadius(val: number) {this.radius = val;}public get circleRadius(): number {return this.radius;}// 读取自界说实体数据center、position、radiuspublic dwgInFields(filter: IMcDbDwgFiler): boolean {this.center = filter.readPoint("center").val;this.position = filter.readPoint("position").val;this.radius = filter.readDouble("radius").val;return true;}// 写入自界说实体数据center、position、radiuspublic dwgOutFields(filter: IMcDbDwgFiler): boolean {filter.writePoint("center", this.center);filter.writePoint("position", this.position);filter.writeDouble("radius", this.radius);return true;}// 出动自界说对象的夹点public moveGripPointsAt(iIndex: number, dXOffset: number, dYOffset: number, dZOffset: number) {this.assertWrite();this.position.x += dXOffset;this.position.y += dYOffset;this.position.z += dZOffset;};// 取得自界说对象的夹点public getGripPoints(): McGePoint3dArray {let ret = new McGePoint3dArray()ret.append(this.position);return ret;};// 画图实体public worldDraw(draw: MxCADWorldDraw): void {// 画图标注圆与标注信息const circle = new McDbCircle();circle.center = this.center;circle.radius = this.radius;const length = circle.getLength().val;const radius = circle.radius;const area = Math.PI * radius * radius;const mText = new McDbMText();mText.contents = `半径:${radius.toFixed(2)} \\P 周长:${length.toFixed(2)} \\P 面积:${area.toFixed(2)}`mText.textHeight = radius / 6;mText.attachment = McDb.AttachmentPoint.kMiddleCenter;mText.location = this.position;mText.trueColor = circle.trueColor = this.trueColor;draw.drawEntity(mText);draw.drawEntity(circle);}// 开导pt1public setCenter(pt: McGePoint3d) {this.assertWrite();this.center = pt.clone();}// 取得pt1public getCenter() {return this.center;}// 取得positionpublic setPosition(pt: McGePoint3d) {this.assertWrite();this.position = pt.clone();}// 取得positionpublic getPosition() {return this.position;}}
2.2 注册自界说类信息
开动代码:
new McDbTestMeasuringCircle().rxInit();
2.3 编写相貌调用McDbTestMeasuringCircle自界说圆标注类完了测量圆功能
2.3.1 取得绸缪圆对象,得到干通盘据信息
期骗遴荐实体对象MxCADUiPrEntity()左证用户鼠标点击的坐标得到对应的实体,其中需要只遴荐圆对象五月桃色网,因此,咱们再调用MxCADResbuf()为遴荐实体对象开导过滤器来过滤出绸缪实体,参考代码如下:
// 遴荐实体对象const getEnt = new MxCADUiPrEntity();// 开导指示信息getEnt.setMessage("请遴荐一个圆对象");// 开导过滤器const filter = new MxCADResbuf([DxfCode.kEntityType, "CIRCLE"]);getEnt.setFilter(filter);// entId过滤遴荐后的圆实体对象IDconst entId = await getEnt.go();if (!entId.id) return;// 取得圆干系信息const circle = entId.getMcDbEntity() as McDbCircle;const mCircle = new McDbTestMeasuringCircle();mCircle.setCenter(circle.center);mCircle.circleRadius = circle.radius;
2.3.2 指定标注点并画图圆标注对象
期骗MxCADUiPrPoint取点对象在页面中交互取点。在取点经过中通过MxCADUiPrPoint.setUserDraw()相貌动态画图标注对象,使用户愈加直不雅的不雅察到标注对象的位置变化,参考代码如下:
// 开导取点对象const getPt = new MxCADUiPrPoint();// 开导指示信息getPt.setMessage('请指定翰墨位置');// 动态画图getPt.setUserDraw((pt, pw) => {pw.setColor(0xFF0000);mCircle.setPosition(pt);pw.drawMcDbEntity(mCircle);});const point = await getPt.go();if (!point) return;// 开导标注文本位置mCircle.setPosition(point);// 开导圆标注对象颜料mCircle.trueColor = new McCmColor(255, 0, 0)const mxcad = MxCpp.getCurrentMxCAD();// 画图圆标注对象mxcad.drawEntity(mCircle);
效能如下图:
三、测量面积的功能完了
3.1 完了自界说面积标注类
为了节略后期管束与修改标注,不错通过接收McDbCustomEntity自界说实体类来彭胀完了面积标注类。
其中,在mxcad中多边形与矩形王人是通过多段线画图出来的,多段线对象对应的实体类为McDbPolyline,该类提供了取得或开导图形对象干系信息的属性或相貌,左证功能需求去遴荐调用。
在测量面积(多边形、矩形)功能中,调用McDbPolyline.getLength()相貌取得多段线对象的长度、以及在多段线闭合的情况下调用McDbPolyline.getArea()相貌取得面积。然后期骗McDbMText构造测量信息多文本对象,将对象的标注信息画图在页面中,参考代码如下:
// 自界说面积标注类class McDbTestAreaComment extends McDbCustomEntity {/** 点数组 */private ptArr: McGePoint3d[] = [];/** 标注点 */private pt: McGePoint3d;/** 字高 */private height: number = 50;/** 凸度数组 */private dbulges: number[] = [];/** 测量对象是否为矩形 */private isReactFlag: boolean = false;constructor(imp?: any) {super(imp);}public create(imp: any) {return new McDbTestAreaComment(imp)}/** 取得类名 */public getTypeName(): string {return "McDbTestAreaComment";}//开导或取得文本字高public set textHeight(val: number) {this.height = val;}public get textHeight(): number {return this.height;}//开导或取得测量对象是否为矩形public set isReact(val: boolean) {this.isReactFlag = val;}public get isReact(): boolean {return this.isReactFlag;}/** 读取数据 */public dwgInFields(filter: IMcDbDwgFiler): boolean {this.ptArr = filter.readPoints('ptArr').val;this.pt = filter.readPoint('pt').val;const _dbulges = filter.readString("dbulges").val;this.dbulges = _dbulges.split(',').map(Number);this.isReactFlag = filter.readLong("isReactFlag").val ? true : false;return true;}/** 写入数据 */public dwgOutFields(filter: IMcDbDwgFiler): boolean {filter.writePoints("ptArr", this.ptArr);filter.writePoint("pt", this.pt);const _dbulges = this.dbulges.toString();filter.writeString("dbulges", _dbulges);filter.writeLong("isReactFlag", this.isReactFlag ? 1 : 0);return true;}/** 出动夹点 */public moveGripPointsAt(iIndex: number, dXOffset: number, dYOffset: number, dZOffset: number) {this.assertWrite();this.ptArr.forEach((point, index) => {if (index === iIndex) {point.x += dXOffset;point.y += dYOffset;point.z += dZOffset;}});if (iIndex === this.ptArr.length) {this.pt.x += dXOffset;this.pt.y += dYOffset;this.pt.z += dZOffset;}};/** 取得夹点 */public getGripPoints(): McGePoint3dArray {let ret = new McGePoint3dArray();this.ptArr.forEach(point => {ret.append(point);})ret.append(this.pt);return ret;};/** 动态画图 */public worldDraw(draw: MxCADWorldDraw): void {const pl = new McDbPolyline();pl.isClosed = true;if (this.isReactFlag) {// 测量的是矩形,得到的便是矩形的两个角点pl.addVertexAt(this.ptArr[0], 0, 0.1, 0.1);pl.addVertexAt(new McGePoint3d(this.ptArr[1].x, this.ptArr[0].y), 0, 0.1, 0.1);pl.addVertexAt(this.ptArr[1], 0, 0.1, 0.1);pl.addVertexAt(new McGePoint3d(this.ptArr[0].x, this.ptArr[1].y), 0, 0.1, 0.1);} else {// 测量的是多边形this.ptArr.forEach((pt, index) => {pl.addVertexAt(pt, this.dbulges[index], 0.1, 0.1);});}// 取得对象数据信息const area = pl.getArea().val;const length = pl.getLength().val;// 构造对文本对象const text = new McDbMText();text.contents = `面积:${area.toFixed(2)}\\P周长:${length.toFixed(2)} `;text.attachment = McDb.AttachmentPoint.kMiddleCenter;text.location = this.pt;text.textHeight = MxFun.screenCoordLong2Doc(this.height);// 开导标注颜料text.trueColor = this.trueColor;draw.trueColor = this.trueColor;// 画图标注对象draw.drawEntity(text);draw.drawEntity(pl);}/** 开导标注点 */public setPoint(pt: McGePoint3d) {this.assertWrite();this.pt = pt.clone();}/** 增多过火 */public addVertex(pt: McGePoint3d, dbulge?: number) {this.assertWrite();this.ptArr.push(pt.clone());this.dbulges.push(dbulge ? dbulge : 0);}/** 取得标注点 */public getPoint() {return this.pt;}}
3.2 注册自界说类信息
开动代码如下:
newMcDbTestAreaComment().rxInit();
3.3 编写相貌,调用McDbTestAreaComment自界说面积标注类完了测量面积(多边形、矩形)功能
3.3.1 测量多边形面积
测量多边形面积需要衔接取点,且取点不少于3个。期骗MxCADUiPrPoint取点对象在图纸中轮回取点来详情需要测量多边形的位置与大小,并通过MxCADUiPrPoint.setKeyWords()为其开导关节字列表,使用户简略完了自主废弃操作或提前边界操作,节略交互,参考代码如下:
// 测量多边形面积async function Mx_Area() {// 多边形过火数值const ptArr: McGePoint3d[] = [];// 取得多边形第一个过火const getPt1 = new MxCADUiPrPoint();getPt1.setMessage('请遴荐面积的第一个点');const pt1 = await getPt1.go();if (!pt1) return;ptArr.push(pt1);// 轮回取点while (true) {const getPt = new MxCADUiPrPoint();getPt.setMessage('请遴荐下一个点');getPt.setKeyWords("[回退(B)/边界(O)]");getPt.setUserDraw((pt, pw) => {const draw_pl = new McDbPolyline();ptArr.forEach(point => {draw_pl.addVertexAt(point, 0, 0.1, 0.1);});draw_pl.addVertexAt(pt);pw.drawMcDbEntity(draw_pl);});const pt = await getPt.go();const key = getPt.keyWordPicked();if (key === 'B') {// 回退到上一个取点对象if (ptArr.length > 1) {ptArr.pop();}} else if (key === 'O') {// 边界取点break;} else {if (!pt) break;ptArr.push(pt);}};// 测量多边形至少取3个点if(ptArr.length < 2) return;// 构造面积标注对象并开导标注位置const mxcad = MxCpp.getCurrentMxCAD();const area = new McDbTestAreaComment();ptArr.forEach((point) => {area.addVertex(point, 0);});const position = new MxCADUiPrPoint();position.setMessage('请遴荐面积标注的位置');position.setUserDraw((pt, pw) => {pw.setColor(0xFF0000)area.setPoint(pt);pw.drawMcDbEntity(area);})const positionPt = await position.go();if (!positionPt) return;area.setPoint(positionPt);area.trueColor = new McCmColor(255,0,0)mxcad.drawEntity(area);}
3.3.2 测量矩形面积
测量矩形面积只需要中式矩形的两个角点就不错详情矩形的大小与位置,参考代码如下:
// 矩形面积async function Mx_ReactArea() {// 开导测量矩形的两个角点const getPt1 = new MxCADUiPrPoint();getPt1.setMessage('请遴荐矩形的角点1');const pt1 = await getPt1.go();if (!pt1) return;const getPt2 = new MxCADUiPrPoint();getPt2.setMessage('请遴荐矩形的角点2');// 动态画图矩形getPt2.setUserDraw((pt, pw) => {const pl = new McDbPolyline();pl.isClosed = true;pl.addVertexAt(pt1);pl.addVertexAt(new McGePoint3d(pt.x, pt1.y));pl.addVertexAt(pt);pl.addVertexAt(new McGePoint3d(pt1.x, pt.y));pw.drawMcDbEntity(pl);})const pt2 = await getPt2.go();if (!pt2) return;// 构造面积标注const reactArea = new McDbTestAreaComment();reactArea.isReact = true;reactArea.addVertex(pt1);reactArea.addVertex(pt2);// 开导标注位置const getPos = new MxCADUiPrPoint();getPos.setMessage('请开导面积标注的位置');getPos.setUserDraw((pt, pw) => {pw.setColor(0xFF0000)reactArea.setPoint(pt);pw.drawMcDbEntity(reactArea);});const position = await getPos.go();if (!position) return;reactArea.setPoint(position);// 开导标注颜料reactArea.trueColor = new McCmColor(255, 0, 0)MxCpp.getCurrentMxCAD().drawEntity(reactArea);}
效能如下图:
五月桃色网