I think I'm going about this the wrong way.
I want to rename the RasterImage Files to Match the Drawing Naming Convention Plus a Suffix.
In this case _A, _B, _C etc.
And update that in the Drawing.
Public Shared Sub RenameRasterImagesAndReattachToDrawing(ByVal FilePath As String) Dim sNewName As String = "" Dim iImg As Integer = 0 Dim Suffix As String = "A" Dim myDwgIO As New IO.FileInfo(FilePath) 'Get FileName from Full FilePath '<<<1. Locate Raster Images 'Autocad Database Adapter 'Read Autocad File Silently without opening file on Screen Dim myDoc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument 'Start(Transaction) Using myTrans As Autodesk.AutoCAD.DatabaseServices.Transaction = myDoc.Database.TransactionManager.StartTransaction '~~~~~~~~~~~~~~~~~~~~~~~~TEMP CODE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'Take a Look at Attachments 'Open The Dictionary Dim nod As Autodesk.AutoCAD.DatabaseServices.DBDictionary Dim imageDict As Autodesk.AutoCAD.DatabaseServices.DBDictionary nod = CType(myTrans.GetObject(myDoc.Database.NamedObjectsDictionaryId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite, False), _ Autodesk.AutoCAD.DatabaseServices.DBDictionary) imageDict = CType(myTrans.GetObject(nod.GetAt("ACAD_IMAGE_DICT"), _ Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite, False), _ Autodesk.AutoCAD.DatabaseServices.DBDictionary) For Each dicentry As Autodesk.AutoCAD.DatabaseServices.DBDictionaryEntry In imageDict Dim myRasterImgDef As Autodesk.AutoCAD.DatabaseServices.RasterImageDef = _ CType(myTrans.GetObject(CType(dicentry.Value, Autodesk.AutoCAD.DatabaseServices.ObjectId), _ Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite), _ Autodesk.AutoCAD.DatabaseServices.RasterImageDef) '<<<2. Save Information to Memory Object Dim TempString As String = myRasterImgDef.Handle.ToString 'MsgBox(TempString) '<<<3. Rename Files (Enforce Naming Convention) Try Dim sFileFullName As String = myRasterImgDef.SourceFileName 'Get File Directory Dim myFileIO As System.IO.FileInfo myFileIO = My.Computer.FileSystem.GetFileInfo(sFileFullName) 'FileName Dim sFileName As String = myFileIO.Name.Substring(0, (myFileIO.Name.Length - 4)) 'Store NewName in Memory Object sNewName = myFileIO.Directory.ToString & "\" & sFileName & "_" & Suffix & ".dwg" MsgBox(sNewName) 'Rename File If Not My.Computer.FileSystem.FileExists(sNewName) Then My.Computer.FileSystem.CopyFile(sFileFullName, sNewName) End If 'Increment Letter Suffix = GetNextLetter(Suffix) '<<<4. Confirm Renames?? Catch ex As Exception MsgBox(ex.Message) End Try myRasterImgDef.SourceFileName = sNewName myRasterImgDef.ActiveFileName = sNewName MsgBox(myRasterImgDef.ActiveFileName & vbCrLf & myRasterImgDef.SourceFileName) Next 'Commit Transaction myTrans.Commit() End Using End Sub
You are further along with raster images than I have gotten. I would try to detach the old and attach the new if renaming does not work.
I'm trying to do that at the moment: find and detach all images in the drawing. Seems to be trickier than it sounds, or I am half asleep... Dale
Glad to know I'm not alone. I'd post some sample code but I've gone down so many rabbit holes it's shameful. I'll keep note of this page and post a solution when I get it resolved. Simply I want to detach all images in a drawing; trivial in Lisp. Regards, Dale
Following code deletes all raster images in a drawing. It seems to work well, but I'm open to improvements or comment. Regards, Dale
<code>
[CommandMethod("RXC")]
public static void RasterRemoveAll()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
// store drawing name for error report
string lstrDWGFileName = db.Filename.ToString();
using(Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
// Tanzillo:
// To get your code to work in 2009, do this before you create any RasterImageDef objects:
RasterImage image = new RasterImage();
image.Dispose();
// These two lines of code will force the managed runtime to load the RasterImage .DBX module.
// who knows if needed now?
// open The Named Object Dictionary
DBDictionary nod = tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary;
if(nod.Contains("ACAD_IMAGE_DICT") == false)
{
return;
}
// get the image definition dictionary id
ObjectId imageDictId = RasterImageDef.GetImageDictionary(db);
if(imageDictId == null)
{
return;
}
// get the image definition dictionary
DBDictionary imageDict = tr.GetObject(imageDictId, OpenMode.ForRead) as DBDictionary;
if(imageDict.Count == 0)
{
return;
}
imageDict.UpgradeOpen();
// loop through all images
foreach(DBDictionaryEntry entry in imageDict)
{
// get the object ID of the image definition
imageDictId = entry.Value;
if(imageDictId == null)
{
return;
}
// remove image definition from the dictionary
imageDict.Remove(imageDictId);
// delete image definition object
RasterImageDef imageDef = tr.GetObject(imageDictId, OpenMode.ForWrite) as RasterImageDef;
imageDef.Erase();
}
tr.Commit();
}
catch(Autodesk.AutoCAD.Runtime.Exception ex)
{
MessageBox.Show(ATK_CommonLibrary.UtilityLibrary.ErrorMessage(ex, MethodBase.GetCurrentMethod().Name.ToString(), lstrDWGFileName), "Error Log");
}
}
}
<code>
Can't find what you're looking for? Ask the community or share your knowledge.