ParseDXF.py 15 KB

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