学习飞狐大哥模拟Line方法,添加“反接”和“圆弧”两个功能
using System;using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
namespace EventHandler
{
public class CLineJig : DrawJig
{
CBasicMethod myMethod = new CBasicMethod();
List<Point3d> m_Points = new List<Point3d>();
List<ObjectId> m_Lines = new List<ObjectId>();
List<Point3d> m_ArcPts = new List<Point3d>();
Point3d m_EndPoint;
int tt = 0;
Point3d[] pt_Reverse = new Point3d; // 控制点的反接,限10次以内
LineJigStatus m_Status = LineJigStatus.Add;
private bool m_ArcStart = false;
private string lineJudge = null;
public enum LineJigStatus
{
Add,
Undo,
Close,
Finish,
Reverse
}
public LineJigStatus JigStatus
{
get { return m_Status; }
set { m_Status = value; }
}
public Point3d StartPoint
{
get { return m_Points; }
}
public Point3d EndPoint
{
get { return m_Points; }
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions jigOpts = new JigPromptPointOptions();
jigOpts.UserInputControls =
UserInputControls.Accept3dCoordinates |
UserInputControls.NoZeroResponseAccepted |
UserInputControls.NoNegativeResponseAccepted |
UserInputControls.NullResponseAccepted;
switch (m_Points.Count)
{
case 0:
jigOpts.UseBasePoint = false;
jigOpts.SetMessageAndKeywords("\n指定第一点或[标准化(S)]:", "Standard");
break;
case 1:
jigOpts.Cursor = CursorType.RubberBand;
jigOpts.SetMessageAndKeywords("\n指定下一点或 [放弃(U)]:", "Undo");
jigOpts.UseBasePoint = true;
jigOpts.BasePoint = m_Points;
break;
default:
jigOpts.Cursor = CursorType.RubberBand;
jigOpts.SetMessageAndKeywords("\n指定下一点或 [回退(U)/反接(R)/闭合(C)/弧(A)]:", "Undo Reverse Close Arc");
jigOpts.UseBasePoint = true;
jigOpts.BasePoint = m_Points;
break;
}
PromptPointResult res = prompts.AcquirePoint(jigOpts);
m_Status = LineJigStatus.Add;
switch (res.Status)
{
case PromptStatus.Cancel:
case PromptStatus.None:
m_Status = LineJigStatus.Finish;
return SamplerStatus.Cancel;
case PromptStatus.Keyword:
switch (res.StringResult)
{
case "Undo":
m_Status = LineJigStatus.Undo;
break;
case "Close":
m_EndPoint = m_Points;
m_Status = LineJigStatus.Close;
break;
case "Reverse":
if (tt == 0)
{
pt_Reverse = m_Points;
m_Points = m_Points;
m_EndPoint = m_Points;
tt++;
}
else
{
pt_Reverse = m_Points;
m_Points = pt_Reverse;
m_EndPoint = pt_Reverse;
tt++;
}
m_Status = LineJigStatus.Add;
break;
case "Arc":
m_Status = LineJigStatus.Add;
m_ArcPts.Insert(0, m_Points);
m_ArcStart = true;
break;
case "Standard":
break;
}
return SamplerStatus.OK;
default:
Point3d positionTemp = res.Value;
if (positionTemp != m_EndPoint)
{
m_EndPoint = positionTemp;
}
else
{
return SamplerStatus.NoChange;
}
break;
}
return SamplerStatus.OK;
}
public int AddPoint()
{
m_Points.Insert(0, m_EndPoint);
return m_Points.Count;
}
public void AddLine(Line line)
{
m_Lines.Insert(0, line.ObjectId);
}
public ObjectId CurrLine
{
get { return m_Lines.Count == 0 ? ObjectId.Null : m_Lines; }
}
public int RemovePoint()
{
m_Points.RemoveAt(0);
return m_Points.Count;
}
public void RemveLine()
{
m_Lines.RemoveAt(0);
}
public void DrawLineMethod()
{
Database db = CopyDatabase.destDb;
CLineJig ljig = new CLineJig();
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ObjectId linetypeID = ObjectId.Null;
Line line = null;
// 开始交互绘制
while (ljig.JigStatus != LineJigStatus.Finish)
{
// 圆弧绘制的启动开关
if (ljig.m_ArcStart == false)
{
PromptResult res = ed.Drag(ljig);
}
else
{
PromptPointOptions opt = new PromptPointOptions("\n指定一个弧上的点: ");
PromptPointResult prt;
prt = ed.GetPoint(opt);
if (prt.Status == PromptStatus.OK)
{
ljig.m_ArcPts.Insert(0, prt.Value);
PromptPointOptions opt1 = new PromptPointOptions("\n指定弧段的终点:");
PromptPointResult prt1;
prt1 = ed.GetPoint(opt1);
if (prt1.Status == PromptStatus.OK)
{
ljig.m_ArcPts.Insert(0, prt1.Value);
ljig.m_EndPoint = prt1.Value;
ljig.AddPoint();
}
}
}
switch (ljig.JigStatus)
{
case LineJigStatus.Add:
case LineJigStatus.Close:
if (ljig.AddPoint() != 1 && ljig.m_ArcStart == false)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// 注意,当要修改一个不是当前文档的数据库时,或者要阻止别的执行文本来修改该数据库时,就需要锁定文档。
DocumentLock documentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
line = new Line(ljig.StartPoint, ljig.EndPoint);
line.SetDatabaseDefaults();
btr.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);
ljig.AddLine(line);
tr.Commit();
documentLock.Dispose();
}
}
else
{
if (ljig.m_ArcPts.Count == 3)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DocumentLock documentLock = Application.DocumentManager.MdiActiveDocument.LockDocument();
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
Arc myArc = myMethod.AddArcFromPt(ljig.m_ArcPts, ljig.m_ArcPts, ljig.m_ArcPts);
btr.AppendEntity(myArc);
tr.AddNewlyCreatedDBObject(myArc, true);
tr.Commit();
documentLock.Dispose();
ljig.m_ArcStart = false;
ljig.m_ArcPts.Clear();
}
}
}
break;
case LineJigStatus.Undo:
ljig.RemovePoint();
if (ljig.CurrLine != ObjectId.Null)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DocumentLock documentLock = Application.DocumentManager.MdiActiveDocument.LockDocument();
line = (Line)ljig.CurrLine.GetObject(OpenMode.ForWrite);
line.Erase(true);
ljig.RemveLine();
tr.Commit();
documentLock.Dispose();
}
}
break;
default:
break;
}
if (ljig.JigStatus == LineJigStatus.Close)
{
ljig.JigStatus = LineJigStatus.Finish;
}
}
}
}
}
说明:其中绘制圆弧方法见上一贴“根据三点画圆弧”。
http://space.mjtd.com/xwb/images/bgimg/icon_logo.png 该贴已经同步到 菜鸟Liu的微博 开来懂LISP就是好啊
页:
[1]