Cad二次开发图案填充系统预值写到构造函数

图案填充系统预值写到构造函数

填充图案

public struct HatchPatternName
{
    public static readonly string solid = "SOLID";
    public static readonly string angle = "ANGLE";
    public static readonly string ansi31 = "ANSI31";
    public static readonly string ansi32 = "ANSI32";
    public static readonly string ansi33 = "ANSI33";
    public static readonly string ansi34 = "ANSI34";
    public static readonly string ansi35 = "ANSI35";
    public static readonly string ansi36 = "ANSI36";
    public static readonly string ansi37 = "ANSI37";
    public static readonly string ansi38 = "ANSI38";
    public static readonly string arb816 = "AR-B816";
    public static readonly string arb816C = "AR-B816C";
    public static readonly string arb88 = "AR-B88";
    public static readonly string arbrelm = "AR-BRELM";
    public static readonly string arbrstd = "AR-BRSTD";
    public static readonly string arbconc = "AR-CONC";
}

渐变填充图案

public struct HatchGradientName
{
    public static readonly string gr_linear = "Linear";
    public static readonly string gr_cylinder = "CyLinder";
    public static readonly string gr_invcylinder = "InvcyLinder";
    public static readonly string gr_spherical = "Spherical";
    public static readonly string gr_hemispherical = "Hemispherical";
    public static readonly string gr_curved = "Curved";
    public static readonly string gr_invspherical = "Invspherical";
    public static readonly string gr_invhemisperical = "Invhemispherical";
    public static readonly string gr_invcurved = "Invcurved";
}

使用

[CommandMethod("HatchDemo")]
        public void HatchDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            ObjectId rect = db.AddRectToModelSpace(new Point2d(100,100),new Point2d(500,300));
            
            ObjectId hatchId = ObjectId.Null;
            ObjectIdCollection objIds = new ObjectIdCollection();
            objIds.Add(rect);


            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //声明填充对象
                Hatch hatch = new Hatch();
                //设置填充类型为渐变填充
                hatch.HatchObjectType = HatchObjectType.GradientObject;
                //设置渐变填充的类型(系统预定义or客户自定义)和渐变填充的图案名称
                hatch.SetGradient(GradientPatternType.PreDefinedGradient, HatchTool.HatchGradientName.gr_hemispherical);
                //设置填充颜色
                Color color1 = Color.FromColorIndex(ColorMethod.ByColor, 1);
                Color color2 = Color.FromColorIndex(ColorMethod.ByColor, 2);
                GradientColor gColor1 = new GradientColor(color1, 0);
                GradientColor gColor2 = new GradientColor(color2, 1);
                new GradientColor(color2, 1);
                hatch.SetGradientColors(new GradientColor[] { gColor1, gColor2 });
                //将填充对象加入图形数据库
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                btr.AppendEntity(hatch);
                //图形数据库数据更新
                trans.AddNewlyCreatedDBObject(hatch, true);
                //添加关联
                hatch.Associative = true;
                hatch.AppendLoop(HatchLoopTypes.Outermost, objIds);
                //计算并显示填充
                hatch.EvaluateHatch(true);

                //提交事务处理
                trans.Commit();
            }
        }

NetLoad

  • 1
    点赞
  • 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
发出的红包

打赏作者

周杰伦fans

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

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

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

打赏作者

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

抵扣说明:

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

余额充值