coldmoon 发表于 2007-4-30 17:08:00

菜鸟求助!折腾了我一下午的问题

本帖最后由 作者 于 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/>&nbsp;// TODO: Implement the command<br/>&nbsp;&nbsp;&nbsp; AcDbBlockTable *pBT;<br/>&nbsp;acdbHostApplicationServices()-&gt;workingDatabase()-&gt;getSymbolTable(pBT,AcDb::kForRead);//取出当前数据库的实体表<br/>&nbsp;&nbsp; <br/>&nbsp;AcDbBlockTableIterator* pIterator;<br/>&nbsp;pBT-&gt;newIterator(pIterator);<br/>&nbsp;pBT-&gt;close();</p><p>AcDbLine *pLine,*pLine1;&nbsp;<br/>Cmyline myline1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //自己定义的直线类,用于存储各个直线的端点<br/>&nbsp;</p><p>&nbsp;&nbsp;int i=0,Count=0;</p><p>&nbsp;for (pIterator-&gt;start();!pIterator-&gt;done();pIterator-&gt;step())<br/>&nbsp;{<br/>&nbsp;&nbsp;AcDbBlockTableRecord *pBTR;<br/>&nbsp;&nbsp;pIterator-&gt;getRecord(pBTR,AcDb::kForRead,Adesk::kFalse);</p><p>&nbsp;&nbsp;AcDbBlockTableRecordIterator* pIrterator;<br/>&nbsp;&nbsp;pBTR-&gt;newIterator(pIrterator);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //建立迭代器&nbsp;&nbsp;<br/>&nbsp;&nbsp;pBTR-&gt;close();</p><p>&nbsp;&nbsp;&nbsp;while(!pIrterator-&gt;done())<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;AcDbEntity*pEnt;<br/>&nbsp;&nbsp;&nbsp;pIrterator-&gt;getEntity(pEnt,AcDb::kForWrite); //打开实体,进行编辑<br/>&nbsp;&nbsp;&nbsp;if (pEnt-&gt;isA() == AcDbLine::desc())//若是线形,则继续<br/>&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;pLine=AcDbLine::cast(pEnt);<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;myline1.startpt=pLine-&gt;startPoint();//将直线的两端点加以保存<br/>&nbsp;&nbsp;&nbsp;&nbsp;myline1.endpt=pLine-&gt;endPoint();<br/>&nbsp;&nbsp;&nbsp;&nbsp;</p><p>&nbsp;&nbsp;&nbsp; pIrterator-&gt;step();<br/>&nbsp;&nbsp;&nbsp;&nbsp;i++;<br/>&nbsp;&nbsp;&nbsp;&nbsp;Count++;<br/>&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;delete pIrterator;<br/>&nbsp;}</p><p>&nbsp;for (i=0; i&lt;Count;i++)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;for (int j=i+1;j&lt;Count;j++)<br/>&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;judgement(myline1,myline1);<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;acdbHostApplicationServices()-&gt;workingDatabase()<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt;getSymbolTable(pBT,AcDb::kForWrite);<br/>&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;pBT-&gt;getAt(ACDB_MODEL_SPACE, pBTR,AcDb::kForWrite);</p><p>&nbsp;&nbsp;&nbsp;&nbsp;delete pLine;//删除图上的重复的线(<font color="#ee3d11">这里错的吧 我不知道怎么删除线 相关资料没找到</font>)<br/>&nbsp;&nbsp;&nbsp;&nbsp;delete pLine;</p><p>&nbsp;&nbsp;&nbsp;&nbsp;pLine1=new AcDbLine(get_startpoint,get_endpoint);//重画一条线代替重复的两条线<br/>&nbsp;&nbsp;&nbsp;&nbsp;pLine1-&gt;close();<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;for (i=0;i&lt;Count;i++)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;pLine-&gt;close();<br/>&nbsp;&nbsp; delete pIterator;<br/>}</p><p><br/>AcGePoint3d judgement(Cmyline Cmyline1,Cmyline Cmyline2)<br/>{<br/>&nbsp;double fslope1,fslope2;//斜率<br/>&nbsp;double Sx1,Sy1,Ex1,Ey1;//比较时用到的第一条直线的两个端点坐标变量<br/>&nbsp;double Sx2,Sy2,Ex2,Ey2;//第二条直线端点坐标变量</p><p>&nbsp;Sx1=Cmyline1.startpt.x;<br/>&nbsp;Sy1=Cmyline1.startpt.y;<br/>&nbsp;Ex1=Cmyline1.endpt.x;<br/>&nbsp;Ey1=Cmyline1.endpt.y;</p><p>&nbsp;Sx2=Cmyline2.startpt.x;<br/>&nbsp;Sy2=Cmyline2.startpt.y;<br/>&nbsp;Ex2=Cmyline2.endpt.x;<br/>&nbsp;Ey2=Cmyline2.endpt.y;</p><p>&nbsp;fslope1=(Ey1 - Sy1) / (Ex1 - Sx1);<br/>&nbsp;fslope2=(Ey2 - Sy2) / (Ex2 - Sx2);</p><p>&nbsp;double Max_distance,distance1,distance2,distance3,distance4;</p><p>&nbsp;distance1 = (Sx1 - Ex1) * (Sx1 - Ex1) + (Sy1 - Ey1) * (Sy1 - Ey1);//第一条直线两端点间的距离<br/>&nbsp;distance2 = (Sx2 - Ex2) * (Sx2 - Ex2) + (Sy2 - Ey2) * (Sy2 - Ey2);//第二条直线两端点间的距离<br/>&nbsp;distance3 = (Sx1 - Ex2) * (Sx1 - Ex2) + (Sy1 - Ey2) * (Sy1 - Ey2);//第一条直线起点于第二条直线终点间的距离<br/>&nbsp;distance4 = (Sx2 - Ex1) * (Sx2 - Ex1) + (Sy2 - Ey1) * (Sy2 - Ey1);//第一条直线终点与第二条直线起点间的距离</p><p>&nbsp;if ((fslope1==fslope2)<br/>&nbsp;&nbsp;&amp;&amp;((Sy2==(Sx2-Sx1)*fslope1+Sy1)||(Ey2=(Ex2-Sx1)*fslope1+Sy1))<br/>&nbsp;&nbsp;&amp;&amp;((Sx1&lt;=Sx2&amp;&amp;Sx2&lt;=Ex1)||(Sx1&lt;=Ex2&amp;&amp;Ex2&lt;=Ex1)))&nbsp;{<br/>&nbsp;&nbsp;Max_distance=distance1;<br/>&nbsp;&nbsp;if (distance2&gt;Max_distance)<br/>&nbsp;&nbsp;&nbsp;Max_distance=distance2;<br/>&nbsp;&nbsp;if (distance3&gt;Max_distance)<br/>&nbsp;&nbsp;&nbsp;Max_distance=distance3;<br/>&nbsp;&nbsp;if (distance4&gt;Max_distance)<br/>&nbsp;&nbsp;&nbsp;Max_distance=distance4;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (Max_distance==distance1)<br/>&nbsp;&nbsp;{&nbsp;get_startpoint.x=Sx1;<br/>&nbsp;&nbsp;&nbsp;get_startpoint.y=Sy1;<br/>&nbsp;&nbsp;&nbsp;get_endpoint.x=Ex1;<br/>&nbsp;&nbsp;&nbsp;get_endpoint.y=Ey1;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; else if (Max_distance==distance2)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;get_startpoint.x=Sx2;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get_startpoint.y=Sy2;<br/>&nbsp;&nbsp;&nbsp;get_endpoint.x=Ex2;<br/>&nbsp;&nbsp;&nbsp;get_endpoint.y=Ey2;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;else if (Max_distance==distance3)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;get_startpoint.x=Sx1;<br/>&nbsp;&nbsp;&nbsp;get_startpoint.y=Sy1;<br/>&nbsp;&nbsp;&nbsp;get_endpoint.x=Ex2;<br/>&nbsp;&nbsp;&nbsp;get_endpoint.y=Ey2;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;else if (Max_distance==distance4)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;get_startpoint.x=Sx2;<br/>&nbsp;&nbsp;&nbsp;get_startpoint.y=Sy2;<br/>&nbsp;&nbsp;&nbsp;get_endpoint.x=Ex1;<br/>&nbsp;&nbsp;get_endpoint.y=Ey1;</p><p>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;else<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;ads_printf("\n error please restart the program !");<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;}<br/>&nbsp;}<br/>&nbsp;return get_endpoint,get_startpoint;(<font color="#dd4822">是不是可以返回两个值,我不太清楚</font>)<br/>&nbsp;<br/>}</p><p><br/></p>

zhang007 发表于 2007-5-2 00:49:00

<p>myline1.startpt=pLine-&gt;startPoint();//将直线的两端点加以保存<br/>&nbsp;&nbsp;&nbsp;&nbsp;myline1.endpt=pLine-&gt;endPoint();<br/></p><p><font color="#000000">&nbsp;pEnt-&gt;erase();<br/>&nbsp;pEnt-&gt;close();</font></p>

zhang007 发表于 2007-5-2 01:03:00

<p>其实不用这么复杂,记住一条线的端点坐标后,再用后来的线端点逐一与它比较,如果相差不大就删掉后面的线.打开实体时一定要试试是不是能以写打开,否则CAD就崩了</p><p>好像用选择集更好</p><p></p>

coldmoon 发表于 2007-5-2 12:54:00

<p>问题已经解决 结贴</p>
页: [1]
查看完整版本: 菜鸟求助!折腾了我一下午的问题