Salome HOME
Copyright update 2022
[modules/smesh.git] / src / Tools / blocFissure / gmu / whichSide.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2014-2022  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 """Calcul de la position d'une shape par rapport à une face (dessus, dessous, sur la surface même)"""
21
22 import logging
23 from .geomsmesh import geompy
24
25 def whichSide(face, obj, tol = 1.e-3):
26   """
27   Position d'un objet par rapport a une face (non nécessairement plane).
28   L'objet est supposé situé entierement d'un coté de la face,
29   ou lui appartenant totalement (objets traversants non pris en compte)
30   renvoie 1 si 'objet est du coté de la normale à la face,
31   -1 de l'autre coté, 0 si il est sur la face
32   """
33   logging.debug('start')
34   side = 0
35   logging.debug("shape info %s", geompy.ShapeInfo(obj))
36   nbEdges = geompy.NbShapes(obj, geompy.ShapeType["EDGE"]) # --- attention ! pour une seule edge presente, renvoie 2
37   logging.debug("   nbEdges %s", nbEdges)
38   nbFaces = geompy.NbShapes(obj, geompy.ShapeType["FACE"]) # --- attention ! pour une seule face presente, renvoie 2
39   logging.debug("   nbFaces %s", nbFaces)
40   vertices = geompy.ExtractShapes(obj, geompy.ShapeType["VERTEX"], False)
41
42   if nbEdges > 0 and nbFaces == 0: # --- edges
43     if nbEdges <= 2:
44       point = geompy.MakeVertexOnCurve(obj, 0.5)
45       vertices.append(point)
46     else:
47       edges = geompy.ExtractShapes(obj, geompy.ShapeType["EDGE"], False)
48       for anEdge in edges:
49         point = geompy.MakeVertexOnCurve(anEdge, 0.5)
50         vertices.append(point)
51
52   elif nbFaces >0: # --- faces
53     if nbFaces <=2:
54       point = geompy.MakeVertexOnSurface(obj, 0.5, 0.5)
55       vertices.append(point)
56     else:
57       faces = geompy.ExtractShapes(obj, geompy.ShapeType["FACE"], False)
58       for aFace in faces:
59         point = geompy.MakeVertexOnSurface(aFace, 0.5, 0.5)
60         vertices.append(point)
61
62   else: # --- vertices
63     vertices = [obj]
64
65   for vertex in vertices:
66     distance = geompy.MinDistance(vertex, face)
67     logging.debug("    distance %s", distance)
68     if distance > tol:
69       projection = geompy.MakeProjection(vertex, face)
70       normal = geompy.GetNormal(face, projection)
71       vect = geompy.MakeVector(projection, vertex)
72       angle = geompy.GetAngle(normal, vect)
73       logging.debug("  angle %s", angle)
74       side = 1
75       if abs(angle) > 10:
76         side = -1
77       break
78   logging.debug("  side %s", side)
79
80   return side