# http://www.salome-platform.org/forum/forum_10/935107576

import sys
import salome

salome.salome_init()
theStudy = salome.myStudy

import salome_notebook

###
### GEOM component
###

import GEOM
import geompy
import math
import SALOMEDS


geompy.init_geom(theStudy)

Cylinder_1 = geompy.MakeCylinderRH(100, 300)
Plane_1 = geompy.MakePlaneLCS(None, 2000, 2)
Plane_2 = geompy.MakePlaneLCS(None, 2000, 3)
Partition_1 = geompy.MakePartition([Cylinder_1], [Plane_1, Plane_2], [], [], geompy.ShapeType["SOLID"], 0, [], 0)
[Solid_1,Solid_2,Solid_3,Solid_4] = geompy.ExtractShapes(Partition_1, geompy.ShapeType["SOLID"], True)
to_be_meshed = geompy.MakePartition([Cylinder_1, Solid_2], [], [], [], geompy.ShapeType["SOLID"], 0, [], 0)
revolution_start_face = geompy.CreateGroup(to_be_meshed, geompy.ShapeType["FACE"])
geompy.UnionIDs(revolution_start_face, [34])
mannually = geompy.CreateGroup(to_be_meshed, geompy.ShapeType["SOLID"])
geompy.UnionIDs(mannually, [36])
with_algorithms = geompy.CreateGroup(to_be_meshed, geompy.ShapeType["SOLID"])
geompy.UnionIDs(with_algorithms, [2])
revolution_start_face.SetColor(SALOMEDS.Color(1,0,0))
mannually.SetColor(SALOMEDS.Color(1,0.666667,0))
with_algorithms.SetColor(SALOMEDS.Color(0,0.666667,1))
geompy.addToStudy( Plane_2, 'Plane_2' )
geompy.addToStudyInFather( Partition_1, Solid_1, 'Solid_1' )
geompy.addToStudyInFather( Partition_1, Solid_2, 'Solid_2' )
geompy.addToStudyInFather( Partition_1, Solid_3, 'Solid_3' )
geompy.addToStudyInFather( Partition_1, Solid_4, 'Solid_4' )
geompy.addToStudy( to_be_meshed, 'to be meshed' )
geompy.addToStudyInFather( to_be_meshed, revolution_start_face, 'revolution start face' )
geompy.addToStudyInFather( to_be_meshed, mannually, 'mannually' )
geompy.addToStudyInFather( to_be_meshed, with_algorithms, 'with algorithms' )

### Store presentation parameters of displayed objects
import iparameters
ipar = iparameters.IParameters(theStudy.GetModuleParameters("Interface Applicative", "GEOM", 1))

###
### SMESH component
###

import smesh, SMESH, SALOMEDS

smesh.SetCurrentStudy(theStudy)
import StdMeshers
Mesh_1 = smesh.Mesh(to_be_meshed)
Regular_1D = Mesh_1.Segment(geom=revolution_start_face)
Nb_Segments_1 = Regular_1D.NumberOfSegments(15,[],[  ])
Nb_Segments_1.SetDistrType( 0 )
Quadrangle_2D = Mesh_1.Quadrangle(algo=smesh.QUADRANGLE,geom=revolution_start_face)
#isDone = Mesh_1.Compute()
revolution_start_face_1 = Regular_1D.GetSubMesh()
# See comments below
#Mesh_1.RotationSweepObject( revolution_start_face_1, SMESH.AxisStruct( 0, 0, 0, 0, 0, 20 ), 0.10472, 15, 1e-05 )

# - Create a mesh group including the resulting 2D elements of this mesh on
#   the rest of this revolution
revolution_end_faces = Mesh_1.MakeGroup( "revolution_end_faces", SMESH.FACE,
                                         SMESH.FT_BelongToGeom,"=",Plane_2, Tolerance=1e-2)

# - Create a submesh with the 2D algorithm Use Existing 2D Elements
#    to 'copy' the manually generated 2D element on the submesh.
revolution_end_face = geompy.GetInPlace( with_algorithms, Plane_2 )
geompy.addToStudyInFather( to_be_meshed, revolution_end_face, "revolution_end_face" )
######## This doesn't work since mesh on egdes of revolution_end_face is cleared
# as "Use Existing 2D Elements" is assigned. This happens because "Use Existing 2D Elements"
# is so called multi-dimensional algorithm, i.e. it can generate both 2D and 1D elements,
# and the existing 1D mesh (segments and their nodes) is cleared to correspond to a
# new algo generating 1D elements.

# useExistingAlgo = Mesh_1.UseExisting2DElements(revolution_end_face)
# useExistingAlgo.SourceFaces( [revolution_end_faces] )

######## A workaroud (among several possible ones) can be as follows: to mesh
# revolution_end_face with same algos and hypos as revolution_start_face
# and merge equal nodes and elements after all.
# To avoid removal of elements created by Rotation at AddHypothesis(),
# perform Rotation after Mesh_1.Compute()
Mesh_1.AddHypothesis( Regular_1D, revolution_end_face)
Mesh_1.AddHypothesis( Nb_Segments_1, revolution_end_face)
Mesh_1.AddHypothesis( Quadrangle_2D, revolution_end_face)

# - mesh the other geometrical volume group with algorithm
Mesh_1.Triangle( algo=smesh.NETGEN_1D2D, geom=with_algorithms )
Mesh_1.Tetrahedron( algo=smesh.NETGEN_3D, geom=with_algorithms )
Mesh_1.Compute()

Mesh_1.RotationSweepObject( revolution_start_face_1, SMESH.AxisStruct( 0, 0, 0, 0, 0, 20 ), 0.10472, 15, 1e-05 )
Mesh_1.MergeNodes( Mesh_1.FindCoincidentNodes( 1e-1 ))
Mesh_1.MergeEqualElements()


## set object names
smesh.SetName(Mesh_1.GetMesh(), 'Mesh_1')
smesh.SetName(Regular_1D.GetAlgorithm(), 'Regular_1D')
smesh.SetName(Nb_Segments_1, 'Nb. Segments_1')
smesh.SetName(revolution_start_face_1, 'revolution start face')
smesh.SetName(Quadrangle_2D.GetAlgorithm(), 'Quadrangle_2D')


if salome.sg.hasDesktop():
  salome.sg.updateObjBrowser(1)
