#!/usr/bin/env python


import sys
import salome

salome.salome_init()

###
### SHAPER component
###

from salome.shaper import model

model.begin()
partSet = model.moduleDocument()

### Create Part
Part_1 = model.addPart(partSet)
Part_1_doc = Part_1.document()

### Create Sketch
Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))

### Create SketchLine
SketchLine_1 = Sketch_1.addLine(0, 0, 54.55178029722894, 0)

### Create SketchProjection
SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
SketchPoint_1 = SketchProjection_1.createdFeature()
Sketch_1.setCoincident(SketchLine_1.startPoint(), SketchPoint_1.result(), True)

### Create SketchProjection
SketchProjection_2 = Sketch_1.addProjection(model.selection("EDGE", "PartSet/OX"), False)
SketchLine_2 = SketchProjection_2.createdFeature()
Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.result(), True)

### Create SketchLine
SketchLine_3 = Sketch_1.addLine(54.55178029722894, 0, 54.55178029722894, 43.0892781143164)
Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_3.startPoint(), True)
Sketch_1.setVertical(SketchLine_3.result(), True)

### Create SketchArc
SketchArc_1 = Sketch_1.addArc(0.6192276636187076, 55.2924403426879, 0, 0, 54.55178029722894, 43.0892781143164, False)
Sketch_1.setCoincident(SketchLine_1.startPoint(), SketchArc_1.startPoint(), True)
Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchArc_1.endPoint(), True)
model.do()

### Create Edge
Edge_1 = model.addEdge(Part_1_doc, [model.selection("EDGE", "Sketch_1/SketchArc_1_2")], False)

### Create Edge
Edge_2 = model.addEdge(Part_1_doc, [model.selection("EDGE", "Sketch_1/SketchLine_1")], False)

### Create Edge
Edge_3 = model.addEdge(Part_1_doc, [model.selection("EDGE", "Sketch_1/SketchLine_3")], False)

### Create Wire
Wire_1_objects = [model.selection("EDGE", "Edge_1_1"),
                  model.selection("EDGE", "Edge_2_1"),
                  model.selection("EDGE", "Edge_3_1")]
Wire_1 = model.addWire(Part_1_doc, Wire_1_objects, False)

### Create Face
Face_1 = model.addFace(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchLine_1r-SketchLine_3f-SketchArc_1_2r")])

### Create Point
Point_2 = model.addPoint(Part_1_doc, model.selection("EDGE", "Sketch_1/SketchArc_1_2"), 0.5, True, False)

### Create Vertex
Vertex_1 = model.addVertex(Part_1_doc, [model.selection("VERTEX", "Point_1")], False)

### Create Split
Split_1 = model.addSplit(Part_1_doc, [model.selection("FACE", "Face_1_1")], [model.selection("VERTEX", "Vertex_1_1")], keepSubResults = True)

model.end()

###
### SHAPERSTUDY component
###

model.publishToShaperStudy()
import SHAPERSTUDY
Wire_1_1, = SHAPERSTUDY.shape(model.featureStringId(Wire_1))
# This shape does not exist among the SHAPER results; if it is referenced by SMESH, this may cause an error
# Face_1_1, = SHAPERSTUDY.shape("dead02_8:24")
Split_1_1, = SHAPERSTUDY.shape(model.featureStringId(Split_1))
###
### SMESH component
###

import  SMESH, SALOMEDS
from salome.smesh import smeshBuilder

smesh = smeshBuilder.New()
#smesh.SetEnablePublish( False ) # Set to False to avoid publish in study if not needed or in some particular situations:
                                 # multiples meshes built in parallel, complex and numerous mesh edition (performance)

Number_of_Segments_1 = smesh.CreateHypothesis('NumberOfSegments')
Number_of_Segments_1.SetNumberOfSegments( 5 )
Regular_1D = smesh.CreateHypothesis('Regular_1D')
Quadrangle_2D = smesh.CreateHypothesis('Quadrangle_2D')
Mesh_1 = smesh.Mesh(Split_1_1,'Mesh_1')
status = Mesh_1.AddHypothesis(Number_of_Segments_1)
status = Mesh_1.AddHypothesis(Regular_1D)
status = Mesh_1.AddHypothesis(Quadrangle_2D)
isDone = Mesh_1.Compute()
Mesh_1.CheckCompute()

# create a butterfly cell by movine a node

n = Mesh_1.FindNodeClosestTo(8.48, 0.55, 0)
Mesh_1.MoveNode(n, 8.48, -0.55, 0)

# export to medcoupling in memory
umesh_2d = Mesh_1.ExportMEDCoupling()[0]
faces_pb_ids = umesh_2d.checkButterflyCells()

# convert medcoupling cells numerotation to SMESH cells numerotation
# - SMESH starts at 1 and aggregate all cells dimension ids
# - MEDCoupling starts at 0 for each cell dimension
faces_pb_ids += 1+Mesh_1.NbEdges()

if faces_pb_ids:
  print(f"WARNING: {faces_pb_ids.getNbOfElems()} buttefly cells. Display the group butterfly_cells to see them.")
  gr_butterfly_cells = Mesh_1.MakeGroupByIds("butterfly_cells", SMESH.FACE, faces_pb_ids.getValues())

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