Skip to content

Commit

Permalink
Fixed broken conditional statements in ComponentPlotter so now choose…
Browse files Browse the repository at this point in the history
…s correct plotting option (#2980)

* fix conditional statements to work based on face colour

* improve coverage of plotting example for all priority levels

* added test coverage
  • Loading branch information
kj5248 authored Feb 5, 2024
1 parent 86bc1f4 commit ab1878e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
14 changes: 11 additions & 3 deletions bluemira/display/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
get_centroid_3d,
rotation_matrix_v1v2,
)
from bluemira.utilities.tools import flatten_iterable

if TYPE_CHECKING:
from bluemira.geometry.base import BluemiraGeo
Expand Down Expand Up @@ -552,9 +553,16 @@ def _populate_data(self, comp):

def _populate_plotters(comp):
if comp.is_leaf and getattr(comp, "shape", None) is not None:
options = (
self.options if comp.plot_options is None else comp.plot_options
)
if comp.plot_options.face_options["color"] in flatten_iterable(
BLUE_PALETTE.as_hex()
):
if self.options.face_options["color"] == "blue":
options = comp.plot_options
else:
# not possible with ComponentPlotter only plot_2d
options = self.options
else:
options = comp.plot_options
plotter = _get_plotter_class(comp.shape)(options)
plotter._populate_data(comp.shape)
self._cplotters.append(plotter)
Expand Down
8 changes: 8 additions & 0 deletions examples/geometry/plotting_tutorial.ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@
#
# Creates a `Component` and plots it in the xz plane using matplotlib defaults.
# Here we override some defaults and make our custom set of plot options.
# The custom setting of red loses out in priority to c3 comp being set specifically
# to green. In latter 2 plots shows that returns to component default settings when
# custom options are default or removed.

# %%
group = Component("Components")
Expand All @@ -412,6 +415,11 @@
c1 = PhysicalComponent("Comp1", face, parent=group)
c2 = PhysicalComponent("Comp2", wface, parent=group)
c3 = PhysicalComponent("Comp3", w1face, parent=group)
c3.plot_options.face_options["color"] = "green"
display.plot_2d(group, **my_group_options)
my_group_options["face_options"] = {"color": "blue"}
display.plot_2d(group, **my_group_options)
my_group_options["face_options"] = {}
display.plot_2d(group, **my_group_options)

# %% [markdown]
Expand Down
19 changes: 17 additions & 2 deletions tests/display/test_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from bluemira.base.components import Component, PhysicalComponent
from bluemira.base.constants import EPS
from bluemira.display import plot_3d, plotter
from bluemira.display import plot_2d, plot_3d, plotter
from bluemira.display.error import DisplayError
from bluemira.geometry import face, placement, tools
from bluemira.utilities.plot_tools import Plot3D
Expand Down Expand Up @@ -236,15 +236,30 @@ class TestComponentPlotter:
def setup_method(self):
wire1 = tools.make_polygon(SQUARE_POINTS, closed=True)
wire2 = tools.make_polygon(SQUARE_POINTS + 2.0, closed=True)
wire3 = tools.make_polygon(SQUARE_POINTS + 4.0, closed=True)
wire4 = tools.make_polygon(SQUARE_POINTS + 6.0, closed=True)
face1 = face.BluemiraFace(wire1)
face2 = face.BluemiraFace(wire2)

face3 = face.BluemiraFace(wire3)
face4 = face.BluemiraFace(wire4)
# default group
self.group = Component("Parent")
self.child1 = PhysicalComponent("Child1", shape=face1, parent=self.group)
self.child2 = PhysicalComponent("Child2", shape=face2, parent=self.group)
# make child2 have non-default colour on creation
self.child2.plot_options.face_options["color"] = "green"
# custom group
self.group2 = Component("Components")
self.group2_options = self.group2.plot_options.as_dict()
self.group2_options["wire_options"] = {}
self.group2_options["face_options"] = {"color": "red"}
self.child3 = PhysicalComponent("Child3", shape=face3, parent=self.group2)
self.child4 = PhysicalComponent("Child4", shape=face4, parent=self.group2)
self.child4.plot_options.face_options["color"] = "green"

def test_plotting_2d(self):
plotter.ComponentPlotter().plot_2d(self.group)
plot_2d(self.group2, **self.group2_options)

def test_plotting_2d_no_wires(self):
plotter.ComponentPlotter(show_wires=True).plot_2d(self.group)
Expand Down

0 comments on commit ab1878e

Please sign in to comment.