forked from chuongmep/CadPythonShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountBlock.py
More file actions
32 lines (32 loc) · 1.15 KB
/
countBlock.py
File metadata and controls
32 lines (32 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#Many thanks Cyril-Pop
import clr
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')
# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
doc = Application.DocumentManager.MdiActiveDocument
ed = doc.Editor
db = doc.Database
# Write Code Below
all_blkName = []
with doc.LockDocument():
with doc.Database as db:
with db.TransactionManager.StartTransaction() as t:
bt = t.GetObject(db.BlockTableId,OpenMode.ForRead)
btr = t.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForRead)
for objectid in btr:
blkRef = t.GetObject(objectid, OpenMode.ForRead)
if isinstance(blkRef, BlockReference):
if blkRef.IsDynamicBlock:
block = t.GetObject(blkRef.DynamicBlockTableRecord, OpenMode.ForRead)
all_blkName.append(block.Name)
else:
all_blkName.append(blkRef.Name)
resultcount = ["{} - total:{}".format(x, all_blkName.count(x)) for x in set(all_blkName)]
print("\n".join(resultcount))
t.Commit()