Skip to content

Commit

Permalink
feat(extra): Add a component for screen-oriented text
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey committed May 19, 2023
1 parent 4069191 commit 92b292a
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 8 deletions.
Binary file modified ladybug_grasshopper/icon/LB Legend 2D Parameters.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions ladybug_grasshopper/json/LB_Legend_2D_Parameters.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.6.0",
"version": "1.6.1",
"nickname": "Legend2D",
"outputs": [
[
Expand Down Expand Up @@ -53,5 +53,5 @@
"code": "\n\ntry:\n from ladybug.legend import Legend2DParameters\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug:\\n\\t{}'.format(e))\n\n\ndef parse_dim_text(dim_text):\n \"\"\"Parse text representing a dimension into an input for legend parameters.\"\"\"\n try:\n px_txt = int(dim_text)\n return '{}px'.format(px_txt)\n except ValueError:\n return dim_text\n\n\n# parse all of the inputs\norigin_x_ = parse_dim_text(origin_x_) if origin_x_ is not None else None\norigin_y_ = parse_dim_text(origin_y_) if origin_y_ is not None else None\nseg_height_ = parse_dim_text(seg_height_) if seg_height_ is not None else None\nseg_width_ = parse_dim_text(seg_width_) if seg_width_ is not None else None\ntext_height_ = parse_dim_text(text_height_) if text_height_ is not None else None\n\n# make the 2D legend parameters\nleg_par2d = Legend2DParameters(origin_x_, origin_y_, seg_height_, seg_width_, text_height_)\n",
"category": "Ladybug",
"name": "LB Legend 2D Parameters",
"description": "Use this component to change the colors, numerical range, and/or number of divisions\nof any Ladybug legend along with the corresponding colored mesh that the legend refers to.\n-\nAny Ladybug component that outputs a colored mesh and a legend will have an input\nthat can accept Legend Parameters from this component.\n-"
"description": "Customize the properties of a screen-oreinted 2D legend displaying with the\n\"LB Preview VisualizationSet\" component.\n-"
}
35 changes: 35 additions & 0 deletions ladybug_grasshopper/json/LB_Screen_Oriented_Text.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "1.6.0",
"nickname": "Text2D",
"outputs": [
[]
],
"inputs": [
{
"access": "list",
"name": "_text",
"description": "Text string to be displayed in the plane of the screen.",
"type": "string",
"default": null
},
{
"access": "list",
"name": "leg_par2d_",
"description": "Optional 2D LegendParameters from the \"LB Legend Parameters 2D\"\ncomponent, which will be used to customize a text in the plane\nof the screen. Note that only the text_height, origin_x and origin_y\ninputs of this component affect the placement of the text.",
"type": "System.Object",
"default": null
},
{
"access": "item",
"name": "_font_",
"description": "An optional text string to specify the font to be used for the text.\nExamples include \"Arial\", \"Times New Roman\", \"Courier\" (all without\nquotations). Default is \"Arial\".",
"type": "string",
"default": null
}
],
"subcategory": "4 :: Extra",
"code": "",
"category": "Ladybug",
"name": "LB Screen Oriented Text",
"description": "Generate screen-oriented text that displays in the Rhino scene as a head-up\ndisplay (HUD).\n_\nThis is useful when there are certain summary results or information that should\nalways be displayed on-screen.\n-"
}
9 changes: 3 additions & 6 deletions ladybug_grasshopper/src/LB Legend 2D Parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
# @license AGPL-3.0-or-later <https://spdx.org/licenses/AGPL-3.0-or-later>

"""
Use this component to change the colors, numerical range, and/or number of divisions
of any Ladybug legend along with the corresponding colored mesh that the legend refers to.
-
Any Ladybug component that outputs a colored mesh and a legend will have an input
that can accept Legend Parameters from this component.
Customize the properties of a screen-oreinted 2D legend displaying with the
"LB Preview VisualizationSet" component.
-
Args:
Expand Down Expand Up @@ -54,7 +51,7 @@

ghenv.Component.Name = 'LB Legend 2D Parameters'
ghenv.Component.NickName = 'Legend2D'
ghenv.Component.Message = '1.6.0'
ghenv.Component.Message = '1.6.1'
ghenv.Component.Category = 'Ladybug'
ghenv.Component.SubCategory = '4 :: Extra'
ghenv.Component.AdditionalHelpFromDocStrings = '0'
Expand Down
96 changes: 96 additions & 0 deletions ladybug_grasshopper/src/LB Screen Oriented Text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Ladybug: A Plugin for Environmental Analysis (GPL)
# This file is part of Ladybug.
#
# Copyright (c) 2023, Ladybug Tools.
# You should have received a copy of the GNU Affero General Public License
# along with Ladybug; If not, see <http://www.gnu.org/licenses/>.
#
# @license AGPL-3.0-or-later <https://spdx.org/licenses/AGPL-3.0-or-later>

"""
Generate screen-oriented text that displays in the Rhino scene as a head-up
display (HUD).
_
This is useful when there are certain summary results or information that should
always be displayed on-screen.
-
Args:
_text: Text string to be displayed in the plane of the screen.
leg_par2d_: Optional 2D LegendParameters from the "LB Legend Parameters 2D"
component, which will be used to customize a text in the plane
of the screen. Note that only the text_height, origin_x and origin_y
inputs of this component affect the placement of the text.
_font_: An optional text string to specify the font to be used for the text.
Examples include "Arial", "Times New Roman", "Courier" (all without
quotations). Default is "Arial".
"""

from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
import rhinoscriptsyntax as rs

class MyComponent(component):

def __init__(self):
super(MyComponent,self).__init__()
self.text_2d_args = None

def RunScript(self, _text, leg_par2d_, _font_):
ghenv.Component.Name = 'LB Screen Oriented Text'
ghenv.Component.NickName = 'Text2D'
ghenv.Component.Message = '1.6.0'
ghenv.Component.Category = 'Ladybug'
ghenv.Component.SubCategory = '4 :: Extra'
ghenv.Component.AdditionalHelpFromDocStrings = '0'

try:
from ladybug.legend import Legend, Legend2DParameters
except ImportError as e:
raise ImportError('\nFailed to import ladybug:\n\t{}'.format(e))

try:
from ladybug_rhino.color import black
from ladybug_rhino.grasshopper import all_required_inputs, longest_list
except ImportError as e:
raise ImportError('\nFailed to import ladybug_rhino:\n\t{}'.format(e))

import scriptcontext as sc
import Rhino.Geometry as rg

if all_required_inputs(ghenv.Component):
# get the screen dimensions
active_view = sc.doc.Views.ActiveView.ActiveViewport
v_size = active_view.Size
vw = v_size.Width
vh = v_size.Height
_font = 'Arial' if _font_ is None else _font_

# convert the inputs into arguments to be rendered
self.text_2d_args = []
for i, txt in enumerate(_text):
if len(leg_par2d_) == 0: # make default 2D parameters
l_par = Legend2DParameters()
else:
l_par = longest_list(leg_par2d_, i)
_height = Legend.parse_dim_2d(l_par.text_height, vh)
or_x = Legend.parse_dim_2d(l_par.origin_x, vw)
or_y = Legend.parse_dim_2d(l_par.origin_y, vh)
d_args = (
txt, black(), rg.Point2d(or_x,or_y), False, _height, _font)
self.text_2d_args.append(d_args)
else:
self.text_2d_args = None

def DrawViewportWires(self, args):
try:
if self.text_2d_args is not None:
# get the DisplayPipeline from the event arguments
display = args.Display
# render the 2D text from the arguments
for draw_args in self.text_2d_args:
display.Draw2dText(*draw_args)
except Exception, e:
System.Windows.Forms.MessageBox.Show(str(e), "script error")
Binary file modified ladybug_grasshopper/user_objects/LB Legend 2D Parameters.ghuser
Binary file not shown.
Binary file not shown.

0 comments on commit 92b292a

Please sign in to comment.