Attach image by c# function
in Other
Hi,
I tried to create function for attaching georefereced TIF files with TFW text file. I use part of code with some modifications from this web pages http://through-the-interface.typepad.com/through_the_interface/autocad_net/ Sources from that site are written for Acad, but it works also with BricsCAD in most of cases.
[code]
......
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr) {
ObjectId dictId = RasterImageDef.GetImageDictionary(db);
ObjectId defId;
RasterImageDef rid;
if (dictId.IsNull)
{
// Image dictionary doesn't exist, create new
dictId = RasterImageDef.CreateImageDictionary(db);
}
// Open the image dictionary
DBDictionary dict = (DBDictionary)tr.GetObject(dictId,OpenMode.ForRead);
if (dict.Contains(sImgName))
{
defId = dict.GetAt(sImgName);
rid = tr.GetObject(defId, OpenMode.ForWrite) as RasterImageDef;
}
else
{
rid = new RasterImageDef();
// Set its source image
rid.SourceFileName = sFileName;
rid.ActiveFileName = ".\\"+System.IO.Path.GetFileName(sFileName);
// Load it
//rid.Load(); ...must be as comment BricsCAD create exception Error
dict.UpgradeOpen();
defId = dict.SetAt(sImgName, rid);
// Let the transaction know
tr.AddNewlyCreatedDBObject(rid, true);
}
// Create the raster image that references the
// definition
RasterImage ri = new RasterImage();
ri.ImageDefId = defId;
ri.ShowImage = false;
ri.Rotation = 0;
// Just place the image at the origin in a unit square
Vector3d imgScaleX = new Vector3d(dSx * ri.ImageWidth, 0, 0);
Vector3d imgScaleY = new Vector3d(0, dSy * ri.ImageHeight, 0);
System.Drawing.Bitmap img = new System.Drawing.Bitmap(sFileName);
Point3d imgTranslation = new Point3d(dTx, dTy - dSy * img.Height, 0);
ri.Orientation = new CoordinateSystem3d(imgTranslation, imgScaleX, imgScaleY);
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId,OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
btr.AppendEntity(ri);
tr.AddNewlyCreatedDBObject(ri, true);
// Create a reactor between the RasterImage and the
// RasterImageDef to avoid the "unreferenced"
// warning in the XRef palette
//RasterImage.EnableReactors(true); must be as comment BricsCAD create exception Error
ri.AssociateRasterDef(rid);
tr.Commit();
}
[/code]
This function add images, but it seems that DWG file is not correct. Everything is looks good till the moment when path to images or dwg is changed. Images appear only as rectangles without content.
0
Comments
-
Do you resolve this?It is a little diferent attaching picture in Autocad and in Bricscad. I resolve this, if you need code I can post it later in the afternoon.0
-
TryDim PIC_NAME As String = original_picture_nameDim dictId As ObjectId = RasterImageDef.GetImageDictionary(Db)If dictId = Nothing ThendictId = RasterImageDef.CreateImageDictionary(Db)End IfDim dict As DBDictionary = CType(acTrans.GetObject(dictId, OpenMode.ForRead), DBDictionary)Dim recName As String = Picture_name_in_drawingDim rid As RasterImageDef = New RasterImageDefrid.SourceFileName = PIC_NAMEdict.UpgradeOpen()Dim defId As ObjectId = dict.SetAt(recName, rid)rid.Load()acTrans.AddNewlyCreatedDBObject(rid, True)Dim ri As RasterImage = New RasterImageri.ImageDefId = defIdri.ShowImage = Trueri.Orientation = New CoordinateSystem3d(New Point3d(x1, y1, 0), New Vector3d(1, 0, 0), New Vector3d(0, 1, 0))Dim bt As BlockTableDim btr As BlockTableRecordbt = acTrans.GetObject(Db.BlockTableId, OpenMode.ForRead, False)btr = acTrans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, False)btr.AppendEntity(ri)acTrans.AddNewlyCreatedDBObject(ri, True)ri.AssociateRasterDef(rid)ri.TransformBy(Matrix3d.Scaling(picture_scale, New Point3d(x1, y1, 0)))CatchExit SubEnd Try0
This discussion has been closed.