Salome HOME
Fix memory leaks
[modules/geom.git] / src / GEOM_SWIG / GEOM_Spanner.py
index cc336a7b441d64181c71b84f83641700a1934530..8d7a0189d540e93ab64e09af1f8d19df5382bf46 100644 (file)
@@ -1,42 +1,41 @@
-#  GEOM GEOM_SWIG : binding of C++ implementaion with Python
+#  -*- coding: iso-8859-1 -*-
+# Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
 #
-#  Copyright (C) 2003  CEA
+# Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 #
-#  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.
+# 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, 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
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-#  Lesser General Public License for more details.
-#
-#  You should have received a copy of the GNU Lesser General Public
-#  License along with this library; if not, write to the Free Software
-#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
-#
-#  See http://www.salome-platform.org or email : webmaster.salome@opencascade.org
+# This library 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
+# Lesser General Public License for more details.
 #
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 #
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
 #  File   : GEOM_Spanner.py
 #  Author : Julia DOROVSKIKH
 #  Module : GEOM
-#  $Header$
-
+#
+# ! Please, if you edit this example file, update also
+# ! GEOM_SRC/doc/salome/gui/GEOM/input/tui_test_spanner.doc
+# ! as some sequences of symbols from this example are used during
+# ! documentation generation to identify certain places of this file
+#
 ############# MakeSpanner #############
-
-def MakeSpanner (salome, geompy, math, isBlocksTest = 0, isMeshTest = 0, smesh = None, hasGUI = 0):
+def MakeSpanner (geompy, math, isBlocksTest = 0, isMeshTest = 0, smesh = None):
 
   ### Variables ###
 
   th = 4.0
 
-  ### BlocksOp ###
-
-  BlocksOp = geompy.BlocksOp
-
   ### Basic points and directions ###
 
   p0  = geompy.MakeVertex(0., 0., 0.)
@@ -100,7 +99,7 @@ def MakeSpanner (salome, geompy, math, isBlocksTest = 0, isMeshTest = 0, smesh =
   else:
     print "Prism 1 is not a hexahedral solid"
 
-  Prism1_faces = geompy.SubShapeAllSorted(Prism1, geompy.ShapeType["FACE"])
+  Prism1_faces = geompy.SubShapeAllSortedCentres(Prism1, geompy.ShapeType["FACE"])
   ii = 1
   for aFace in  Prism1_faces:
     name = geompy.SubShapeName(aFace, Prism1)
@@ -276,17 +275,21 @@ def MakeSpanner (salome, geompy, math, isBlocksTest = 0, isMeshTest = 0, smesh =
   Face22h = geompy.GetOppositeFace(Block2h, Face21h)
   id_face22h = geompy.addToStudyInFather(Block2h, Face22h, "Face 2")
 
-  Block3h = BlocksOp.GetBlockByParts(Handle, [Face11h, Face21h])
-  if BlocksOp.IsDone() == 0:
-    Block3h = BlocksOp.GetBlockByParts(Handle, [Face11h, Face22h])
-    if BlocksOp.IsDone() == 0:
-        Block3h = BlocksOp.GetBlockByParts(Handle, [Face12h, Face21h])
-        if BlocksOp.IsDone() == 0:
-            Block3h = BlocksOp.GetBlockByParts(Handle, [Face12h, Face22h])
-  if BlocksOp.IsDone() == 0:
-    print "ERROR: BlocksOp.GetBlockByParts() failed : ", BlocksOp.GetErrorCode()
-  else:
-    id_block3h = geompy.addToStudyInFather(Handle, Block3h, "Block 3 of Handle")
+  try:
+    Block3h = geompy.GetBlockByParts(Handle, [Face11h, Face21h])
+  except RuntimeError:
+    try:
+      Block3h = geompy.GetBlockByParts(Handle, [Face11h, Face22h])
+    except RuntimeError:
+      try:
+        Block3h = geompy.GetBlockByParts(Handle, [Face12h, Face21h])
+      except RuntimeError:
+        try:
+          Block3h = geompy.GetBlockByParts(Handle, [Face12h, Face22h])
+        except RuntimeError:
+          print "ERROR: BlocksOp.GetBlockByParts() failed : ", geompy.BlocksOp.GetErrorCode()
+        else:
+          id_block3h = geompy.addToStudyInFather(Handle, Block3h, "Block 3 of Handle")
 
   ### The whole shape ###
 
@@ -296,9 +299,14 @@ def MakeSpanner (salome, geompy, math, isBlocksTest = 0, isMeshTest = 0, smesh =
 
   ### Check the Spanner ###
 
-  isCompOfBlocks6 = BlocksOp.CheckCompoundOfBlocks(Spanner)
+  isCompOfBlocks6 = geompy.CheckCompoundOfBlocks(Spanner)
   if isCompOfBlocks6 == 0:
     print "Spanner is not a compound of hexahedral solids"
+    (NonBlocks, NonQuads) = geompy.GetNonBlocks(Spanner)
+    if NonBlocks is not None:
+      geompy.addToStudyInFather(Spanner, NonBlocks, "Group of non-hexahedral solids")
+    if NonQuads is not None:
+      geompy.addToStudyInFather(Spanner, NonQuads, "Group of non-quadrangular faces")
   else:
     print "Spanner is a compound of hexahedral solids"
 
@@ -324,8 +332,13 @@ def MakeSpanner (salome, geompy, math, isBlocksTest = 0, isMeshTest = 0, smesh =
       Face_5_horiz = geompy.GetFaceByNormale(aBlock, vz)
       f_5_horiz_ind = geompy.LocalOp.GetSubShapeIndex(aBlock, Face_5_horiz)
 
-      MRot = geompy.MakeMultiTransformation2D(aBlock, f42_sp_ind, 0, 3, f_5_horiz_ind, 0, 5)
-      if MRot is not None:
+      MRot = None
+      is2Dok = True
+      try:
+        MRot = geompy.MakeMultiTransformation2D(aBlock, f42_sp_ind, 0, 3, f_5_horiz_ind, 0, 5)
+      except:
+        is2Dok = False
+      if is2Dok and MRot is not None:
         isMRot2D = 1
         p_z100 = geompy.MakeVertex(0., 0., 100.)
         MRot_tr = geompy.MakeTranslationTwoPoints(MRot, p0, p_z100)
@@ -355,11 +368,10 @@ def MakeSpanner (salome, geompy, math, isBlocksTest = 0, isMeshTest = 0, smesh =
     Block_new = geompy.MakeHexa2Faces(Face_g_1, Face_g_1_tr)
     id_block_new = geompy.addToStudy(Block_new, "Block New")
 
-  if isMeshTest == 1:
+  if isMeshTest == 1 and smesh is not None:
 
     print "##################### Build Mesh #####################"
 
-    import StdMeshers
 
     # ---- add a middle block of spanner handle in study
 
@@ -374,123 +386,74 @@ def MakeSpanner (salome, geompy, math, isBlocksTest = 0, isMeshTest = 0, smesh =
 
     # ---- add long edges of the top face in study
 
-    FaceTop_edges = geompy.SubShapeAllSorted(FaceTop, geompy.ShapeType["EDGE"])
+    FaceTop_edges = geompy.SubShapeAllSortedCentres(FaceTop, geompy.ShapeType["EDGE"])
     Edge1 = FaceTop_edges[0]
     Edge2 = FaceTop_edges[3]
     Id_Edge1 = geompy.addToStudyInFather(FaceTop, Edge1, "Edge 1")
     Id_Edge2 = geompy.addToStudyInFather(FaceTop, Edge2, "Edge 2")
 
-    # ---- launch SMESH
-
-    smeshgui = None
-    if hasGUI == 1:
-      smeshgui = salome.ImportComponentGUI("SMESH")
-      smeshgui.Init(salome.myStudyId)
-
-    print "-------------------------- create Hypothesis"
-
-    print "-------------------------- NumberOfSegments"
-
-    hypNbSeg3 = smesh.CreateHypothesis("NumberOfSegments", "libStdMeshersEngine.so")
-    hypNbSeg3.SetNumberOfSegments(3)
-    print hypNbSeg3.GetName()
-    print hypNbSeg3.GetId()
-    print hypNbSeg3.GetNumberOfSegments()
-
-    if hasGUI == 1:
-      idseg = salome.ObjectToID(hypNbSeg3)
-      smeshgui.SetName(idseg, "NumberOfSegments_3");
-
-    print "-------------------------- LocalLength"
-
-    hypLen1 = smesh.CreateHypothesis("LocalLength", "libStdMeshersEngine.so")
-    hypLen1.SetLength(10)
-    print hypLen1.GetName()
-    print hypLen1.GetId()
-    print hypLen1.GetLength()
-
-    if hasGUI == 1:
-      idlength = salome.ObjectToID(hypLen1)
-      smeshgui.SetName(idlength, "Local_Length_10");
-
-    print "-------------------------- Propagation"
+    print "-------------------------- Algorithm and Hypothesis"
 
-    hypProp = smesh.CreateHypothesis("Propagation", "libStdMeshersEngine.so")
-    print hypProp.GetName()
-    print hypProp.GetId()
-
-    if hasGUI == 1:
-      idprop = salome.ObjectToID(hypProp)
-      smeshgui.SetName(idprop, "Propagation hypothesis");
+    print "---- Init a Mesh with the Spanner"
 
-    #print "-------------------------- NumberOfSegments"
-    #
-    #hypNbSeg15 = smesh.CreateHypothesis("NumberOfSegments", "libStdMeshersEngine.so")
-    #hypNbSeg15.SetNumberOfSegments(15)
-    #print hypNbSeg15.GetName()
-    #print hypNbSeg15.GetId()
-    #print hypNbSeg15.GetNumberOfSegments()
-    #
-    #if hasGUI == 1:
-    #  idseg15 = salome.ObjectToID(hypNbSeg15)
-    #  smeshgui.SetName(idseg15, "NumberOfSegments_15");
+    mesh = smesh.Mesh(Spanner, "Meshed Spanner")
 
-    print "-------------------------- Regular_1D"
+    print "-------------------------- add hypothesis to Spanner"
 
-    algoReg = smesh.CreateHypothesis("Regular_1D", "libStdMeshersEngine.so")
+    print "-------------------------- NumberOfSegments"
+    algoReg = mesh.Segment()
     listHyp = algoReg.GetCompatibleHypothesis()
     for hyp in listHyp:
-        print hyp
+      print hyp
     print algoReg.GetName()
     print algoReg.GetId()
+    algoReg.SetName("Regular_1D")
+
 
-    if hasGUI == 1:
-      idreg = salome.ObjectToID(algoReg)
-      smeshgui.SetName(idreg, "Regular_1D");
+    hypNbSeg3 = algoReg.NumberOfSegments(3)
+    print hypNbSeg3.GetName()
+    print hypNbSeg3.GetId()
+    print hypNbSeg3.GetNumberOfSegments()
+    smesh.SetName(hypNbSeg3, "NumberOfSegments_3")
 
     print "-------------------------- Quadrangle_2D"
 
-    algoQuad = smesh.CreateHypothesis("Quadrangle_2D", "libStdMeshersEngine.so")
+    algoQuad = mesh.Quadrangle()
     listHyp = algoQuad.GetCompatibleHypothesis()
     for hyp in listHyp:
         print hyp
     print algoQuad.GetName()
     print algoQuad.GetId()
-
-    if hasGUI == 1:
-      idquad = salome.ObjectToID(algoQuad)
-      smeshgui.SetName(idquad, "Quadrangle_2D");
-
-    print "---- Init a Mesh with the Spanner"
-
-    mesh = smesh.CreateMesh(Spanner)
-    if hasGUI == 1:
-      idmesh = salome.ObjectToID(mesh)
-      smeshgui.SetName(idmesh, "Meshed Spanner");
-
-    print "-------------------------- add hypothesis to Spanner"
-
-    mesh.AddHypothesis(Spanner, algoReg)
-    mesh.AddHypothesis(Spanner, hypNbSeg3)
-    mesh.AddHypothesis(Spanner, algoQuad)
+    algoQuad.SetName("Quadrangle_2D")
 
     print "-------------------------- add hypothesis to the Middle Block"
 
-    submesh_bl = mesh.GetSubMesh(BlockMh, "SubMesh Middle Block")
-    mesh.AddHypothesis(BlockMh, hypLen1)
+    print "-------------------------- LocalLength"
+    algoRegMb = mesh.Segment(BlockMh)
+    hypLen1 = algoRegMb.LocalLength(10)
+    print hypLen1.GetName()
+    print hypLen1.GetId()
+    print hypLen1.GetLength()
+    smesh.SetName(hypLen1, "Local_Length_10")
 
     print "-------------------------- add hypothesis to the long edges of the Top Face of the Middle Block"
 
-    submesh_e1 = mesh.GetSubMesh(Edge1, "SubMesh Edge 1 of Top Face")
-    mesh.AddHypothesis(Edge1, hypProp)
-    #mesh.AddHypothesis(Edge1, hypNbSeg15)
+    algoRegE1 = mesh.Segment(Edge1)
+    hypPropE1 = algoRegE1.Propagation()
+    print hypPropE1.GetName()
+    print hypPropE1.GetId()
+    smesh.SetName(hypPropE1, "Propagation hypothesis")
+    smesh.SetName(algoRegE1.GetSubMesh(), "SubMesh Edge 1 of Top Face")
 
-    submesh_e2 = mesh.GetSubMesh(Edge2, "SubMesh Edge 2 of Top Face")
-    mesh.AddHypothesis(Edge2, hypProp)
-    #mesh.AddHypothesis(Edge2, hypNbSeg15)
+    algoRegE2 = mesh.Segment(Edge2)
+    hypPropE2 = algoRegE2.Propagation()
+    print hypPropE2.GetName()
+    print hypPropE2.GetId()
+    smesh.SetName(hypPropE2, "Propagation hypothesis")
+    smesh.SetName(algoRegE2.GetSubMesh(), "SubMesh Edge 2 of Top Face")
 
     print "-------------------------- compute the mesh"
-    smesh.Compute(mesh, Spanner)
+    mesh.Compute()
 
     print "Information about the Mesh:"
     print "Number of nodes       : ", mesh.NbNodes()