-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclassDrawing.py
8794 lines (8040 loc) · 273 KB
/
classDrawing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2007 Philippe LAWRENCE
#
# This file is part of pyBar.
# pyBar is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# pyBar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyBar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from gi.repository import Gtk, Gdk, GObject, GdkPixbuf
import math
import sys
import os
import copy
#import classEditor
import classRdm
import function
import Const
import classPrefs
import classDialog
import cairo
#from time import sleep
import xml.etree.ElementTree as ET
#import time
class MyEntry(Gtk.Entry):
"""Entry à taille modifiable"""
def __init__(self):
GObject.GObject.__init__(self)
self.connect("draw", self.draw_event)
def draw_event(self, widget, event):
text = self.get_text()
n_chars = len(text)
self.set_width_chars(n_chars)
# functions
def draw_square(cr, x, y, fill=True):
"""Dessine un carré jaune à fond blanc, x et y en pixels (device)"""
#print("_draw_square")
cr.save()
# antialiasing utile si les coordonnées ne sont pas des entiers
cr.set_antialias(cairo.ANTIALIAS_NONE)
size = 4
cr.rectangle(x-size, y-size, 2*size, 2*size)
cr.fill()
cr.stroke()
size = 3
if not fill:
cr.restore()
return
cr.set_source_rgb(1, 1, 1)
cr.rectangle(x-size, y-size, 2*size, 2*size)
cr.fill()
cr.stroke()
cr.restore()
def draw_single_soll_text(cr, x, y, text, angle=0, font_size=1, bold=False):
"""Ecrit une valeur caractéristique sur un diagramme de sollicitation
pt est donné en coordonnées device
pos : 0 : angle supérieur gauche
"""
cr.save()
cr.set_font_size(Const.FONT_SIZE*font_size)
# cr.select_font_face(Const.FONT,
# cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
x_bearing, y_bearing, width, height = cr.text_extents(text)[:4]
#cr.arc(x, y, 3, 0, 6.29)
#cr.fill()
#cr.stroke()
cr.move_to(x, y)
cr.rotate(angle)
cr.rel_move_to( - width / 2 - x_bearing, - height / 2 - y_bearing)
cr.show_text(text)
cr.restore()
def get_influ_conv(status, unit_conv):
"""Retourne le facteur de conversion pour les lignes d'influences"""
if status == 2:
return unit_conv['F']*unit_conv['L']
elif status == 3:
return unit_conv['L']
return unit_conv['F']
class Singleton(object):
def __new__(cls, *args, **kwargs):
if '_inst' not in vars(cls):
cls._inst = object.__new__(cls, *args, **kwargs)
return cls._inst
class InfluParams(object):
#class_counter = 0
def __init__(self, id):
self.id = id
#self.id = InfluParams.class_counter
#InfluParams.class_counter += 1
def add(self, args): # mettre un dictionnaire
for nom, val in args.items():
setattr(self, nom, val)
class FGColor(Singleton):
"""Classe pour la gestion des couleurs"""
def __init__(self):
self._configure_colors()
def _configure_colors(self):
"""Définit les couleurs du drawing_area
Les couleurs sont placées dans la propriété diColor"""
# pixmap colors
#colormap = Gdk.colormap_get_system()
#color = colormap.alloc_color("white", writeable=False, best_match=True)
#self._white = color
# cairo colors
self._colors = {'red': (1, 0, 0),
'blue': (0, 0, 1),
'grey': (0.4, 0.4, 0.4),
'white': (1, 1, 1),
'orange': (0.8, 0.8, 0.2),
'black': (0, 0, 0),
'green': (0, 1, 0)}
def get_nth_color(self, i):
"""Retourne la couleur de rang i"""
colors = list(self._colors.keys())
colors.remove('white')
n = len(colors)
return colors[i % n]
def set_nth_color(self, cr, i): # inutilisée
"""Retourne la couleur de rang i"""
colors = list(self._colors.keys())
n = len(colors)
color = self._colors[colors[i % n]]
cr.set_source_rgb(color[0], color[1], color[2])
def set_color_by_name(self, cr, color, alpha=1.):
try:
color = self._colors[color]
except KeyError:
print("FGColor color name error")
return
cr.set_source_rgba(color[0], color[1], color[2], alpha)
def get_random_color(self):
pass
class Value(object):
"""Classes de base pour les légendes des courbes"""
def __init__(self, disc=0, auto=True):
self.disc = disc
self.auto = auto # valeur automatique ou user
def get_position(self):
"""Retourne la position de la légende"""
return self.x, self.y
def get_is_selected(self, x_event, y_event, m):
x, y = self.x, self.y
if x_event < x-m or x_event > x + m:
return False
if y_event < y-m or y_event > y + m:
return False
return True
class ReacValue(Value):
"""Classes pour les légendes des réactions d'appuis"""
def __init__(self, u, name, tu):
text, x, y, a, disc = tu
Value.__init__(self)
self.text = text # utile?
self.u = u # 0, 1, 2
self.name = name
self.x = x
self.y = y
self.a = a
def get_reac_text(self, rdm, unit_conv, unit_name, Char, drawing):
"""Met en évidence une réaction d'appui"""
barre = self.name
if drawing.parent is None:
is_child = False
texts = ['Fx', 'Fy', 'M']
else:
is_child = True
texts = ['N', 'V', 'M']
if is_child and drawing.N1[2] == barre:
pos = 0
else:
pos = 1
Char = rdm.GetCharByNumber(drawing.s_case)
if self.u == 0:
if is_child:
val = Char.EndBarSol[drawing.s_bar][pos][0]
else:
val = Char.Reactions[barre]["Fx"] # barre est ici un noeud!!
elif self.u == 1:
if is_child:
val = Char.EndBarSol[drawing.s_bar][pos][1]
else:
val = Char.Reactions[barre]["Fy"] # barre est ici un noeud!!
else:
if is_child:
val = Char.EndBarSol[drawing.s_bar][pos][2]
else:
val = Char.Reactions[barre]["Mz"] # barre est ici un noeud!!
val = val/unit_conv
legend = '%s = %f %s' % (texts[self.u], val, unit_name)
return legend
class BarreValue(Value):
"""Classes pour les légendes des courbes pour les barres simples"""
def __init__(self, u, name, tu):
values, x, y, a, disc, auto = tu
Value.__init__(self, disc, auto)
self.values = values
assert len(values) == 2
# u : longueur absolue
self.u = float(u) # convertit type 'numpy.float64'
self.name = name
self.x = x
self.y = y
self.a = a
def get_soll_text(self, rdm, unit_conv, unit_name):
"""Met en évidence une légende"""
u, barre = self.u, self.name
l = rdm.struct.Lengths[barre]
if u == 0 or u == l:
is_node = True
if u == 0:
node = rdm.struct.Barres[barre][0]
else:
node = rdm.struct.Barres[barre][1]
else:
is_node = False
if is_node:
legend = "sur %s: %s" % (barre, node)
else:
u = function.PrintValue(u, unit_conv, True)
legend = "sur %s: %s %s" % (barre, u, unit_name)
return legend
class CurveValue(Value):
"""Classes pour les légendes des courbes pour un arc"""
def __init__(self, u, name, tu):
values, x, y, a, disc, auto = tu
Value.__init__(self, disc, auto)
self.values = values
assert len(values) == 2
self.u = float(u) # longueur absolu par rapport origine arc
self.name = name
self.x = x
self.y = y
self.a = a
def get_soll_text(self, rdm, unit_conv, unit_name):
"""Met en évidence une légende"""
struct = rdm.struct
Lengths = struct.Lengths
Arc = struct.Curves[self.name]
ltot = Arc.get_size(Lengths)
user_nodes = Arc.user_nodes
N0, N1 = user_nodes[0].name, user_nodes[-1].name
if self.u == 0:
legend = "sur %s: %s" % (self.name, N0)
elif self.u == ltot:
legend = "sur %s: %s" % (self.name, N1)
else:
pos = function.PrintValue(self.u, unit_conv, True)
legend = "Position sur %s: %s %s" % (self.name, pos, unit_name)
return legend
class Node(object):
"""Classes contenant un noeud pour le mapping"""
def __init__(self, name, coors):
self.name = name
self.coors = coors
class MInfo(object):
class_counter = 0
def __init__(self):
self.id = MInfo.class_counter
MInfo.class_counter += 1
class MText(MInfo):
def __init__(self, box, text, id=None):
self.visible = True
self.box = box
self.text = text
if id is None:
MInfo.__init__(self)
else:
self.id = id
class MScale(MInfo):
def __init__(self, box, id=None):
self.visible = True
self.box = box
if id is None:
MInfo.__init__(self)
else:
self.id = id
class MSeries(MInfo): # les héritages pas utile en l'état
def __init__(self, box, id=None):
self.visible = True
self.box = box
if id is None:
MInfo.__init__(self)
else:
self.id = id
class MParabola(object):
"""Classes contenant une parabole pour le mapping"""
# simplifier : ldefo, dx ...
def __init__(self, name, coors):
self.name = name
self.coors = coors
def get_is_selected(self, x_event, y_event, m):
pt, coefs, geom, bezier = self.coors
a, b = coefs[1:3]
x, dx, x1, dx1, angle = geom
l = x1+dx1
x0, y0 = pt # origine de la barre
x_e = (x_event-x0)*math.cos(angle)-(y_event-y0)*math.sin(angle)
y_e = -(x_event-x0)*math.sin(angle)-(y_event-y0)*math.cos(angle) # inversion signe
if x_e < x+dx or x_e > x+dx+l:
return False
y_chart = a*x_e**2+b*x_e
if abs(y_chart-y_e) < m:
return True
return False
def redraw(self, cr):
"""Redessine l'arc à partir des données du mapping"""
x0, y0 = self.coors[0]
cr.move_to(x0, y0)
c1x, c1y, c2x, c2y, x1, y1 = self.coors[3]
cr.rel_curve_to(c1x, c1y, c2x, c2y, x1, y1)
cr.stroke()
draw_square(cr, x0, y0)
draw_square(cr, x0+x1, y0+y1)
class MArc(object):
"""Classes contenant un arc pour le mapping"""
def __init__(self, name, coors):
self.name = name
self.coors = coors
def get_is_selected(self, x, y, m):
"""Retourne False or True si l'arc est mappé"""
xc, yc, r, teta1, teta2 = self.coors
d = ((x-xc)**2 + (y-yc)**2)**0.5
if abs(d-r) > m:
return False
if teta1 == teta2:
if abs(d-r) < m:
return True
a = function.get_vector_angle((xc, -yc), (x, -y))
if teta1 <= 0 and teta2 > 0:
if a > teta2 or a < teta2:
return True
else:
if teta2 < a and a < teta1:
return True
return False
def redraw(self, cr):
"""Redessine l'arc à partir des données du mapping"""
xc, yc, r, teta1, teta2 = self.coors
if teta1 == teta2:
cr.arc(xc, yc, r, 0, 6.29)
else:
cr.arc(xc, yc, r, -teta1, -teta2)
cr.stroke()
draw_square(cr, xc, yc)
class MCurve(object):
"""Classe contenant un segment de courbe"""
def __init__(self):
pass
class MCurveDisc(object):
"""Discontinuité du tracé"""
def __init__(self, x, y):
self.x, self.y = x, y
def get_is_selected(self, x_event, y_event, m):
return False
def redraw(self, cr):
"""Effectue un déplacement correspondant à la discontinuité"""
cr.move_to(self.x, self.y)
class MCurveSeg(object):
"""Segment de droite du tracé"""
def __init__(self, tu):
self.pt0, self.pt1, pos = tu
self.l = ((self.pt1[0]-self.pt0[0])**2+(self.pt1[1]-self.pt0[1])**2)**0.5
self.u0, self.u1 = pos
def get_is_selected(self, x_event, y_event, m):
#print("get_is_selected", x_event, y_event, m)
pt0, pt1 = self.pt0, self.pt1
x0, y0 = pt0
x1, y1 = pt1
if x1 < x0:
x0, x1 = x1, x0
y0, y1 = y1, y0
dx, dy = x1-x0, y1-y0
if x_event < x0-1 or x_event > x1+1:
return False
#if abs(dx) < 1: # courbe verticale
# if y0 < y1:
# if y_event > y0 and y_event < y1:
# self.is_selected = (x0, y_event, None)
# return True
# else:
# if y_event > y1 and y_event < y0:
# self.is_selected = (x0, y_event, None)
# return True
# return False
if dx > abs(dy):
y = dy * (x_event-x0) / dx + y0
d = abs(y_event-y)
if d < m:
self.is_selected = (x_event, y, None)
return True
else:
x = dx * (y_event-y0) / dy + x0
d = abs(x_event-x)
if d < m:
self.is_selected = (x, y_event, None)
return True
return False
def redraw(self, cr):
x, y = self.pt1
cr.line_to(x, y)
def push_hover_value(self, cr, study, barre, drawing, n_case):
x, y, u = self.is_selected # en px absolu, u = None
u0, u1 = self.u0, self.u1
x0, y0 = self.pt0 # origine de la barre
dl = ((x-x0)**2+(y-y0)**2)**0.5
u = u0 + dl/self.l*(self.u1-self.u0)
if u > self.u1:
u = self.u1
if u < self.u0:
u = self.u0
self.is_selected = (x, y, u)
angle = function.get_vector_angle(self.pt0, self.pt1)
if drawing.status == 7:
rdm = study.rdm
unit_conv = rdm.struct.units
Char = rdm.GetCharByNumber(n_case)
val = rdm.GetValue(barre, u, Char, drawing.status)
valx, valy = val
text = function.PrintValue(valy, unit_conv['L'])
elif drawing.status == 8:
rdm = study.influ_rdm
unit_conv = rdm.struct.units
i_obj = drawing.influ_list[n_case]
l = rdm.struct.Lengths[barre]
val = rdm.ValueLigneInf(barre, u/l, i_obj.elem, i_obj.u, i_obj.status)
influ_unit = get_influ_conv(i_obj.status, unit_conv)
text = function.PrintValue(val, influ_unit)
val = (0., val) # provisoire
elif drawing.status == 6:
rdm = study.rdm
unit_conv = rdm.struct.units
Char = rdm.GetCharByNumber(n_case)
val = rdm.GetValue(barre, u, Char, drawing.status)
valx, valy = val
text = function.PrintValue(valy, unit_conv['F']*unit_conv['L'])
else:
rdm = study.rdm
unit_conv = rdm.struct.units
Char = rdm.GetCharByNumber(n_case)
val = rdm.GetValue(barre, u, Char, drawing.status)
valx, valy = val
text = function.PrintValue(valy, unit_conv['F'])
self.text = text
cr.push_group()
color = drawing._fg.get_nth_color(n_case)
drawing._fg.set_color_by_name(cr, color)
cr.save()
cr.set_font_size(Const.FONT_SIZE)
cr.move_to(x, y-10)
cr.rotate(angle)
cr.show_text("%s" % text)
cr.restore()
drawing._draw_legend_position(cr, rdm.struct, barre, u, val)
drawing.p_window = cr.pop_group()
class MCurveArc(object):
"""segment de Bezier du tracé"""
def __init__(self, elem):
self.x0 = elem[0]
self.y0 = elem[1]
self.c0x = elem[2]
self.c0y = elem[3]
self.c1x = elem[4]
self.c1y = elem[5]
self.x1 = elem[6]
self.y1 = elem[7]
self.b0 = elem[8]
self.b1 = elem[9]
#print("init=", self.b0, self.b1)
# meme methode que pout les segments
def get_is_selected(self, x_event, y_event, m):
#pt0, pt1 = self.pt0, self.pt1
x0, y0 = self.x0, self.y0
x1, y1 = self.x1, self.y1
if x1 < x0:
x0, x1 = x1, x0
y0, y1 = y1, y0
dx, dy = x1-x0, y1-y0
if x_event < x0 or x_event > x1:
return False
y = dy * (x_event-x0) / dx + y0
d = abs(y_event-y)
if d < m:
l0 = (dx**2+dy**2)**0.5
l = ((x_event-x0)**2+(y-y0)**2)**0.5
pos = l/l0
n = self.b1 - self.b0 + 1
db = int(n*pos)
b = self.b0 + db
pos1 = pos*n-db # position sur la barre en relatif
self.is_selected = (pos1, b, None) # pos1 : par rapport début barre, pos : début arc
return True
return False
def push_hover_value(self, cr, study, arc, drawing, n_case):
rdm = study.rdm
struct = rdm.struct
#unit_conv = rdm.struct.units
Char = rdm.GetCharByNumber(n_case)
status = drawing.status
pos1, b, pos = self.is_selected
node0 = struct.Barres[b][0]
angle = struct.Angles[b]
val, text = rdm.GetArcValue(Char, status, b, pos1)
#print("val=", val)
x0, y0 = struct.Nodes[node0]
X0, Y0 = drawing.x0, drawing.y0
struct_scale = drawing.struct_scale
chart_scale = drawing.chart_scale
l = struct.Lengths[b]
cr.push_group()
#print(cr.get_matrix())
drawing._fg.set_color_by_name(cr, "red")
cr.save()
cr.translate(X0 + x0*struct_scale, Y0 - y0*struct_scale)
cr.rotate(-angle)
dv = -val[1]*chart_scale
color = drawing._fg.get_nth_color(n_case)
drawing._fg.set_color_by_name(cr, color)
cr.set_font_size(Const.FONT_SIZE)
cr.move_to(0, dv-10)
cr.show_text("%s" % text)
cr.restore()
drawing._c_draw_legend_position(cr, struct, b, X0, Y0, x0, y0, l*pos1, val[0], val[1], False)
drawing.p_window = cr.pop_group()
def redraw(self, cr):
x0, y0 = self.x0, self.y0
x1, y1 = self.x1, self.y1
c0x, c0y = self.c0x, self.c0y
c1x, c1y = self.c1x, self.c1y
cr.move_to(x0, y0)
cr.curve_to(c0x, c0y, c1x, c1y, x1, y1)
class MCurveB(object):
"""segment de Bezier du tracé"""
def __init__(self, elem):
self.pt0, self.coefs, self.geom, self.bezier = elem
def get_is_selected(self, x_event, y_event, m):
u, dx, x1, dx1, angle = self.geom
ldefo = x1+dx1
x0, y0 = self.pt0 # origine de la barre en px
x_e = (x_event-x0)*math.cos(angle)+(y_event-y0)*math.sin(angle)
y_e = (x_event-x0)*math.sin(angle)-(y_event-y0)*math.cos(angle) # dans le rep de la barre en px
if x_e < u+dx or x_e > u+dx+ldefo:
return False
y_chart = self.f(x_e-dx, self.coefs)
if abs(y_chart-y_e) < m:
x, y = function.Rotation(angle, x_e, y_chart)
x, y = x+x0, y0-y # repère global en px
self.is_selected = (x, y, x_e)
return True
return False
def push_hover_value(self, cr, study, barre, drawing, n_case):
x, y, u = self.is_selected # x, y position sur courbe en px, u position sur intervalle
u0, du0, u1, du1, angle = self.geom
#print("resu=", u0, du0, u1, du1, u)
x0, y0 = self.pt0 # origine de la barre
struct_scale = drawing.struct_scale
if struct_scale == 0:
return
k = (u-u0-du0)/((u1+du1))
u0 /= struct_scale
u1 /= struct_scale
pos = u0+k*u1
self.is_selected = (x, y, pos)
if drawing.status == 7:
rdm = study.rdm
unit_conv = rdm.struct.units
Char = rdm.GetCharByNumber(n_case)
val = rdm.GetValue(barre, pos, Char, drawing.status)
valx, valy = val
text = (valx**2+valy**2)**0.5
text = function.PrintValue(text, unit_conv['L'])
elif drawing.status == 8:
rdm = study.influ_rdm
unit_conv = rdm.struct.units
i_obj = drawing.influ_list[n_case]
l = rdm.struct.Lengths[barre]
val = rdm.ValueLigneInf(barre, pos/l, i_obj.elem, i_obj.u, i_obj.status)
influ_unit = get_influ_conv(i_obj.status, unit_conv)
text = function.PrintValue(val, influ_unit)
val = (0., val) # provisoire
elif drawing.status == 6:
rdm = study.rdm
unit_conv = rdm.struct.units
Char = rdm.GetCharByNumber(n_case)
val = rdm.GetValue(barre, pos, Char, drawing.status)
valx, valy = val
text = function.PrintValue(valy, unit_conv['F']*unit_conv['L'])
else:
rdm = study.rdm
unit_conv = rdm.struct.units
Char = rdm.GetCharByNumber(n_case)
val = rdm.GetValue(barre, pos, Char, drawing.status)
valx, valy = val
text = function.PrintValue(valy, unit_conv['F'])
self.text = text
cr.push_group()
color = drawing._fg.get_nth_color(n_case)
drawing._fg.set_color_by_name(cr, color)
cr.save()
cr.set_font_size(Const.FONT_SIZE)
cr.move_to(x, y-20)
#cr.rotate(angle)
cr.show_text("%s" % text)
cr.restore()
drawing._draw_legend_position(cr, rdm.struct, barre, pos, val)
drawing.p_window = cr.pop_group()
def f(self, x, coefs):
"""La fonction polynomiale"""
rank = len(coefs)
y = 0
for c in coefs:
y += c*x**(rank-1)
rank -= 1
return y
def redraw(self, cr):
c1x, c1y, c2x, c2y, x, y = self.bezier
cr.curve_to(c1x, c1y, c2x, c2y, x, y)
class Barre(object):
"""Classes contenant une barre pour le mapping"""
def __init__(self, name, coors):
self.name = name
x0, y0, x1, y1 = coors
dx = x1-x0
dy = y1-y0
# 2 cas selon que la barre est plutot verticale ou horizontale
if abs(dx) >= abs(dy):
self.inv = False
# le sens de parcourt est choisi pour que x augmente
if dx >= 0:
self.coors = (x0, y0, dx, dy)
else:
self.coors = (x1, y1, -dx, -dy)
else:
self.inv = True
# le sens de parcourt est choisi pour que y augmente
if dy >= 0:
self.coors = (x0, y0, dx, dy)
else:
self.coors = (x1, y1, -dx, -dy)
def get_is_selected(self, x, y, m):
"""Retourne False or True si la barre est mappée"""
x0, y0, deltax, deltay = self.coors
if not self.inv:
if x < x0 or x > x0+deltax:
return False
if deltax == 0:
y1 = y0
else:
y1 = deltay * (x-x0) / deltax + y0
d = abs(y-y1)
if d < m:
return True
else:
if y < y0 or y > y0+deltay:
return False
if deltay == 0:
x1 = x0
else:
x1 = deltax * (y-y0) / deltay + x0
d = abs(x-x1)
if d < m:
return True
return False
def redraw(self, cr):
"""Redessine la barre à partir des données du mapping"""
x0, y0, dx, dy = self.coors
cr.move_to(x0, y0)
cr.rel_line_to(dx, dy)
cr.stroke()
draw_square(cr, x0, y0)
draw_square(cr, x0+dx, y0+dy)
class AreaMapping(object):
"""Classes contenant les opérations de mapping pour la sélection des objects dans le drawingarea"""
def __init__(self):
#print("init AreaMapping")
self.curves = {}
self.nodes = {}
self.bars = {}
self.box = {} # x, y -> coin Haut Gauche, w, h
self.curve_values = {}
self.infos = {}
# ajouter points
def remove_map(self, drawing_id):
"""Supprime les données du diagramme drawing"""
try:
del(self.nodes[drawing_id])
except KeyError:
pass
try:
del(self.bars[drawing_id])
except KeyError:
pass
try:
del(self.infos[drawing_id])
except KeyError:
pass
#print('remove self.bars',self.bars)
def clear(self, drawing_id):
"""Efface certains éléments pour le mapping"""
try:
self.curves[drawing_id] = {}
self.bars[drawing_id] = []
self.curve_values[drawing_id] = {}
except KeyError:
pass
def set_mapping_arc(self, cr, scale, name, arc, drawing_id, centers):
"""Crée le mapping pour un arc de cercle"""
center, r, teta1, teta2 = arc.c, arc.r, arc.teta1, arc.teta2
xc, yc = centers[center]
yc = -yc
x_d, y_d = cr.user_to_device(xc, yc)
Arc = MArc(name, (x_d, y_d, r*scale, teta1, teta2))
self.bars.setdefault(drawing_id, []).append(Arc)
def set_mapping_para(self, cr, scale, name, para, drawing_id, node0, node1):
"""Crée le mapping pour une parabole"""
x0, y0 = node0
x1, y1 = node1
C1x, C1y, C2x, C2y = para.bezier
a, b = para.coefs
a = a/scale
l = para.l*scale
C1x, C1y = cr.user_to_device(x0+C1x, -y0+C1y)
C2x, C2y = cr.user_to_device(x0+C2x, -y0+C2y)
x0, y0 = cr.user_to_device(x0, -y0)
C1x, C1y = C1x-x0, C1y-y0
C2x, C2y = C2x-x0, C2y-y0
x1, y1 = cr.user_to_device(x1, -y1)
#print(cr.device_to_user_distance(1, 1))
bezier_pts = (C1x, C1y, C2x, C2y, x1-x0, y1-y0)
Para = MParabola(name, ((x0, y0), (0., a, b, 0.), (0., 0., l, 0., para.a), bezier_pts))
# utilité de tous ces 0 ??
self.bars.setdefault(drawing_id, []).append(Para)
def set_mapping_bars(self, drawing_id, nodes, bars, box):
"""Crée le mapping pour une barre utilisateur"""
di = {}
for name, coors in nodes.items():
di[name] = Node(name, coors)
self.nodes[drawing_id] = di
for name, coors in bars.items():
bar = Barre(name, coors)
self.bars.setdefault(drawing_id, []).append(bar)
self.set_box(drawing_id, box)
def set_curve_values(self, drawing, values, n_case, name='bar'):
"""Remplit un dictionnaire {drawing_id: {n_case: {(x, y): Obj}}} de d'instances de classes Value"""
#print("set_curve_values", n_case, name, values)
def get_object(name, u, barre, tu):
if name == "arc":
return CurveValue(u, barre, tu)
elif name == "bar":
return BarreValue(u, barre, tu)
elif name == "reac":
return ReacValue(u, barre, tu)
return None
drawing_id = drawing.id
status = drawing.status
x0, y0, w, h = self.box[drawing_id]
user_values = drawing.user_values.get(status, {}).get(n_case, {})
objs = []
for barre in values:
are_moved = user_values.get(barre, {})
for u in values[barre]:
is_moved = are_moved.get(u, {})
data = values[barre][u]
for li in data:
text, x, y, a, pos = li[0:5]
if is_moved:
try:
dx, dy, hide = is_moved[pos]
if hide: continue
li[1] -= dx
li[2] -= dy
x -= dx
y -= dy
except KeyError:
pass
Obj = get_object(name, u, barre, li)
objs.append(Obj)
wtext = len(text)*4 # demie largeur
if x-wtext < x0:
w += x0 - x+wtext
x0 = x-wtext
elif x+wtext > x0+w:
w = x - x0+wtext
if y < y0:
h += y0 - y
y0 = y-10
elif y > y0+h:
h = y - y0 + 32
self.curve_values.setdefault(drawing_id, {})
self.curve_values[drawing_id].setdefault(n_case, []).extend(objs)
self.extend_box(drawing_id, x0, y0)
self.extend_box(drawing_id, x0+w, y0+h)
def set_info(self, id, obj):
"""Stocke l'info"""
#print("set_info", id)
if not id in self.infos:
self.infos[id] = {}
self.infos[id][obj.id] = obj
def set_moved_info(self, id, info_id, box):
"""Position de la boite du contour d'une info"""
x, y, w, h = box
self.infos[id][info_id].box = (int(x), int(y), int(w), int(h)) # coin haut gauche, largeur, hauteur
def set_box(self, id, box):
"""Position de la boite du contour d'un drawing"""
x, y, w, h = box
self.box[id] = (int(x), int(y), int(w), int(h)) # coin haut gauche, width, height
def extend_box(self, id, x, y, Hm=0, Vm=0):
"""Modifie la taille de la boite pour tenir compte des légendes et autres"""
x0, y0, w, h = self.box[id]
x1, y1 = x0+w, y0+h
change = False
if x < x0:
change = True
x0 = x-Hm
elif x > x1:
x1 = x+Hm
change = True
if y < y0:
y0 = y-Vm
change = True
elif y > y1:
y1 = y+Vm
change = True
if change:
self.box[id] = (int(x0), int(y0), int(x1-x0), int(y1-y0))
# si degré >= 4, on coupe le tronçon en deux et les coefs a, b, c, d sont stockés deux fois
def set_curves(self, drawing_id, n_case, barre, points):
"""Contient les informations sur les courbes, redimensionne la boite du contour"""
#print("set_curves", barre, points)
if not drawing_id in self.curves:
self.curves[drawing_id] = {}
if not n_case in self.curves[drawing_id]:
self.curves[drawing_id][n_case] = {}
curves = self.curves[drawing_id][n_case][barre] = []
x0, y0, w, h = self.box[drawing_id]
x1, y1 = x0+w, y0+h
for elem in points:
if len(elem) == 1:
x, y = elem[0]
self.extend_box(drawing_id, x, y, 20, 20)
curves.append(MCurveDisc(x, y))
elif len(elem) == 3:
x, y = elem[1]
self.extend_box(drawing_id, x, y, 20, 20)
curves.append(MCurveSeg(elem))
elif len(elem) == 4:
bezier = elem[3]
Cx1, Cy1, Cx2, Cy2, endx, endy = bezier
#self.extend_box(drawing_id, Cx1, Cy1)
#self.extend_box(drawing_id, Cx2, Cy2)
# Faudrait caler les dim de la box à partir des valeurs de rdm.bar_values
# à paramétrer en attendant mieux
self.extend_box(drawing_id, (Cx1+Cx2)/2, (Cy1+Cy2)/2)
self.extend_box(drawing_id, endx, endy, 20, 20)
curves.append(MCurveB(elem))
else:
print("debug in set_curves", len(elem), elem)
def set_arc_curves(self, drawing_id, n_case, arc, points):
"""Crée le mapping pour les courbes des arcs"""
#print("set_arc_curves", arc, points)
if not drawing_id in self.curves:
self.curves[drawing_id] = {}
if not n_case in self.curves[drawing_id]:
self.curves[drawing_id][n_case] = {}
curves = self.curves[drawing_id][n_case][arc] = []
for elem in points:
for tu in elem:
curves.append(MCurveArc(tu))
self.extend_box(drawing_id, tu[6], tu[7])
def select_curve_values(self, x, y, is_selected):
"""Retourne la légende sélectionnée"""
if not is_selected:
return False
drawing = is_selected[1]