几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量

几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 (http://www.dimcax.com/hust/index.php)
-   ObjectARX(VB.NET/C#) (http://www.dimcax.com/hust/forumdisplay.php?f=176)
-   -   【转帖】使用.net从外部dwg文件中导入块 (http://www.dimcax.com/hust/showthread.php?t=9291)

yang686526 2009-04-20 06:18 PM

【转帖】使用.net从外部dwg文件中导入块
 
使用.net从外部dwg文件中导入块
www.dimcax.com
使用.net从外部dwg文件中导入块
我们使用一个"内存数据库"(一个已经载入内存但没有被载入到autocad环境的数据库)把另外一个dwg文件中的块导入到当前的数据库中。
下面是有关的c#代码。
using system;
using autodesk.autocad.runtime;
using autodesk.autocad.applicationservices;
using autodesk.autocad.editorinput;
using autodesk.autocad.databaseservices;
namespace blockimport
{
public class blockimportclass
{
[commandmethod("ib")]
public void importblocks()
{
documentcollection dm = application.documentmanager;
editor ed = dm.mdiactivedocument.editor;
database destdb = dm.mdiactivedocument.database;
database sourcedb = new database(false, true);
promptresult sourcefilename;
try
{
//输入所要复制的块所在的dwg文件名
sourcefilename = ed.getstring("\nenter the name of the source drawing: ");
//把dwg文件读入到一个临时的数据库中
sourcedb.readdwgfile(sourcefilename.stringresult, system.io.fileshare.read, true, null);
//创建一个变量用来存储块的objectid列表
objectidcollection blockids = new objectidcollection();
autodesk.autocad.databaseservices.transactionmanager tm = sourcedb.transactionmanager;
using (transaction myt = tm.starttransaction())
{
//打开块表
blocktable bt = (blocktable)tm.getobject(sourcedb.blocktableid, openmode.forread, false);
//循环遍历块表中的块
foreach (objectid btrid in bt)
{
blocktablerecord btr = (blocktablerecord)tm.getobject(btrid, openmode.forread, false);
//只加入有名块和非布局块
if (!btr.isanonymous && !btr.islayout)
{
blockids.add(btrid);
}
btr.dispose();
}
bt.dispose();
//不需要提交事务处理
             //myt.commit();
}
//从源数据库向目的数据库复制块
idmapping mapping = new idmapping();
sourcedb.wblockcloneobjects(blockids, destdb.blocktableid, mapping, duplicaterecordcloning.replace, false);
ed.writemessage("\ncopied "
+ blockids.count.tostring()
+ " block definitions from "
+ sourcefilename.stringresult
+ " to the current drawing.");
}
catch (autodesk.autocad.runtime.exception ex)
{
ed.writemessage("\nerror during copy: " + ex.message);
}
}
}
}
好东东,多谢老大
向老大学习
顺便贴出vb.net版本。
'================
imports system
imports autodesk.autocad
imports autodesk.autocad.runtime
imports autodesk.autocad.geometry
imports autodesk.autocad.applicationservices
imports autodesk.autocad.databaseservices
imports autodesk.autocad.editorinput
imports system.collections.generic
public class gsterclass
' define command 'asdkcmd1'
<commandmethod("ib")> _
public sub asdkcmd1()
' type your code here
dim dm as documentcollection = application.documentmanager
dim ed as editor = dm.mdiactivedocument.editor
dim destdb as database = dm.mdiactivedocument.database
dim sourcedb as database = new database(false, true)
dim sourcefilename as promptresult
dim ex as autodesk.autocad.runtime.exception
try
' 选择要从中导入块的图形文件名
sourcefilename = ed.getstring("enter the name of the source drawing: ")
'将dwg文件读入到side database
sourcedb.readdwgfile(sourcefilename.stringresult, system.io.fileshare.read, true, "")
'创建一个变量存储块标示号列表
dim blockids as objectidcollection = new objectidcollection()
dim tm as autodesk.autocad.databaseservices.transactionmanager = sourcedb.transactionmanager
dim myt as transaction = tm.starttransaction()
using myt
'打开块表
dim bt as blocktable = ctype(tm.getobject(sourcedb.blocktableid, openmode.forread, false), blocktable)
'遍历每个块
dim btrid as objectid
for each btrid in bt
dim btr as blocktablerecord = ctype(tm.getobject(btrid, openmode.forread, false), blocktablerecord)
'只加入命名块和非布局块到复制列表中
if not btr.isanonymous and not btr.islayout then
blockids.add(btrid)
btr.dispose()
end if
next
bt.dispose()
myt.dispose()
end using
sourcedb.wblockcloneobjects(blockids, destdb.blocktableid, duplicaterecordcloning.replace, false)
ed.writemessage("copied " + blockids.count.tostring() + " block definitions from " + sourcefilename.stringresult + " to the current drawing.")
catch ex
ed.writemessage("error during copy: " + ex.message)
end try
sourcedb.dispose()
end sub
end class
'=================================
把kean的另一篇代码解析也译出来贴在这吧,发现我英语真是糟糕呢。
原文:
庖丁解牛——近距离观察导入块的c#代码
在上一个主题中我没能花太多时间去讨论代码(周五我贴出它的时候夜已经很深了)。这里是一些重要函数的分析。
代码中我们进行的第一个重要任务是声明和初始化一个新数据库对象。这个对象就是只在内存中的图形(side database)。我们可以存取这个图中的信息,但是图形不会在acad的编辑器中出现。
database sourcedb = new database(false, true);
注意,第一个参数(builddefaultdrawing)值为false。只有在下面会提到的两种情况下这个值才会为true。如果你错误地在不需要的情况下传给它true值,函数将返回且不会报错,但是dwg几乎肯定不能正常加载。这种情况时常发生,所以很需要关注这个复杂而微妙的问题。
下面是需要给builddefaultdrawing参数赋true值的两种情况:
1、自己创建图形,而不是从任何地方读取。
2、读取r12及更早版本的acad图形。
虽然例子中并不涉及这项技术,但如果打算在程序的辅助数据库中可以读入r13之前版本的dwg文件,你需要在程序中检查dwg的版本并为数据库的构造函数传入相对应的builddefaultdrawing参数。你可能要问了,“那如何在读入dwg文件前检查它的版本?”很幸运,dwg文件的前六个字节标示了它的版本号(你可以将dwg用记事本打开查看它的原始字符):
ac1.50 = r2.05
ac1002 = r2.6
ac1004 = r9
ac1006 = r10
ac1009 = r11/r12
ac1012 = r13
ac1014 = r14
ac1015 =

精品文章 威望 + 1
好文章
我是这样解决的
public static void importblocks()
{
documentcollection dm = application.documentmanager;
editor ed = dm.mdiactivedocument.editor;
database destdb = dm.mdiactivedocument.database;
//database sourcedb = new database(false, true);
database sourcedb;
try
{
document doctemp = dm.open("gc014.dwg");
doctemp.window.visible=false;
sourcedb = doctemp.database;
destdb.insert("gc014.dwg", sourcedb, false);
}
catch(system.exception es)
{
helper.message(es);
}
}
但是
这样一来,屏幕会闪一下
..........
好文章
我为什么调试的时候老出现致命错误?
请说的具体一点?
c#最适合开发autocad,因为它拥有vb容易的特点,却具有vc++的强大功能。


1


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