.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Attach image using RealDWG .NET API

3 REPLIES 3
Reply
Message 1 of 4
pankaj.sachdeva
1486 Views, 3 Replies

Attach image using RealDWG .NET API

I am developing a program in which I need to write come points, lines, polygons, and attach an Image to DWG file. I am using the RealDWG .NET API and I able to write other entities except the Image.

 

I tried the code as mentioned in this article. So, when I ran the code and found that the code is crashing when setting the "SourceFileName" of "RasterImageDef" class and the exception says "eFileAccessErr". I checked and the access is fine with that image file.

 

So, I just wanted to know whether is it possible to attach the image using RealDWG .NET API or I am just wasting my time with this API and if so, then please let me know why I am getting the exception.

 

Thanks,

Pankaj Sachdeva

Tags (1)
3 REPLIES 3
Message 2 of 4

Hi,

Kean's code to which you refer is quite old (2010). I have done a lot with RealDWG, but not for some time. When I checked, raster imaging was not part of that project. As you are aware, RealDWG is only lacking display and print functionality (Document/Editor), so I would have thought that attaching a raster image to the database would be straightforward (famous last words).

Below is some code that is more up to date, but not converted to RealDWG (HostApplicationServices.WorkingDatabase). From this site:

RasterImagesNET2015 

Good luck. Please post some code when you get it runnng.

Dale

<code>

 

[CommandMethod("AttachRasterImage")] 

public void AttachRasterImage()

// Get the current database and start a transaction 

Database db = AcadApp.DocumentManager.MdiActiveDocument.Database; 

using (Transaction tr = db.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(db); 

// Check to see if the dictionary does not exist, it not then create it 

if (acImgDctID.IsNull)

{

acImgDctID = RasterImageDef.CreateImageDictionary(db);

// Open the image dictionary 

DBDictionary acImgDict = tr.GetObject(acImgDctID, OpenMode.ForRead) as DBDictionary; 

// Check to see if the image definition already exists 

if (acImgDict.Contains(strImgName))

{

acImgDefId = acImgDict.GetAt(strImgName);

acRasterDef = tr.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

acImgDict.UpgradeOpen();

acImgDefId = acImgDict.SetAt(strImgName, acRasterDefNew);

tr.AddNewlyCreatedDBObject(acRasterDefNew,true);

acRasterDef = acRasterDefNew;

bRasterDefCreated =true;

// Open the Block table for read 

BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; 

// Open the Block table record Model space for write 

BlockTableRecord btr = tr.GetObject(bt[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 (db.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

btr.AppendEntity(acRaster);

tr.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

tr.Commit(); 

// Dispose of the transaction

}

}

 

<code>




______________
Yes, I'm Satoshi.
Message 3 of 4
jainendra
in reply to: Dale.Bartlett

Hi,

 

The code below throws up an Autocad Runtime Exception eFileAccessErr at the highlighted line in the code below.

 

// 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(); 

 

I am using RealDwg and AutoCAD Civil 3D 2014.

 

Please help me to resolve this issue.

 

 

Thanks,

Jainendra

 

Message 4 of 4
matthias_beyer
in reply to: jainendra

I had the same issue. What you have to do is update the FindFile function in your RealDwgInitialize class.

 

public override string FindFile(string fileName, Database database, FindFileHint hint)
        {
            if (fileName.Contains(".") == false)
            {
                string extension = "";
                switch (hint)
                {
                    case FindFileHint.Default:
                        break;
                    case FindFileHint.FontFile:

                        break;
                    case FindFileHint.CompiledShapeFile:
                        extension = ".shx";
                        break;
                    case FindFileHint.TrueTypeFontFile:
                        extension = ".ttf";
                        break;
                    case FindFileHint.EmbeddedImageFile:
                        break;
                    case FindFileHint.XRefDrawing:
                        extension = ".dwg";
                        break;
                    case FindFileHint.PatternFile:
                        break;
                    case FindFileHint.ArxApplication:
                        extension = ".dbx";
                        break;
                    case FindFileHint.FontMapFile:
                        extension = ".fmp";
                        break;
                    case FindFileHint.UnderlayFile:
                        break;
                    default:

                        break;
                }
                if (extension.Length == 0)
                {
                    Console.WriteLine("RealDWG Init - FindFile - Need to handle this.");
                }

                fileName += extension;

            }
            return SearchPath(database, fileName);
        }

 

 

private string SearchPath(Database database, string specifiedFileName)
        {
            FileInfo fileName = new FileInfo(specifiedFileName);

            if (fileName.Exists == true)
                return fileName.FullName;

            // get the application folder to check there
            DirectoryInfo appFolder = new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
            fileName = new FileInfo(Path.Combine(appFolder.FullName, fileName.Name));
            if (fileName.Exists == true)
            {
                return fileName.FullName;
            }

            // check autodesk shared
            DirectoryInfo adeskSharedFolder = new DirectoryInfo(Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"), @"Common Files\Autodesk Shared"));
            fileName = new FileInfo(Path.Combine(adeskSharedFolder.FullName, fileName.Name));
            if (fileName.Exists == true)
            {
                return fileName.FullName;
            }

            // check our fonts folder
            DirectoryInfo fontsFolder = new DirectoryInfo(Path.Combine(appFolder.FullName, "Fonts"));
            fileName = new FileInfo(Path.Combine(fontsFolder.FullName, fileName.Name));
            if (fileName.Exists == true)
            {
                return fileName.FullName;
            }

            // check the system fonts folder
            DirectoryInfo systemFontsFolder = new DirectoryInfo(Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "Fonts"));
            fileName = new FileInfo(Path.Combine(systemFontsFolder.FullName, fileName.Name));
            if (fileName.Exists == true)
            {
                return fileName.FullName;
            }

            if (database != null)
            {
                DirectoryInfo currentDwgFolder = new DirectoryInfo(Path.GetDirectoryName(database.Filename));
                fileName = new FileInfo(Path.Combine(currentDwgFolder.FullName, specifiedFileName));
                if (fileName.Exists == true)
                {
                    return fileName.FullName;
                }

                DirectoryInfo currentOriginalDwgFolder = new DirectoryInfo(Path.GetDirectoryName(database.OriginalFileName));
                fileName = new FileInfo(Path.Combine(currentOriginalDwgFolder.FullName, specifiedFileName));
                if (fileName.Exists == true)
                {
                    return fileName.FullName;
                }
            }

            return "";
        }

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost