几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量  


返回   几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 » 仿射空间:CAX软件开发(三)二次开发与程序设计 » CAD二次开发 » AutoCAD二次开发 » ObjectARX(C++)
用户名
密码
注册 帮助 会员 日历 银行 搜索 今日新帖 标记论坛为已读


 
 
主题工具 搜索本主题 显示模式
旧 2009-04-18, 07:14 PM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】菜鸟求助!折腾了我一下午的问题

菜鸟求助!折腾了我一下午的问题
菜鸟求助!折腾了我一下午的问题
以下是我所编写的一段代码,目的是将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就崩了
好像用选择集更好
问题已经解决 结贴
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
 


主题工具 搜索本主题
搜索本主题:

高级搜索
显示模式

发帖规则
不可以发表新主题
不可以回复主题
不可以上传附件
不可以编辑您的帖子

vB 代码开启
[IMG]代码开启
HTML代码关闭



所有的时间均为北京时间。 现在的时间是 07:24 PM.


于2004年创办,几何尺寸与公差论坛"致力于产品几何量公差标准GD&T | GPS研究/CAD设计/CAM加工/CMM测量"。免责声明:论坛严禁发布色情反动言论及有关违反国家法律法规内容!情节严重者提供其IP,并配合相关部门进行严厉查处,若內容有涉及侵权,请立即联系我们QQ:44671734。注:此论坛须管理员验证方可发帖。
沪ICP备06057009号-2
更多