明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3234|回复: 6

[讨论]如何插入光栅图像

[复制链接]
发表于 2009-10-20 23:27:00 | 显示全部楼层 |阅读模式
我想把一幅光栅图像插入到DWG图形中,并设置插入点、缩放系数和旋转角,就象手工插入的效果,但不知程序怎么写,请大家提示一下主要步骤,什么地方可找到例程?
发表于 2009-10-21 09:34:00 | 显示全部楼层
autodesk讨论组找到的代码:),直接贴过来了
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using Autodesk.AutoCAD.Runtime;
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.EditorInput;
  9. using Autodesk.AutoCAD.Windows;
  10. using Autodesk.AutoCAD.Geometry;
  11. using AcRx = Autodesk.AutoCAD.Runtime;
  12. using AcEd = Autodesk.AutoCAD.EditorInput;
  13. using AcDb = Autodesk.AutoCAD.DatabaseServices;
  14. using AcAp = Autodesk.AutoCAD.ApplicationServices;
  15. using AcGe = Autodesk.AutoCAD.Geometry;
  16. using AcWin = Autodesk.AutoCAD.Windows;
  17. [assembly: CommandClass(typeof(Rivilis.Img))]
  18. namespace Rivilis
  19. {
  20. public class Img
  21. {
  22. // Define Command "RastIns"
  23. [CommandMethod("RastIns")]
  24. static public void RastIns()
  25. // This method can have any name
  26. {
  27. AcEd.Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
  28. AcWin.OpenFileDialog dlg = new AcWin.OpenFileDialog("Select raster file", "", "bmp;jpg;tif;gif", "Raster Files", AcWin.OpenFileDialog.OpenFileDialogFlags.AllowAnyExtension);
  29. DialogResult rsdlg = dlg.ShowDialog();
  30. if (rsdlg == DialogResult.OK)
  31. {
  32. string filename = dlg.Filename;
  33. PromptPointOptions prOpt = new PromptPointOptions("\nInsert center point: ");
  34. PromptPointResult es = ed.GetPoint(prOpt);
  35. if (es.Status != PromptStatus.OK)
  36. {
  37. ed.WriteMessage("\nError !");
  38. return;
  39. }
  40. AcDb.Database db = AcDb.HostApplicationServices.WorkingDatabase;
  41. using (AcDb.Transaction tr = db.TransactionManager.StartTransaction())
  42. {
  43. AcDb.RasterImageDef rDef = new AcDb.RasterImageDef();
  44. rDef.ActiveFileName = rDef.SourceFileName = filename;
  45. rDef.Load();
  46. AcDb.ObjectId imgDictId = AcDb.RasterImageDef.GetImageDictionary(db);
  47. if (imgDictId.IsNull)
  48. {
  49. AcDb.RasterImageDef.CreateImageDictionary(db);
  50. imgDictId = AcDb.RasterImageDef.GetImageDictionary(db);
  51. }
  52. if (imgDictId.IsNull)
  53. return;
  54. AcDb.DBDictionary imgDict = tr.GetObject(imgDictId, AcDb.OpenMode.ForWrite) as DBDictionary;
  55. if (imgDict != null)
  56. {
  57. string keyname = filename.Substring(filename.LastIndexOfAny("\\/:".ToCharArray()) + 1);
  58. keyname = keyname.Substring(0, keyname.LastIndexOf("."));
  59. if (!imgDict.Contains(keyname))
  60. {
  61. imgDict.UpgradeOpen();
  62. AcDb.ObjectId imgDefId = imgDict.SetAt(keyname, rDef);
  63. tr.AddNewlyCreatedDBObject(rDef, true);
  64. AcDb.RasterImage raster = new AcDb.RasterImage();
  65. raster.SetDatabaseDefaults(db);
  66. AcGe.Matrix3d mat = new AcGe.Matrix3d();
  67. //mat = AcGe.Matrix3d.Displacement(new AcGe.Vector3d(es.Value.X, es.Value.Y, 0));
  68. double dblScale =1000;
  69. //--> change scale factor to you suit
  70. mat = AcGe.Matrix3d.Scaling(dblScale, new AcGe.Point3d(es.Value.X, es.Value.Y, 0.0));
  71. raster.TransformBy(mat);
  72. raster.ImageDefId = imgDefId;
  73. AcDb.BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as AcDb.BlockTableRecord;
  74. btr.AppendEntity(raster);
  75. tr.AddNewlyCreatedDBObject(raster, true);
  76. }
  77. }
  78. tr.Commit();
  79. }
  80. }
  81. }
  82. }
  83. }
 楼主| 发表于 2009-10-21 13:03:00 | 显示全部楼层

感谢版主的热情帮助,马上试试。

 楼主| 发表于 2009-11-9 13:23:00 | 显示全部楼层
经试验,此代码确实可以插入光栅图像,但存在一定问题,插入的光栅图像在外部参照对话框里显示的状态是错误的,明明图像已经显示出来了,但状态值是“未参照”。下图第二行为用代码插入的图像,第三行为手工插图的图像。什么原因造成的?请大家指点。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 2009-11-16 13:14:00 | 显示全部楼层
顶起来,希望高手们关注一下。
发表于 2009-11-16 17:10:00 | 显示全部楼层

    要看的话应在图像管理器中看吧,不应在外部参照管理器中看吧!

 楼主| 发表于 2009-11-17 22:12:00 | 显示全部楼层
fjfhgdwfn发表于2009-11-16 17:10:00    要看的话应在图像管理器中看吧,不应在外部参照管理器中看吧!

AutoCAD还有图像管理器吗?我键入“image”命令后,直接弹出外部参照管理器对话框。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-29 11:27 , Processed in 0.195128 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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