![]() |
【转帖】『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++的强大功能。 绝世好贴~顶一个 |
所有的时间均为北京时间。 现在的时间是 11:14 PM. |