[求助]如何模仿MLINE用Jig画两条平行线
<p>如何模仿MLINE用Jig画两条平行线。</p><p>对Jig非常感兴趣,但是实在模不到门路。</p><p>希望哪位高手能花点时间给个例子:</p><p>1. “指定起点”pt1 命令行显示“平行线间距:100”(默认间距为100)</p><p>2. 命令行出现 “指定下一点[平行线间距(S)/放弃(U)]<退出>:”</p><p>2.1 如果输入S则用getdistance 取得平行线的间距,输完后回到“指定下一点[平行线间距(S)/放弃(U)]<退出>:”</p><p>2.2 至于放弃(U)这个功能,参看Line命令。</p><p>2.3 如果指定了下一点。即返回为点值。即绘制出两条平行的LINE。</p><p>3. 如此循环,类似绘制LINE (Mline)</p><p></p> <p>类似mline,其对正类型为无(Z).</p><p>看KEAN例子,看晕了。</p><p>这个应该是动态生成再加上动态输入关键字</p><p>如果高手觉得太麻烦。</p><p>给个更简单的例子也行:</p><p>就是按一楼的那样子,不要生成平行线。</p><p></p><p>用JIG模仿LINE命令(会不会思路更清晰些)</p> 本帖最后由 作者 于 2009-6-25 21:41:16 编辑这几天有点忙:),天天开会,汗
写了个模拟Line的,写的有点乱:)
改写了下,这样可能要清晰一点
实际上Line应该可以不用Jig,这里只是实现一下Jig的流程
双线的Jig加一些计算,改动一下就可以了吧
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 TlsCad
{
class MyLineJig : DrawJig
{
List<Point3d> m_Points = new List<Point3d>();
List<ObjectId> m_Lines = new List<ObjectId>();
Point3d m_EndPoint;
LineJigStatus m_Status = LineJigStatus.Add;
public enum LineJigStatus
{
Add,
Undo,
Close,
Finish
}
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.Message = "\n指定第一点:";
break;
case 1:
case 2:
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指定下一点或 [闭合(C)/放弃(U)]:", "Close Undo");
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;
}
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 static void DoIt()
{
MyLineJig ljig = new MyLineJig();
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
while (ljig.JigStatus != LineJigStatus.Finish)
{
PromptResult res = ed.Drag(ljig);
Line line;
switch (ljig.JigStatus)
{
case LineJigStatus.Add:
case LineJigStatus.Close:
if (ljig.AddPoint() != 1)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
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();
}
}
break;
case LineJigStatus.Undo:
ljig.RemovePoint();
if (ljig.CurrLine != ObjectId.Null)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
line = (Line)ljig.CurrLine.GetObject(OpenMode.ForWrite);
line.Erase(true);
ljig.RemveLine();
tr.Commit();
}
}
break;
}
if (ljig.JigStatus == LineJigStatus.Close)
ljig.JigStatus = LineJigStatus.Finish;
}
}
}
}
<p>太感谢版主了。。。</p><p>因为有这么热心的版主。。。</p><p>我相信这个版块一定会越办越好。</p><p>我先好好学习一下。</p> 试着改写成PolyLine的,:)
没有考虑Ucs的
class PolyLineJig : DrawJig
{
ObjectId m_PolyLine = ObjectId.Null;
Point3d m_FirstPoint;
Polyline m_CurrPolyLine;
Point3d m_StartPoint = Point3d.Origin;
Vector3d m_StartVector = Vector3d.XAxis;
Point3d m_EndPoint;
bool m_IsLine = true;
int m_Count = -1;
PolyLineJigStatus m_Status = PolyLineJigStatus.Add;
public enum PolyLineJigStatus
{
Add,
Keyword,
Undo,
Finish,
Close,
}
public PolyLineJig()
{
m_CurrPolyLine = new Polyline();
m_CurrPolyLine.AddVertexAt(0, m_StartPoint.Convert2d(new Plane()), 0, 0, 0);
m_CurrPolyLine.AddVertexAt(0, m_StartPoint.Convert2d(new Plane()), 0, 0, 0);
}
public PolyLineJigStatus JigStatus
{
get { return m_Status; }
set { m_Status = value; }
}
public Point3d StartPoint
{
get { return m_StartPoint; }
}
public Point3d EndPoint
{
get { return m_EndPoint; }
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
if (m_Count > -1)
{
double bugle = GetBugle();
if (bugle != 0)
{
m_CurrPolyLine.SetBulgeAt(0, GetBugle());
m_CurrPolyLine.SetPointAt(1, m_EndPoint.Convert2d(new Plane()));
return draw.Geometry.Draw(m_CurrPolyLine);
}
}
return true;
}
private double GetBugle()
{
if (m_IsLine)
{
return 0;
}
else
{
Plane plane = new Plane();
double ang = (m_EndPoint - m_StartPoint).AngleOnPlane(plane);
ang -= m_StartVector.AngleOnPlane(plane);
return Math.Tan(ang * 0.5);
}
}
public Polyline Update()
{
Polyline pl;
switch (m_Status)
{
case PolyLineJigStatus.Add:
case PolyLineJigStatus.Close:
if (m_Count > -1)
{
pl = (Polyline)m_PolyLine.GetObject(OpenMode.ForWrite);
pl.SetBulgeAt(m_Count, GetBugle());
}
else
{
pl = new Polyline();
m_FirstPoint = m_EndPoint;
}
m_Count++;
pl.AddVertexAt(m_Count, m_EndPoint.Convert2d(new Plane()), GetBugle(), 0, 0);
m_StartPoint = m_EndPoint;
m_CurrPolyLine.SetPointAt(0, m_StartPoint.Convert2d(new Plane()));
try
{
m_StartVector = pl.GetFirstDerivative(pl.EndParam);
}
catch
{
m_StartVector = Vector3d.XAxis;
}
return pl;
case PolyLineJigStatus.Undo:
pl = (Polyline)m_PolyLine.GetObject(OpenMode.ForWrite);
if (m_Count == 0)
{
pl.Erase(true);
m_PolyLine = ObjectId.Null;
}
else
{
pl.RemoveVertexAt(m_Count);
m_StartPoint = pl.EndPoint;
m_CurrPolyLine.SetPointAt(0, m_StartPoint.Convert2d(new Plane()));
}
m_Count--;
if (m_Count > -1)
{
m_StartPoint = pl.GetPoint3dAt(m_Count);
}
try
{
m_StartVector = pl.GetFirstDerivative(pl.EndParam);
}
catch
{
m_StartVector = Vector3d.XAxis;
}
break;
}
return null;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions jigOpts = new JigPromptPointOptions();
jigOpts.UserInputControls =
UserInputControls.Accept3dCoordinates |
UserInputControls.NoZeroResponseAccepted |
UserInputControls.NoNegativeResponseAccepted |
UserInputControls.NullResponseAccepted;
switch (m_Count)
{
case -1:
jigOpts.Message = "\n指定第一点:";
break;
case 0:
case 1:
jigOpts.Cursor = CursorType.RubberBand;
jigOpts.Message = "\n指定下一个点:";
if (m_IsLine)
{
jigOpts.Keywords.Add("Arc", "Arc", "圆弧(A)");
}
else
{
jigOpts.Keywords.Add("Line", "Line", "直线(L)");
}
jigOpts.Keywords.Add("Undo", "Undo", "放弃(U)");
jigOpts.UseBasePoint = true;
jigOpts.BasePoint = m_StartPoint;
break;
default:
jigOpts.Cursor = CursorType.RubberBand;
jigOpts.Message = "\n指定下一个点:";
if (m_IsLine)
{
jigOpts.Keywords.Add("Arc", "Arc", "圆弧(A)");
}
else
{
jigOpts.Keywords.Add("Line", "Line", "直线(L)");
}
jigOpts.Keywords.Add("Close", "Close", "闭合(C)");
jigOpts.Keywords.Add("Undo", "Undo", "放弃(U)");
jigOpts.UseBasePoint = true;
jigOpts.BasePoint = m_StartPoint;
break;
}
PromptPointResult res = prompts.AcquirePoint(jigOpts);
m_Status = PolyLineJigStatus.Add;
switch (res.Status)
{
case PromptStatus.Cancel:
case PromptStatus.None:
m_Status = PolyLineJigStatus.Finish;
return SamplerStatus.Cancel;
case PromptStatus.Keyword:
switch (res.StringResult)
{
case "Arc":
case "Line":
m_IsLine = !m_IsLine;
m_Status = PolyLineJigStatus.Keyword;
return SamplerStatus.OK;
case "Add":
m_Status = PolyLineJigStatus.Add;
return SamplerStatus.OK;
case "Undo":
m_Status = PolyLineJigStatus.Undo;
return SamplerStatus.OK;
case "Close":
m_EndPoint = m_FirstPoint;
m_Status = PolyLineJigStatus.Close;
return SamplerStatus.Cancel;
}
break;
default:
Point3d positionTemp = res.Value;
if (positionTemp != m_EndPoint)
{
m_EndPoint = positionTemp;
}
else
{
return SamplerStatus.NoChange;
}
break;
}
return SamplerStatus.OK;
}
public static void DoIt()
{
PolyLineJig ljig = new PolyLineJig();
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
while (ljig.JigStatus != PolyLineJigStatus.Finish)
{
ed.Drag(ljig);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Polyline pl = ljig.Update();
if (ljig.m_PolyLine == ObjectId.Null && pl != null)
{
BlockTableRecord btr = db.CurrentSpaceId.GetObject(OpenMode.ForWrite) as BlockTableRecord;
ljig.m_PolyLine = btr.AppendEntity(pl);
tr.AddNewlyCreatedDBObject(pl, true);
}
tr.Commit();
}
if (ljig.JigStatus == PolyLineJigStatus.Close)
ljig.JigStatus = PolyLineJigStatus.Finish;
}
}
}
怎么看不到 学习了...... 本帖最后由 shirazbj 于 2012-2-22 17:10 编辑
学习了。就是用plj时,每次只是显示一条线,前一步的线要是也能显示出来,就和本身的pl命令一样了。那就太棒了。
lj点击输入下一点后,线怎么不画出来?
第一次来这里,还不知道程序怎么用,保存的后缀是什么?命令名是什么?还望多多指教 2.3 如果指定了下一点。即返回为点值。即绘制出两条平行的LINE。
页:
[1]
2