Skip to content

Commit

Permalink
Finishes LinkStyles
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelduchesne committed Jul 8, 2019
1 parent 4f1445d commit c5ed8b7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 35 deletions.
3 changes: 2 additions & 1 deletion pyTrnsysType/input_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ def __init__(self, version, simulation, tolerances=None, limits=None,
details.
Note:
Some Statements have not been implemented because only TRNSYS gods 😇
Some Statements have not been implemented because only TRNSYS
gods 😇
use them. Here is a list of Statements that have been ignored:
- The Convergence Promotion Statement (ACCELERATE)
Expand Down
124 changes: 92 additions & 32 deletions pyTrnsysType/trnsymodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ def invalidate_connections(self):
self.inputs[key].__dict__['_connected_to'] = None

def set_link_style(self, other, loc='best', color='#1f78b4',
path=None, **kwargs):
"""Set outgoing link styles. Adds a LinkStyle object to the origin
Model's studio attribute.
linestyle='-', linewidth=1,
path=None):
"""Set outgoing link styles between self and other.
Args:
other (TrnsysModel): The destination model.
Expand All @@ -371,25 +371,27 @@ def set_link_style(self, other, loc='best', color='#1f78b4',
:class:`TrnsysModel` and the destination :class:`TrnsysModel`.
color (str): color string. Can be a single color format string
(default='#1f78b4').
linestyle (str): Possible values: '-' or 'solid', '--' or 'dashed',
'-.' or 'dashdot', ':' or 'dotted', '-.' or 'dashdotdot'.
linewidth (float): The width of the line in points.
path (LineString or MultiLineString, optional): The path of the
link.
**kwargs:
"""
if self == other:
# trying to connect to itself.
raise NotImplementedError('This version does not support '
'connecting a TrnsysModel to itself')
if other is None:
raise ValueError('Other is None')
color_rgb = tuple([u * 255 for u in colorConverter.to_rgb(color)])
e = None
f = None
g = None
h = None
self.studio.link_styles.append(
LinkStyle(self, other, loc,
e, color_rgb, f, g, h, path)
)

style = LinkStyle(self, other, loc, path=path)

style.set_color(color)
style.set_linestyle(linestyle)
style.set_linewidth(linewidth)
u = self.unit_number
v = other.unit_number
self.studio.link_styles.update({(u, v): style})

def set_canvas_position(self, pt):
"""Set position of self in the canvas. Use cartesian coordinates: origin
Expand Down Expand Up @@ -1067,7 +1069,7 @@ def __init__(self, unit_name, model, position, layer=None):
"""
if layer is None:
layer = ["Main"]
self.link_styles = []
self.link_styles = {}
self.layer = layer
self.position = position
self.model = model
Expand All @@ -1084,12 +1086,24 @@ def from_trnsysmodel(cls, model):
return cls(model.unit_name, model.model, position, layer)


def _linestyle_to_studio(ls):
"""
Args:
ls:
"""
linestyle_dict = {'-': 0, 'solid': 0,
'--': 1, 'dashed': 1,
':': 2, 'dotted': 2,
'-.': 3, 'dashdot': 3,
'-..': 4, 'dashdotdot': 4}
_ls = linestyle_dict.get(ls)
return _ls


class LinkStyle(object):
def __init__(self, u, v, loc, e, rgb, f, g, h, path):
def __init__(self, u, v, loc, color='black', linestyle='-', linewidth=None,
path=None):
"""
{anchors}:{e}:{rgb_int}:{f}:{g}:{h}: 40:20:0:20:1:0:0:0:1:189,462:
432,462: 432,455: 459,455
Args:
u (TrnsysModel): from Model.
v (TrnsysModel): to Model.
Expand All @@ -1104,11 +1118,10 @@ def __init__(self, u, v, loc, e, rgb, f, g, h, path):
destination :class:`TrnsysModel` (other). The location can also
be a 2-tuple giving the coordinates of the origin
:class:`TrnsysModel` and the destination :class:`TrnsysModel`.
e:
rgb (tuple): The color of the line
f:
g:
h:
color (color): The color of the line.
linestyle (str): Possible values: '-' or 'solid', '--' or 'dashed',
'-.' or 'dashdot', ':' or 'dotted', '-.' or 'dashdotdot'.
linewidth (float): The link line width in points.
path (LineString or MultiLineString):
"""
if isinstance(loc, tuple):
Expand All @@ -1120,10 +1133,9 @@ def __init__(self, u, v, loc, e, rgb, f, g, h, path):
self.u = u
self.u_anchor_name, self.v_anchor_name = \
AnchorPoint(self.u).studio_anchor(self.v, (loc_u, loc_v))
self.rgb = rgb
self.f = f
self.g = g
self.h = h
self._color = color
self._linestyle = linestyle
self._linewidth = linewidth

if path is None:
u = AnchorPoint(self.u).anchor_points[self.u_anchor_name]
Expand All @@ -1134,19 +1146,67 @@ def __init__(self, u, v, loc, e, rgb, f, g, h, path):
self.path = path

def __repr__(self):
return self.to_deck()
return self._to_deck()

def set_color(self, color):
"""Set the color of the line.
Args:
color (color):
"""
self._color = color

def get_color(self):
"""Return the line color."""
return self._color

def set_linestyle(self, ls):
"""Set the linestyle of the line.
def to_deck(self):
Args:
ls (str): Possible values: '-' or 'solid', '--' or 'dashed', '-.' or
'dashdot', ':' or 'dotted', '-.' or 'dashdotdot'.
"""
if isinstance(ls, str):
self._linestyle = ls

def get_linestyle(self):
"""Return the linestyle.
See also :meth:`~pyTrnsysType.trnsymodel.LinkStyle.set_linestyle`.
"""
return self._linestyle

def set_linewidth(self, lw):
"""Set the line width in points.
Args:
lw (float): The line width in points.
"""
self._linewidth = lw

def get_linewidth(self):
"""Return the linewidth.
See also :meth:`~pyTrnsysType.trnsymodel.LinkStyle.set_linewidth`.
"""
return self._linewidth

def _to_deck(self):
"""0:20:40:20:1:0:0:0:1:513,441:471,441:471,430:447,430"""
anchors = ":".join([":".join(map(str, AnchorPoint(
self.u).studio_anchor_mapping[self.u_anchor_name])),
":".join(map(str, AnchorPoint(
self.u).studio_anchor_mapping[
self.v_anchor_name]))])
color = get_int_from_rgb(self.rgb)
self.v_anchor_name]))]) + ":"

color = str(get_int_from_rgb(tuple(
[u * 255 for u in colorConverter.to_rgb(self.get_color())]))) + ":"
path = ",".join([":".join(map(str, n.astype(int).tolist()))
for n in np.array(self.path)])
return anchors + ":1:" + str(color) + path
linestyle = str(_linestyle_to_studio(self.get_linestyle())) + ":"
linewidth = str(self.get_linewidth()) + ":"
return anchors + "1:" + color + linestyle + linewidth + "1:" + path


class AnchorPoint(object):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def test_set_link_style(self, fan_type):
fan2 = fan_type.copy()
fan2.set_canvas_position((100, 100))
fan_type.set_link_style(fan2, loc=('top-left', 'top-right'))
print(fan_type.studio.link_styles[0])
[print(stl) for stl in fan_type.studio.link_styles.values()]

with pytest.raises(ValueError):
# In case None is passed, should raise Value Error
Expand All @@ -315,7 +315,7 @@ def test_set_link_style_best(self, fan_type):
fan2 = fan_type.copy()
fan2.set_canvas_position((100, 50))
fan_type.set_link_style(fan2, loc='best')
print(fan_type.studio.link_styles[0])
[print(stl) for stl in fan_type.studio.link_styles.values()]

@pytest.mark.xfail()
def test_set_anchor_point(self, pipe_type):
Expand Down

0 comments on commit c5ed8b7

Please sign in to comment.