[讨论]如何插入光栅图像
我想把一幅光栅图像插入到DWG图形中,并设置插入点、缩放系数和旋转角,就象手工插入的效果,但不知程序怎么写,请大家提示一下主要步骤,什么地方可找到例程? autodesk讨论组找到的代码:),直接贴过来了using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Geometry;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcWin = Autodesk.AutoCAD.Windows;
namespace Rivilis
{
public class Img
{
// Define Command "RastIns"
static public void RastIns()
// This method can have any name
{
AcEd.Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
AcWin.OpenFileDialog dlg = new AcWin.OpenFileDialog("Select raster file", "", "bmp;jpg;tif;gif", "Raster Files", AcWin.OpenFileDialog.OpenFileDialogFlags.AllowAnyExtension);
DialogResult rsdlg = dlg.ShowDialog();
if (rsdlg == DialogResult.OK)
{
string filename = dlg.Filename;
PromptPointOptions prOpt = new PromptPointOptions("\nInsert center point: ");
PromptPointResult es = ed.GetPoint(prOpt);
if (es.Status != PromptStatus.OK)
{
ed.WriteMessage("\nError !");
return;
}
AcDb.Database db = AcDb.HostApplicationServices.WorkingDatabase;
using (AcDb.Transaction tr = db.TransactionManager.StartTransaction())
{
AcDb.RasterImageDef rDef = new AcDb.RasterImageDef();
rDef.ActiveFileName = rDef.SourceFileName = filename;
rDef.Load();
AcDb.ObjectId imgDictId = AcDb.RasterImageDef.GetImageDictionary(db);
if (imgDictId.IsNull)
{
AcDb.RasterImageDef.CreateImageDictionary(db);
imgDictId = AcDb.RasterImageDef.GetImageDictionary(db);
}
if (imgDictId.IsNull)
return;
AcDb.DBDictionary imgDict = tr.GetObject(imgDictId, AcDb.OpenMode.ForWrite) as DBDictionary;
if (imgDict != null)
{
string keyname = filename.Substring(filename.LastIndexOfAny("\\/:".ToCharArray()) + 1);
keyname = keyname.Substring(0, keyname.LastIndexOf("."));
if (!imgDict.Contains(keyname))
{
imgDict.UpgradeOpen();
AcDb.ObjectId imgDefId = imgDict.SetAt(keyname, rDef);
tr.AddNewlyCreatedDBObject(rDef, true);
AcDb.RasterImage raster = new AcDb.RasterImage();
raster.SetDatabaseDefaults(db);
AcGe.Matrix3d mat = new AcGe.Matrix3d();
//mat = AcGe.Matrix3d.Displacement(new AcGe.Vector3d(es.Value.X, es.Value.Y, 0));
double dblScale =1000;
//--> change scale factor to you suit
mat = AcGe.Matrix3d.Scaling(dblScale, new AcGe.Point3d(es.Value.X, es.Value.Y, 0.0));
raster.TransformBy(mat);
raster.ImageDefId = imgDefId;
AcDb.BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as AcDb.BlockTableRecord;
btr.AppendEntity(raster);
tr.AddNewlyCreatedDBObject(raster, true);
}
}
tr.Commit();
}
}
}
}
}
<p>感谢版主的热情帮助,马上试试。</p> 经试验,此代码确实可以插入光栅图像,但存在一定问题,插入的光栅图像在外部参照对话框里显示的状态是错误的,明明图像已经显示出来了,但状态值是“未参照”。下图第二行为用代码插入的图像,第三行为手工插图的图像。什么原因造成的?请大家指点。 顶起来,希望高手们关注一下。 <p> 要看的话应在图像管理器中看吧,不应在外部参照管理器中看吧!</p> fjfhgdwfn发表于2009-11-16 17:10:00static/image/common/back.gif 要看的话应在图像管理器中看吧,不应在外部参照管理器中看吧!
<p></p>AutoCAD还有图像管理器吗?我键入“image”命令后,直接弹出外部参照管理器对话框。
页:
[1]