test_pysketcher.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. from __future__ import division
  2. from __future__ import unicode_literals
  3. from __future__ import print_function
  4. from __future__ import absolute_import
  5. from future import standard_library
  6. standard_library.install_aliases()
  7. from builtins import zip
  8. from builtins import *
  9. from pysketcher import *
  10. def equal_dict(d1, d2):
  11. """Return True if nested dicts d1 and d2 are equal."""
  12. for k in d1:
  13. #print('comparing', k)
  14. if k not in d2:
  15. return False
  16. else:
  17. if isinstance(d1[k], dict):
  18. if not equal_dict(d1[k], d2[k]):
  19. return False
  20. else:
  21. # Hack: remove u' for unicode if present
  22. d1_k = d1[k].replace("u'", "'")
  23. d2_k = d2[k].replace("u'", "'")
  24. if d1_k != d2_k:
  25. #print('values differ: [%s] vs [%s]' % (d1_k, d2_k))
  26. return False
  27. return True
  28. def test_Axis():
  29. drawing_tool.set_coordinate_system(
  30. xmin=0, xmax=15, ymin=-7, ymax=8, axis=True,
  31. instruction_file='tmp_Axis.py')
  32. # Draw normal x and y axis with origin at (7.5, 2)
  33. # in the coordinate system of the sketch: [0,15]x[-7,8]
  34. x_axis = Axis((7.5,2), 5, 'x', rotation_angle=0)
  35. y_axis = Axis((7.5,2), 5, 'y', rotation_angle=90)
  36. system = Composition({'x axis': x_axis, 'y axis': y_axis})
  37. system.draw()
  38. drawing_tool.display()
  39. # Rotate this system 40 degrees counter clockwise
  40. # and draw it with dashed lines
  41. system.set_linestyle('dashed')
  42. system.rotate(40, (7.5,2))
  43. system.draw()
  44. drawing_tool.display()
  45. # Rotate this system another 220 degrees and show
  46. # with dotted lines
  47. system.set_linestyle('dotted')
  48. system.rotate(220, (7.5,2))
  49. system.draw()
  50. drawing_tool.display()
  51. drawing_tool.display('Axis')
  52. drawing_tool.savefig('tmp_Axis')
  53. expected = {
  54. 'x axis': {
  55. 'arrow': {
  56. 'head left': {'line': "2 (x,y) coords linestyle='dotted'",},
  57. 'line': {'line': "2 (x,y) coords linestyle='dotted'",},
  58. 'head right': {'line': "2 (x,y) coords linestyle='dotted'",},},
  59. 'label': "Text at (6.57388,-3.25231)",},
  60. 'y axis': {
  61. 'arrow': {
  62. 'head left': {'line': "2 (x,y) coords linestyle='dotted'",},
  63. 'line': {'line': "2 (x,y) coords linestyle='dotted'",},
  64. 'head right': {'line': "2 (x,y) coords linestyle='dotted'",},},
  65. 'label': "Text at (12.7523,1.07388)",},}
  66. computed = eval(repr(system))
  67. msg = 'expected=%s, computed=%s' % (expected, computed)
  68. assert equal_dict(computed, expected), msg
  69. def test_Distance_wText():
  70. drawing_tool.set_coordinate_system(
  71. xmin=0, xmax=10, ymin=0, ymax=6,
  72. axis=True, instruction_file='tmp_Distance_wText.py')
  73. fontsize=14
  74. t = r'$ 2\pi R^2 $' # sample text
  75. examples = Composition({
  76. 'a0': Distance_wText((4,5), (8, 5), t, fontsize),
  77. 'a6': Distance_wText((4,5), (4, 4), t, fontsize),
  78. 'a1': Distance_wText((0,2), (2, 4.5), t, fontsize),
  79. 'a2': Distance_wText((0,2), (2, 0), t, fontsize),
  80. 'a3': Distance_wText((2,4.5), (0, 5.5), t, fontsize),
  81. 'a4': Distance_wText((8,4), (10, 3), t, fontsize,
  82. text_spacing=-1./60),
  83. 'a5': Distance_wText((8,2), (10, 1), t, fontsize,
  84. text_spacing=-1./40, alignment='right'),
  85. 'c1': Text_wArrow('text_spacing=-1./60',
  86. (4, 3.5), (9, 3.2),
  87. fontsize=10, alignment='left'),
  88. 'c2': Text_wArrow('text_spacing=-1./40, alignment="right"',
  89. (4, 0.5), (9, 1.2),
  90. fontsize=10, alignment='left'),
  91. })
  92. examples.draw()
  93. drawing_tool.display('Distance_wText and text positioning')
  94. drawing_tool.savefig('tmp_Distance_wText')
  95. expected = {
  96. 'a1': {
  97. 'text': "Text at (1.13014,3.14588)",
  98. 'arrow': {
  99. 'arrow': {
  100. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  101. 'a0': {
  102. 'text': "Text at (6,5.16667)",
  103. 'arrow': {'arrow': {
  104. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  105. 'a3': {
  106. 'text': "Text at (1.07454,5.14907)",
  107. 'arrow': {'arrow': {
  108. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  109. 'a2': {
  110. 'text': "Text at (1.11785,1.11785)",
  111. 'arrow': {'arrow': {
  112. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  113. 'a5': {
  114. 'text': "Text at (8.8882,1.27639)",
  115. 'arrow': {'arrow': {
  116. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  117. 'a4': {
  118. 'text': "Text at (8.92546,3.35093)",
  119. 'arrow': {'arrow': {
  120. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  121. 'a6': {
  122. 'text': "Text at (4.16667,4.5)",
  123. 'arrow': {'arrow': {
  124. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  125. 'c2': "Text_wArrow at (4,0.5)",
  126. 'c1': "Text_wArrow at (4,3.5)",
  127. }
  128. computed = eval(repr(examples))
  129. msg = 'expected=%s, computed=%s' % (expected, computed)
  130. assert equal_dict(computed, expected), msg
  131. def test_Rectangle():
  132. L = 3.0
  133. W = 4.0
  134. drawing_tool.set_coordinate_system(
  135. xmin=0, xmax=2*W, ymin=-L/2, ymax=2*L,
  136. axis=True, instruction_file='tmp_Rectangle.py')
  137. drawing_tool.set_linecolor('blue')
  138. drawing_tool.set_grid(True)
  139. xpos = W/2
  140. r = Rectangle(lower_left_corner=(xpos,0), width=W, height=L)
  141. r.draw()
  142. r.draw_dimensions()
  143. drawing_tool.display('Rectangle')
  144. drawing_tool.savefig('tmp_Rectangle')
  145. expected = {'rectangle': "5 (x,y) coords",}
  146. computed = eval(repr(r))
  147. msg = 'expected=%s, computed=%s' % (expected, computed)
  148. assert equal_dict(computed, expected), msg
  149. def test_Triangle():
  150. L = 3.0
  151. W = 4.0
  152. drawing_tool.set_coordinate_system(
  153. xmin=0, xmax=2*W, ymin=-L/2, ymax=1.2*L,
  154. axis=True, instruction_file='tmp_Triangle.py')
  155. drawing_tool.set_linecolor('blue')
  156. drawing_tool.set_grid(True)
  157. xpos = 1
  158. t = Triangle(p1=(W/2,0), p2=(3*W/2,W/2), p3=(4*W/5.,L))
  159. t.draw()
  160. t.draw_dimensions()
  161. drawing_tool.display('Triangle')
  162. drawing_tool.savefig('tmp_Triangle')
  163. expected = {'triangle': "4 (x,y) coords",}
  164. computed = eval(repr(t))
  165. msg = 'expected=%s, computed=%s' % (expected, computed)
  166. assert equal_dict(computed, expected), msg
  167. def test_Arc():
  168. L = 4.0
  169. W = 4.0
  170. drawing_tool.set_coordinate_system(
  171. xmin=-W/2, xmax=W, ymin=-L/2, ymax=1.5*L,
  172. axis=True, instruction_file='tmp_Arc.py')
  173. drawing_tool.set_linecolor('blue')
  174. drawing_tool.set_grid(True)
  175. center = point(0,0)
  176. radius = L/2
  177. start_angle = 60
  178. arc_angle = 45
  179. a = Arc(center, radius, start_angle, arc_angle)
  180. a.draw()
  181. R1 = 1.25*radius
  182. R2 = 1.5*radius
  183. R = 2*radius
  184. a.dimensions = {
  185. 'start_angle':
  186. Arc_wText(
  187. 'start_angle', center, R1, start_angle=0,
  188. arc_angle=start_angle, text_spacing=1/10.),
  189. 'arc_angle':
  190. Arc_wText(
  191. 'arc_angle', center, R2, start_angle=start_angle,
  192. arc_angle=arc_angle, text_spacing=1/20.),
  193. 'r=0':
  194. Line(center, center +
  195. point(R*cos(radians(start_angle)),
  196. R*sin(radians(start_angle)))),
  197. 'r=start_angle':
  198. Line(center, center +
  199. point(R*cos(radians(start_angle+arc_angle)),
  200. R*sin(radians(start_angle+arc_angle)))),
  201. 'r=start+arc_angle':
  202. Line(center, center +
  203. point(R, 0)).set_linestyle('dashed'),
  204. 'radius': Distance_wText(center, a(0), 'radius', text_spacing=1/40.),
  205. 'center': Text('center', center-point(radius/10., radius/10.)),
  206. }
  207. for dimension in a.dimensions:
  208. if dimension.startswith('r='):
  209. dim = a.dimensions[dimension]
  210. dim.set_linestyle('dashed')
  211. dim.set_linewidth(1)
  212. dim.set_linecolor('black')
  213. a.draw_dimensions()
  214. drawing_tool.display('Arc')
  215. drawing_tool.savefig('tmp_Arc')
  216. expected = {'arc': "181 (x,y) coords"}
  217. computed = eval(repr(a))
  218. msg = 'expected=%s, computed=%s' % (expected, computed)
  219. assert equal_dict(computed, expected), msg
  220. expected = {
  221. 'center': 'text "center" at (-0.2,-0.2)',
  222. 'start_angle': {'text': "Text at (2.68468,1.55)",
  223. 'arc': {'arc': "181 (x,y) coords",},},
  224. 'r=start+arc_angle': {
  225. 'line': "2 (x,y) coords linecolor='k' linewidth=1 linestyle='dashed'",},
  226. 'r=0': {'line': "2 (x,y) coords linecolor='k' linewidth=1 linestyle='dashed'",},
  227. 'radius': {'text': "Text at (0.629904,0.791025)",
  228. 'arrow': {'arrow': {
  229. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  230. 'r=start_angle': {'line': "2 (x,y) coords linecolor='k' linewidth=1 linestyle='dashed'",},
  231. 'arc_angle': {'text': "Text at (0.430736,3.27177)",
  232. 'arc': {'arc': "181 (x,y) coords",},}
  233. }
  234. computed = eval(repr(a.dimensions))
  235. msg = 'expected=%s, computed=%s' % (expected, computed)
  236. assert equal_dict(computed, expected), msg
  237. def test_Spring():
  238. L = 5.0
  239. W = 2.0
  240. drawing_tool.set_coordinate_system(
  241. xmin=0, xmax=7*W, ymin=-L/2, ymax=1.5*L,
  242. axis=True, instruction_file='tmp_Spring.py')
  243. drawing_tool.set_linecolor('blue')
  244. drawing_tool.set_grid(True)
  245. xpos = W
  246. s1 = Spring((W,0), L, teeth=True)
  247. s1_title = Text('Default Spring',
  248. s1.geometric_features()['end'] + point(0,L/10))
  249. s1.draw()
  250. s1_title.draw()
  251. #s1.draw_dimensions()
  252. xpos += 3*W
  253. s2 = Spring(start=(xpos,0), length=L, width=W/2.,
  254. bar_length=L/6., teeth=False)
  255. s2.draw()
  256. s2.draw_dimensions()
  257. drawing_tool.display('Spring')
  258. drawing_tool.savefig('tmp_Spring')
  259. # Check s1 and s1.dimensions
  260. expected = {
  261. 'bar1': {'line': "2 (x,y) coords",},
  262. 'bar2': {'line': "2 (x,y) coords",},
  263. 'spiral': "45 (x,y) coords",}
  264. computed = eval(repr(s1))
  265. msg = 'expected=%s, computed=%s' % (expected, computed)
  266. assert equal_dict(computed, expected), msg
  267. expected = {
  268. 'bar_length1': {'text': "Text_wArrow at (-1.5,1.75)",
  269. 'arrow': {'arrow': {
  270. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  271. 'bar_length2': {'text': "Text_wArrow at (-1.5,5.5)",
  272. 'arrow': {'arrow': {
  273. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  274. 'width': {'text': "Text at (2,-1.51667)",
  275. 'arrow': {'arrow': {
  276. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  277. 'start': 'annotation "start" at (1.25,-0.75) with arrow to (2,0)',
  278. 'length': {'text': "Text at (3.73333,2.5)",
  279. 'arrow': {'arrow': {
  280. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  281. 'num_windings': 'annotation "num_windings" at (3,5.5) with arrow to (2.6,2.5)'
  282. }
  283. computed = eval(repr(s1.dimensions))
  284. msg = 'expected=%s, computed=%s' % (expected, computed)
  285. assert equal_dict(computed, expected), msg
  286. def test_Dashpot():
  287. L = 5.0
  288. W = 2.0
  289. xpos = 0
  290. drawing_tool.set_coordinate_system(
  291. xmin=xpos, xmax=xpos+5.5*W, ymin=-L/2, ymax=1.5*L,
  292. axis=True, instruction_file='tmp_Dashpot.py')
  293. drawing_tool.set_linecolor('blue')
  294. drawing_tool.set_grid(True)
  295. # Default (simple) dashpot
  296. xpos = 1.5
  297. d1 = Dashpot(start=(xpos,0), total_length=L)
  298. d1_title = Text('Dashpot (default)',
  299. d1.geometric_features()['end'] + point(0,L/10))
  300. d1.draw()
  301. d1_title.draw()
  302. # Dashpot for animation with fixed bar_length, dashpot_length and
  303. # prescribed piston_pos
  304. xpos += 2.5*W
  305. d2 = Dashpot(start=(xpos,0), total_length=1.2*L, width=W/2,
  306. bar_length=W, dashpot_length=L/2, piston_pos=2*W)
  307. d2.draw()
  308. d2.draw_dimensions()
  309. drawing_tool.display('Dashpot')
  310. drawing_tool.savefig('tmp_Dashpot')
  311. expected = {
  312. 'line start': {'line': "2 (x,y) coords",},
  313. 'piston': {
  314. 'line': {'line': "2 (x,y) coords",},
  315. 'rectangle': {
  316. 'rectangle': "5 (x,y) coords fillcolor='' fillpattern='X'",},},
  317. 'pot': "4 (x,y) coords",
  318. }
  319. computed = eval(repr(d2))
  320. msg = 'expected=%s, computed=%s' % (expected, computed)
  321. assert equal_dict(computed, expected), msg
  322. expected = {
  323. 'width': {'text': "Text at (6.5,-1.56667)",
  324. 'arrow': {'arrow': {
  325. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  326. 'start': 'annotation "start" at (5.75,-0.75) with arrow to (6.5,0)',
  327. 'bar_length': {'text': "Text_wArrow at (3.5,1.5)",
  328. 'arrow': {'arrow': {
  329. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  330. 'total_length': {'text': "Text_wArrow at (8.75,5)",
  331. 'arrow': {'arrow': {
  332. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  333. 'dashpot_length': {'text': "Text_wArrow at (7,-0.5)",
  334. 'arrow': {'arrow': {
  335. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},},
  336. 'piston_pos': {'text': "Text_wArrow at (3.5,3.6875)",
  337. 'arrow': {'arrow': {
  338. 'line': "2 (x,y) coords linecolor='k' linewidth=1 arrow='<->'",},},}
  339. }
  340. computed = eval(repr(d2.dimensions))
  341. msg = 'expected=%s, computed=%s' % (expected, computed)
  342. assert equal_dict(computed, expected), msg
  343. def test_Wavy():
  344. drawing_tool.set_coordinate_system(xmin=0, xmax=1.5,
  345. ymin=-0.5, ymax=5,
  346. axis=True,
  347. instruction_file='tmp_Wavy.py')
  348. w = Wavy(main_curve=lambda x: 1 + sin(2*x),
  349. interval=[0,1.5],
  350. wavelength_of_perturbations=0.3,
  351. amplitude_of_perturbations=0.1,
  352. smoothness=0.05)
  353. w.draw()
  354. drawing_tool.display('Wavy')
  355. drawing_tool.savefig('tmp_Wavy')
  356. expected = {'wavy': "2001 (x,y) coords",}
  357. computed = eval(repr(w))
  358. msg = 'expected=%s, computed=%s' % (expected, computed)
  359. assert equal_dict(computed, expected), msg
  360. def test_beam():
  361. L = 8.0
  362. a = 3*L/4
  363. b = L - a
  364. H = 1.0
  365. xpos = 0.0
  366. ypos = 3.0
  367. drawing_tool.set_coordinate_system(
  368. xmin=-3, xmax=xpos+1.5*L,
  369. ymin=0, ymax=ypos+5*H,
  370. axis=False)
  371. drawing_tool.set_linecolor('blue')
  372. #drawing_tool.set_grid(True)
  373. drawing_tool.set_fontsize(16)
  374. A = point(xpos,ypos)
  375. beam = Rectangle(A, L, H)
  376. h = L/16 # size of support, clamped wall etc
  377. clamped = Rectangle(A - point(h,0) - point(0,2*h), h,
  378. 6*h).set_filled_curves(pattern='/')
  379. load = ConstantBeamLoad(A + point(0,H), L, H)
  380. load.set_linewidth(1).set_linecolor('black')
  381. load_text = Text('$w$',
  382. load.geometric_features()['mid_top'] +
  383. point(0,h/2.))
  384. B = A + point(a, 0)
  385. C = B + point(b, 0)
  386. support = SimplySupportedBeam(B, h) # pt B is simply supported
  387. R1 = Force(A-point(0,2*H), A, '$R_1$', text_spacing=1./50)
  388. R1.set_linewidth(3).set_linecolor('black')
  389. R2 = Force(B-point(0,2*H),
  390. support.geometric_features()['mid_support'],
  391. '$R_2$', text_spacing=1./50)
  392. R2.set_linewidth(3).set_linecolor('black')
  393. M1 = Moment('$M_1$', center=A + point(-H, H/2), radius=H/2,
  394. left=True, text_spacing=1/30.)
  395. M1.set_linecolor('black')
  396. ab_level = point(0, 3*h)
  397. a_dim = Distance_wText(A - ab_level, B - ab_level, '$a$')
  398. b_dim = Distance_wText(B - ab_level, C - ab_level, '$b$')
  399. dims = Composition({'a': a_dim, 'b': b_dim})
  400. symbols = Composition(
  401. {'R1': R1, 'R2': R2, 'M1': M1,
  402. 'w': load, 'w text': load_text,
  403. 'A': Text('$A$', A+point(0.7*h,-0.9*h)),
  404. 'B': Text('$B$',
  405. support.geometric_features()['mid_support']-
  406. point(1.25*h,0)),
  407. 'C': Text('$C$', C+point(h/2,-h/2))})
  408. x_axis = Axis(A + point(L+h, H/2), 2*H, '$x$',).\
  409. set_linecolor('black')
  410. y_axis = Axis(A + point(0,H/2), 3.5*H, '$y$',
  411. label_alignment='left',
  412. rotation_angle=90).set_linecolor('black')
  413. axes = Composition({'x axis': x_axis, 'y axis': y_axis})
  414. annotations = Composition({'dims': dims, 'symbols': symbols,
  415. 'axes': axes})
  416. beam = Composition({'beam': beam, 'support': support,
  417. 'clamped end': clamped, 'load': load})
  418. def deflection(x, a, b, w):
  419. import numpy as np
  420. R1 = 5./8*w*a - 3*w*b**2/(4*a)
  421. R2 = 3./8*w*a + w*b + 3*w*b**2/(4*a)
  422. M1 = R1*a/3 - w*a**2/12
  423. y = -(M1/2.)*x**2 + 1./6*R1*x**3 - w/24.*x**4 + \
  424. 1./6*R2*np.where(x > a, 1, 0)*(x-a)**3
  425. return y
  426. x = linspace(0, L, 101)
  427. y = deflection(x, a, b, w=1.0)
  428. y /= abs(y.max() - y.min())
  429. y += ypos + H/2
  430. elastic_line = Curve(x, y).\
  431. set_linecolor('red').\
  432. set_linestyle('dashed').\
  433. set_linewidth(3)
  434. beam.draw()
  435. drawing_tool.display()
  436. import time
  437. time.sleep(1.5)
  438. annotations.draw()
  439. drawing_tool.display()
  440. time.sleep(1.5)
  441. elastic_line.draw()
  442. drawing_tool.display()
  443. #beam.draw_dimensions()
  444. drawing_tool.savefig('tmp_beam')
  445. all = Composition({'beam': beam, 'text': annotations,
  446. 'deflection': elastic_line})
  447. expected = {
  448. 'beam': {
  449. 'load': {
  450. u'box': {
  451. u'rectangle': "5 (x,y) coords linecolor=u'k' linewidth=1",},
  452. u'arrow9': {u'arrow': {
  453. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  454. u'arrow8': {u'arrow': {
  455. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  456. u'arrow7': {u'arrow': {
  457. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  458. u'arrow6': {u'arrow': {
  459. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  460. u'arrow5': {u'arrow': {
  461. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  462. u'arrow4': {u'arrow': {
  463. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  464. u'arrow3': {u'arrow': {
  465. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  466. u'arrow2': {u'arrow': {
  467. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  468. u'arrow1': {u'arrow': {
  469. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  470. u'arrow0': {u'arrow': {
  471. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},},
  472. 'beam': {
  473. u'rectangle': "5 (x,y) coords",},
  474. 'support': {
  475. u'triangle': {u'triangle': "4 (x,y) coords",},
  476. u'rectangle': {u'rectangle': "5 (x,y) coords fillcolor=u'' fillpattern=u'/'",},},
  477. 'clamped end': {
  478. u'rectangle': "5 (x,y) coords fillcolor=u'' fillpattern='/'",},},
  479. 'deflection': "101 (x,y) coords linecolor='red' linewidth=3 linestyle='dashed'",
  480. 'text': {
  481. 'symbols': {
  482. 'A': "Text at (0.35,2.55)",
  483. 'w': {u'box': {u'rectangle': "5 (x,y) coords linecolor=u'k' linewidth=1",},
  484. u'arrow9': {u'arrow': {
  485. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  486. u'arrow8': {u'arrow': {
  487. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  488. u'arrow7': {u'arrow': {
  489. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  490. u'arrow6': {u'arrow': {
  491. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  492. u'arrow5': {u'arrow': {
  493. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  494. u'arrow4': {u'arrow': {
  495. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  496. u'arrow3': {u'arrow': {
  497. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  498. u'arrow2': {u'arrow': {
  499. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  500. u'arrow1': {u'arrow': {
  501. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},
  502. u'arrow0': {u'arrow': {
  503. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'->'",},},},
  504. 'C': "Text at (8.25,2.75)",
  505. 'B': "Text at (5.375,2.275)",
  506. 'M1': {
  507. u'text': "Text at (-2,3.5)",
  508. u'arc': {u'arc': "181 (x,y) coords linecolor=u'k' arrow=u'->'",},},
  509. 'R1': {
  510. u'text': "Text at (0,0.49)",
  511. u'arrow': {u'line': "2 (x,y) coords linecolor=u'k' linewidth=3 arrow=u'->'",},},
  512. 'R2': {
  513. u'text': "Text at (6,0.49)",
  514. u'arrow': {u'line': "2 (x,y) coords linecolor=u'k' linewidth=3 arrow=u'->'",},},
  515. 'w text': "Text at (4,5.25)",},
  516. 'dims': {
  517. 'a': {
  518. u'text': "Text at (3,1.75)",
  519. u'arrow': {u'arrow': {
  520. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'<->'",},},},
  521. 'b': {
  522. u'text': "Text at (7,1.75)",
  523. u'arrow': {u'arrow': {
  524. u'line': "2 (x,y) coords linecolor=u'k' linewidth=1 arrow=u'<->'",},},},},
  525. 'axes': {'x axis': {
  526. u'arrow': {u'head left': {u'line': "2 (x,y) coords linecolor=u'k' linestyle=u'solid'",}, u'line': {u'line': "2 (x,y) coords linecolor=u'k'",}, u'head right': {u'line': "2 (x,y) coords linecolor=u'k' linestyle=u'solid'",},}, u'label': "Text at (10.8333,3.5)",},
  527. 'y axis': {
  528. u'arrow': {u'head left': {u'line': "2 (x,y) coords linecolor=u'k' linestyle=u'solid'",}, u'line': {u'line': "2 (x,y) coords linecolor=u'k'",}, u'head right': {u'line': "2 (x,y) coords linecolor=u'k' linestyle=u'solid'",},}, u'label': "Text at (2.34724e-16,7.33333)",},
  529. },
  530. },
  531. }
  532. computed = eval(repr(all))
  533. msg = 'expected=%s, computed=%s' % (expected, computed)
  534. assert equal_dict(computed, expected), msg
  535. def diff_files(files1, files2, mode='HTML'):
  536. import difflib, time
  537. n = 3
  538. for fromfile, tofile in zip(files1, files2):
  539. fromdate = time.ctime(os.stat(fromfile).st_mtime)
  540. todate = time.ctime(os.stat(tofile).st_mtime)
  541. fromlines = open(fromfile, 'U').readlines()
  542. tolines = open(tofile, 'U').readlines()
  543. diff_html = difflib.HtmlDiff().\
  544. make_file(fromlines,tolines,
  545. fromfile,tofile,context=True,numlines=n)
  546. diff_plain = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
  547. filename_plain = fromfile + '.diff.txt'
  548. filename_html = fromfile + '.diff.html'
  549. if os.path.isfile(filename_plain):
  550. os.remove(filename_plain)
  551. if os.path.isfile(filename_html):
  552. os.remove(filename_html)
  553. f = open(filename_plain, 'w')
  554. f.writelines(diff_plain)
  555. f.close()
  556. size = os.path.getsize(filename_plain)
  557. if size > 4:
  558. print('found differences:', fromfile, tofile)
  559. f = open(filename_html, 'w')
  560. f.writelines(diff_html)
  561. f.close()
  562. def _test_test():
  563. """Compare files."""
  564. # Does not work yet.
  565. os.chdir('test')
  566. funcs = [name for name in globals() if name.startswith('test_') and callable(globals()[name])]
  567. funcs.remove('test_test')
  568. new_files = []
  569. res_files = []
  570. for func in funcs:
  571. mplfile = func.replace('test_', 'tmp_') + '.py'
  572. #exec(func + '()')
  573. new_files.append(mplfile)
  574. resfile = mplfile.replace('tmp_', 'res_')
  575. res_files.append(resfile)
  576. diff_files(new_files, res_files)
  577. test_Arc()