![]() |
【转帖】[讨论]这两种方法创建实体有什么不同?
[讨论]这两种方法创建实体有什么不同?
www.dimcax.com [讨论]这两种方法创建实体有什么不同? 在.net开发objectarx程序时,可以使用两种方法来创建一个实体。可以手工操作块表、块表记录,将实体添加到模型空间的块表记录中,完成实体的创建: [commandmethod("createent")] static public void zffcreateent() // this method can have any name { point3d ptstart = new point3d(0, 0, 0); point3d ptend = new point3d(100, 100, 0); line line = new line(ptstart, ptend); objectid btid = hostapplicationservices.workingdatabase.blocktableid; blocktable blktbl = (blocktable)btid.open(openmode.forread); objectid btrid = blktbl[blocktablerecord.modelspace]; blocktablerecord blktblrcd = (blocktablerecord)btrid.open(openmode.forwrite); blktbl.close(); objectid lineid = blktblrcd.appendentity(line); blktblrcd.close(); line.close(); } autodesk建议的方式是通过事物来辅助完成实体的创建: [commandmethod("createline")] static public void zffcreateline() { point3d startpoint = new point3d(4.0, 2.0, 0.0); point3d endpoint = new point3d(10.0, 7.0, 0.0); line line = new line(startpoint,endpoint); objectid lineid; // 获得当前活动图形文档的图形数据库 database db=application.documentmanager.mdiactivedocument.database; // 获得事务管理器 dbtransman tm=db.transactionmanager; // 开始记录事物 using(transaction trans=tm.starttransaction()) { blocktable bt=(blocktable)tm.getobject(db.blocktableid,openmode.forread); blocktablerecord btr=(blocktablerecord)tm.getobject( bt[blocktablerecord.modelspace],openmode.forwrite); // 将实体添加到图形数据库的块表记录中 lineid=btr.appendentity(line); tm.addnewlycreateddbobject(line,true); trans.commit(); trans.dispose(); } } 这两种方法有什么不同?欢迎大家发表自己的意见。 从代码上来看,似乎仅仅是事务辅助处理时不需要手工关闭打开或者新建的对象(这一操作在vc++的objectarx中非常普遍)。 大意了…编译的时候第一种方法通不过,开发教程中居然还有这样的代码,晕。发现autodesk对.net开发的帮助文件极不负责,已经发现了不少错误。 第一种方法是可以的,只不过编译的时候出现警告而已。 c#最适合开发autocad,因为它拥有vb容易的特点,却具有vc++的强大功能。 事务在对连续操作出错时可以回滚,所以对添加块表记录和添加实体对象操作时使用事务包起来 呵呵,个人理解 跟楼上想法一样 ***************我就是我***************** ***************我爱学习***************** |
| 所有的时间均为北京时间。 现在的时间是 02:47 PM. |