zoukankan      html  css  js  c++  java
  • Kean专题:拖动一个属性块(JIG拖拽)

    原文转载自:http://through-the-interface.typepad.com/through_the_interface/jigs/(该口已无法访问)

    可访问转载入口:http://bbs.mjtd.com/thread-75618-1-1.html(转载自明镜通道by雪山飞狐_lzh

    原kean博客已经无法看到,故转载明经通道 雪山飞狐_lzh 老师整理内容

    1.kean原文翻译

    March 18, 2009
    Jigging an AutoCAD block with attributes using .NET

    利用.net做AutoCAD的属性块拖动
    Thanks, once again, to Philippe Leefsma, a DevTech engineer based in Prague, for contributing the code for this post.再次感谢  Philippe Leefsma 一名位于布拉格的开发技术工程师,贡献了这篇文章中的代码

    While researching an issue he was working on Philippe stumbled across a comment on this previous post where I more-or-less said jigging attributes wasn’t possible. 在研究一个他工作中的问题的时候,Philippe

    突然发现在之前的文章中发表的一个评论,或多或少的提到拖动属性块是不可能的

    Ahem. Anyway, Philippe decided to – quite rightly – prove me wrong, and the result is today’s post. :-)

    Philippe决定用正确的方式证明我是不对的,所有就有了个今天这篇帖子
    It turns out that the trick to jigging a block with attributes is to add the block reference to the database prior to running the jig.他证实拖动属性块的技巧是在进行拖动之前先将块参照添加到数据库中

    I’d been coming at this from another direction – working out how to call through to the right version of the ObjectARX function, the one that allows the block reference to be in-memory rather than db-resident – but Philippe’s approach means that’s no longer needed.对于这个问题我一直朝着另一个方向,通过如何调用正确的ObjectARX 功能来完成一个允许将块属性保存在内存中而不是宿主在图形数据库中,这也就是意味着Philippe采用的方法不再被需要啦

    I see this technique as potentially being useful when jigging other entities that benefit from being database resident (Solid3d objects spring to mind), so I really appreciate Philippe’s hard work on this.我知道这个技术在拖拽其他受益于宿主图形数据库中的实体是非常有用的,所有我真的很欣赏Philippe对于这方面做出的努力
    Here’s the C# code which I’ve edited for posting:

    1. using Autodesk.AutoCAD.Runtime;

    2. using Autodesk.AutoCAD.ApplicationServices;

    3. using Autodesk.AutoCAD.DatabaseServices;

    4. using Autodesk.AutoCAD.EditorInput;

    5. using Autodesk.AutoCAD.Geometry;

    6. using System.Collections.Generic;

    7. namespace BlockJig

    8. {

    9. class CBlockJig : EntityJig

    10.   {

    11. private Point3d _pos;

    12. private Dictionary<string, Point3d> _attPos;

    13. private Transaction _tr;

    14. public CBlockJig(Transaction tr, BlockReference br)

    15.       : base(br)

    16.     {

    17.       _pos = br.Position;

    18. // Initialize our dictionary with the tag /

    19. // AttributeDefinition position

    20.       _attPos = new Dictionary<string, Point3d>();

    21.       _tr = tr;

    22.       BlockTableRecord btr =

    23. (BlockTableRecord)_tr.GetObject(

    24.           br.BlockTableRecord,

    25.           OpenMode.ForRead

    26. );

    27. if (btr.HasAttributeDefinitions)

    28.       {

    29. foreach (ObjectId id in btr)

    30.         {

    31.           DBObject obj =

    32.             tr.GetObject(id, OpenMode.ForRead);

    33.           AttributeDefinition ad =

    34.             obj as AttributeDefinition;

    35. if (ad != null)

    36.           {

    37.             _attPos.Add(ad.Tag, ad.Position);

    38.           }

    39.         }

    40.       }

    41.     }

    42. protected override bool Update()

    43.     {

    44.       BlockReference br = Entity as BlockReference;

    45.       br.Position = _pos;

    46. if (br.AttributeCollection.Count != 0)

    47.       {

    48. foreach (ObjectId id in br.AttributeCollection)

    49.         {

    50.           DBObject obj =

    51.             _tr.GetObject(id, OpenMode.ForRead);

    52.           AttributeReference ar =

    53.             obj as AttributeReference;

    54. // Apply block transform to att def position

    55. if (ar != null)

    56.           {

    57.             ar.UpgradeOpen();

    58.             ar.Position =

    59.               _attPos[ar.Tag].TransformBy(br.BlockTransform);

    60.           }

    61.         }

    62.       }

    63. return true;

    64.     }

    65. protected override SamplerStatus Sampler(JigPrompts prompts)

    66.     {

    67.       JigPromptPointOptions opts =

    68. new JigPromptPointOptions(" Select insertion point:");

    69.       opts.BasePoint = new Point3d(0, 0, 0);

    70.       opts.UserInputControls =

    71.         UserInputControls.NoZeroResponseAccepted;

    72.       PromptPointResult ppr = prompts.AcquirePoint(opts);

    73. if (_pos == ppr.Value)

    74.       {

    75. return SamplerStatus.NoChange;

    76.       }

    77.       _pos = ppr.Value;

    78. return SamplerStatus.OK;

    79.     }

    80. public PromptStatus Run()

    81.     {

    82. Document doc =

    83. Application.DocumentManager.MdiActiveDocument;

    84. Editor ed = doc.Editor;

    85.       PromptResult promptResult = ed.Drag(this);

    86. return promptResult.Status;

    87.     }

    88.   }

    89. public class Commands

    90.   {

    91. [CommandMethod("BJ")]

    92. static public void BlockJig()

    93.     {

    94. Document doc =

    95. Application.DocumentManager.MdiActiveDocument;

    96. Database db = doc.Database;

    97. Editor ed = doc.Editor;

    98.       PromptStringOptions pso =

    99. new PromptStringOptions(" Enter block name: ");

    100.       PromptResult pr = ed.GetString(pso);

    101. if (pr.Status != PromptStatus.OK)

    102. return;

    103.       Transaction tr =

    104.         doc.TransactionManager.StartTransaction();

    105. using (tr)

    106.       {

    107.         BlockTable bt =

    108. (BlockTable)tr.GetObject(

    109.             db.BlockTableId,

    110.             OpenMode.ForRead

    111. );

    112.         BlockTableRecord space =

    113. (BlockTableRecord)tr.GetObject(

    114.             db.CurrentSpaceId,

    115.             OpenMode.ForRead

    116. );

    117. if (!bt.Has(pr.StringResult))

    118.         {

    119.           ed.WriteMessage(

    120. " Block "" + pr.StringResult + "" not found.");

    121. return;

    122.         }

    123.         space.UpgradeOpen();

    124.         BlockTableRecord btr =

    125. (BlockTableRecord)tr.GetObject(

    126.             bt[pr.StringResult],

    127.             OpenMode.ForRead);

    128. // Block needs to be inserted to current space before

    129. // being able to append attribute to it

    130.         BlockReference br =

    131. new BlockReference(new Point3d(), btr.ObjectId);

    132.         space.AppendEntity(br);

    133.         tr.AddNewlyCreatedDBObject(br, true);

    134. if (btr.HasAttributeDefinitions)

    135.         {

    136. foreach (ObjectId id in btr)

    137.           {

    138.             DBObject obj =

    139.               tr.GetObject(id, OpenMode.ForRead);

    140.             AttributeDefinition ad =

    141.               obj as AttributeDefinition;

    142. if (ad != null && !ad.Constant)

    143.             {

    144.               AttributeReference ar =

    145. new AttributeReference();

    146.               ar.SetAttributeFromBlock(ad, br.BlockTransform);

    147.               ar.Position =

    148.                 ad.Position.TransformBy(br.BlockTransform);

    149.               ar.TextString = ad.TextString;

    150.               br.AttributeCollection.AppendAttribute(ar);

    151.               tr.AddNewlyCreatedDBObject(ar, true);

    152.             }

    153.           }

    154.         }

    155. // Run the jig

    156.         CBlockJig myJig = new CBlockJig(tr, br);

    157. if (myJig.Run() != PromptStatus.OK)

    158. return;

    159. // Commit changes if user accepted, otherwise discard

    160.         tr.Commit();

    161.       }

    162.     }

    163.   }

    164. }

    When you run the BJ command (short for BlockJig) and specify the name of a block in the current drawing which contains attributes, you’ll now see the attributes with their default values shown as part of the block being jigged.当你运行一个BJ命令并且制定一个块名在当前包含特性的图形中,你会看到定义的属性值作为作为被拖拽块的一部分被显示

    Implementing the code to allow editing of those attributes after insertion is left as an exercise for the reader.

    编写一段代码允许编辑已经插入的属性留给读者作为练习完成

    练习部分由我完成


    Update:
    This code didn't work for a few situations, such as when using justification (attributes would end up at the origin after being dragged) or with MText attributes (which would start at the origin until the mouse was moved).

    这个代码不能工作在一些情况,例如当你使用对正(再被拖拽后属性将会回到原点处)或多行文本(当鼠标被移动是属性将从原点开始)等情况
    A big thanks to Roland Feletic from PAUSER ZT-GMBH for helping identify and diagnose the various cases.
    Here's the updated C# code:

    1. using Autodesk.AutoCAD.Runtime;

    2. using Autodesk.AutoCAD.ApplicationServices;

    3. using Autodesk.AutoCAD.DatabaseServices;

    4. using Autodesk.AutoCAD.EditorInput;

    5. using Autodesk.AutoCAD.Geometry;

    6. using System.Collections.Generic;

    7. namespace BlockJigApplication

    8. {

    9. class AttInfo

    10.   {

    11. private Point3d _pos;

    12. private Point3d _aln;

    13. private bool _aligned;

    14. public AttInfo(Point3d pos, Point3d aln, bool aligned)

    15.     {

    16.       _pos = pos;

    17.       _aln = aln;

    18.       _aligned = aligned;

    19.     }

    20. public Point3d Position

    21.     {

    22.       set { _pos = value; }

    23.       get { return _pos; }

    24.     }

    25. public Point3d Alignment

    26.     {

    27.       set { _aln = value; }

    28.       get { return _aln; }

    29.     }

    30. public bool IsAligned

    31.     {

    32.       set { _aligned = value; }

    33.       get { return _aligned; }

    34.     }

    35.   }

    36. class BlockJig : EntityJig

    37.   {

    38. private Point3d _pos;

    39. private Dictionary<ObjectId, AttInfo> _attInfo;

    40. private Transaction _tr;

    41. public BlockJig(

    42.       Transaction tr,

    43.       BlockReference br,

    44.       Dictionary<ObjectId, AttInfo> attInfo

    45. ) : base(br)

    46.     {

    47.       _pos = br.Position;

    48.       _attInfo = attInfo;

    49.       _tr = tr;

    50.     }

    51. protected override bool Update()

    52.     {

    53.       BlockReference br = Entity as BlockReference;

    54.       br.Position = _pos;

    55. if (br.AttributeCollection.Count != 0)

    56.       {

    57. foreach (ObjectId id in br.AttributeCollection)

    58.         {

    59.           DBObject obj =

    60.             _tr.GetObject(id, OpenMode.ForRead);

    61.           AttributeReference ar =

    62.             obj as AttributeReference;

    63. // Apply block transform to att def position

    64. if (ar != null)

    65.           {

    66.             ar.UpgradeOpen();

    67.             AttInfo ai = _attInfo[ar.ObjectId];

    68.             ar.Position =

    69.               ai.Position.TransformBy(br.BlockTransform);

    70. if (ai.IsAligned)

    71.             {

    72.               ar.AlignmentPoint =

    73.                 ai.Alignment.TransformBy(br.BlockTransform);

    74.             }

    75. if (ar.IsMTextAttribute)

    76.             {

    77.               ar.UpdateMTextAttribute();

    78.             }

    79.           }

    80.         }

    81.       }

    82. return true;

    83.     }

    84. protected override SamplerStatus Sampler(JigPrompts prompts)

    85.     {

    86.       JigPromptPointOptions opts =

    87. new JigPromptPointOptions(" Select insertion point:");

    88.       opts.BasePoint = new Point3d(0, 0, 0);

    89.       opts.UserInputControls =

    90.         UserInputControls.NoZeroResponseAccepted;

    91.       PromptPointResult ppr = prompts.AcquirePoint(opts);

    92. if (_pos == ppr.Value)

    93.       {

    94. return SamplerStatus.NoChange;

    95.       }

    96.       _pos = ppr.Value;

    97. return SamplerStatus.OK;

    98.     }

    99. public PromptStatus Run()

    100.     {

    101. Document doc =

    102. Application.DocumentManager.MdiActiveDocument;

    103. Editor ed = doc.Editor;

    104.       PromptResult promptResult = ed.Drag(this);

    105. return promptResult.Status;

    106.     }

    107.   }

    108. public class Commands

    109.   {

    110. [CommandMethod("BJ")]

    111. static public void BlockJigCmd()

    112.     {

    113. Document doc =

    114. Application.DocumentManager.MdiActiveDocument;

    115. Database db = doc.Database;

    116. Editor ed = doc.Editor;

    117.       PromptStringOptions pso =

    118. new PromptStringOptions(" Enter block name: ");

    119.       PromptResult pr = ed.GetString(pso);

    120. if (pr.Status != PromptStatus.OK)

    121. return;

    122.       Transaction tr =

    123.         doc.TransactionManager.StartTransaction();

    124. using (tr)

    125.       {

    126.         BlockTable bt =

    127. (BlockTable)tr.GetObject(

    128.             db.BlockTableId,

    129.             OpenMode.ForRead

    130. );

    131. if (!bt.Has(pr.StringResult))

    132.         {

    133.           ed.WriteMessage(

    134. " Block "" + pr.StringResult + "" not found.");

    135. return;

    136.         }

    137.         BlockTableRecord space =

    138. (BlockTableRecord)tr.GetObject(

    139.             db.CurrentSpaceId,

    140.             OpenMode.ForWrite

    141. );

    142.         BlockTableRecord btr =

    143. (BlockTableRecord)tr.GetObject(

    144.             bt[pr.StringResult],

    145.             OpenMode.ForRead);

    146. // Block needs to be inserted to current space before

    147. // being able to append attribute to it

    148.         BlockReference br =

    149. new BlockReference(new Point3d(), btr.ObjectId);

    150.         space.AppendEntity(br);

    151.         tr.AddNewlyCreatedDBObject(br, true);

    152.         Dictionary<ObjectId, AttInfo> attInfo =

    153. new Dictionary<ObjectId,AttInfo>();

    154. if (btr.HasAttributeDefinitions)

    155.         {

    156. foreach (ObjectId id in btr)

    157.           {

    158.             DBObject obj =

    159.               tr.GetObject(id, OpenMode.ForRead);

    160.             AttributeDefinition ad =

    161.               obj as AttributeDefinition;

    162. if (ad != null && !ad.Constant)

    163.             {

    164.               AttributeReference ar =

    165. new AttributeReference();

    166.               ar.SetAttributeFromBlock(ad, br.BlockTransform);

    167.               ar.Position =

    168.                 ad.Position.TransformBy(br.BlockTransform);

    169. if (ad.Justify != AttachmentPoint.BaseLeft)

    170.               {

    171.                 ar.AlignmentPoint =

    172.                   ad.AlignmentPoint.TransformBy(br.BlockTransform);

    173.               }

    174. if (ar.IsMTextAttribute)

    175.               {

    176.                 ar.UpdateMTextAttribute();

    177.               }

    178.               ar.TextString = ad.TextString;

    179.               ObjectId arId =

    180.                 br.AttributeCollection.AppendAttribute(ar);

    181.               tr.AddNewlyCreatedDBObject(ar, true);

    182. // Initialize our dictionary with the ObjectId of

    183. // the attribute reference + attribute definition info

    184.               attInfo.Add(

    185.                 arId,

    186. new AttInfo(

    187.                   ad.Position,

    188.                   ad.AlignmentPoint,

    189.                   ad.Justify != AttachmentPoint.BaseLeft

    190. )

    191. );

    192.             }

    193.           }

    194.         }

    195. // Run the jig

    196.         BlockJig myJig = new BlockJig(tr, br, attInfo);

    197. if (myJig.Run() != PromptStatus.OK)

    198. return;

    199. // Commit changes if user accepted, otherwise discard

    200.         tr.Commit();

    201.       }

    202.     }

    203.   }

    204. }

    A few comments on this code:
    It's been refactored to make a single pass through the block definition to both create the block reference and collect the attribute information to store in our dictionary.
    The attribute information is now held in a class, which allows us to store more than just the position in our dictionary (without using multiple dictionaries), I went to the effort of exposing public properties for the various private members, as this is generally a good technique to use (if a little redundant, here).
    The dictionary now stores this attribute information against an ObjectId rather than the tag string. Roland made the excellent point that blocks can contain attributes with duplicate tags, so this is much safe. We also had to use the ObjectId of the AttributeReference, as later on inside the jig's Update() function we no longer have access to the AttributeDefinition.

    一些对于这些代码的评价

    它被重构用来定义一个块来创建块参照并收集属性信息存储在我们的字典。

    属性信息现在被约束在一个类中,它可以让我们存储更多的不仅仅是位置在我们的字典中(在不使用多个词典的情况下),我们努力为更多的私有成员暴漏他们的公有属性,因为这通常是一个良好的技术使用(但是在这有点多余)。

    字典存储现在针对的ObjectId,而不是标记字符串该属性信息。罗兰提出,块可以包含重复的标记属性非常好的一点,所以这是非常安全的。我们也可以用AttributeReference的的ObjectId,作为后来拖拽的更新()函数中我们不再有机会获得AttributeDefinition。

  • 相关阅读:
    将iso镜像转换为docker镜像
    Linux awk使用方法~~整理
    Linux sed使用方法
    Linux 环境变量梳理
    学习docker——命令总结
    Golang 字符串操作--使用strings、strconv包
    WebSocket实现一个聊天室
    学习WebSocket
    PHP面试题整理
    php使用gd库输出中文内容的图片
  • 原文地址:https://www.cnblogs.com/sinper/p/5062175.html
Copyright © 2011-2022 走看看