明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 725|回复: 1

怎么通过选择来过滤动态块的线性参数?

[复制链接]
发表于 2020-3-1 10:18 | 显示全部楼层 |阅读模式
动态块内的线性参数想通过选择过滤后赋值,但是找不到过滤参数,请问动态块的线性参数怎么过滤?AddAllowedClass怎么过滤?
发表于 2020-3-4 21:19 | 显示全部楼层
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;

  6. namespace DYCS_20200302
  7. {
  8.     public class Commands
  9.     {
  10.         [CommandMethod("DBP")]
  11.         static public void DynamicBlockProps()
  12.         {
  13.             Document doc =
  14.               Application.DocumentManager.MdiActiveDocument;
  15.             Database db = doc.Database;
  16.             Editor ed = doc.Editor;

  17.             PromptStringOptions pso =
  18.               new PromptStringOptions(
  19.                 "\nEnter dynamic block name or enter to select: "
  20.               );

  21.             pso.AllowSpaces = true;
  22.             PromptResult pr = ed.GetString(pso);

  23.             if (pr.Status != PromptStatus.OK)
  24.                 return;

  25.             Transaction tr =
  26.               db.TransactionManager.StartTransaction();
  27.             using (tr)
  28.             {
  29.                 BlockReference br = null;

  30.                 // If a null string was entered allow entity selection

  31.                 if (pr.StringResult == "")
  32.                 {
  33.                     // Select a block reference

  34.                     PromptEntityOptions peo =
  35.                       new PromptEntityOptions(
  36.                         "\nSelect dynamic block reference: "
  37.                       );

  38.                     peo.SetRejectMessage("\nEntity is not a block.");
  39.                     peo.AddAllowedClass(typeof(BlockReference), false);

  40.                     PromptEntityResult per =
  41.                       ed.GetEntity(peo);

  42.                     if (per.Status != PromptStatus.OK)
  43.                         return;

  44.                     // Access the selected block reference

  45.                     br =
  46.                       tr.GetObject(
  47.                         per.ObjectId,
  48.                         OpenMode.ForRead
  49.                       ) as BlockReference;
  50.                 }
  51.                 else
  52.                 {
  53.                     // Otherwise we look up the block by name

  54.                     BlockTable bt =
  55.                       tr.GetObject(
  56.                         db.BlockTableId,
  57.                         OpenMode.ForRead) as BlockTable;

  58.                     if (!bt.Has(pr.StringResult))
  59.                     {
  60.                         ed.WriteMessage(
  61.                           "\nBlock \"" + pr.StringResult + "\" does not exist."
  62.                         );
  63.                         return;
  64.                     }

  65.                     // Create a new block reference referring to the block

  66.                     br =
  67.                       new BlockReference(
  68.                         new Point3d(),
  69.                         bt[pr.StringResult]
  70.                       );
  71.                 }

  72.                 BlockTableRecord btr =
  73.                   (BlockTableRecord)tr.GetObject(
  74.                     br.DynamicBlockTableRecord,
  75.                     OpenMode.ForRead
  76.                   );

  77.                 // Call our function to display the block properties

  78.                 DisplayDynBlockProperties(ed, br, btr.Name);

  79.                 // Committing is cheaper than aborting

  80.                 tr.Commit();
  81.             }
  82.         }

  83.         private static void DisplayDynBlockProperties(
  84.           Editor ed, BlockReference br, string name
  85.         )
  86.         {
  87.             // Only continue is we have a valid dynamic block

  88.             if (br != null && br.IsDynamicBlock)
  89.             {
  90.                 ed.WriteMessage(
  91.                   "\nDynamic properties for \"{0}\"\n",
  92.                   name
  93.                 );

  94.                 // Get the dynamic block's property collection

  95.                 DynamicBlockReferencePropertyCollection pc =
  96.                   br.DynamicBlockReferencePropertyCollection;

  97.                 // Loop through, getting the info for each property

  98.                 foreach (DynamicBlockReferenceProperty prop in pc)
  99.                 {
  100.                     // Start with the property name, type and description

  101.                     ed.WriteMessage(
  102.                       "\nProperty: \"{0}\" : {1}",
  103.                       prop.PropertyName,
  104.                       prop.UnitsType
  105.                     );

  106.                     if (prop.Description != "")
  107.                         ed.WriteMessage(
  108.                           "\n  Description: {0}",
  109.                           prop.Description
  110.                         );

  111.                     // Is it read-only?

  112.                     if (prop.ReadOnly)
  113.                         ed.WriteMessage(" (Read Only)");

  114.                     // Get the allowed values, if it's constrained

  115.                     bool first = true;

  116.                     foreach (object value in prop.GetAllowedValues())
  117.                     {
  118.                         ed.WriteMessage(
  119.                           (first ? "\n  Allowed values: [" : ", ")
  120.                         );
  121.                         ed.WriteMessage("\"{0}\"", value);

  122.                         first = false;
  123.                     }
  124.                     if (!first)
  125.                         ed.WriteMessage("]");

  126.                     // And finally the current value

  127.                     ed.WriteMessage(
  128.                       "\n  Current value: \"{0}\"\n",
  129.                       prop.Value
  130.                     );
  131.                 }
  132.             }
  133.         }
  134.     }
  135. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-6-18 20:20 , Processed in 0.121957 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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