|
发表于 2020-3-20 23:28:00
|
显示全部楼层
请参考gile的程序片段:http://www.theswamp.org/index.php?topic=46284.msg513392#msg513392
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass(typeof(RectangleDrawJig.CommandMethods))]
namespace RectangleDrawJig
{
public class CommandMethods
{
[CommandMethod("Test", CommandFlags.Modal)]
public void Test()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptPointResult ppr = ed.GetPoint("\nSpecify the first corner: ");
if (ppr.Status != PromptStatus.OK) return;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
Point3d basePt = ppr.Value;
using (Transaction tr = db.TransactionManager.StartTransaction())
using(Line line = new Line(basePt, basePt))
using(Polyline pline = new Polyline(4))
{
Point2d basePt2d = new Point2d(basePt.X, basePt.Y);
pline.AddVertexAt(0, basePt2d, 0.0, 0.0, 0.0);
pline.AddVertexAt(1, basePt2d, 0.0, 0.0, 0.0);
pline.AddVertexAt(2, basePt2d, 0.0, 0.0, 0.0);
pline.AddVertexAt(3, basePt2d, 0.0, 0.0, 0.0);
pline.Closed = true;
RectangleJig jig = new RectangleJig(pline, line, ucs);
PromptResult pr = ed.Drag(jig);
if (pr.Status == PromptStatus.OK)
{
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
pline.TransformBy(ucs);
btr.AppendEntity(pline);
tr.AddNewlyCreatedDBObject(pline, true);
line.TransformBy(ucs);
btr.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);
}
tr.Commit();
}
}
class RectangleJig : DrawJig
{
private Polyline pline;
private Line line;
private Matrix3d ucs2wcs, wcs2ucs;
private Point3d dragPt;
private Point2d basePt;
public RectangleJig(Polyline pline, Line line, Matrix3d ucs)
{
this.pline = pline;
this.line = line;
this.ucs2wcs = ucs;
this.wcs2ucs = ucs.Inverse();
this.dragPt = line.EndPoint;
this.basePt = pline.GetPoint2dAt(0);
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
Autodesk.AutoCAD.GraphicsInterface.WorldGeometry geo = draw.Geometry;
if (geo != null)
{
geo.PushModelTransform(ucs2wcs);
geo.Draw(line);
geo.Draw(pline);
geo.PopModelTransform();
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
PromptPointResult ppr = prompts.AcquirePoint("\nSpecify the opposite corner: ");
if (ppr.Value.IsEqualTo(dragPt))
return SamplerStatus.NoChange;
dragPt = ppr.Value;
Update();
return SamplerStatus.OK;
}
private void Update()
{
Point3d pt = dragPt.TransformBy(wcs2ucs);
line.EndPoint = pt;
pline.SetPointAt(1, new Point2d(pt.X, basePt.Y));
pline.SetPointAt(2, new Point2d(pt.X, pt.Y));
pline.SetPointAt(3, new Point2d(basePt.X, pt.Y));
}
}
}
}
|
|