雪山飞狐_lzh 发表于 2005-7-15 10:49:00

照着例子做了一个块的拖动:)

本帖最后由 作者 于 2005-7-15 16:02:27 编辑

先建一个名为“1”的块using System;
using Autodesk.AutoCAD.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);
   }
}
}
}

ntchjie 发表于 2005-7-15 14:42:00

<P>第一个支持。研究研究。</P>

ahlzl 发表于 2005-7-15 21:32:00

飞狐,能不能用VB。NET写一下!

雪山飞狐_lzh 发表于 2005-7-18 12:36:00

<P>把代码编译为Dll后,可以在VB.Net里引用的,用VB.Net写一下就比较头痛了:)</P>

月海临风 发表于 2005-7-18 19:08:00

可以把dll传上来吗?硬盘太小,没有装C#

雪山飞狐_lzh 发表于 2005-7-20 12:11:00

<P>我把这里改过了,想输入角度,直接输入值没问题,但鼠标动作时有问题,郁闷</P>
<P>&nbsp;&nbsp;protected&nbsp; override SamplerStatus Sampler(JigPrompts prompts)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;JigPromptOptions jigOpts = new JigPromptOptions();<BR>&nbsp;&nbsp;&nbsp;jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NoZeroResponseAccepted | UserInputControls.NoNegativeResponseAccepted );<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;if(mPromptCounter == 0)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;jigOpts.Message = "\n请输入基点:";<BR>&nbsp;&nbsp;&nbsp;&nbsp;PromptPointResult dres = prompts.AcquirePoint(jigOpts);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;Point3d positionTemp = dres.Value;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(positionTemp != mPosition)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mPosition = positionTemp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SamplerStatus.NoChange;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;if(dres.Status == PromptStatus.Cancel)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SamplerStatus.Cancel;<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SamplerStatus.OK;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;else if (mPromptCounter == 1) <BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;jigOpts.BasePoint = mPosition;<BR>&nbsp;&nbsp;&nbsp;&nbsp;jigOpts.UseBasePoint = true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;jigOpts.Message = "\n请输入旋转角度:";<BR>&nbsp;&nbsp;&nbsp;&nbsp;double angleTemp = -1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;PromptDoubleResult res = prompts.AcquireAngle(jigOpts);<BR>&nbsp;&nbsp;&nbsp;&nbsp;angleTemp = res.Value;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (angleTemp != mAngle)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mAngle = angleTemp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;else <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SamplerStatus.NoChange;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;if(res.Status == PromptStatus.Cancel)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SamplerStatus.Cancel;<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SamplerStatus.OK;<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;return SamplerStatus.NoChange;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;</P>

雪山飞狐_lzh 发表于 2005-7-20 16:58:00

<P>感觉AcquireAngle好像取的是距离,难道是个BUG?</P>

tbeahgl 发表于 2007-4-18 14:37:00

在autocad2006下不能运行

sailor538 发表于 2007-7-17 19:29:00

<p>代码中还是有几处问题,修改后成功了,但有些问题很困惑</p><p>1.用F8运行,为什么出现问题 ,要求输入点的地方不停顿,F5就行</p><p>2.把块删掉后 PURGE 重建一个,就不能正确运行了,是什么原因</p><p><strong><font face="Verdana" color="#da2549">lzh741206版主能加我QQ:79850399</font></strong></p><p></p>

makegood 发表于 2007-8-17 15:44:00

AutoCadTM说缺少using指令集或程序集引用怎么回事!
我是新手请大家多多指导!
页: [1] 2
查看完整版本: 照着例子做了一个块的拖动:)