TheSwamp
Code Red => .NET => Topic started by: David Hall on July 27, 2010, 09:59:25 AM
-
I am trying to fix some pathing problems, and as a test, I hard coded the path and name of the image. It worked, so I thought I would improve and pull the name from the entity....it broke. i can not find where the name is stored.
using (Transaction tr = acDB.TransactionManager.StartTransaction())
{
ObjectId idImageDict;
DBDictionary imageDict;
idImageDict = RasterImageDef.GetImageDictionary(acDB);
imageDict = (DBDictionary) tr.GetObject(idImageDict, OpenMode.ForRead);
foreach (DictionaryEntry de in imageDict)
{
ObjectId imageId = (ObjectId)de.Value;
acImageDef = tr.GetObject(imageId, OpenMode.ForWrite, false) as RasterImageDef;
if (acImageDef.IsLoaded == false)
{
acImageDef.SourceFileName = @"C:\Documents and Settings\ua02038\Desktop\StandardsFix\" + "8-65-4065.JPG";
acImageDef.Load();
}
}
tr.Commit();
}
-
Also, I am having trouble with ImageDef vs Image. It seems to me that they are backwards, where the ImageDef is the instance of the image, and the Image is the "definition" of the image. I guess I'm thinking of Block vs instance of Block.
-
I think I found it in Keans post.
-
Are you talking to yourself (again) CMDR?
-
Here is what I ended up with
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
namespace StandardsImageFix
{
public class ImageFix
{
[CommandMethod("FixPath")]
public void FixPath()
{
RasterImageDef acImageDef;
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDB = acDoc.Database;
using (Transaction tr = acDB.TransactionManager.StartTransaction())
{
ObjectId idImageDict;
DBDictionary imageDict;
idImageDict = RasterImageDef.GetImageDictionary(acDB);
imageDict = (DBDictionary)tr.GetObject(idImageDict, OpenMode.ForRead);
foreach (DictionaryEntry de in imageDict)
{
ObjectId imageId = (ObjectId)de.Value;
acImageDef = tr.GetObject(imageId, OpenMode.ForRead, false) as RasterImageDef;
if (acImageDef.IsLoaded == false)
{
acImageDef.UpgradeOpen();
string strImageName = acImageDef.SourceFileName;
strImageName = strImageName.Substring(acImageDef.SourceFileName.LastIndexOf("\\") + 1);
acImageDef.SourceFileName = Path.Combine(@"C:\Documents and Settings\ua02038\Desktop\StandardsFix\" , strImageName);
acImageDef.Load();
}
}
tr.Commit();
}
}
}
}
-
What is baffleing me is in Kean's post (below) he added some "wrapper" code, which I didn't, yet mine worked. Anyone able to shed some light on this?
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
[color=red] If obj.GetRXClass.IsDerivedFrom(Autodesk.AutoCAD.Runtime.RXClass.GetClass(GetType(RasterImageDef))) Then
'create a RasterImageDef wrapper[/color]
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