Author Topic: Get raster image paths with .net  (Read 4851 times)

0 Members and 1 Guest are viewing this topic.

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Get raster image paths with .net
« on: July 21, 2008, 06:52:58 PM »
I can't seem to find the code for looping through the images attached to a drawing.
pls direct me to such a post if there is one.

I'll try to write it in the mean time, and post when done...
James Maeding

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Get raster image paths with .net
« Reply #1 on: July 21, 2008, 07:55:45 PM »
Here is the thread where Tony T. helped me with finding where the image is inserted.
[ http://discussion.autodesk.com/thread.jspa?messageID=5649679 ]

Here is the code I came up with, with his help.

Attached is the file in whole.  It will list all xrefs/images per files selected.  Also attached is my utilities file.  Not sure if it's needed, but incase it is, it is attached.

You may also want to check this thread about finding the paths.
[ http://www.theswamp.org/index.php?topic=17165.0 ]

Code: [Select]
private MyImageInformation[] FindImages (Database db) {
using (Transaction Trans = db.TransactionManager.StartTransaction()) {
DBDictionary LoDict = (DBDictionary)Trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
ObjectId ImgDictId = RasterImageDef.GetImageDictionary(db);
if (ImgDictId == ObjectId.Null) return null;
DBDictionary ImgDict = (DBDictionary)Trans.GetObject(ImgDictId, OpenMode.ForRead);
if (ImgDict.Count.Equals(0)) return null;
MyImageInformation[] ImInfoArray = new MyImageInformation[ImgDict.Count];
int i = 0;
foreach (DictionaryEntry de in ImgDict) {
string ImName = de.Key as string;
ObjectId ObjId = (ObjectId)de.Value;
RasterImageDef rid = Trans.GetObject(ObjId, OpenMode.ForRead) as RasterImageDef;
if (rid != null) {
MyImageInformation ImInfo = new MyImageInformation();
ImInfo.ImageName = ImName;
ImInfo.IsEmbedded = rid.IsEmbedded;
ImInfo.ImagePath = rid.SourceFileName;
if (string.Compare(ImInfo.ImagePath, string.Empty).Equals(0))
ImInfo.ImagePath = rid.ActiveFileName;
ImInfo.IsLoaded = rid.IsLoaded;
bool LockedLayers;
int ImCnt = rid.GetEntityCount(out LockedLayers);
if (ImCnt.Equals(0)) ImInfo.IsReferenced = false;
else {
string FoundAt = WillLoad(ImInfo.ImagePath, db);
if (!string.Compare(FoundAt, string.Empty).Equals(0))
ImInfo.ImageFoundAtPath = FoundAt;
ImInfo.IsReferenced = true;
}
ObjectId[] ImLocIdArray = new ObjectId[ImCnt];
string[] ImLocArray = new string[ImCnt];
int j = 0;
ResultBuffer rb = SafeNativeMethods.EntGet(ObjId);
TypedValue[] TvArray = rb.AsArray();
foreach (TypedValue Tv in TvArray) {
try {
ObjectId ReactId = (ObjectId)Tv.Value;
if (ReactId != ObjectId.Null) {
ResultBuffer ReactRb = SafeNativeMethods.EntGet(ReactId);
TypedValue[] ReactTvArray = ReactRb.AsArray();
foreach (TypedValue ReactTv in ReactTvArray) {
try {
ObjectId LocId = (ObjectId)ReactTv.Value;
ResultBuffer LocRb = SafeNativeMethods.EntGet(LocId);
TypedValue[] LocTvArray = LocRb.AsArray();
foreach (TypedValue LocTv in LocTvArray) {
bool IsIn = false;
foreach (ObjectId tempId in ImLocIdArray) {
if (tempId == LocId) IsIn = true;
}
if (LocTv.TypeCode == 410 && !IsIn) {
if (LoDict.GetAt((string)LocTv.Value) == ObjectId.Null)
ImLocArray[j] = "Block: " + (string)LocTv.Value;
else ImLocArray[j] = "Layout: " + (string)LocTv.Value;
ImLocIdArray[j] = LocId;
++j;
}
}
}
catch {}
}
}
}
catch {}
if (TvArray.Length.Equals(j)) break;
}
ImInfo.InsertedWhere = ImLocArray;
ImInfoArray[i] = ImInfo;
++i;
}
}
return ImInfoArray;
}
}
Code: [Select]
public class SafeNativeMethods
{
//Provided by Tony Tanzillo
[DllImport( "acdb16.dll", CallingConvention = CallingConvention.Cdecl,
           EntryPoint = "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AAY01JVAcDbObjectId@@@Z" )]
extern static public ErrorStatus acdbGetAdsName( out Int64 entres, ObjectId id );
[DllImport( "acad.exe", CallingConvention = CallingConvention.Cdecl )]
extern static IntPtr acdbEntGet( out Int64 e );
public static ResultBuffer EntGet( ObjectId id )
{
Int64 e;
if( acdbGetAdsName( out e, id ) == ErrorStatus.OK )
{
IntPtr res = acdbEntGet( out e );
if( res != IntPtr.Zero )
return ResultBuffer.Create( res, true );
}
return null;
}
}
Code: [Select]
public class MyImageInformation {
private string ImName;
private string ImPath;
private string ImFoundAt;
private bool Loaded;
private bool Embedded;
private bool IsRefed;
private string[] ImLoc;

public MyImageInformation() {}
public MyImageInformation (string ImName, string ImPath, bool Loaded) {}
public string ImageName {
get { return ImName; }
set { ImName = value; }
}
public string ImagePath {
get { return ImPath; }
set { ImPath = value; }
}
public string ImageFoundAtPath {
get { return ImFoundAt; }
set { ImFoundAt = value;}
}
public bool IsLoaded {
get { return Loaded; }
set { Loaded = value; }
}
public bool IsEmbedded {
get { return Embedded; }
set { Embedded = value; }
}
public bool IsReferenced {
get { return IsRefed; }
set { IsRefed = value; }
}
public string[] InsertedWhere {
get { return ImLoc; }
set { ImLoc = value; }
}
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Re: Get raster image paths with .net
« Reply #2 on: July 21, 2008, 08:06:51 PM »
wow, thanks for the code.
I had found this:
http://discussion.autodesk.com/thread.jspa?messageID=5368660
but you even made a class to hold the props, thanks a bunch.
James Maeding