Changing raster image paths in drawing to relative paths

<?xml encoding=”UTF-8″>By Balaji Ramamoorthy

Here is a code snippet to change the raster image paths in a drawing from absolute path to one that is relative to the host drawing path. Raster images that already have a relative path set remain unchanged.

 <span>using</span><span>  System.IO;</span>
 
 Document doc 
 = Application.DocumentManager.MdiActiveDocument;
 Editor ed = doc.Editor;
 
 <span>string</span><span>  dwgFilePath = <span>null</span><span> ;</span></span>
 dwgFilePath = <span>@"D:TempTest.dwg"</span><span> ;</span>
 
 <span>using</span><span>  (Database db = <span>new</span><span>  Database(<span>false</span><span> , <span>true</span><span> ))</span></span></span></span>
 <span>{</span>
     db.ReadDwgFile(dwgFilePath, 
         FileOpenMode.OpenForReadAndWriteNoShare, 
         <span>false</span><span> , <span>""</span><span> );</span></span>
 
     <span>using</span><span>  (Transaction tr </span>
         = db.TransactionManager.StartTransaction())
     <span>{</span>
         DBDictionary nod = tr.GetObject(
             db.NamedObjectsDictionaryId, 
             OpenMode.ForRead) <span>as</span><span>  DBDictionary;</span>
         ObjectId imageDictId = nod.GetAt(<span>"ACAD_IMAGE_DICT"</span><span> );</span>
         DBDictionary imageDict 
             = tr.GetObject(imageDictId, OpenMode.ForRead) 
                 <span>as</span><span>  DBDictionary;</span>
 
         <span>foreach</span><span>  (DBDictionaryEntry dbDictEntry <span>in</span><span>  imageDict)</span></span>
         <span>{</span>
             RasterImageDef rasterImageDef = tr.GetObject(
                 dbDictEntry.Value, 
                 OpenMode.ForWrite) <span>as</span><span>  RasterImageDef;</span>
 
             ed.WriteMessage(<span>"<span>{</span>0<span>}</span> Old SourcefileName : <span>{</span>1<span>}</span>"</span><span> , </span>
             Environment.NewLine, rasterImageDef.SourceFileName);
 
             <span>try</span><span> </span>
             <span>{</span>
                 <span>if</span><span>  (File.Exists(rasterImageDef.SourceFileName))</span>
                 <span>{</span>
                     dynamic dwgPathUri 
                             = <span>new</span><span>  Uri(dwgFilePath);</span>
                     dynamic rasterImagePathUri 
                         = <span>new</span><span>  Uri(rasterImageDef.SourceFileName);</span>
 
                     <span>// Make the raster image path </span><span> </span>
                     <span>// relative to the drawing path</span><span> </span>
                     dynamic relativeRasterPathUri 
                         = dwgPathUri.MakeRelativeUri
                                 (rasterImagePathUri);
 
                     <span>// Set the source path as relative</span><span> </span>
                     rasterImageDef.SourceFileName 
                     = Uri.UnescapeDataString(
                         relativeRasterPathUri.ToString());
 
                     ed.WriteMessage(
                         <span>"<span>{</span>0<span>}</span> Image path changed to : <span>{</span>1<span>}</span>"</span><span> , </span>
                         Environment.NewLine, 
                         rasterImageDef.SourceFileName);
 
                     <span>// Reload for AutoCAD to </span><span> </span>
                     <span>// resolve active path</span><span> </span>
                     rasterImageDef.Load();
 
                     <span>// Check if we found it</span><span> </span>
                     ed.WriteMessage(<span>"<span>{</span>0<span>}</span> Image found at : <span>{</span>1<span>}</span>"</span><span> ,</span>
                     Environment.NewLine, 
                     rasterImageDef.ActiveFileName);
                 <span>}</span>
             <span>}</span>
             <span>catch</span><span>  (UriFormatException ex)</span>
             <span>{</span>
                 <span>// Will ignore this.</span><span> </span>
                 <span>// If the raster image path is already relative</span><span> </span>
                 <span>// we might catch this exception</span><span> </span>
             <span>}</span>
         <span>}</span>
 
         tr.Commit();
     <span>}</span>
 
     db.SaveAs(db.OriginalFileName, 
             <span>true</span><span> , </span>
             db.OriginalFileVersion, 
             db.SecurityParameters);
 <span>}</span>
 


Comments

One response to “Changing raster image paths in drawing to relative paths”

  1. In the .NET environment, if you try this where the total path length of the drawing (including the drawing name) plus the length of the relative path to the image, including the image filename, exceeds the 256 limit, then this will give an eFileAccess error.
    If you do the same test using the XREF palette and the right-click Path > Make Relative command, then it will work. So there seems to be a bug in the .NET API.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading