Author Topic: How to select boundary points on pure Hatch?  (Read 365 times)

0 Members and 1 Guest are viewing this topic.

ken2023

  • Mosquito
  • Posts: 7
How to select boundary points on pure Hatch?
« on: March 14, 2024, 10:13:11 PM »
I create a hatch using the AutoCAD .NET,  and here is my code:

Code - C#: [Select]
  1.             var pts = new Point2d[]
  2.             {
  3.                 new Point2d(10, 0),
  4.                 new Point2d(10, 10),
  5.                 new Point2d(20, 10),
  6.                 new Point2d(20, 0),
  7.                 new Point2d(10, 0),
  8.             };
  9.             var ptCol = new Point2dCollection(pts);
  10.             var doubleCol = new DoubleCollection();
  11.  
  12.             var doc = Acap.DocumentManager.MdiActiveDocument;
  13.             using (var tran = doc.Database.TransactionManager.StartTransaction())
  14.             {
  15.                 var table = tran.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
  16.                 var record = tran.GetObject(table[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  17.  
  18.                 var hatch = new Hatch();
  19.                 record.AppendEntity(hatch);
  20.  
  21.                 hatch.AppendLoop(HatchLoopTypes.Default, ptCol, doubleCol);
  22.                 hatch.EvaluateHatch(true);
  23.  
  24.                 tran.AddNewlyCreatedDBObject(hatch, true);
  25.                 tran.Commit();
  26.  
  27.             }
  28.  

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

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to select boundary points on pure Hatch?
« Reply #1 on: March 15, 2024, 06:12:22 AM »
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#: [Select]
  1. using Autodesk.AutoCAD.DatabaseServices;
  2. using Autodesk.AutoCAD.Geometry;
  3. using Autodesk.AutoCAD.GraphicsInterface;
  4. using Autodesk.AutoCAD.Runtime;
  5.  
  6. namespace HatchBoundaryOsnap
  7. {
  8.     internal class HatchBoundOsnap
  9.     {
  10.         public static void Create()
  11.         {
  12.             var hatchBoundGlyph = new HatchBoundGlyph();
  13.             var hatchBoundSnapMode = new CustomObjectSnapMode("HBV", "HBV", "Hatch boundary vertex", hatchBoundGlyph);
  14.             var hatchBoundInfo = new HatchBoundInfo();
  15.             hatchBoundSnapMode.ApplyToEntityType(RXObject.GetClass(typeof(Hatch)), new AddObjectSnapInfo(HatchBoundInfo.VertexInfo));
  16.         }
  17.  
  18.         class HatchBoundGlyph : Glyph
  19.         {
  20.             private Point3d point;
  21.  
  22.             public override void SetLocation(Point3d point)
  23.             {
  24.                 this.point = point;
  25.             }
  26.  
  27.             protected override void SubViewportDraw(ViewportDraw vd)
  28.             {
  29.                 int glyphPixels = CustomObjectSnapMode.GlyphSize;
  30.                 Point2d glyphSize = vd.Viewport.GetNumPixelsInUnitSquare(point);
  31.                 double offset = (glyphPixels / glyphSize.Y) * 0.8;
  32.                 var points = new Point3dCollection
  33.                 {
  34.                     new Point3d(point.X, point.Y - offset, point.Z),
  35.                     new Point3d(point.X + offset, point.Y, point.Z),
  36.                     new Point3d(point.X, point.Y + offset, point.Z),
  37.                     new Point3d(point.X - offset, point.Y, point.Z),
  38.                     new Point3d(point.X, point.Y - offset, point.Z),
  39.                 };
  40.                 vd.Geometry.DeviceContextPolygon(points);
  41.             }
  42.         }
  43.  
  44.         class HatchBoundInfo
  45.         {
  46.             public static void VertexInfo(ObjectSnapContext context, ObjectSnapInfo result)
  47.             {
  48.                 if (context.PickedObject is Hatch hatch)
  49.                 {
  50.                     var xform = Matrix3d.PlaneToWorld(new Plane(Point3d.Origin, hatch.Normal));
  51.                     double elevation = hatch.Elevation;
  52.                     Point3d convert3d(Point2d pt) =>
  53.                         new Point3d(pt.X, pt.Y, elevation).TransformBy(xform);
  54.                     for (int i = 0; i < hatch.NumberOfLoops; i++)
  55.                     {
  56.                         var loop = hatch.GetLoopAt(i);
  57.                         if (loop.IsPolyline)
  58.                         {
  59.                             foreach (BulgeVertex vertex in loop.Polyline)
  60.                             {
  61.                                 result.SnapPoints.Add(convert3d(vertex.Vertex));
  62.                             }
  63.                         }
  64.                         else
  65.                         {
  66.                             foreach (Curve2d curve in loop.Curves)
  67.                             {
  68.                                 result.SnapPoints.Add(convert3d(curve.StartPoint));
  69.                             }
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.     }
  76. }
  77.  

To have this osnap available, simply call the HatchBoundOsnap.Create() method in the Initialize method of a class implementing IExtensionApplication.
Code - C#: [Select]
  1. using Autodesk.AutoCAD.ApplicationServices.Core;
  2. using Autodesk.AutoCAD.Runtime;
  3.  
  4. using System;
  5.  
  6. namespace HatchBoundaryOsnap
  7. {
  8.     public class Initialization : IExtensionApplication
  9.     {
  10.         public void Initialize()
  11.         {
  12.             HatchBoundOsnap.Create();
  13.         }
  14.  
  15.         public void Terminate()
  16.         { }
  17.     }
  18. }
  19.  

You can have a look at the attached screencast.
« Last Edit: March 15, 2024, 10:08:44 AM by gile »
Speaking English as a French Frog

ken2023

  • Mosquito
  • Posts: 7
Re: How to select boundary points on pure Hatch?
« Reply #2 on: March 16, 2024, 01:20:36 AM »