Skip to content

Commit a761ba5

Browse files
authored
Merge pull request #15 from AlvaroCarnicero/dev_AlvaroCarnicero
Add script to create text entities at the center of polylines in AutoCAD
2 parents e8c0e41 + b7e9ad8 commit a761ba5

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Many thanks to Chuong Ho for this awesome repository
2+
# Many thanks Cyril-Pop for the starting point in this script.
3+
4+
import clr
5+
clr.AddReference('acmgd')
6+
clr.AddReference('acdbmgd')
7+
clr.AddReference('accoremgd')
8+
9+
# Import references from AutoCAD
10+
from Autodesk.AutoCAD.Runtime import *
11+
from Autodesk.AutoCAD.ApplicationServices import *
12+
from Autodesk.AutoCAD.EditorInput import *
13+
from Autodesk.AutoCAD.DatabaseServices import *
14+
from Autodesk.AutoCAD.Geometry import *
15+
16+
# Global AutoCAD variables
17+
doc = Application.DocumentManager.MdiActiveDocument
18+
ed = doc.Editor
19+
db = doc.Database
20+
21+
# Layer names
22+
polyline_layer_name = "Aux-REVISION"
23+
text_layer_name = "I-WALL"
24+
25+
def polyline_center(polyline):
26+
"""
27+
Calculates the approximate geometric center of a polyline.
28+
"""
29+
extents = polyline.GeometricExtents
30+
center = Point3d(
31+
(extents.MinPoint.X + extents.MaxPoint.X) / 2,
32+
(extents.MinPoint.Y + extents.MaxPoint.Y) / 2,
33+
(extents.MinPoint.Z + extents.MaxPoint.Z) / 2
34+
)
35+
return center
36+
37+
def create_text(btr, trans, position, text_value, text_layer):
38+
"""
39+
Creates a text entity in AutoCAD and adds it to the model space.
40+
"""
41+
text = DBText()
42+
text.Position = position
43+
text.Height = 3 # Text height
44+
text.TextString = text_value # Text content
45+
text.Layer = text_layer # Assign the text to the specified layer
46+
btr.AppendEntity(text)
47+
trans.AddNewlyCreatedDBObject(text, True)
48+
return text
49+
50+
def main():
51+
output = [] # List to store created texts
52+
errors = [] # List to record errors
53+
54+
with doc.LockDocument(): # Lock the document
55+
with db.TransactionManager.StartTransaction() as t:
56+
# Access the model space
57+
bt = t.GetObject(db.BlockTableId, OpenMode.ForRead)
58+
btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
59+
60+
# Iterate through the entities in model space
61+
for objectid in btr:
62+
blkRef = t.GetObject(objectid, OpenMode.ForRead)
63+
if isinstance(blkRef, Polyline) and blkRef.Layer == polyline_layer_name:
64+
try:
65+
# Get the geometric center of the polyline
66+
center = polyline_center(blkRef)
67+
68+
# Create a unique text entity at the center of the polyline
69+
text = create_text(btr, t, center, f"Text-{len(output) + 1}", text_layer_name)
70+
output.append(text)
71+
except Exception as e:
72+
errors.append((blkRef, str(e)))
73+
ed.WriteMessage(f"\nError processing polyline: {e}")
74+
else:
75+
errors.append(blkRef)
76+
77+
# Commit the transaction
78+
t.Commit()
79+
80+
# Results
81+
ed.WriteMessage(f"\n{len(output)} texts were created.")
82+
if errors:
83+
ed.WriteMessage(f"\nErrors found: {len(errors)}")
84+
85+
if __name__ == "__main__":
86+
main()

0 commit comments

Comments
 (0)