Salome HOME
selection on object browser for shapes in fitShapesPointsToMesheEdges dialog
[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     print("importPolylines %s %s"%(shapeName, shapeType))
67     index = 0  # for multiple shapes
68     while isShapeFound:
69         shapeNameIndex = shapeName + "_%d" % index
70         index = index + 1
71         print("try name %s"%shapeNameIndex)
72         shape = document.FindObjectByName(shapeNameIndex)
73         if shape is None:
74             isShapeFound = False
75         else:
76             print("found %s"%shapeNameIndex)
77             for i in range(shape.NbSections()):
78                 shape.SetSectionType(i, shapeType)
79                 shape.Update()
80                 shape.SetZLevel(displayLevel)
81             shapes.append(shape)
82     if index <= 1: # no multiple shapes, try single shape  
83         print("try name %s"%shapeName)
84         shape = document.FindObjectByName(shapeName) # single shape
85         if shape is not None:
86             print("found %s"%shapeName)
87             for i in range(shape.NbSections()):
88                 shape.SetSectionType(i, shapeType)
89                 shape.Update()
90                 shape.SetZLevel(displayLevel)
91             shapes.append(shape)
92     return shapes
93
94 def importBathymetry(document, bathyName, bathyPath):
95     """
96     """
97     bathy = document.CreateObject(KIND_BATHYMETRY)
98     bathy.SetName(bathyName)
99     bathy.SetAltitudesInverted(0)
100     if not(bathy.ImportFromFile( os.path.join(bathyPath, bathyName + '.xyz' ))):
101         raise ValueError('problem while loading bathymetry')
102     bathy.Update()
103     return bathy
104
105 def createImmersibleZone(document, imzName, polyLine, bathy, isImmersible, displayLevel):
106     """
107     """
108     imz = document.CreateObject(KIND_IMMERSIBLE_ZONE)
109     imz.SetName(imzName)
110     imz.SetZLevel(displayLevel)
111     imz.SetAltitudeObject(bathy)
112     imz.SetPolyline(polyLine)
113     imz.SetIsSubmersible(isImmersible)
114     imz.SetFillingColor(QColor(randint(0,255), randint(0,255), randint(0,255), 255))
115     imz.Update()
116     return imz
117
118 def mergePolylines(document, polyName, polyLines, isConnectedBySegment = False, tolerance = 1.E-3):
119     """
120     """
121     op=HYDROData_PolylineOperator()
122     op.Merge(document, polyName, polyLines, isConnectedBySegment, tolerance)
123     shape = document.FindObjectByName(polyName)
124     return shape
125
126 def createAxis3DDEmbankmentAltiProfile(document, axis3DName, axisXY, altiPts):
127     """
128     document: current HYDROData_Document
129     axis3DName: name to give to the axis3D created
130     axisXY: imported polyline, Embankment line
131     altiPts: list of tuples(x, h) : parametric coordinate, altitude, along the axis
132     return altiProfile, axis3D
133     """
134     altiProfile = document.CreateObject(KIND_PROFILE)
135     altiProfile.SetName("%s_altiProfile"%axis3DName)
136     altiProfilePoints = []
137     for p in altiPts:
138         altiProfilePoints.append(gp_XY(p[0], p[1]))
139     altiProfile.SetParametricPoints(altiProfilePoints)
140     altiProfile.Update()
141     axis3D = document.CreateObject(KIND_POLYLINE)
142     axis3D.SetName(axis3DName)
143     axis3D.SetPolylineXY(axisXY)
144     axis3D.SetProfileUZ(altiProfile.GetProfileUZ())
145     axis3D.Update()
146     return (altiProfile, axis3D)
147     
148 def createAxis3DDEmbankmentBathy(document, axis3DName, axisXY, aCloud):
149     """
150     document: current HYDROData_Document
151     axis3DName: name to give to the axis3D created
152     axisXY: imported polyline, Embankment line
153     aCloud:  altimetry, for projection of axisXY
154     return altiProfile, axis3D
155     """
156     altiProfile = document.CreateObject(KIND_PROFILE)
157     altiProfile.SetName("%s_altiProfile"%axis3DName)
158     axis3D = document.CreateObject(KIND_POLYLINE)
159     axis3D.SetName(axis3DName)
160     axis3D.SetPolylineXY(axisXY)
161     axis3D.SetAltitudeObject(aCloud)
162     axis3D.SetChildProfileUZ(altiProfile.GetProfileUZ())
163     altiProfile.Update()
164     axis3D.Update()
165     return (altiProfile, axis3D)
166
167 def createEmbankmentSectionA(document, embankmentName, axis3D, sectionPoints, d, displayLevel):
168     """
169     document: current HYDROData_Document
170     embankmentName: name to give to the embankment
171     axis3D: the 3D axis of the embankment
172     sectionPoints: a list of tuple (x, h) to define a half section, beginning to x=0 (center)
173     d calculation parameter, take 2 or 3 times the section width
174     displayLevel : z level for 2D representation: high values are drawn above low values
175     return section, embankment
176     """
177     section = document.CreateObject(KIND_PROFILE)
178     section.SetName("%s_section"%embankmentName)
179     sectionPts = []
180     for i,p in enumerate(sectionPoints):
181         sectionPts.append(gp_XY(p[0], p[1]))
182         if i>0:
183             sectionPts.insert(0, gp_XY(-p[0], p[1]))
184     section.SetParametricPoints(sectionPts)
185     section.Update()
186     embankment = document.CreateObject(KIND_DIGUE)
187     embankment.SetName(embankmentName)
188     embankment.SetGuideLine(axis3D)
189     embankment.SetProfileMode(True)
190     embankment.SetProfile(section)
191     embankment.SetEquiDistance(d)
192     embankment.SetZLevel(displayLevel)
193     embankment.SetFillingColor(QColor(randint(0,255), randint(0,255), randint(0,255), 255))
194     embankment.Update()
195     return (section, embankment)
196
197 def createEmbankmentSectionB(document, embankmentName, axis3D, LC,DZ,CZ, d, displayLevel):
198     """
199     document: current HYDROData_Document
200     embankmentName: name to give to the embankment
201     axis3D: the 3D axis of the embankment
202     LC, DZ, CZ: width, height, height offset above axis3D
203     d calculation parameter, take 2 or 3 times the section width
204     displayLevel : z level for 2D representation: high values are drawn above low values
205     return embankment
206     """
207     embankment = document.CreateObject(KIND_DIGUE)
208     embankment.SetName(embankmentName)
209     embankment.SetGuideLine(axis3D)
210     embankment.SetProfileMode(False)
211     embankment.SetLCValue(LC)
212     embankment.SetDeltaZValue(DZ)
213     embankment.SetCoteZValue(CZ)
214     embankment.SetEquiDistance(d)
215     embankment.SetZLevel(displayLevel)
216     embankment.SetFillingColor(QColor(randint(0,255), randint(0,255), randint(0,255), 255))
217     embankment.Update()
218     return embankment
219
220     
221