JFT165ScriptEditortoJMAG e

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

JMAG Function Tutorial

Running JMAG Using a Python Development


Environment

Contents

Overview........................................................................................................................................ 2
1 Function Description ................................................................................................................... 2
1.1 Importing to JMAG-Designer ............................................................................................... 2
1.2 Automatically Recording The Script ..................................................................................... 2
2 Procedures ................................................................................................................................. 3
2.1 Starting the Script Editor ...................................................................................................... 3
2.2 Setting the Environment Variables....................................................................................... 3
2.3 Starting JMAG-Designer ...................................................................................................... 4
2.4 Loading the Model ............................................................................................................... 4
2.5 Setting the Script Language ................................................................................................ 4
2.6 Automatically Recording The Script ..................................................................................... 5
2.7 Editing the Script .................................................................................................................. 6
2.8 Running the Script ............................................................................................................... 7
2.9 Checking the Settings .......................................................................................................... 8
Appendix A How to Always Start a New Process ...................................................................... 9
Appendix B Input Assist Function for Scripts ........................................................................... 10

This document was created using the following software version.


■ Version JMAG-Designer 22.0, Spyder IDE 5.3.3
This document uses the following data.
■ JMAG Project File A JFT165ScriptEditortoJMAG-01.jproj
JMAG Function Tutorial - Running JMAG Using a Python Development Environment -

Overview
Although one way of improving analysis efficiency is to automate JMAG operations by using
scripts, developing the scripts can be time consuming. JMAG can be operated from an external
development environment by using Python scripts. Scripts can also be developed by using a
third-party integrated development environment which increases the efficiency of script
development. By using a third-party integrated development environment, you can develop
scripts while also checking the console output, and variable displays, etc.
This document explains how to run a Python script on a third-party integrated development
environment, then configure the settings for conditions in JMAG.

Keywords: Automation, Script Editor, Python, Editor, Debug, Spyder, IDE

1 Function Description
This explains the function used in this document.

1.1 Importing to JMAG-Designer


By importing JMAG-Designer modules, JMAG-Designer can be started and operated from an
external development environment by using Python scripts.

1.2 Automatically Recording The Script


Operations that are run with the JMAG-Designer GUI can be recorded as scripts. A recorded
script can then be edited with a script editor, etc.

2
JFT165-01e
JMAG Function Tutorial - Running JMAG Using a Python Development Environment -

2 Procedures
In this document, the operation for setting a current condition in JMAG-Designer is recorded as
a script. This script is then run in a third-party integrated development environment to check that
the current condition has indeed been set in JMAG-Designer.
The version of JMAG-Designer is specified via PYTHONPATH. In addition, a JMAG-Designer
module is imported for running from the external development environment.

<Preconditions>
The following environment is required for this tutorial.
 Versions of Python 3 are required to run the script.
 You can use either Python libraries that are already installed on the machine that you are
using, or the Python libraries that are installed together with JMAG-Designer.

2.1 Starting the Script Editor


Start the third-party script editor. Spyder is used in this example.

1. Start Spyder.

2.2 Setting the Environment Variables


Add the JMAG-Designer installation folder to the PYTHONPATH environment variable. Add the
PYTHONPATH manager in Spyder.

1. Select [Tools] > [PYTHONPATH manager] from the Spyder menu bar.
The [PYTHONPATH manager] dialog box is displayed.
2. Click [Add path].
The [Select directory] dialog box is displayed.
3. Open the JMAG-Designer installation folder.
This is installed in "C:¥Program Files¥JMAG-Designer22.0" by default.
4. Click [Select Folder].
The path to the JMAG-Designer installation folder is added.
5. Click [OK].
The [PYTHONPATH manager] dialog box closes.

3
JFT165-01e
JMAG Function Tutorial - Running JMAG Using a Python Development Environment -

2.3 Starting JMAG-Designer


Start JMAG-Designer.

1. Start JMAG-Designer.

2.4 Loading the Model


Open JMAG Project File A in JMAG-Designer.

1. Select [File] > [Open] from the JMAG-Designer menu bar.


The [Open File] dialog box is displayed.
2. Select JMAG Project File A (*.jproj).
3. Click [Open].
JMAG Project File A opens.

2.5 Setting the Script Language


Set the JMAG-Designer script language to Python.

1. Select [Tools] > [Preferences] from the JMAG-Designer menu bar.


The [Preferences] dialog box is displayed.
2. Click [Script] in the [Preferences] dialog box.
The [Script] window is displayed.
3. Set [Python (3.8)] as the [Script Language].
4. Set [Record using names] for the [Recording Preferences].
This records by name the arguments of the script that is to be recorded automatically.
5. Click [OK].
The [Preferences] dialog box closes.

4
JFT165-01e
JMAG Function Tutorial - Running JMAG Using a Python Development Environment -

2.6 Automatically Recording The Script


Record the operations in JMAG-Designer as a script. Delete the condition that is set after
recording the script. This is so to check the operation of the script in the later steps.

1. Select [Scripts] > [Start Recording] from the JMAG-Designer menu bar.
The automatic recording of the script starts, and [Recording] is displayed in the toolbar.
2. Click the [Conditions] tab in the [Toolbox].
The [Toolbox] switches to a display of a list of conditions.
3. Click the [Current] condition under [Current].
4. Drag this to a part in the graphics window.
The [Current] setting panel opens. [Square] is displayed under [Parts].
5. Click [OK].
The [Current] setting panel closes. [Current] is added under [2D Model] > [Study] >
[Conditions].
6. Select [Script] > [Stop Recording] from the menu bar.
The automatic recording of the script ends, and the [Script Editor] dialog box is displayed.
The script for setting the current condition has now been recorded (Fig. 2-6-1).
7. Right-click [Current] under [2D Model] > [Study] > [Conditions] in the [Project Manager]
tree.
8. Select [Delete] from the context menu.
[Current] is deleted from under [2D Model] > [Study] > [Conditions] (Fig. 2-6-2).
9. Ensure that JMAG-Designer remains displayed.

2 app = designer.GetApplication()

3 app.GetModel(u"Simple").GetStudy(u"Static").CreateCondition(u"Current", u"untitled 1")

4 app.GetModel(u"Simple").GetStudy(u"Static").GetCondition(u"untitled 1").ClearParts()

5 sel = app.GetModel(u"Simple").GetStudy(u"Static").GetCondition(u"untitled 1").GetSelection()

6 sel.SelectPart(4)

7 app.GetModel(u"Simple").GetStudy(u"Static").GetCondition(u"untitled 1").AddSelected(sel)

Fig. 2-6-1 Automatically recorded script

Fig. 2-6-2 No conditions are currently set

5
JFT165-01e
JMAG Function Tutorial - Running JMAG Using a Python Development Environment -

2.7 Editing the Script


Edit the script so that the JMAG-Designer module can be used in Spyder.

1. Enter "from jmag.designer import *" (the red frame displayed in Fig. 2-7-1) into the
Spyder editor.
2. Copy and paste the script that was recorded in Step 2.6.
3. Check that the recording is the same as Fig. 2-7-2.

Fig. 2-7-1 Spyder

8 from jmag.designer import *


9 app = designer.GetApplication()

10 app.GetModel(u"Simple").GetStudy(u"Static").CreateCondition(u"Current", u"untitled 1")

11 app.GetModel(u"Simple").GetStudy(u"Static").GetCondition(u"untitled 1").ClearParts()

12 sel = app.GetModel(u"Simple").GetStudy(u"Static").GetCondition(u"untitled 1").GetSelection()

13 sel.SelectPart(4)

14 app.GetModel(u"Simple").GetStudy(u"Static").GetCondition(u"untitled 1").AddSelected(sel)

Fig. 2-7-2 Edited script

6
JFT165-01e
JMAG Function Tutorial - Running JMAG Using a Python Development Environment -

2.8 Running the Script


Run the script in Spyder.

1. Select [Run] > [Run] from the Spyder menu bar.


The [Select JMAG-Designer(x64)] dialog box is displayed (Fig. 2-8-1). If JMAG-Designer
is running when the script is run, you can choose whether to use that instance of JMAG-
Designer which is already running, or start a new instance of JMAG-Designer.
2. Check that [Select Available JMAG-Designer(x64) to Attach] is selected.
3. Click [OK].

Fig. 2-8-1 [Select JMAG-Designer(x64)] dialog box


If JMAG-Designer is minimized, or started in Non-GUI mode, [Window] will not be displayed in [Window

Title].

7
JFT165-01e
JMAG Function Tutorial - Running JMAG Using a Python Development Environment -

2.9 Checking the Settings


In JMAG-Designer, check that the current condition is set by the script that was run in Spyder.

1. Display JMAG-Designer.
2. Check that [Current] is set under [2D Model] > [Study] > [Conditions] in the [Project
Manager] tree (Fig. 2-9-1).
3. Right-click [Current].
4. Select [Edit] from the context menu.
The [Current] setting panel opens.
5. Check that a part is added under [Parts].

Fig. 2-9-1 Set condition

END

8
JFT165-01e
JMAG Function Tutorial - Running JMAG Using a Python Development Environment -

Appendix A How to Always Start a New Process


To always start a new instance of JMAG-Designer when running a script, change the
GetApplication() command in Fig. 2-7-2 to a CreateApplication() command. An example script is
shown below. A Load() command is added to open JMAG Project File A.

1 from jmag.designer import *

2 # -*- coding: utf-8 -*-

3 app = designer.CreateApplication()

4 app.Load(u"D:¥JFT165ScriptEditortoJMAG-01.jproj")

5 app.GetModel(u"Simple").GetStudy(u"Static").CreateCondition(u"Current", u"untitled 1")

6 app.GetModel(u"Simple").GetStudy(u"Static").GetCondition(u"untitled 1").ClearParts()

7 sel = app.GetModel(u"Simple").GetStudy(u"Static").GetCondition(u"untitled 1").GetSelection()

8 sel.SelectPart(4)

9 app.GetModel(u"Simple").GetStudy(u"Static").GetCondition(u"untitled 1").AddSelected(sel)

Fig. A-1 Script for starting a new process

9
JFT165-01e
JMAG Function Tutorial - Running JMAG Using a Python Development Environment -

Appendix B Input Assist Function for Scripts


JMAG features an assist function for script commands. This function can also be used on an
external development environment. Enter "." after a command to list other commands that can
be used as candidates. The process, arguments, and return values of selected commands are
displayed via ToolTips. In addition, the code interpolation functions of external development
environments are also available. Partially enter a command, and press the Tab key on the
keyboard to run code interpolation.

1. Enter "app.GetCurrentModel()." in the Spyder editor.


GetCurrentModel() is a command to retrieve a Model object. Enter "." after the command
to display a list of Model class commands (Fig. B-1).
2. Click "CheckForNewResults()".
A description for CheckForNewResults() is displayed in a ToolTip (Fig. B-2).

Fig. B-1 Display of a list of command candidates and descriptions

Fig. B-2 CheckForNewResults() command description

10
JFT165-01e
JMAG Function Tutorial - Running JMAG Using a Python Development Environment -

URL https://www.jmag-international.com/tutorial/

JSOL Corporation
jmag-support@sci.jsol.co.jp
Technical Support
or
A distributor in your country

JMAG Function Tutorial


Copyright(C) 2022 JSOL Corporation All Rights Reserved.

11
JFT165-01e

You might also like