Jig示例——拖动圆心
ARX帮助中的示例文件中,有一个用C#画椭圆的Jig范例,这里我写了一个更简单的一个Jig范例,希望对您能有所帮助。我们希望有这样的效果,如何实现呢?
一、建立一个C#类库工程,注册一个“JigCircle”命令,生成代码如下:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
namespace JigTest
{
public class JigClass : DrawJig
{
public void MyTest()
{
}
}
}二、在类名JigClass后面键入“: DrawJig”(注意空格),然后在DrawJig这几个字母上右击,选择“实现抽象类”。自动生成生成两个重载的函数,这时完整代码如下:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
namespace JigTest
{
public class JigClass : DrawJig
{
public void MyTest()
{
}
protected override bool WorldDraw(WorldDraw draw)
{
throw new System.NotImplementedException();
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
throw new System.NotImplementedException();
}
}
}三、添加两个全局量:
private Circle mCircle;
private Point3d mCenterPt;四、为各个函数书写代码,并添加一个添加图元到数据库的函数AppendEntity。最后完整代码如下:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
namespace JigTest
{
public class JigClass : DrawJig
{
private Circle mCircle;
private Point3d mCenterPt;
public void MyTest()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Matrix3d mt = ed.CurrentUserCoordinateSystem;
mCircle = new Circle();
mCircle.Radius = 100;
mCircle.Normal = db.Ucsxdir.CrossProduct(db.Ucsydir);
PromptResult resJig = ed.Drag(this);
if (resJig.Status == PromptStatus.OK)
{
AppendEntity(mCircle);
}
}
protected override bool WorldDraw(WorldDraw draw)
{
draw.Geometry.Draw(mCircle);
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions optJigDis = new JigPromptPointOptions
("\n请指定圆的圆心");
// 设置对拖拽的约束.
optJigDis.UserInputControls = UserInputControls.Accept3dCoordinates;
PromptPointResult resJigDis = prompts.AcquirePoint(optJigDis);
Point3d tempPt = resJigDis.Value;
if (resJigDis.Status == PromptStatus.Cancel)
{
return SamplerStatus.Cancel;
}
if (mCenterPt != tempPt)
{
mCenterPt = tempPt;
mCircle.Center = mCenterPt;
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;
}
}
} <p>后话,请把这个例子改为插入块。</p> <p>看过几个示例及专题。。。</p><p>发现一个共同的问题。</p><p>没有注释。</p><p>或是只有几句原作者的英文注释。</p><p>确实看不懂。</p> 就是,没有注释看水懂啦 <p>能否来个Jig写的:</p><p>加引线的序号球。</p><p>1.指定起点ptA</p><p>2.在指定第二点的之前,动态显示序号球及序号。</p><p> 动态输入改变序号,动态指定第二点的位置。</p><p></p> <p>序号球看看这里吧</p><p>动态输入改变序号的话,这个要改动一下</p><p><a href="http://bbs.mjtd.com/forum.php?mod=viewthread&tid=75604">http://bbs.mjtd.com/forum.php?mod=viewthread&tid=75604</a></p> 不错不错,学习学习 学习 ~ //一个Jig插入块的实例
public class jigInsertBlock : DrawJig
{
private BlockReference blkRef;
public void t2()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = doc.Editor;
ObjectId blkRec;
//这里创建一个事务, 但是不能直接在这个事务里面进行操作,否则会造成出错.出错的原因是块引用为Null,可能是因为Using后直接被销毁了?
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord modelSpace = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite);
if (bt.Has("tt"))
{
blkRec = bt["tt"];
}
else
{
ed.WriteMessage("图中没有tt图块的定义,程序退出.\n");
return;
}
}
blkRef = new BlockReference(new Point3d(0, 0, 0), blkRec);
PromptResult promptRes = ed.Drag(this);
if (promptRes.Status == PromptStatus.OK)
{
//绘图事物
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord modelSpace = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite);
modelSpace.AppendEntity(blkRef);
trans.AddNewlyCreatedDBObject(blkRef, true);
trans.Commit();
}
}
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
//提供的功能有可能产生几何不为不同的视口变化的能力。 draw 应该是类似Lisp中的 (redraw)
draw.Geometry.Draw(blkRef);
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions optJigDis = new JigPromptPointOptions
("\n请指定块的位置");
// 设置对拖拽的约束.
//这里我尝试使用基点,但是没有成功
optJigDis.UseBasePoint = true;
optJigDis.BasePoint = new Point3d(0, 0, 0);
optJigDis.UserInputControls = UserInputControls.Accept3dCoordinates;
PromptPointResult resJigDis = prompts.AcquirePoint(optJigDis);
Point3d tempPt = resJigDis.Value;
if (resJigDis.Status == PromptStatus.Cancel)
{
return SamplerStatus.Cancel;
}
//这里我直接使用了块参照的基点
if (blkRef.Position != tempPt)
{
blkRef.Position = tempPt;
return SamplerStatus.OK;
}
else
{
return SamplerStatus.NoChange;
}
}
}
页:
[1]