![]() |
|
| |
|
![]() |
|
主题工具 | 搜索本主题 | 显示模式 |
![]() |
#1 |
高级会员
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
![]() |
![]() 求助:加入实体出现einvalidinput 错误,附代码,请高手看看
www.dimcax.com 求助:加入实体出现einvalidinput 错误,附代码,请高手看看 , , openmode.forread); //遍历对象,加入到动态数组中 foreach (objectid id in btr_source) { entity ent = (entity)t_source.getobject(id, openmode.forread); ents.add(ent); } } using (transaction trans = doc.transactionmanager.starttransaction()) { //打开块表 blocktable bt = (blocktable)trans.getobject(doc.database.blocktableid, openmode.forread); //打开当前文档的模型空间 blocktablerecord btr = (blocktablerecord)trans.getobject(bt[blocktablerecord.modelspace], openmode.forwrite); //在当前模型空间加入实体 for (int i = 0; i < ents.count; i++) { objectid id = btr.appendentity((entity)ents); trans.addnewlycreateddbobject((entity)ents, true); } trans.commit(); } } } } catch (exception ex) { ed.writemessage("\nerror during insertblockrefwithattfromdwgfile: " + ex.message); } } einvalidinput 错误已解决,参照论坛上一位网友的 方法: entity e = (entity)ents[i]; entity ee =e.clone(); objectid id = btr.appendentity(ee); 可以执行成功,模型空间也加入了实体,但是接着出现内部错误:epermanentlyerased,请知道的朋友指点一下。 修改后的代码如下: private void copyfromotherdwg(string filename) { acadappser.document doc = acadappser.application.documentmanager.mdiactivedocument; editor ed = doc.editor; try { if (!file.exists(filename)) { //filename = hostapplicationservices.current.findfile(filename, doc.database, findfilehint.default); ed.writemessage("\n 不存在文件:" + filename + "\n"); return; } using (acadappser.documentlock doclock = doc.lockdocument()) //多文档要先这样,否则报至命错误,原因还是不太明白。 { using (database db_source = new database(false, true)) //从文件读取时第一个为false ,第二个表 不关联到当前文档, { //读dwg文件 db_source.readdwgfile(filename, fileshare.read, true, null); //为了让插入块的函数在多个图形文件打开的情况下起作用,你必须使用下面的函数把dwg文件关闭 db_source.closeinput(true); entity[] ents = new entity[1]; using (transaction t_source = db_source.transactionmanager.starttransaction()) { //打开源数据库的块表 blocktable bt_source = (blocktable)t_source.getobject(db_source.blocktableid, openmode.forread); //打开模型空间块表记录 blocktablerecord btr_source = (blocktablerecord)t_source.getobject(bt_source[blocktablerecord.modelspace], openmode.forread); //遍历对象,加入到动态数组中 int n = 0; foreach (objectid id in btr_source) { n++; } //改变数组的长度 array.resize<entity>(ref ents, n); n = 0; foreach (objectid id in btr_source) { ents[n++] = (entity)t_source.getobject(id, openmode.forread); } } using (transaction trans = doc.transactionmanager.starttransaction()) { //打开块表 blocktable bt = (blocktable)trans.getobject(doc.database.blocktableid, openmode.forread); //打开当前文档的模型空间 blocktablerecord btr = (blocktablerecord)trans.getobject(bt[blocktablerecord.modelspace], openmode.forwrite); //在当前模型空间加入实体 for (int i = 0; i < ents.length; i++) { entity e = (entity)ents[i].clone(); objectid id = btr.appendentity(e); trans.addnewlycreateddbobject(e, true); } trans.commit(); btr.dispose(); } } } } catch (exception ex) { ed.writemessage("\nerror during insertblockrefwithattfromdwgfile: " + ex.message); } } 下面的两个方法,首先调用getentsfromfile,得到外部文件的实体 再调用appendentitytomodelspace,将实体加入当前文档模型空间 出现错误:enotopenforwrite 我的代码读写模式就是forwrite,不知道为什么,请知道的人告诉我一下,谢谢! 我用的是vs ents) { acadappser.document doc = acadappser.application.documentmanager.mdiactivedocument; editor ed = doc.editor; try { if (!file.exists(filename)) { //filename = hostapplicationservices.current.findfile(filename, doc.database, findfilehint.default); ed.writemessage("\n 不存在文件:" + filename + "\n"); return; } using (acadappser.documentlock doclock = doc.lockdocument()) //多文档要先这样,否则报至命错误,原因还是不太明白。 { using (database db_source = new database(false, true)) //从文件读取时第一个为false ,第二个表 不关联到当前文档, { //读dwg文件 db_source.readdwgfile(filename, fileshare.read, true, null); //为了让插入块的函数在多个图形文件打开的情况下起作用,你必须使用下面的函数把dwg文件关闭 db_source.closeinput(true); using (transaction t_source = db_source.transactionmanager.starttransaction()) { //打开源数据库的块表 blocktable bt_source = (blocktable)t_source.getobject(db_source.blocktableid, openmode.forread); //打开模型空间块表记录 blocktablerecord btr_source = (blocktablerecord)t_source.getobject(bt_source[blocktablerecord.modelspace], openmode.forread); //遍历对象,加入到动态数组中 int n = 0; foreach (objectid id in btr_source) { n++; } //改变数组的长度 array.resize<entity>(ref ents, n); n = 0; foreach (objectid id in btr_source) { ents[n++] = (entity)t_source.getobject(id, openmode.forread); } } } } } catch (exception ex) { ed.writemessage("\nerror during getents: " + ex.message); } } //将实体加入当前模型空间 public static objectid appendentitytomodelspace(entity ent) { objectid entid = objectid.null; acadappser.document doc = acadappser.application.documentmanager.mdiactivedocument; editor ed = doc.editor; try { database db = hostapplicationservices.workingdatabase; using (acadappser.documentlock doclock = doc.lockdocument()) //多文档要先这样,否则报至命错误,原因还是不太明白。 { using (transaction trans = db.transactionmanager.starttransaction()) { blocktable bt = (blocktable)trans.getobject(db.blocktableid, openmode.forwrite); blocktablerecord btr = (blocktablerecord)trans.getobject(bt[blocktablerecord.modelspace], openmode.forwrite); entid = btr.appendentity(ent); trans.addnewlycreateddbobject(ent, true); trans.commit(); } } } catch (exception ex) { ed.writemessage("\nerror during appendentity: " + ex.message); } return entid; } 你的 t_source 没有提交事务处理,加上t_source.commit()试试看 谢谢楼上,我加上了,还是出错: trans.commit(); 这一句出错。 错误类型为:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。 ents = new entity[1]; if (!file.exists(filename)) { //filename = hostapplicationservices.current.findfile(filename, doc.database, findfilehint.default); throw new exception("\n 不存在文件:" + filename + "\n"); return; } using (acadappser.documentlock doclock = doc.lockdocument()) //多文档要先这样,否则报至命错误,原因还是不太明白。 { using (database db_source = new database(false, true)) //从文件读取时第一个为false ,第二个表 不关联到当前文档, { //读dwg文件 db_source.readdwgfile(filename, fileshare.read, true, null); //为了让插入块的函数在多个图形文件打开的情况下起作用,你必须使用下面的函数把dwg文件关闭 db_source.closeinput(true); using (transaction t_source = db_source.transactionmanager.starttransaction()) { try { //打开源数据库的块表 blocktable bt_source = (blocktable)t_source.getobject(db_source.blocktableid, openmode.forread); //打开模型空间块表记录 blocktablerecord btr_source = (blocktablerecord)t_source.getobject(bt_source[blocktablerecord.modelspace], openmode.forread); //获取对象个数 int n = 0; foreach (objectid id in btr_source) { n++; } //改变数组的长度 array.resize<entity>(ref ents, n); n = 0; //遍历对象,加入到动态数组中 foreach (objectid id in btr_source) { entity e = (entity)t_source.getobject(id, openmode.forread); ents[n++] = (entity)e.clone(); } bt_source.dispose(); btr_source.dispose(); t_source.commit(); } catch (exception ex) { throw new system.exception("t_source错误: " + ex.message); } } using (transaction trans = doc.transactionmanager.starttransaction()) { try { database db = hostapplicationservices.workingdatabase; //打开块表 blocktable bt = (blocktable)trans.getobject(db.blocktableid, openmode.forread); //打开当前文档的模型空间 blocktablerecord btr = (blocktablerecord)trans.getobject(db.currentspaceid, openmode.forwrite); //在当前模型空间加入实体 //line line = new line(new point3d(300, 400, 0), new point3d(80, 60, 0)); //btr.appendentity(line); //trans.addnewlycreateddbobject(line, true); //line line1 = new line(new point3d(600, 700, 0), new point3d(80, 60, 0)); //btr.appendentity(line1); //trans.addnewlycreateddbobject(line1, true); for (int i = 0; i < ents.length; i++) { objectid id = btr.appendentity(ents); trans.addnewlycreateddbobject(ents, true); } trans.commit(); bt.dispose(); btr.dispose(); } catch (exception ex) { throw new system.exception("trans错误: " + ex.message); } } } } } catch (exception ex) { throw new system.exception("copyfromotherdwg错误: " + ex.message); } } |
![]() |
![]() |
GDT自动化论坛(仅游客可见) |