Skip to content

Commit f283f80

Browse files
Midas NX execute actions (#395)
2 parents 6ab0f55 + 34bab56 commit f283f80

File tree

5 files changed

+294
-124
lines changed

5 files changed

+294
-124
lines changed

MidasCivil_Adapter/AdapterActions/Execute.cs

Lines changed: 194 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
using BH.oM.Adapter.Commands;
3131
using BH.oM.Structure.Loads;
3232
using System.Runtime.InteropServices;
33+
using System.Threading.Tasks;
34+
using System.Net.Http;
3335

3436
namespace BH.Adapter.MidasCivil
3537
{
@@ -44,64 +46,110 @@ public override Output<List<object>, bool> Execute(IExecuteCommand command, Acti
4446
{
4547
var output = new Output<List<object>, bool>() { Item1 = null, Item2 = false };
4648

47-
output.Item2 = RunCommand(command as dynamic);
49+
output.Item2 = RunCommandWithTaskCompletionSource(command);
4850

4951
return output;
5052
}
5153

54+
/**************************************************/
55+
56+
private bool RunCommandWithTaskCompletionSource(IExecuteCommand command)
57+
{
58+
var tcs = new TaskCompletionSource<bool>();
59+
60+
Task.Run(async () =>
61+
{
62+
try
63+
{
64+
var result = await RunCommand(command as dynamic);
65+
tcs.SetResult(result);
66+
}
67+
catch (Exception ex)
68+
{
69+
tcs.SetException(ex);
70+
}
71+
});
72+
73+
return tcs.Task.GetAwaiter().GetResult();
74+
}
75+
5276
/***************************************************/
5377
/**** Commands ****/
5478
/***************************************************/
5579

56-
public bool RunCommand(NewModel command)
80+
public async Task<bool> RunCommand(NewModel command)
5781
{
58-
string newDirectory = GetDirectoryRoot(m_directory) + "\\Untitled";
59-
60-
bool directoryExists = Directory.Exists(newDirectory);
82+
if (m_midasCivilVersion == "9.5.0.nx")
83+
{
84+
string endpoint = "doc/NEW";
85+
string jsonPayload = "{\"Argument\": {}}";
6186

62-
int i = 1;
87+
await SendRequestAsync(endpoint, HttpMethod.Post, jsonPayload).ConfigureAwait(false);
88+
}
6389

64-
while (directoryExists)
90+
else
6591
{
66-
newDirectory = newDirectory + 1;
67-
directoryExists = Directory.Exists(newDirectory);
68-
i++;
69-
}
92+
string newDirectory = GetDirectoryRoot(m_directory) + "\\Untitled";
7093

71-
string unitExtension = "\\TextFiles\\" + "UNIT" + ".txt";
72-
string versionExtension = "\\TextFiles\\" + "VERSION" + ".txt";
73-
string unitFile = m_directory + unitExtension;
74-
string versionFile = m_directory + versionExtension;
94+
bool directoryExists = Directory.Exists(newDirectory);
7595

76-
Directory.CreateDirectory(newDirectory + "\\TextFiles");
96+
int i = 1;
7797

78-
if (!File.Exists(unitFile))
79-
File.Copy(unitFile, newDirectory + unitExtension);
80-
else
81-
File.AppendAllLines(newDirectory + unitExtension, new List<string>() { "*UNIT", "N,M,KJ,C" });
98+
while (directoryExists)
99+
{
100+
newDirectory = newDirectory + 1;
101+
directoryExists = Directory.Exists(newDirectory);
102+
i++;
103+
}
82104

83-
if (!File.Exists(versionFile))
84-
File.Copy(versionFile, newDirectory + versionExtension);
85-
else
86-
File.AppendAllLines(newDirectory + versionExtension, new List<string>() { "*VERSION", m_midasCivilVersion });
105+
string unitExtension = "\\TextFiles\\" + "UNIT" + ".txt";
106+
string versionExtension = "\\TextFiles\\" + "VERSION" + ".txt";
107+
string unitFile = m_directory + unitExtension;
108+
string versionFile = m_directory + versionExtension;
109+
110+
Directory.CreateDirectory(newDirectory + "\\TextFiles");
87111

88-
m_directory = newDirectory;
89-
Directory.CreateDirectory(newDirectory + "\\Results");
112+
if (!File.Exists(unitFile))
113+
File.Copy(unitFile, newDirectory + unitExtension);
114+
else
115+
File.AppendAllLines(newDirectory + unitExtension, new List<string>() { "*UNIT", "N,M,KJ,C" });
116+
117+
if (!File.Exists(versionFile))
118+
File.Copy(versionFile, newDirectory + versionExtension);
119+
else
120+
File.AppendAllLines(newDirectory + versionExtension, new List<string>() { "*VERSION", m_midasCivilVersion });
121+
122+
m_directory = newDirectory;
123+
Directory.CreateDirectory(newDirectory + "\\Results");
124+
}
90125

91126
return true;
127+
92128
}
93129

94130
/***************************************************/
95131

96-
public bool RunCommand(Save command)
132+
public async Task<bool> RunCommand(Save command)
97133
{
98-
Engine.Base.Compute.RecordWarning($"The command {command.GetType().Name} is not supported by this Adapter.");
99-
return false;
134+
if (m_midasCivilVersion == "9.5.0.nx")
135+
{
136+
string endpoint = "doc/SAVE";
137+
string jsonPayload = "{\"Argument\": {}}";
138+
139+
await SendRequestAsync(endpoint, HttpMethod.Post, jsonPayload).ConfigureAwait(false);
140+
return true;
141+
}
142+
143+
else
144+
{
145+
Engine.Base.Compute.RecordWarning($"The command {command.GetType().Name} is not supported by this Adapter version.");
146+
return false;
147+
}
100148
}
101149

102150
/***************************************************/
103151

104-
public bool RunCommand(SaveAs command)
152+
public async Task<bool> RunCommand(SaveAs command)
105153
{
106154
string fileName = command.FileName;
107155
string newDirectory = GetDirectoryRoot(m_directory) + "\\" + fileName;
@@ -112,24 +160,38 @@ public bool RunCommand(SaveAs command)
112160
return false;
113161
}
114162

115-
Directory.CreateDirectory(newDirectory);
116-
string[] mcbFiles = Directory.GetFiles(m_directory, "*.mcb");
117-
foreach (string mcbFile in mcbFiles)
118-
File.Copy(mcbFile, Path.Combine(newDirectory, fileName + ".mcb"));
119-
string[] mctFiles = Directory.GetFiles(m_directory, "*.mcb");
120-
foreach (string mctFile in mctFiles)
121-
File.Copy(mctFile, Path.Combine(newDirectory, fileName + ".mct"));
122-
CopyAll(new DirectoryInfo(m_directory + "\\TextFiles"), new DirectoryInfo(newDirectory + "\\TextFiles"));
123-
CopyAll(new DirectoryInfo(m_directory + "\\Results"), new DirectoryInfo(newDirectory + "\\Results"));
163+
if (m_midasCivilVersion == "9.5.0.nx")
164+
{
165+
string filePath = newDirectory.Replace("\\", "\\\\");
166+
167+
string endpoint = "doc/SAVEAS";
168+
string jsonPayload = "{\"Argument\": \"" + filePath + ".mcb\"}";
124169

125-
m_directory = newDirectory;
170+
await SendRequestAsync(endpoint, HttpMethod.Post, jsonPayload).ConfigureAwait(false);
171+
m_directory = newDirectory;
172+
}
173+
174+
else
175+
{
176+
Directory.CreateDirectory(newDirectory);
177+
string[] mcbFiles = Directory.GetFiles(m_directory, "*.mcb");
178+
foreach (string mcbFile in mcbFiles)
179+
File.Copy(mcbFile, Path.Combine(newDirectory, fileName + ".mcb"));
180+
string[] mctFiles = Directory.GetFiles(m_directory, "*.mcb");
181+
foreach (string mctFile in mctFiles)
182+
File.Copy(mctFile, Path.Combine(newDirectory, fileName + ".mct"));
183+
CopyAll(new DirectoryInfo(m_directory + "\\TextFiles"), new DirectoryInfo(newDirectory + "\\TextFiles"));
184+
CopyAll(new DirectoryInfo(m_directory + "\\Results"), new DirectoryInfo(newDirectory + "\\Results"));
185+
186+
m_directory = newDirectory;
187+
}
126188

127189
return true;
128190
}
129191

130192
/***************************************************/
131193

132-
public bool RunCommand(Open command)
194+
public async Task<bool> RunCommand(Open command)
133195
{
134196
string filePath = command.FileName;
135197

@@ -139,93 +201,121 @@ public bool RunCommand(Open command)
139201
}
140202
else
141203
{
142-
if (IsApplicationRunning())
204+
m_directory = Path.GetDirectoryName(filePath);
205+
206+
if (m_midasCivilVersion == "9.5.0.nx")
143207
{
144-
Engine.Base.Compute.RecordWarning("MidasCivil process already running");
208+
if (File.Exists(filePath))
209+
filePath = filePath.Replace("\\", "\\\\");
210+
else
211+
Engine.Base.Compute.RecordError("The given file path does not exist.");
212+
213+
string endpoint = "doc/OPEN";
214+
string jsonPayload = "{\"Argument\": \"" + filePath + "\"}";
215+
216+
await SendRequestAsync(endpoint, HttpMethod.Post, jsonPayload).ConfigureAwait(false);
145217
}
218+
146219
else
147220
{
148-
try
221+
if (IsApplicationRunning())
149222
{
150-
System.Diagnostics.Process.Start(filePath);
223+
Engine.Base.Compute.RecordWarning("MidasCivil process already running");
151224
}
152-
catch (System.ComponentModel.Win32Exception)
225+
else
153226
{
154-
throw new Exception("File does not exist, please reference an .mcb file");
227+
try
228+
{
229+
System.Diagnostics.Process.Start(filePath);
230+
}
231+
catch (System.ComponentModel.Win32Exception)
232+
{
233+
throw new Exception("File does not exist, please reference an .mcb file");
234+
}
155235
}
156-
}
157-
m_directory = Path.GetDirectoryName(filePath);
158-
string fileName = Path.GetFileNameWithoutExtension(filePath);
159-
string txtFile = m_directory + "\\" + fileName + ".txt";
160-
string mctFile = m_directory + "\\" + fileName + ".mct";
161236

162-
if (File.Exists(txtFile))
163-
{
164-
m_midasText = File.ReadAllLines(txtFile).ToList();
165-
SetSectionText();
166-
}
167-
else if (File.Exists(mctFile))
168-
{
169-
m_midasText = File.ReadAllLines(mctFile).ToList();
170-
SetSectionText();
171-
}
237+
string fileName = Path.GetFileNameWithoutExtension(filePath);
238+
string txtFile = m_directory + "\\" + fileName + ".txt";
239+
string mctFile = m_directory + "\\" + fileName + ".mct";
172240

173-
string versionFile = m_directory + "\\TextFiles\\" + "VERSION" + ".txt";
174-
if (!(m_midasCivilVersion == ""))
175-
{
176-
m_midasCivilVersion = m_midasCivilVersion.Trim();
177-
if (File.Exists(versionFile))
241+
if (File.Exists(txtFile))
242+
{
243+
m_midasText = File.ReadAllLines(txtFile).ToList();
244+
SetSectionText();
245+
}
246+
else if (File.Exists(mctFile))
178247
{
179-
File.Delete(versionFile);
180-
File.AppendAllLines(versionFile, new List<string>() { "*VERSION", m_midasCivilVersion });
181-
Engine.Base.Compute.RecordWarning("*VERSION file found, user input used to overide: version = " + m_midasCivilVersion);
248+
m_midasText = File.ReadAllLines(mctFile).ToList();
249+
SetSectionText();
182250
}
183251

184-
}
185-
else if (File.Exists(versionFile))
186-
{
187-
List<string> versionText = GetSectionText("VERSION");
188-
m_midasCivilVersion = versionText[0].Trim();
189-
}
190-
else
191-
{
192-
m_midasCivilVersion = "9.4.0";
193-
Engine.Base.Compute.RecordWarning("*VERSION file not found in directory and no version specified, MidasCivil version assumed default value = " + m_midasCivilVersion);
194-
}
252+
string versionFile = m_directory + "\\TextFiles\\" + "VERSION" + ".txt";
253+
if (!(m_midasCivilVersion == ""))
254+
{
255+
m_midasCivilVersion = m_midasCivilVersion.Trim();
256+
if (File.Exists(versionFile))
257+
{
258+
File.Delete(versionFile);
259+
File.AppendAllLines(versionFile, new List<string>() { "*VERSION", m_midasCivilVersion });
260+
Engine.Base.Compute.RecordWarning("*VERSION file found, user input used to overide: version = " + m_midasCivilVersion);
261+
}
195262

196-
try
197-
{
198-
List<string> units = GetSectionText("UNIT")[0].Split(',').ToList();
199-
m_forceUnit = units[0].Trim();
200-
m_lengthUnit = units[1].Trim();
201-
m_heatUnit = units[2].Trim();
202-
m_temperatureUnit = units[3].Trim();
203-
}
204-
catch (DirectoryNotFoundException)
205-
{
206-
Engine.Base.Compute.RecordWarning(
207-
"No UNIT.txt file found, MidasCivil model units assumed to be Newtons, metres, kilojoules and celcius. Therefore, no unit conversion will occur when pushing and pulling to/from MidasCivil.");
208-
}
209-
catch (ArgumentOutOfRangeException)
210-
{
211-
Engine.Base.Compute.RecordWarning(
212-
"No UNIT.txt file found, MidasCivil model units assumed to be Newtons, metres, kilojoules and celcius. Therefore, no unit conversion will occur when pushing and pulling to/from MidasCivil.");
213-
}
263+
}
264+
else if (File.Exists(versionFile))
265+
{
266+
List<string> versionText = GetSectionText("VERSION");
267+
m_midasCivilVersion = versionText[0].Trim();
268+
}
269+
else
270+
{
271+
m_midasCivilVersion = "9.4.0";
272+
Engine.Base.Compute.RecordWarning("*VERSION file not found in directory and no version specified, MidasCivil version assumed default value = " + m_midasCivilVersion);
273+
}
214274

215-
Directory.CreateDirectory(m_directory + "\\Results");
275+
try
276+
{
277+
List<string> units = GetSectionText("UNIT")[0].Split(',').ToList();
278+
m_forceUnit = units[0].Trim();
279+
m_lengthUnit = units[1].Trim();
280+
m_heatUnit = units[2].Trim();
281+
m_temperatureUnit = units[3].Trim();
282+
}
283+
catch (DirectoryNotFoundException)
284+
{
285+
Engine.Base.Compute.RecordWarning(
286+
"No UNIT.txt file found, MidasCivil model units assumed to be Newtons, metres, kilojoules and celcius. Therefore, no unit conversion will occur when pushing and pulling to/from MidasCivil.");
287+
}
288+
catch (ArgumentOutOfRangeException)
289+
{
290+
Engine.Base.Compute.RecordWarning(
291+
"No UNIT.txt file found, MidasCivil model units assumed to be Newtons, metres, kilojoules and celcius. Therefore, no unit conversion will occur when pushing and pulling to/from MidasCivil.");
292+
}
216293

294+
Directory.CreateDirectory(m_directory + "\\Results");
295+
}
217296
}
218297

219-
220298
return true;
221299
}
222300

223301
/***************************************************/
224302

225-
public bool RunCommand(Analyse command)
303+
public async Task<bool> RunCommand(Analyse command)
226304
{
227-
Engine.Base.Compute.RecordWarning($"The command {command.GetType().Name} is not supported by this Adapter.");
228-
return false;
305+
if (m_midasCivilVersion == "9.5.0.nx")
306+
{
307+
string endpoint = "doc/ANAL";
308+
string jsonPayload = "{}";
309+
310+
await SendRequestAsync(endpoint, HttpMethod.Post, jsonPayload).ConfigureAwait(false);
311+
return true;
312+
}
313+
314+
else
315+
{
316+
Engine.Base.Compute.RecordWarning($"The command {command.GetType().Name} is not supported by this Adapter version.");
317+
return false;
318+
}
229319
}
230320

231321
/***************************************************/

0 commit comments

Comments
 (0)