Hi,
I had to replace an existing image with a new one in multiple drawings. I didn't find an "mapiinsert" function in Map API.
Here is what I used at the end:
public static void win_imageinsert(Database openedFileDB, bool backgroundTransparent)
{
/*
Database acCurDb = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Document curDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
*/
Database acCurDb = openedFileDB;
Editor ed = UtilCadActive.Editor;
Document curDoc = UtilCadActive.Document;
using (DocumentLock docLock = curDoc.LockDocument())
{
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Define the name and image to use
string strImgName = dicImageInsertDetails["imagename"];
string strFileName = dicImageInsertDetails["sourcename"];
RasterImageDef acRasterDef;
bool bRasterDefCreated = false;
ObjectId acImgDefId;
// Get the image dictionary
ObjectId acImgDctID = RasterImageDef.GetImageDictionary(acCurDb);
// Check to see if the dictionary does not exist, it not then create it
if (acImgDctID.IsNull)
{
acImgDctID = RasterImageDef.CreateImageDictionary(acCurDb);
}
// Open the image dictionary
DBDictionary acImgDict = acTrans.GetObject(acImgDctID, OpenMode.ForRead) as DBDictionary;
// Check to see if the image definition already exists
if (acImgDict.Contains(strImgName))
{
acImgDefId = acImgDict.GetAt(strImgName);
acRasterDef = acTrans.GetObject(acImgDefId, OpenMode.ForWrite) as RasterImageDef;
}
else
{
// Create a raster image definition
RasterImageDef acRasterDefNew = new RasterImageDef();
//--> robert
//acRasterDefNew.ResolutionMMPerPixel = new Vector2d(312.5, 312.5);
//acRasterDefNew.ResolutionUnits = Unit.Meter;
// Set the source for the image file
acRasterDefNew.SourceFileName = strFileName;
// Load the image into memory
acRasterDefNew.Load();
//ed.WriteMessage("\nResMMPerPixel = " + acRasterDefNew.ResolutionMMPerPixel);
string x_y = dicImageInsertDetails["resmmperpixel_x_y"];
string[] xy = x_y.Split(',');
double x, y;
bool b = double.TryParse(xy[0], out x);
b = double.TryParse(xy[1], out y);
acRasterDefNew.ResolutionMMPerPixel = new Vector2d(x, y);
//ed.WriteMessage("\nResMMPerPixel = " + acRasterDefNew.ResolutionMMPerPixel);
acRasterDefNew.ResolutionUnits = Unit.Meter;
// Add the image definition to the dictionary
acImgDict.UpgradeOpen();
acImgDefId = acImgDict.SetAt(strImgName, acRasterDefNew);
acTrans.AddNewlyCreatedDBObject(acRasterDefNew, true);
acRasterDef = acRasterDefNew;
bRasterDefCreated = true;
}
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
// Create the new image and assign it the image definition
using (RasterImage acRaster = new RasterImage())
{
acRaster.ImageDefId = acImgDefId;
string layername = dicImageInsertDetails["layer"];
LayerTable lt = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
if (lt.Has(layername))
{
acRaster.Layer = dicImageInsertDetails["layer"];
}
else
{
//WriteMessage("\nlayer not found: "+layername);
}
// Use ImageWidth and ImageHeight to get the size of the image in pixels (1024 x 768).
// Use ResolutionMMPerPixel to determine the number of millimeters in a pixel so you
// can convert the size of the drawing into other units or millimeters based on the
// drawing units used in the current drawing.
// Define the position for the image
string x_y = dicImageInsertDetails["insertionpoint"];
string[] xy = x_y.Split(',');
double x, y;
bool b = double.TryParse(xy[0], out x);
b = double.TryParse(xy[1], out y);
Point3d insPt = new Point3d(x, y, 0.0);
string res = dicImageInsertDetails["resmperpixel_x"];
double re;
b = double.TryParse(res, out re);
Vector3d u = new Vector3d(acRaster.ImageWidth * re, 0, 0);
Vector3d v = new Vector3d(0, acRaster.ImageHeight * re, 0);
acRasterDef.ResolutionUnits = Unit.Meter;
acRaster.Orientation = new CoordinateSystem3d(insPt, u, v);
/*
ed.WriteMessage("\nOrientation = " + acRaster.Orientation.ToString());
ed.WriteMessage("\nu /v vector = " + u.ToString() + " - " + v.ToString());
ed.WriteMessage("\nResMMPerPixel = " + acRasterDef.ResolutionMMPerPixel);
ed.WriteMessage("\nRasterDefSize = " + acRasterDef.Size);
ed.WriteMessage("\nResolutionUnits = " + acRasterDef.ResolutionUnits);
*/
acRasterDef.ResolutionUnits = Unit.Meter;
// Set the rotation angle for the image
acRaster.Rotation = 0;
if(backgroundTransparent)
acRaster.DisplayOptions = ImageDisplayOptions.Transparent;
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acRaster);
acTrans.AddNewlyCreatedDBObject(acRaster, true);
// Connect the raster definition and image together so the definition
// does not appear as "unreferenced" in the External References palette.
RasterImage.EnableReactors(true);
acRaster.AssociateRasterDef(acRasterDef);
//Draw order - to bottom
//Entity ent = acTrans.GetObject(acRaster.ObjectId, OpenMode.ForRead) as Entity;
//BlockTableRecord block = acTrans.GetObject(acRaster.ObjectId, OpenMode.ForRead) as BlockTableRecord;
//dRAWoRDER
DrawOrderTable dot = acTrans.GetObject(acBlkTblRec.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;
ObjectIdCollection objToMove = new ObjectIdCollection();
objToMove.Add(acRaster.ObjectId);
//objToMove.Add(acRasterDef.ObjectId);
dot.MoveToBottom(objToMove);
acRaster.ColorIndex = 256;
if (bRasterDefCreated)
{
acRasterDef.Dispose();
}
}
// Save the new object to the database
acTrans.Commit();
// Dispose of the transaction
}
}
}