Salome HOME
Merge branch V7_3_1_BR
[modules/smesh.git] / src / Tools / MacMesh / MacMesh / SharpAngle.py
1 # Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2
3 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22
23
24 # This is an automation of the sharp angle object, with a corner at (X0,Y0), side length : Extension and a fine local meshing : LocalMeshing
25 # The corner orientation is defined as NE (North-East) , NW (North-West), SE, or SW. The object's "arm" is 8/14 of Extension
26 #                       |      |     8      6
27 #                -------        ---------
28 #       ---->   |                        |   <----
29 #               |   NW             NE    |  oo
30 #          _____|                        |_____
31
32 import sys, math, commands
33 CWD = commands.getoutput('pwd')
34 sys.path.append(CWD)
35
36 from MacObject import *
37 from CompositeBox import *
38 import Config, GenFunctions
39
40 def SharpAngleOut (X0 , Y0 , DX , DY , DLocal, LocalMeshing , CornerOrientation , NLevels, **args) : 
41         if DLocal == 'auto' : DLocal = float(min(DX,DY))
42
43         BoxSide = DLocal/(2.**(NLevels+1))
44         InternalMeshing = int(math.ceil(BoxSide/(3*LocalMeshing)))
45         InternalMeshing = InternalMeshing+InternalMeshing%2     # An even number is needed, otherwise the objects would not be compatible once created
46         if InternalMeshing == 0 : InternalMeshing = 2           # This sets a minimum meshing condition in order to avoid an error. The user is notified of the value considered for the local meshing
47         print "Possible Local meshing is :", BoxSide/(3*InternalMeshing), "\nThis value is returned by this function for your convenience"
48
49         DirPar =     {'NE' : lambda : ['NE', 'NW', 'SE', 'EW', 'NW', 'SN', 'SN', 'NE', 'WE', 'WE', 'SE', 'NS'],
50                       'NW' : lambda : ['NW', 'NE', 'SW', 'WE', 'NE', 'SN', 'SN', 'NW', 'EW', 'EW', 'SW', 'NS'],
51                       'SE' : lambda : ['SE', 'SW', 'NE', 'EW', 'SW', 'NS', 'NS', 'SE', 'WE', 'WE', 'NE', 'SN'],
52                       'SW' : lambda : ['SW', 'SE', 'NW', 'WE', 'SE', 'NS', 'NS', 'SW', 'EW', 'EW', 'NW', 'SN'], }[CornerOrientation]()
53
54         CoefVer =   {'NE' : lambda :  1,
55                      'NW' : lambda :  1,
56                      'SE' : lambda : -1,
57                      'SW' : lambda : -1, }[CornerOrientation]()
58
59         CoefHor =   {'NE' : lambda :  1,
60                      'NW' : lambda : -1,
61                      'SE' : lambda :  1,
62                      'SW' : lambda : -1, }[CornerOrientation]()
63                      
64         ToLook  = {'NE' : lambda : [0,2,1,3],
65                    'NW' : lambda : [0,3,1,2],
66                    'SE' : lambda : [1,2,0,3],
67                    'SW' : lambda : [1,3,0,2], }[CornerOrientation]()        
68        
69         if args.__contains__('groups') :
70                 GroupNames = args['groups']
71         else : GroupNames = [None, None, None, None, None, None]
72         
73         GN00 = GroupArray(ToLook[0],GroupNames[0])
74         GN01 = GroupArray(ToLook[1],GroupNames[1])
75         
76         GN1 = GroupArray([ToLook[0],ToLook[1]],[GroupNames[0],GroupNames[5]])
77         GN7 = GroupArray([ToLook[0],ToLook[1]],[GroupNames[4],GroupNames[1]])
78         
79         if DY == DLocal :
80                 GN2 = GroupArray([ToLook[1],ToLook[2]],[GroupNames[5],GroupNames[2]])
81                 GN3 = GroupArray(ToLook[2],GroupNames[2])
82                 if DX == DLocal:
83                         GN4 = GroupArray([ToLook[2],ToLook[3]],[GroupNames[2],GroupNames[3]])
84                         GN5 = GroupArray(ToLook[3],GroupNames[3])
85                         GN6 = GroupArray([ToLook[3],ToLook[0]],[GroupNames[3],GroupNames[4]])
86                 else :
87                         GN4 = GroupArray(ToLook[2],GroupNames[2])
88                         GN5 = [None,None,None,None]
89                         GN6 = GroupArray(ToLook[0],GroupNames[4])
90                         GN21 = GroupArray([ToLook[3],ToLook[0],ToLook[2]],[GroupNames[3],GroupNames[4],GroupNames[2]])
91         else :
92                 GN2 = GroupArray(ToLook[1],GroupNames[5])
93                 GN3 = [None,None,None,None]
94                 if DX == DLocal:
95                         GN4 = GroupArray(ToLook[3],GroupNames[3])
96                         GN5 = GroupArray(ToLook[3],GroupNames[3])
97                         GN6 = GroupArray([ToLook[3],ToLook[0]],[GroupNames[3],GroupNames[4]])
98                         GN22 = GroupArray([ToLook[1],ToLook[2],ToLook[3]],[GroupNames[5],GroupNames[2],GroupNames[3]])
99                 else :
100                         GN4 = [None,None,None,None]
101                         GN5 = [None,None,None,None]
102                         GN6 = GroupArray(ToLook[0],GroupNames[4])
103                         GN21 = GroupArray([ToLook[3],ToLook[0]],[GroupNames[3],GroupNames[4]])
104                         GN22 = GroupArray([ToLook[1],ToLook[2]],[GroupNames[5],GroupNames[2]])
105                         GN23 = GroupArray([ToLook[2],ToLook[3]],[GroupNames[2],GroupNames[3]])
106                         
107         Obj = []
108
109         Obj.append(MacObject('BoxAng32',[(X0+CoefHor*BoxSide/2,Y0+CoefVer*BoxSide/2),(BoxSide,BoxSide)],[InternalMeshing,DirPar[0]]))
110         Obj.append(MacObject('BoxAng32',[(X0-CoefHor*BoxSide/2,Y0+CoefVer*BoxSide/2),(BoxSide,BoxSide)],['auto',DirPar[1]], groups = GroupArray(ToLook[0],GroupNames[0])))
111         Obj.append(MacObject('BoxAng32',[(X0+CoefHor*BoxSide/2,Y0-CoefVer*BoxSide/2),(BoxSide,BoxSide)],['auto',DirPar[2]], groups = GroupArray(ToLook[1],GroupNames[1])))
112         
113         for N in range (1,NLevels+1):
114                 n = N-1
115                 if N < NLevels : 
116                        Obj.append(MacObject('Box42',[(X0-CoefHor*BoxSide*(2**n)*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[3]]      , groups = GN00))
117                        Obj.append(MacObject('BoxAng32',[(X0-CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[4]]               ))
118                        Obj.append(MacObject('Box42',[(X0-CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[5]]                    ))
119                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[6]]                    ))
120                        Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[7]]               ))
121                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[8]]                    ))
122                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0-CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[9]]                    ))
123                        Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0-CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[10]]              ))
124                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0-CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[11]]     , groups = GN01))
125                 else :
126                        Obj.append(MacObject('Box42',[(X0-CoefHor*BoxSide*(2**n)*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[3]]      , groups = GN1))
127                        Obj.append(MacObject('BoxAng32',[(X0-CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[4]] , groups = GN2))
128                        Obj.append(MacObject('Box42',[(X0-CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[5]]      , groups = GN3))
129                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[6]]      , groups = GN3))
130                        Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[7]] , groups = GN4))
131                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[8]]      , groups = GN5))
132                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0-CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[9]]      , groups = GN5))
133                        Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0-CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[10]], groups = GN6))
134                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0-CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[11]]     , groups = GN7))
135                        
136         OuterMeshing = (3/2)*InternalMeshing*2**(NLevels-1)
137         OuterSegLength = (DLocal/OuterMeshing)
138
139         if DX > DLocal :
140                 dX = DX - DLocal
141                 Obj.append(MacObject('CompBoxF',[(X0+CoefHor*(DX)/2.,Y0),(dX,DLocal)],['auto'], groups = GN21))
142         if DY > DLocal :
143                 dY = DY - DLocal
144                 if DX > DLocal : 
145                         Obj.append(MacObject('CompBoxF',[(X0+CoefHor*DX/2.,Y0+CoefVer*(DY)/2.),(DX-DLocal,dY)],['auto'], groups = GN23))
146
147                 Obj.append(MacObject('CompBoxF',[(X0,Y0+CoefVer*(DY)/2.),(DLocal,dY)],['auto'], groups = GN22))
148                 
149         return Obj
150
151 def SharpAngleIn (X0 , Y0 , DX , DY , DLocal, LocalMeshing , CornerOrientation , NLevels, **args) : 
152         if DLocal == 'auto' : DLocal = float(min(DX,DY))
153
154         BoxSide = DLocal/(2.**(NLevels))
155         InternalMeshing = int(math.ceil(BoxSide/(3*LocalMeshing)))
156         InternalMeshing = InternalMeshing+InternalMeshing%2     # An even number is needed, otherwise the objects would not be compatible once created
157         if InternalMeshing == 0 : InternalMeshing = 2           # This sets a minimum meshing condition in order to avoid an error. The user is notified of the value considered for the local meshing
158         print "Possible Local meshing is :", BoxSide/(3*InternalMeshing), "\nThis value is returned by this function for your convenience..."
159
160         DirPar =     {'NE' : lambda : ['NE', 'SN', 'NE', 'WE'],
161                       'NW' : lambda : ['NW', 'SN', 'NW', 'EW'],
162                       'SE' : lambda : ['SE', 'NS', 'SE', 'WE'],
163                       'SW' : lambda : ['SW', 'NS', 'SW', 'EW'], }[CornerOrientation]()
164
165         CoefVer =   {'NE' : lambda :  1,
166                      'NW' : lambda :  1,
167                      'SE' : lambda : -1,
168                      'SW' : lambda : -1, }[CornerOrientation]()
169
170         CoefHor =   {'NE' : lambda :  1,
171                      'NW' : lambda : -1,
172                      'SE' : lambda :  1,
173                      'SW' : lambda : -1, }[CornerOrientation]()
174                      
175         ToLook  = {'NE' : lambda : [0,2,1,3],
176                    'NW' : lambda : [0,3,1,2],
177                    'SE' : lambda : [1,2,0,3],
178                    'SW' : lambda : [1,3,0,2], }[CornerOrientation]()
179                                                
180         if args.__contains__('groups') :
181                 GroupNames = args['groups']
182         else : GroupNames = [None, None, None, None]
183         
184         GN01 = GroupArray([ToLook[0],ToLook[1]],[GroupNames[ToLook[0]],GroupNames[ToLook[1]]])
185         GN02 = GroupArray(ToLook[1],GroupNames[ToLook[1]])        
186         GN03 = [None, None, None, None]
187         GN04 = GroupArray(ToLook[0],GroupNames[ToLook[0]])  
188
189         if DY == DLocal :
190                 GN05 = GroupArray([ToLook[1],ToLook[2]],[GroupNames[ToLook[1]],GroupNames[ToLook[2]]])
191                 GN08 = GroupArray([ToLook[0],ToLook[2],ToLook[3]],[GroupNames[ToLook[0]],GroupNames[ToLook[2]],GroupNames[ToLook[3]]])
192                 if DX == DLocal:
193                         GN06 = GroupArray([ToLook[2],ToLook[3]],[GroupNames[ToLook[2]],GroupNames[ToLook[3]]])
194                         GN07 = GroupArray([ToLook[0],ToLook[3]],[GroupNames[ToLook[0]],GroupNames[ToLook[3]]])
195                 else :
196                         GN06 = GroupArray(ToLook[2],GroupNames[ToLook[2]])
197                         GN07 = GroupArray(ToLook[0],GroupNames[ToLook[0]])
198         else :
199                 GN05 = GroupArray(ToLook[1],GroupNames[ToLook[1]])
200                 if DX == DLocal : 
201                         GN06 = GroupArray(ToLook[3],GroupNames[ToLook[3]])
202                         GN07 = GroupArray([ToLook[0],ToLook[3]],[GroupNames[ToLook[0]],GroupNames[ToLook[3]]])
203                         GN10 = GroupArray([ToLook[1],ToLook[2],ToLook[3]],[GroupNames[ToLook[1]],GroupNames[ToLook[2]],GroupNames[ToLook[3]]])
204                 else :
205                         GN06 = [None, None, None, None]
206                         GN07 = GroupArray(ToLook[0],GroupNames[ToLook[0]])
207                         GN08 = GroupArray([ToLook[0],ToLook[3]],[GroupNames[ToLook[0]],GroupNames[ToLook[3]]])
208                         GN09 = GroupArray([ToLook[2],ToLook[3]],[GroupNames[ToLook[2]],GroupNames[ToLook[3]]])
209                         GN10 = GroupArray([ToLook[1],ToLook[2]],[GroupNames[ToLook[1]],GroupNames[ToLook[2]]])
210          
211         Obj = []
212
213         Obj.append(MacObject('BoxAng32',[(X0+CoefHor*BoxSide/2,Y0+CoefVer*BoxSide/2),(BoxSide,BoxSide)],[InternalMeshing,DirPar[0]],groups = GN01))
214         
215         for N in range (1,NLevels+1):
216                 n = N-1
217                 if N < NLevels : 
218                         Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[1]],groups = GN02))
219                         Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[2]],groups = GN03))
220                         Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[3]],groups = GN04))
221                 else :
222                         Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[1]],groups = GN05))
223                         Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[2]],groups = GN06))
224                         Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[3]],groups = GN07))
225  
226         OuterMeshing = (3/2)*InternalMeshing*2**(NLevels-1)
227         OuterSegLength = (DLocal/OuterMeshing)
228
229         if DX > DLocal :
230                 dX = DX - DLocal
231                 Obj = Obj + CompositeBox(X0+CoefHor*(DLocal+dX/2.),Y0+CoefVer*(DLocal)/2.,dX,DLocal, groups = GN08)
232         if DY > DLocal :
233                 dY = DY - DLocal
234
235                 if DX > DLocal : 
236                         Obj = Obj + CompositeBox(X0+CoefHor*(DLocal+(DX-DLocal)/2.),Y0+CoefVer*(DLocal+dY/2.),DX-DLocal,dY, groups = GN09)
237
238                 Obj = Obj + CompositeBox(X0+CoefHor*DLocal/2,Y0+CoefVer*(DLocal+dY/2.),DLocal,dY,groups = GN10)
239
240         return Obj
241
242 def GroupArray(indices, GroupNames) :
243         if type(indices) is int : 
244                 indices = [indices]
245                 GroupNames = [GroupNames]
246         Output = [None,None,None,None]
247         for i, ind in enumerate(indices) : 
248                 Output[ind] = GroupNames[i]
249         return Output