Salome HOME
debug abort on stream linear interpolation when bathy displayed and modified
[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
121
122 def createAxis3DDEmbankmentAltiProfile(document, axis3DName, axisXY, altiPts):
123     """
124     document: current HYDROData_Document
125     axis3DName: name to give to the axis3D created
126     axisXY: imported polyline, Embankment line
127     altiPts: list of tuples(x, h) : parametric coordinate, altitude, along the axis
128     return altiProfile, axis3D
129     """
130     altiProfile = document.CreateObject(KIND_PROFILE)
131     altiProfile.SetName("%s_altiProfile"%axis3DName)
132     altiProfilePoints = []
133     for p in altiPts:
134         altiProfilePoints.append(gp_XY(p[0], p[1]))
135     altiProfile.SetParametricPoints(altiProfilePoints)
136     altiProfile.Update()
137     axis3D = document.CreateObject(KIND_POLYLINE)
138     axis3D.SetName(axis3DName)
139     axis3D.SetPolylineXY(axisXY)
140     axis3D.SetProfileUZ(altiProfile.GetProfileUZ())
141     axis3D.Update()
142     return (altiProfile, axis3D)
143     
144 def createAxis3DDEmbankmentBathy(document, axis3DName, axisXY, aCloud):
145     """
146     document: current HYDROData_Document
147     axis3DName: name to give to the axis3D created
148     axisXY: imported polyline, Embankment line
149     aCloud:  altimetry, for projection of axisXY
150     return altiProfile, axis3D
151     """
152     altiProfile = document.CreateObject(KIND_PROFILE)
153     altiProfile.SetName("%s_altiProfile"%axis3DName)
154     axis3D = document.CreateObject(KIND_POLYLINE)
155     axis3D.SetName(axis3DName)
156     axis3D.SetPolylineXY(axisXY)
157     axis3D.SetAltitudeObject(aCloud)
158     axis3D.SetChildProfileUZ(altiProfile.GetProfileUZ())
159     altiProfile.Update()
160     axis3D.Update()
161     return (altiProfile, axis3D)
162
163 def createEmbankmentSectionA(document, embankmentName, axis3D, sectionPoints, d, displayLevel):
164     """
165     document: current HYDROData_Document
166     embankmentName: name to give to the embankment
167     axis3D: the 3D axis of the embankment
168     sectionPoints: a list of tuple (x, h) to define a half section, beginning to x=0 (center)
169     d calculation parameter, take 2 or 3 times the section width
170     displayLevel : z level for 2D representation: high values are drawn above low values
171     return section, embankment
172     """
173     section = document.CreateObject(KIND_PROFILE)
174     section.SetName("%s_section"%embankmentName)
175     sectionPts = []
176     for i,p in enumerate(sectionPoints):
177         sectionPts.append(gp_XY(p[0], p[1]))
178         if i>0:
179             sectionPts.insert(0, gp_XY(-p[0], p[1]))
180     section.SetParametricPoints(sectionPts)
181     section.Update()
182     embankment = document.CreateObject(KIND_DIGUE)
183     embankment.SetName(embankmentName)
184     embankment.SetGuideLine(axis3D)
185     embankment.SetProfileMode(True)
186     embankment.SetProfile(section)
187     embankment.SetEquiDistance(d)
188     embankment.SetZLevel(displayLevel)
189     embankment.SetFillingColor(QColor(randint(0,255), randint(0,255), randint(0,255), 255))
190     embankment.Update()
191     return (section, embankment)
192
193 def createEmbankmentSectionB(document, embankmentName, axis3D, LC,DZ,CZ, d, displayLevel):
194     """
195     document: current HYDROData_Document
196     embankmentName: name to give to the embankment
197     axis3D: the 3D axis of the embankment
198     LC, DZ, CZ: width, height, height offset above axis3D
199     d calculation parameter, take 2 or 3 times the section width
200     displayLevel : z level for 2D representation: high values are drawn above low values
201     return embankment
202     """
203     embankment = document.CreateObject(KIND_DIGUE)
204     embankment.SetName(embankmentName)
205     embankment.SetGuideLine(axis3D)
206     embankment.SetProfileMode(False)
207     embankment.SetLCValue(LC)
208     embankment.SetDeltaZValue(DZ)
209     embankment.SetCoteZValue(CZ)
210     embankment.SetEquiDistance(d)
211     embankment.SetZLevel(displayLevel)
212     embankment.SetFillingColor(QColor(randint(0,255), randint(0,255), randint(0,255), 255))
213     embankment.Update()
214     return embankment
215
216     
217