Inserting a Raster Image
Hi,
I am trying to insert a raster image using c# but my attempts keep failing.
In my class if fails at this line: rid.Load();//<<=======Fails here===========>>""
Here is a snippet:
RasterImageDef rid = new RasterImageDef(); rid.SourceFileName = path; rid.Load();//<<=======Fails here===========>>
When I call the Load method it silently fails.
I have checked the path and it is correct.
I have used the InsertImage command at the command line and this works as expected.
I am unable to catch the error, I have put a try / catch around the Load method using System.Exception but it doesn't work.
I get a message in the command line that says "Error executing "RINS", RINS being the name of my command method.
Any ideas on what I can check?
Comments
-
clip from my code that works, some vars are declared earlier, its a clip:
ObjectId dictId = RasterImageDef.GetImageDictionary(db); //get image dict id
if (dictId.IsNull)
//if dict dont exists, creat it
dictId = RasterImageDef.CreateImageDictionary(db);
// open the dict
using (DBDictionary dict = (DBDictionary)tr.GetObject(dictId, OpenMode.ForWrite)) {
// make sure that the dict entry is uniq
int i = 1;
string RasterName = Path.GetFileNameWithoutExtension(RasterPath);
string aux = RasterName;
while (dict.Contains(aux)) {
aux = RasterName + i.ToString();
i++;
}
RasterName = aux;using (RasterImageDef rid = new RasterImageDef()) { //set the file path rid.SourceFileName = RasterPath; // load it rid.Load(); ObjectId defId = dict.SetAt(RasterName, rid); //add to transaction tr.AddNewlyCreatedDBObject(rid, true); // Creat the rastes image and add to model RasterImage ri = new RasterImage(); ri.ImageDefId = defId; ri.ShowImage = true; // place at the correct location ri.Orientation = new CoordinateSystem3d(Position, new Vector3d(width, 0.0, 0), new Vector3d(0.0, height, 0)); ri.Rotation = rot; //set layer ri.LayerId = layid; //set clip if (clippts != null && clippts.Count > 2) { //ri.IsClipped = true; not needed and bcad rejects ri.SetClipBoundary(ClipBoundaryType.Poly, clippts); } BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); using (BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)) { DrawOrderTable dot = (DrawOrderTable)tr.GetObject(btr.DrawOrderTableId, OpenMode.ForWrite); riId = btr.AppendEntity(ri); } tr.AddNewlyCreatedDBObject(ri, true); //move to display order bottom //ObjectIdCollection col = new ObjectIdCollection() { riId }; //dot.MoveToBottom(col); // Creat the reactor of RasterImage and // RasterImageDef that holds the "unreferenced" alert // on the XRef pallet RasterImage.EnableReactors(true); ri.AssociateRasterDef(rid);
You can get this from the autodesk .net customization group, and Kean Walmsley's blog Through the Interface.
0 -
Load() requires the image definition to be database resident (or it might be enough to just call SetDatabaseDefaults(), I'm not sure).
0 -
Thanks for the code. It is very similar to what I am using, I grabbed it from Kean. :-)
Owen, what does it mean for the "image definition to be database resident ". How would I do that?
0 -
To make it database resident, you have to add the image definition to an image dictionary (dict.SetAt() in the snippet that James posted). Also, SetDatabaseDefaults() only works with entities, so it can't help in this case.
0 -
Owen that fixed it!
I moved the dic.SetAt() method above rid.Load and it worked as expected.
Thank you so much!
0