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


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


回复
 
主题工具 搜索本主题 显示模式
旧 2009-04-29, 05:10 PM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】『已解决』把框选后的线与圆弧合并成多段线 - 精华帖集合

『已解决』把框选后的线与圆弧合并成多段线 - 精华帖集合
www.dimcax.com
『已解决』把框选后的线与圆弧合并成多段线
通过“递归”原理,把框选后的线与圆弧合并成多段线(可以合并成多条) 原以为objectarx.net有合并多段线的功能,找遍整个internet,才发现... 自己动手写了几个过程,请高手帮我看看,还有没有更简便的方法。
<commandmethod("test")> public sub mypolyline()
dim ed as editor = application.documentmanager.mdiactivedocument.editor
dim values() as typedvalue = {new typedvalue(dxfcode.start, "line,arc")}
dim sfilter as new selectionfilter(values)
dim opts as new promptselectionoptions()
opts.messageforadding = vbcrlf & "选择线与圆弧"
dim res as promptselectionresult
res = ed.getselection(opts, sfilter)
if res.status = promptstatus.ok then
if not res.value is nothing then
dim ss as autodesk.autocad.editorinput.selectionset = res.value
dim idarray as objectid() = ss.getobjectids()
dim obc as new objectidcollection
for each id as objectid in idarray
obc.add(id)
next
if obc.count > 1 then
dwgpolyline(obc)
for each id as objectid in idarray '把合并后的实体删除
dim db as database = hostapplicationservices.workingdatabase
using trans as transaction = db.transactionmanager.starttransaction()
dim ent as entity = trans.getobject(id, openmode.forwrite, false)
ent.erase()
trans.commit()
end using
next
end if
end if
end if
end sub
复制代码
主过程
private sub dwgpolyline(byval objectidc as objectidcollection) '画多段线
dim ed as editor = application.documentmanager.mdiactivedocument.editor
dim pl as new polyline
dim tobjectid as objectidcollection = objectidc
dim whilebo as boolean = true
do while whilebo
for each id as objectid in tobjectid
dim db as database = hostapplicationservices.workingdatabase
using trans as transaction = db.transactionmanager.starttransaction()
dim c as curve = trans.getobject(id, openmode.forwrite, false)
dim entname as string = c.gettype.name
if pl.numberofvertices = 0 then
if entname = "arc" then '如果是圆弧,设置polyline的凸起值
pl.addvertexat(0, c.startpoint.convert2d(new plane), getbulge(c, true), 0, 0)
else
pl.addvertexat(0, c.startpoint.convert2d(new plane), 0, 0, 0)
end if
pl.addvertexat(1, c.endpoint.convert2d(new plane), 0, 0, 0)
tobjectid.remove(id) '该实体已经合并完,删除该实体id在组数
whilebo = true
exit for
else
if c.startpoint = pl.getpoint3dat(0) then
if entname = "arc" then
pl.addvertexat(0, c.endpoint.convert2d(new plane), getbulge(c, false), 0, 0)
else
pl.addvertexat(0, c.endpoint.convert2d(new plane), 0, 0, 0)
end if
tobjectid.remove(id)
whilebo = true
exit for
elseif c.endpoint = pl.getpoint3dat(0) then
if entname = "arc" then
pl.addvertexat(0, c.startpoint.convert2d(new plane), getbulge(c, true), 0, 0)
else
pl.addvertexat(0, c.startpoint.convert2d(new plane), 0, 0, 0)
end if
tobjectid.remove(id)
whilebo = true
exit for
elseif c.startpoint = pl.getpoint3dat(pl.numberofvertices - 1) then
if entname = "arc" then
pl.setbulgeat(pl.numberofvertices - 1, getbulge(c, true))
end if
pl.addvertexat(pl.numberofvertices, c.endpoint.convert2d(new plane), 0, 0, 0)
tobjectid.remove(id)
whilebo = true
exit for
elseif c.endpoint = pl.getpoint3dat(pl.numberofvertices - 1) then
if entname = "arc" then
pl.setbulgeat(pl.numberofvertices - 1, getbulge(c, false))
end if
pl.addvertexat(pl.numberofvertices, c.startpoint.convert2d(new plane), 0, 0, 0)
tobjectid.remove(id)
whilebo = true
exit for
else
whilebo = false
end if
end if
trans.commit()
end using
next
if tobjectid.count < 1 then
whilebo = false
end if
loop
drawentity(pl) '把新建的polyline加到数据库
if tobjectid.count > 0 then '判断是否把线与圆弧合并完
dwgpolyline(tobjectid)
end if
end sub
复制代码
[ ]
模具相关绿色软件
皆唯网
辅助过程
private function getbulge(byval a as arc, byval sebo as boolean) as double '计算凸起值
dim bulge as double
dim l1 as double = jvtools.jvtool.getdistance(a.startpoint, a.endpoint)
dim angle as double = a.endangle - a.startangle
if angle < 0 then
angle = math.pi * 2 + angle '计算圆弧总角度
end if
if angle > math.pi then '判断是否大于180度
bulge = a.radius + math.sqrt(a.radius ^ 2 - (l1 / 2) ^ 2) '计算凸起值
else
bulge = a.radius - math.sqrt(a.radius ^ 2 - (l1 / 2) ^ 2) '计算凸起值
end if
dim pt2 as point3d = a.getpointatdist(a.getdistanceatparameter(a.endparam) / 2) '取中点
dim tempdouble as double
if sebo = true then '判断方向
tempdouble = ptside(a.startpoint, pt2, a.endpoint)
else
tempdouble = ptside(a.endpoint, pt2, a.startpoint)
end if
if tempdouble > 0 then '判断圆弧是凸向哪边
return -bulge / (l1 / 2)
elseif tempdouble < 0 then
return bulge / (l1 / 2)
end if
end function
private shared function ptside(byval pt1 as point3d, byval pt2 as point3d, byval pt3 as point3d) as double '判断圆弧是凸向哪边
dim vect1 as vector3d = pt1.getvectorto(pt2)
dim vect2 as vector3d = pt1.getvectorto(pt3)
return vect2.x * vect1.y - vect1.x * vect2.y
end function
public function drawentity(byval ent as entity) as objectid '加入实体到数据库
dim ed as editor = application.documentmanager.mdiactivedocument.editor
try
dim db as database = hostapplicationservices.workingdatabase
using trans as transaction = db.transactionmanager.starttransaction()
dim bt as blocktable = directcast(trans.getobject(db.blocktableid, openmode.forread), blocktable)
dim btr as blocktablerecord = directcast(trans.getobject(bt(blocktablerecord.modelspace), openmode.forwrite), blocktablerecord)
btr.appendentity(ent)
trans.addnewlycreateddbobject(ent, true)
trans.commit()
end using
return ent.objectid
catch ex as exception
ed.writemessage(ex.message)
return objectid.null
end try
end function
复制代码
[ ]
模具相关绿色软件
皆唯网
不错,暂时没有想到更好的办法。
没这么复杂吧,我用lisp写的很短的代码~
原帖由 nigma 于 2007-11-4 04:37 pm 发表 没这么复杂吧,我用lisp写的很短的代码~
能否把你的思路说说,我这个过程是可以合并成很多条多段线。事先不知道,框选到的线是可以合并成多少条的。
模具相关绿色软件
皆唯网
jvtools 是什么??
原帖由 david.xw 于 2007-11-23 01:02 am 发表 jvtools 是什么??
这是个辅助过程,用来计算两点间的距离的。 public shared function getdistance(byval pt1 as point3d, byval pt2 as point3d) as double dim v1 as vector3d = pt1.getvectorto(pt2) return v1.length end function
模具相关绿色软件
皆唯网
一个样,本文对我帮助很大!!
c#代码
在李赫兄的帮助下 ,把原来的vb.net转为c#代码,并调试成功
[commandmethod("test")]
public void mypolyline()
{
autodesk.autocad.editorinput.editor ed = autodesk.autocad.applicationservices.application.documentmanager.mdiactivedocument.editor;
documentlock doclock = autodesk.autocad.applicationservices.application.documentmanager.mdiactivedocument.lockdocument();
typedvalue[] values = { new typedvalue((int)dxfcode.start, "line,arc") };
selectionfilter sfilter = new selectionfilter(values);
promptselectionoptions opts = new promptselectionoptions();
opts.messageforadding = "\n选择线与圆弧";
promptselectionresult res = default(promptselectionresult);
res = ed.getselection(opts, sfilter);
if (res.status == promptstatus.ok)
{
if ((res.value != null))
{
autodesk.autocad.editorinput.selectionset ss = res.value;
objectid[] idarray = ss.getobjectids();
objectidcollection obc = new objectidcollection();
foreach (objectid id in idarray)
{
obc.add(id);
}
if (obc.count > 1)
{
dwgpolyline(obc);
foreach (objectid id in idarray)
{
//把合并后的实体删除
database db = hostapplicationservices.workingdatabase;
using (transaction trans = db.transactionmanager.starttransaction())
{
entity ent = trans.getobject(id, openmode.forwrite, false) as entity;
ent.erase();
trans.commit();
}
}
}
}
}
doclock.dispose();
}
复制代码
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
回复


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

高级搜索
显示模式

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

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



所有的时间均为北京时间。 现在的时间是 12:14 AM.


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