[即时预览] 直线即时预览
一般情况下,在提示用户输入一些信息绘图的过程中,是没有当前形状的预览的,这样不够直观,例如:提示用户输入两点创建一条直线,用户必须输入好两个点以后才能生成直线,如何让用户在选择点的过程中预览到他正在绘制的图形呢?用Jig可以解决这个问题。 本例通过Jig的方式绘制一条直线,在选取过程中你可以预览到直线的形状,以便判断是不是你所需要的效果。using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
namespace Sample
{
class lineJig
{
public void Jig()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
LineJig lineJig = new LineJig();
PromptResult res = ed.Drag(lineJig);
if (res.Status == PromptStatus.OK)
{
lineJig.SetCounter(1);
res = ed.Drag(lineJig);
ToModelSpace(lineJig.Entity);
}
}
/// <summary>
/// 添加对象到模型空间
/// </summary>
/// <param name="ent">要添加的对象</param>
/// <returns></returns>
public static ObjectId ToModelSpace(Entity ent)
{
Database db = HostApplicationServices.WorkingDatabase;
ObjectId entId;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
}
return entId;
}
}
public class LineJig : EntityJig
{
Line myline;
Point3d startpoint;
Point3d endpoint;
int count;
public LineJig()
: base(new Line())
{
myline = new Line();
count = 0;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions pntops = new JigPromptPointOptions();
pntops.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NoZeroResponseAccepted | UserInputControls.NoNegativeResponseAccepted);
pntops.UseBasePoint = false;
pntops.DefaultValue = new Point3d();
if (count == 0)
{
pntops.Message = "\n选择起点";
PromptPointResult pntres = prompts.AcquirePoint(pntops);
PromptStatus ss = pntres.Status;
if (pntres.Status == PromptStatus.OK)
{
startpoint = pntres.Value;
endpoint = pntres.Value;
return SamplerStatus.OK;
}
}
if (count == 1)
{
pntops.Message = "\n选择终点";
PromptPointResult pntres = prompts.AcquirePoint(pntops);
if (pntres.Status == PromptStatus.OK)
{
endpoint = pntres.Value;
return SamplerStatus.OK;
}
}
return SamplerStatus.Cancel;
}
protected override bool Update()
{
((Line)Entity).StartPoint = startpoint;
((Line)Entity).EndPoint = endpoint;
return true;
}
public void SetCounter(int i)
{
this.count = i;
}
public Entity Entity
{
get { return base.Entity; }
}
}
}
页:
[1]