イメージは図面ファイルにアタッチして配置することができますが、実際には図面ファイルの一部ではありません。イメージは、パス名またはデータ管理ドキュメント ID によって、図面ファイルにリンクされます。リンクされたイメージのパスは、いつでも変更したり削除することができます。イメージをアタッチするには、最初に参照されているディスクに保存されているイメージ ファイルに関する定義情報を含む RasterImageDef オブジェクトを作成し、次に RasterImage オブジェクトを作成します。
RasterImageDef オブジェクトを作成したら、RasterImage オブジェクトに割り当てます。RasterImage は、図面ウィンドウ内で操作するオブジェクトを表し、挿入位置、尺度、方向、およびその他のプロパティの中で特にイメージの外観をコントロールすることができます。1 つの RasterImageDef オブジェクトを使用して、イメージを複数回アタッチすることができます。アタッチごとに、専用のクリップ境界、明るさ、コントラスト、フェード、透過性が設定されます。
RasterImage オブジェクトを作成した後で、イメージのジオメトリの尺度が AudoCAD の図面で作成されたジオメトリの尺度と一致するように、Scale プロパティを使用してラスター イメージの尺度係数を設定できます。イメージをアタッチする場合、イメージの単位=AutoCAD の単位という尺度係数でイメージが挿入されます。イメージの尺度係数を設定するには、イメージのジオメトリの尺度と、AutoCAD の 1 単位を定義するために使用する尺度単位(インチ、フィートなど)を確認する必要があります。イメージ ファイルには、DPI を定義する解像度およびイメージ内のピクセル数の情報が含まれている必要があります。
イメージに解像度情報が含まれている場合は、AutoCAD はその情報と、尺度係数、および図面でのイメージの尺度を定義する AutoCAD 単位を組み合わせます。たとえば、ラスター イメージが青写真をスキャンしたもので、その尺度が 1 インチ=50 フィート(1:600)であり、AutoCAD の図面では 1 単位が 1 インチである場合は、イメージの尺度係数を 600 に設定します。AutoCAD は、イメージ内のジオメトリが図面のベクトル ジオメトリと同じサイズになるようにイメージの尺度を変更します。
次の例では、ラスター イメージをモデル空間に追加します。この例では、VBA Sample フォルダにある WorldMap.tif ファイルを使用します。このイメージが存在しない場合や、別のフォルダにある場合は、有効なパスおよびファイル名を使用するようにコードを変更してください。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("AttachRasterImage")]
public void AttachRasterImage()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Define the name and image to use
string strImgName = "WorldMap";
string strFileName = "C:\\AutoCAD\\Sample\\VBA\\WorldMap.TIF";
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();
// Set the source for the image file
acRasterDefNew.SourceFileName = strFileName;
// Load the image into memory
acRasterDefNew.Load();
// Add the image definition to the dictionary
acTrans.GetObject(acImgDctID, OpenMode.ForWrite);
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;
// 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 width and height of the image
Vector3d width;
Vector3d height;
// Check to see if the measurement is set to English (Imperial) or Metric units
if (acCurDb.Measurement == MeasurementValue.English)
{
width = new Vector3d((acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth) / 25.4, 0, 0);
height = new Vector3d(0, (acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight) / 25.4, 0);
}
else
{
width = new Vector3d(acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth, 0, 0);
height = new Vector3d(0, acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight, 0);
}
// Define the position for the image
Point3d insPt = new Point3d(5.0, 5.0, 0.0);
// Define and assign a coordinate system for the image's orientation
CoordinateSystem3d coordinateSystem = new CoordinateSystem3d(insPt, width * 2, height * 2);
acRaster.Orientation = coordinateSystem;
// Set the rotation angle for the image
acRaster.Rotation = 0;
// 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);
if (bRasterDefCreated)
{
acRasterDef.Dispose();
}
}
// Save the new object to the database
acTrans.Commit();
// Dispose of the transaction
}
}