Distinguished Contributor
Posts: 136
Registered: ‎07-24-2009
Message 1 of 7 (251 Views)


251 Views, 6 Replies
03-01-2012 02:41 PM

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

        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),  _

            imageDict = CType(myTrans.GetObject(nod.GetAt("ACAD_IMAGE_DICT"), _
                            Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite, False),  _

            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),  _

                '<<<2. Save Information to Memory Object
                Dim TempString As String = myRasterImgDef.Handle.ToString

                '<<<3. Rename Files (Enforce Naming Convention)
                    Dim sFileFullName As String = myRasterImgDef.SourceFileName

                    'Get File Directory
                    Dim myFileIO As System.IO.FileInfo
                    myFileIO = My.Computer.FileSystem.GetFileInfo(sFileFullName)

                    Dim sFileName As String = myFileIO.Name.Substring(0, (myFileIO.Name.Length - 4))

                    'Store NewName in Memory Object
                    sNewName = myFileIO.Directory.ToString & "\" & sFileName & "_" & Suffix & ".dwg"

                    '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
                End Try

                myRasterImgDef.SourceFileName = sNewName
                myRasterImgDef.ActiveFileName = sNewName

                MsgBox(myRasterImgDef.ActiveFileName & vbCrLf & myRasterImgDef.SourceFileName)


            'Commit Transaction

        End Using

    End Sub


Valued Mentor
Posts: 358
Registered: ‎03-31-2005
Message 2 of 7 (245 Views)

Re: RasterImages

03-01-2012 03:32 PM in reply to: VB_Autocad_guy

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.

Distinguished Contributor
Posts: 136
Registered: ‎07-24-2009
Message 3 of 7 (220 Views)

Re: RasterImages

03-05-2012 08:08 AM in reply to: fieldguy

Thanks, I'll give it a shot. 

Distinguished Contributor
Posts: 132
Registered: ‎01-06-2003
Message 4 of 7 (187 Views)

Re: RasterImages

04-03-2012 03:12 AM in reply to: VB_Autocad_guy

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

Distinguished Contributor
Posts: 136
Registered: ‎07-24-2009
Message 5 of 7 (170 Views)

Re: RasterImages

04-03-2012 10:14 AM in reply to: VB_Autocad_guy
Yah it is tricky,
Haven't figured it out yet...
But let me know if you have any luck... Or if I get around to it again I'll post my code.
Distinguished Contributor
Posts: 132
Registered: ‎01-06-2003
Message 6 of 7 (159 Views)

Re: RasterImages

04-03-2012 09:18 PM in reply to: VB_Autocad_guy

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

Distinguished Contributor
Posts: 132
Registered: ‎01-06-2003
Message 7 of 7 (149 Views)

Re: RasterImages

04-04-2012 02:36 AM in reply to: Dale.Bartlett

Following code deletes all raster images in a drawing. It seems to work well, but I'm open to improvements or comment. Regards, Dale


        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())
                    // Tanzillo:
                    // To get your code to work in 2009, do this before you create any RasterImageDef objects:
                    RasterImage image = new RasterImage();
                    // 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)

                    // get the image definition dictionary id
                    ObjectId imageDictId = RasterImageDef.GetImageDictionary(db);
                    if(imageDictId == null)
                    // get the image definition dictionary
                    DBDictionary imageDict = tr.GetObject(imageDictId, OpenMode.ForRead) as DBDictionary;
                    if(imageDict.Count == 0)

                    // loop through all images
                    foreach(DBDictionaryEntry entry in imageDict)
                        // get the object ID of the image definition
                        imageDictId = entry.Value;
                        if(imageDictId == null)
                        // remove image definition from the dictionary
                        // delete image definition object
                        RasterImageDef imageDef = tr.GetObject(imageDictId, OpenMode.ForWrite) as RasterImageDef;

                catch(Autodesk.AutoCAD.Runtime.Exception ex)
                    MessageBox.Show(ATK_CommonLibrary.UtilityLibrary.ErrorMessage(ex, MethodBase.GetCurrentMethod().Name.ToString(), lstrDWGFileName), "Error Log");


You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you interested in helping shape the Autodesk Community?
We’re looking at a few different ways to improve the “All Forums” landing page and need your feedback! If interested, please take a few minutes to fill out the following Usability Study. Thank you for your time!

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community