Salome HOME
use hydroGeoMeshUtils in scripts: lot1, h009, h010, h011, h015, h020, h021
[modules/hydro.git] / src / HYDROTools / hydroGeoMeshUtils.py
1 import sys
2 import os
3 import salome
4
5 salome.salome_init()
6
7 from HYDROPy import *
8 from PyQt5.QtCore import *
9 from PyQt5.QtGui import *
10
11 from random import randint
12
13 def getChildrenInStudy(obj):
14     """
15     Given an object published in SALOME study (for instance a GEOM object), retreive its children.
16     return a dictionary [name] --> object
17     """
18     SO = salome.myStudy.FindObjectIOR(salome.myStudy.ConvertObjectToIOR(obj))
19     childIterator = salome.myStudy.NewChildIterator(SO)
20     children = {}
21     while childIterator.More():
22         childItem = childIterator.Value()
23         print("item", childItem)
24         itemName = childItem.GetName()
25         itemID = childItem.GetID()
26         itemObj = salome.IDToObject(str(itemID))
27         children[itemName] = itemObj
28         childIterator.Next()
29     return children
30
31 def loadImage(document, imageFile, imageName, displayLevel):
32     """
33     """
34     image = document.CreateObject(KIND_IMAGE)
35     image.SetName(imageName)
36     image.SetZLevel(displayLevel)
37     if not(image.LoadImage(imageFile)):
38         raise ValueError('problem while loading image %s'%imageFile)
39     image.Update()
40     return image
41     
42 def GeolocaliseImageCoords(image, a, b, c ,d):
43     """
44     """
45     image.SetLocalPoints(a, b)
46     image.SetGlobalPoints(1, c, d)
47     image.Update()
48
49 def GeolocaliseImageReference(image, imageRef, a, b, c ,d):
50     """
51     """
52     image.SetLocalPoints(a, b)
53     image.SetGlobalPoints(3, c, d)
54     image.SetTrsfReferenceImage(imageRef)
55     image.Update()
56     
57 def importPolylines(document, shapeFile, shapeName, iSpline, displayLevel):
58     """
59     """
60     HYDROData_PolylineXY.ImportShapesFromFile(shapeFile)
61     shapeType = 0 # polyline
62     if iSpline:
63         shapeType = 1 # polyline
64     shapes = []
65     isShapeFound = True
66     shape = document.FindObjectByName(shapeName) # single shape
67     if shape is not None:
68         for i in range(shape.NbSections()):
69             shape.SetSectionType(i, shapeType)
70             shape.Update()
71             shape.SetZLevel( displayLevel )
72         shapes.append(shape)    
73     index = 0  # for multiple shapes
74     while isShapeFound:
75         shapeNameIndex = shapeName + "_%d" % index
76         index = index + 1
77         print(shapeNameIndex)
78         shape = document.FindObjectByName(shapeNameIndex)
79         print(shape)
80         if shape is None:
81             isShapeFound = False
82         else:
83             for i in range(shape.NbSections()):
84                 shape.SetSectionType(i, shapeType)
85                 shape.Update()
86                 shape.SetZLevel( displayLevel )
87             shapes.append(shape)    
88     return shapes
89
90 def importBathymetry(document, bathyName, bathyPath):
91     """
92     """
93     bathy = document.CreateObject(KIND_BATHYMETRY)
94     bathy.SetName(bathyName)
95     bathy.SetAltitudesInverted(0)
96     if not(bathy.ImportFromFile( os.path.join(bathyPath, bathyName + '.xyz' ))):
97         raise ValueError('problem while loading bathymetry')
98     bathy.Update()
99     return bathy
100
101 def createImmersibleZone(document, imzName, polyLine, bathy, isImmersible, displayLevel):
102     """
103     """
104     imz = document.CreateObject( KIND_IMMERSIBLE_ZONE )
105     imz.SetName( imzName )
106     imz.SetZLevel( displayLevel )
107     imz.SetAltitudeObject( bathy )
108     imz.SetPolyline( polyLine )
109     imz.SetIsSubmersible(isImmersible)
110     imz.SetFillingColor(QColor(randint(0,255), randint(0,255), randint(0,255), 255))
111     imz.Update()
112     return imz
113
114 def mergePolylines(document, polyName, polyLines, isConnectedBySegment = False, tolerance = 1.E-3):
115     """
116     """
117     op=HYDROData_PolylineOperator()
118     op.Merge(document, polyName, polyLines, isConnectedBySegment, tolerance)
119     shape = document.FindObjectByName(polyName)
120     return shape