using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using GI = Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.EditorInput;
using System.Collections.Generic;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(TestDrawJig.MyCommands))]
namespace TestDrawJig
{
public class MyCommands
{
[CommandMethod("Test01")]
public static void Test01()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (OpenCloseTransaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
BlockTableRecord ms = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
List<Entity> ents = new List<Entity>();
Point3d p = Point3d.Origin;
int num = 1;
for (int j = 0; j < 20; j++)
{
for (int i = 0; i < 20; i++)
{
DBText txt = new DBText();
txt.TextString = num.ToString();
txt.Position = new Point3d(p.X + 6 * i, p.Y - 6 * j, 0);
ents.Add(txt);
num++;
}
}
TestJigClass jig = new TestJigClass(ents);
PromptResult res = ed.Drag(jig);
if (res.Status == PromptStatus.OK)
{
BlockTableRecord curSpace = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
foreach (Entity ent in ents)
{
ent.TransformBy(Matrix3d.Displacement(jig.offset));
curSpace.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
}
}
trans.Commit();
}
}
}
public class TestJigClass : DrawJig
{
private readonly List<Entity> ents = new List<Entity>();
private Point3d _position;
internal Vector3d offset;
public TestJigClass(List<Entity> _ents)
{
ents = _ents;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.PointMonitor += new PointMonitorEventHandler(ed_PointMonitor);
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions opt_point = new JigPromptPointOptions("New point: ");
PromptPointResult res_point = prompts.AcquirePoint(opt_point);
if (res_point.Status == PromptStatus.OK)
{
offset = Point3d.Origin.GetVectorTo(_position);
}
return SamplerStatus.OK;
}
protected override bool WorldDraw(GI.WorldDraw draw)
{
if (draw.Geometry != null)
{
Matrix3d mat = Matrix3d.Displacement(offset);
draw.Geometry.PushModelTransform(mat);
foreach (Entity ent in ents)
{
if (!draw.Geometry.Draw(ent)) break;
}
draw.Geometry.PopModelTransform();
}
return true;
}
void ed_PointMonitor(object sender, PointMonitorEventArgs e)
{
_position = e.Context.ComputedPoint;
}
}
}