本帖最后由 作者 于 2007-4-30 21:42:26 编辑
以下是我所编写的一段代码,目的是将2条重合的线合并成一条.编译可以通过,但在CAD上运行老是出错.本菜鸟对ARX 实在不熟,对此问题也是实在没折了,万望各位大侠,不吝赐教. // ObjectARX defined commands #include "StdAfx.h" #include "StdArx.h" #include "myline.h" AcGePoint3d get_startpoint,get_endpoint; AcGePoint3d judgement(Cmyline Cmyline1,Cmyline Cmyline2); // This is command 'REPLINE' void zzh_hfutrepline() { // TODO: Implement the command AcDbBlockTable *pBT; acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBT,AcDb::kForRead);//取出当前数据库的实体表 AcDbBlockTableIterator* pIterator; pBT->newIterator(pIterator); pBT->close(); AcDbLine *pLine[20],*pLine1; Cmyline myline1[20]; //自己定义的直线类,用于存储各个直线的端点 int i=0,Count=0; for (pIterator->start();!pIterator->done();pIterator->step()) { AcDbBlockTableRecord *pBTR; pIterator->getRecord(pBTR,AcDb::kForRead,Adesk::kFalse); AcDbBlockTableRecordIterator* pIrterator; pBTR->newIterator(pIrterator); //建立迭代器 pBTR->close(); while(!pIrterator->done()) { AcDbEntity*pEnt; pIrterator->getEntity(pEnt,AcDb::kForWrite); //打开实体,进行编辑 if (pEnt->isA() == AcDbLine::desc())//若是线形,则继续 { pLine=AcDbLine::cast(pEnt); myline1.startpt=pLine->startPoint();//将直线的两端点加以保存 myline1.endpt=pLine->endPoint(); pIrterator->step(); i++; Count++; } } } delete pIrterator; } for (i=0; i<Count;i++) { for (int j=i+1;j<Count;j++) { judgement(myline1,myline1[j]); acdbHostApplicationServices()->workingDatabase() ->getSymbolTable(pBT,AcDb::kForWrite); pBT->getAt(ACDB_MODEL_SPACE, pBTR,AcDb::kForWrite); delete pLine;//删除图上的重复的线(这里错的吧 我不知道怎么删除线 相关资料没找到) delete pLine[j]; pLine1=new AcDbLine(get_startpoint,get_endpoint);//重画一条线代替重复的两条线 pLine1->close(); } } for (i=0;i<Count;i++) { pLine->close(); delete pIterator; } AcGePoint3d judgement(Cmyline Cmyline1,Cmyline Cmyline2) { double fslope1,fslope2;//斜率 double Sx1,Sy1,Ex1,Ey1;//比较时用到的第一条直线的两个端点坐标变量 double Sx2,Sy2,Ex2,Ey2;//第二条直线端点坐标变量
Sx1=Cmyline1.startpt.x; Sy1=Cmyline1.startpt.y; Ex1=Cmyline1.endpt.x; Ey1=Cmyline1.endpt.y; Sx2=Cmyline2.startpt.x; Sy2=Cmyline2.startpt.y; Ex2=Cmyline2.endpt.x; Ey2=Cmyline2.endpt.y; fslope1=(Ey1 - Sy1) / (Ex1 - Sx1); fslope2=(Ey2 - Sy2) / (Ex2 - Sx2); double Max_distance,distance1,distance2,distance3,distance4; distance1 = (Sx1 - Ex1) * (Sx1 - Ex1) + (Sy1 - Ey1) * (Sy1 - Ey1);//第一条直线两端点间的距离 distance2 = (Sx2 - Ex2) * (Sx2 - Ex2) + (Sy2 - Ey2) * (Sy2 - Ey2);//第二条直线两端点间的距离 distance3 = (Sx1 - Ex2) * (Sx1 - Ex2) + (Sy1 - Ey2) * (Sy1 - Ey2);//第一条直线起点于第二条直线终点间的距离 distance4 = (Sx2 - Ex1) * (Sx2 - Ex1) + (Sy2 - Ey1) * (Sy2 - Ey1);//第一条直线终点与第二条直线起点间的距离 if ((fslope1==fslope2) &&((Sy2==(Sx2-Sx1)*fslope1+Sy1)||(Ey2=(Ex2-Sx1)*fslope1+Sy1)) &&((Sx1<=Sx2&&Sx2<=Ex1)||(Sx1<=Ex2&&Ex2<=Ex1))) { Max_distance=distance1; if (distance2>Max_distance) Max_distance=distance2; if (distance3>Max_distance) Max_distance=distance3; if (distance4>Max_distance) Max_distance=distance4; if (Max_distance==distance1) { get_startpoint.x=Sx1; get_startpoint.y=Sy1; get_endpoint.x=Ex1; get_endpoint.y=Ey1; } else if (Max_distance==distance2) { get_startpoint.x=Sx2; get_startpoint.y=Sy2; get_endpoint.x=Ex2; get_endpoint.y=Ey2; } else if (Max_distance==distance3) { get_startpoint.x=Sx1; get_startpoint.y=Sy1; get_endpoint.x=Ex2; get_endpoint.y=Ey2; } else if (Max_distance==distance4) { get_startpoint.x=Sx2; get_startpoint.y=Sy2; get_endpoint.x=Ex1; get_endpoint.y=Ey1; } else { ads_printf("\n error please restart the program !"); } } return get_endpoint,get_startpoint;(是不是可以返回两个值,我不太清楚) }
|