This is a repeat of the code in the last post for people using the web interface:
{code}
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System.Windows.Forms;
using System.IO;
using Acad = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass( typeof( RasterImageSample.Commands ) )]
namespace RasterImageSample
{
public class Commands
{
[CommandMethod( "RASTER_SAMPLE" )]
public static void RasterCommand()
{
Document doc = Acad.DocumentManager.MdiActiveDocument;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "JPeg Files|*.jpg|Gif Files|*.gif|PNG Files|*.png";
ofd.FilterIndex = 0;
ofd.Title = "Select Image File";
if( ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK )
{
string filename = ofd.FileName;
using( Transaction trans = doc.TransactionManager.StartTransaction() )
{
ObjectId idModel = SymbolUtilityServices.GetBlockModelSpaceId(doc.Database);
BlockTableRecord ms = trans.GetObject( idModel, OpenMode.ForWrite) as BlockTableRecord;
RasterImageDef imageDef = AddImageDef( doc.Database, trans, filename );
if( imageDef != null )
{
RasterImage image = new RasterImage();
image.ImageDefId = imageDef.ObjectId;
ObjectId imageId = ms.AppendEntity( image );
if( imageId.IsNull )
throw new Autodesk.AutoCAD.Runtime.Exception( ErrorStatus.NullObjectId );
trans.AddNewlyCreatedDBObject( image, true );
RasterImage.EnableReactors( true );
image.AssociateRasterDef( imageDef );
Point3d origin = (Point3d) Acad.GetSystemVariable( "VIEWCTR" );
image.Orientation = new CoordinateSystem3d( origin, Vector3d.XAxis, Vector3d.YAxis);
}
trans.Commit();
}
}
}
public static RasterImageDef AddImageDef( Database db, Transaction trans, string imageFilespec )
{
if( !File.Exists( imageFilespec ) )
throw new FileNotFoundException( imageFilespec );
ObjectId idImageDict = RasterImageDef.GetImageDictionary( db );
if( idImageDict.IsNull )
idImageDict = RasterImageDef.CreateImageDictionary( db );
if( idImageDict.IsNull )
throw new InvalidOperationException( "failed to get or create image dictionary" );
DBDictionary imageDictionary = trans.GetObject( idImageDict, OpenMode.ForRead ) as DBDictionary;
if( imageDictionary == null )
throw new InvalidOperationException( "Failed to open image dictionary" );
RasterImageDef imageDef = new RasterImageDef();
imageDef.SourceFileName = imageFilespec;
imageDef.Load();
String name = RasterImageDef.SuggestName( imageDictionary, imageFilespec );
imageDictionary.UpgradeOpen();
ObjectId idEntry = imageDictionary.SetAt( name, imageDef );
imageDictionary.DowngradeOpen();
if( idEntry.IsNull )
{
imageDef.Dispose();
imageDef = null;
throw new InvalidOperationException( "Failed to add image definition to image dictionary" );
}
trans.AddNewlyCreatedDBObject( imageDef, true );
return imageDef;
}
}
}
{code}
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com
Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm
"CB"
wrote in message news:[email protected]...
I have been trying to pass the object rasterimagedef object with no results. It continues to throw the eNotinDatabase exception. There is another method used as a switch called enablereactors(). Should i refrain from telling the transaction manager to add the newly created rasterimagedef until the new raster image has called the associaterasterdef... or does it not really matter. I can't tell if this is causing the problem. I have tried it both ways and the associaterasterdef() method will not work with the rasterimagedef. Do I have to fully commit() the transaction and start a new transaction before I can attempt to feed it as a database object into the associate method?
cbertschy
"Tony Tanzillo" wrote in message news:[email protected]...
You don't pass any reactor id to AssociateRasterDef().
You pass what the name of that method implies (the
RasterImageDef object, which may need to be open
and write-enabled).
That's all there is to it. You don't need to deal with
any reactors.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com
Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm
"CB" wrote in message news:[email protected]...
That is correct. When I code something like:
ri.associaterasterdef(rid);
I get an error in autocad that tells me the entry does not exist in the database.
I am guessing that it is looking for a reactor id that does not exist or that the raster image def is not being handle properly. My question is really where in the code would I put the call to ri.associaterasterdef(rasterimagedef). I found a small tid bit that mentioned setting up a reactionid but nothing on how to handle this. Attached is my version. It is a work in progress. I am posting via OE so I have no idea how this will look.
Thanks, Tony.
public class ACADutility {
public static void addimage(double height, double width, Point3d p1, Point3d p2){
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
DocumentLock doclock = doc.LockDocument();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = new BlockTableRecord();
btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
RasterImageDef rid = new RasterImageDef();
ObjectId ImgDictID = RasterImageDef.GetImageDictionary(db);
if (ImgDictID.IsNull)
{
ImgDictID = RasterImageDef.CreateImageDictionary(db);
}
rid.SourceFileName = "c:\\temp\\gis-image-test.jpg";
rid.ResolutionUnits = Unit.Foot;
rid.Load();
rid.OpenImage();
rid.UpdateEntities();
DBDictionary imgdict =(DBDictionary)tr.GetObject(ImgDictID, OpenMode.ForWrite);
ObjectId rasterimdefid;
rasterimdefid = imgdict.SetAt("image-SERVICE", rid);
tr.AddNewlyCreatedDBObject(rid, true);
RasterImage ri = new RasterImage();
RasterImageDef newimagedef = (RasterImageDef)tr.GetObject(rid.ObjectId, OpenMode.ForWrite);// this was my last ditch effort to get a raster image def to work. stuffing
//the original rid object into the method this not work so I attempted to get update object after the image dictionary update. Not that this should mattre but I can't
//get it to work upgrade open was something ignorant that I also tried. I had the ri.imagedfid = rid.objectid commented out but still did not work. all the
//example on the web show only this method being used to link the raster image to the dictionary definition. this does not work correctly. It does work to make the added image
//use the imagedefinition but does not add the neccesary reactor info to recognize that the imagedef has references in the drawing
newimagedef.UpgradeOpen();
ri.ImageDefId = rid.ObjectId;
ri.AssociateRasterDef(newimagedef);
ri.Visible = true;
ri.Orientation = new CoordinateSystem3d( p1, new Vector3d(width,0,0), new Vector3d(0,height,0));
btr.AppendEntity(ri);
tr.AddNewlyCreatedDBObject(ri, true);
tr.Commit();
}
doclock.Dispose();
}
}
}
"Tony Tanzillo" wrote in message news:[email protected]...
Your code isn't calling RasterImage.AssociateImageDef(), which is
what creates the reactor and adds it to the RasterImageDef.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com
Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm
wrote in message news:[email protected]...
I currently have code working that will grab an image stored on a local drive, create the rasterimagedef dictionary entry, create the raster image object, and display the image in autocad. The program works but there is no reactor link to the image and this is apparent as the xref/image manager shows that the added image entry has no references in the drawing. The drawing does have the image working... but clearly something is not linked correctly. I would normally post my code but I amaffraid it will be a garbled mess like the garbage I have been wading through to find something helpful since the forum update.
My code is very similar in structure to the attached link I found which has the same issue as my code is having. In fact, everything I have found online appears to have this same problem.
http://www.objectarx.net/bbs/viewthread.php?tid=2500 &ex tra=page%3D1
I have been trying to fix this by implimenting the rasterimage.AssociateRasterDef and stuffing the parameter with the new rasterimagedef() entry (both before and after the dictionary entry and transaction) with no luck. The mgd help is very vague on this and mentions that a RasterImageDefReactor must be constructed. I am still learning the C# api and language, with no background in c++ arx. A small example of using the AssociateRasterDef correctly would be very helpful.
Thanks,
Chris Bertschy
Side note: I typically post with OE, but I used the RTF web format for this one. It appears that the only way to get a readable post is to use the web rtf box. Are the users of newsgroup readers being punished??