本帖最后由 箭头_Row 于 2025-11-8 00:01 编辑

直接上碼,一切在碼中。。。。。。

下面這段代碼的開發初始需求是使用源泉設計填充時會自動將填充透明度設置為80,好久麼折騰源泉,對其不熟悉了,麼知哪個參數控制填充的透明度,看久了填充透明度不bylayer看的眼痛,故搞個命令直接全圖替換其透明度吧!

  1. [CommandMethod(nameof(Transparency_Bylayer))]
  2. public static void Transparency_Bylayer()
  3. {
  4.     // 彈出防呆提示:防止誤操作
  5.     const string alarmMsg =
  6.         $"当前空间及所有块内的填充透明度变更为ByLayer,注意提前检查是否有单独设置透明度的填充色块!"
  7.         + "\n"
  8.         + "\n是否继续执行此操作?"
  9.         + "\n"
  10.         + "\n*此操作不可撤销,请谨慎操作!*";
  11.     if (!SheetSetCheck.ShowConfirmationDialog(alarmMsg))
  12.     {
  13.         "*取消*".Print();
  14.         return;
  15.     }

  16.     using DBTrans tr = new();
  17.     List<ObjectId> hatchIds =
  18.     [
  19.         .. tr
  20.             .CurrentSpace.Cast<ObjectId>()
  21.             .Where(id => id.ObjectClass.DxfName.Equals(nameof(Hatch), StringComparison.OrdinalIgnoreCase)),
  22.     ];

  23.     // 直接处理当前空间的填充
  24.     foreach (var id in hatchIds)
  25.     {
  26.         using var hatch = (Hatch)tr.GetObject(id);
  27.         EnsureTransparencyByLayer(hatch);
  28.     }

  29.     // 处理块表记录里的内容
  30.     foreach (var brId in tr.BlockTable)
  31.     {
  32.         using var btr = (BlockTableRecord)tr.GetObject(brId);
  33.         ProcessBlockTransparencyRecursive(btr);
  34.     }

  35.     Env.Editor.Regen();
  36. }

  37. private static void ProcessBlockTransparencyRecursive(BlockTableRecord btr)
  38. {
  39.     DBTrans tr = DBTrans.GetTop(btr.Database);
  40.     List<ObjectId> refIds = [];

  41.     if (btr.IsFromExternalReference)
  42.         return;
  43.     if (btr.IsLayout)
  44.         return;

  45.     refIds.AddRange(
  46.         btr.Cast<ObjectId>()
  47.             .Where(id =>
  48.                 id.ObjectClass.DxfName.Equals(nameof(Hatch), StringComparison.OrdinalIgnoreCase)
  49.                 || id.ObjectClass.DxfName == DxfMap[nameof(BlockReference)]
  50.             )
  51.     );

  52.     foreach (var id in refIds)
  53.     {
  54.         if (id.ObjectClass.DxfName != DxfMap[nameof(BlockReference)])
  55.         {
  56.             using var hatch = (Hatch)tr.GetObject(id);
  57.             EnsureTransparencyByLayer(hatch);
  58.         }
  59.         else
  60.         {
  61.             using var brInBtr = (BlockReference)tr.GetObject(id);
  62.             using var btrInBr = (BlockTableRecord)tr.GetObject(brInBtr.BlockTableRecord);
  63.             ProcessBlockTransparencyRecursive(btrInBr);
  64.         }
  65.     }
  66. }

  67. private static void EnsureTransparencyByLayer(Entity ent)
  68. {
  69.     if (!ent.Transparency.IsByLayer)
  70.     {
  71.         using (ent.ForWrite())
  72.             ent.Transparency = new Transparency(TransparencyMethod.ByLayer);
  73.     }
  74. }


  1.   /// <summary>
  2.   /// 定義選定對象類型的字典(类别class, DxfName)。
  3.   /// <para> 0x01 BlockReference:對應 "INSERT"。</para>
  4.   /// <para> 0x02 Polyline:對應 "LWPOLYLINE"。</para>
  5.   /// </summary>
  6.   public static readonly Dictionary<string, string> DxfMap = new()
  7.   {
  8.       { nameof(AttributeDefinition), "ATTDEF" },
  9.       { nameof(BlockReference), "INSERT" },
  10.       { nameof(DBText), "TEXT" },
  11.       { nameof(Polyline), "LWPOLYLINE" },
  12.   };


  1. /// <summary>
  2. /// 彈出防呆提示:執行命令前檢查布局是否已添加至圖紙集。
  3. /// </summary>
  4. internal static bool ShowConfirmationDialog(string alarmMsg)
  5. {
  6.      // 添加確認是否執行操作的交互消息框
  7.      DialogResult result = MessageBox.Show(
  8.          alarmMsg,
  9.          @"胡亂設計",
  10.          MessageBoxButtons.YesNoCancel,
  11.          MessageBoxIcon.Warning,
  12.          MessageBoxDefaultButton.Button3
  13.      );

  14.      return result == DialogResult.Yes;
  15. }





网友答: 使用场景是啥?

网友答: 看不太懂,不过谢谢分享

网友答: 本帖最后由 箭头_Row 于 2025-11-8 09:32 编辑
dingweiyang111 发表于 2025-11-8 08:00
使用场景是啥?

折騰半天,重點代碼段其實就是這個函數,最後都是通過這個函數來處理邏輯,其中用到了依據id來過濾不同類型的圖元,其二使用了塊內遞歸遍歷塊,其三使用了ifox封裝庫來簡化代碼對事務的commit or abourd自動處理。其四對塊表記錄理解,涉及對cad存儲結構的認識問題。

上面說的複雜,總結起來就是處理當前dwg文檔中過濾出的指定屬性的圖元,然後批量對其進行更改屬性的操作!

  1. private static void EnsureTransparencyByLayer(Entity ent)
  2. {
  3.     if (!ent.Transparency.IsByLayer)
  4.     {
  5.         using (ent.ForWrite())
  6.             ent.Transparency = new Transparency(TransparencyMethod.ByLayer);
  7.     }
  8. }


网友答:
dingweiyang111 发表于 2025-11-8 08:00
使用场景是啥?

你看懂了吗


网友答: 填充完,entlast,然后调调属性?EntityTransparency = "50"
  • 上一篇:长期更新CAD二次开发教程视频(原创)
  • 下一篇:没有了