ParseDXF.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 3/10/2019 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from shapely.geometry import LineString
  8. from shapely.affinity import rotate
  9. from ezdxf.math import Vector as ezdxf_vector
  10. from appParsers.ParseFont import *
  11. from appParsers.ParseDXF_Spline import *
  12. import logging
  13. log = logging.getLogger('base2')
  14. def distance(pt1, pt2):
  15. return math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2)
  16. def dxfpoint2shapely(point):
  17. geo = Point(point.dxf.location).buffer(0.01)
  18. return geo
  19. def dxfline2shapely(line):
  20. try:
  21. start = (line.dxf.start[0], line.dxf.start[1])
  22. stop = (line.dxf.end[0], line.dxf.end[1])
  23. except Exception as e:
  24. log.debug(str(e))
  25. return None
  26. geo = LineString([start, stop])
  27. return geo
  28. def dxfcircle2shapely(circle, n_points=100):
  29. ocs = circle.ocs()
  30. # if the extrusion attribute is not (0, 0, 1) then we have to change the coordinate system from OCS to WCS
  31. if circle.dxf.extrusion != (0, 0, 1):
  32. center_pt = ocs.to_wcs(circle.dxf.center)
  33. else:
  34. center_pt = circle.dxf.center
  35. radius = circle.dxf.radius
  36. geo = Point(center_pt).buffer(radius, int(n_points / 4))
  37. return geo
  38. def dxfarc2shapely(arc, n_points=100):
  39. # ocs = arc.ocs()
  40. # # if the extrusion attribute is not (0, 0, 1) then we have to change the coordinate system from OCS to WCS
  41. # if arc.dxf.extrusion != (0, 0, 1):
  42. # arc_center = ocs.to_wcs(arc.dxf.center)
  43. # start_angle = math.radians(arc.dxf.start_angle) + math.pi
  44. # end_angle = math.radians(arc.dxf.end_angle) + math.pi
  45. # dir = 'CW'
  46. # else:
  47. # arc_center = arc.dxf.center
  48. # start_angle = math.radians(arc.dxf.start_angle)
  49. # end_angle = math.radians(arc.dxf.end_angle)
  50. # dir = 'CCW'
  51. #
  52. # center_x = arc_center[0]
  53. # center_y = arc_center[1]
  54. # radius = arc.dxf.radius
  55. #
  56. # point_list = []
  57. #
  58. # if start_angle > end_angle:
  59. # start_angle += 2 * math.pi
  60. #
  61. # line_seg = int((n_points * (end_angle - start_angle)) / math.pi)
  62. # step_angle = (end_angle - start_angle) / float(line_seg)
  63. #
  64. # angle = start_angle
  65. # for step in range(line_seg + 1):
  66. # if dir == 'CCW':
  67. # x = center_x + radius * math.cos(angle)
  68. # y = center_y + radius * math.sin(angle)
  69. # else:
  70. # x = center_x + radius * math.cos(-angle)
  71. # y = center_y + radius * math.sin(-angle)
  72. # point_list.append((x, y))
  73. # angle += step_angle
  74. #
  75. #
  76. # log.debug("X = %.4f, Y = %.4f, Radius = %.4f, start_angle = %.1f, stop_angle = %.1f, step_angle = %.4f, dir=%s" %
  77. # (center_x, center_y, radius, start_angle, end_angle, step_angle, dir))
  78. #
  79. # geo = LineString(point_list)
  80. # return geo
  81. ocs = arc.ocs()
  82. # if the extrusion attribute is not (0, 0, 1) then we have to change the coordinate system from OCS to WCS
  83. if arc.dxf.extrusion != (0, 0, 1):
  84. arc_center = ocs.to_wcs(arc.dxf.center)
  85. start_angle = arc.dxf.start_angle + 180
  86. end_angle = arc.dxf.end_angle + 180
  87. direction = 'CW'
  88. else:
  89. arc_center = arc.dxf.center
  90. start_angle = arc.dxf.start_angle
  91. end_angle = arc.dxf.end_angle
  92. direction = 'CCW'
  93. center_x = arc_center[0]
  94. center_y = arc_center[1]
  95. radius = arc.dxf.radius
  96. point_list = []
  97. if start_angle > end_angle:
  98. start_angle = start_angle - 360
  99. angle = start_angle
  100. step_angle = float(abs(end_angle - start_angle) / n_points)
  101. while angle <= end_angle:
  102. if direction == 'CCW':
  103. x = center_x + radius * math.cos(math.radians(angle))
  104. y = center_y + radius * math.sin(math.radians(angle))
  105. else:
  106. x = center_x + radius * math.cos(math.radians(-angle))
  107. y = center_y + radius * math.sin(math.radians(-angle))
  108. point_list.append((x, y))
  109. angle += abs(step_angle)
  110. # in case the number of segments do not cover everything until the end of the arc
  111. if angle != end_angle:
  112. if direction == 'CCW':
  113. x = center_x + radius * math.cos(math.radians(end_angle))
  114. y = center_y + radius * math.sin(math.radians(end_angle))
  115. else:
  116. x = center_x + radius * math.cos(math.radians(- end_angle))
  117. y = center_y + radius * math.sin(math.radians(- end_angle))
  118. point_list.append((x, y))
  119. # log.debug("X = %.4f, Y = %.4f, Radius = %.4f, start_angle = %.1f, stop_angle = %.1f, step_angle = %.4f" %
  120. # (center_x, center_y, radius, start_angle, end_angle, step_angle))
  121. geo = LineString(point_list)
  122. return geo
  123. def dxfellipse2shapely(ellipse, ellipse_segments=100):
  124. # center = ellipse.dxf.center
  125. # start_angle = ellipse.dxf.start_param
  126. # end_angle = ellipse.dxf.end_param
  127. ocs = ellipse.ocs()
  128. # if the extrusion attribute is not (0, 0, 1) then we have to change the coordinate system from OCS to WCS
  129. if ellipse.dxf.extrusion != (0, 0, 1):
  130. center = ocs.to_wcs(ellipse.dxf.center)
  131. start_angle = ocs.to_wcs(ellipse.dxf.start_param)
  132. end_angle = ocs.to_wcs(ellipse.dxf.end_param)
  133. direction = 'CW'
  134. else:
  135. center = ellipse.dxf.center
  136. start_angle = ellipse.dxf.start_param
  137. end_angle = ellipse.dxf.end_param
  138. direction = 'CCW'
  139. # print("Dir = %s" % dir)
  140. major_axis = ellipse.dxf.major_axis
  141. ratio = ellipse.dxf.ratio
  142. points_list = []
  143. major_axis = Vector(list(major_axis))
  144. major_x = major_axis[0]
  145. major_y = major_axis[1]
  146. if start_angle >= end_angle:
  147. end_angle += 2.0 * math.pi
  148. line_seg = int((ellipse_segments * (end_angle - start_angle)) / math.pi)
  149. step_angle = abs(end_angle - start_angle) / float(line_seg)
  150. angle = start_angle
  151. for step in range(line_seg + 1):
  152. if direction == 'CW':
  153. major_dim = normalize_2(major_axis)
  154. minor_dim = normalize_2(Vector([ratio * k for k in major_axis]))
  155. vx = (major_dim[0] + major_dim[1]) * math.cos(angle)
  156. vy = (minor_dim[0] - minor_dim[1]) * math.sin(angle)
  157. x = center[0] + major_x * vx - major_y * vy
  158. y = center[1] + major_y * vx + major_x * vy
  159. angle += step_angle
  160. else:
  161. major_dim = normalize_2(major_axis)
  162. minor_dim = (Vector([ratio * k for k in major_dim]))
  163. vx = (major_dim[0] + major_dim[1]) * math.cos(angle)
  164. vy = (minor_dim[0] + minor_dim[1]) * math.sin(angle)
  165. x = center[0] + major_x * vx + major_y * vy
  166. y = center[1] + major_y * vx + major_x * vy
  167. angle += step_angle
  168. points_list.append((x, y))
  169. geo = LineString(points_list)
  170. return geo
  171. def dxfpolyline2shapely(polyline):
  172. final_pts = []
  173. pts = polyline.points()
  174. for i in pts:
  175. final_pts.append((i[0], i[1]))
  176. if polyline.is_closed:
  177. final_pts.append(final_pts[0])
  178. geo = LineString(final_pts)
  179. return geo
  180. def dxflwpolyline2shapely(lwpolyline):
  181. final_pts = []
  182. for point in lwpolyline:
  183. x, y, _, _, _ = point
  184. final_pts.append((x, y))
  185. if lwpolyline.closed:
  186. final_pts.append(final_pts[0])
  187. geo = LineString(final_pts)
  188. return geo
  189. def dxfsolid2shapely(solid):
  190. iterator = 0
  191. corner_list = []
  192. try:
  193. corner_list.append(solid[iterator])
  194. iterator += 1
  195. except Exception:
  196. return Polygon(corner_list)
  197. def dxfspline2shapely(spline):
  198. # for old version of ezdxf
  199. # with spline.edit_data() as spline_data:
  200. # ctrl_points = spline_data.control_points
  201. # try:
  202. # # required if using old version of ezdxf
  203. # knot_values = spline_data.knot_values
  204. # except AttributeError:
  205. # knot_values = spline_data.knots
  206. ctrl_points = spline.control_points
  207. knot_values = spline.knots
  208. is_closed = spline.closed
  209. degree = spline.dxf.degree
  210. x_list, y_list, _ = spline2Polyline(ctrl_points, degree=degree, closed=is_closed, segments=20, knots=knot_values)
  211. points_list = zip(x_list, y_list)
  212. geo = LineString(points_list)
  213. return geo
  214. def dxftrace2shapely(trace):
  215. iterator = 0
  216. corner_list = []
  217. try:
  218. corner_list.append(trace[iterator])
  219. iterator += 1
  220. except Exception:
  221. return Polygon(corner_list)
  222. def getdxfgeo(dxf_object):
  223. msp = dxf_object.modelspace()
  224. geos = get_geo(dxf_object, msp)
  225. # geo_block = get_geo_from_block(dxf_object)
  226. return geos
  227. def get_geo_from_insert(dxf_object, insert):
  228. geo_block_transformed = []
  229. phi = insert.dxf.rotation
  230. tr = insert.dxf.insert
  231. sx = insert.dxf.xscale
  232. sy = insert.dxf.yscale
  233. r_count = insert.dxf.row_count
  234. r_spacing = insert.dxf.row_spacing
  235. c_count = insert.dxf.column_count
  236. c_spacing = insert.dxf.column_spacing
  237. # print(phi, tr)
  238. # identify the block given the 'INSERT' type entity name
  239. block = dxf_object.blocks[insert.dxf.name]
  240. block_coords = (block.block.dxf.base_point[0], block.block.dxf.base_point[1])
  241. # get a list of geometries found in the block
  242. geo_block = get_geo(dxf_object, block)
  243. # iterate over the geometries found and apply any transformation found in the 'INSERT' entity attributes
  244. for geo in geo_block:
  245. # get the bounds of the geometry
  246. # minx, miny, maxx, maxy = geo.bounds
  247. if tr[0] != 0 or tr[1] != 0:
  248. geo = translate(geo, (tr[0] - block_coords[0]), (tr[1] - block_coords[1]))
  249. # support for array block insertions
  250. if r_count > 1:
  251. for r in range(r_count):
  252. geo_block_transformed.append(translate(geo, (tr[0] + (r * r_spacing) - block_coords[0]), 0))
  253. if c_count > 1:
  254. for c in range(c_count):
  255. geo_block_transformed.append(translate(geo, 0, (tr[1] + (c * c_spacing) - block_coords[1])))
  256. if sx != 1 or sy != 1:
  257. geo = scale(geo, sx, sy)
  258. if phi != 0:
  259. if isinstance(tr, str) and tr.lower() == 'c':
  260. tr = 'center'
  261. elif isinstance(tr, ezdxf_vector):
  262. tr = list(tr)
  263. geo = rotate(geo, phi, origin=tr)
  264. geo_block_transformed.append(geo)
  265. return geo_block_transformed
  266. def get_geo(dxf_object, container):
  267. # store shapely geometry here
  268. geo = []
  269. for dxf_entity in container:
  270. g = []
  271. # print("Entity", dxf_entity.dxftype())
  272. if dxf_entity.dxftype() == 'POINT':
  273. g = dxfpoint2shapely(dxf_entity,)
  274. elif dxf_entity.dxftype() == 'LINE':
  275. g = dxfline2shapely(dxf_entity,)
  276. elif dxf_entity.dxftype() == 'CIRCLE':
  277. g = dxfcircle2shapely(dxf_entity)
  278. elif dxf_entity.dxftype() == 'ARC':
  279. g = dxfarc2shapely(dxf_entity)
  280. elif dxf_entity.dxftype() == 'ELLIPSE':
  281. g = dxfellipse2shapely(dxf_entity)
  282. elif dxf_entity.dxftype() == 'LWPOLYLINE':
  283. g = dxflwpolyline2shapely(dxf_entity)
  284. elif dxf_entity.dxftype() == 'POLYLINE':
  285. g = dxfpolyline2shapely(dxf_entity)
  286. elif dxf_entity.dxftype() == 'SOLID':
  287. g = dxfsolid2shapely(dxf_entity)
  288. elif dxf_entity.dxftype() == 'TRACE':
  289. g = dxftrace2shapely(dxf_entity)
  290. elif dxf_entity.dxftype() == 'SPLINE':
  291. g = dxfspline2shapely(dxf_entity)
  292. elif dxf_entity.dxftype() == 'INSERT':
  293. g = get_geo_from_insert(dxf_object, dxf_entity)
  294. else:
  295. log.debug(" %s is not supported yet." % dxf_entity.dxftype())
  296. if g is not None:
  297. if type(g) == list:
  298. for subg in g:
  299. geo.append(subg)
  300. else:
  301. geo.append(g)
  302. return geo
  303. def getdxftext(exf_object, object_type, units=None):
  304. pass
  305. # def get_geo_from_block(dxf_object):
  306. # geo_block_transformed = []
  307. #
  308. # msp = dxf_object.modelspace()
  309. # # iterate through all 'INSERT' entities found in modelspace msp
  310. # for insert in msp.query('INSERT'):
  311. # phi = insert.dxf.rotation
  312. # tr = insert.dxf.insert
  313. # sx = insert.dxf.xscale
  314. # sy = insert.dxf.yscale
  315. # r_count = insert.dxf.row_count
  316. # r_spacing = insert.dxf.row_spacing
  317. # c_count = insert.dxf.column_count
  318. # c_spacing = insert.dxf.column_spacing
  319. #
  320. # # print(phi, tr)
  321. #
  322. # # identify the block given the 'INSERT' type entity name
  323. # print(insert.dxf.name)
  324. # block = dxf_object.blocks[insert.dxf.name]
  325. # block_coords = (block.block.dxf.base_point[0], block.block.dxf.base_point[1])
  326. #
  327. # # get a list of geometries found in the block
  328. # # store shapely geometry here
  329. # geo_block = []
  330. #
  331. # for dxf_entity in block:
  332. # g = []
  333. # # print("Entity", dxf_entity.dxftype())
  334. # if dxf_entity.dxftype() == 'POINT':
  335. # g = dxfpoint2shapely(dxf_entity, )
  336. # elif dxf_entity.dxftype() == 'LINE':
  337. # g = dxfline2shapely(dxf_entity, )
  338. # elif dxf_entity.dxftype() == 'CIRCLE':
  339. # g = dxfcircle2shapely(dxf_entity)
  340. # elif dxf_entity.dxftype() == 'ARC':
  341. # g = dxfarc2shapely(dxf_entity)
  342. # elif dxf_entity.dxftype() == 'ELLIPSE':
  343. # g = dxfellipse2shapely(dxf_entity)
  344. # elif dxf_entity.dxftype() == 'LWPOLYLINE':
  345. # g = dxflwpolyline2shapely(dxf_entity)
  346. # elif dxf_entity.dxftype() == 'POLYLINE':
  347. # g = dxfpolyline2shapely(dxf_entity)
  348. # elif dxf_entity.dxftype() == 'SOLID':
  349. # g = dxfsolid2shapely(dxf_entity)
  350. # elif dxf_entity.dxftype() == 'TRACE':
  351. # g = dxftrace2shapely(dxf_entity)
  352. # elif dxf_entity.dxftype() == 'SPLINE':
  353. # g = dxfspline2shapely(dxf_entity)
  354. # elif dxf_entity.dxftype() == 'INSERT':
  355. # log.debug("Not supported yet.")
  356. # else:
  357. # log.debug("Not supported yet.")
  358. #
  359. # if g is not None:
  360. # if type(g) == list:
  361. # for subg in g:
  362. # geo_block.append(subg)
  363. # else:
  364. # geo_block.append(g)
  365. #
  366. # # iterate over the geometries found and apply any transformation found in the 'INSERT' entity attributes
  367. # for geo in geo_block:
  368. # if tr[0] != 0 or tr[1] != 0:
  369. # geo = translate(geo, (tr[0] - block_coords[0]), (tr[1] - block_coords[1]))
  370. #
  371. # # support for array block insertions
  372. # if r_count > 1:
  373. # for r in range(r_count):
  374. # geo_block_transformed.append(translate(geo, (tr[0] + (r * r_spacing) - block_coords[0]), 0))
  375. #
  376. # if c_count > 1:
  377. # for c in range(c_count):
  378. # geo_block_transformed.append(translate(geo, 0, (tr[1] + (c * c_spacing) - block_coords[1])))
  379. #
  380. # if sx != 1 or sy != 1:
  381. # geo = scale(geo, sx, sy)
  382. # if phi != 0:
  383. # geo = rotate(geo, phi, origin=tr)
  384. #
  385. # geo_block_transformed.append(geo)
  386. # return geo_block_transformed