.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to modify raster reference path

3 REPLIES 3
Reply
Message 1 of 4
rtejada
1027 Views, 3 Replies

How to modify raster reference path

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
3 REPLIES 3
Message 2 of 4
kean.walmsley
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


Kean Walmsley

Platform Architect & Evangelist, Autodesk Research

Blog | Twitter
Message 3 of 4
rtejada
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
Message 4 of 4
kean.walmsley
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




Kean Walmsley

Platform Architect & Evangelist, Autodesk Research

Blog | Twitter

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost