Skip to content
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

fix view and plot label in plot_field_from_fieldplot #4293

Merged
merged 8 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions _unittest/test_12_1_PostProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,13 @@ def test_14_Field_Ploton_cutplanedesignname(self):
assert plot1.update_field_plot_settings()
self.aedtapp.logger.info("Generating the image")
plot_obj = self.aedtapp.post.plot_field_from_fieldplot(
plot1.name,
plotname=plot1.name,
project_path=self.local_scratch.path,
meshplot=False,
imageformat="jpg",
view="isometric",
view="xy",
show=False,
plot_label=plot1.name + " label",
)
assert os.path.exists(plot_obj.image_file)
os.unlink(plot_obj.image_file)
Expand All @@ -526,6 +527,19 @@ def test_14_Field_Ploton_cutplanedesignname(self):
plot_obj.plot(plot_obj.image_file)
assert os.path.exists(plot_obj.image_file)

plot_obj = self.aedtapp.post.plot_field_from_fieldplot(
plotname=plot1.name,
project_path=self.local_scratch.path,
meshplot=False,
imageformat="jpg",
view="xy",
show=False,
plot_label=plot1.name + " label",
file_format="aedtplt",
)
assert os.path.exists(plot_obj.image_file)
plot_obj.plot(plot_obj.image_file)

@pytest.mark.skipif(is_linux or sys.version_info < (3, 8), reason="Not running in ironpython")
gmalinve marked this conversation as resolved.
Show resolved Hide resolved
def test_14B_Field_Ploton_Vector(self):
cutlist = ["Global:XY"]
Expand Down
42 changes: 31 additions & 11 deletions pyaedt/generic/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ def __init__(
self._is_frame = False
self.is_vector = False
self.vector_scale = 1.0
self.scalar_name = None


class CommonPlotter(object):
Expand Down Expand Up @@ -1326,7 +1327,7 @@ def _read_mesh_files(self, read_frames=False):
if ".case" in field.path:
reader = pv.get_reader(os.path.abspath(field.path)).read()
field._cached_polydata = reader[reader.keys()[0]].extract_surface()
field.label = field._cached_polydata.point_data.active_scalars_name
field.scalar_name = field._cached_polydata.point_data.active_scalars_name

elif ".aedtplt" in field.path:
vertices, faces, scalars, log1 = _parse_aedtplt(field.path)
Expand All @@ -1344,10 +1345,12 @@ def _read_mesh_files(self, read_frames=False):
field._cached_polydata.point_data[field.label] = np.array(
[np.linalg.norm(x) for x in np.vstack(scalars[0]).T]
)
field.scalar_name = field.field._cached_polydata.point_data.active_scalars_name

field.is_vector = True
else:
field._cached_polydata.point_data[field.label] = scalars[0]
field.scalar_name = field._cached_polydata.point_data.active_scalars_name
field.is_vector = False
field.log = log1
else:
Expand Down Expand Up @@ -1400,10 +1403,13 @@ def _read_mesh_files(self, read_frames=False):
filedata["vectors"] = np.vstack(values) * vector_scale
field.label = "Vector " + field.label
filedata.point_data[field.label] = np.array([np.linalg.norm(x) for x in np.vstack(values)])
field.scalar_name = field._cached_polydata.point_data.active_scalars_name
field.is_vector = True
field.is_vector = True
else:
filedata = filedata.delaunay_2d(tol=field.surface_mapping_tolerance)
filedata.point_data[field.label] = np.array(values)
field.scalar_name = field._cached_polydata.point_data.active_scalars_name
field._cached_polydata = filedata

@pyaedt_function_handler()
Expand Down Expand Up @@ -1552,12 +1558,13 @@ def plot(self, export_image_path=None):
position_y=2,
)
for field in self._fields:
sargs["title"] = field.label
if field.is_vector:
field._cached_polydata.set_active_vectors("vectors")
field._cached_polydata["vectors"] = field._cached_polydata["vectors"] * field.vector_scale
self.pv.add_mesh(
field._cached_polydata.arrows,
scalars=field.label,
scalars=field.scalar_name,
log_scale=False if self.convert_fields_in_db else field.log_scale,
scalar_bar_args=sargs,
cmap=field.color_map,
Expand All @@ -1566,7 +1573,7 @@ def plot(self, export_image_path=None):
elif self.range_max is not None and self.range_min is not None:
field._cached_mesh = self.pv.add_mesh(
field._cached_polydata,
scalars=field.label,
scalars=field.scalar_name,
log_scale=False if self.convert_fields_in_db else field.log_scale,
scalar_bar_args=sargs,
cmap=field.color_map,
Expand All @@ -1577,7 +1584,7 @@ def plot(self, export_image_path=None):
else:
field._cached_mesh = self.pv.add_mesh(
field._cached_polydata,
scalars=field.label,
scalars=field.scalar_name,
log_scale=False if self.convert_fields_in_db else field.log_scale,
scalar_bar_args=sargs,
cmap=field.color_map,
Expand Down Expand Up @@ -1605,6 +1612,12 @@ def plot(self, export_image_path=None):
self.pv.camera.position = self.camera_position
self.pv.camera.focal_point = self.focal_point
self.pv.camera.viewup = self.view_up
elif self.camera_position == "xy":
self.pv.view_xy()
elif self.camera_position == "xz":
self.pv.view_xz()
elif self.camera_position == "yz":
self.pv.view_yz()
else:
self.pv.camera_position = self.camera_position
self.pv.camera.focal_point = self.focal_point
Expand Down Expand Up @@ -1745,9 +1758,10 @@ def p_callback():
)

for field in self._fields:
sargs["title"] = field.label
field._cached_mesh = self.pv.add_mesh(
field._cached_polydata,
scalars=field.label,
scalars=field.scalar_name,
log_scale=False if self.convert_fields_in_db else field.log_scale,
scalar_bar_args=sargs,
cmap=field.color_map,
Expand All @@ -1761,14 +1775,14 @@ def p_callback():
mins = 1e20
maxs = -1e20
for el in self.frames:
if np.min(el._cached_polydata.point_data[el.label]) < mins:
mins = np.min(el._cached_polydata.point_data[el.label])
if np.max(el._cached_polydata.point_data[el.label]) > maxs:
maxs = np.max(el._cached_polydata.point_data[el.label])
if np.min(el._cached_polydata.point_data[el.scalar_name]) < mins:
mins = np.min(el._cached_polydata.point_data[el.scalar_name])
if np.max(el._cached_polydata.point_data[el.scalar_name]) > maxs:
maxs = np.max(el._cached_polydata.point_data[el.scalar_name])

self.frames[0]._cached_mesh = self.pv.add_mesh(
self.frames[0]._cached_polydata,
scalars=self.frames[0].label,
scalars=self.frames[0].scalar_name,
log_scale=False if self.convert_fields_in_db else self.frames[0].log_scale,
scalar_bar_args=sargs,
cmap=self.frames[0].color_map,
Expand All @@ -1787,6 +1801,12 @@ def p_callback():
self.pv.camera.position = self.camera_position
self.pv.camera.focal_point = self.focal_point
self.pv.camera.up = self.view_up
elif self.camera_position == "xy":
self.pv.view_xy()
elif self.camera_position == "xz":
self.pv.view_xz()
elif self.camera_position == "yz":
self.pv.view_yz()
else:
self.pv.camera_position = self.camera_position
self.pv.camera.azimuth += self.azimuth_angle
Expand Down Expand Up @@ -1819,7 +1839,7 @@ def p_callback():
break
i = 0
first_loop = False
scalars = self.frames[i]._cached_polydata.point_data[self.frames[i].label]
scalars = self.frames[i]._cached_polydata.point_data[self.frames[i].scalar_name]
self.pv.update_scalars(scalars, render=False)
if not hasattr(self.pv, "ren_win"):
break
Expand Down
9 changes: 7 additions & 2 deletions pyaedt/modules/AdvancedPostProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ def plot_field_from_fieldplot(
show_bounding=False,
show_legend=True,
plot_as_separate_objects=True,
file_format="case",
):
"""Export a field plot to an image file (JPG or PNG) using Python PyVista.

Expand Down Expand Up @@ -377,6 +378,12 @@ def plot_field_from_fieldplot(
Whether to display the legend or not. The default is ``True``.
plot_as_separate_objects : bool, optional
Plot each object separately. It may require more time to export from AEDT.
file_format : str, optional
File format of the exported plot.
Available options are: ``"case"`` or ``"aedtplt"``.
gmalinve marked this conversation as resolved.
Show resolved Hide resolved
In case the active design is a Q3D design, the file format is automatically
set to ``"fldplt"``.
The default is ``"case"``.
gmalinve marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
Expand All @@ -393,8 +400,6 @@ def plot_field_from_fieldplot(

if self.field_plots[plotname].field_type == "DC R/L Fields":
file_format = "fldplt"
else:
file_format = "case"
file_to_add = self.export_field_plot(plotname, self._app.working_directory, file_format=file_format)
model = self.get_model_plotter_geometries(
generate_mesh=False,
Expand Down