Code Red > .NET

How to select boundary points on pure Hatch?

(1/1)

ken2023:
I create a hatch using the AutoCAD .NET,  and here is my code:


--- Code - C#: ---            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();             } 
The code works well, and I can see a hatch in the AutoCAD interface.

Now I want to draw a line, so I use "line" command, but I can't pick a point on the Hatch I created.
I hope to pick boundary points on Hatch, even if there is no Polyline around Hatch.

I have searched for some information, and I may need to customize an entity using C++ to realize the function. I hope to obtain a more concise solution.

Thanks a lot.

gile:
Hi,

You can create a custom object snap for the hatch boundary vertices.

The value of OSPTIONS sysvar must not include 1 (i.e. 0, 2, 4 or 6).


--- Code - C#: ---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));                            }                        }                    }                }            }        }    }} 
To have this osnap available, simply call the HatchBoundOsnap.Create() method in the Initialize method of a class implementing IExtensionApplication.

--- Code - C#: ---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()        { }    }} 
You can have a look at the attached screencast.

ken2023:
I really appreciate this.


--- Quote from: gile on March 15, 2024, 06:12:22 AM ---



--- End quote ---

Navigation

[0] Message Index

Go to full version