绝情一剑 发表于 2020-3-1 10:18:00

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

动态块内的线性参数想通过选择过滤后赋值,但是找不到过滤参数,请问动态块的线性参数怎么过滤?AddAllowedClass怎么过滤?

qq1254582201 发表于 2020-3-4 21:19:00

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace DYCS_20200302
{
    public class Commands
    {
      
      static public void DynamicBlockProps()
      {
            Document doc =
            Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptStringOptions pso =
            new PromptStringOptions(
                "\nEnter dynamic block name or enter to select: "
            );
            pso.AllowSpaces = true;
            PromptResult pr = ed.GetString(pso);
            if (pr.Status != PromptStatus.OK)
                return;
            Transaction tr =
            db.TransactionManager.StartTransaction();
            using (tr)
            {
                BlockReference br = null;
                // If a null string was entered allow entity selection
                if (pr.StringResult == "")
                {
                  // Select a block reference
                  PromptEntityOptions peo =
                      new PromptEntityOptions(
                        "\nSelect dynamic block reference: "
                      );
                  peo.SetRejectMessage("\nEntity is not a block.");
                  peo.AddAllowedClass(typeof(BlockReference), false);
                  PromptEntityResult per =
                      ed.GetEntity(peo);
                  if (per.Status != PromptStatus.OK)
                        return;
                  // Access the selected block reference
                  br =
                      tr.GetObject(
                        per.ObjectId,
                        OpenMode.ForRead
                      ) as BlockReference;
                }
                else
                {
                  // Otherwise we look up the block by name
                  BlockTable bt =
                      tr.GetObject(
                        db.BlockTableId,
                        OpenMode.ForRead) as BlockTable;
                  if (!bt.Has(pr.StringResult))
                  {
                        ed.WriteMessage(
                        "\nBlock \"" + pr.StringResult + "\" does not exist."
                        );
                        return;
                  }
                  // Create a new block reference referring to the block
                  br =
                      new BlockReference(
                        new Point3d(),
                        bt
                      );
                }
                BlockTableRecord btr =
                  (BlockTableRecord)tr.GetObject(
                  br.DynamicBlockTableRecord,
                  OpenMode.ForRead
                  );
                // Call our function to display the block properties
                DisplayDynBlockProperties(ed, br, btr.Name);
                // Committing is cheaper than aborting
                tr.Commit();
            }
      }
      private static void DisplayDynBlockProperties(
          Editor ed, BlockReference br, string name
      )
      {
            // Only continue is we have a valid dynamic block
            if (br != null && br.IsDynamicBlock)
            {
                ed.WriteMessage(
                  "\nDynamic properties for \"{0}\"\n",
                  name
                );
                // Get the dynamic block's property collection
                DynamicBlockReferencePropertyCollection pc =
                  br.DynamicBlockReferencePropertyCollection;
                // Loop through, getting the info for each property
                foreach (DynamicBlockReferenceProperty prop in pc)
                {
                  // Start with the property name, type and description
                  ed.WriteMessage(
                      "\nProperty: \"{0}\" : {1}",
                      prop.PropertyName,
                      prop.UnitsType
                  );
                  if (prop.Description != "")
                        ed.WriteMessage(
                        "\nDescription: {0}",
                        prop.Description
                        );
                  // Is it read-only?
                  if (prop.ReadOnly)
                        ed.WriteMessage(" (Read Only)");
                  // Get the allowed values, if it's constrained
                  bool first = true;
                  foreach (object value in prop.GetAllowedValues())
                  {
                        ed.WriteMessage(
                        (first ? "\nAllowed values: [" : ", ")
                        );
                        ed.WriteMessage("\"{0}\"", value);
                        first = false;
                  }
                  if (!first)
                        ed.WriteMessage("]");
                  // And finally the current value
                  ed.WriteMessage(
                      "\nCurrent value: \"{0}\"\n",
                      prop.Value
                  );
                }
            }
      }
    }
}
页: [1]
查看完整版本: 怎么通过选择来过滤动态块的线性参数?