在当前图中插入另外一个dwg文件的问题(请教admin)
www.dimcax.com
在当前图中插入另外一个dwg文件的问题(请教admin)
//在当前图形中插入另外一个文件 36 [commandmethod("insertblock")] 37 public void insertblock() 38 { 39 string sourcefilename = @"c:\oftenusedblock.dwg"; 40 string newblockname = "ofenusedblock"; 41 try 42 { 43 using (database sourcedatabase=getdatabasefromfile(sourcefilename)) 44 { 45 //把源数据库模型空间中的实体插入到当前数据库的一个新的块表记录中 46 //此处作为新块插入! 47 hostapplicationservices.workingdatabase.insert(newblockname, sourcedatabase, false); 48 } 49 } 50 catch (system.exception e) 51 { 52 application.showalertdialog(e.message); 53 } 54 } 以下为我目前写的代码,遇到的问题我会在代码中备注! public void insertdwg(string layername2) { string filename; filename="c:\\xlgiscadtemp\\"+layername2+".dwg"; try { using (database sourcedatabase=getdatabasefromfile(filename) ) { //把源数据库模型空间中的dwg插入到当前的一个dwg中 hostapplicationservices.workingdatabase.insert(layername2, sourcedatabase, false); //此处的layername2不是块,而是当前的打开的dwg,我不明白该怎样获取到当前的dwg这个对象?(注意我插入的不是一个块,而是整个目标dwg的图元实体被分散地插入) //vc++的写法为: /*把地形图插入cad中 acdbdatabase extdb(adesk::kfalse); extdb.readdwgfile(filename); acdbdatabase *ptempdb; extdb.wblock(ptempdb); acdbcurdwg()->insert(acgematrix3d::kidentity,ptempdb);//此处的acgematrix3d::kidentity在c#中该如何表达*/ ed.writemessage("\n成功插入dwg!\n"); } } catch (system.exception e) { application.showalertdialog(e.message); } } public static database getdatabasefromfile(string filename) { database databasefromfile = new database(false, true); databasefromfile.readdwgfile(filename, system.io.fileshare.none, false, null); //为了让插入块的函数在多个图形文件打开的情况下起作用,你必须使用下面的函数把源数据库对象关闭。 databasefromfile.closeinput(true); return databasefromfile; } [
我的代码: public function insertblock(byval sourcefilename as string, byval newblockname as string, byval po as point3d) as objectid try dim db as database = hostapplicationservices.workingdatabase() dim trans as transaction = db.transactionmanager.starttransaction() dim bt as blocktable = trans.getobject(db.blocktableid, openmode.forwrite) dim btr as blocktablerecord = trans.getobject(bt(btr.modelspace), openmode.forwrite) dim sourcedatabase as database = getdatabasefromfile(sourcefilename) '把源数据库模型空间中的实体插入到当前数据库的一个新的块表记录中 dim bobj as objectid = hostapplicationservices.workingdatabase.insert(newblockname, sourcedatabase, false) dim bref as blockreference = new blockreference(po, bobj) dim blockobj as objectid = btr.appendentity(bref) '''' dim empbtr as blocktablerecord = trans.getobject(bt(newblockname), openmode.forread) dim id as objectid for each id in empbtr dim ent as entity = trans.getobject(id, openmode.forread, false) if typeof ent is attributedefinition then dim attref as attributereference = new attributereference dim attdef as attributedefinition = ctype(ent, attributedefinition) attref.setpropertiesfrom(attdef) attref.position = new point3d(bref.position.x + attdef.position.x, bref.position.y + attdef.position.y, bref.position.z + attdef.position.z) attref.height = attdef.height attref.rotation = attdef.rotation attref.tag = attdef.tag attref.textstring = attdef.textstring bref.attributecollection.appendattribute(attref) trans.addnewlycreateddbobject(attref, true) end if next ''' trans.addnewlycreateddbobject(bref, true) trans.commit() trans.dispose() return blockobj catch e as system.exception application.showalertdialog(e.message) end try ''' end function private function getdatabasefromfile(byval filename as string) as database ''' dim databasefromfile as database = new database(false, true) databasefromfile.readdwgfile(filename, system.io.fileshare.read, false, dbnull.value.tostring) '为了让插入块的函数在多个图形文件打开的情况下起作用,你必须使用下面的函数把源数据库对象关闭。 databasefromfile.closeinput(true) return databasefromfile end function
cad开发爱好者
兄弟,您还是没解决我的问题!这和我的例子还是一样的!你也是通过newblockname来插入的;但是当前的newblockname如何获取啊? 怕问题说不清楚:补充下: 我现在打开的dwg中需要插入一个已知路径(filename)的dwg(加载它的所有图元,不是块); 但是c#的insert函数的格式为: public autodesk.autocad.databaseservices.objectid insert ( system.string sourceblockname , system.string destinationblockname , autodesk.autocad.databaseservices.database database , system.boolean preservesourcedatabase ) autodesk.autocad.databaseservices.database 的成员 //此处 sourceblockname 即为当前的dwg的块表名,但是我不明白怎么获取,请教! 摘要: creates a new block table record in the database. 参数: sourceblockname: name of the blocktablerecord. destinationblockname: name to be used by the new block table record created by this method. database: database from which to insert entities. preservesourcedatabase: bool to determine whether the source database database is left intact. 返回值: object id for the new block table record created by this method. [
我真是一笨蛋! acgematrix3d::kidentity直接用matrix3d.identity 就ok了! matrix3d.identity matrix3d.identity matrix3d.identity matrix3d.identity我八辈子也忘不了你了! [
能帮我解决一下吗
执行这一句:databasefromfile.readdwgfile(filename, system.io.fileshare.none, false, null);老出错,提示 [system.accessviolationexception] = {"尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"} 怎么办?
不错,也算是解决问题了。
c#最适合开发autocad,因为它拥有vb容易的特点,却具有vc++的强大功能。
1