我试图从我的绘图和实际计数中获取所有块和动态块。但我有一个奇怪的问题,每个块的计数比预期的多1。获取正常BlockReferences的计数我正在计算重复项。但是对于动态块我得到的实际数量 with var blockCount = anonymousBtr.GetBlockReferenceIds(true,true).Count; 我尝试使用普通块:
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;
}
有没有办法获得所有的块和那里数?在我放弃之前,我现在尝试了3天......也许有人可以帮助我。 感谢
答案 0 :(得分: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();
}
}