2013-06-24 74 views
0

我有一些麻煩,選擇目標acadObject。我通過selectionset.SelectonScreen方法得到輸入。如何獲得通過選擇集只有一個acadobject

在這裏,我可以根據我的過濾條件從模型空間中獲取更多數量的對象。但我只需要來自用戶的一個對象。

這裏我提到我下面的代碼:

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. 

請幫我解決這個問題。

+0

一個簡單的方法是'如果計數> 1,顯示消息並再次選擇'。 –

回答

1

這可能使用其他Autocad .NET庫(而不是Interop庫)。但幸運的是,一個不排除另一個。

您需要引用包含下面的命名空間的庫:

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

(你會看到那些下載歐特克免費對象ARX庫):

您將需要從訪問Editor一個autocad Document。按照您顯示的代碼,您可能正在使用AcadDocument文檔。 因此,要轉變一個AcadDocumentDocument,做到這一點:

//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); 
    } 
} 
+0

這不是一個乾淨的解決方案,但這是我找到的。我討厭與這些Arx圖書館合作,因爲這些交易和複雜的過濾器,但有時他們會更好地工作。 –

+0

您的解決方案可能是OP的最佳解決方案,但是您的術語不正確。 「ARX」是嚴格的本地C++。託管API的正確術語是「AutoCAD .NET API」,而本機C++ API則是「ObjectARX」。 –

+0

好....直到現在,我永遠無法分辨差異。 Autodesk對這些問題的記錄非常差。 –

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 
    } 
}