多谢了!
这个代码可以!
如下:
-
-
- [CommandMethod("insertimage")]
- public void insertimage_new()
- {
- Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
- Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
- using (Transaction t = tm.StartTransaction())
- { // open the dictionary
- Autodesk.AutoCAD.DatabaseServices.ObjectId imgDictID = RasterImageDef.GetImageDictionary(db);
- DBDictionary imgDict;
- if (imgDictID.OldId == 0)
- {
- imgDictID = RasterImageDef.CreateImageDictionary(db);
- }
- // create the raster image definition
- RasterImageDef rasterDef = new RasterImageDef();
- // rasterDef.SourceFileName = mapURL;
- rasterDef.SourceFileName = "D:\\atoll.jpg";
- rasterDef.Load(); // test the image dictionary and the raster before going further
- bool bTestLoad = rasterDef.IsLoaded;
- Autodesk.AutoCAD.DatabaseServices.ObjectId TestImgDictID = RasterImageDef.GetImageDictionary(db);
- imgDict = (DBDictionary)t.GetObject(imgDictID, OpenMode.ForWrite);
- // add the raster definition to the dictionary iff it doesn't already exist
- Autodesk.AutoCAD.DatabaseServices.ObjectId rasterDefID;
- if (!imgDict.Contains("NewMap"))
- {
- rasterDefID = imgDict.SetAt("NewMap", rasterDef);
- }
- t.AddNewlyCreatedDBObject(rasterDef, true); /* test */
- bool bTestImage = imgDict.Contains("NewMap"); // everything is good up to this point...
- // now add the REAL raster image reference
- RasterImage rasterRef = new RasterImage();
- rasterRef.ImageDefId = rasterDef.ObjectId;
- Autodesk.AutoCAD.DatabaseServices.ObjectId testRefID = rasterRef.ObjectId;
- BlockTable bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
- BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
- btr.AppendEntity(rasterRef); tm.AddNewlyCreatedDBObject(rasterRef, true);
- testRefID = rasterRef.ObjectId; t.Commit(); }
-
|