Author Topic: How to get Raster Name  (Read 2537 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
How to get Raster Name
« 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.
Code: [Select]
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();
            }
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: How to get Raster Name
« Reply #1 on: July 27, 2010, 10:06:55 AM »
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. 
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: How to get Raster Name
« Reply #2 on: July 27, 2010, 10:36:13 AM »
I think I found it in Keans post.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: How to get Raster Name
« Reply #3 on: July 27, 2010, 10:38:41 AM »
Are you talking to yourself (again)  CMDR? 
Be your Best


Michael Farrell
http://primeservicesglobal.com/

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: How to get Raster Name
« Reply #4 on: July 27, 2010, 10:47:03 AM »
Here is what I ended up with
Code: [Select]
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();
            }
        }
    }
}
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: How to get Raster Name
« Reply #5 on: July 27, 2010, 10:49:59 AM »
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?
Code: [Select]
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
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)