几何尺寸与公差论坛------致力于产品几何量公差标准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:06 PM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】『theswamp』炸开多段线出错 - 精华帖集合

『theswamp』炸开多段线出错 - 精华帖集合
www.dimcax.com
『theswamp』炸开多段线出错
, ,
在 autocad 2008 中,下面的代码"curve.explode(xobjs)"行出现致命错误。如果我去掉explode部分,则不会出错。
public sub test()
dim ed as editor = application.documentmanager.mdiactivedocument.editor
dim db as database = application.documentmanager.mdiactivedocument.database
dim trans as transaction = db.transactionmanager.starttransaction()
dim curves_p as dbobjectcollection = nothing
dim curves_n as dbobjectcollection = nothing
dim xents as dbobjectcollection = nothing
dim xobjs as dbobjectcollection = nothing
try
dim seloptions as promptselectionoptions = new promptselectionoptions
seloptions.messageforadding = "select polyline(s) to offset:"
seloptions.allowduplicates = false
seloptions.singleonly = false
dim result as promptselectionresult = ed.getselection(seloptions)
if result.status <> promptstatus.ok then return
dim selset as selectionset = result.value
dim dbloptions as new promptdoubleoptions(controlchars.lf & "enter offset: ")
dbloptions.allownegative = false
dbloptions.allownone = false
dbloptions.allowzero = false
dim dbloptionsresult as promptdoubleresult = ed.getdouble(dbloptions)
if dbloptionsresult.status <> promptstatus.ok then return
dim offset as double = dbloptionsresult.value
dim objidarray as objectid() = selset.getobjectids()
dim btr as blocktablerecord = trans.getobject(db.currentspaceid, openmode.forwrite)
dim ent as entity
dim pl as polyline
dim xent as entity
dim id as objectid
for each id in objidarray
ent = directcast(trans.getobject(id, openmode.forread), entity)
pl = directcast(ent, polyline)
curves_p = pl.getoffsetcurves(offset) 'positive offset
curves_n = pl.getoffsetcurves(-offset) 'negative offset
if curves_n isnot nothing then
for each curve as entity in curves_n
curve.colorindex = 1
btr.appendentity(curve)
trans.addnewlycreateddbobject(curve, true)
next
end if
if curves_p isnot nothing then
for each curve as entity in curves_p
curve.colorindex = 1
'explode/
curve.explode(xobjs)
for each xobj as dbobject in xobjs
xent = directcast(xobj, entity)
btr.appendentity(xent)
trans.addnewlycreateddbobject(xent, true)
next
'/explode
'btr.appendentity(curve)
'trans.addnewlycreateddbobject(curve, true)
next
end if
next id
trans.commit()
curves_p.dispose()
curves_n.dispose()
catch ex as exception
trans.abort()
msgbox(ex.message)
finally
trans.dispose()
end try
end sub
复制代码
下面是c#的代码,在我的机器上运行,没有出错。
[commandmethod("doit")]
public void test()
{
editor ed = application.documentmanager.mdiactivedocument.editor;
database db = application.documentmanager.mdiactivedocument.database;
transaction trans = db.transactionmanager.starttransaction();
dbobjectcollection curves_p = new dbobjectcollection(); //<<<-----
dbobjectcollection curves_n = new dbobjectcollection();//<<<-----
dbobjectcollection xobjs = new dbobjectcollection();//<<<-----
try
{
promptselectionoptions seloptions = new promptselectionoptions();
seloptions.messageforadding = "select polyline(s) to offset:";
seloptions.allowduplicates = false;
seloptions.singleonly = false;
promptselectionresult result = ed.getselection(seloptions);
if (result.status == promptstatus.ok)
{
selectionset selset = result.value;
promptdoubleoptions dbloptions = new promptdoubleoptions('\n' + "enter offset: ");
dbloptions.allownegative = false;
dbloptions.allownone = false;
dbloptions.allowzero = false;
promptdoubleresult dbloptionsresult = ed.getdouble(dbloptions);
if (dbloptionsresult.status == promptstatus.ok)
{
double offset = dbloptionsresult.value;
objectid[] objidarray = selset.getobjectids();
blocktablerecord btr = trans.getobject(db.currentspaceid, openmode.forwrite) as blocktablerecord;
entity ent = null;
polyline pl = null;
entity xent = null;
foreach (objectid id in objidarray)
{
ent = (entity)trans.getobject(id, openmode.forread);
pl = (polyline)ent;
curves_p = pl.getoffsetcurves(offset);
curves_n = pl.getoffsetcurves(-offset);
if (curves_n != null)
{
foreach (entity curve in curves_n)
{
curve.colorindex = 1;
btr.appendentity(curve);
trans.addnewlycreateddbobject(curve, true);
}
}
if (curves_p != null)
{
foreach (entity curve in curves_p)
{
curve.colorindex = 1;
curve.explode(xobjs);
foreach (dbobject xobj in xobjs)
{
xent = (entity)xobj;
if (xent.isnewobject)//<<<-----
{
btr.appendentity(xent);
trans.addnewlycreateddbobject(xent, true);
}
}
btr.appendentity(curve);
trans.addnewlycreateddbobject(curve, true);
}
}
}
trans.commit();
}
}
}
catch (systemexception ex)
{
trans.abort();
ed.writemessage(ex.message);
ed.writemessage(ex.stacktrace);
}
finally
{
trans.dispose();
}
}
复制代码
c#最适合开发autocad,因为它拥有vb容易的特点,却具有vc++的强大功能。
一些需要注意的地方: 1.你不能把getoffsetcurves() 生成的对象在炸开之间添加到数据库中。 2.炸开会把对象添加到一个集合中,所以我认为集合不能为空。 3.由于对象被添加到集合中,你只需要一个集合。还有因为这个集合可能对相同的对象包含多于一个的指针,所以你可能需要检查对象是否已经存在于数据库中。
[commandmethod("doit")]
public void test()
{
editor ed = application.documentmanager.mdiactivedocument.editor;
database db = application.documentmanager.mdiactivedocument.database;
transaction trans = db.transactionmanager.starttransaction();
dbobjectcollection curves = new dbobjectcollection(); //<<<-----
try
{
promptselectionoptions seloptions = new promptselectionoptions();
seloptions.messageforadding = "select polyline(s) to offset:";
seloptions.allowduplicates = false;
seloptions.singleonly = false;
promptselectionresult result = ed.getselection(seloptions);
if (result.status == promptstatus.ok)
{
selectionset selset = result.value;
promptdoubleoptions dbloptions = new promptdoubleoptions('\n' + "enter offset: ");
dbloptions.allownegative = false;
dbloptions.allownone = false;
dbloptions.allowzero = false;
promptdoubleresult dbloptionsresult = ed.getdouble(dbloptions);
if (dbloptionsresult.status == promptstatus.ok)
{
double offset = dbloptionsresult.value;
objectid[] objidarray = selset.getobjectids();
blocktablerecord btr = trans.getobject(db.currentspaceid, openmode.forwrite) as blocktablerecord;
foreach (objectid id in objidarray)
{
entity ent = (entity)trans.getobject(id, openmode.forread);
polyline pl = (polyline)ent;
foreach (entity curve in pl.getoffsetcurves(offset))
{
curve.explode(curves);
}
foreach (entity curve in pl.getoffsetcurves(-offset))
{
curve.explode(curves);
}
foreach (dbobject xobj in curves)
{
entity xent = (entity)xobj;
if (xent.isnewobject)//<<<-----
{
xent.colorindex = 1;
btr.appendentity(xent);
trans.addnewlycreateddbobject(xent, true);
}
}
}
trans.commit();
}
}
}
catch (systemexception ex)
{
trans.abort();
ed.writemessage(ex.message);
ed.writemessage(ex.stacktrace);
}
finally
{
trans.dispose();
}
}
复制代码
c#最适合开发autocad,因为它拥有vb容易的特点,却具有vc++的强大功能。
你可能需要对多段线选择进行过滤,请看一下 promptentityresult.addallowedclass(...) 。新的代码如下:
[commandmethod("doit")]
public void test()
{
editor ed = application.documentmanager.mdiactivedocument.editor;
typedvalue[] values = new typedvalue[]
{
new typedvalue((short)dxfcode.start, "lwpolyline")
};
selectionfilter filter = new selectionfilter(values);
database db = application.documentmanager.mdiactivedocument.database;
transaction trans = db.transactionmanager.starttransaction();
dbobjectcollection curves = new dbobjectcollection();
try
{
promptselectionoptions seloptions = new promptselectionoptions();
seloptions.messageforadding = "select polyline(s) to offset:";
seloptions.allowduplicates = false;
seloptions.singleonly = false;
promptselectionresult result = ed.getselection(seloptions,filter);
if (result.status == promptstatus.ok)
{
selectionset selset = result.value;
promptdoubleoptions dbloptions = new promptdoubleoptions('\n' + "enter offset: ");
dbloptions.allownegative = false;
dbloptions.allownone = false;
dbloptions.allowzero = false;
promptdoubleresult dbloptionsresult = ed.getdouble(dbloptions);
if (dbloptionsresult.status == promptstatus.ok)
{
double offset = dbloptionsresult.value;
objectid[] objidarray = selset.getobjectids();
blocktablerecord btr = trans.getobject(db.currentspaceid, openmode.forwrite) as blocktablerecord;
foreach (objectid id in objidarray)
{
entity ent = (entity)trans.getobject(id, openmode.forread);
polyline pl = (polyline)ent;
foreach (entity curve in pl.getoffsetcurves(offset))
{
using (curve)
{
curve.explode(curves);
continue;
}
}
foreach (entity curve in pl.getoffsetcurves(-offset))
{
using (curve)
{
curve.explode(curves);
continue;
}
}
foreach (dbobject xobj in curves)
{
entity xent = (entity)xobj;
if (xent.isnewobject)
{
xent.colorindex = 1;
btr.appendentity(xent);
trans.addnewlycreateddbobject(xent, true);
}
}
}
trans.commit();
}
}
}
catch (systemexception ex)
{
trans.abort();
ed.writemessage(ex.message);
ed.writemessage(ex.stacktrace);
}
finally
{
trans.dispose();
}
}
复制代码
c#最适合开发autocad,因为它拥有vb容易的特点,却具有vc++的强大功能。
下面是vb.net的代码:
<commandmethod("doit")> _
public sub test()
dim ed as editor = application.documentmanager.mdiactivedocument.editor
dim values as typedvalue() = new typedvalue() {new typedvalue(cshort(dxfcode.start), "lwpolyline")}
dim filter as new selectionfilter(values)
dim db as database = application.documentmanager.mdiactivedocument.database
dim trans as transaction = db.transactionmanager.starttransaction()
dim curves as new dbobjectcollection()
try
dim seloptions as new promptselectionoptions()
seloptions.messageforadding = "select polyline(s) to offset:"
seloptions.allowduplicates = false
seloptions.singleonly = false
dim result as promptselectionresult = ed.getselection(seloptions, filter)
if result.status = promptstatus.ok then
dim selset as selectionset = result.value
dim dbloptions as new promptdoubleoptions(controlchars.lf & "enter offset: ")
dbloptions.allownegative = false
dbloptions.allownone = false
dbloptions.allowzero = false
dim dbloptionsresult as promptdoubleresult = ed.getdouble(dbloptions)
if dbloptionsresult.status = promptstatus.ok then
dim offset as double = dbloptionsresult.value
dim objidarray as objectid() = selset.getobjectids()
dim btr as blocktablerecord = trycast(trans.getobject(db.currentspaceid, openmode.forwrite), blocktablerecord)
for each id as objectid in objidarray
dim ent as entity = directcast(trans.getobject(id, openmode.forread), entity)
dim pl as polyline = directcast(ent, polyline)
for each curve as entity in pl.getoffsetcurves(offset)
using curve
curve.explode(curves)
continue for
end using
next
for each curve as entity in pl.getoffsetcurves(-offset)
using curve
curve.explode(curves)
continue for
end using
next
for each xobj as dbobject in curves
dim xent as entity = directcast(xobj, entity)
if xent.isnewobject then
xent.colorindex = 1
btr.appendentity(xent)
trans.addnewlycreateddbobject(xent, true)
end if
next
next
trans.commit()
end if
end if
catch ex as systemexception
trans.abort()
ed.writemessage(ex.message)
ed.writemessage(ex.stacktrace)
finally
trans.dispose()
end try
end sub
复制代码
c#最适合开发autocad,因为它拥有vb容易的特点,却具有vc++的强大功能。
绝世好贴~顶一个
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
回复


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

高级搜索
显示模式

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

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



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


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