如何通过选择集只获得一个acadobject

时间:2013-06-24 09:29:22

标签: c# autocad

我在选择目标acadObject时遇到了一些麻烦。我通过selectionset.SelectonScreen方法获得输入。

在这里,我可以根据我的过滤条件从模型空间获取更多的对象。但我只需要用户提供一个对象。

我在下面提到了我的代码:

AcadSelectionSet selset= null;
selset=currDoc.selectionset.add("Selset");
short[] ftype=new short[1];
object[] fdata=new object[1];
ftype[0]=2;//for select the blockreference
fdata[0]=blkname;
selset.selectOnScreen ftype,fdata;  // Here i can select any no. of blocks according to filter value but i need only one block reference.

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

可以使用其他Autocad .NET库(而不是Interop库)。但幸运的是,一个人不排除另一个。

您需要引用包含以下命名空间的库:

using Autodesk.Autocad.ApplicationServices
using Autodesk.Autocad.EditorInput
using Autodesk.Autocad.DatabaseServices

(您可以从Autodesk免费下载Object Arx库):

您需要从autocad Editor访问Document。 根据您显示的代码,您可能正在使用AcadDocument文档。 因此,要将AcadDocument转换为Document,请执行以下操作:

//These are extension methods and must be in a static class
//Will only work if Doc is saved at least once (has full name) - if document is new, the name will be 
public static Document GetAsAppServicesDoc(this IAcadDocument Doc)
    {
        return Application.DocumentManager.OfType<Document>().First(D => D.Name == Doc.FullOrNewName());
    }

 public static string FullOrNewName(this IAcadDocument Doc)
    {
        if (Doc.FullName == "")
            return Doc.Name;
        else
            return Doc.FullName;
    }

获得Document后,获取EditorGetSelection(Options, Filter)

选项包含属性SingleOnlySinglePickInSpace。将其设置为true可以满足您的需求。 (试着看看哪个更好用)

//Seleciton options, with single selection
PromptSelectionOptions Options = new PromptSelectionOptions();
Options.SingleOnly = true;
Options.SinglePickInSpace = true;

//This is the filter for blockreferences
SelectionFilter Filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "INSERT") });


//calls the user selection
PromptSelectionResult Selection = Document.Editor.GetSelection(Options, Filter);

if (Selection.Status == PromptStatus.OK)
{
    using (Transaction Trans = Document.Database.TransactionManager.StartTransaction())
    {
        //This line returns the selected items
       AcadBlockReference SelectedRef = (AcadBlockReference)(Trans.GetObject(Selection.Value.OfType<SelectedObject>().First().ObjectId, OpenMode.ForRead).AcadObject);
    }
}

答案 1 :(得分:1)

这是autocad开发者帮助的直接引用

http://docs.autodesk.com/ACD/2013/ENU/index.html?url=files/GUID-CBECEDCF-3B4E-4DF3-99A0-47103D10DADD.htm,topicNumber=d30e724932

AutoCAD .NET API有大量文档。

你需要

[assembly: CommandClass(typeof(namespace.class))]

如果希望能够在NetLoad .dll之后从命令行调用此命令(如果它是classLibrary),则在命名空间上方

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;

[CommandMethod("SelectObjectsOnscreen")]
public static void SelectObjectsOnscreen()
{
  // Get the current document and database
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;

  // Start a transaction
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // Request for objects to be selected in the drawing area
      PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();

      // If the prompt status is OK, objects were selected
      if (acSSPrompt.Status == PromptStatus.OK)
      {
          SelectionSet acSSet = acSSPrompt.Value;

          // Step through the objects in the selection set
          foreach (SelectedObject acSSObj in acSSet)
          {
              // Check to make sure a valid SelectedObject object was returned
              if (acSSObj != null)
              {
                  // Open the selected object for write
                  Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,
                                                   OpenMode.ForWrite) as Entity;

                  if (acEnt != null)
                  {
                      // Change the object's color to Green
                      acEnt.ColorIndex = 3;
                  }
              }
          }

          // Save the new object to the database
          acTrans.Commit();
      }

      // Dispose of the transaction
  }
}