using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using System;
[assembly
: ExtensionApplication
(typeof(CustOsmodeSample
.CustomOsnap))]
namespace CustOsmodeSample
{
public class CustomOsnap : IExtensionApplication
{
private static Variable cosmode;
public void Initialize()
{
cosmode = SystemObjects.Variables["FITH_CUSTOSMODE"];
cosmode.Changed += cosmode_Changed;
FithGlyph glyph
= new FithGlyph
();
CustomObjectSnapMode fthOsmode
= new CustomObjectSnapMode
("FTH",
"FTH",
"Fith", glyph
); FithOsnapInfo fthInfo
= new FithOsnapInfo
(); fthOsmode.ApplyToEntityType(
RXClass
.GetClass(typeof(Curve
)), fthInfo
.FithSnapInfoCurve); fthOsmode.ApplyToEntityType(
RXClass
.GetClass(typeof(Entity
)), fthInfo
.FithSnapInfoEntity);
SetCustomOsnap((short)cosmode.Value);
}
public void Terminate() { }
void cosmode_Changed(object sender, VariableChangedEventArgs e)
{
SetCustomOsnap((short)e.NewValue);
}
private void SetCustomOsnap(int value)
{
if (value == 1 && !CustomObjectSnapMode.IsActive("FTH"))
CustomObjectSnapMode.Activate("FTH");
else if (value == 0 && CustomObjectSnapMode.IsActive("FTH"))
CustomObjectSnapMode.Deactivate("FTH");
}
[CommandMethod("ToggleFith", CommandFlags.Transparent)]
public static void ToggleFith()
{
cosmode.Value = (short)((short)cosmode.Value ^ 1);
}
}
class FithGlyph : 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 dist = (glyphPixels / glyphSize.Y) * 0.8;
Matrix3d e2w = vd.Viewport.EyeToWorldTransform;
double angle = Math.PI / 2.0;
double incr = 2.0 * Math.PI / 5.0;
Point3d[] points =
{
Polar(point, angle, dist),
Polar(point, angle + incr, dist),
Polar(point, angle + 2 * incr, dist),
Polar(point, angle + 3 * incr, dist),
Polar(point, angle + 4 * incr, dist)
};
vd.SubEntityTraits.LineWeight = LineWeight.LineWeight030;
vd
.Geometry.Polygon(new Point3dCollection
(points
)); }
private Point3d Polar(Point3d point, double angle, double distance)
{
point.X + distance * Math.Cos(angle),
point.Y + distance * Math.Sin(angle),
point.Z);
}
}
class FithOsnapInfo
{
public void FithSnapInfoCurve(ObjectSnapContext context, ObjectSnapInfo result)
{
Curve curve = context.PickedObject as Curve;
if (curve
== null || curve
is Xline
|| curve
is Ray
|| curve
is Spline
) return;
double len = curve.GetDistanceAtParameter(curve.EndParam);
double dist = len / 5.0;
result.SnapPoints.Add(curve.GetPointAtDist(dist));
result.SnapPoints.Add(curve.GetPointAtDist(dist * 2.0));
result.SnapPoints.Add(curve.GetPointAtDist(dist * 3.0));
result.SnapPoints.Add(curve.GetPointAtDist(dist * 4.0));
}
public void FithSnapInfoEntity(ObjectSnapContext context, ObjectSnapInfo result) { }
}
}