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