CAD二次开发 解决填充图案(Hatch)填充比例无法自动更新问题

前言:记录在项目上遇到的bug和群友提供的解决方案。

背景

在项目中需要用程序自动修改填充图案Hatch的填充图案比例属性(PatternScale属性),把比例10改成比例20,但是修改后发现填充图案比例数值已经修改为20,但是图案显示的比例还是10,并没有刷新。因此有了这篇文章的记录

原填充:

原填充图案比例

使用代码修改

在这里插入图片描述

代码

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[assembly:CommandClass(typeof(CADBlogDemo.HatchUnableToUpdateCmd))]

namespace CADBlogDemo
{
    public class HatchUnableToUpdateCmd
    {
        [CommandMethod(nameof(HatchUnableToUpdate))]
        public void HatchUnableToUpdate()
        {
            //选择一个填充图案
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            PromptEntityResult selResult = doc.Editor.GetEntity(new Autodesk.AutoCAD.EditorInput.PromptEntityOptions("选择修改的填充图案"));
            if (selResult.Status != PromptStatus.OK) return;
            using(Transaction trans = doc.TransactionManager.StartTransaction())
            {
                Hatch targetHacth = trans.GetObject(selResult.ObjectId,OpenMode.ForWrite) as Hatch;
                if(targetHacth == null)
                {
                    trans.Abort();
                    return;
                }
                //修改填充图案的比例
                targetHacth.PatternScale = 20;
                targetHacth.SetHatchPattern(targetHacth.PatternType, targetHacth.PatternName);
                trans.Commit();
            }

        }
    }
}

代码解析

SetHatchPattern方法

重新设置填充图案的图案,这样子就可以解决填充图案无法自动更新的问题

解决后

在这里插入图片描述

写在最后

这里需要注意一个问题,使用者环境中没有加载到填充图案的时候,直接修改会报无效构件的错误信息。因此,如果你的图案是自定义图案,最好可以跟着程序文件一起移动,然后在受信任路径中添加对应的路径就可以使用了。希望可以帮你避开不必要的坑~~~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是C#二次开发CAD填充代码的示例: ```csharp using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Geometry; using System; namespace FillPolyline { public class Commands { [CommandMethod("FillPolyline")] public void FillPolyline() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // 选择多段线 PromptEntityOptions peo = new PromptEntityOptions("\n选择多段线: "); peo.SetRejectMessage("\n必须选择一个多段线!"); peo.AddAllowedClass(typeof(Polyline), true); PromptEntityResult per = ed.GetEntity(peo); if (per.Status != PromptStatus.OK) return; using (Transaction tr = db.TransactionManager.StartTransaction()) { Polyline pline = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Polyline; if (pline == null) return; // 创建填充对象 Hatch hatch = new Hatch(); hatch.SetDatabaseDefaults(); hatch.Layer = pline.Layer; hatch.ColorIndex = pline.ColorIndex; hatch.PatternScale = 1.0; // 设置填充类型 hatch.PatternType = HatchPatternType.PreDefined; hatch.PatternName = "SOLID"; // 获取多段线边界 Point3dCollection pts = new Point3dCollection(); for (int i = 0; i < pline.NumberOfVertices; i++) { pts.Add(pline.GetPoint3dAt(i)); } // 添加填充边界 hatch.AppendLoop(HatchLoopTypes.Outermost, pts); // 将填充对象添加到模型空间 BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; btr.AppendEntity(hatch); tr.AddNewlyCreatedDBObject(hatch, true); // 关联填充对象和多段线 hatch.Associative = true; hatch.AppendLoop(HatchLoopTypes.Default, new ObjectIdCollection() { pline.ObjectId }); hatch.EvaluateHatch(true); tr.Commit(); } } } } ``` 上述代码中,通过 `PromptEntityOptions` 和 `ed.GetEntity` 方法选择了一个多段线,然后创建了一个填充对象并将其添加到模型空间。最后,通过将填充对象与多段线关联,实现了填充功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baobao熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值