This document is a MIDAS Civil API operating example using Java.
1. Setting Java Development Environment
1.1 Download file
Download an installing file from the Java official website.
1.2 Install Java
Installing Java.
These editors offer a convenient developing environment for users.
👷‍♂️ Recommended Code Editors for Java
(This manual is written using Eclipse.)
1.3 Install additional module
JSON Module helps your work powerfully in Java.
Setting the JSON Module in the Eclipse as below.
â‘ Download JSON-Java through the under link.
GitHub - stleary/JSON-java: A reference implementation of a JSON package in Java.
② Run the Eclipse, select the file path, and click the Launch button.
③ Create a new Java Project.
â‘Ł In Package Explorer -> Buildpath/Configure Build Path
⑤ Open the previously downloaded JSON-Jave file as below.
â‘Ą Create Example Class for generating examples.
⑦ You can find the JSON-Java is registered as below.
2. Operating MIDAS/Civil API Using Java
Let’s look over with simple examples.
2.1 Operating MIDAS/Civil API
Civil can execute to communicate with API as below.
Java Code
public class Example { public static void main(String[] args) throws InterruptedException { String mCivil = "C:\\CivilBuild-API\\CVLw.exe /API"; Runtime rt = Runtime.getRuntime(); Process p; try { p = rt.exec(mCivil); p.getErrorStream().close(); Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); } } }
MIDAS/Civil execute file path needs to be edited depending on the user.
2.2 Open MIDAS/Civil API Document
Open the existed file after running MIDAS/Civil.
Java Code
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject; public class Example { public static void main(String[] args) throws InterruptedException { String mCivil = "C:\\CivilBuild-API\\CVLw.exe /API"; Runtime rt = Runtime.getRuntime(); Process p; try { p = rt.exec(mCivil); p.getErrorStream().close(); Thread.sleep(10000); String rm = "POST"; String dcOpen = "/doc/open"; JSONObject jOpen = new JSONObject(); jOpen.put("Argument", "C:\\FSM 1Cells Box.mcb"); restAPI(rm,dcOpen,jOpen); } catch (Exception e) { e.printStackTrace(); } } public static void restAPI(String rm, String ct, JSONObject jobj) { try { URL url = new URL("http://127.0.0.1:10024"+ct); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod(rm); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoInput(true); conn.setDoOutput(true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); bw.write(jobj.toString()); bw.flush(); bw.close(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } System.out.print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } }
MIDAS/Civil execute file path needs to be edited depending on the user.
You can find the URL address in the APIServer.exe, which operates MIDAS/Civil through API.
2.3 MIDAS/Civil API- Run the Analysis and Close program
Now, run the analysis with opened MIDAS/Civil file and close the Civil.
Java Code
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject; public class Example { public static void main(String[] args) throws InterruptedException { String mCivil = "C:\\CivilBuild-API\\CVLw.exe /API"; Runtime rt = Runtime.getRuntime(); Process p; try { p = rt.exec(mCivil); p.getErrorStream().close(); Thread.sleep(10000); String post = "POST"; String dcOpen = "/doc/open"; JSONObject jOpen = new JSONObject(); jOpen.put("Argument", "C:\\FSM 1Cells Box.mcb"); restAPI(post,dcOpen,jOpen); String dcAnal = "/doc/anal"; JSONObject jDump = new JSONObject(); restAPI(post,dcAnal,jDump); String dcExit = "/doc/exit"; restAPI(post,dcExit,jDump); } catch (Exception e) { e.printStackTrace(); } } public static void restAPI(String rm, String ct, JSONObject jobj) { try { URL url = new URL("http://127.0.0.1:10024"+ct); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod(rm); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoInput(true); conn.setDoOutput(true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); bw.write(jobj.toString()); bw.flush(); bw.close(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } System.out.print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } }
3. Command for operating
3.1 DOC Command
DOC command controls documents such as create/open/close files.
Open document: “/doc/open”
Create a new document: “/doc/new”
Close document: “/doc/close”
Run analysis: “/doc/anal”
Save document: “/doc/save”
Save as: “/doc/saveas”
Import JSON data: “/doc/import”
Export JSON data: “/doc/export”
Close program: “doc/exit”
Java Code(Example)
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject; public class docCommand { public static void main(String[] args) { String post = "POST"; //DOC OPEN String dcOpen = "/doc/open"; JSONObject jOpen = new JSONObject(); jOpen.put("Argument", "C:\\FSM 1Cells Box.mcb"); restAPI(post,dcOpen,jOpen); //DOC ANAL String dcAnal = "/doc/anal"; JSONObject jDump = new JSONObject(); restAPI(post,dcAnal,jDump); //DOC EXPORT String dcExpt = "/doc/export"; JSONObject jExpt = new JSONObject(); jExpt.put("Argument", "C:\\FSM 1Cells Box.json"); restAPI(post,dcExpt,jExpt); //DOC CLOSE String dcClse = "/doc/close"; restAPI(post,dcClse,jDump); //DOC NEW String dcNew = "/doc/new"; restAPI(post,dcNew,jDump); //DOC SAVEAS String dcSvas = "/doc/saveas"; JSONObject jSvas = new JSONObject(); jSvas.put("Argument", "C:\\FSM 1Cells Box2.mcb"); restAPI(post,dcSvas,jSvas); //DOC IMPORT String dcImpt = "/doc/import"; JSONObject jImpt = new JSONObject(); jImpt.put("Argument", "C:\\FSM 1Cells Box.json"); restAPI(post,dcImpt,jImpt); //DOC SAVE String dcSave = "/doc/save"; restAPI(post,dcSave,jDump); //DOC EXIT String dcExit = "/doc/exit"; restAPI(post,dcExit,jDump); } public static void restAPI(String rm, String ct, JSONObject jobj) { try { URL url = new URL("http://127.0.0.1:10024"+ct); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod(rm); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoInput(true); conn.setDoOutput(true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); bw.write(jobj.toString()); bw.flush(); bw.close(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } System.out.print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } }
3.2 DB Command
DB Command requests the data name. Each Data name is embodied by GET/POST/PUT/DELETE methods.
GET DB Method: inquiry opened file data.
Example1: This example code calls all Nodes information from the opened file and prints specific Node information.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject; public class GetDB { public static void main(String[] args) { try { URL url = new URL("http://127.0.0.1:10024/db/node"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoOutput(true); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } JSONObject obj = new JSONObject(sb.toString()); JSONObject node = obj.getJSONObject("NODE").getJSONObject("1003"); System.out.print(node.toString()); } catch (Exception e) { e.printStackTrace(); } } }
Example2: The following code calls only specific Node information from the opened file.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject; public class GetDB2 { public static void main(String[] args) { try { URL url = new URL("http://127.0.0.1:10024/db/node/1003"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoOutput(true); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } JSONObject obj = new JSONObject(sb.toString()); System.out.print(obj.toString()); } catch (Exception e) { e.printStackTrace(); } } }
Post DB Method: Add data to the opened file. If the key value exists, then it will get an error.
The following example adds a specific Node to the opened file and prints that Node information.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject; public class PostDB { public static void main(String[] args) { try { URL url = new URL("http://127.0.0.1:10024/db/node"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoInput(true); conn.setDoOutput(true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); JSONObject jCoord = new JSONObject(); jCoord.put("X", 0); jCoord.put("Y", 20.0); jCoord.put("Z", 0); JSONObject jNode = new JSONObject(); jNode.put("999", jCoord); JSONObject jObj = new JSONObject(); jObj.put("Assign", jNode); bw.write(jObj.toString()); bw.flush(); bw.close(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } System.out.print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } }
Put DB Method: Change data to the opened file. If you request the key value that does not exist, it will get an error.
The following example changes the coordinates of a specific node.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject; public class PutDB { public static void main(String[] args) { try { URL url = new URL("http://127.0.0.1:10024/db/node"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("PUT"); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoInput(true); conn.setDoOutput(true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); JSONObject jCoord = new JSONObject(); jCoord.put("X", 0); jCoord.put("Y", 20.0); jCoord.put("Z", 0); JSONObject jNode = new JSONObject(); jNode.put("1003", jCoord); JSONObject jObj = new JSONObject(); jObj.put("Assign", jNode); bw.write(jObj.toString()); bw.flush(); bw.close(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } System.out.print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } }
Delete DB Method: Delete the opened file data. If you request the key value that does not exist, it will get an error.
Â
Example1: Delete the specific Node, and print that deleted Node information.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class DeleteDB { public static void main(String[] args) { try { URL url = new URL("http://127.0.0.1:10024/db/node/1003"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("DELETE"); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoOutput(true); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } System.out.print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } }
Example2: Delete all Nodes.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class DeleteDB2 { public static void main(String[] args) { try { URL url = new URL("http://127.0.0.1:10024/db/node"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("DELETE"); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoOutput(true); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } System.out.print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } }
3.3 POST Command
Post Command is post-process operators after analysis.
Â
Call result table: “/post/table”
Example1: This example calls the analyzed results from the opened document and saves them in JSON format.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONArray; import org.json.JSONObject; public class PostTable { public static void main(String[] args) { try { URL url = new URL("http://127.0.0.1:10024/post/table"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoInput(true); conn.setDoOutput(true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); JSONArray parts = new JSONArray(); parts.put("Part I"); parts.put("Part J"); JSONArray Ldcase = new JSONArray(); Ldcase.put("ULS_SET B(CB:max)"); Ldcase.put("ULS_SET B(CB:min)"); Ldcase.put("ULS_EQ(CB:max)"); Ldcase.put("ULS_EQ(CB:min)"); JSONObject Unit = new JSONObject(); Unit.put("FORCE", "KN"); Unit.put("DIST", "M"); Unit.put("HEAT", "CAL"); Unit.put("TEMP", "C"); JSONObject NdEl = new JSONObject(); NdEl.put("TO", "1003 to 1013"); JSONObject jKey = new JSONObject(); jKey.put("EXPORT_PATH", "C:\\Users\\yjw0608\\Desktop\\FSM 1-Cell Box\\ResultTable.json"); jKey.put("TABLE_TYPE", "beam force"); jKey.put("UNIT", Unit); jKey.put("NODE_ELEMS", NdEl); jKey.put("LOAD_CASE_NAMES", Ldcase); jKey.put("PARTS", parts); JSONObject jObj = new JSONObject(); jObj.put("Argument", jKey); bw.write(jObj.toString()); bw.flush(); bw.close(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } System.out.print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } }
Example2: Call the analyzed results from the opened file, and print the previous results.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONArray; import org.json.JSONObject; public class PostTable2 { public static void main(String[] args) { try { URL url = new URL("http://127.0.0.1:10024/post/table"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoInput(true); conn.setDoOutput(true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); JSONArray parts = new JSONArray(); parts.put("Part I"); parts.put("Part J"); JSONArray Ldcase = new JSONArray(); Ldcase.put("ULS_SET B(CB:max)"); Ldcase.put("ULS_SET B(CB:min)"); Ldcase.put("ULS_EQ(CB:max)"); Ldcase.put("ULS_EQ(CB:min)"); JSONObject Unit = new JSONObject(); Unit.put("FORCE", "KN"); Unit.put("DIST", "M"); Unit.put("HEAT", "CAL"); Unit.put("TEMP", "C"); JSONObject NdEl = new JSONObject(); NdEl.put("TO", "1003 to 1013"); JSONObject jKey = new JSONObject(); jKey.put("EXPORT_PATH", "C:\\Users\\yjw0608\\Desktop\\FSM 1-Cell Box\\ResultTable.json"); jKey.put("TABLE_TYPE", "beam force"); jKey.put("UNIT", Unit); jKey.put("NODE_ELEMS", NdEl); jKey.put("LOAD_CASE_NAMES", Ldcase); jKey.put("PARTS", parts); JSONObject jObj = new JSONObject(); jObj.put("Argument", jKey); bw.write(jObj.toString()); bw.flush(); bw.close(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } JSONObject obj = new JSONObject(sb.toString()); JSONArray data1 = obj.getJSONObject("empty").getJSONArray("DATA").getJSONArray(0); System.out.print(data1.toString()); } catch (Exception e) { e.printStackTrace(); } } }
3.4 VIEW Command
View Command takes images in Post-process.
Â
Diagram call: “/view/capture”
This example shows how to call the Diagram and save it where you want to.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONArray; import org.json.JSONObject; public class PostView { public static void main(String[] args) { try { URL url = new URL("http://127.0.0.1:10024/view/capture"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoInput(true); conn.setDoOutput(true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); JSONObject DispOp = new JSONObject(); DispOp.put("PRINT_VALUE", false); DispOp.put("VALUE_DECIMAL_PT", 3); DispOp.put("PRINT_LEGEND", true); DispOp.put("LEGEND_DECIMAL_PT", 3); JSONObject jKey = new JSONObject(); jKey.put("IS_PRE_MODE", false); jKey.put("VIEW_HORIZONTAL", 45); jKey.put("VIEW_VERTICAL", 30); jKey.put("POST_DISP_OPT", DispOp); jKey.put("OUTPUT_PATH", "C:\\ULS_SET B(Shear-Z).jpg"); jKey.put("LOADCASE_MINMAX", "ALL"); jKey.put("LOADCASE_TYPE", "CB"); jKey.put("LOADCASE_NAME", "ULS_SET B"); jKey.put("CURRENTMODE", 6); jKey.put("RESULT_COMP", 2); JSONArray ufig = new JSONArray(); ufig.put(jKey); JSONObject list = new JSONObject(); list.put("UFIG_LIST", ufig); JSONObject jObj = new JSONObject(); jObj.put("Argument",list); bw.write(jObj.toString()); bw.flush(); bw.close(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while((line=br.readLine()) !=null) { sb.append(line); } System.out.print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } }