graphics.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # Created: 25.03.2011
  2. # Copyright (c) 2011-2018, Manfred Moitzi
  3. # License: MIT License
  4. from typing import TYPE_CHECKING, Tuple
  5. from ezdxf.legacy import graphics as r12graphics
  6. from ezdxf.lldxf.extendedtags import ExtendedTags
  7. from ezdxf.lldxf.attributes import DXFAttr, DXFAttributes, DefSubclass, XType
  8. from ezdxf.tools.rgb import int2rgb, rgb2int
  9. from ezdxf.tools import float2transparency, transparency2float
  10. if TYPE_CHECKING:
  11. from ezdxf.eztypes import Auditor
  12. none_subclass = DefSubclass(None, {
  13. 'handle': DXFAttr(5),
  14. 'owner': DXFAttr(330), # Soft-pointer ID/handle to owner BLOCK_RECORD object
  15. })
  16. entity_subclass = DefSubclass('AcDbEntity', {
  17. 'paperspace': DXFAttr(67, default=0), # 0 .. modelspace, 1 .. paperspace
  18. 'layer': DXFAttr(8, default='0'), # layername as string
  19. 'linetype': DXFAttr(6, default='BYLAYER'), # linetype as string, special names BYLAYER/BYBLOCK
  20. 'lineweight': DXFAttr(370), # Stored and moved around as a 16-bit integer
  21. # Line weight in mm times 100 (e.g. 0.13mm = 13). Smallest line weight is 13 and biggest line weight is 200, values
  22. # outside this range prevents AutoCAD from loading the file.
  23. # Special values:
  24. # LINEWEIGHT_BYLAYER = -1
  25. # LINEWEIGHT_BYBLOCK = -2
  26. # LINEWEIGHT_DEFAULT = -3
  27. 'ltscale': DXFAttr(48, default=1.0), # linetype scale
  28. 'invisible': DXFAttr(60, default=0), # invisible .. 1, visible .. 0
  29. 'color': DXFAttr(62, default=256), # dxf color index, 0 .. BYBLOCK, 256 .. BYLAYER
  30. 'true_color': DXFAttr(420, dxfversion='AC1018'), # true color as 0x00RRGGBB 24-bit value
  31. 'color_name': DXFAttr(430, dxfversion='AC1018'), # color name as string
  32. 'transparency': DXFAttr(440, dxfversion='AC1018'), # transparency value 0x020000TT 0 = fully transparent / 255 = opaque
  33. 'shadow_mode': DXFAttr(284, dxfversion='AC1021'), # shadow_mode
  34. # 0 = Casts and receives shadows
  35. # 1 = Casts shadows
  36. # 2 = Receives shadows
  37. # 3 = Ignores shadows
  38. })
  39. # noinspection PyUnresolvedReferences
  40. class ModernGraphicEntityExtension:
  41. __slots__ = ()
  42. @property
  43. def rgb(self) -> Tuple[int, int, int]:
  44. return int2rgb(self.get_dxf_attrib('true_color'))
  45. @rgb.setter # line.rgb = (12, 34, 56)
  46. def rgb(self, rgb: Tuple[int, int, int])->None:
  47. self.set_dxf_attrib('true_color', rgb2int(rgb))
  48. @property
  49. def transparency(self) -> float:
  50. return transparency2float(self.get_dxf_attrib('transparency'))
  51. @transparency.setter # line.transparency = 0.50
  52. def transparency(self, transparency: float) -> None:
  53. # 0.0 = opaque & 1.0 if 100% transparent
  54. self.set_dxf_attrib('transparency', float2transparency(transparency))
  55. class ModernGraphicEntity(r12graphics.GraphicEntity, ModernGraphicEntityExtension):
  56. __slots__ = ()
  57. """
  58. Default graphic entity wrapper, allows access to following dxf attributes:
  59. - handle
  60. - owner handle
  61. - layer
  62. - linetype
  63. - lineweight
  64. - color
  65. - paperspace
  66. - ltscale
  67. - invisible
  68. - true_color (as int), requires DXF Version AC1018 (AutoCAD R2004)
  69. - color_name, requires DXF Version AC1018 (AutoCAD R2004)
  70. - transparency, requires DXF Version AC1018 (AutoCAD R2004)
  71. - shadow_mode, requires DXF Version AC1021 (AutoCAD R2007)
  72. Wrapper for all unsupported graphic entities.
  73. """
  74. DXFATTRIBS = DXFAttributes(none_subclass, entity_subclass)
  75. def audit(self, auditor: 'Auditor') -> None:
  76. super(ModernGraphicEntity, self).audit(auditor)
  77. auditor.check_pointer_target_exists(self, zero_pointer_valid=False)
  78. _LINETEMPLATE = """0
  79. LINE
  80. 5
  81. 0
  82. 330
  83. 0
  84. 100
  85. AcDbEntity
  86. 8
  87. 0
  88. 100
  89. AcDbLine
  90. 10
  91. 0.0
  92. 20
  93. 0.0
  94. 30
  95. 0.0
  96. 11
  97. 1.0
  98. 21
  99. 1.0
  100. 31
  101. 1.0
  102. """
  103. line_subclass = DefSubclass('AcDbLine', {
  104. 'start': DXFAttr(10, xtype=XType.any_point),
  105. 'end': DXFAttr(11, xtype=XType.any_point),
  106. 'thickness': DXFAttr(39, default=0),
  107. 'extrusion': DXFAttr(210, xtype=XType.point3d, default=(0.0, 0.0, 1.0)),
  108. })
  109. class Line(r12graphics.Line, ModernGraphicEntityExtension):
  110. __slots__ = ()
  111. TEMPLATE = ExtendedTags.from_text(_LINETEMPLATE)
  112. DXFATTRIBS = DXFAttributes(none_subclass, entity_subclass, line_subclass)
  113. _POINT_TPL = """0
  114. POINT
  115. 5
  116. 0
  117. 330
  118. 0
  119. 100
  120. AcDbEntity
  121. 8
  122. 0
  123. 100
  124. AcDbPoint
  125. 10
  126. 0.0
  127. 20
  128. 0.0
  129. 30
  130. 0.0
  131. """
  132. point_subclass = DefSubclass('AcDbPoint', {
  133. 'location': DXFAttr(10, xtype=XType.any_point),
  134. 'thickness': DXFAttr(39, default=None),
  135. 'extrusion': DXFAttr(210, xtype=XType.point3d, default=(0.0, 0.0, 1.0)),
  136. })
  137. class Point(r12graphics.Point, ModernGraphicEntityExtension):
  138. __slots__ = ()
  139. TEMPLATE = ExtendedTags.from_text(_POINT_TPL)
  140. DXFATTRIBS = DXFAttributes(none_subclass, entity_subclass, point_subclass)
  141. _CIRCLE_TPL = """0
  142. CIRCLE
  143. 5
  144. 0
  145. 330
  146. 0
  147. 100
  148. AcDbEntity
  149. 8
  150. 0
  151. 100
  152. AcDbCircle
  153. 10
  154. 0.0
  155. 20
  156. 0.0
  157. 30
  158. 0.0
  159. 40
  160. 1.0
  161. """
  162. circle_subclass = DefSubclass('AcDbCircle', {
  163. 'center': DXFAttr(10, xtype=XType.any_point),
  164. 'radius': DXFAttr(40),
  165. 'thickness': DXFAttr(39, default=0.0),
  166. 'extrusion': DXFAttr(210, xtype=XType.point3d, default=(0.0, 0.0, 1.0)),
  167. })
  168. class Circle(r12graphics.Circle, ModernGraphicEntityExtension):
  169. __slots__ = ()
  170. TEMPLATE = ExtendedTags.from_text(_CIRCLE_TPL)
  171. DXFATTRIBS = DXFAttributes(none_subclass, entity_subclass, circle_subclass)
  172. _ARC_TPL = """0
  173. ARC
  174. 5
  175. 0
  176. 330
  177. 0
  178. 100
  179. AcDbEntity
  180. 8
  181. 0
  182. 100
  183. AcDbCircle
  184. 10
  185. 0.0
  186. 20
  187. 0.0
  188. 30
  189. 0.0
  190. 40
  191. 1.0
  192. 100
  193. AcDbArc
  194. 50
  195. 0
  196. 51
  197. 360
  198. """
  199. arc_subclass = DefSubclass('AcDbArc', {
  200. 'start_angle': DXFAttr(50),
  201. 'end_angle': DXFAttr(51),
  202. })
  203. class Arc(r12graphics.Arc, ModernGraphicEntityExtension):
  204. __slots__ = ()
  205. TEMPLATE = ExtendedTags.from_text(_ARC_TPL)
  206. DXFATTRIBS = DXFAttributes(none_subclass, entity_subclass, circle_subclass, arc_subclass)
  207. class SeqEnd(r12graphics.SeqEnd):
  208. __slots__ = ()
  209. TEMPLATE = ExtendedTags.from_text(" 0\nSEQEND\n 5\n0\n330\n 0\n100\nAcDbEntity\n")
  210. DXFATTRIBS = DXFAttributes(none_subclass, entity_subclass)
  211. _SHAPE_TPL = """0
  212. SHAPE
  213. 5
  214. 0
  215. 330
  216. 0
  217. 100
  218. AcDbEntity
  219. 8
  220. 0
  221. 100
  222. AcDbShape
  223. 10
  224. 0.0
  225. 20
  226. 0.0
  227. 30
  228. 0.0
  229. 40
  230. 1.0
  231. 2
  232. NAME
  233. 50
  234. 0.0
  235. 41
  236. 1.0
  237. 51
  238. 0.0
  239. """
  240. shape_subclass = DefSubclass('AcDbShape', {
  241. 'thickness': DXFAttr(39, default=0.0),
  242. 'insert': DXFAttr(10, xtype=XType.any_point),
  243. 'size': DXFAttr(40),
  244. 'name': DXFAttr(2),
  245. 'rotation': DXFAttr(50, default=0.0),
  246. 'xscale': DXFAttr(41, default=1.0),
  247. 'oblique': DXFAttr(51, default=0.0),
  248. 'extrusion': DXFAttr(210, xtype=XType.point3d, default=(0.0, 0.0, 1.0)),
  249. })
  250. # SHAPE is not tested with real world DXF drawings!
  251. class Shape(ModernGraphicEntity):
  252. __slots__ = ()
  253. TEMPLATE = ExtendedTags.from_text(_SHAPE_TPL)
  254. DXFATTRIBS = DXFAttributes(none_subclass, entity_subclass, shape_subclass)