lisp 多段线转面域_Objectarx之相交矩形求并集 面域转多段线

本文介绍了如何使用LISP在AutoCAD中将两个相交矩形合并为一个面域,再将面域转换为封闭的多段线。通过DrawRecJig类实现矩形的绘制,并使用AcDbPolyline和AcDbRegion进行面域操作,最后将面域转换回多段线。关键步骤包括:拾取矩形插入点,创建相交矩形,通过布尔运算合并面域,以及将面域转为多段线。
摘要由CSDN通过智能技术生成

152abec46c83ce72d25724eba539b2d5.png

主要思路:拾取一个点作为矩形的插入点,分别以该点进行两次jig操作,就能得到白色的两个相交的polyline,之后需要变成红色的封闭多段线。做法就是:求出两个白色矩形的面域,然后通过bool操作的并集,使得两个面域合并成一个面域。最后就把这个面域在转换成polyline就完成了。其中要注意, 要想在使用向导创建的工程里使用面域的类,需要在头文件:StdAfx.h 中写上这句宏命令#define _BREP_SUPPORT_,就不需要引用库文件了。下面给出详细代码:

拖动矩形的jig操作:

DrawRecJig::DrawRecJig()

{

pl = NULL;

}

DrawRecJig::~DrawRecJig()

{

}

AcEdJig::DragStatus DrawRecJig::sampler()

{

setUserInputControls((AcEdJig::UserInputControls)(

AcEdJig::kAccept3dCoordinates |

AcEdJig::kNoNegativeResponseAccepted |

AcEdJig::kNullResponseAccepted

));

AcGePoint3d ptTemp;

AcEdJig::DragStatus status = acquirePoint(ptTemp);

if (ptTemp != ptCurrent) {

ptCurrent= ptTemp;

}

else if (status == DragStatus::kNormal) {

return AcEdJig::kNoChange;

}

return status;

}

bool DrawRecJig::update()

{

double dist = CConvertUtil::ToPoint2d(ptCenter).distanceTo(CConvertUtil::ToPoint2d(ptCurrent));

// 提取两个角点的坐标值

double x1 = ptCenter.x, x2 = ptCurrent.x;

double y1 = ptCenter.y, y2 = ptCurrent.y;

// 计算矩形的角点

AcGePoint2d ptLeftBottom(min(x1, x2), min(y1, y2));

AcGePoint2d ptRightBottom(max(x1, x2), min(y1, y2));

AcGePoint2d ptRightTop(max(x1, x2), max(y1, y2));

AcGePoint2d ptLeftTop(min(x1, x2), max(y1, y2));

pl->setPointAt(0, ptLeftBottom);

pl->setPointAt(1, ptLeftTop);

pl->setPointAt(2, ptRightTop);

pl->setPointAt(3, ptRightBottom);

return true;

}

AcDbEntity * DrawRecJig::entity() const

{

return pl;

}

bool DrawRecJig::DoIt(AcGePoint3d & ptCenter, AcDbObjectId &plId)

{

this->ptCenter = ptCenter;

pl = new AcDbPolyline();

for (int i = 0; i < 4; i++)

{

pl->addVertexAt(i, CConvertUtil::ToPoint2d(ptCenter));

}

pl->setClosed(true);

setDispPrompt(L"请拖动鼠标:");

if (drag()==kNormal) {

//加入到模型空间的封装

plId=CDwgDataBaseUtil::PostToModelSpace(pl);

return true;

}

else {

delete pl;

return false;

}

}

命令类:

static void ECDMyGroupEUnion() {

DrawRecJig recJig = DrawRecJig();

ads_point pt;

AcDbPolyline *pl1 = NULL, *pl2 = NULL;

if (acedGetPoint(NULL, L"\n请选择插入点:", pt) == RTNORM) {

AcDbObjectId oId1, oId2;

if (recJig.DoIt(asPnt3d(pt), oId1)) {

pl1 = AcDbPolyline::cast(recJig.entity());

}

if (recJig.DoIt(asPnt3d(pt), oId2)) {

pl2 = AcDbPolyline::cast(recJig.entity());

}

AcDbVoidPtrArray curveSegments;

AcDbVoidPtrArray regions;

curveSegments.append(pl1);

curveSegments.append(pl2);

if (AcDbRegion::createFromCurves(curveSegments, regions) == Acad::eOk) {

AcDbRegion *region =(AcDbRegion *)regions[0];

region->booleanOper(AcDb::BoolOperType::kBoolUnite, (AcDbRegion *)regions[1]);

AcGePoint3dArray ptArr;

CRegionUtil::GetRegionPoints(region, ptArr);

for (int i = 0; i < regions.length (); i++)

{

AcDbRegion *regionTemp = (AcDbRegion *)regions[i];

regionTemp->erase();

regionTemp->close();

}

region->close();

AcDbPolyline *plTemp1 = new AcDbPolyline();

for (int i = 0; i

{

plTemp1->addVertexAt(plTemp1->numVerts(), CConvertUtil::ToPoint2d(ptArr.at(i)), 0, 0, 0);

}

plTemp1->setClosed(Adesk::kTrue);

plTemp1->setColorIndex(1);

CDwgDataBaseUtil::PostToModelSpace(plTemp1);

plTemp1->close();

}

pl1->close();

pl2->close();

}

}

面域转AcGePoint3d:

void CRegionUtil::GetRegionPoints(AcDbRegion* pRegion,AcGePoint3dArray &points)

{

AcBrBrep*pBrep = new AcBrBrep;

pBrep->set(*pRegion);

AcBrBrepFaceTraverser brFaTrav;

for (brFaTrav.setBrep(*pBrep); !brFaTrav.done(); brFaTrav.next())

{

AcBrFaceLoopTraverser faLoTrav;

AcBrFace face;

brFaTrav.getFace(face);

for (faLoTrav.setFace(face); !faLoTrav.done(); faLoTrav.next())

{

AcBrLoopEdgeTraverser loEdTrav;

if (loEdTrav.setLoop(faLoTrav) == AcBr::eOk)

{

for (; !loEdTrav.done(); loEdTrav.next())

{

AcBrEdge edge;

loEdTrav.getEdge(edge);

AcBrVertex start;

edge.getVertex1(start);

AcGePoint3d pt;

start.getPoint(pt);

points.append(pt);

}

} // else its an isolated loop

}

}

delete pBrep;

}

转自:https://www.cnblogs.com/HelloQLQ/p/12197133.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值