Skip to content

refactor: improve python integration by using magick method and properties #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/source/cheat_sheet/cheat_sheet_script.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ root_part.commit()
body_1 = root_part.create_body(name='TheBodyB1')
body_1.commit()
body_1_face_1 = body_1.create_face(name='TheFaceF1')
body_1_face_1.set_vertices([])
body_1_face_1.set_facets([])
body_1_face_1.set_normals([])
body_1_face_1.vertices = []
body_1_face_1.facets = []
body_1_face_1.normals = []
body_1_face_1.commit()
```

Expand Down
6 changes: 3 additions & 3 deletions examples/core-modify-scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
# +
for sensor_i in my_scene.get().sensors:
print(sensor_i) # Print instance data model
print(speos.client.get_item(key=sensor_i.sensor_guid)) # Print template data model
print(speos.client[sensor_i.sensor_guid]) # Print template data model
print("\n")
# -

Expand All @@ -72,7 +72,7 @@
for sensor_i in my_scene.get().sensors:
if sensor_i.HasField("camera_properties"):
print(sensor_i) # Print instance data model
print(speos.client.get_item(key=sensor_i.sensor_guid)) # Print template data model
print(speos.client[sensor_i.sensor_guid]) # Print template data model
print("\n")
# -

Expand Down Expand Up @@ -104,7 +104,7 @@
new_distortion_file = os.path.join(tests_data_path, os.path.join("CameraInputFiles", "CameraDistortion_150deg.OPTDistortion"))

# Retrieve SensorTemplateLink corresponding to camera_i_0.sensor_guid
camera_t_0 = speos.client.get_item(camera_i_0.sensor_guid)
camera_t_0 = speos.client[camera_i_0.sensor_guid]
assert isinstance(camera_t_0, core.SensorTemplateLink)

# get() = retrieve datamodel corresponding to camera_t_0 from database
Expand Down
25 changes: 11 additions & 14 deletions examples/script-part.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@
# Each triangle/facet is defined by vertices and vertice normals.

# +
face_b1_f1 = (
body_b1.create_face(name="TheFaceF1")
.set_vertices([0, 0, 0, 1, 0, 0, 0, 1, 0])
.set_facets([0, 1, 2])
.set_normals([0, 0, 1, 0, 0, 1, 0, 0, 1])
.commit()
)
face_b1_f1 = body_b1.create_face(name="TheFaceF1")
face_b1_f1.vertices = [0, 0, 0, 1, 0, 0, 0, 1, 0]
face_b1_f1.facets = [0, 1, 2]
face_b1_f1.normals = [0, 0, 1, 0, 0, 1, 0, 0, 1]
face_b1_f1.commit()

print(root_part)
# -

Expand All @@ -86,13 +85,11 @@

body_sp1_b1 = sub_part1.create_body(name="TheBodySP1_B1").commit()
print(root_part)
face_sp1_b1_f1 = (
body_sp1_b1.create_face(name="TheFaceSP1_B1_F1")
.set_vertices([0, 1, 0, 0, 2, 0, 1, 2, 0])
.set_facets([0, 1, 2])
.set_normals([0, 0, 1, 0, 0, 1, 0, 0, 1])
.commit()
)
face_sp1_b1_f1 = body_sp1_b1.create_face(name="TheFaceSP1_B1_F1")
face_sp1_b1_f1.vertices = [0, 1, 0, 0, 2, 0, 1, 2, 0]
face_sp1_b1_f1.facets = [0, 1, 2]
face_sp1_b1_f1.normals = [0, 0, 1, 0, 0, 1, 0, 0, 1]
face_sp1_b1_f1.commit()
print(root_part)
# -

Expand Down
8 changes: 5 additions & 3 deletions examples/script-simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@

# +
root_part = p.create_root_part()
root_part.create_body(name="Body.1").create_face(name="Face.1").set_vertices([0, 1, 2, 0, 2, 2, 1, 2, 2]).set_facets([0, 1, 2]).set_normals(
[0, 0, 1, 0, 0, 1, 0, 0, 1]
)
body = root_part.create_body(name="Body.1")
face = body.create_face(name="Face.1")
face.vertices = [0, 1, 2, 0, 2, 2, 1, 2, 2]
face.facets = [0, 1, 2]
face.normals = [0, 0, 1, 0, 0, 1, 0, 0, 1]
root_part.commit()
# -

Expand Down
2 changes: 1 addition & 1 deletion src/ansys/speos/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def jobs(self) -> JobStub:
self._jobDB = JobStub(self._channel)
return self._jobDB

def get_item(
def __getitem__(
self, key: str
) -> Union[
SOPTemplateLink,
Expand Down
24 changes: 21 additions & 3 deletions src/ansys/speos/script/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ def __init__(
# Create local Face
self._face = core.Face(name=name, description=description, metadata=metadata)

def set_vertices(self, values: List[float]) -> Face:
@property
def vertices(self):
res = self.face_link.get().vertices
return res

@vertices.setter
def vertices(self, values: List[float]) -> Face:
"""Set the face vertices.

Parameters
Expand All @@ -88,7 +94,13 @@ def set_vertices(self, values: List[float]) -> Face:
self._face.vertices[:] = values
return self

def set_facets(self, values: List[int]) -> Face:
@property
def facets(self):
res = self.face_link.get().facets
return res

@facets.setter
def facets(self, values: List[int]) -> Face:
"""Set the facets.

Parameters
Expand All @@ -104,7 +116,13 @@ def set_facets(self, values: List[int]) -> Face:
self._face.facets[:] = values
return self

def set_normals(self, values: List[float]) -> Face:
@property
def normals(self):
res = self.face_link.get().normals
return res

@normals.setter
def normals(self, values: List[float]) -> Face:
"""Set the face normals.

Parameters
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/speos/script/intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def __init__(
self.set_cos(N=1) # By default will be lambertian (cos with N =1)
else:
# Retrieve IntensityTemplate
self.intensity_template_link = speos_client.get_item(key=key)
self.intensity_template_link = speos_client[key]
self._intensity_template = self.intensity_template_link.get()

def set_library(self) -> Intensity.Library:
Expand Down
4 changes: 2 additions & 2 deletions src/ansys/speos/script/opt_prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ def delete(self) -> OptProp:
def _fill(self, mat_inst):
self._unique_id = mat_inst.metadata["UniqueId"]
self._material_instance = mat_inst
self.vop_template_link = self._project.client.get_item(key=mat_inst.vop_guid)
self.vop_template_link = self._project.client[mat_inst.vop_guid]
if len(mat_inst.sop_guids) > 0:
self.sop_template_link = self._project.client.get_item(key=mat_inst.sop_guids[0])
self.sop_template_link = self._project.client[mat_inst.sop_guids[0]]
else: # Specific case for ambient material
self._sop_template = None
self.reset()
16 changes: 8 additions & 8 deletions src/ansys/speos/script/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def _fill_bodies(self, body_guids: List[str], feat_host: Union[part.Part, part.P
def _add_unique_ids(self):
scene_data = self.scene_link.get()

root_part_link = self.client.get_item(key=scene_data.part_guid)
root_part_link = self.client[scene_data.part_guid]
root_part = root_part_link.get()
update_rp = False
for sub_part in root_part.parts:
Expand Down Expand Up @@ -527,7 +527,7 @@ def _fill_features(self):

scene_data = self.scene_link.get()

root_part_link = self.client.get_item(key=scene_data.part_guid)
root_part_link = self.client[scene_data.part_guid]
root_part_data = root_part_link.get()
root_part_feats = self.find(name="", feature_type=part.Part)
root_part_feat = None
Expand All @@ -547,7 +547,7 @@ def _fill_features(self):
if sp.description.startswith("UniqueId_"):
idx = sp.description.find("_")
sp_feat._unique_id = sp.description[idx + 1 :]
sp_feat.part_link = self.client.get_item(key=sp.part_guid)
sp_feat.part_link = self.client[sp.part_guid]
part_data = sp_feat.part_link.get()
sp_feat._part_instance = sp
sp_feat._part = part_data # instead of sp_feat.reset() - this avoid a useless read in server
Expand Down Expand Up @@ -577,7 +577,7 @@ def _fill_features(self):
self._features.append(ssr_feat)

for sim_inst in scene_data.simulations:
simulation_template_link = self.client.get_item(key=sim_inst.simulation_guid).get()
simulation_template_link = self.client[sim_inst.simulation_guid].get()
if simulation_template_link.HasField("direct_mc_simulation_template"):
sim_feat = simulation.Direct(project=self, name=sim_inst.name, simulation_instance=sim_inst, default_values=False)
elif simulation_template_link.HasField("inverse_mc_simulation_template"):
Expand Down Expand Up @@ -631,9 +631,9 @@ def local2absolute(local_vertice: np.ndarray, coordinates) -> np.ndarray:
part_coordinate = part_coordinate_info
part_mesh_info = None
for body_idx, body_guid in enumerate(part_data.body_guids):
body_item_data = self.client.get_item(body_guid).get()
body_item_data = self.client[body_guid].get()
for face_idx, face_guid in enumerate(body_item_data.face_guids):
face_item_data = self.client.get_item(face_guid).get()
face_item_data = self.client[face_guid].get()
vertices = np.array(face_item_data.vertices)
facets = np.array(face_item_data.facets)
vertices = vertices.reshape(-1, 3)
Expand Down Expand Up @@ -668,12 +668,12 @@ def _create_preview(self, viz_args=None) -> pv.Plotter:
viz_args = {}
_preview_mesh = pv.PolyData()
# Retrieve root part
root_part_data = self.client.get_item(self.scene_link.get().part_guid).get()
root_part_data = self.client[self.scene_link.get().part_guid].get()

# Loop on all sub parts to retrieve their mesh
if len(root_part_data.parts) != 0:
for part_idx, part_item in enumerate(root_part_data.parts):
part_item_data = self.client.get_item(part_item.part_guid).get()
part_item_data = self.client[part_item.part_guid].get()
poly_data = self.__extract_part_mesh_info(part_data=part_item_data, part_coordinate_info=part_item.axis_system)
if poly_data is not None:
_preview_mesh = _preview_mesh.append_polydata(poly_data)
Expand Down
4 changes: 2 additions & 2 deletions src/ansys/speos/script/proto_message_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _replace_guid_elt(speos_client: SpeosClient, json_dict: dict, ignore_simple_
# If we are in the case of key "xxx_guid", with a guid non empty and that the key is not to ignore
if k.endswith("_guid") and v != "" and k != ignore_simple_key:
# Retrieve the item from db and transform it to dictionary
new_v = protobuf_message_to_dict(message=speos_client.get_item(key=v).get())
new_v = protobuf_message_to_dict(message=speos_client[v].get())

# This item can potentially have some "xxx_guid" fields to replace
_replace_guid_elt(speos_client=speos_client, json_dict=new_v, ignore_simple_key=ignore_simple_key)
Expand All @@ -71,7 +71,7 @@ def _replace_guid_elt(speos_client: SpeosClient, json_dict: dict, ignore_simple_
new_value_list = []
for iv in v:
# Retrieve the item from db and transform it to dictionary
new_v = protobuf_message_to_dict(message=speos_client.get_item(key=iv).get())
new_v = protobuf_message_to_dict(message=speos_client[iv].get())

# This item can potentially have some "xxx_guid" fields to replace
_replace_guid_elt(speos_client=speos_client, json_dict=new_v, ignore_simple_key=ignore_simple_key)
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/speos/script/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(
self._sensor_instance = core.Scene.SensorInstance(name=name, description=description, metadata=metadata)
else:
self._unique_id = sensor_instance.metadata["UniqueId"]
self.sensor_template_link = self._project.client.get_item(key=sensor_instance.sensor_guid)
self.sensor_template_link = self._project.client[sensor_instance.sensor_guid]
# reset will fill _sensor_instance and _sensor_template from respectively project (using _unique_id) and sensor_template_link
self.reset()

Expand Down
4 changes: 2 additions & 2 deletions src/ansys/speos/script/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def __init__(
self._simulation_instance = core.Scene.SimulationInstance(name=name, description=description, metadata=metadata)
else:
self._unique_id = simulation_instance.metadata["UniqueId"]
self.simulation_template_link = self._project.client.get_item(key=simulation_instance.simulation_guid)
self.simulation_template_link = self._project.client[simulation_instance.simulation_guid]
self.reset()

# Create local Job
Expand Down Expand Up @@ -433,7 +433,7 @@ def delete(self) -> BaseSimulation:
def _fill(self, sim_inst):
self._unique_id = sim_inst.metadata["UniqueId"]
self._simulation_instance = sim_inst
self.simulation_template_link = self._project.client.get_item(key=sim_inst.simulation_guid)
self.simulation_template_link = self._project.client[sim_inst.simulation_guid]
self.reset()


Expand Down
4 changes: 2 additions & 2 deletions src/ansys/speos/script/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __init__(
self._source_instance = core.Scene.SourceInstance(name=name, description=description, metadata=metadata)
else:
self._unique_id = source_instance.metadata["UniqueId"]
self.source_template_link = self._project.client.get_item(key=source_instance.source_guid)
self.source_template_link = self._project.client[source_instance.source_guid]
self._reset()

class _Spectrum:
Expand Down Expand Up @@ -292,7 +292,7 @@ def _delete(self) -> BaseSource:
def _fill(self, src_inst):
self._unique_id = src_inst.metadata["UniqueId"]
self._source_instance = src_inst
self.source_template_link = self._project.client.get_item(key=src_inst.source_guid)
self.source_template_link = self._project.client[src_inst.source_guid]
self._reset()

def commit(self) -> BaseSource:
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/speos/script/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(
self.set_monochromatic() # By default will be monochromatic
else:
# Retrieve Spectrum
self.spectrum_link = speos_client.get_item(key=key)
self.spectrum_link = speos_client[key]
self._spectrum = self.spectrum_link.get()

def set_monochromatic(self, wavelength: float = 555.0) -> Spectrum:
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/speos/workflow/combine_speos.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def insert_speos(project: Project, speos_to_insert: List[SpeosFileInstance]) ->
if project.scene_link.get().part_guid == "":
part_link = project.client.parts().create(message=Part())
else:
part_link = project.client.get_item(project.scene_link.get().part_guid)
part_link = project.client[project.scene_link.get().part_guid]

# Combine all speos_to_insert into the project
_combine(project=project, part_link=part_link, speos_to_combine=speos_to_insert)
Expand Down
6 changes: 3 additions & 3 deletions tests/core_tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_body(speos: Speos):
faces_to_delete = body0.get().face_guids
body0.delete()
for face_key in faces_to_delete:
face = speos.client.get_item(key=face_key)
face = speos.client[face_key]
assert isinstance(face, FaceLink)
face.delete()

Expand Down Expand Up @@ -212,10 +212,10 @@ def test_part(speos: Speos):
assert part1.key != ""

for body_key in part1.get().body_guids:
body = speos.client.get_item(key=body_key)
body = speos.client[body_key]
assert isinstance(body, BodyLink)
for face_key in body.get().face_guids:
face = speos.client.get_item(key=face_key)
face = speos.client[face_key]
assert isinstance(face, FaceLink)
face.delete()
body.delete()
Expand Down
30 changes: 13 additions & 17 deletions tests/script_tests/test_Part.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,27 @@ def test_create_face(speos: Speos):
assert len(body1.body_link.get().face_guids) == 0

# Add a face
face1 = (
body1.create_face(name="Face.1")
.set_vertices([0, 1, 0, 0, 2, 0, 1, 2, 0])
.set_facets([0, 1, 2])
.set_normals([0, 0, 1, 0, 0, 1, 0, 0, 1])
.commit()
)
assert len(body1._geom_features) == 1
face1 = body1.create_face(name="Face.1")
face1.vertices = [0, 1, 0, 0, 2, 0, 1, 2, 0]
face1.facets = [0, 1, 2]
face1.normals = [0, 0, 1, 0, 0, 1, 0, 0, 1]
face1.commit()

assert len(body1._geom_features) == 1
assert len(body1.body_link.get().face_guids) == 1
assert body1.body_link.get().face_guids[0] == face1.face_link.key
assert face1.face_link.get().vertices == [0, 1, 0, 0, 2, 0, 1, 2, 0]
assert face1.face_link.get().facets == [0, 1, 2]
assert face1.face_link.get().normals == [0, 0, 1, 0, 0, 1, 0, 0, 1]

# Add another face + commit on root part
face2 = (
body1.create_face(name="Face.2")
.set_vertices([0, 0, 0, 1, 0, 0, 0, 1, 0])
.set_facets([0, 2, 1])
.set_normals([0, 0, 1, 0, 0, 1, 0, 0, 1])
.commit()
)
assert len(body1._geom_features) == 2
face2 = body1.create_face(name="Face.2")
face2.vertices = [0, 0, 0, 1, 0, 0, 0, 1, 0]
face2.facets = [0, 2, 1]
face2.normals = [0, 0, 1, 0, 0, 1, 0, 0, 1]
face2.commit()

assert len(body1._geom_features) == 2
assert len(body1.body_link.get().face_guids) == 2
assert body1.body_link.get().face_guids[1] == face2.face_link.key
assert face2.face_link.get().vertices == [0, 0, 0, 1, 0, 0, 0, 1, 0]
Expand Down Expand Up @@ -239,7 +235,7 @@ def test_commit_part(speos: Speos):
assert root_part.part_link is not None
assert p.scene_link.get().part_guid == root_part.part_link.key
assert len(root_part.part_link.get().parts) == 1
assert len(speos.client.get_item(key=root_part.part_link.get().parts[0].part_guid).get().parts) == 1
assert len(speos.client[root_part.part_link.get().parts[0].part_guid].get().parts) == 1
assert len(root_part.part_link.get().body_guids) == 1

# Change only in local not committed
Expand Down
Loading
Loading