How can I get polyline color?

523 views
Skip to first unread message

Henry Zhang

unread,
Jul 15, 2020, 3:23:52 AM7/15/20
to python-ezdxf
Hi, 

I can get polyline from the attached dxf file with the following code. How can I get ployline color from the dxf file as well? Attached a screenshot from Autodesk Viewer, and there are three colors on it. 


import ezdxf
from ezdxf import DXFStructureError

try:
doc = ezdxf.readfile("w3b1-0552-3250-PP-20200621.dxf")
msp = doc.modelspace()
polylines = msp.query('POLYLINE')
for polyline in polylines:
print('Polyline #{}'.format(polyline.dxf.handle))
for i, location in enumerate(polyline.points()):
print('Point at index {}: {}'.format(i, location))
except DXFStructureError as error:
print(error)


Regards,

Henry
w3b1-0552-3250-PP-20200621.dxf
polylines.png

Manfred Moitzi

unread,
Jul 18, 2020, 12:45:39 AM7/18/20
to Henry Zhang, python-ezdxf
import ezdxf
from ezdxf.tools.rgb import DXF_DEFAULT_COLORS, int2rgb

doc = ezdxf.readfile('w3b1-0552-3250-PP-20200621.dxf')
msp = doc.modelspace()
polylines = msp.query('POLYLINE')
for polyline in polylines:
    print(str(polyline))
# AutoCAD Color Index
aci = polyline.dxf.color
print(f'Color Index: {aci}')
# RGB values of default colors are not officially documented,
# but an accurate translation table is included in ezdxf
rgb24 = DXF_DEFAULT_COLORS[aci] # 24 bit value RRRRRRRRGGGGGGGGBBBBBBBB
print(f'RGB Hex Value: #{rgb24:06X}')
r, g, b = int2rgb(rgb24)
print(f'RGB Channel Values: R={r:02X} G={g:02X} b={b:02X}')
print()
The ACI color index 7 is special: white on dark background and black on light background.

Best Regards,
Manfred

--
You received this message because you are subscribed to the Google Groups "python-ezdxf" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-ezdxf...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-ezdxf/caa0fd04-ce20-4bd3-9c67-8435bb9a24eao%40googlegroups.com.

Manfred Moitzi

unread,
Jul 18, 2020, 2:20:00 AM7/18/20
to python-ezdxf
Added two how-tos to the documentation:

How to get/set colors, which also shows how to handle BYLAYER values: https://ezdxf.mozman.at/docs/howto/content.html#get-set-entity-color  


Best regards,
Manfred
Reply all
Reply to author
Forward
0 new messages