Author Topic: Xref - reload image  (Read 2424 times)

0 Members and 1 Guest are viewing this topic.

latour_g

  • Newt
  • Posts: 184
Xref - reload image
« on: March 12, 2020, 11:37:32 AM »
Hi,

I'm working on a tool to edit xref.  For dwg xref it work fine.  For image, it work (I'm able to edit the path) but the image disappear and I can't find a way to reload it programatically.  Basically I wan't to do what REDIR does in Express Tools. 

Here is what I do to edit the path :

Code: [Select]
// XREF DWG
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
foreach (ObjectId btrId in bt)
{
    BlockTableRecord btr = tr.GetObject(btrId, OpenMode.ForWrite) as BlockTableRecord;
    if (btr.IsFromExternalReference)
    {
        btr.PathName = btr.PathName.Replace("U:\\", cServerName);
        collection.Add(btrId);
    }
}

// XREF IMAGE
if (dictDB.Contains(imgkey))
{
    DBDictionary imgDic = (DBDictionary)tr.GetObject(dictDB.GetAt(imgkey), OpenMode.ForWrite);

    foreach (DBDictionaryEntry dbe in imgDic)
    {
        if (String.IsNullOrEmpty(dbe.m_key)) continue;
        RasterImageDef underlayDefinition = (RasterImageDef)tr.GetObject(dbe.Value, OpenMode.ForWrite);

        if (underlayDefinition.SourceFileName.StartsWith("G:\\") || underlayDefinition.SourceFileName.StartsWith("U:\\"))
        {
                underlayDefinition.SourceFileName = underlayDefinition.SourceFileName.Replace("U:\\", cServerName);
                //collection.Add(underlayDefinition.Id);
                //collection.Add(underlayDefinition.ObjectId);
        }
    }
}

if (collection.Count > 0) db.ReloadXrefs(collection);

I can't add ID from image xref in my collection, I get an error at "db.ReloadXrefs" wrong object type.

Any idea on how to do that properly ?

Thank you !
« Last Edit: March 12, 2020, 11:42:11 AM by latour_g »

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Xref - reload image
« Reply #1 on: March 12, 2020, 02:26:42 PM »
Did you try using the CloseImage method? According to the docs: Closes the associated image data object, and updates any changes to the image instances in AutoCAD.

latour_g

  • Newt
  • Posts: 184
Re: Xref - reload image
« Reply #2 on: March 12, 2020, 03:15:12 PM »
Hi,

Thanks for your answer but unforntunately it's not working, it still disappear and I have to refresh the xref manually for the image to reappear.

n.yuan

  • Bull Frog
  • Posts: 348
Re: Xref - reload image
« Reply #3 on: March 13, 2020, 12:36:25 PM »
While AutoCAD shows raster image inserted into Drawing in Xreference Manager, rasterimage is fundamentally/quite different from Xref, even user deals it in same/similar ways for inserting(attaching), load/reload... So, Database.Reload[Resulve]Xrefs() does nothing to RasterImage in drawing.

In your case, when RasterInageDef.SourceFileName is changed, RasterImage.IsLoaded property changes (i.e. if it was true, it changes to false). So, after changing SourceFileName property, if you want the RasterImage that references the RasterImageDef to be visible, you need to call RasterImageDef.Load() method.

See following code example:

Code: [Select]
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(UpdateImagePath.MyCommands))]

namespace UpdateImagePath
{
    public class MyCommands
    {
        [CommandMethod("ImgPath")]
        public static void RunMyCommand()
        {
            var imgPath =
                @"C:\Users\norman.yuan\Documents\Visual Studio 2019\Projects\AutoCAD2020\MiscTests\UpdateImagePath\Images";

            var doc = CadApp.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;

            var imgId = SelectImage(ed);
            if (!imgId.IsNull)
            {
                UpdateImagePath(imgId, imgPath);
                ed.UpdateScreen();
            }
        }

        private static ObjectId SelectImage(Editor ed)
        {
            var opt = new PromptEntityOptions(
                "\nSelect raster image:");
            opt.SetRejectMessage("\nNot raster image!");
            opt.AddAllowedClass(typeof(RasterImage), true);
            var res = ed.GetEntity(opt);

            return res.Status == PromptStatus.OK ? res.ObjectId : ObjectId.Null;
        }

        private static void UpdateImagePath(ObjectId imgId, string imgPath)
        {
            using (var tran = imgId.Database.TransactionManager.StartTransaction())
            {
                var img = (RasterImage)tran.GetObject(imgId, OpenMode.ForRead);
                var imgDef = (RasterImageDef)tran.GetObject(img.ImageDefId, OpenMode.ForWrite);
                CadApp.ShowAlertDialog(
                    $"IsLoaded: {imgDef.IsLoaded}");
                var fileName = System.IO.Path.GetFileName(imgDef.SourceFileName);
                imgDef.SourceFileName = imgPath + "\\" + fileName;
                CadApp.ShowAlertDialog(
                    $"IsLoaded after SourceFileName changed: {imgDef.IsLoaded}");
                imgDef.Load();

                tran.Commit();
            }
        }
    }
}

Notice: the second alert dialog (after SourceFileName is changed) would show IsLoaded property becomes "false".

latour_g

  • Newt
  • Posts: 184
Re: Xref - reload image
« Reply #4 on: March 13, 2020, 03:21:26 PM »
Yes, it's working great now with .load().  Thanks a lot !