Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

Java Downloads | Oracle

...

1.2 Install Java

Installing Java.

...

These editors offer a convenient developing environment for users.

...

(This manual is written using Eclipse.)

1.3 Install additional module

JSON Module helps your work powerfully in Java.

...

⑦ 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.

...

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.

...

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.

...

Code Block
languagejava
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.

...

Code Block
languagejava
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.

...

Code Block
languagejava
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.

...

Code Block
languagejava
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.

...