graphics.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # Created: 25.03.2011
  2. # Copyright (c) 2011-2018, Manfred Moitzi
  3. # License: MIT License
  4. from typing import TYPE_CHECKING
  5. from ezdxf.lldxf.extendedtags import ExtendedTags
  6. from ezdxf.lldxf.attributes import DXFAttr, DXFAttributes, DefSubclass, XType
  7. from ezdxf.dxfentity import DXFEntity
  8. if TYPE_CHECKING:
  9. from ezdxf.eztypes import Auditor
  10. def make_attribs(additional: dict = None) -> DXFAttributes:
  11. dxfattribs = {
  12. 'handle': DXFAttr(5),
  13. 'layer': DXFAttr(8, default='0'), # layer name as string, mandatory according to the DXF Reference
  14. 'linetype': DXFAttr(6, default='BYLAYER'), # linetype as string, special names BYLAYER/BYBLOCK
  15. 'color': DXFAttr(62, default=256), # dxf color index, 0 .. BYBLOCK, 256 .. BYLAYER
  16. 'thickness': DXFAttr(39, default=0), # thickness of 2D elements
  17. 'paperspace': DXFAttr(67, default=0), # 0=modelspace; 1=paperspace
  18. 'extrusion': DXFAttr(210, xtype=XType.point3d, default=(0.0, 0.0, 1.0)), # Z-axis of OCS (Object-Coordinate-System)
  19. }
  20. if additional is not None:
  21. dxfattribs.update(additional)
  22. return DXFAttributes(DefSubclass(None, dxfattribs))
  23. class GraphicEntity(DXFEntity):
  24. __slots__ = ()
  25. """ Default graphic entity wrapper, allows access to following dxf attributes:
  26. - handle
  27. - layer
  28. - linetype
  29. - color
  30. - paperspace
  31. - extrusion
  32. Wrapper for all unsupported graphic entities.
  33. """
  34. DXFATTRIBS = make_attribs()
  35. def audit(self, auditor: 'Auditor') -> None:
  36. auditor.check_for_valid_layer_name(self)
  37. auditor.check_if_linetype_exists(self)
  38. auditor.check_for_valid_color_index(self)
  39. _LINE_TPL = """0
  40. LINE
  41. 5
  42. 0
  43. 8
  44. 0
  45. 10
  46. 0.0
  47. 20
  48. 0.0
  49. 30
  50. 0.0
  51. 11
  52. 1.0
  53. 21
  54. 1.0
  55. 31
  56. 1.0
  57. """
  58. class Line(GraphicEntity):
  59. __slots__ = ()
  60. TEMPLATE = ExtendedTags.from_text(_LINE_TPL)
  61. DXFATTRIBS = make_attribs({
  62. 'start': DXFAttr(10, xtype=XType.any_point),
  63. 'end': DXFAttr(11, xtype=XType.any_point),
  64. })
  65. _POINT_TPL = """0
  66. POINT
  67. 5
  68. 0
  69. 8
  70. 0
  71. 10
  72. 0.0
  73. 20
  74. 0.0
  75. 30
  76. 0.0
  77. """
  78. class Point(GraphicEntity):
  79. __slots__ = ()
  80. TEMPLATE = ExtendedTags.from_text(_POINT_TPL)
  81. DXFATTRIBS = make_attribs({
  82. 'location': DXFAttr(10, xtype=XType.any_point),
  83. })
  84. _CIRCLE_TPL = """0
  85. CIRCLE
  86. 5
  87. 0
  88. 8
  89. 0
  90. 10
  91. 0.0
  92. 20
  93. 0.0
  94. 30
  95. 0.0
  96. 40
  97. 1.0
  98. """
  99. class Circle(GraphicEntity):
  100. __slots__ = ()
  101. TEMPLATE = ExtendedTags.from_text(_CIRCLE_TPL)
  102. DXFATTRIBS = make_attribs({
  103. 'center': DXFAttr(10, xtype=XType.any_point),
  104. 'radius': DXFAttr(40),
  105. })
  106. _ARC_TPL = """0
  107. ARC
  108. 5
  109. 0
  110. 8
  111. 0
  112. 10
  113. 0.0
  114. 20
  115. 0.0
  116. 30
  117. 0.0
  118. 40
  119. 1.0
  120. 50
  121. 0
  122. 51
  123. 360
  124. """
  125. class Arc(GraphicEntity):
  126. __slots__ = ()
  127. TEMPLATE = ExtendedTags.from_text(_ARC_TPL)
  128. DXFATTRIBS = make_attribs({
  129. 'center': DXFAttr(10, xtype=XType.any_point),
  130. 'radius': DXFAttr(40),
  131. 'start_angle': DXFAttr(50),
  132. 'end_angle': DXFAttr(51),
  133. })
  134. class SeqEnd(GraphicEntity):
  135. __slots__ = ()
  136. TEMPLATE = ExtendedTags.from_text(" 0\nSEQEND\n 5\n0\n")
  137. DXFATTRIBS = DXFAttributes(DefSubclass(None, {
  138. 'handle': DXFAttr(5),
  139. 'paperspace': DXFAttr(67, default=0),
  140. }))
  141. _SHAPE_TPL = """0
  142. SHAPE
  143. 5
  144. 0
  145. 8
  146. 0
  147. 10
  148. 0.0
  149. 20
  150. 0.0
  151. 30
  152. 0.0
  153. 40
  154. 1.0
  155. 2
  156. NAME
  157. 50
  158. 0.0
  159. 41
  160. 1.0
  161. 51
  162. 0.0
  163. """
  164. # SHAPE is not tested with real world DXF drawings!
  165. class Shape(GraphicEntity):
  166. __slots__ = ()
  167. TEMPLATE = ExtendedTags.from_text(_SHAPE_TPL)
  168. DXFATTRIBS = make_attribs({
  169. 'insert': DXFAttr(10, xtype=XType.any_point),
  170. 'size': DXFAttr(40),
  171. 'name': DXFAttr(2),
  172. 'rotation': DXFAttr(50, default=0.0),
  173. 'xscale': DXFAttr(41, default=1.0),
  174. 'oblique': DXFAttr(51, default=0.0),
  175. })