.NET

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

RasterImages

295 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


        '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

 

Valued Mentor
fieldguy
Posts: 379
Registered: ‎03-31-2005
Message 2 of 7 (289 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
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 3 of 7 (264 Views)

Re: RasterImages

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

Thanks, I'll give it a shot. 

Distinguished Contributor
Dale.Bartlett
Posts: 151
Registered: ‎01-06-2003
Message 4 of 7 (231 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
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 5 of 7 (214 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
Dale.Bartlett
Posts: 151
Registered: ‎01-06-2003
Message 6 of 7 (203 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
Dale.Bartlett
Posts: 151
Registered: ‎01-06-2003
Message 7 of 7 (193 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

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

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!