latour_g 发表于 2020-3-12 11:37:32

外部参照 - 重新加载图像

嗨,
我正在开发一个编辑外部参照的工具。对于dwg外部参照,它工作正常。对于图像,它的工作(我可以编辑路径),但图像消失了,我无法找到一种方法来重新加载它。基本上我不想做REDIR在Express Tools中做的事情。
下面是我编辑路径的步骤:
// 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);

我无法从收藏中的图像外部参照添加ID,在“db。错误的对象类型。
知道如何正确地做到这一点吗?
谢谢您!
**** Hidden Message *****

Jeff_M 发表于 2020-3-12 14:26:42

你试过使用CloseImage方法吗?根据文档:关闭关联的图像数据对象,并更新对AutoCAD中图像实例的任何更改。

latour_g 发表于 2020-3-12 15:15:12

您好,
感谢您的回答,但不幸的是,它不起作用,它仍然会消失,我必须手动刷新外部参照才能重新显示图像。

n.yuan 发表于 2020-3-13 12:36:25

虽然AutoCAD在Xreference管理器中显示插入到绘图中的光栅图像,但光栅图像与Xref有根本/完全不同的区别,即使用户以相同/相似的方式处理它以进行插入(附着),加载/重新加载...因此,Database.ReloadXrefs() 在绘图中对 RasterImage 没有任何作用。
在您的例子中,当 RasterInageDef.SourceFileName 发生更改时,RasterImage.IsLoaded 属性会发生变化(即,如果它是 true,它将更改为 false)。因此,在更改 SourceFileName 属性后,如果希望引用 RasterImageDef 的 RasterImage 可见,则需要调用 RasterImageDef.Load() 方法。
请参阅下面的代码示例:
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;

namespace UpdateImagePath
{
    public class MyCommands
    {
      
      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();
            }
      }
    }
}

注意:第二个警报对话框(在 SourceFileName 更改后)将显示 IsLoaded 属性变为“false”。

latour_g 发表于 2020-3-13 15:21:26

是的,现在的工作与.load()。谢谢
页: [1]
查看完整版本: 外部参照 - 重新加载图像