- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2009-10-21 09:34:00
|
显示全部楼层
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;
- [assembly: CommandClass(typeof(Rivilis.Img))]
- namespace Rivilis
- {
- public class Img
- {
- // Define Command "RastIns"
- [CommandMethod("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();
- }
- }
- }
- }
- }
|
|