11/*
22 * This file is part of the Buildings and Habitats object Model (BHoM)
3- * Copyright (c) 2015 - 2023 , the respective contributors. All rights reserved.
3+ * Copyright (c) 2015 - 2024 , the respective contributors. All rights reserved.
44 *
55 * Each contributor holds copyright over their respective contributions.
66 * The project versioning (Git) records all such contribution source information.
3434using System . IO ;
3535using System . Linq ;
3636using System . Text ;
37+ using BH . Engine . Base ;
38+ using System . Drawing ;
39+ using BH . Engine . Serialiser ;
40+ using BH . Engine . LadyBugTools ;
41+ using System . Reflection ;
3742
3843namespace BH . Adapter . LadybugTools
3944{
@@ -74,7 +79,6 @@ public List<object> IRunCommand(IExecuteCommand command)
7479
7580 private List < object > RunCommand ( GetMaterialCommand command )
7681 {
77- PythonEnvironment env = Engine . LadybugTools . Compute . InstallPythonEnv_LBT ( true ) ;
7882 LadybugConfig config = new LadybugConfig ( )
7983 {
8084 JsonFile = new FileSettings ( )
@@ -88,7 +92,7 @@ private List<object> RunCommand(GetMaterialCommand command)
8892 {
8993 string script = Path . Combine ( Engine . Python . Query . DirectoryCode ( ) , "LadybugTools_Toolkit\\ src\\ ladybugtools_toolkit\\ bhom\\ wrapped" , "get_material.py" ) ;
9094
91- string cmdCommand = $ "{ env . Executable } { script } -j \" { config . JsonFile . GetFullFileName ( ) } \" ";
95+ string cmdCommand = $ "{ m_environment . Executable } { script } -j \" { config . JsonFile . GetFullFileName ( ) } \" ";
9296
9397 Engine . Python . Compute . RunCommandStdout ( command : cmdCommand , hideWindows : true ) ;
9498 }
@@ -103,7 +107,6 @@ private List<object> RunCommand(GetMaterialCommand command)
103107
104108 private List < object > RunCommand ( GetTypologyCommand command )
105109 {
106- PythonEnvironment env = Engine . LadybugTools . Compute . InstallPythonEnv_LBT ( true ) ;
107110 LadybugConfig config = new LadybugConfig ( )
108111 {
109112 JsonFile = new FileSettings ( )
@@ -117,7 +120,7 @@ private List<object> RunCommand(GetTypologyCommand command)
117120 {
118121 string script = Path . Combine ( Engine . Python . Query . DirectoryCode ( ) , "LadybugTools_Toolkit\\ src\\ ladybugtools_toolkit\\ bhom\\ wrapped" , "get_typology.py" ) ;
119122
120- string cmdCommand = $ "{ env . Executable } { script } -j \" { config . JsonFile . GetFullFileName ( ) } \" ";
123+ string cmdCommand = $ "{ m_environment . Executable } { script } -j \" { config . JsonFile . GetFullFileName ( ) } \" ";
121124
122125 Engine . Python . Compute . RunCommandStdout ( command : cmdCommand , hideWindows : true ) ;
123126 }
@@ -133,15 +136,15 @@ private List<object> RunCommand(GetTypologyCommand command)
133136 private List < object > RunCommand ( RunSimulationCommand command )
134137 {
135138 // validation prior to passing to Python
136- if ( command . EpwFile == null )
139+ if ( command . EPWFile == null )
137140 {
138- BH . Engine . Base . Compute . RecordError ( $ "{ nameof ( command . EpwFile ) } input cannot be null.") ;
141+ BH . Engine . Base . Compute . RecordError ( $ "{ nameof ( command . EPWFile ) } input cannot be null.") ;
139142 return null ;
140143 }
141144
142- if ( ! File . Exists ( command . EpwFile . GetFullFileName ( ) ) )
145+ if ( ! File . Exists ( command . EPWFile . GetFullFileName ( ) ) )
143146 {
144- BH . Engine . Base . Compute . RecordError ( $ "{ command . EpwFile . GetFullFileName ( ) } does not exist.") ;
147+ BH . Engine . Base . Compute . RecordError ( $ "FIle ' { command . EPWFile . GetFullFileName ( ) } ' does not exist.") ;
145148 return null ;
146149 }
147150
@@ -170,21 +173,20 @@ private List<object> RunCommand(RunSimulationCommand command)
170173 // construct the base object and file to be passed to Python for simulation
171174 SimulationResult simulationResult = new SimulationResult ( )
172175 {
173- EpwFile = Path . GetFullPath ( command . EpwFile . GetFullFileName ( ) ) . Replace ( @"\" , "/" ) ,
176+ EpwFile = Path . GetFullPath ( command . EPWFile . GetFullFileName ( ) ) . Replace ( @"\" , "/" ) ,
174177 GroundMaterial = command . GroundMaterial ,
175178 ShadeMaterial = command . ShadeMaterial ,
176- Name = Engine . LadybugTools . Compute . SimulationID ( command . EpwFile . GetFullFileName ( ) , command . GroundMaterial , command . ShadeMaterial )
179+ Name = Engine . LadybugTools . Compute . SimulationID ( command . EPWFile . GetFullFileName ( ) , command . GroundMaterial , command . ShadeMaterial )
177180 } ;
178181
179182 // push object to json file
180183 Push ( new List < SimulationResult > ( ) { simulationResult } , actionConfig : config ) ;
181184
182- // locate the Python executable and file containing the simulation code
183- PythonEnvironment env = Engine . LadybugTools . Compute . InstallPythonEnv_LBT ( true ) ;
185+ // locate the Python file containing the simulation code
184186 string script = Path . Combine ( Engine . Python . Query . DirectoryCode ( ) , "LadybugTools_Toolkit\\ src\\ ladybugtools_toolkit\\ bhom\\ wrapped" , "simulation_result.py" ) ;
185187
186188 // run the simulation
187- string cmdCommand = $ "{ env . Executable } { script } -j \" { config . JsonFile . GetFullFileName ( ) } \" ";
189+ string cmdCommand = $ "{ m_environment . Executable } { script } -j \" { config . JsonFile . GetFullFileName ( ) } \" ";
188190 Engine . Python . Compute . RunCommandStdout ( command : cmdCommand , hideWindows : true ) ;
189191
190192 // reload from Python results
@@ -232,12 +234,11 @@ private List<object> RunCommand(RunExternalComfortCommand command)
232234 // push objects to json file
233235 Push ( new List < ExternalComfort > ( ) { externalComfort } , actionConfig : config ) ;
234236
235- // locate the Python executable and file containing the simulation code
236- PythonEnvironment env = Engine . LadybugTools . Compute . InstallPythonEnv_LBT ( true ) ;
237+ // locate the Python file containing the simulation code
237238 string script = Path . Combine ( Engine . Python . Query . DirectoryCode ( ) , "LadybugTools_Toolkit\\ src\\ ladybugtools_toolkit\\ bhom\\ wrapped" , "external_comfort.py" ) ;
238239
239240 // run the calculation
240- string cmdCommand = $ "{ env . Executable } { script } -j \" { config . JsonFile . GetFullFileName ( ) } \" ";
241+ string cmdCommand = $ "{ m_environment . Executable } { script } -j \" { config . JsonFile . GetFullFileName ( ) } \" ";
241242 Engine . Python . Compute . RunCommandStdout ( command : cmdCommand , hideWindows : true ) ;
242243
243244 // reload from Python results
@@ -250,6 +251,157 @@ private List<object> RunCommand(RunExternalComfortCommand command)
250251 return externalComfortPopulated ;
251252 }
252253
254+ /**************************************************/
255+
256+ private List < object > RunCommand ( HeatPlotCommand command )
257+ {
258+ if ( command . EPWFile == null )
259+ {
260+ BH . Engine . Base . Compute . RecordError ( $ "{ nameof ( command . EPWFile ) } input cannot be null.") ;
261+ return null ;
262+ }
263+
264+ if ( ! System . IO . File . Exists ( command . EPWFile . GetFullFileName ( ) ) )
265+ {
266+ BH . Engine . Base . Compute . RecordError ( $ "File '{ command . EPWFile } ' does not exist.") ;
267+ return null ;
268+ }
269+
270+ string epwFile = System . IO . Path . GetFullPath ( command . EPWFile . GetFullFileName ( ) ) ;
271+
272+ string script = Path . Combine ( Engine . Python . Query . DirectoryCode ( ) , "LadybugTools_Toolkit\\ src\\ ladybugtools_toolkit\\ bhom\\ wrapped\\ plot" , "heatmap.py" ) ;
273+
274+ //check if the colourmap is valid for user warning, but run with input anyway as the map could be defined separately.
275+ string colourMap = command . ColourMap ;
276+ if ( colourMap . ColourMapValidity ( ) )
277+ colourMap = colourMap . ToColourMap ( ) . FromColourMap ( ) ;
278+
279+ // run the process
280+ string cmdCommand = $ "{ m_environment . Executable } { script } -e \" { epwFile } \" -dtk \" { command . EPWKey . ToText ( ) } \" -cmap \" { colourMap } \" -p \" { command . OutputLocation } \" ";
281+ string result = Engine . Python . Compute . RunCommandStdout ( command : cmdCommand , hideWindows : true ) ;
282+
283+ m_executeSuccess = true ;
284+ return new List < object > ( ) { result } ;
285+ }
286+
287+ /**************************************************/
288+
289+ private List < object > RunCommand ( WindroseCommand command )
290+ {
291+ if ( command . EPWFile == null )
292+ {
293+ BH . Engine . Base . Compute . RecordError ( $ "{ nameof ( command . EPWFile ) } input cannot be null.") ;
294+ return null ;
295+ }
296+
297+ if ( ! System . IO . File . Exists ( command . EPWFile . GetFullFileName ( ) ) )
298+ {
299+ BH . Engine . Base . Compute . RecordError ( $ "File '{ command . EPWFile . GetFullFileName ( ) } ' does not exist.") ;
300+ return null ;
301+ }
302+
303+ if ( command . AnalysisPeriod == null )
304+ {
305+ BH . Engine . Base . Compute . RecordError ( $ "Please input a valid analysis period to run this command.") ;
306+ return null ;
307+ }
308+
309+ string epwFile = System . IO . Path . GetFullPath ( command . EPWFile . GetFullFileName ( ) ) ;
310+
311+ string script = Path . Combine ( Engine . Python . Query . DirectoryCode ( ) , "LadybugTools_Toolkit\\ src\\ ladybugtools_toolkit\\ bhom\\ wrapped\\ plot" , "windrose.py" ) ;
312+
313+ //check if the colourmap is valid for user warning, but run with input anyway as the map could be defined separately.
314+ string colourMap = command . ColourMap ;
315+ if ( colourMap . ColourMapValidity ( ) )
316+ colourMap = colourMap . ToColourMap ( ) . FromColourMap ( ) ;
317+
318+ // run the process
319+ string cmdCommand = $ "{ m_environment . Executable } { script } -e \" { epwFile } \" -ap \" { command . AnalysisPeriod . FromBHoM ( ) . Replace ( "\" " , "\\ \" " ) } \" -cmap \" { colourMap } \" -bins \" { command . NumberOfDirectionBins } \" -p \" { command . OutputLocation } \" ";
320+ string result = Engine . Python . Compute . RunCommandStdout ( command : cmdCommand , hideWindows : true ) ;
321+
322+ m_executeSuccess = true ;
323+ return new List < object > { result } ;
324+ }
325+
326+ /**************************************************/
327+
328+ private List < object > RunCommand ( UTCIHeatPlotCommand command )
329+ {
330+ if ( command . EPWFile == null )
331+ {
332+ BH . Engine . Base . Compute . RecordError ( $ "{ nameof ( command . EPWFile ) } input cannot be null.") ;
333+ return null ;
334+ }
335+
336+ if ( ! System . IO . File . Exists ( command . EPWFile . GetFullFileName ( ) ) )
337+ {
338+ BH . Engine . Base . Compute . RecordError ( $ "File '{ command . EPWFile . GetFullFileName ( ) } ' does not exist.") ;
339+ return null ;
340+ }
341+
342+ if ( command . GroundMaterial == null )
343+ {
344+ BH . Engine . Base . Compute . RecordError ( $ "Please input a valid ground material to run this command.") ;
345+ return null ;
346+ }
347+
348+ if ( command . ShadeMaterial == null )
349+ {
350+ BH . Engine . Base . Compute . RecordError ( $ "Please input a valid shade material to run this command.") ;
351+ return null ;
352+ }
353+
354+ if ( command . Typology == null )
355+ {
356+ BH . Engine . Base . Compute . RecordError ( $ "Please input a valid Typology to run this command.") ;
357+ }
358+
359+ if ( ! ( command . BinColours . Count == 10 || command . BinColours . Count == 0 ) )
360+ {
361+ BH . Engine . Base . Compute . RecordError ( $ "When overriding bin colours 10 colours must be provided, but { command . BinColours . Count } colours were provided instead.") ;
362+ return null ;
363+ }
364+ List < string > colours = command . BinColours . Select ( x => x . ToHexCode ( ) ) . ToList ( ) ;
365+
366+ string hexColours = $ "[\" { string . Join ( "\" ,\" " , colours ) } \" ]";
367+ if ( hexColours == "[\" \" ]" )
368+ hexColours = "[]" ;
369+
370+ Dictionary < string , string > inputObjects = new Dictionary < string , string > ( )
371+ {
372+ { "ground_material" , command . GroundMaterial . FromBHoM ( ) } ,
373+ { "shade_material" , command . ShadeMaterial . FromBHoM ( ) } ,
374+ { "typology" , command . Typology . FromBHoM ( ) } ,
375+ { "bin_colours" , hexColours }
376+ } ;
377+
378+ string argFile = Path . GetTempFileName ( ) ;
379+ File . WriteAllText ( argFile , inputObjects . ToJson ( ) ) ;
380+
381+ string epwFile = System . IO . Path . GetFullPath ( command . EPWFile . GetFullFileName ( ) ) ;
382+
383+ string script = Path . Combine ( Engine . Python . Query . DirectoryCode ( ) , "LadybugTools_Toolkit\\ src\\ ladybugtools_toolkit\\ bhom\\ wrapped\\ plot" , "utci_heatmap.py" ) ;
384+
385+ // run the process
386+ string cmdCommand = $ "{ m_environment . Executable } \" { script } \" -e \" { epwFile } \" -in \" { argFile } \" -ws \" { command . WindSpeedMultiplier } \" -sp \" { command . OutputLocation } \" ";
387+ string result = "" ;
388+
389+ try
390+ {
391+ result = Engine . Python . Compute . RunCommandStdout ( command : cmdCommand , hideWindows : true ) ;
392+ }
393+ catch ( Exception ex )
394+ {
395+ BH . Engine . Base . Compute . RecordError ( ex , "An error occurred while running some python." ) ;
396+ }
397+ finally
398+ {
399+ File . Delete ( argFile ) ;
400+ }
401+
402+ return new List < object > { result . Split ( '\n ' ) . Last ( ) } ;
403+ }
404+
253405 /**************************************************/
254406 /* Private methods - Fallback */
255407 /**************************************************/
@@ -261,3 +413,4 @@ private List<object> RunCommand(IExecuteCommand command)
261413 }
262414 }
263415}
416+
0 commit comments