值中秋之际,和小伙伴出去后,借点时间看一下关于多实体实时拖拽效果(jig),在AutoCAD中,通过鼠标的移动,动态的展示效果会给绘图者直观的感觉。在AutoCAD中关于拖拽有两个实现类,第一是EntityJig,另外一个是DrawJig类。其中前者只能针对一个实体,而另外一个是针对多个实体,可以实现拖拽效果。而对于前者者继承类中需要实现Sampler和Updata两个函数,Sampler用于数据的交互,比如提示用户输入一定的数据,Updata是用于接收数据后在图形界面实时更新。同EntityJig类,DrawJig也需要重写Samper函数,另外该类中含有WorldDraw函数需要重写,用于实时展示动态图形效果。
下面的这个测试demo是在图形界面拾取多个实体,然后再指定另外一个移动点,在Samper函数中提示用户拾取,将拾取的多个实体在WorldDraw中实时展示出来。整个工程的源代码如下所示。其中我们制作一个效果视屏。
这是jig实现类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
namespace MoveMultipleEntityJig
{
public class JigMoveMultipleEntity : DrawJig
{
private Point3d mBase;
private Point3d mLocation;
List<Entity> mEntities;
public JigMoveMultipleEntity(Point3d basePt)
{
mBase = basePt.TransformBy(UCS);
mEntities = new List<Entity>();
}
public Point3d Base
{
get { return mLocation; }
set { mLocation = value; }
}
public Point3d Location
{
get { return mLocation; }
set { mLocation = value; }
}
public Matrix3d UCS
{
get
{
return Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
}
}
public void AddEntity(Entity ent)
{
mEntities.Add(ent);
}
public void TransformEntities()
{
Matrix3d mat = Matrix3d.Displacement(mBase.GetVectorTo(mLocation));
foreach (Entity ent in mEntities)
{
ent.TransformBy(mat);
}
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\n新的位置:");
prOptions1.UseBasePoint = false;
PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
if (prResult1.Status == PromptStatus.Cancel || prResult1.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (!mLocation.IsEqualTo(prResult1.Value, new Tolerance(10e-10, 10e-10)))
{
mLocation = prResult1.Value;
return SamplerStatus.OK;
}
else
return SamplerStatus.NoChange;
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
Matrix3d mat = Matrix3d.Displacement(mBase.GetVectorTo(mLocation));
WorldGeometry geo = draw.Geometry;
if (geo != null)
{
geo.PushModelTransform(mat);
foreach (Entity ent in mEntities)
{
geo.Draw(ent);
}
geo.PopModelTransform();
}
return true;
}
}
}
这是使用的代码;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
namespace MoveMultipleEntityJig
{
public class Class1
{
public static JigMoveMultipleEntity jigger;
[CommandMethod("tsjig")]
public static void demo(){
try
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionResult selRes = ed.GetSelection();
if (selRes.Status != PromptStatus.OK) return;
PromptPointOptions prOpt = new PromptPointOptions("\n选择基点:");
PromptPointResult pr = ed.GetPoint(prOpt);
if (pr.Status != PromptStatus.OK) return;
jigger = new JigMoveMultipleEntity(pr.Value);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in selRes.Value.GetObjectIds())
{
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
jigger.AddEntity(ent);
}
PromptResult jigRes = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.Drag(jigger);
if (jigRes.Status == PromptStatus.OK)
{
jigger.TransformEntities();
tr.Commit();
}
else
tr.Abort();
}
}
catch (System.Exception ex)
{
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}
}
}
}
更多内容,请关注公众号