Salome HOME
Fix regression mesh_BLSURF_02/C1
[plugins/blsurfplugin.git] / src / BLSURFPlugin / BLSURFPluginBuilder.py
index 4c2316e74e37c98098285f8020e3705382447bfe..f06850f72f37704dbb05219f267b802e8d198cc3 100644 (file)
@@ -1,9 +1,9 @@
-# Copyright (C) 2007-2013  CEA/DEN, EDF R&D
+# Copyright (C) 2007-2015  CEA/DEN, EDF R&D
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
 # License as published by the Free Software Foundation; either
-# version 2.1 of the License.
+# version 2.1 of the License, or (at your option) any later version.
 #
 # This library is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 ##
 # @package BLSURFPluginBuilder
-# Python API for the BLSURF meshing plug-in module.
+# Python API for the MG-CADSurf meshing plug-in module.
 
 from salome.smesh.smesh_algorithm import Mesh_Algorithm
-from salome.smesh.smeshBuilder import AssureGeomPublished
 
-# Topology treatment way of BLSURF
+LIBRARY = "libBLSURFEngine.so"
+
+# Topology treatment way of MG-CADSurf
 FromCAD, PreProcess, PreProcessPlus, PreCAD = 0,1,2,3
 
-# Element size flag of BLSURF
-DefaultSize, DefaultGeom, BLSURF_GlobalSize, BLSURF_LocalSize = 0,0,1,2
+# Element size flag of MG-CADSurf
+DefaultSize, DefaultGeom, MG_CADSURF_GlobalSize, MG_CADSURF_LocalSize = 0,0,1,2
 # Retrocompatibility
-BLSURF_Custom, SizeMap = BLSURF_GlobalSize, BLSURF_LocalSize
-
+MG_CADSURF_Custom, SizeMap = MG_CADSURF_GlobalSize, MG_CADSURF_LocalSize
+BLSURF_Custom, BLSURF_GlobalSize, BLSURF_LocalSize = MG_CADSURF_Custom, MG_CADSURF_GlobalSize, MG_CADSURF_LocalSize
 
 # import BLSURFPlugin module if possible
 noBLSURFPlugin = 0
@@ -45,28 +46,29 @@ except ImportError:
 # Mesh algo type identifiers
 #----------------------------
 
-## Algorithm type: BLSurf triangle 2D algorithm, see BLSURF_Algorithm
-BLSURF = "BLSURF"
+## Algorithm type: MG-CADSurf triangle algorithm, see BLSURF_Algorithm
+MG_CADSurf = "MG-CADSurf"
+BLSURF = MG_CADSurf
 
 #----------------------
 # Algorithms
 #----------------------
 
-## BLSurf 2D algorithm.
+## MG-CADSurf 2D algorithm.
 #
-#  It can be created by calling smesh.Mesh.Triangle(smesh.BLSURF,geom=0)
+#  It can be created by calling smeshBuilder.Mesh.Triangle(smeshBuilder.MG-CADSurf,geom=0)
 #
 class BLSURF_Algorithm(Mesh_Algorithm):
 
-  ## name of the dynamic method in smesh.Mesh class
+  ## name of the dynamic method in smeshBuilder.Mesh class
   #  @internal
   meshMethod = "Triangle"
-  ## type of algorithm used with helper function in smesh.Mesh class
+  ## type of algorithm used with helper function in smeshBuilder.Mesh class
   #  @internal
-  algoType   = BLSURF
+  algoType   = MG_CADSurf
   ## doc string of the method
   #  @internal
-  docHelper  = "Creates triangle 2D algorithm for faces"
+  docHelper  = "Creates triangle algorithm for faces"
 
   _anisotropic_ratio = 0
   _bad_surface_element_aspect_ratio = 1000
@@ -83,28 +85,29 @@ class BLSURF_Algorithm(Mesh_Algorithm):
     Mesh_Algorithm.__init__(self)
     if noBLSURFPlugin:
       print "Warning: BLSURFPlugin module unavailable"
-    self.Create(mesh, geom, BLSURF, "libBLSURFEngine.so")
+    self.Create(mesh, geom, BLSURF, LIBRARY)
     self.params=None
+    self.geompyD = mesh.geompyD
     #self.SetPhysicalMesh() - PAL19680
     pass
 
   ## Sets a way to define size of mesh elements to generate.
-  #  @param thePhysicalMesh is: DefaultSize, BLSURF_Custom or SizeMap.
+  #  @param thePhysicalMesh is: DefaultSize, MG_CADSURF_Custom or SizeMap.
   def SetPhysicalMesh(self, thePhysicalMesh=DefaultSize):
     physical_size_mode = thePhysicalMesh
     if self.Parameters().GetGeometricMesh() == DefaultGeom:
       if physical_size_mode == DefaultSize:
-        physical_size_mode = BLSURF_GlobalSize
+        physical_size_mode = MG_CADSURF_GlobalSize
     self.Parameters().SetPhysicalMesh(physical_size_mode)
     pass
 
   ## Sets a way to define maximum angular deflection of mesh from CAD model.
-  #  @param theGeometricMesh is: DefaultGeom (0)) or BLSURF_GlobalSize (1))
+  #  @param theGeometricMesh is: DefaultGeom (0)) or MG_CADSURF_GlobalSize (1))
   def SetGeometricMesh(self, theGeometricMesh=DefaultGeom):
     geometric_size_mode = theGeometricMesh
     if self.Parameters().GetPhysicalMesh() == DefaultSize:
       if geometric_size_mode == DefaultGeom:
-        geometric_size_mode = BLSURF_GlobalSize
+        geometric_size_mode = MG_CADSURF_GlobalSize
     self.Parameters().SetGeometricMesh(geometric_size_mode)
     pass
 
@@ -113,7 +116,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   #  @param isRelative : if True, the value is relative to the length of the diagonal of the bounding box
   def SetPhySize(self, theVal, isRelative = False):
     if self.Parameters().GetPhysicalMesh() == DefaultSize:
-      self.SetPhysicalMesh(BLSURF_GlobalSize)
+      self.SetPhysicalMesh(MG_CADSURF_GlobalSize)
     if isRelative:
       self.Parameters().SetPhySizeRel(theVal)
     else:
@@ -133,7 +136,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   ## Sets upper boundary of mesh element size.
   #  @param theVal : global maximal cell size desired.
   #  @param isRelative : if True, the value is relative to the length of the diagonal of the bounding box
-  def SetMaxSize(self, theVal=-1):
+  def SetMaxSize(self, theVal=-1, isRelative = False):
     if isRelative:
       self.Parameters().SetMaxSizeRel(theVal)
     else:
@@ -144,7 +147,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   #  @param theVal value of angular deflection
   def SetAngleMesh(self, theVal=_geometric_approximation):
     if self.Parameters().GetGeometricMesh() == DefaultGeom:
-      self.SetGeometricMesh(BLSURF_GlobalSize)
+      self.SetGeometricMesh(MG_CADSURF_GlobalSize)
     self.Parameters().SetAngleMesh(theVal)
     pass
 
@@ -161,7 +164,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   # <li>PreProcess or PreProcessPlus - by pre-processing a CAD model (OBSOLETE: FromCAD will be used)</li>
   # <li>PreCAD - by pre-processing with PreCAD a CAD model</li></ul>
   def SetTopology(self, way):
-    if way != PreCAD:
+    if way != PreCAD and way != FromCAD:
       print "Warning: topology mode %d is no longer supported. Mode FromCAD is used."%way
       way = FromCAD
     self.Parameters().SetTopology(way)
@@ -237,6 +240,22 @@ class BLSURF_Algorithm(Mesh_Algorithm):
       self.SetTopology(PreCAD)
     self.Parameters().SetPreCADOptionValue(optionName,optionValue)
     pass
+  
+  ## Adds custom advanced option value.
+  #  @param optionName custom advanced option name
+  #  @param level custom advanced option value
+  def AddOption(self, optionName, level):
+    self.Parameters().AddOption(optionName,level)
+    pass
+
+  ## Adds custom advanced PreCAD option value.
+  #  @param optionName custom name of the option
+  #  @param optionValue value of the option
+  def AddPreCADOption(self, optionName, optionValue):
+    if self.Parameters().GetTopology() != PreCAD:
+      self.SetTopology(PreCAD)
+    self.Parameters().AddPreCADOption(optionName,optionValue)
+    pass
 
   ## Sets GMF file for export at computation
   #  @param fileName GMF file name
@@ -267,6 +286,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   ## To get all the enforced vertices on a face (or group, compound)
   #  @param theFace : GEOM face (or group, compound) on which to define an enforced vertex
   def GetEnforcedVertices(self, theFace):
+    from salome.smesh.smeshBuilder import AssureGeomPublished
     AssureGeomPublished( self.mesh, theFace )
     return self.Parameters().GetEnforcedVertices(theFace)
 
@@ -282,6 +302,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   #  @param vertexName   : name of the enforced vertex
   #  @param groupName    : name of the group
   def SetEnforcedVertex(self, theFace, x, y, z, vertexName = "", groupName = ""):
+    from salome.smesh.smeshBuilder import AssureGeomPublished
     AssureGeomPublished( self.mesh, theFace )
     if vertexName == "":
       if groupName == "":
@@ -302,6 +323,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   #  @param theVertex    : GEOM vertex (or group, compound) to be projected on theFace.
   #  @param groupName    : name of the group
   def SetEnforcedVertexGeom(self, theFace, theVertex, groupName = ""):
+    from salome.smesh.smeshBuilder import AssureGeomPublished
     AssureGeomPublished( self.mesh, theFace )
     AssureGeomPublished( self.mesh, theVertex )
     if groupName == "":
@@ -316,6 +338,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   #  @param y            : y coordinate
   #  @param z            : z coordinate
   def UnsetEnforcedVertex(self, theFace, x, y, z):
+    from salome.smesh.smeshBuilder import AssureGeomPublished
     AssureGeomPublished( self.mesh, theFace )
     return self.Parameters().UnsetEnforcedVertex(theFace, x, y, z)
 
@@ -323,6 +346,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   #  @param theFace      : GEOM face (or group, compound) on which to remove the enforced vertex
   #  @param theVertex    : GEOM vertex (or group, compound) to remove.
   def UnsetEnforcedVertexGeom(self, theFace, theVertex):
+    from salome.smesh.smeshBuilder import AssureGeomPublished
     AssureGeomPublished( self.mesh, theFace )
     AssureGeomPublished( self.mesh, theVertex )
     return self.Parameters().UnsetEnforcedVertexGeom(theFace, theVertex)
@@ -330,6 +354,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   ## To remove all enforced vertices on a given face.
   #  @param theFace      : face (or group/compound of faces) on which to remove all enforced vertices
   def UnsetEnforcedVertices(self, theFace):
+    from salome.smesh.smeshBuilder import AssureGeomPublished
     AssureGeomPublished( self.mesh, theFace )
     return self.Parameters().UnsetEnforcedVertices(theFace)
 
@@ -363,6 +388,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   #  @param theInfluenceDistance : influence of the attractor ( the size grow slower on theFace if it's high)
   #  @param theConstantSizeDistance : distance until which the mesh size will be kept constant on theFace
   def SetAttractorGeom(self, theFace, theAttractor, theStartSize, theEndSize, theInfluenceDistance, theConstantSizeDistance):
+    from salome.smesh.smeshBuilder import AssureGeomPublished
     AssureGeomPublished( self.mesh, theFace )
     AssureGeomPublished( self.mesh, theAttractor )
     self.Parameters().SetAttractorGeom(theFace, theAttractor, theStartSize, theEndSize, theInfluenceDistance, theConstantSizeDistance)
@@ -371,6 +397,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   ## Unsets an attractor on the chosen face.
   #  @param theFace      : face on which the attractor has to be removed
   def UnsetAttractorGeom(self, theFace):
+    from salome.smesh.smeshBuilder import AssureGeomPublished
     AssureGeomPublished( self.mesh, theFace )
     self.Parameters().SetAttractorGeom(theFace)
     pass
@@ -386,6 +413,7 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   #  @param theObject   : GEOM face, edge or vertex (or group, compound) on which to define a size map
   #  @param theSizeMap  : Size map defined as a string
   def SetSizeMap(self, theObject, theSizeMap):
+    from salome.smesh.smeshBuilder import AssureGeomPublished
     AssureGeomPublished( self.mesh, theObject )
     self.Parameters().SetSizeMap(theObject, theSizeMap)
     pass
@@ -394,12 +422,14 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   #  @param theObject   : GEOM face, edge or vertex (or group, compound) on which to define a size map
   #  @param theSizeMap  : Size map defined as a double
   def SetConstantSizeMap(self, theObject, theSizeMap):
+    from salome.smesh.smeshBuilder import AssureGeomPublished
     AssureGeomPublished( self.mesh, theObject )
     self.Parameters().SetConstantSizeMap(theObject, theSizeMap)
 
   ## To remove a size map defined on a face, edge or vertex (or group, compound)
   #  @param theObject   : GEOM face, edge or vertex (or group, compound) on which to define a size map
   def UnsetSizeMap(self, theObject):
+    from salome.smesh.smeshBuilder import AssureGeomPublished
     AssureGeomPublished( self.mesh, theObject )
     self.Parameters().UnsetSizeMap(theObject)
     pass
@@ -419,11 +449,170 @@ class BLSURF_Algorithm(Mesh_Algorithm):
   #  @return hypothesis object
   def Parameters(self):
     if not self.params:
-      self.params = self.Hypothesis("BLSURF_Parameters", [],
-                                    "libBLSURFEngine.so", UseExisting=0)
+      self.params = self.Hypothesis("MG-CADSurf Parameters", [],
+                                    LIBRARY, UseExisting=0)
       pass
     return self.params
 
+  #-----------------------------------------
+  # Periodicity (BLSURF with PreCAD)
+  #-----------------------------------------
+  
+  ## Defines periodicity between two groups of faces, using PreCAD
+  #  @param theFace1 : GEOM face (or group, compound) to associate with theFace2
+  #  @param theFace2 : GEOM face (or group, compound) associated with theFace1
+  #  @param theSourceVertices (optionnal): list of GEOM vertices on theFace1 defining the transformation from theFace1 to theFace2.
+  #    If None, PreCAD tries to find a simple translation. Else, need at least 3 not aligned vertices.
+  #  @param theTargetVertices (optionnal): list of GEOM vertices on theFace2 defining the transformation from theFace1 to theFace2.
+  #    If None, PreCAD tries to find a simple translation. Else, need at least 3 not aligned vertices.
+  def AddPreCadFacesPeriodicity(self, theFace1, theFace2, theSourceVertices=[], theTargetVertices=[]):
+    """calls preCad function:
+    status_t cad_add_face_multiple_periodicity_with_transformation_function(cad t *cad,
+          integer *fid1, integer size1, integer *fid2, integer size2,
+          periodicity_transformation_t transf, void *user data);
+    """
+    if theSourceVertices and theTargetVertices:
+      self.Parameters().AddPreCadFacesPeriodicityWithVertices(theFace1, theFace2, theSourceVertices, theTargetVertices)
+    else:
+      self.Parameters().AddPreCadFacesPeriodicity(theFace1, theFace2)
+    pass
+
+  ## Defines periodicity between two groups of edges, using PreCAD
+  #  @param theEdge1 : GEOM edge (or group, compound) to associate with theEdge2
+  #  @param theEdge2 : GEOM edge (or group, compound) associated with theEdge1
+  #  @param theSourceVertices (optionnal): list of GEOM vertices on theEdge1 defining the transformation from theEdge1 to theEdge2.
+  #    If None, PreCAD tries to find a simple translation. Else, need at least 3 not aligned vertices.
+  #  @param  theTargetVertices (optionnal): list of GEOM vertices on theEdge2 defining the transformation from theEdge1 to theEdge2.
+  #    If None, PreCAD tries to find a simple translation. Else, need at least 3 not aligned vertices.
+  def AddPreCadEdgesPeriodicity(self, theEdge1, theEdge2, theSourceVertices=[], theTargetVertices=[]):
+    """calls preCad function:
+    status_t cad_add_edge_multiple_periodicity_with_transformation_function(cad t *cad,
+          integer *eid1, integer size1, integer *eid2, integer size2,
+          periodicity_transformation_t transf, void *user data);
+    """
+    if theSourceVertices and theTargetVertices:
+        self.Parameters().AddPreCadEdgesPeriodicityWithVertices(theEdge1, theEdge2, theSourceVertices, theTargetVertices)
+    else:
+        self.Parameters().AddPreCadEdgesPeriodicity(theEdge1, theEdge2)
+    pass
+
+
+  #-----------------------------------------
+  # Periodicity (BLSURF without PreCAD)
+  #-----------------------------------------
+
+
+  ## Defines periodicity between two faces, without using PreCAD.
+  #  User has to call AddEdgePeriodicity with the edges of the face,
+  #  and AddVertexPeriodicity with the vertices of each edge.
+  #  @param theFace1 : GEOM face to associate with theFace2
+  #  @param theFace2 : GEOM face associated with theFace1
+  def AddFacePeriodicity(self, theFace1, theFace2):
+    self.Parameters().AddFacePeriodicity(theFace1, theFace2)
+    pass
+      
+  ## Defines periodicity between two edges belonging to two periodic faces, without using PreCAD.
+  #  To be used with AddFacePeriodicity.
+  #  User has to call AddVertexPeriodicity with the vertices of each edge
+  #  @param theFace1 : GEOM face to associate with theFace2
+  #  @param theEdge1 : GEOM edge to associate with theEdge2
+  #  @param theFace2 : GEOM face associated with theFace1
+  #  @param theEdge2 : GEOM edge associated with theEdge1
+  #  @param theEdgeOrientation : -1 (reversed), 0 (unknown) or 1 (forward)
+  def AddEdgePeriodicity(self, theFace1, theEdge1, theFace2, theEdge2, theEdgeOrientation=0):
+    self.Parameters().AddEdgePeriodicity(theFace1, theEdge1, theFace2, theEdge2, theEdgeOrientation)
+    pass
+
+  ## Defines periodicity between two edges without face periodicity, without using PreCAD.
+  #  User has to call AddVertexPeriodicity with the vertices of each edge.
+  #  @param theEdge1 : GEOM edge to associate with theEdge2
+  #  @param theEdge2 : GEOM edge associated with theEdge1
+  #  @param theEdgeOrientation : -1 (reversed), 0 (unknown) or 1 (forward)
+  def AddEdgePeriodicityWithoutFaces(self, theEdge1, theEdge2, theEdgeOrientation=0):
+    self.Parameters().AddEdgePeriodicityWithoutFaces(theEdge1, theEdge2, theEdgeOrientation)
+    pass
+      
+  ## Defines periodicity between two vertices.
+  #  To be used with AddFacePeriodicity and AddEdgePeriodicity.
+  #  @param theEdge1 : GEOM edge to associate with theEdge2
+  #  @param theVertex1 : GEOM face to associate with theVertex2
+  #  @param theEdge2 : GEOM edge associated with theEdge1
+  #  @param theVertex2 : GEOM face associated with theVertex1
+  def AddVertexPeriodicity(self, theEdge1, theVertex1, theEdge2, theVertex2):
+    self.Parameters().AddVertexPeriodicity(theEdge1, theVertex1, theEdge2, theVertex2)
+    pass
+
+  ## Define periodicity between two groups of faces, given a transformation function.
+  #  This uses the basic BLSURF API for each face, each edge, and each vertex.
+  #  @param theFace1 : GEOM face (or group, compound) to associate with theFace2
+  #  @param theFace2 : GEOM face (or group, compound) associated with theFace1
+  #  @param f_transf : python function defining the transformation between an object of theFace1
+  # into an object of theFace2
+  def AddAdvancedFacesPeriodicity(self, theFace1, theFace2, f_transf):
+    source_faces = self.geompyD.SubShapeAll(theFace1, self.geompyD.ShapeType["FACE"])
+    i = 0
+    j = 0
+    k = 0
+    for source_face in source_faces:
+      self.geompyD.addToStudyInFather(theFace1, source_face, "source_face_%i"%i)
+      p_source = self.geompyD.MakeVertexInsideFace(source_face)
+      p_target = f_transf(p_source)
+      target_face = self.geompyD.GetFaceNearPoint(theFace2, p_target)
+      self.geompyD.addToStudyInFather(theFace2, target_face, "target_face_%i"%i)
+      self.AddFacePeriodicity(source_face, target_face)
+      i += 1
+      
+      source_edges = self.geompyD.SubShapeAll(source_face, self.geompyD.ShapeType["EDGE"])
+      for source_edge in source_edges:
+        self.geompyD.addToStudyInFather(theFace1, source_edge, "source_edge_%i"%(j))
+        p_source = self.geompyD.MakeVertexOnCurve(source_edge, 0.5)
+        p_target = f_transf(p_source)
+        target_edge = self.geompyD.GetEdgeNearPoint(theFace2, p_target)
+        self.geompyD.addToStudyInFather(theFace2, target_edge, "target_edge_%i"%(j))
+        self.AddEdgePeriodicity(source_face, source_edge, target_face, target_edge, 1)
+        j += 1
+        
+        source_vertices = self.geompyD.SubShapeAll(source_edge, self.geompyD.ShapeType["VERTEX"])
+        for source_vertex in source_vertices:
+          self.geompyD.addToStudyInFather(theFace1, source_vertex, "source_vertex_%i"%(k))
+          target_vertex_tmp = f_transf(source_vertex)
+          target_vertex = self.geompyD.GetSame(theFace2, target_vertex_tmp)
+          self.geompyD.addToStudyInFather(theFace2, target_vertex, "target_vertex_%i"%(k))
+          self.AddVertexPeriodicity(source_edge, source_vertex, target_edge, target_vertex)
+          k += 1
+      pass
+
+  ## Define periodicity between two groups of edges, without faces, given a transformation function.
+  #  This uses the basic BLSURF API for each edge and each vertex.
+  #  @param theEdge1 : GEOM edge (or group, compound) to associate with theEdge2
+  #  @param theEdge2 : GEOM edge (or group, compound) associated with theEdge1
+  #  @param f_transf : python function defining the transformation between an object of theEdge1
+  # into an object of theFace2
+  def AddAdvancedEdgesPeriodicity(self, theEdge1, theEdge2, f_transf):
+    source_edges = self.geompyD.SubShapeAll(theEdge1, self.geompyD.ShapeType["EDGE"])
+    j = 0
+    k = 0
+    for source_edge in source_edges:
+      self.geompyD.addToStudyInFather(theEdge1, source_edge, "source_edge_%i"%j)
+      p_source = self.geompyD.MakeVertexOnCurve(source_edge, 0.5)
+      p_target = f_transf(p_source)
+      target_edge = self.geompyD.GetEdgeNearPoint(theEdge2, p_target)
+      self.geompyD.addToStudyInFather(theEdge2, target_edge, "target_edge_%i"%j)
+      self.AddEdgePeriodicityWithoutFaces(source_edge, target_edge)
+      
+      j += 1
+      
+      source_vertices = self.geompyD.SubShapeAll(source_edge, self.geompyD.ShapeType["VERTEX"])
+      for source_vertex in source_vertices:
+        self.geompyD.addToStudyInFather(theEdge1, source_vertex, "source_vertex_%i"%k)
+        target_vertex_tmp = f_transf(source_vertex)
+        target_vertex = self.geompyD.GetSame(theEdge2, target_vertex_tmp)
+        self.geompyD.addToStudyInFather(theEdge2, target_vertex, "target_vertex_%i"%k)
+        self.AddVertexPeriodicity(source_edge, source_vertex, target_edge, target_vertex)
+        
+        k += 1
+    pass
+
   #=====================
   # Obsolete methods
   #=====================