Skip to content

Commit b7330b9

Browse files
committed
fix: migrate to omf mira, omf doesn't seem to work with anything
1 parent 2754b47 commit b7330b9

File tree

1 file changed

+49
-21
lines changed

1 file changed

+49
-21
lines changed

LoopStructural/export/omf_wrapper.py

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,55 @@
33
except ImportError:
44
raise ImportError(
55
"You need to install the omf package to use this feature. "
6-
"You can install it with: pip install --pre omf"
6+
"You can install it with: pip install mira-omf"
77
)
8+
import numpy as np
9+
import datetime
10+
import os
811

912

1013
def get_project(filename):
11-
try:
12-
project = omf.load(filename)
13-
except FileNotFoundError:
14-
project = omf.Project(name='LoopStructural Model')
15-
return project
14+
if os.path.exists(filename):
15+
16+
try:
17+
reader = omf.OMFReader(filename)
18+
project = reader.get_project()
19+
except (FileNotFoundError, ValueError):
20+
project = omf.Project(name='LoopStructural Model')
21+
return project
22+
else:
23+
return omf.Project(name='LoopStructural Model')
1624

1725

1826
def get_cell_attributes(loopobject):
1927
attributes = []
2028
if loopobject.cell_properties:
21-
attributes += [
22-
omf.NumericAttribute(name=k, array=v, location="faces")
23-
for k, v in loopobject.cell_properties.items()
24-
]
29+
for k, v in loopobject.cell_properties.items():
30+
v = np.array(v)
31+
if len(v.shape) > 1 and v.shape[1] > 1:
32+
for i in range(v.shape[1]):
33+
attributes.append(
34+
omf.ScalarData(name=f'{k}_{i}', array=v[:, i], location="faces")
35+
)
36+
else:
37+
attributes.append(omf.ScalarData(name=k, array=v, location="faces"))
38+
2539
return attributes
2640

2741

2842
def get_point_attributed(loopobject):
2943
attributes = []
3044
if loopobject.properties:
31-
attributes += [
32-
omf.NumericAttribute(name=k, array=v, location="vertices")
33-
for k, v in loopobject.properties.items()
34-
]
45+
for k, v in loopobject.properties.items():
46+
v = np.array(v)
47+
if len(v.shape) > 1 and v.shape[1] > 1:
48+
for i in range(v.shape[1]):
49+
attributes.append(
50+
omf.ScalarData(name=f'{k}_{i}', array=v[:, i], location="vertices")
51+
)
52+
else:
53+
attributes.append(omf.ScalarData(name=k, array=v, location="vertices"))
54+
3555
return attributes
3656

3757

@@ -40,32 +60,40 @@ def add_surface_to_omf(surface, filename):
4060
attributes = []
4161
attributes += get_cell_attributes(surface)
4262
attributes += get_point_attributed(surface)
43-
surface = omf.Surface(
44-
vertices=surface.vertices,
45-
triangles=surface.triangles,
46-
attributes=attributes,
63+
surface = omf.SurfaceElement(
64+
geometry=omf.SurfaceGeometry(
65+
vertices=surface.vertices,
66+
triangles=surface.triangles,
67+
),
68+
data=attributes,
4769
name=surface.name,
4870
)
4971
project = get_project(filename)
5072

5173
project.elements += [surface]
52-
omf.save(project, filename, mode='w')
74+
project.metadata = {
75+
"coordinate_reference_system": "epsg 3857",
76+
"date_created": datetime.datetime.utcnow(),
77+
"version": "v1.3",
78+
"revision": "10",
79+
}
80+
omf.OMFWriter(project, filename)
5381

5482

5583
def add_pointset_to_omf(points, filename):
5684

5785
attributes = []
5886
attributes += get_point_attributed(points)
5987

60-
points = omf.PointSet(
88+
points = omf.PointSetElement(
6189
vertices=points.locations,
6290
attributes=attributes,
6391
name=points.name,
6492
)
6593

6694
project = get_project(filename)
6795
project.elements += [points]
68-
omf.save(project, filename, mode='w')
96+
omf.OMFWriter(project, filename)
6997

7098

7199
def add_structured_grid_to_omf(grid, filename):

0 commit comments

Comments
 (0)