这可以使用其他 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);
}
}