.NET

Reply
Valued Contributor
rtejada
Posts: 61
Registered: ‎06-19-2002
Message 1 of 4 (713 Views)

How to modify raster reference path

713 Views, 3 Replies
10-16-2006 09:29 AM
Hi everybody

I need to modify a bunch of AutoCAD raster image reference paths on diferent dwg files.
I am able to display image path property using the code below.
How can I modify the image reference path?
I will appreciate any help
Thanks




Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.DatabaseServices


Public Sub LoadImageProperties(ByVal localFileName As String)
Dim db As New Database
db.ReadDwgFile(localFileName, IO.FileShare.ReadWrite, False, Nothing)
Dim tm As TransactionManager = db.TransactionManager
Dim rasterImageDef As RasterImageDef
Dim dictEntry As System.Collections.DictionaryEntry
Dim nod As DBDictionary
Dim imageDict As DBDictionary
nod = CType(tm.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead, False), DBDictionary)
imageDict = CType(tm.GetObject(nod.GetAt("ACAD_IMAGE_DICT"), OpenMode.ForRead, False), DBDictionary)
Dim imagePath As String
For Each dictEntry In imageDict
Dim obj As DBObject
obj = tm.GetObject(CType(dictEntry.Value(), ObjectId), OpenMode.ForRead, False)
'the next is needed to get the raster image path
If obj.GetRXClass.IsDerivedFrom(Autodesk.AutoCAD.Runtime.RXClass.GetClass(GetType(RasterImageDef))) Then
'create a RasterImageDef wrapper
rasterImageDef = CType(Autodesk.AutoCAD.Runtime.DisposableWrapper.Create(GetType(RasterImageDef), obj.UnmanagedObject, False), RasterImageDef)
imagePath = rasterImageDef.SourceFileName
msgbox(imagePath)
End If
Next
ta.Commit()
ta.Dispose()
db.Dispose()
End Sub
Employee
kean.walmsley
Posts: 27
Registered: ‎01-17-2005
Message 2 of 4 (713 Views)

Re: How to modify raster reference path

10-20-2006 02:45 AM in reply to: rtejada
Hi Raul,

I had to do a few things to get your code to work...

You're missing a line (which I'm guessing is there in your original code, somewhere):

Dim ta As Transaction = tm.StartTransaction

You also need to open the RasterImageDef object for write, rather than read. You can do that in the GetObject() call or (and this following approach is better) call UpgradeOpen() once you've established it's a raster image definition.

I'd also recommend not using the default Database constructor, but passing (false,true) as arguments.

Hopefully that's enough to get this working...

Kean
Valued Contributor
rtejada
Posts: 61
Registered: ‎06-19-2002
Message 3 of 4 (713 Views)

Re: How to modify raster reference path

10-21-2006 04:27 PM in reply to: rtejada
Hi Kean
I fix the code per your suggestions thanks, but still getting the exception error when assigning the rasterimageDefinition.Sourcefilename or ActiveFileName.
I do not know what you mean by "I'd also recommend not using the default Database constructor, but passing (false,true) as arguments."
Private Sub Repath()
Dim db As New Database
db.ReadDwgFile(localFileName, IO.FileShare.ReadWrite, False, Nothing)
Dim tm As Transaction = db.TransactionManager.StartTransaction()
Dim rasterImageDef As RasterImageDef
Dim dictEntry As System.Collections.DictionaryEntry
Dim nod As DBDictionary
Dim imageDict As DBDictionary
nod = CType(tm.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite, False), DBDictionary)
imageDict = CType(tm.GetObject(nod.GetAt("ACAD_IMAGE_DICT"), OpenMode.ForWrite, False), DBDictionary)
Dim imagePath As String
For Each dictEntry In imageDict
Dim obj As DBObject
obj = tm.GetObject(CType(dictEntry.Value(), ObjectId), OpenMode.ForWrite, False)
'the next is needed to get the raster image path
If obj.GetRXClass.IsDerivedFrom(Autodesk.AutoCAD.Runtime.RXClass.GetClass(GetType(RasterImageDef))) Then
'create a RasterImageDef wrapper
rasterImageDef = CType(Autodesk.AutoCAD.Runtime.DisposableWrapper.Create(GetType(RasterImageDef), obj.UnmanagedObject, False), RasterImageDef)
imagePath = rasterImageDef.SourceFileName
obj.UpgradeOpen()
try
'this next does not work, I get a exception of the type "eFileAccessErr" in any of the 2 next lines
rasterImageDef.SourceFileName = "c:\test.jgp"
rasterImageDef.ActiveFileName = "c:\test.jpg"
Catch ex As Exception

End Try
End If
Next
tm.Commit()
db.Save()
tm.Dispose()
db.Dispose()
End Sub
Employee
kean.walmsley
Posts: 27
Registered: ‎01-17-2005
Message 4 of 4 (713 Views)

Re: How to modify raster reference path

10-27-2006 01:38 AM in reply to: rtejada
Hi Juan,

Here's your code with the modifications I made to test... it worked just fine on AutoCAD 2007.

Regards,

Kean



Imports Autodesk.AutoCAD.Runtime


Imports Autodesk.AutoCAD.ApplicationServices


Imports Autodesk.AutoCAD.DatabaseServices


Imports Autodesk.AutoCAD.EditorInput


 


Namespace ImageTest


  Public Class ImageCmds


 


    ' Must have UsePickSet specified


    "TEST")> _


    Public Sub LoadImageProperties()


      Dim db As New Database(False, True)


      'This DWG file contains images attached from C:\My Documents


      db.ReadDwgFile("c:\temp\testimage.dwg", IO.FileShare.ReadWrite, False, Nothing)


      Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager


      Dim ta As Transaction = tm.StartTransaction


      Dim rasterImageDef As RasterImageDef


      Dim dictEntry As System.Collections.DictionaryEntry


      Dim nod As DBDictionary


      Dim imageDict As DBDictionary


      nod = CType(tm.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead, False), DBDictionary)


      imageDict = CType(tm.GetObject(nod.GetAt("ACAD_IMAGE_DICT"), OpenMode.ForRead, False), DBDictionary)


      Dim imagePath As String


      For Each dictEntry In imageDict


        Dim obj As DBObject


        obj = tm.GetObject(CType(dictEntry.Value(), ObjectId), OpenMode.ForRead, False)


        'the next is needed to get the raster image path


        If obj.GetRXClass.IsDerivedFrom(Autodesk.AutoCAD.Runtime.RXClass.GetClass(GetType(RasterImageDef))) Then


          'create a RasterImageDef wrapper


          rasterImageDef = CType(Autodesk.AutoCAD.Runtime.DisposableWrapper.Create(GetType(RasterImageDef), obj.UnmanagedObject, False), RasterImageDef)


          imagePath = rasterImageDef.SourceFileName


          MsgBox(imagePath)


          'Strip off the filename, looking for the lask backslash, repath it to the temp folder


          imagePath = "C:\temp\" + imagePath.Substring(imagePath.LastIndexOf(Chr(92)) + 1)


          'Make sure the def object is open for write, then set the filename to the new location


          rasterImageDef.UpgradeOpen()


          rasterImageDef.SourceFileName = imagePath


          imagePath = rasterImageDef.SourceFileName


          'Just to double-check, let's get the filename and show it


          MsgBox(imagePath)


        End If


      Next


      ta.Commit()


      ta.Dispose()


      db.Dispose()


    End Sub


  End Class


End Namespace


Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.