Identifying number of times a Raster Image is used in a drawing

By Virupaksha Aithal

Each raster image will have a definition called “RasterImageDef” and will be stores inside the raster image Dictionary. A raster image reactor is stored in “RasterImageDef” for each image referred to it. So if you count the number of raster image reactor, then you can identify the number of raster images referring to same “RasterImageDef”. Refer below code.

[CommandMethod("ImageCount")]
public static void ImageCount()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;
 
    ObjectId imageDict = RasterImageDef.GetImageDictionary(db);
 
    if (imageDict == ObjectId.Null)
    {
        ed.WriteMessage("No images in the drawing.n");
        return;
    }
 
    using (Transaction Tx =
        db.TransactionManager.StartTransaction())
    {
        DBDictionary ImageDic =
          (DBDictionary)Tx.GetObject(imageDict,OpenMode.ForRead);
 
        ObjectIdCollection ImageCount = new ObjectIdCollection();
 
        foreach (DBDictionaryEntry ImageDef in ImageDic)
        {
            RasterImageDef imageDef = (RasterImageDef)Tx.GetObject(
                                ImageDef.Value, OpenMode.ForRead);
 
            ObjectIdCollection ids = 
                                imageDef.GetPersistentReactorIds();
 
            foreach (ObjectId id in ids)
            {
                DBObject reactor = 
                                Tx.GetObject(id, OpenMode.ForRead);
                string name = reactor.GetRXClass().DxfName;
                if (string.Compare(name, 
                                    "IMAGEDEF_REACTOR", true) == 0)
                {
                    if (!reactor.OwnerId.IsErased)
                    {
                        DBObject obj = Tx.GetObject(reactor.OwnerId,
                                                    OpenMode.ForRead);
 
                        if (obj is RasterImage)
                        {
                            ImageCount.Add(obj.ObjectId);
                        }
                    }
 
                }
            }
 
            if (ImageCount.Count > 0)
            {
                ed.WriteMessage("Raster image file " + 
                    imageDef.ActiveFileName + "  is used "
               + ImageCount.Count.ToString() + " timesn");
            }
 
            ImageCount.Clear();
        }
 
        Tx.Commit();
    } 
}

Comments

4 responses to “Identifying number of times a Raster Image is used in a drawing”

  1. Francois Avatar
    Francois

    Keep them coming ;-)
    If you have the same for xrefs, please, fell fre :-)

  2. Thanks for the comments.
    Sure, you will have xref related posts from our team.
    Thanks
    Viru

  3. Doublefish Avatar
    Doublefish

    How detect status in raster image ?
    FileNotFound and Unloaded ?

  4. Thanks for the comments.
    Please refer new post http://adndevblog.typepad.com/autocad/2012/06/identify-image-status.html for the image status.
    Thanks
    Viru

Leave a Reply to FrancoisCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading