几何尺寸与公差论坛------致力于产品几何量公差标准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-20, 09:32 PM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】在当前图中插入另外一个dwg文件的问题(请教admin)

在当前图中插入另外一个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
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
 


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

高级搜索
显示模式

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

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



所有的时间均为北京时间。 现在的时间是 01:46 PM.


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