這可能使用其他Autocad .NET庫(而不是Interop庫)。但幸運的是,一個不排除另一個。
您需要引用包含下面的命名空間的庫:
using Autodesk.Autocad.ApplicationServices
using Autodesk.Autocad.EditorInput
using Autodesk.Autocad.DatabaseServices
(你會看到那些下載歐特克免費對象ARX庫):
您將需要從訪問Editor
一個autocad 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
,得到Editor
和GetSelection(Options, Filter)
的選項包含屬性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);
}
}
一個簡單的方法是'如果計數> 1,顯示消息並再次選擇'。 –