using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
namespace DrawJigQuestion
{
public class Class1
{
[CommandMethod("DrawJigQuestion")]
public static void Main()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
RotatedDimension en = null;
var peo = new PromptEntityOptions("\nSelect rotated dimension: ");
peo.SetRejectMessage("\nWrong!");
peo.AddAllowedClass(typeof(RotatedDimension), false);
var per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
Point3d pWCS = per.PickedPoint.TransformBy(ed.CurrentUserCoordinateSystem);
var jig = new DJig(); // JIg
using (RotatedDimension enOrig = per.ObjectId.Open(OpenMode.ForWrite) as RotatedDimension)
{
en = enOrig.Clone() as RotatedDimension;
enOrig.Visible = false;
if (pWCS.DistanceTo(en.XLine1Point) < pWCS.DistanceTo(en.XLine2Point))
jig.firstPoint = true;
}
doc.TransactionManager.FlushGraphics();
var rs = jig.StartJig(en); // Jig result
using (RotatedDimension enOrig = per.ObjectId.Open(OpenMode.ForWrite) as RotatedDimension)
{
enOrig.Visible = true;
if (rs.Status == PromptStatus.OK)
{
if (jig.firstPoint)
enOrig.XLine1Point = jig._pnew;
else
enOrig.XLine2Point = jig._pnew;
}
}
en.Dispose();
doc.TransactionManager.FlushGraphics();
}
}
public class DJig : DrawJig
{
private Point3d _prevPoint;
private Point3d _currPoint;
private RotatedDimension _dimension;
public Point3d _pnew;
public bool firstPoint = false;
public PromptResult StartJig(RotatedDimension en)
{
_dimension = en;
_prevPoint = new Point3d(0, 0, 0);
_pnew = _dimension.XLine2Point;
return Application.DocumentManager.MdiActiveDocument.Editor.Drag(this);
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
var jppo = new JigPromptPointOptions("\nPoint: ")
{
UserInputControls = (UserInputControls.Accept3dCoordinates
| UserInputControls.NoZeroResponseAccepted
| UserInputControls.AcceptOtherInputString
| UserInputControls.NoNegativeResponseAccepted)
};
var rs = prompts.AcquirePoint(jppo);
_currPoint = rs.Value;
if (rs.Status != PromptStatus.OK) return SamplerStatus.Cancel;
if (!CursorHasMoved()) return SamplerStatus.NoChange;
_prevPoint = _currPoint;
return SamplerStatus.OK;
}
private bool CursorHasMoved()
{
return _currPoint.DistanceTo(_prevPoint) > 1e-6;
}
protected override bool WorldDraw(WorldDraw draw)
{
var dim = (RotatedDimension)_dimension;
_pnew = _currPoint;
Line3d ls = new Line3d(dim.XLine1Point, dim.XLine2Point);
try {
PointOnCurve3d pc = ls.GetClosestPointTo(_pnew);
_pnew = pc.Point;
}
catch {
}
finally {
ls.Dispose();
}
if (firstPoint)
dim.XLine1Point = _pnew;
else
dim.XLine2Point = _pnew;
draw.Geometry.Draw(dim);
return true;
}
}
}