首页 > 其他分享 >【AutoCAD .NET】如何在无边界Hatch上选择边界点?

【AutoCAD .NET】如何在无边界Hatch上选择边界点?

时间:2024-03-16 13:45:42浏览次数:33  
标签:AutoCAD point hatch 边界点 Hatch var new Point3d

出处

https://www.theswamp.org/index.php?topic=59354.msg619875#msg619875

问题描述

用以下代码创建了一个Hatch,这Hatch周围没有创建Polyline,如何选择这个无边界Hatch的边界上的点。
比如用line命令时如何捕捉Hatch边界上的点?
希望有一个简洁的方案。

            var pts = new Point2d[]
            {
                new Point2d(10, 0),
                new Point2d(10, 10),
                new Point2d(20, 10),
                new Point2d(20, 0),
                new Point2d(10, 0),
            };
            var ptCol = new Point2dCollection(pts);
            var doubleCol = new DoubleCollection();

            var doc = Acap.DocumentManager.MdiActiveDocument;
            using (var tran = doc.Database.TransactionManager.StartTransaction())
            {
                var table = tran.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
                var record = tran.GetObject(table[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                var hatch = new Hatch();
                record.AppendEntity(hatch);

                hatch.AppendLoop(HatchLoopTypes.Default, ptCol, doubleCol);
                hatch.EvaluateHatch(true);

                tran.AddNewlyCreatedDBObject(hatch, true);
                tran.Commit();

            }
 

回答

可以为图案填充边界顶点创建自定义对象捕捉。

系统变量OSPTIONS的值不得包括1(即0、2、4或6)。

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
 
namespace HatchBoundaryOsnap
{
    internal class HatchBoundOsnap
    {
        public static void Create()
        {
            var hatchBoundGlyph = new HatchBoundGlyph();
            var hatchBoundSnapMode = new CustomObjectSnapMode("HBV", "HBV", "Hatch boundary vertex", hatchBoundGlyph);
            var hatchBoundInfo = new HatchBoundInfo();
            hatchBoundSnapMode.ApplyToEntityType(RXObject.GetClass(typeof(Hatch)), new AddObjectSnapInfo(HatchBoundInfo.VertexInfo));
        }
 
        class HatchBoundGlyph : Glyph
        {
            private Point3d point;
 
            public override void SetLocation(Point3d point)
            {
                this.point = point;
            }
 
            protected override void SubViewportDraw(ViewportDraw vd)
            {
                int glyphPixels = CustomObjectSnapMode.GlyphSize;
                Point2d glyphSize = vd.Viewport.GetNumPixelsInUnitSquare(point);
                double offset = (glyphPixels / glyphSize.Y) * 0.8;
                var points = new Point3dCollection
                {
                    new Point3d(point.X, point.Y - offset, point.Z),
                    new Point3d(point.X + offset, point.Y, point.Z),
                    new Point3d(point.X, point.Y + offset, point.Z),
                    new Point3d(point.X - offset, point.Y, point.Z),
                    new Point3d(point.X, point.Y - offset, point.Z),
                };
                vd.Geometry.DeviceContextPolygon(points);
            }
        }
 
        class HatchBoundInfo
        {
            public static void VertexInfo(ObjectSnapContext context, ObjectSnapInfo result)
            {
                if (context.PickedObject is Hatch hatch)
                {
                    var xform = Matrix3d.PlaneToWorld(new Plane(Point3d.Origin, hatch.Normal));
                    double elevation = hatch.Elevation;
                    Point3d convert3d(Point2d pt) =>
                        new Point3d(pt.X, pt.Y, elevation).TransformBy(xform);
                    for (int i = 0; i < hatch.NumberOfLoops; i++)
                    {
                        var loop = hatch.GetLoopAt(i);
                        if (loop.IsPolyline)
                        {
                            foreach (BulgeVertex vertex in loop.Polyline)
                            {
                                result.SnapPoints.Add(convert3d(vertex.Vertex));
                            }
                        }
                        else
                        {
                            foreach (Curve2d curve in loop.Curves)
                            {
                                result.SnapPoints.Add(convert3d(curve.StartPoint));
                            }
                        }
                    }
                }
            }
        }
    }
}
 

若要使此对象捕捉可用,只需在实现IExtensionApplication的类的Initialize方法中,调用HatchBoundOsnap.Create()方法。

using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.Runtime;
 
using System;
 
namespace HatchBoundaryOsnap
{
    public class Initialization : IExtensionApplication
    {
        public void Initialize()
        {
            HatchBoundOsnap.Create();
        }
 
        public void Terminate()
        { }
    }
}
 

标签:AutoCAD,point,hatch,边界点,Hatch,var,new,Point3d
From: https://www.cnblogs.com/redcode/p/18076986

相关文章

  • Langchain-Chatchat开源库使用的随笔记(一)
    转自:https://zhuanlan.zhihu.com/p/6760612691Chatchat项目结构整个结构是server 启动API,然后项目内自行调用API。API详情可见:http://xxx:7861/docs ,整个代码架构还是蛮适合深入学习 在这里插入图片描述 2Chatchat一些代码学习2.112个分块函数统一使用截止2023......
  • 【AutoCAD .NET】创建Hatch时报错eInvalidInput
    问题出处https://forums.autodesk.com/t5/net/hatch-to-drawn-polyline-e-message-quot-einvalidinput-quot/m-p/9631373问题描述我使用以下语句绘制了一条多段线:Acad.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("PL",true,false,true);按如下......
  • C# AutoCAD 图纸合并以及拆分
    拆分和合并非常常规的需求usingAutodesk.AutoCAD.ApplicationServices;usingAutodesk.AutoCAD.DatabaseServices;usingAutodesk.AutoCAD.EditorInput;usingAutodesk.AutoCAD.Geometry;usingAutodesk.AutoCAD.Runtime;usingMicrosoft.VisualBasic.ApplicationServices......
  • 关于AutoCAD反复弹窗Nonvalid Software Detected的解决办法
    事件起因:客户安装的CAD2020频繁弹窗NonvalidSoftwareDetected,报错内容:YOURACCESSISNOWBLOCKED 解决办法:在文件资源管理器中搜索路径C:\ProgramFiles\Autodesk\AutoCAD2020\Support\NewTabPage\config\ACAD\zh-CN(注意自己安装的版本和位置,我这里是2020版本安......
  • AutoCAD2024画圆或矩形实时预览消失了如何解决?
    最近有小伙伴问这个问题,他在使用AutoCAD绘制图形时,发现画圆或矩形实时预览没有了,如下,画图不容易定位,非常影响画图效率,十分苦恼不知道如何恢复? 正常CAD画圆(或矩形)会显示实时预览,如下:操作步骤:AutoCAD20241、打开AutoCAD2024软件,然后在命令栏输入:DRAGMODE,然后按Enter键......
  • 梳理Langchain-Chatchat知识库API接口
    一.Langchain-Chatchat知识库管理1.Langchain-Chatchat对话和知识库管理界面  Langchain-Chatchatv0.28完整的界面截图,如下所示:2.知识库中源文件和向量库  知识库test中源文件和向量库的位置,如下所示:3.知识库表结构  knowledge_base数据表内容,如下所示:二.......
  • 解决方案 | AutoCAD 版本+版本号+受支持的 .NET SDK版本+.NET Framework版本
    关于Managed.NET兼容性Managed.NET应用程序通常与扩展基于AutoCAD的产品的行为和功能的公司和第三方应用程序关联。在移植到最新版本后,并非所有.NET应用程序都可以正常工作。.NET应用程序的兼容性在各版本之间可能随时更改,以利用最新的.NETFramework和Auto......
  • AUTOCAD快捷键
    快捷命令L直线M移动C圆EL椭圆XL射线轴线RO旋转E删除H填充TR修剪EX延伸PO点S拉伸U返回DDI直径标注DAN角度标注OP系统选项设置A圆弧T多行文字B块定义I块插入W定义块文件CO复制MI镜像O偏移F倒直角或圆角D标注样式DLI线性......
  • AutoCAD Civil 3D 2024:实现精准建模与仿真,优化基础设施项目
    AutoCADCivil3D2024是一款专门为土木工程师和土地设计师打造的高级三维设计软件。它基于AutoCAD平台,并提供了丰富的工具和功能,以支持从规划、设计到施工的整个基础设施项目生命周期。点击获取AutoCADCivil3D2024AutoCADCivil3D2024引入了许多新功能和改进,以提升用......
  • 大模型工具_Langchain-Chatchat
    https://github.com/chatchat-space/Langchain-Chatchat原Langchain-ChatGLM1功能整体功能,想解决什么问题基于Langchain与ChatGLM等LLM模型,搭建一套针对中文场景与开源模型,界面友好、可离线运行的知识库问答解决方案。当前解决了什么问题,哪些问题解决不了目前0.2.8......