Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

This document is a manual for executing the MIDAS Civil API with Python.

1. Setting a Python Development Environment

1.1 Python Install download

Download an installing file from the Python official website.

Welcome to Python.org

* You can find various OS installers besides the Window.

1.2 Install Python

Install Python.

These editors offer a convenient developing environment for users.

👷‍♂️ Recommended Code Editors for Python

(This manual is written using Visual Studio Code.)

1.3 Install additional module

You can control MIDAS/Civil API only when installing the Request Module in Python.

The Request Module can be installed as follows.

When you cannot see the under a terminal window, use Menu-Terminal-New Terminal, then run the Terminal window. Copy and Paste under the text, and press the Enter key.

py -3 -m pip install requests

2. Operating MIDAS/Civil API using Python

Let’s see how MIDAS/Civil API works with simple examples.

2.1 Operating MIDAS/Civil API

To communicate with the API, Civil is executable as follows.

  • Python Code

import subprocess
import time

p = subprocess.Popen([r"C:\\CivilBuild-API\\CVLw.exe", "/API"])
## wait until civil api process ready
time.sleep(10)

The user can change the MIDAS/Civil file path depending on their work environment.

2.2 Open MIDAS/Civil API documnent

We are opening a saved file after executing MIDAS/Civil API.

  • Python Code

import requests
CIVIL_API_URL = "http://127.0.0.1:10024"

## DOC.OPEN
requests.post(CIVIL_API_URL + "/doc/open", json={"Argument":"C:\\FSM 1Cells Box.mcb"})

The user can change the MIDAS/Civil file path depending on their work environment.

The user can find an URL in the APIServer.exe when executing 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.

  • Python Code

## DOC.ANAL
requests.post(CIVIL_API_URL + "/doc/anal", json={})

## DOC.EXIT
requests.post(CIVIL_API_URL + "/doc/exit", json={})

3. Command for operating

3.1 DOC Command

DOC command controls documents such as create/open/close files

  • Open file: “/doc/open”

import requests
#String = MCB file path (ex : "C:\\Python\\Example.mcb")
requests.post("http://127.0.0.1:10024/doc/open", json={"Argument":String})

  • Open New: “/doc/new”

import requests

requests.post("http://127.0.0.1:10024/doc/new", json={})

  • Close file: “/doc/close”

import requests

requests.post("http://127.0.0.1:10024/doc/close", json={})

  • Run analysis: “/doc/anal”

import requests

requests.post("http://127.0.0.1:10024/doc/anal", json={})

  • Save file: “/doc/save”

import requests

requests.post("http://127.0.0.1:10024/doc/save", json={})

  • Save as: “/doc/saveas”

import requests
#String = MCB file path (ex : "C:\\Python\\Example.mcb")
requests.post("http://127.0.0.1:10024/doc/saveas", json={"Argument":String})

  • Json data(file) Import : “/doc/import”

import requests
#String = MCB file path (ex : "C:\\Json\\Example.json")
requests.post("http://127.0.0.1:10024/doc/import", json={"Argument":String})

  • Json data(file) Export : “/doc/export”

import requests
#String = MCB file path (ex : "C:\\Json\\Example.json")
requests.post("http://127.0.0.1:10024/doc/export", json={"Argument":String})

3.2 DB Command

DB Command requests the data name. Each Data name is embodied 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 requests
import json

CIVIL_API_URL = "http://127.0.0.1:10024"

## Get db/node
get_data=requests.get(CIVIL_API_URL + "/db/node").text
get_data=json.loads(get_data)

print(get_data['NODE']['1003'])

Example2: The following code calls only specific Node information from the opened file.

import requests

CIVIL_API_URL = "http://127.0.0.1:10024"

## Get db/node
get_data=requests.get(CIVIL_API_URL + "/db/node/1003").text

print(get_data)

  • 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 requests
import json

CIVIL_API_URL = "http://127.0.0.1:10024"

## Post db/node
requests.post(CIVIL_API_URL + "/db/node",json={"Assign":{"999":{"X":0,"Y":10.0,"Z":0}}})

## Get db/node
get_data=requests.get(CIVIL_API_URL + "/db/node").text
get_data=json.loads(get_data)

print(get_data['NODE']['999'])

  • 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 requests

CIVIL_API_URL = "http://127.0.0.1:10024"

## Get db/node
get_data=requests.get(CIVIL_API_URL + "/db/node/1003").text
print(get_data)

## Put db/node
requests.put(CIVIL_API_URL + "/db/node",json={"Assign":{"1003":{"X":0,"Y":20.0,"Z":0}}})

## Get db/node
get_data=requests.get(CIVIL_API_URL + "/db/node/1003").text
print(get_data)

  • 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 requests

CIVIL_API_URL = "http://127.0.0.1:10024"

## Delete db/node
Delete_Data=requests.delete(CIVIL_API_URL + "/db/node/999").text

print(Delete_Data)

Example2: Delete all Nodes.

import requests

CIVIL_API_URL = "http://127.0.0.1:10024"

## Delete db/node
requests.delete(CIVIL_API_URL + "/db/node")

3.3 POST Command

Post Command is post-process operators after analysis.

  • Call result table: “/doc/table”

When you call the results table, you can use this data in both ways - saved/ w/o saved file. The first example shows when you save the file, and the second example shows without the saved file.

Example 1: This example calls the analyzed results from the opened document and saves them in JSON format. (The yellow box indicated saved file path.)

import requests

CIVIL_API_URL = "http://127.0.0.1:10024"

## POST /POST/TABLE
requests.post(CIVIL_API_URL + "/post/table",json={
    "Argument": {
        "EXPORT_PATH": "C:\\Users\\yjw0608\\Desktop\\FSM 1-Cell Box\\ResultTable.json",
        "TABLE_TYPE": "beam force",
        "UNIT": {
            "FORCE": "KN",
            "DIST": "M",
            "HEAT": "CAL",
            "TEMP": "C"
        },
        "NODE_ELEMS": {
            "TO": "1003 to 1013"
        },
        "LOAD_CASE_NAMES": [
            "ULS_SET B(CB:max)",
            "ULS_SET B(CB:min)",
            "ULS_EQ(CB:max)",
            "ULS_EQ(CB:min)"
        ],
        "PARTS": [
            "Part I",
            "Part J"
        ]
    }
})

Example 2: Call the analyzed results from the opened file, and print the first node results. (The results would be the same as the right upper “Data” results.)

import requests
import json

CIVIL_API_URL = "http://127.0.0.1:10024"

## POST /POST/TABLE
Anal_Result=requests.post(CIVIL_API_URL + "/post/table",json={
    "Argument": {
        "TABLE_TYPE": "beam force",
        "UNIT": {
            "FORCE": "KN",
            "DIST": "M",
            "HEAT": "CAL",
            "TEMP": "C"
        },
        "NODE_ELEMS": {
            "TO": "1003 to 1013"
        },
        "LOAD_CASE_NAMES": [
            "ULS_SET B(CB:max)",
            "ULS_SET B(CB:min)",
            "ULS_EQ(CB:max)",
            "ULS_EQ(CB:min)"
        ],
        "PARTS": [
            "Part I",
            "Part J"
        ]
    }
}).text

Anal_Result=json.loads(Anal_Result)
Anal_IntForc=Anal_Result['empty']['DATA']
print(Anal_IntForc[0])

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 requests
import json

CIVIL_API_URL = "http://127.0.0.1:10024"

## POST /VIEW/CAPTURE
requests.post(CIVIL_API_URL + "/view/capture",json = {
    "Argument":{
        "UFIG_LIST": [
            {
                "IS_PRE_MODE": False,
                "VIEW_HORIZONTAL": 45,
                "VIEW_VERTICAL": 30,
                "POST_DISP_OPT": {
                    "PRINT_VALUE": False,
                    "VALUE_DECIMAL_PT": 3,
                    "PRINT_LEGEND": True,
                    "LEGEND_DECIMAL_PT": 3
                },
                "OUTPUT_PATH": "C:\\ULS_SET B(Shear-Z).jpg",
                "LOADCASE_MINMAX": "ALL",
                "LOADCASE_TYPE": "CB",
                "LOADCASE_NAME": "ULS_SET B",
                "CURRENTMODE": 6,
                "RESULT_COMP": 2,
            }
        ]
    }
})

  • No labels