Net Core教程

cad.net 封装一个填充类

本文主要是介绍cad.net 封装一个填充类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

封装一个填充类

渐变填充部分参考了才鸟的书里的代码,但是我改的也挺多的...

调用

namespace JoinBox
{
    public partial class CmdTest
    {
        /// <summary>
        /// 测试填充
        /// </summary>
        [CommandMethod("CmdTest_hatch")]
        public void CmdTest_hatch()
        {
            var dm = Acap.DocumentManager;
            var doc = dm.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            ed.WriteMessage($"{Environment.NewLine}填充测试");

            //这里是选择已有图元加入填充
            int actionNum = 4;
            if ((actionNum & 1) == 1)
            {
                var psr = ed.GetSelection();
                if (psr.Status != PromptStatus.OK)
                    return;
                db.Action(tr => {
                    new HatchInfo(psr.Value.GetObjectIds(), 100)
                        .Mode1PreDefined("ANSI34")
                        .Build(tr, db);
                });
            }
            if ((actionNum & 2) == 2)
            {
                var psr = ed.GetSelection();
                if (psr.Status != PromptStatus.OK)
                    return;

                db.Action(tr => {
                    new HatchInfo(psr.Value.GetObjectIds(), 100)
                          .Mode2UserDefined()
                          .Build(tr, db);
                });
            }

            if ((actionNum & 4) == 4)
            {
                var psr = ed.GetSelection();
                if (psr.Status != PromptStatus.OK)
                    return;

                db.Action(tr => {
                    new HatchInfo(psr.Value.GetObjectIds(), 100, Math.PI / 2)
                    .Mode4Gradient(HatchInfo.HatchGradientName.Linear,
                                   Autodesk.AutoCAD.Colors.Color.FromRgb(0, 0, 0),
                                   Autodesk.AutoCAD.Colors.Color.FromRgb(152, 35, 100))
                    .Build(tr, db);
                });
            }
        }
    }
}

封装

#if !HC2020
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
#else
using GrxCAD.DatabaseServices;
using GrxCAD.Geometry;
using GrxCAD.Colors;
#endif
using System.Collections.Generic;
using System;

namespace JoinBox
{
    /// <summary>
    /// 图案填充
    /// </summary>
    public class HatchInfo
    {
        #region 枚举
        /// <summary>
        /// 渐变色填充的图案名称
        /// </summary>
        public enum HatchGradientName
        {
            /// <summary>
            /// 线状渐变
            /// </summary>
            Linear,
            /// <summary>
            /// 圆柱状渐变
            /// </summary>
            Cylinder,
            /// <summary>
            /// 反圆柱状渐变
            /// </summary>
            Invcylinder,
            /// <summary>
            /// 球状渐变
            /// </summary>
            Spherical,
            /// <summary>
            /// 反球状渐变
            /// </summary>
            Invspherical,
            /// <summary>
            /// 半球状渐变
            /// </summary>
            Hemisperical,
            /// <summary>
            /// 反半球状渐变
            /// </summary>
            InvHemisperical,
            /// <summary>
            /// 抛物面状渐变
            /// </summary>
            Curved,
            /// <summary>
            /// 反抛物面状渐变
            /// </summary>
            Incurved
        }
        #endregion

        #region 成员
        /// <summary>
        /// 边界id(最外面放第一)
        /// </summary>
        IEnumerable<ObjectId> _boundaryIds;
        /// <summary>
        /// 填充图元
        /// </summary>
        Hatch _hatch;
        /// <summary>
        /// 填充的名字
        /// </summary>
        string _hatchName;
        /// <summary>
        /// 填充模式类型(预定义/用户定义/自定义)
        /// </summary>
        HatchPatternType _patternTypeHatch;
        /// <summary>
        /// 渐变模式类型
        /// </summary>
        GradientPatternType _patternTypeGradient;
        /// <summary>
        /// 比例/间距
        /// </summary>
        double _hatchScale;
        /// <summary>
        /// 角度
        /// </summary>
        double _hatchAngle;
        /// <summary>
        /// 关联边界
        /// </summary>
        bool _boundaryAssociative = true;

        #endregion

        #region 构造
        /// <summary>
        /// 图案填充
        /// </summary>
        /// <param name="boundaryIds">边界</param>
        /// <param name="hatchScale">比例</param>
        /// <param name="hatchAngle">角度</param>
        /// <param name="hatchOrigin">填充原点</param>
        /// <param name="boundaryAssociative">关联边界</param>
        public HatchInfo(IEnumerable<ObjectId> boundaryIds,
                         double hatchScale = 1,
                         double hatchAngle = 0,
                         bool boundaryAssociative = true,
                         Point2d? hatchOrigin = null)
        {
            _boundaryIds = boundaryIds;
            _hatchScale = hatchScale;
            _hatchAngle = hatchAngle;
            _boundaryAssociative = boundaryAssociative;
            hatchOrigin ??= Point2d.Origin;

            _hatch = new Hatch();
            _hatch.SetDatabaseDefaults();
            _hatch.Origin = hatchOrigin.Value; //填充原点
            _hatch.PatternScale = _hatchScale; //填充比例
            _hatch.PatternAngle = _hatchAngle; //填充角度
        }
        #endregion

        #region 方法
        /// <summary>
        /// 模式1:预定义
        /// </summary>
        public HatchInfo Mode1PreDefined(string name)
        {
            _hatchName = name;
            _patternTypeHatch = HatchPatternType.PreDefined;
            return this;
        }

        /// <summary>
        /// 模式2:用户定义
        /// </summary>
        /// <param name="patternDouble">是否双向</param>
        public HatchInfo Mode2UserDefined(bool patternDouble = true)
        {
            _hatchName = "_USER";
            _patternTypeHatch = HatchPatternType.UserDefined;
            _hatch.HatchObjectType = HatchObjectType.HatchObject; //对象类型(填充/渐变)

            _hatch.PatternDouble = patternDouble;                 //是否双向(必须写在 SetHatchPattern 之前)
            _hatch.PatternSpace = _hatchScale;                    //间距(必须写在 SetHatchPattern 之前)

            return this;
        }

        /// <summary>
        /// 模式3:自定义
        /// </summary>
        /// <param name="name"></param>
        public HatchInfo Mode3UserDefined(string name)
        {
            _hatchName = name;
            _patternTypeHatch = HatchPatternType.CustomDefined;
            _hatch.HatchObjectType = HatchObjectType.HatchObject; //对象类型(填充/渐变)

            return this;
        }

        /// <summary>
        /// 模式4:渐变填充
        /// </summary>
        /// <param name="name">渐变填充名称</param>
        /// <param name="colorStart">渐变色起始颜色</param>
        /// <param name="colorEnd">渐变色结束颜色</param>
        /// <param name="gradientShift">渐变移动</param>
        /// <param name="shadeTintValue">色调值</param>
        /// <param name="gradientOneColorMode">单色<see langword="true"/>双色<see langword="false"/></param>
        public HatchInfo Mode4Gradient(HatchGradientName name, Color colorStart, Color colorEnd,
            float gradientShift = 0,
            float shadeTintValue = 0,
            bool gradientOneColorMode = false)
        {
            //_hatchName = "SOLID"; //渐变的话,名字必然是它,但是不用写
            _hatchName = name.ToString();
            _patternTypeGradient = GradientPatternType.PreDefinedGradient;//.....................这里另一种模式缺少哦
            _hatch.HatchObjectType = HatchObjectType.GradientObject;      //对象类型(填充/渐变)

            //设置渐变色填充的起始和结束颜色
            var gColor1 = new GradientColor(colorStart, 0);
            var gColor2 = new GradientColor(colorEnd, 1);
            _hatch.SetGradientColors(new GradientColor[] { gColor1, gColor2 });

            _hatch.GradientShift = gradientShift;              //渐变移动
            _hatch.ShadeTintValue = shadeTintValue;            //色调值
            _hatch.GradientOneColorMode = gradientOneColorMode;//单色/双色
            _hatch.GradientAngle = _hatchAngle;                //渐变角

            return this;
        }

        /// <summary>
        /// 构建
        /// </summary>
        /// <param name="tr">事务</param>
        /// <param name="db">数据库</param>
        /// <param name="associative">关联边界</param>
        /// <returns></returns>
        public ObjectId Build(Transaction tr, Database db)
        {
            var id = tr.AddEntityToMsPs(db, _hatch);

            if (_hatch.HatchObjectType == HatchObjectType.GradientObject)
                _hatch.SetGradient(_patternTypeGradient, _hatchName);  //渐变模式类型
            else
                _hatch.SetHatchPattern(_patternTypeHatch, _hatchName); //填充模式类型

            _hatch.Associative = _boundaryAssociative; //关联边界(不先添加数据库会出错)

            var obIds = new ObjectIdCollection();

            //边界是闭合的,而且已经加入数据库
            //填充闭合环类型.最外面
            foreach (var border in _boundaryIds)
            {
                obIds.Clear();
                obIds.Add(border);
                _hatch.AppendLoop(HatchLoopTypes.Default, obIds.Collection);
            }
            _hatch.EvaluateHatch(true); //计算填充并显示(若边界出错,这句会异常)

            obIds.Dispose();

            return id;
        }

        /// <summary>
        /// 执行图元的属性修改
        /// </summary>
        /// <param name="action">扔出填充实体</param>
        /// <returns></returns>
        public HatchInfo Action(Action<Hatch> action)
        {
            action(_hatch);
            return this;
        }
        #endregion
    }
}
这篇关于cad.net 封装一个填充类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!