照着例子做了一个块的拖动:)
先建一个名为“1”的块using System;
using Autodesk..DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using System.Reflection;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.GraphicsInterface;
namespace TlsCad
{
public class BlockRefJig:EntityJig
{
Point3d mPosition,mAnglePnt;
Vector3d mNormal;
double mAngle;
int mPromptCounter;
DynamicDimensionDataCollection m_dims;
public BlockRefJig(Vector3d vec,ObjectId id):base(new BlockReference(new Point3d(0,0,0),id))
{
mPosition=new Point3d(0,0,0);
mNormal=vec;
mAngle=0;
m_dims = new DynamicDimensionDataCollection();
Dimension dim1 = new AlignedDimension();
dim1.SetDatabaseDefaults();
m_dims.Add(new DynamicDimensionData(dim1,true,true));
Dimension dim2 = new AlignedDimension();
dim2.SetDatabaseDefaults();
m_dims.Add(new DynamicDimensionData(dim2,true,true));
}
protectedoverride SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptOptions jigOpts = new JigPromptOptions();
jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NoZeroResponseAccepted | UserInputControls.NoNegativeResponseAccepted);
if(mPromptCounter == 0)
{
jigOpts.Message = "\nInput InsertPoint:";
PromptPointResult dres = prompts.AcquirePoint(jigOpts);
Point3d positionTemp = dres.Value;
if(positionTemp != mPosition)
{
mPosition = positionTemp;
}
else
return SamplerStatus.NoChange;
if(dres.Status == PromptStatus.Cancel)
return SamplerStatus.Cancel;
else
return SamplerStatus.OK;
}
else if (mPromptCounter == 1)
{
jigOpts.BasePoint = mPosition;
jigOpts.UseBasePoint = true;
jigOpts.Message = "\nInput Angle:";
double angleTemp = -1;
PromptPointResult res = prompts.AcquirePoint(jigOpts);
mAnglePnt = res.Value;
angleTemp = mAnglePnt.GetVectorTo(mPosition).AngleOnPlane(
new Plane(
Application.DocumentManager.MdiActiveDocument.Database.Ucsorg,
Application.DocumentManager.MdiActiveDocument.Database.Ucsxdir,
Application.DocumentManager.MdiActiveDocument.Database.Ucsydir
));
if (angleTemp != mAngle)
mAngle = angleTemp;
else
return SamplerStatus.NoChange;
if(res.Status == PromptStatus.Cancel)
return SamplerStatus.Cancel;
else
return SamplerStatus.OK;
}
else
{
return SamplerStatus.NoChange;
}
}
protected override bool Update()
{
try
{
((BlockReference)Entity).Position=mPosition;
((BlockReference)Entity).Rotation =mAngle;
UpdateDimensions();
}
catch(System.Exception)
{
return false;
}
return true;
}
protected override DynamicDimensionDataCollection GetDynamicDimensionData(double dimScale)
{
return m_dims;
}
protected override void OnDimensionValueChanged(Autodesk.AutoCAD.DatabaseServices.DynamicDimensionChangedEventArgs e)
{
}
void UpdateDimensions()
{
BlockReference blkref = (BlockReference)Entity;
if(mPromptCounter == 0)
{
AlignedDimension dim1 = (AlignedDimension)m_dims.Dimension;
dim1.XLine1Point = blkref.Position;
dim1.DimLinePoint = blkref.Position;
}
else
{
Ellipse myellipse = (Ellipse)Entity;
AlignedDimension dim2 = (AlignedDimension)m_dims.Dimension;
dim2.XLine1Point = blkref.Position;
dim2.XLine2Point = mAnglePnt;
dim2.DimLinePoint = blkref.Position;
}
}
public void setPromptCounter(int i)
{
mPromptCounter = i;
}
public Entity GetEntity()
{
return Entity;
}
static public void DoIt()
{
Vector3d x = Application.DocumentManager.MdiActiveDocument.Database.Ucsxdir;
Vector3d y = Application.DocumentManager.MdiActiveDocument.Database.Ucsydir;
Vector3d NormalVec = x.CrossProduct(y);
using (TlsTM tm = new TlsTM(true))
{
BlockTable pbt=(BlockTable)tm.AutoCadTM.GetObject(tm.Database.BlockTableId, OpenMode.ForRead,true);
BlockRefJig jig = new BlockRefJig(NormalVec,pbt["1"]);
jig.setPromptCounter(0);
Application.DocumentManager.MdiActiveDocument.Editor.Drag(jig);
jig.setPromptCounter(1);
Application.DocumentManager.MdiActiveDocument.Editor.Drag(jig);
tm.OpenBlockTableRecord(BlockTableRecord.ModelSpace);
tm.Add(jig.GetEntity());
}
}
}
public class TlsTM:IDisposable
{
private Database db;
private AutoCadTM tm;
private Transaction ta;
private BlockTable bt;
private BlockTableRecord btr;
private bool IsStarted=false;
public TlsTM(bool Starting)
{
if(Starting)
{
db = HostApplicationServices.WorkingDatabase;
tm = db.TransactionManager;
ta = tm.StartTransaction();
}
IsStarted=Starting;
}public Editor Editor
{
get
{
return Application.DocumentManager.MdiActiveDocument.Editor;
}
}
public Database Database
{
get
{
return db;
}
}
public AutoCadTM AutoCadTM
{
get
{
return tm;
}
}
public Transaction Transaction
{
get
{
return ta;
}
}
public BlockTable BlockTable
{
get
{
return bt;
}
}
public BlockTableRecord BlockTableRecord
{
get
{
return btr;
}
}#region Add Entity
public ObjectId Add(Entity entity)
{
ObjectId id = btr.AppendEntity(entity);
tm.AddNewlyCreatedDBObject(entity, true);
return id;
}
public ObjectIdCollection Add(DBObjectCollection objs)
{
ObjectIdCollection ids = new ObjectIdCollection();
foreach(DBObject obj in objs)
{
ids.Add(this.Add((Entity)obj));
}
return ids;
}
public ObjectIdCollection Add(DBObject[] objs)
{
ObjectIdCollection ids = new ObjectIdCollection();
foreach(DBObject obj in objs)
{
ids.Add(this.Add((Entity)obj));
}
return ids;
}
#endregion
#region Remove Entity
public bool Remove(ObjectId id)
{
DBObject obj;
try
{
obj = tm.GetObject(id,OpenMode.ForWrite);
obj.Erase(true);
}
catch
{
return false;
}
return true;
}
public bool Remove(ObjectIdCollection ids)
{
foreach(ObjectId id in ids)
{
try
{
DBObject obj;
obj = tm.GetObject(id,OpenMode.ForWrite);
obj.Erase(true);
}
catch
{
return false;
}
}
return true;
}
public bool Remove(ObjectId[] ids)
{
foreach(ObjectId id in ids)
{
try
{
DBObject obj;
obj = tm.GetObject(id,OpenMode.ForWrite);
obj.Erase(true);
}
catch
{
return false;
}
}
return true;
}
#endregion
#region Trans
public void OpenBlockTableRecord(string str)
{
bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
btr = (BlockTableRecord)tm.GetObject(bt, OpenMode.ForWrite, false);
}
public Entity GetObject(ObjectId id,OpenMode mode)
{
return (Entity)tm.GetObject(id,mode,true);
}
void IDisposable.Dispose()
{
if(IsStarted)
{
ta.Commit();
}
ta.Dispose();
}
#endregion
public void RegApp(string AppName)
{
RegAppTable tbl = (RegAppTable)tm.GetObject(db.RegAppTableId, OpenMode.ForWrite, false);
if(!tbl.Has(AppName))
{
RegAppTableRecord app = new RegAppTableRecord();
app.Name = AppName;
tbl.Add(app);
tm.AddNewlyCreatedDBObject(app, true);
}
}
}
} 第一个支持。研究研究。
飞狐,能不能用VB。NET写一下! 把代码编译为Dll后,可以在VB.Net里引用的,用VB.Net写一下就比较头痛了:)
可以把dll传上来吗?硬盘太小,没有装C# 我把这里改过了,想输入角度,直接输入值没问题,但鼠标动作时有问题,郁闷
protectedoverride SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptOptions jigOpts = new JigPromptOptions();
jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NoZeroResponseAccepted | UserInputControls.NoNegativeResponseAccepted );
if(mPromptCounter == 0)
{
jigOpts.Message = "\n请输入基点:";
 romptPointResult dres = prompts.AcquirePoint(jigOpts);
 oint3d positionTemp = dres.Value;
if(positionTemp != mPosition)
{
mPosition = positionTemp;
}
else
return SamplerStatus.NoChange;
if(dres.Status == PromptStatus.Cancel)
return SamplerStatus.Cancel;
else
return SamplerStatus.OK;
}
else if (mPromptCounter == 1)
{
jigOpts.BasePoint = mPosition;
jigOpts.UseBasePoint = true;
jigOpts.Message = "\n请输入旋转角度:";
double angleTemp = -1;
 romptDoubleResult res = prompts.AcquireAngle(jigOpts);
angleTemp = res.Value;
if (angleTemp != mAngle)
mAngle = angleTemp;
else
return SamplerStatus.NoChange;
if(res.Status == PromptStatus.Cancel)
return SamplerStatus.Cancel;
else
return SamplerStatus.OK;
}
else
{
return SamplerStatus.NoChange;
}
}
感觉AcquireAngle好像取的是距离,难道是个BUG?
在autocad2006下不能运行 代码中还是有几处问题,修改后成功了,但有些问题很困惑
1.用F8运行,为什么出现问题 ,要求输入点的地方不停顿,F5就行
2.把块删掉后 PURGE 重建一个,就不能正确运行了,是什么原因
lzh741206版主能加我QQ:79850399
AutoCadTM说缺少using指令集或程序集引用怎么回事!
我是新手请大家多多指导!
页:
[1]