Salome HOME
Update copyrights
[modules/smesh.git] / src / Tools / blocFissure / gmu / creeZoneDefautGeom.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2014-2019  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import logging
22 from .geomsmesh import geompy
23 from .geomsmesh import geomPublish
24 from .geomsmesh import geomPublishInFather
25 from . import initLog
26 from .prolongeVertices import prolongeVertices
27
28 # -----------------------------------------------------------------------------
29 # --- zone de defaut, constructions geometrique avec CAO d'origine
30
31 def creeZoneDefautGeom(objetSain, shapeDefaut, origShapes, verticesShapes, dmoyen, lgExtrusion=50):
32   """
33   Construction CAO de la zone à remailler, quand on utilise la CAO d'origine,
34   apres appel creeZoneDefautMaillage
35   @param objetSain : la géometrie de l'objet initial
36   @param shapeDefaut : objet géometrique représentant la fissure
37   (selon les cas, un point central, ou une shape plus complexe,
38   dont on ne garde que les vertices)
39   @param origShapes : liste id subShapes
40   @param verticesShapes : listes noeuds de bord
41   @param dmoyen : longueur arete moyenne bord
42   @lgExtrusion : distance d'extrusion de la face du defaut
43   (ne vaut que pour des fissures courtes)
44   @return (facesDefaut, centreDefaut, normalDefaut, extrusionDefaut)
45   """
46   logging.info("start")
47
48   trace = True
49   faces = []
50   curves = []
51   cdgs = []
52   projs = []
53   normals = []
54   extrusions = []
55   partitions = []
56   decoupes = []
57
58   for ishape, vertices in enumerate(verticesShapes):
59     aShape = origShapes[ishape]
60     [face] = geompy.SubShapes(objetSain, [aShape])
61     faces.append(face)
62     curve = geompy.MakePolyline(vertices, False)
63     curves.append(curve)
64     if trace:
65       name="poly_%d"%aShape
66       geomPublish(initLog.debug, curve, name)
67     #
68     cdg = geompy.MakeCDG(curve)
69     cdgs.append(cdg)
70     if trace:
71       name="cdgpoly_%d"%aShape
72       geomPublish(initLog.debug, cdg, name)
73     #
74     projCdg = geompy.MakeProjection(cdg, face)
75     projs.append(projCdg)
76     if trace:
77       name="projCdg_%d"%aShape
78       geomPublish(initLog.debug, projCdg, name)
79     #
80     normal = geompy.GetNormal(face, projCdg)
81     normals.append(normal)
82     if trace:
83       name="normal_%d"%aShape
84       geomPublish(initLog.debug, normal, name)
85     #
86     extrusion = geompy.MakePrismVecH2Ways(curve, normal, 10)
87     extrusions.append(extrusion)
88     if trace:
89       name="extrusion_%d"%aShape
90       geomPublish(initLog.debug, extrusion, name)
91     #
92     verticesProlongees = prolongeVertices(vertices)
93     #
94     curveprol = geompy.MakePolyline(verticesProlongees, False)
95     if trace:
96       name="polyProl_%d"%aShape
97       geomPublish(initLog.debug, curveprol, name)
98     #
99     extruprol = geompy.MakePrismVecH2Ways(curveprol, normal, 10)
100     if trace:
101       name="extruProl_%d"%aShape
102       geomPublish(initLog.debug, extruprol, name)
103     #
104     partition = geompy.MakePartition([face], [extruprol], [], [], geompy.ShapeType["FACE"], 0, [], 0)
105     partitions.append(partition)
106     if trace:
107       name="partition_%d"%aShape
108       geomPublish(initLog.debug, partition, name)
109     pass
110   #
111
112   centreSphere = geompy.MakeCDG(shapeDefaut)
113   geomPublish(initLog.debug, centreSphere, "cdg_defaut")
114   ccurves = geompy.MakeCompound(curves)
115   gravCenter = geompy.MakeCDG(ccurves)
116   geomPublish(initLog.debug, gravCenter, "cdg_curves")
117   for i in range(len(partitions)):
118     if trace:
119       logging.debug(" --- original shape %s", origShapes[i])
120     dists = []
121     facesToSort = []
122     subFaces = geompy.ExtractShapes(partitions[i], geompy.ShapeType["FACE"], True)
123     for aFace in subFaces:
124       cdg = geompy.MakeCDG(aFace)
125       distance = geompy.MinDistance(cdg, centreSphere)
126       dists.append(distance)
127       facesToSort.append(aFace)
128       if trace:
129         logging.debug("distance = %s", distance)
130         pass
131       pass
132     if len(dists) > 0:
133       minDist = min(dists)
134       for j,d in enumerate(dists):
135         if d == minDist:
136           aFace = facesToSort[j]
137           name="decoupe_%d"%origShapes[i]
138           geomPublish(initLog.debug, aFace, name)
139           decoupes.append(aFace)
140           break
141         pass
142       pass
143
144   facesDefaut = decoupes[0]
145   if len(decoupes) > 1:
146     facesDefaut = geompy.MakePartition(decoupes, [], [], [], geompy.ShapeType["FACE"], 0, [], 0)
147   geomPublish(initLog.debug, facesDefaut, "facesDefaut")
148
149   shells=[]
150   if len(decoupes) > 1: # plusieurs faces de defaut
151     subFaces = geompy.ExtractShapes(facesDefaut, geompy.ShapeType["FACE"], True)
152     # --- regroupe les subFaces en shells connectes
153     theFaces = list(subFaces) # copy
154     while len(theFaces) > 0:
155       logging.debug("------- len(theFaces) %s" , len(theFaces))
156       theFace = theFaces[0]
157       logging.debug("  start with face %s",theFaces[0])
158       theFaces[0:1] = []
159       aShell = [theFace]
160       toAdd =[theFace]
161       while len(toAdd) > 0:
162         toAdd = []
163         toRemove = []
164         for i in range(len(theFaces)):
165           logging.debug("  try %s", theFaces[i])
166           for aFace in aShell:
167             logging.debug("    with %s", aFace)
168             try:
169               edge = geompy.GetSharedShapesMulti([aFace, theFaces[i]], geompy.ShapeType["EDGE"])
170               edgeShared = True
171             except:
172               edgeShared = False
173             if edgeShared:
174               if theFaces[i] not in toAdd:
175                 toAdd.append(theFaces[i])
176                 toRemove.append(i)
177                 logging.debug("    --- add %s", theFaces[i])
178         aShell += toAdd
179         for k in sorted(toRemove, reverse=True):
180           theFaces[k:k+1] = []
181       theShell = geompy.MakeShell(aShell)
182       name = "theShell%d"%len(shells)
183       geomPublish(initLog.debug, theShell,name)
184       shells.append(theShell)
185     #
186     distances = []
187     for aShell in shells: # --- trouver le shell en contact avec la fissure
188       distances.append(geompy.MinDistance(aShell, shapeDefaut))
189     minDist = min(distances)
190     for index in range(len(distances)):
191       if distances[index] == minDist:
192         break
193     theShellDefaut = shells[index]
194     #
195   else: # --- une seule face de defaut
196     subFaces = [facesDefaut]
197     theShellDefaut = geompy.MakeShell(subFaces)
198   if trace:
199     geomPublish(initLog.debug, theShellDefaut,"theShellDefaut")
200
201   theFaces = geompy.ExtractShapes(theShellDefaut, geompy.ShapeType["FACE"], True)
202   distances = []
203   for aFace in theFaces:
204     distances.append(geompy.MinDistance(aFace, centreSphere))
205   minDist = min(distances)
206   for index in range(len(distances)):
207     if distances[index] == minDist:
208       break
209
210   centreDefaut = geompy.MakeProjection(centreSphere, theFaces[index])
211   if trace:
212     geomPublish(initLog.debug, centreDefaut, "centreDefaut")
213   normalDefaut = geompy.GetNormal(subFaces[index], centreDefaut)
214   if trace:
215     geomPublish(initLog.debug, normalDefaut, "normalDefaut")
216   extrusionDefaut = geompy.MakePrismVecH(theShellDefaut, normalDefaut, -lgExtrusion)
217   info = geompy.ShapeInfo(extrusionDefaut)
218   logging.debug("shape info %s", info)
219   if (info['SOLID'] > 1) or (info['COMPOUND'] > 0) :
220     solids = geompy.ExtractShapes(extrusionDefaut, geompy.ShapeType["SOLID"], True)
221     solid0 = solids[0]
222     for i in range(1,len(solids)):
223       solid0 = geompy.MakeFuse(solid0, solids[i])
224     extrusionDefaut = solid0
225   if trace:
226     geomPublish(initLog.debug, extrusionDefaut, "extrusionDefaut")
227
228   return facesDefaut, centreDefaut, normalDefaut, extrusionDefaut