|
高级会员
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
|
【转帖】菜鸟求助!折腾了我一下午的问题
菜鸟求助!折腾了我一下午的问题
菜鸟求助!折腾了我一下午的问题
以下是我所编写的一段代码,目的是将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[i]=acdbline::cast(pent);
myline1[i].startpt=pline[i]->startpoint();//将直线的两端点加以保存
myline1[i].endpt=pline[i]->endpoint();
pirterator->step();
i++;
count++;
}
}
}
delete pirterator;
}
for (i=0; i<count;i++)
{
for (int j=i+1;j<count;j++)
{
judgement(myline1[i],myline1[j]);
acdbhostapplicationservices()->workingdatabase()
->getsymboltable(pbt,acdb::kforwrite);
pbt->getat(acdb_model_space, pbtr,acdb::kforwrite);
delete pline[i];//删除图上的重复的线(这里错的吧 我不知道怎么删除线 相关资料没找到)
delete pline[j];
pline1=new acdbline(get_startpoint,get_endpoint);//重画一条线代替重复的两条线
pline1->close();
}
}
for (i=0;i<count;i++)
{
pline[i]->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;(是不是可以返回两个值,我不太清楚)
}
myline1[i].startpt=pline[i]->startpoint();//将直线的两端点加以保存
myline1[i].endpt=pline[i]->endpoint();
pent->erase();
pent->close();
其实不用这么复杂,记住一条线的端点坐标后,再用后来的线端点逐一与它比较,如果相差不大就删掉后面的线.打开实体时一定要试试是不是能以写打开,否则cad就崩了
好像用选择集更好
问题已经解决 结贴
|