1

我在选择目标时遇到了一些麻烦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.

请帮我解决这个问题。

4

2 回答 2

1

这可以使用其他 Autocad .NET 库(而不是 Interop 库)来实现。但幸运的是,一个不排斥另一个。

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

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

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

您将需要Editor从 AutoCAD访问Document. 根据您显示的代码,您可能正在处理AcadDocument文档。因此,要将 a 转换AcadDocument为 a 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,得到Editor,和GetSelection(Options, Filter)

Options 包含一个属性SingleOnly和一个SinglePickInSpace. 将其设置为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);
    }
}
于 2013-06-24T16:27:24.360 回答
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
  }
}
于 2013-07-24T01:35:09.020 回答