Introduction
Moldex3D Studio is a Microsoft Windows application which includes COM technology(Component Object Model).
COM objects can be created with a variety of programming languages.
NOTE
We recommend that user shall use the most current version of the Application Programming Interface (“API”) for the Software.
CoreTech shall not be responsible for the consequences of any error, omissions or incompatibility that may occur in any application built by user with the API and sample codes provided by CoreTech.
Deprecated API functions (Deprecated List) will not be maintained and upgraded, and can be removed in the future versions.
Setup
Register Moldex3D Studio as COM server
To use Moldex3D Studio as a COM server, the application must be registered in the Windows registry.
- Open the Command Prompt (cmd.exe) as administrator
- Register Studio via the following command:
C:\Moldex3D\2024\Bin\MDXStudio.exe /regserver
If the message box pops up and shows “Studio registered”, it means that the Studio is registered successfully.
The information about Moldex3D Studio COM server
- The uuid of type library: {ADF44B00-CF78-4CDA-9669-102E31A0CBF6}
- The external name of the application: Moldex3DStudio2024.App
NOTE
If you need the information for earlier versions, please refer to Appendix.
Required Licenses
- Moldex3D Tools - API is necessary license to use Studio API.
- Moldex3D - STUDIO is necessary license to use Studio application.
- Depending on modules or functions, other licenses may be required.
Getting Started with Studio APIs
In this tutorial, you can know how to automate Moldex3D Studio with Python.
There are two ways to do automation with the Studio application:
- Create a program to manipulate Studio applications.
- Directly run a script in the Studio application to manipulate it.
NOTE
You need to register the Studio application before using Studio API.
If you don't know how to do, you can refer to the section Register Moldex3D Studio as COM server.
Prerequisites
To successfully automate Moldex3D Studio with Python, you need to first set up your Python development environment. It requires:
- Python 3
- Python extension: pywin32
If they are already installed on your computer, you can jump to the section Create a program to manipulate Studio application.
Install Python 3
- Install Python for Windows and add Python to PATH
Python Executable installer is available on Python official website.
- Verify the Python installation
If your computer only installs one version of Python, run the following command: python --version
If your computer installs more than one version of Python, run the following command: py -3 --version
Install the Python extension pywin32
- Check if pip is already installed via the following command:
py -3 -m pip --version
If pip is not installed, you can follow the tutorials on python.org to deal with it.
- Use pip to install packages
py -3 -m pip install pywin32
- Verify the pywin32 installation
py -3 -m pip list
Create a program to manipulate Studio application
1. Generate Python COM support files for a type library
To use a COM object from Python, the module for type library needs to be generated first.
import win32com.client
typelib_uuid = '{ADF44B00-CF78-4CDA-9669-102E31A0CBF6}'
win32com.client.gencache.MakeModuleForTypelib(typelib_uuid, 0, 1, 0)
2. Get a Studio COM object from Python
Before calling Studio API functions, you need to get a Studio COM object.
There are three ways to get a COM object with win32com
module:
win32com.client.DispatchEx(program_id)
win32com.client.Dispatch(program_id)
win32com.client.GetObject(instance_id)
If you are using Moldex3D Studio 2021 or earlier versions, the methods mentioned above are not supported.
You can refer to the section 2-4. Connect to an existing Studio application (Before Moldex3D Studio 2022) for more information.
2-1. Launch a new Studio application
To launch a new Studio application always, you can use the following codes:
import win32com.client
import pythoncom
running_object = pythoncom.GetRunningObjectTable().EnumRunning()
ctx = pythoncom.CreateBindCtx(0)
ro_list = []
for obj in running_object:
ro_list.append(obj.GetDisplayName(ctx,None))
program_id = 'Moldex3DStudio2024.App'
studio = None
while studio == None:
studio = win32com.client.DispatchEx(program_id)
if studio != None and studio.instance_id in ro_list:
studio = None
else:
break
NOTE
The above codes use studio.instance_id to obtain the instance ID via API.
If you are using Moldex3D Studio 2022 or earlier versions, the above codes are not applicable.
2-2. Connect to an existing Studio application
If there is an existing Studio application and you want to get a COM object connecting to the application, you can use the following codes:
import win32com.client
program_id = 'Moldex3DStudio2024.App'
studio = win32com.client.Dispatch(program_id)
NOTE
If there is no open Studio application, a new Studio application will be launched.
If there are multiple existing Studio applications, you can get a COM object connecting to the Studio application that is launched first.
2-3. Connect to an specific Studio application
If there are one or more existing Studio applications and you want to get a COM object connecting to a specific one, you can use the following codes:
import win32com.client
instance_id = ''
studio = win32com.client.GetObject(instance_id)
NOTE
The instance ID can be gotten from About dialog in Studio.
It is a string like '{46122917-DF2F-4DDF-A291-18BB78804A51}'.
2-4. Connect to an existing Studio application (Before Moldex3D Studio 2022)
In earlier versions of Moldex3D Studio, you can only manipulate a Studio application via API at a time.
To get a COM object, you can use the following codes:
import win32com.client
program_id = 'Moldex3DStudio2021.App'
studio_class = win32com.client.gencache.GetClassForProgID(program_id)
if studio_class is None:
print('[Error] win32com.client.gencache.GetClassForProgID() failed!')
studio = None
else:
studio = studio_class()
NOTE
If there is no open Studio application, a new Studio application will be launched.
Otherwise, you will get a COM object connecting to the Studio application that is launched first.
3. Add a new run for Injection Molding via API
Here is a sample script to automate Moldex3d Studio with Python.
It shows you how to do the following actions via API:
- Launch a new Studio application
- Open an existing project
- Add a new run in the project
- Configure run settings
- Close the Studio application
import os
import sys
import win32com.client
"""
What to learn:
(1) how to open a project file
(2) how to add a new run
(3) how to specify mesh file
(4) how to specify material file for part and then generate default run settings automatically
"""
def main():
typelib_uuid = '{ADF44B00-CF78-4CDA-9669-102E31A0CBF6}'
program_id = 'Moldex3DStudio2024.App'
print('1.Generate support for a type library: %s' %typelib_uuid)
win32com.client.gencache.MakeModuleForTypelib(typelib_uuid, 0, 1, 0)
print('2.Launch a new Studio application by DispatchEx(): %s' %program_id)
studio = win32com.client.DispatchEx(program_id)
print('3.Start to manipulate Studio with API...')
proj_file = 'C:\\Moldex3D\\2024\\Samples\\Solid\\Injection\\Gear\\Gear.mrm'
mesh_file = 'C:\\Moldex3D\\2024\\Samples\\Solid\\Injection\\Gear\\Mesh\\Gear_Part.mfe'
material_file = 'C:\\Moldex3D\\2024\\Samples\\Solid\\Injection\\Gear\\Material\\ABS_STYLACVA29_1.mtr'
b_continue = True
print(' -Open a project file: %s' %proj_file)
run_ref = studio.OpenFile(proj_file, 'mm')
if run_ref == '':
print(' OpenFile() failed: %s' %studio.error_msg)
b_continue = False
else:
print(' OpenFile(): get Run %s' %run_ref)
new_run_ref = ''
if b_continue:
new_run_ref = studio.AddRun('IM')
if new_run_ref == '':
print(' -AddRun() failed!')
b_continue = False
else:
print(' -Add a new run for Injection molding: Run %s' %new_run_ref)
if b_continue:
print(' -Specify mesh file: %s' %mesh_file)
studio.SetMeshFile(new_run_ref, mesh_file)
if studio.error_msg != '':
print(' SetMeshFile() failed: %s' %studio.error_msg)
b_continue = False
if b_continue:
print(' -Specify material file for part and then create default process, computation parameters and analysis sequence')
studio.SetMaterialFile(new_run_ref, material_file, '-EM -id 1 -createdefault')
if studio.error_msg != '':
print(' SetMaterialFile() failed: %s' %studio.error_msg)
b_continue = False
os.system("PAUSE")
print(' -Close Studio')
studio.Close()
if __name__=="__main__":
main()
See Examples to learn more.
Run a script in Studio application
Moldex3D Studio 2022 supports a new function Run Script command.
You can use a Python script to specify a batch of actions via API.
This script must be written in Python 3 and contain the specific function run_script(studio)
.
Here is a sample script:
def run_script(studio):
return 0
NOTE
In a script for the Run Script command, you can use modules in Python standard library and modules in pywin32 only.
The version of Python used in the Run Script command is Python3.8.
Other Python modules installed via pip or other ways are not supported.
1. Create a script for Run Script command
Let us modify the sample script provided in the section Create a program to manipulate Studio application. Then, Create a script to do the following actions:
- Check whether a project is open in the Studio
- Add a new run to the open project
- Configure run settings
def run_script(studio):
print('Start to manipulate Studio with API...')
b_continue = True
run_ref = studio.GetCurrentRunRef()
if run_ref == '':
print(' [ERROR] There is no open project.')
b_continue = False
else:
print('Current Run: %s' %run_ref)
sub_step = 1
new_run_ref = ''
if b_continue:
print('(%d) Add a new run for Injection Molding ...' %sub_step)
sub_step += 1
new_run_ref = studio.AddRun('IM')
if new_run_ref == '':
print(' -AddRun() failed!')
b_continue = False
else:
print(' -AddRun() succeeded: get Run %s' %new_run_ref)
mesh_file = 'C:\\Moldex3D\\2024\\Samples\\Solid\\Injection\\Gear\\Mesh\\Gear_Part.mfe'
material_file = 'C:\\Moldex3D\\2024\\Samples\\Solid\\Injection\\Gear\\Material\\ABS_STYLACVA29_1.mtr'
if new_run_ref != '':
if mesh_file != '':
print('(%d) Specify mesh: %s ...' %(sub_step, mesh_file))
sub_step += 1
studio.SetMeshFile(new_run_ref, mesh_file)
print(' -SetMeshFile(): done')
if material_file != '':
print('(%d) Specify material file for part and then create default run settings: %s ...' %(sub_step, material_file))
studio.SetMaterialFile(new_run_ref, material_file, '-EM -id 1 -createdefault')
if studio.error_msg != '':
print(' -SetMaterialFile() failed: %s' %(studio.error_msg))
else:
print(' -SetMaterialFile() succeeded')
pro_file, iret = studio.GetProcessFile(new_run_ref)
print(' -Auto-generated process file: %s' %(pro_file))
return 0
if __name__=="__main__":
pass
2. Call Run Script command
Here is a step-by-step to use Run Script Command in Studio:
- Luanch a Studio application.
- Open a project.
- Click Run Script button in Utility tab.
- Select a Python file to run. Or you can use the Python file created in the previous section.
- If the script is executed successfully, the return value will be 0.