明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 7739|回复: 11

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

  [复制链接]
发表于 2005-7-15 10:49:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2005-7-15 16:02:27 编辑

先建一个名为“1”的块
  1. using System;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.Runtime;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.ApplicationServices;
  6. using System.Reflection;
  7. using System.IO;
  8. using System.Collections;
  9. using System.Runtime.InteropServices;
  10. using System.Diagnostics;
  11. using Autodesk.AutoCAD.EditorInput;
  12. using Autodesk.AutoCAD.GraphicsInterface;
  13. namespace TlsCad
  14. {
  15. public class BlockRefJig:EntityJig
  16. {
  17.   Point3d mPosition,mAnglePnt;
  18.   Vector3d mNormal;
  19.   double mAngle;
  20.   int mPromptCounter;
  21.   DynamicDimensionDataCollection m_dims;
  22.   public BlockRefJig(Vector3d vec,ObjectId id):base(new BlockReference(new Point3d(0,0,0),id))
  23.   {
  24.    mPosition=new Point3d(0,0,0);
  25.    mNormal=vec;
  26.    mAngle=0;
  27.    m_dims = new DynamicDimensionDataCollection();
  28.    Dimension dim1 = new AlignedDimension();
  29.    dim1.SetDatabaseDefaults();
  30.    m_dims.Add(new DynamicDimensionData(dim1,true,true));
  31.    Dimension dim2 = new AlignedDimension();
  32.    dim2.SetDatabaseDefaults();
  33.    m_dims.Add(new DynamicDimensionData(dim2,true,true));
  34.   }
  35.   
  36.   protected  override SamplerStatus Sampler(JigPrompts prompts)
  37.   {
  38.    JigPromptOptions jigOpts = new JigPromptOptions();
  39.    jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NoZeroResponseAccepted | UserInputControls.NoNegativeResponseAccepted);
  40.    
  41.    if(mPromptCounter == 0)
  42.    {
  43.     jigOpts.Message = "\nInput InsertPoint:";
  44.     PromptPointResult dres = prompts.AcquirePoint(jigOpts);
  45.      
  46.     Point3d positionTemp = dres.Value;
  47.     if(positionTemp != mPosition)
  48.     {
  49.      mPosition = positionTemp;
  50.     }
  51.     else
  52.      return SamplerStatus.NoChange;
  53.     if(dres.Status == PromptStatus.Cancel)
  54.      return SamplerStatus.Cancel;
  55.     else
  56.      return SamplerStatus.OK;
  57.      
  58.      
  59.    }
  60.    else if (mPromptCounter == 1)
  61.    {
  62.     jigOpts.BasePoint = mPosition;
  63.     jigOpts.UseBasePoint = true;
  64.     jigOpts.Message = "\nInput Angle:";
  65.     double angleTemp = -1;
  66.     PromptPointResult res = prompts.AcquirePoint(jigOpts);
  67.     mAnglePnt = res.Value;
  68.      
  69.     angleTemp = mAnglePnt.GetVectorTo(mPosition).AngleOnPlane(
  70.      new Plane(
  71.      Application.DocumentManager.MdiActiveDocument.Database.Ucsorg,
  72.      Application.DocumentManager.MdiActiveDocument.Database.Ucsxdir,
  73.      Application.DocumentManager.MdiActiveDocument.Database.Ucsydir
  74.      ));
  75.     if (angleTemp != mAngle)
  76.      mAngle = angleTemp;
  77.     else
  78.      return SamplerStatus.NoChange;
  79.     if(res.Status == PromptStatus.Cancel)
  80.      return SamplerStatus.Cancel;
  81.     else
  82.      return SamplerStatus.OK;
  83.    
  84.    }
  85.    else
  86.    {
  87.     return SamplerStatus.NoChange;
  88.    }
  89.    
  90.    
  91.   }
  92.   protected override bool Update()
  93.   {
  94.    
  95.    try
  96.    {
  97.     ((BlockReference)Entity).Position=mPosition;
  98.     ((BlockReference)Entity).Rotation =mAngle;
  99.     UpdateDimensions();
  100.      
  101.    }
  102.    catch(System.Exception)
  103.    {
  104.     return false;  
  105.    }
  106.    
  107.    return true;  
  108.   }
  109.   protected override DynamicDimensionDataCollection GetDynamicDimensionData(double dimScale)
  110.   {
  111.    return m_dims;
  112.   }
  113.   protected override void OnDimensionValueChanged(Autodesk.AutoCAD.DatabaseServices.DynamicDimensionChangedEventArgs e)
  114.   {
  115.    
  116.   }
  117.   void UpdateDimensions()
  118.   {
  119.    BlockReference blkref = (BlockReference)Entity;
  120.    if(mPromptCounter == 0)
  121.    {
  122.     AlignedDimension dim1 = (AlignedDimension)m_dims[0].Dimension;
  123.     dim1.XLine1Point = blkref.Position;
  124.     dim1.DimLinePoint = blkref.Position;
  125.    }
  126.    else
  127.    {
  128.     Ellipse myellipse = (Ellipse)Entity;
  129.     AlignedDimension dim2 = (AlignedDimension)m_dims[1].Dimension;
  130.     dim2.XLine1Point = blkref.Position;
  131.     dim2.XLine2Point = mAnglePnt;
  132.     dim2.DimLinePoint = blkref.Position;
  133.    }
  134.   }
  135.   public void setPromptCounter(int i)
  136.   {
  137.    mPromptCounter = i;
  138.   }
  139.   public Entity GetEntity()
  140.   {
  141.    return Entity;
  142.   }
  143.   [CommandMethod("tjig")]
  144.   static public void DoIt()
  145.   {
  146.    Vector3d x = Application.DocumentManager.MdiActiveDocument.Database.Ucsxdir;
  147.    Vector3d y = Application.DocumentManager.MdiActiveDocument.Database.Ucsydir;
  148.    Vector3d NormalVec = x.CrossProduct(y);
  149.   
  150.    using (TlsTM tm = new TlsTM(true))
  151.    {              
  152.   
  153.    BlockTable pbt=(BlockTable)tm.AutoCadTM.GetObject(tm.Database.BlockTableId, OpenMode.ForRead,true);
  154.    BlockRefJig jig = new BlockRefJig(NormalVec,pbt["1"]);
  155.    jig.setPromptCounter(0);
  156.    Application.DocumentManager.MdiActiveDocument.Editor.Drag(jig);
  157.    jig.setPromptCounter(1);
  158.    Application.DocumentManager.MdiActiveDocument.Editor.Drag(jig);
  159.     tm.OpenBlockTableRecord(BlockTableRecord.ModelSpace);
  160.     tm.Add(jig.GetEntity());
  161.    }
  162.    
  163.   }
  164. }
  165. public class TlsTM:IDisposable
  166. {
  167.   private Database db;
  168.   private AutoCadTM tm;
  169.   private Transaction ta;
  170.   private BlockTable bt;
  171.   private BlockTableRecord btr;
  172.   private bool IsStarted=false;
  173.   public TlsTM(bool Starting)
  174.   {
  175.    if(Starting)
  176.    {
  177.     db = HostApplicationServices.WorkingDatabase;
  178.     tm = db.TransactionManager;
  179.     ta = tm.StartTransaction();
  180.    }
  181.     IsStarted=Starting;
  182.   }
  183.   public Editor Editor
  184.   {
  185.    get
  186.    {
  187.     return Application.DocumentManager.MdiActiveDocument.Editor;
  188.    }
  189.   }
  190.   public Database Database
  191.   {
  192.    get
  193.    {
  194.     return db;
  195.    }
  196.   }
  197.   public AutoCadTM AutoCadTM
  198.   {
  199.    get
  200.    {
  201.     return tm;
  202.    }
  203.   }
  204.   public Transaction Transaction
  205.   {
  206.    get
  207.    {
  208.     return ta;
  209.    }
  210.   }
  211.   public BlockTable BlockTable
  212.   {
  213.    get
  214.    {
  215.     return bt;
  216.    }
  217.   }
  218.   public BlockTableRecord BlockTableRecord
  219.   {
  220.    get
  221.    {
  222.     return btr;
  223.    }
  224.   }
  225.   #region Add Entity
  226.    public ObjectId Add(Entity entity)
  227.    {
  228.     ObjectId id = btr.AppendEntity(entity);
  229.     tm.AddNewlyCreatedDBObject(entity, true);
  230.     return id;
  231.    }
  232.    public ObjectIdCollection Add(DBObjectCollection objs)
  233.    {
  234.     ObjectIdCollection ids = new ObjectIdCollection();
  235.     foreach(DBObject obj in objs)
  236.     {
  237.      ids.Add(this.Add((Entity)obj));
  238.     }
  239.     return ids;
  240.    }
  241.    public ObjectIdCollection Add(DBObject[] objs)
  242.    {
  243.     ObjectIdCollection ids = new ObjectIdCollection();
  244.     foreach(DBObject obj in objs)
  245.     {
  246.      ids.Add(this.Add((Entity)obj));
  247.     }
  248.     return ids;
  249.    }
  250.   #endregion
  251.   #region Remove Entity
  252.    public bool Remove(ObjectId id)
  253.    {
  254.     DBObject obj;
  255.     try
  256.     {
  257.      obj = tm.GetObject(id,OpenMode.ForWrite);
  258.      obj.Erase(true);
  259.     }
  260.     catch
  261.     {
  262.      return false;
  263.     }
  264.     return true;
  265.    }
  266.    public bool Remove(ObjectIdCollection ids)
  267.    {
  268.     foreach(ObjectId id in ids)
  269.     {
  270.      try
  271.      {
  272.       DBObject obj;
  273.       obj = tm.GetObject(id,OpenMode.ForWrite);
  274.       obj.Erase(true);
  275.      }
  276.      catch
  277.      {
  278.       return false;
  279.      }
  280.     }
  281.     return true;
  282.    }
  283.    public bool Remove(ObjectId[] ids)
  284.    {
  285.     foreach(ObjectId id in ids)
  286.     {
  287.      try
  288.      {
  289.       DBObject obj;
  290.       obj = tm.GetObject(id,OpenMode.ForWrite);
  291.       obj.Erase(true);
  292.      }
  293.      catch
  294.      {
  295.       return false;
  296.      }
  297.     }
  298.     return true;
  299.    }
  300.   #endregion
  301.   #region Trans
  302.   public void OpenBlockTableRecord(string str)
  303.   {
  304.    bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
  305.    btr = (BlockTableRecord)tm.GetObject(bt[str], OpenMode.ForWrite, false);
  306.   }
  307.   public Entity GetObject(ObjectId id,OpenMode mode)
  308.   {
  309.    return (Entity)tm.GetObject(id,mode,true);
  310.   }
  311.   void IDisposable.Dispose()
  312.   {
  313.    if(IsStarted)
  314.    {
  315.     ta.Commit();
  316.    }
  317.    ta.Dispose();
  318.   }
  319.   #endregion
  320.   public void RegApp(string AppName)
  321.   {
  322.    RegAppTable tbl = (RegAppTable)tm.GetObject(db.RegAppTableId, OpenMode.ForWrite, false);
  323.    if(!tbl.Has(AppName))
  324.    {
  325.     RegAppTableRecord app = new RegAppTableRecord();
  326.     app.Name = AppName;
  327.     tbl.Add(app);
  328.     tm.AddNewlyCreatedDBObject(app, true);
  329.    }
  330.   }
  331. }
  332. }
发表于 2005-7-15 14:42:00 | 显示全部楼层

第一个支持。研究研究。

发表于 2005-7-15 21:32:00 | 显示全部楼层
飞狐,能不能用VB。NET写一下!
 楼主| 发表于 2005-7-18 12:36:00 | 显示全部楼层

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

发表于 2005-7-18 19:08:00 | 显示全部楼层
可以把dll传上来吗?硬盘太小,没有装C#
 楼主| 发表于 2005-7-20 12:11:00 | 显示全部楼层

我把这里改过了,想输入角度,直接输入值没问题,但鼠标动作时有问题,郁闷

  protected  override 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;
   }
    
    
  }

 

 楼主| 发表于 2005-7-20 16:58:00 | 显示全部楼层

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

发表于 2007-4-18 14:37:00 | 显示全部楼层
在autocad2006下不能运行
发表于 2007-7-17 19:29:00 | 显示全部楼层

代码中还是有几处问题,修改后成功了,但有些问题很困惑

1.用F8运行,为什么出现问题 ,要求输入点的地方不停顿,F5就行

2.把块删掉后 PURGE 重建一个,就不能正确运行了,是什么原因

lzh741206版主能加我QQ:79850399

发表于 2007-8-17 15:44:00 | 显示全部楼层
AutoCadTM说缺少using指令集或程序集引用怎么回事!
我是新手请大家多多指导!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 18:47 , Processed in 0.206055 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表