拖出来咋这个效果呢?
using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System.Collections.Generic;
namespace CAD0901
{
class JigClass : DrawJig
{
private Polyline pl=new Polyline();
private Point3d p1;
private Point3d p2;
public void MyTest()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
try
{
Matrix3d mx = ed.CurrentUserCoordinateSystem;
PromptPointOptions ppo = new PromptPointOptions("\n第一点");
PromptPointResult ppr = ed.GetPoint(ppo);
p1 = ppr.Value;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject
(bt, OpenMode.ForWrite);
PromptResult resJig = ed.Drag(this);
if (resJig.Status == PromptStatus.OK)
{
btr.AppendEntity(pl);
trans.AddNewlyCreatedDBObject(pl, true);
trans.Commit();
}
}
}
catch (Exception ex)
{
}
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
draw.Geometry.Draw(pl);
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions jpo = new JigPromptPointOptions("\n另一点");
jpo.UserInputControls = UserInputControls.Accept3dCoordinates;
PromptPointResult pr = prompts.AcquirePoint(jpo);
Point3d tempt = pr.Value;
if (pr.Status==PromptStatus.Cancel)
{
return SamplerStatus.Cancel;
}
if (p2!= tempt)
{
p2 = tempt;
pl.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(p2.X, p1.Y), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(p2.X, p2.Y), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(p1.X, p2.Y), 0, 0, 0);
pl.Closed = true;
return SamplerStatus.OK;
}
else return SamplerStatus.NoChange;
}
private ObjectId AppendEntity(Entity ent)
{
ObjectId entId;
Database db = HostApplicationServices.WorkingDatabase;
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;
}
}
}上面这段程序,拖出一个矩形框,结果悲剧了……
我该怎么改呢? 快来看看呀…… protected override SamplerStatus Sampler(JigPrompts prompts)
{
//////////////////////////////////////////////
这里加一次点就够了
if (p2!= tempt)
{
p2 = tempt;
pl.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(p2.X, p1.Y), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(p2.X, p2.Y), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(p1.X, p2.Y), 0, 0, 0);
pl.Closed = true;
return SamplerStatus.OK;
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
}
加一次后是修改点的位置,而不是再加新点 sieben 发表于 2011-9-2 13:34 static/image/common/back.gif
protected override SamplerStatus Sampler(JigPrompts prompts)
{
/////////////////// ...
你的意思是:这里只保留 p2 = tempt;
下面添加多段线顶点的代码提到上面去?
好像也不行呀! 拜托!你想画一个矩形对吧?矩形一共只有4个点对吧?
pl.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(p2.X, p1.Y), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(p2.X, p2.Y), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(p1.X, p2.Y), 0, 0, 0);
pl.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0, 0, 0); 是往多段线加点对吧?你不停的加,那这个多段线会是几个节点? sieben 发表于 2011-9-2 14:19 static/image/common/back.gif
拜托!你想画一个矩形对吧?矩形一共只有4个点对吧?
pl.AddVertexAt(0, new Point2d(p1.X, p1.Y), ...
,我对JIG是如何作用的一直没搞明白,您的意思是说不需要if语句吗?
我实在是不太懂这个,还望大哥悉心指导一下…… 建议你仔细看看你目前出来的多段线,仔细看,放大来看.或许这样你能明白你的问题所在;
sieben 发表于 2011-9-2 14:44 static/image/common/back.gif
建议你仔细看看你目前出来的多段线,仔细看,放大来看.或许这样你能明白你的问题所在;
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
pl = new Polyline();
pl.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(p2.X, p1.Y), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(p2.X, p2.Y), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(p1.X, p2.Y), 0, 0, 0);
pl.Closed = true;
draw.Geometry.Draw(pl);
return true;
}siben兄,
终于发现问题所在了,把添加顶点的代码添加到WorldDraw方法中就OK了,谢谢你的指点哈 本帖最后由 sieben 于 2011-9-3 01:31 编辑
家在湾里 发表于 2011-9-2 21:56 http://bbs.mjtd.com/static/image/common/back.gif
siben兄,
终于发现问题所在了,把添加顶点的代码添加到WorldDraw方法中就OK了,谢谢你的指点哈
这样或许实现了你的需求,但我认为不应该这样做,因为若实现的功能若再复杂些,你将会很难处理;
另外,目前的做法是无数次的生成了一个转眼就被自杀的new polyline,实在是劳心劳力的做些无用工.
幸好.Net的垃圾收集机制或许处理了你遗留的东西.
另:我是Sieben
页:
[1]
2