-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgis2salome.py
executable file
·202 lines (182 loc) · 8.49 KB
/
gis2salome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
#
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
# #
# gis2salome.py #
# #
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
#
# Author: Pat Prodanovic, Ph.D., P.Eng.
#
# Date: Feb 6, 2018
#
# Purpose: Script takes in a text file of the geometry generated in qgis
# (or any other gis or cad package) and geometry to be used by
# the salome platform. The produced file is a python script that can be
# directly loaded by the salome platform for geometry and mesh creation.
#
# Revised: Feb 12, 2018
# Added an extra argument to specify salome splines.
#
# Uses: Python 2 or 3, Numpy
#
# Example:
#
# python gis2gmsh.py -b boundary.csv -l lines.csv -h holes.csv -o out.py
#
# where:
# --> -b is the node listing of the outer boundary for the mesh.
# The boundary file is generated by snapping lines
# to the nodes from the nodes.csv file. The boundary file
# consists of shapeid,x,y of all the lines in the file.
# Boundary has to be a closed shape, where first and last
# nodes are identical. Shapeid is a integer, where the
# boundary is defined with a distict id (i.e., shapeid
# of 0).
#
# --> -l is the node listing of the constraint lines for the mesh.
# The lines file can include open or closed polylines.
# The file listing has shapeid,x,y. Each distinct
# line has to have an individual (integer) shapeid. If no
# constraint lines in the mesh, enter 'none' without the quotes.
#
# --> -h is the node listing of the holes in the mesh.
# The holes file must include closed polylines. The
# file listing has shapeid,x,y. Each distinct hole has to
# have an individual (integer) shapeid. If no holes
# (islands) in the mesh, enter 'none' without the quotes.
#
# --> -s is the node listing of the Salome splines in the mesh.
# The splines file must include open polylines. The
# file listing has shapeid,x,y. Each distinct spline has to
# have an individual (integer) shapeid. If no splines
# in the mesh, enter 'none' without the quotes.
#
#--> -o is the output python script that is to be loaded with the salome platform.
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Global Imports
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import os,sys # system parameters
import numpy as np # numpy
#
# I/O
if (len(sys.argv) == 11):
boundary_file = sys.argv[2]
lines_file = sys.argv[4]
holes_file = sys.argv[6]
splines_file = sys.argv[8]
output_file = sys.argv[10]
else:
print('Wrong number of Arguments, stopping now...')
print('Usage:')
print('python gis2salome.py -b boundary.csv -l lines.csv -h holes.csv -s splines.csv -o out.py')
sys.exit()
# we are now ready to read in the data from the above files
boundary_data = np.loadtxt(boundary_file, delimiter=',',skiprows=0,unpack=True)
xboun = boundary_data[1,:]
yboun = boundary_data[2,:]
nboun = len(xboun)
if (lines_file != 'none'):
lines_data = np.loadtxt(lines_file, delimiter=',',skiprows=0,unpack=True)
if (holes_file != 'none'):
holes_data = np.loadtxt(holes_file, delimiter=',',skiprows=0,unpack=True)
if (splines_file != 'none'):
splines_data = np.loadtxt(splines_file, delimiter=',',skiprows=0,unpack=True)
# we can start writing the output file (this file is to be loaded with salome)
fout = open(output_file, 'w')
# this is the salome header
fout.write('import sys, os, salome' + '\n')
fout.write('salome.salome_init()' + '\n')
fout.write('theStudy = salome.myStudy' + '\n')
fout.write('import salome_notebook' + '\n')
fout.write('notebook = salome_notebook.NoteBook(theStudy)' + '\n')
fout.write('curdir = os.getcwd()' + '\n')
fout.write('sys.path.insert(0, curdir)' + '\n')
fout.write('import GEOM' + '\n')
fout.write('from salome.geom import geomBuilder' + '\n')
fout.write('import math' + '\n')
fout.write('import SALOMEDS' + '\n')
fout.write('geompy = geomBuilder.New(theStudy)' + '\n')
fout.write('O = geompy.MakeVertex(0, 0, 0)' + '\n')
fout.write('OX = geompy.MakeVectorDXDYDZ(1, 0, 0)' + '\n')
fout.write('OY = geompy.MakeVectorDXDYDZ(0, 1, 0)' + '\n')
fout.write('OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)' + '\n')
# now we can write the boundary to the salome format
# the True below is to indicate that the polyline is closed
fout.write('pl = geompy.Polyline2D()' + '\n')
fout.write('pl.addSection("Section_1", GEOM.Polyline, True)' + '\n')
# now we loop through the points of the boundary file, neglecting the last node
for i in range(nboun - 1):
fout.write('pl.addPoints([' + str(xboun[i]) + ', ' + str(yboun[i]) + '])' + '\n')
fout.write('Boundary = pl.result([0, 0, 0, 0, 0, 1, 1, 0, -0])' + '\n')
# if line constraints are present, they are written here
if (lines_file != 'none'):
lns_shapeid = lines_data[0,:]
lns_x = lines_data[1,:]
lns_y = lines_data[2,:]
distinct_lines = np.unique(lns_shapeid)
num_distinct_lines = len(distinct_lines)
pname = list()
# now we can go through each line, and write its data
for i in range(num_distinct_lines):
fout.write('pl = geompy.Polyline2D()' + '\n')
fout.write('pl.addSection("Section_1", GEOM.Polyline, False)' + '\n')
for j in range(len(lns_x)):
if ((int(distinct_lines[i]) - int(lns_shapeid[j])) == 0):
fout.write('pl.addPoints([' + str(lns_x[j]) + ', ' + str(lns_y[j]) + '])' + '\n')
pname.append('Polyline_' + str(int(distinct_lines[i])))
fout.write(pname[i] + ' = pl.result([0, 0, 0, 0, 0, 1, 1, 0, -0])' + '\n')
# if we have islands present, they are written here
# islands are closed polygons
if (holes_file != 'none'):
hls_shapeid = holes_data[0,:]
hls_x = holes_data[1,:]
hls_y = holes_data[2,:]
distinct_holes = np.unique(hls_shapeid)
num_distinct_holes = len(distinct_holes)
hname = list()
# now we go through each hole, and write its data
for i in range(num_distinct_holes):
fout.write('pl = geompy.Polyline2D()' + '\n')
fout.write('pl.addSection("Section_1", GEOM.Polyline, True)' + '\n')
for j in range(len(hls_x)):
if ((int(distinct_holes[i]) - int(hls_shapeid[j])) == 0):
fout.write('pl.addPoints([' + str(hls_x[j]) + ', ' + str(hls_y[j]) + '])' + '\n')
hname.append('Island_' + str(int(distinct_holes[i])))
fout.write(hname[i] + ' = pl.result([0, 0, 0, 0, 0, 1, 1, 0, -0])' + '\n')
# if we have splines present, they are written here
# splines are open polylines
if (splines_file != 'none'):
spl_shapeid = splines_data[0,:]
spl_x = splines_data[1,:]
spl_y = splines_data[2,:]
distinct_splines = np.unique(spl_shapeid)
num_distinct_splines = len(distinct_splines)
sname = list()
# now we go through each hole, and write its data
for i in range(num_distinct_splines):
fout.write('pl = geompy.Polyline2D()' + '\n')
fout.write('pl.addSection("Section_1", GEOM.Interpolation, False)' + '\n')
for j in range(len(spl_x)):
if ((int(distinct_splines[i]) - int(spl_shapeid[j])) == 0):
fout.write('pl.addPoints([' + str(spl_x[j]) + ', ' + str(spl_y[j]) + '])' + '\n')
sname.append('Spline_' + str(int(distinct_splines[i])))
fout.write(sname[i] + ' = pl.result([0, 0, 0, 0, 0, 1, 1, 0, -0])' + '\n')
# this is needed for the tail end of the salome file
fout.write("geompy.addToStudy( O, 'O' )" + '\n')
fout.write("geompy.addToStudy( OX, 'OX' )" + '\n')
fout.write("geompy.addToStudy( OY, 'OY' )" + '\n')
fout.write("geompy.addToStudy( OZ, 'OZ' )" + '\n')
fout.write("geompy.addToStudy( Boundary, 'Boundary' )" + '\n')
if (lines_file != 'none'):
for i in range(len(pname)):
fout.write('geompy.addToStudy( ' + pname[i] + ', ' + "'" + pname[i] + "')" + '\n')
if (holes_file != 'none'):
for i in range(len(hname)):
fout.write('geompy.addToStudy( ' + hname[i] + ', ' + "'" + hname[i] + "')" + '\n')
if (splines_file != 'none'):
for i in range(len(sname)):
fout.write('geompy.addToStudy( ' + sname[i] + ', ' + "'" + sname[i] + "')" + '\n')
fout.write('if salome.sg.hasDesktop():' + '\n')
fout.write(' salome.sg.updateObjBrowser(True)' + '\n')