ParseDXF.py 15 KB

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