TLSインポートスクリプトの例(Cintoo Connect CLI)

この記事には、コメント付きのPythonスクリプトの例が含まれています。このスクリプトは、Cintoo Connect CLIの機能を活用して地上型スキャンデータをインポートできます。


スクリプトの例は、添付ファイルで提供されています。


このスクリプトは、特定のワークゾーンへのスキャンのインポートを自動化する方法の例です。URIは必要ありませんが、いくつかのIDを指定する必要があります。
関数 "import_file" 内でimport-staticコマンドを呼び出します。必要に応じて自由に変更できます。


以下に、各部分のコメント付きでスクリプトを示します。

from pathlib import Path
from typing import Final

import json
import logging
import os
import sys
import subprocess
import requests

TIMEOUT: Final[int] = 10

TOKEN: Final[str] = ""
GLOBAL_HEADERS: Final[dict] = {"Authorization": f"Bearer {TOKEN}"}

ライブラリのインポート。トークンとヘッダーの取得。詳細については、Cintoo Connect CLI APIガイドをご覧ください。



<WORKZONE_ID>/data
ACCOUNT_ID: Final[str] = ""
PROJECT_ID: Final[str] = ""
WORKZONE_ID: Final[str] = ""
  • 対象のプロジェクトをホストするアカウントのアカウントID
  • 対象のワークゾーンが属するプロジェクトに対応するプロジェクトID
  • 対象のワークゾーンに対応するワークゾーンID

これらは、HTTPSリクエストで取得するか、ワークゾーン内のWebアプリケーションのURLから取得できます。

https://aec.cintoo.com/accounts/<ACCOUNT_ID>/projects/<PROJECT_ID>/workzones/

より詳細なガイダンス:Cintoo Connect CLI APIガイド。



DEFAULT_URL: Final[str] = "https://aec.cintoo.com"

デフォルトのURLは、構成に応じてaec.cintoo.com、us.cintoo.com、または組織に専用ドメインがある場合は[company].cintoo.comのいずれかになります。



def get_cintoo_cli_exe() -> str:
    path = Path(os.environ['LOCALAPPDATA']) / "Programs" / "Cintoo"
    if not path.exists():
        path = Path(os.environ['ProgramFiles']) / "Cintoo"
    if not path.exists():
        raise RuntimeError("Could not find Cintoo Connect's installation."
                           "\nIf you did not installed it in the default path,"
                           " please set this path variable to the right path")
    cli = path / "cintooconnect-cli.exe"
    if not cli.exists():
        raise RuntimeError("Could not find cintooconnect-cli.exe."
                           " Please install the latest version of Cintoo Connect: "
                           "https://aec.cintoo.com/download/cintooconnect-setup-latest.exe")
    return cli.as_posix()


CINTOO_CLI_EXE: Final[str] = get_cintoo_cli_exe()

Cintoo Connect CLI実行ファイルの検索と実行。実行ファイルは、Cintoo Connectがインストールされている場所と同じ場所にあります。デフォルトでは、次の場所にあります:

  • Users/"YourUserName"/AppData/Local /Programs/Cintoo
  • ProgramFiles/Cintoo(管理者権限でインストールした場合)



def post_and_check_request(url: str,
                           headers: dict) -> dict:
    logging.info(f"Query: url={url}, headers={headers}")
    try:
        reply = requests.post(url, headers=headers, timeout=TIMEOUT)
        reply.raise_for_status()
        return reply.json()
    except json.JSONDecodeError as exception:
        message = f"Exception during decoding json when using token: {exception}"
    except requests.exceptions.RequestException as exception:
        message = f"Exception during validation of token: {exception}"
    except Exception as exception:
        message = f"Exception: {exception}"
    logging.error(message)
    raise RuntimeError(message)

サーバーへの情報の送信と、サーバーの応答の検証結果の取得。



def validate_login():
    url = f"{DEFAULT_URL}/api/isLogged"
    response = post_and_check_request(url, GLOBAL_HEADERS)
    if not response.get("success"):
        raise ValueError("Login required: unable to verify your login status.\n"
                         f"Details: {response}.\n"
                         "Please log in by following the instructions here:\n"
                         "https://aec.cintoo.com/api/#section/API-Specification/Authentication")

ログインの検証。



def get_cintoo_uri(account_id: str,
                   project_id: str,
                   workzone_id: str):
    url = 
'{DEFAULT_URL}/api/accounts/{account_id}/projects/{project_id}/workzones/{workzone_id}/imports'
    response = post_and_check_request(url, GLOBAL_HEADERS)
    cintoo_uri = response.get("url")
    if not cintoo_uri:
        raise KeyError(f"Could not find url in response: {response}")
    return cintoo_uri

先に定義したアカウント、プロジェクト、ワークゾーンの各コンポーネントからの、Cintoo URIの取得。



def import_file(cintoo_uri: str,
                file_path: str):
    return subprocess.call([CINTOO_CLI_EXE, "import-static",
                            f"--cintoo-uri={cintoo_uri}",
                            f"--file-ref={file_path}"])

Cintoo Connect CLI実行ファイルによるインポート処理の実行。



def import_files_in_dir(cintoo_uri: str,
                        root_dir_path: str):
    for (dirpath, _, filenames) in os.walk(root_dir_path):
        for filename in filenames:
            file_path = os.path.join(dirpath, filename)
            import_file(cintoo_uri, file_path)

指定したディレクトリ内のすべてのファイルのインポート。



def main(path: str):
    assert TOKEN, "Token must be specified"
    assert ACCOUNT_ID and PROJECT_ID and WORKZONE_ID, "IDs must be specified"
    validate_login()
    cintoo_uri = get_cintoo_uri(ACCOUNT_ID, PROJECT_ID, WORKZONE_ID)
    if os.path.isfile(path):
        import_file(cintoo_uri, path)
    elif os.path.isdir(path):
        import_files_in_dir(cintoo_uri, path)

if __name__ == "__main__":
    arguments = sys.argv
    if len(arguments) != 2:
        raise RuntimeError("You need to pass the path to directory or file to import")
    main(arguments[1])

メイン関数の実行。

この記事は役に立ちましたか?

それは素晴らしい!

フィードバックありがとうございます

お役に立てず申し訳ございません!

フィードバックありがとうございます

この記事に改善できることがあれば教えてください。

少なくとも一つの理由を選択してください
CAPTCHA認証が必要です。

フィードバックを送信しました

記事の改善におけるご協力ありがとうございます。