2016-10-27 51 views
0

我試圖從我的繪圖和實際計數獲取所有塊和動態塊。但即時通訊有一個奇怪的問題,其中每個塊的計數比預期多1。即時計數重複的正常BlockReferences的計數。但是對於動態塊即時獲取實際的計數 var blockCount = anonymousBtr.GetBlockReferenceIds(true,true).Count; 什麼即時試圖爲正常塊段: C#AutoCAD獲取塊和動態塊計數

public static void GetShelfBlock() { 
      var acDoc = Application.DocumentManager.MdiActiveDocument; 
      var acDb = acDoc.Database; 
      var dataCollection = new List<DataModel>(); 

      using (var tr = acDb.TransactionManager.StartTransaction()) { 

       try { 
        var entClass = RXObject.GetClass(typeof(Entity)); 
        var modelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(acDb); 
        var modelSpace = (BlockTableRecord)tr.GetObject(modelSpaceId, OpenMode.ForRead); 

        foreach (var id in modelSpace) { 
         if (!id.ObjectClass.IsDerivedFrom(entClass)) { 
          continue; 
         } 

         var ent = (Entity)tr.GetObject(id, OpenMode.ForRead); 
         if (!ent.GetType().ToString().Contains(typeof(BlockReference).Name)) { 
       continue; 
         } 

         var reference = ent as BlockReference; 
         if (reference == null) { 
          continue; 
         } 

         var blockReference = reference; 
         if (CheckPattern(blockReference.Name)) { 
          dataCollection.Add(PrepareShelfObject(blockReference)); 
         } 
        } 
        tr.Commit(); 
        tr.Dispose(); 

        dataCollection.AddRange(GetDynamicBlocks(SearchPatterns)); 
        if (dataCollection.Count > 0) { 
         Serialize(dataCollection, DataType.Regale); 
        } 
       } 
       catch (Exception ex) { 
        acDoc.Editor.WriteMessage(ex.Message); 
       } 
      } 
     } 

什麼即時試圖爲那裏即時得到使用var blockCount計數動態塊= anonymousBtr.GetBlockReferenceIds(true, true).Count;

public static List<DataModel> GetDynamicBlocks() { 
      var db = Application.DocumentManager.MdiActiveDocument.Database; 
      var dynamicBlockList = new List<DataModel>(); 
      var tempList = new List<object>(); 
      using (var trans = db.TransactionManager.StartTransaction()) { 
       var bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead); 

       foreach (var btrId in bt) { 

        var btr = (BlockTableRecord)trans.GetObject(btrId, OpenMode.ForRead); 
        if (!btr.IsDynamicBlock) { 
         continue; 
        } 

        var anonymousIds = btr.GetAnonymousBlockIds(); 
        var dynBlockRefs = new ObjectIdCollection(); 

        foreach (ObjectId anonymousBtrId in anonymousIds) { 
         var anonymousBtr = (BlockTableRecord)trans.GetObject(anonymousBtrId, OpenMode.ForRead); 
         var blockRefIds = anonymousBtr.GetBlockReferenceIds(true, true); 
         foreach (ObjectId id in blockRefIds) { 
          dynBlockRefs.Add(id); 
         } 
        } 

        if (searchPattern.Any(btr.Name.StartsWith)) { 
         dynamicBlockList.Add(PrepareDynObject(btr)); 
        } 
       } 
       trans.Commit(); 
       trans.Dispose(); 
      } 
      return dynamicBlockList; 
     } 

有沒有辦法讓所有的塊並有計數?在放棄之前,我現在試了三天......也許有人可以幫助我。 謝謝

回答

2

這是一種使用Linq的方法。

public void ListBlocks() 
    { 
     var doc = Application.DocumentManager.MdiActiveDocument; 
     var db = doc.Database; 
     var ed = doc.Editor; 

     using (var tr = db.TransactionManager.StartOpenCloseTransaction()) 
     { 
      var modelSpace = (BlockTableRecord)tr.GetObject(
       SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead); 

      var brclass = RXObject.GetClass(typeof(BlockReference)); 

      var blocks = modelSpace 
       .Cast<ObjectId>() 
       .Where(id => id.ObjectClass == brclass) 
       .Select(id => (BlockReference)tr.GetObject(id, OpenMode.ForRead)) 
       .GroupBy(br => ((BlockTableRecord)tr.GetObject(
        br.DynamicBlockTableRecord, OpenMode.ForRead)).Name); 

      foreach (var group in blocks) 
      { 
       ed.WriteMessage($"\n{group.Key}: {group.Count()}"); 
      } 
      tr.Commit(); 
     } 
    }