如何使用DrawJig绘制折线和多边形?
如何使用DrawJig绘制折线和多边形,给出代码,让参考下 请去参考ObjectARX SDK 里面的 \samples\dotNet\EllipseJig 工程 本帖最后由 雪山飞狐_lzh 于 2011-2-11 10:50 编辑using aApp = Autodesk.AutoCAD.ApplicationServices.Application;
using aPolyline = Autodesk.AutoCAD.DatabaseServices.Polyline;
public class sPolylineJig : EntityJig
{
/// <summary>
/// 模拟AutoCAD画多义线命令Polyline
/// </summary>
static public void DrawPolylineJig()
{
try
{
Editor ced = aApp.DocumentManager.MdiActiveDocument.Editor;
Database cdb = aApp.DocumentManager.MdiActiveDocument.Database;
Autodesk.AutoCAD.DatabaseServices.TransactionManager ctm = aApp.DocumentManager.MdiActiveDocument.Database.TransactionManager;
PromptPointOptions opts = new PromptPointOptions("\nStart Point :");
PromptPointResult res = ced.GetPoint(opts);
if (res.Status != PromptStatus.OK) return;
Point3dCollection p3ds = new Point3dCollection();
p3ds.Add(res.Value);
sPolylineJig jig = new sPolylineJig(p3ds);
sPolylineJig jig_bake = jig;
int pcount = p3ds.Count;
ced.Drag(jig);
while (jig.Status == 1)
{
jig_bake = jig;
p3ds = new Point3dCollection();
for (int i = 0; i <= pcount; i++)
{
p3ds.Add(((aPolyline)jig.Entity).GetPoint3dAt(i));
}
jig = new sPolylineJig(p3ds);
pcount += 1;
ced.Drag(jig);
}
if (jig.Status == 0)
return;
if (jig.Status == 2)
((aPolyline)jig_bake.Entity).Closed = true;
using (Transaction ctrans = ctm.StartTransaction())
{
BlockTable cbt = (BlockTable)ctm.GetObject(cdb.BlockTableId, OpenMode.ForRead, false);
BlockTableRecord cbtr = (BlockTableRecord)ctm.GetObject(cbt, OpenMode.ForWrite, false);
cbtr.AppendEntity(jig_bake.GetEntity());
ctm.AddNewlyCreatedDBObject(jig_bake.GetEntity(), true);
ctrans.Commit();
}
jig_bake.Entity.Dispose();
jig.Entity.Dispose();
p3ds.Dispose();
}
catch (System.Exception ex)
{
Debug.WriteLine("\n" + ex.Message);
}
}
static public aPolyline GetPolylineJig()
{
return sPolylineJig.GetPolylineJig(Point3d.Origin, 0);
}
static public aPolyline GetPolylineJig(Point3d basePoint, int index)
{
try
{
Editor ced = aApp.DocumentManager.MdiActiveDocument.Editor;
Point3dCollection p3ds = new Point3dCollection();
switch (index)
{
case 0:
PromptPointOptions opts = new PromptPointOptions("\nStart Point :");
PromptPointResult res = ced.GetPoint(opts);
p3ds.Add(res.Value);
break;
default:
p3ds.Add(basePoint);
break;
}
bool continuous = true;
sPolylineJig jig = new sPolylineJig(p3ds);
sPolylineJig jig_bake = jig;
int pcount = p3ds.Count;
while (continuous)
{
ced.Drag(jig);
if (jig.Status == 1)
{
jig_bake = jig;
p3ds = new Point3dCollection();
for (int i = 0; i <= pcount; i++)
{
p3ds.Add(((aPolyline)jig.Entity).GetPoint3dAt(i));
}
jig = new sPolylineJig(p3ds);
pcount += 1;
}
else
continuous = false;
}
if (jig.Status == 0)
return null;
if (jig.Status == 2)
((aPolyline)jig_bake.Entity).Closed = true;
jig.Entity.Dispose();
p3ds.Dispose();
return (aPolyline)jig_bake.Entity;
}
catch (System.Exception ex)
{
Debug.WriteLine("\n" + ex.Message);
return null;
}
}
Point3d _EndPoint, _StartPoint;
int _Status = 1;
public sPolylineJig(Point3dCollection p3ds)
: base(new aPolyline())
{
////注意: 在此不能用aPolyline作为基类,原因是aPolyline无法初始化,
////而对aPolyline的操作基本上都是要求实体已经实例化,在DataBase已经有记录
////但aPolyline的结果在外面表示就是LWPolyline
try
{
for (int i = 0; i < p3ds.Count; i++)
{
((aPolyline)Entity).AddVertexAt(i, new Point2d(p3ds.X, p3ds.Y), 0, 0, 0);
}
((aPolyline)Entity).AddVertexAt(p3ds.Count, new Point2d(p3ds.X, p3ds.Y), 0, 0, 0);
pcount = p3ds.Count;
_EndPoint = p3ds;
_StartPoint = _EndPoint;
}
catch (System.Exception ex)
{
Debug.WriteLine("\n" + ex.Message);
}
}
private int pcount = 0;
protected override bool Update()
{
try
{
((aPolyline)Entity).SetPointAt(pcount, new Point2d(_EndPoint.X, _EndPoint.Y));
}
catch (System.Exception ex)
{
Debug.WriteLine("\n" + ex.Message);
return false;
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
try
{
JigPromptPointOptions jigopts = new JigPromptPointOptions();
jigopts.UserInputControls = UserInputControls.NullResponseAccepted;
jigopts.BasePoint = _StartPoint;
jigopts.UseBasePoint = true;
jigopts.Keywords.Add("Close");
jigopts.Message = "\nNext Point :";
PromptPointResult dres = prompts.AcquirePoint(jigopts);
if (dres.Value != _EndPoint)
{
_EndPoint = dres.Value;
}
else
{
return SamplerStatus.NoChange;
}
if (dres.StringResult == "Close")
{
_Status = 2;
return SamplerStatus.Cancel;
}
if (dres.Status == PromptStatus.Cancel || dres.Status == PromptStatus.Error)
{
_Status = 0;
return SamplerStatus.Cancel;
}
else if (dres.Status == PromptStatus.None)
{
_Status = 3;
return SamplerStatus.Cancel;
}
else
{
return SamplerStatus.OK;
}
}
catch (System.Exception ex)
{
Debug.WriteLine("\n" + ex.Message);
return SamplerStatus.OK;
}
}
public int Status { get { return _Status; } }
public Entity GetEntity()
{
return Entity;
}
}
/// <summary>
/// 模拟AutoCAD画多义线命令Polyline
/// </summary>
static public void DrawPolylineJig()
{
sPolylineJig.DrawPolylineJig();
} 本帖最后由 sieben 于 2011-2-11 09:59 编辑
再说句多余的,我不认同你问问题的方式和学习的方法!
页:
[1]