File Upload Instructions

General Process Overview

To upload a file to a component or a case, follow these general steps:

  1. Acquire a Pre-signed URL: This is the URL where the file will be uploaded.
  2. Upload the File: Use the provided upload_file function to send the file to the pre-signed URL.
  3. Finalize the Upload: Depending on the file type, use either the convert_file or initialize_file function to complete the process.

Uploading Files for a Specific Component

Purpose: Upload and render a STEP file for a single component.
Steps:

  1. Get a pre-signed URL using the get_file_upload_url method.
  2. Upload the file using the upload_file method.
  3. Convert the file using the convert_file method, which will generate an STL file for visualization.

Automation: These steps are wrapped in the upload_component_file method.

Uploading a Complete Case File

Purpose: Upload and render a full geometry STEP file that contains all regions.
Steps:

  1. Obtain a pre-signed URL (similar to the component upload).
  2. Upload the file using the upload_file method.
  3. Initialize the file using the initialize_file method (instead of converting it).

Automation: These steps are combined in the upload_complete_file method.

Uploading an STL File

Purpose: Upload an STL file for a component.
Steps:

  1. Obtain a pre-signed URL as in previous uploads.
  2. Upload the file using the upload_file method.

Automation: These steps are encapsulated in the upload_stl_file method.


Detailed Methods Overview

get_file_upload_url

The first step in the file upload process, creating an entry in the cloud and returning the pre-signed URL.

API call function:

def get_file_upload_url(self, case_Id, file_name, size, component_Id=None) -> Dict:
    """
    Returns: a json object with meta data for the file entry in the cloud

    :param int case_Id: id of the case you want to upload a file to
    :param string file_name: the name of the file
    :param int size: the size of the file in bytes
    :param int component_Id: optional parameter, id of the component that you want to upload a file for. Leave this parameter open if you want to upload a step file containging all components.
    """

    file_url = self.fileserver_url + "/cases/" + str(case_Id) + "/files"

    payload = json.dumps({"fileName": file_name,"fileSize": size, "caseComponentId": component_Id})


    response = requests.post(file_url, data=payload, headers=self.put_header)

    if(response.ok): 
        print("url acquired: \n", response.text, "\n")
        return response.json()
    else: 
        logger.error(f"failed to acquire url: {response.content}")
        raise Exception

upload_file

Handling the actual file upload to the pre-signed URL.

API call function:

def upload_file(self, presigned_url, file_path):
    """
    :param string presigned_url: a pre signed url to where you want to upload your file
    :param string file_path: path to the file you want to upload
    """

    with open(file_path, 'rb') as file:
        file_data = file.read()

    #response = requests.put(presigned_url, data=file_data, headers={'Content-Type': 'application/octet-stream'})
    response = requests.put(presigned_url, data=file_data, headers=self.url_post_header)


    if(response.ok): 
        print("file upload successful \n")
    else: 
        logger.error(f"file upload failed: {response.content}")
        raise Exception

convert_file

Converting the uploaded STEP file into an STL file, necessary for visualizing the file in the system.

API call function:

def convert_file(self, case_Id, file_Id, file_url) -> Dict:
    """
    Returns: a json object with meta data for the job
    
    :param int case_Id: id of the case you want a step file to be visualized in
    :param int file_Id: id of the file you want to convert (visualize)
    :param string file_url: url to the file you want to convert. This url is obtained in a previous step (the 'get_file_url' method). This is the normal url, not the pre-signed one
    """

    file_url_server = self.caseserver_url + "/jobs/convert"

    payload = json.dumps({"caseId": case_Id, "fileId": file_Id, "fileURL": file_url})

    response = requests.post(file_url_server, data=payload, headers=self.put_header)

    if(response.ok): 
        print("file conversion successful: \n", response.text, "\n")
        return response.json()
    else:
        if b"Job already runs for case" in response.content:
            print("A job is already running, please wait while the program tries again")
            while not response.ok:
                time.sleep(2)
                response = requests.post(file_url_server, data=payload, headers=self.put_header)
                if(response.ok):
                    print("file conversion successful: \n", response.text, "\n")
                    return response.json()
        else:
            logger.error(f"file conversion failed: {response.content}")
            raise Exception

initialize_file

Converts a previously uploaded file. This will generate an stl file, which will visualize the step file.

API call function:

def initialize_file(self, case_Id, file_Id, file_url) -> Dict:
    """
    Returns: a json object with meta data for the job
    
    :param int case_Id: id of the case you want a step file to be visualized in
    :param int file_Id: id of the file you want to convert (visualize)
    :param string file_url: url to the file you want to convert. This url is obtained in a previous step (the 'get_file_url' method). This is the normal url, not the pre-signed one
    """

    url = self.caseserver_url + "/jobs/initialize"

    payload = json.dumps({"caseId": case_Id, "fileId": file_Id, "fileURL": file_url})

    response = requests.post(url, data=payload, headers=self.put_header)

    if(response.ok): 
        print("file initialization successful: \n", response.text, "\n")
        return response.json()
    else: 
        logger.error(f"file initialization failed: {response.content}")
        raise Exception