明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 5682|回复: 10

[图元] [求助]如何用C#实现插入光栅图像?

[复制链接]
发表于 2010-7-16 10:12 | 显示全部楼层 |阅读模式

本人用的是VS 2005 C#,ObjectArx 2007开发环境,ObjectArx 下有个一个sample,ism文件夹下的,是VC++的,可以实现插入光栅图像,

现在需要用C#实现,但是C++和C#使用的函数并不是一一对应的。

本人刚接触C#语言和CAD二次开发,一些语法还不熟悉,CAD中的DataBase、BlockTable等概念还不清楚。。。

 

程序运行的效果应该与cad菜单中的 插入->光栅图像参照 差不多,只不过程序插入图像的时候已经在代码中限定了插入位置和缩放比例,所以

不需要再指定插入点和缩放比例。

 

需要用到的类应该有RasterImage,RasterImageDef,其他的还不知道有没有。

 

希望各位不吝赐教

发表于 2010-7-16 17:30 | 显示全部楼层
 楼主| 发表于 2010-7-16 17:36 | 显示全部楼层

[求助]如何用C#实现插入光栅图像?

这个贴子的代码可以运行吗?不是说Crash了吗?

我先看看吧!谢谢!

 楼主| 发表于 2010-7-16 20:04 | 显示全部楼层

回复:(lzh741206)http://forums.autodesk.com/t5/N...

多谢了!
这个代码可以!

如下:
  1.    
  2.         [CommandMethod("insertimage")]
  3.         public void insertimage_new()
  4.         {
  5.             Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
  6.             Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
  7.             using (Transaction t = tm.StartTransaction())
  8.             {
  9.                 // open the dictionary
  10.                 Autodesk.AutoCAD.DatabaseServices.ObjectId imgDictID = RasterImageDef.GetImageDictionary(db);
  11.                 DBDictionary imgDict;
  12.                 if (imgDictID.OldId == 0)
  13.                 {
  14.                     imgDictID = RasterImageDef.CreateImageDictionary(db);
  15.                 }
  16.                 // create the raster image definition
  17.                 RasterImageDef rasterDef = new RasterImageDef();
  18.                 // rasterDef.SourceFileName = mapURL;
  19.                 rasterDef.SourceFileName = "D:\\atoll.jpg";
  20.                 rasterDef.Load();
  21.                 // test the image dictionary and the raster before going further
  22.                 bool bTestLoad = rasterDef.IsLoaded;
  23.                 Autodesk.AutoCAD.DatabaseServices.ObjectId TestImgDictID = RasterImageDef.GetImageDictionary(db);
  24.                 imgDict = (DBDictionary)t.GetObject(imgDictID, OpenMode.ForWrite);
  25.                 // add the raster definition to the dictionary iff it doesn't already exist
  26.                 Autodesk.AutoCAD.DatabaseServices.ObjectId rasterDefID;
  27.                 if (!imgDict.Contains("NewMap"))
  28.                 {
  29.                     rasterDefID = imgDict.SetAt("NewMap", rasterDef);
  30.                 }
  31.                 t.AddNewlyCreatedDBObject(rasterDef, true);
  32.                 /* test */
  33.                 bool bTestImage = imgDict.Contains("NewMap");
  34.                 // everything is good up to this point...
  35.                 // now add the REAL raster image reference
  36.                 RasterImage rasterRef = new RasterImage();
  37.                 rasterRef.ImageDefId = rasterDef.ObjectId;
  38.                 Autodesk.AutoCAD.DatabaseServices.ObjectId testRefID = rasterRef.ObjectId;
  39.                 BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
  40.                 BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
  41.                 btr.AppendEntity(rasterRef);
  42.                 tm.AddNewlyCreatedDBObject(rasterRef, true);
  43.                 testRefID = rasterRef.ObjectId;
  44.                 t.Commit();
  45.             }
  46.             

评分

参与人数 1明经币 +1 金钱 +5 贡献 +5 激情 +5 收起 理由
雪山飞狐_lzh + 1 + 5 + 5 + 5 【好评】表扬一下

查看全部评分

发表于 2010-7-19 13:11 | 显示全部楼层

这段程序有问题,用程序中的“insertimage”命令插入图像之后,在参照管理器中该图像的状态显示为“未参照”,而且将图像拆离之后,屏幕上还留下一个图像的边界框;而用AutoCAD自身的功能插入图像,图像的状态显示为“已参照”,将图像拆离之后屏幕上的图像被彻底删除。这是怎么回事?

 楼主| 发表于 2010-7-20 09:20 | 显示全部楼层

回复:(gyl)这段程序有问题,用程序中的“insertima...

这个我刚看了,确实是这样的,但是不知道为什么,可能是某些参数没有设置,等待高手来解答
 楼主| 发表于 2010-7-20 09:36 | 显示全部楼层

回复:(gyl)这段程序有问题,用程序中的“insertima...

经过研究,在“ t.Commit();”前加入以下代码,就行了!
  1. RasterImage.EnableReactors(true);
  2. rasterImg.AssociateRasterDef(rasterDef);
复制代码
发表于 2010-7-20 22:11 | 显示全部楼层
malasky发表于2010-7-20 9:36:00经过研究,在“ t.Commit();”前加入以下代码,就行了!以下内容为程序代码:RasterImage.EnableReactors(true); rasterImg.AssociateRasterDef(rasterDef);
经过试验,确实如此。第一行代码没有也可以。
  1. 帮助里解释的太简单了,许多方法和属性都没有讲到实质,让人摸不着头脑。
复制代码
发表于 2010-11-3 12:34 | 显示全部楼层

我先看看吧!谢谢!

发表于 2011-12-2 14:04 | 显示全部楼层
请问例子的arx代码里如何控制图片的显示大小? 望指教 !
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-28 19:02 , Processed in 0.282082 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表