using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
#pragma warning disable 0618
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(TestDrawJigWithDynDim.MyCommands))]
namespace TestDrawJigWithDynDim
{
public class MyCommands
{
[CommandMethod("TestDrawJig")]
public void MyCommand() // This method can have any name
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
if (doc == null) return;
Editor ed = doc.Editor;
Matrix3d mat = ed.CurrentUserCoordinateSystem;
PromptPointResult res = ed.GetPoint("\nУкажите базовую точку: ");
if (res.Status == PromptStatus.OK)
{
Point3d basePt = res.Value.TransformBy(mat);
DrawJigWithDynDim jig = new DrawJigWithDynDim(basePt, 500, 500);
if (jig.DragMe() == PromptStatus.OK)
{
Point3dCollection pts = new Point3dCollection();
pts.Add(basePt);
pts.Add(basePt + new Vector3d(0, jig.height, 0));
pts.Add(basePt + new Vector3d(jig.width, jig.height, 0));
pts.Add(basePt + new Vector3d(jig.width, 0, 0));
using (Polyline3d poly = new Polyline3d(Poly3dType.SimplePoly, pts, true))
{
using (BlockTableRecord btr = db.CurrentSpaceId.Open(OpenMode.ForWrite) as BlockTableRecord)
{
btr.AppendEntity(poly);
}
}
}
}
}
}
public class DrawJigWithDynDim: DrawJig
{
private DynamicDimensionDataCollection dimCol = new DynamicDimensionDataCollection();
private Point3d basePt, prevPt;
public double width = 100, height = 100;
private double minWidth = 100, minHeight = 100;
private bool bFixWidth = false;
private bool bFixHeight = false;
public DrawJigWithDynDim(Point3d pt, double w, double h)
{
basePt = prevPt = pt; width = w; height = h;
}
public PromptStatus DragMe()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = Application.DocumentManager.MdiActiveDocument.Database;
Point3d p1 = basePt;
Point3d p2 = basePt + new Vector3d(width, 0, 0);
Point3d p3 = basePt + new Vector3d(0, height, 0);
AlignedDimension dim1 = new AlignedDimension();
dim1.DynamicDimension = true;
AlignedDimension dim2 = new AlignedDimension();
dim2.DynamicDimension = true;
DynamicDimensionData ddim1 = new DynamicDimensionData(dim1, true, false);
ddim1.Editable = true; ddim1.Focal = true;
dimCol.Add(ddim1);
DynamicDimensionData ddim2 = new DynamicDimensionData(dim2, true, false);
ddim2.Editable = true; ddim2.Focal = true;
dimCol.Add(ddim2);
UpdateDimensions();
PromptResult res;
while ((res = ed.Drag(this)).Status == PromptStatus.Other);
return (res.Status == PromptStatus.None) ? PromptStatus.OK : res.Status;
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
Point3dCollection pts = new Point3dCollection();
pts.Add(basePt);
pts.Add(basePt + new Vector3d(0, height, 0));
pts.Add(basePt + new Vector3d(width, height, 0));
pts.Add(basePt + new Vector3d(width, 0, 0));
pts.Add(basePt);
return draw.Geometry.Polygon(pts);
}
protected override DynamicDimensionDataCollection GetDynamicDimensionData(double dimScale)
{
return dimCol;
}
protected override void OnDimensionValueChanged(DynamicDimensionChangedEventArgs e)
{
switch (e.Index)
{
case 0:
if (e.Value.ToString() != "0")
{
width = e.Value * Math.Sign(prevPt.X - basePt.X);
bFixWidth = true;
}
else
{
bFixWidth = false;
}
break;
case 1:
if (e.Value.ToString() != "0")
{
height = e.Value * Math.Sign(prevPt.Y - basePt.Y);
bFixHeight = true;
}
else
{
bFixHeight = false;
}
break;
}
UpdateDimensions();
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions jigOpts = new JigPromptPointOptions();
jigOpts.UserInputControls =
( UserInputControls.Accept3dCoordinates | UserInputControls.NullResponseAccepted);
jigOpts.BasePoint = basePt;
jigOpts.UseBasePoint = true;
jigOpts.Cursor = CursorType.RubberBand;
jigOpts.Message = "\nУкажите диагональную точку: ";
PromptPointResult dres = prompts.AcquirePoint(jigOpts);
if (dres.Status != PromptStatus.OK && dres.Status != PromptStatus.Other)
return SamplerStatus.Cancel;
if (dres.Status == PromptStatus.OK)
{
Point3d curPt = dres.Value;
if (curPt.DistanceTo(prevPt) < 1e-3)
return SamplerStatus.NoChange;
prevPt = curPt;
if (!bFixWidth)
width = Math.Max(Math.Abs(curPt.X - basePt.X), minWidth) * Math.Sign(curPt.X - basePt.X);
if (!bFixHeight)
height = Math.Max(Math.Abs(curPt.Y - basePt.Y), minHeight) * Math.Sign(curPt.Y - basePt.Y);
}
UpdateDimensions();
return SamplerStatus.OK;
}
protected void UpdateDimensions()
{
Point3d p1 = basePt;
Point3d p2 = basePt + new Vector3d(width, 0, 0);
Point3d p3 = basePt + new Vector3d(0, height, 0);
Point3d p4 = basePt + new Vector3d(width, height, 0);
AlignedDimension dim;
dim = dimCol[0].Dimension as AlignedDimension;
dim.XLine1Point = p1;
dim.XLine2Point = p2;
dim.DimLinePoint = p1 + (p2 - p1) * 0.5 -
new Vector3d(0, minHeight, 0) * Math.Sign(p4.Y - p1.Y);
dim = dimCol[1].Dimension as AlignedDimension;
dim.XLine1Point = p1;
dim.XLine2Point = p3;
dim.DimLinePoint = p1 + (p3 - p1) * 0.5 -
new Vector3d(minWidth, 0, 0) * Math.Sign(p4.X - p1.X);
}
}
}