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));
}
}
}
}
}
}
}
}