-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbox_resources.cs
More file actions
192 lines (160 loc) · 6.3 KB
/
toolbox_resources.cs
File metadata and controls
192 lines (160 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Shapes;
using Microsoft.Win32;
using YamlDotNet.Serialization;
using static SWAT__Toolbox.home_module;
namespace SWAT__Toolbox
{
class toolbox_functions
{
public static string[] split_string_by_space(string input_string)
{
string[] string_parts = input_string.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
return string_parts;
}
public static string[] read_from(string file_path)
{
string[] lines;
try
{
lines = File.ReadAllLines(file_path);
}
catch (Exception)
{
MessageBox.Show($@"Could not read the file,{System.IO.Path.GetFileName(file_path)}{Environment.NewLine}Make sure it exists.");
return new string[] { };
}
return lines;
}
public static string get_parameter_change_type_calibration(String change_type)
{
string par_change_typ;
switch (change_type)
// Channel, Landscape Unit, Hydrologic Response Unit, All Basin
{
case "Percent":
par_change_typ = "pctchg";
break;
case "Relative":
par_change_typ = "abschg";
break;
case "Replace":
par_change_typ = "absval";
break;
default:
par_change_typ = "pctchg";
break;
}
return par_change_typ;
}
public static ObservableCollection<recent_project> sort_recents(ObservableCollection<recent_project> recents, project project_obj)
{
foreach (var item in recents) {
if (item.project_name == null)
{
recents.Remove(item);
}
}
if (project_obj == null) { return recents; }
recent_project recent_instance = new recent_project
{
project_name = project_obj.project_name,
project_path = project_obj.project_path,
txtinout = project_obj.txtinout,
last_modified = project_obj.last_modified
};
// saving recent projects
using (StreamWriter streamWriter = new StreamWriter($@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\SWAT+ Toolbox\recents.dll"))
{
Serializer serializer = new Serializer();
serializer.Serialize(streamWriter, recents);
}
try // incase a different thread tries to modify source
{
recents.Remove(recents.Where(i => i.project_name == recent_instance.project_name).First());
recents.Remove(recents.Where(i => i.project_name == "").First());
}
catch (Exception)
{
}
try // incase a different thread tries to modify source
{
recents.Insert(0, recent_instance);
}
catch (Exception)
{
}
return recents;
}
public static bool save_project(project project_object, ObservableCollection<recent_project> recents)
{
// saving the project
using (StreamWriter streamWriter = new StreamWriter(project_object.project_path))
{
Serializer serializer = new Serializer();
serializer.Serialize(streamWriter, project_object);
}
//if not path - create
Directory.CreateDirectory($@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\SWAT+ Toolbox");
//sort recents
recents = sort_recents(recents, project_object);
// saving recent projects
using (StreamWriter streamWriter = new StreamWriter($@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\SWAT+ Toolbox\recents.dll"))
{
Serializer serializer = new Serializer();
serializer.Serialize(streamWriter, recents);
}
return true;
}
public static string pick_file(string filter_extension, string window_heading, string action_button)
{
string file_path = "";
Microsoft.Win32.OpenFileDialog open_dialog = new Microsoft.Win32.OpenFileDialog();
open_dialog.Filter = filter_extension;
open_dialog.Title = window_heading;
open_dialog.RestoreDirectory = true;
open_dialog.DefaultExt = ".csv";
Nullable<bool> dialog_result = open_dialog.ShowDialog();
if (dialog_result == true)
{
file_path = open_dialog.FileName;
}
return file_path;
}
public static string pick_folder(string window_heading, string action_button)
{
string folder_path = "";
FolderBrowserDialog folder_dialog = new FolderBrowserDialog();
folder_dialog.ShowNewFolderButton = false;
folder_dialog.Description = window_heading;
DialogResult result = folder_dialog.ShowDialog();
if (result == DialogResult.OK)
{
folder_path = folder_dialog.SelectedPath;
}
return folder_path;
}
public static string pick_save_path(string filter_extension, string window_heading, string action_button)
{
string file_path = "";
Microsoft.Win32.SaveFileDialog save_dialog = new Microsoft.Win32.SaveFileDialog();
save_dialog.Filter = filter_extension;
save_dialog.Title = window_heading;
save_dialog.RestoreDirectory = true;
save_dialog.DefaultExt = ".spt";
Nullable<bool> dialog_result = save_dialog.ShowDialog();
if (dialog_result == true)
{
file_path = save_dialog.FileName;
}
return file_path;
}
}
}