此功能的介绍:cad里面自带有一个绘制矩形的命令,加入你希望绘制一个斜矩形(0,1; 2,3 ; 3,2,;0,1),你可以用下面的代码了
如果他可以让你选择第一点 1,第二点 2 ,第三点只需要在直线(注意是“直线”不是“线段”)34上即可;;当然还有改进的地方,第三次输入的时候其实还可以输入宽度更加合适;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System;
using JIG = Autodesk.AutoCAD.GraphicsInterface;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace aaaa
{
///
/// 根据随意三点绘制矩形(可能长度和宽度不平行于X轴和Y轴)的JIG
///
public class LeanRectByThreePoint : EntityJig
{
private Polyline polyline;
private List
private Point3d curPoint;
private Point3d pointThree = new Point3d();
private Point3d pointFour = new Point3d();
private int count;
public LeanRectByThreePoint(string layerName, int? ColorValue)
: base(new Polyline())
{
points = new List
points.Add(new Point3d());
points.Add(new Point3d());
points.Add(new Point3d());
points.Add(new Point3d());
Polyline pl = Entity as Polyline;
pl.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(0, 0), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(0, 0), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(0, 0), 0, 0, 0);
pl.Closed = true;
polyline = new Polyline();
polyline.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
polyline.AddVertexAt(1, new Point2d(0, 0), 0, 0, 0);
polyline.AddVertexAt(2, new Point2d(0, 0), 0, 0, 0);
polyline.AddVertexAt(3, new Point2d(0, 0), 0, 0, 0);
polyline.Closed = true;
if (CADDrawingMgrHelper.HasLayer(layerName))
pl.Layer = polyline.Layer = layerName;
if (ColorValue != null)
pl.Color = polyline.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.FromArgb((int)ColorValue));
count = 0;
}
protected override bool Update()
{
Polyline pl = (Polyline)Entity;
//更新jig多段线
for (int nIndex = 0; nIndex < points.Count; nIndex++)
{
polyline.SetPointAt(nIndex, new Point2d(points[nIndex].X, points[nIndex].Y));
pl.SetPointAt(nIndex, new Point2d(points[nIndex].X, points[nIndex].Y));
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions pntops = new JigPromptPointOptions();
pntops.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NoZeroResponseAccepted | UserInputControls.NoNegativeResponseAccepted);
pntops.UseBasePoint = false;
if (count == 0)
{
pntops.Message = "\n选择第一个点";
PromptPointResult pntres = prompts.AcquirePoint(pntops);
PromptStatus ss = pntres.Status;
if (pntres.Status == PromptStatus.OK)
{
curPoint = pntres.Value;
pntops.DefaultValue = curPoint;
polyline.SetPointAt(0, new Point2d(curPoint.X, curPoint.Y));
polyline.SetPointAt(1, new Point2d(curPoint.X, curPoint.Y));
polyline.SetPointAt(2, new Point2d(curPoint.X, curPoint.Y));
polyline.SetPointAt(3, new Point2d(curPoint.X, curPoint.Y));
points[0] = curPoint;
points[1] = curPoint;
points[2] = curPoint;
points[3] = curPoint;
return SamplerStatus.OK;
}
}
else if (count == 1)
{
pntops.Message = "\n选择第二个点";
PromptPointResult pntres = prompts.AcquirePoint(pntops);
if (pntres.Status == PromptStatus.OK)
{
curPoint = pntres.Value;
if (Math.Abs(polyline.GetPoint2dAt(0).X - curPoint.X) < 0.000001 && Math.Abs(polyline.GetPoint2dAt(0).Y - curPoint.Y) < 0.000001)
{
return SamplerStatus.OK;
}
polyline.SetPointAt(1, new Point2d(curPoint.X, curPoint.Y));
points[1] = curPoint;
points[2] = curPoint;
points[3] = curPoint;
return SamplerStatus.OK;
}
else if (pntres.Status == PromptStatus.Cancel)
{
return SamplerStatus.Cancel;
}
}
else if (count == 2)
{
pntops.Message = "\n选择第三个点";
PromptPointResult pntres = prompts.AcquirePoint(pntops);
if (pntres.Status == PromptStatus.OK)
{
curPoint = pntres.Value;
Line lin = new Line(polyline.GetPoint3dAt(0), polyline.GetPoint3dAt(1));
double dis = lin.GetClosestPointTo(curPoint, true).DistanceTo(curPoint);
if (dis < 0.00000001)
{
return SamplerStatus.OK;
}
double datX = curPoint.X - polyline.GetPoint3dAt(1).X;
double datY = curPoint.Y - polyline.GetPoint3dAt(1).Y;
Point3d pointLast = new Point3d(polyline.GetPoint3dAt(0).X + datX, polyline.GetPoint3dAt(0).Y + datY, 0);
Line NewLin = new Line(pointLast, curPoint);
if (NewLin.GetClosestPointTo(polyline.GetPoint3dAt(0), true) == NewLin.GetClosestPointTo(polyline.GetPoint3dAt(0), false))
{
pointThree = NewLin.GetClosestPointTo(polyline.GetPoint3dAt(0), true);
pointFour = new Point3d(polyline.GetPoint3dAt(1).X + (pointThree.X - polyline.GetPoint3dAt(0).X), polyline.GetPoint3dAt(1).Y + (pointThree.Y - polyline.GetPoint3dAt(0).Y),0);
if (pointThree.X - points[1].X != pointThree.X - points[0].X || pointThree.Y - points[1].Y != pointThree.Y - points[0].Y)
{
pointThree = pointFour;
pointFour = NewLin.GetClosestPointTo(polyline.GetPoint3dAt(0), true);
}
}
else
{
pointThree = NewLin.GetClosestPointTo(polyline.GetPoint3dAt(1), true);
pointFour = new Point3d(polyline.GetPoint3dAt(0).X + (pointThree.X - polyline.GetPoint3dAt(1).X), polyline.GetPoint3dAt(0).Y + (pointThree.Y - polyline.GetPoint3dAt(1).Y), 0);
}
polyline.SetPointAt(2, new Point2d(pointThree.X, pointThree.Y));
polyline.SetPointAt(3, new Point2d(pointFour.X, pointFour.Y));
points[2] = pointThree;
points[3] = pointFour;
return SamplerStatus.OK;
}
else if (pntres.Status == PromptStatus.Cancel)
{
return SamplerStatus.Cancel;
}
}
return SamplerStatus.Cancel;
}
public void setCounter(int i)
{
this.count = i;
}
public Polyline Rect { get { return this.polyline; } }
}
}