Repository navigation
Expand file tree
/
Copy pathdebug_acad_ipy_script.py
More file actions
122 lines (102 loc) · 3.21 KB
/
Copy pathdebug_acad_ipy_script.py
File metadata and controls
122 lines (102 loc) · 3.21 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""IronPython debug sample (AutoCAD). Host listens on 127.0.0.1:4567.
Must stay named *_ipy_script.py so ScriptExecutionProvider uses IronPython.
Do not import pydevd here -- AcadDevTool already called _enable_attach.
Attach with launch.json "Attach Host: IronPython" (debugpy, port 4567).
CPython debugpy stays on 5678.
IronPython does not run SetupAcad.py; add AutoCAD CLR refs before imports.
"""
import clr
clr.AddReference("accoremgd")
clr.AddReference("acdbmgd")
clr.AddReference("acmgd")
from Autodesk.AutoCAD.ApplicationServices.Core import Application
from Autodesk.AutoCAD.DatabaseServices import (
BlockTableRecord,
Entity,
LayerTableRecord,
OpenMode,
)
from Autodesk.AutoCAD.EditorInput import PromptStatus
def main():
doc = Application.DocumentManager.MdiActiveDocument
if doc is None:
print("No active document. Open a drawing, then run again.")
return
db = doc.Database
ed = doc.Editor
# Breakpoint here after VS Code is attached to 4567.
print("document:", doc.Name)
print("filename:", db.Filename)
loc = doc.LockDocument()
try:
tr = db.TransactionManager.StartTransaction()
try:
_dump_selection(ed, tr)
_dump_layers(db, tr)
_dump_model_space(db, tr)
tr.Commit()
except Exception:
tr.Abort()
raise
finally:
tr.Dispose()
finally:
loc.Dispose()
def _dump_selection(ed, tr):
result = ed.SelectImplied()
if result.Status != PromptStatus.OK or result.Value is None:
print("selection count: 0")
return
n = result.Value.Count
print("selection count:", n)
shown = 0
for selected in result.Value:
if selected is None:
continue
ent = tr.GetObject(selected.ObjectId, OpenMode.ForRead)
if not isinstance(ent, Entity):
continue
print(
" ",
ent.GetType().Name,
"handle=",
str(ent.Handle),
"layer=",
ent.Layer,
)
shown += 1
if shown >= 10:
print(" ... truncated")
break
def _dump_layers(db, tr):
table = tr.GetObject(db.LayerTableId, OpenMode.ForRead)
names = []
for layer_id in table:
rec = tr.GetObject(layer_id, OpenMode.ForRead)
if isinstance(rec, LayerTableRecord):
names.append(rec.Name)
names.sort()
print("layer count:", len(names))
for name in names[:15]:
print(" ", name)
if len(names) > 15:
print(" ... truncated")
def _dump_model_space(db, tr):
bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
model = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead)
counts = {}
total = 0
for obj_id in model:
ent = tr.GetObject(obj_id, OpenMode.ForRead)
if not isinstance(ent, Entity):
continue
type_name = ent.GetType().Name
counts[type_name] = counts.get(type_name, 0) + 1
total += 1
print("model space entities:", total)
types = list(counts.keys())
types.sort()
for type_name in types:
print(" ", type_name, counts[type_name])
if __name__ == "__main__":
main()