{"id":180947,"date":"2024-01-03T11:21:33","date_gmt":"2024-01-03T11:21:33","guid":{"rendered":"\/knowledge\/forums\/topic\/using-the-scade-python-apis-from-your-favorite-ide\/"},"modified":"2024-09-13T08:19:17","modified_gmt":"2024-09-13T08:19:17","slug":"using-the-scade-python-apis-from-your-favorite-ide","status":"publish","type":"topic","link":"https:\/\/innovationspace.ansys.com\/knowledge\/forums\/topic\/using-the-scade-python-apis-from-your-favorite-ide\/","title":{"rendered":"Using the SCADE Python APIs from your favorite IDE"},"content":{"rendered":"<h3  id=\"INTRODUCTION\">Introduction<\/h3>\n<p>Like most Ansys products, SCADE comes with a comprehensive Python API. It allows you to programmatically use SCADE to automate tasks (e.g. setting up a CI\/CD toolchain for your SCADE models).<br \/>\nIn this article, we\u2019ll explore how to set up your favorite Integrated Development Environment (IDE) to write and debug Python code interacting with SCADE.<\/p>\n<h3  id=\"REQUIREMENTS\">Requirements<\/h3>\n<p>To get started, you\u2019ll need access to SCADE dev docs, a working installation of SCADE, a sample Python script to test that all works as intended, and of course, your trusty IDE. Let\u2019s tick these off one by one.<\/p>\n<h4  id=\"SCADE-PYTHON-API-DOCS\">SCADE Python API docs<\/h4>\n<p>As a developer, your go-to page for working with SCADE should be <a href=\"https:\/\/developer.ansys.com\/scade\" target=\"_blank\" rel=\"noopener\">developer.ansys.com\/scade<\/a>. On there, you will find key resources such as the SCADE Python API Guide.<br \/>\nAt the time of this writing, the page points to the guide for version 2023 R2.<br \/>\nThe API allows you to:<\/p>\n<ul>\n<li>Programmatically read and modify SCADE models, test results, and generated artifacts.<\/li>\n<li>Run SCADE jobs such as code generation or model testing.<\/li>\n<\/ul>\n<h4  id=\"A-LOCAL-SCADE-INSTALLATION\">A local SCADE installation<\/h4>\n<p>To interact with SCADE, you need a local installation of the SCADE product<a href=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-sherlock.gif\" target=\"_blank\" rel=\"noopener\">.<\/a><\/p>\n<p>It comes with:<\/p>\n<ul>\n<li>A distribution of Python. For instance, SCADE 2023 R2 embeds Python 3.10 64bit under <code>\/contrib\/Python310<\/code><\/li>\n<li>A Python implementation of the SCADE API library, under <code>\/SCADE\/APIs\/Python\/lib<\/code><\/li>\n<li>Binary executables that will be used to execute some functions of the API (e.g. code generation), under <code>\/SCADE\/bin<\/code><\/li>\n<\/ul>\n<blockquote><p><em>Note: you may use your own distribution of Python instead of the one provided with SCADE. Should you opt for this, please make sure you extend your <code>PYTHONPATH<\/code> environment variable with the above directories.<\/em><\/p><\/blockquote>\n<h4  id=\"A-SAMPLE-STARTER-SCRIPT\">A sample starter script<\/h4>\n<p>Let\u2019s get started with a basic script template that opens a SCADE project and sets it up for use through the API. It will allow you to verify that your IDE setup is correct and functional.<\/p>\n<p>First, we need to load the project itself, represented by a <code>.etp<\/code> file on disk.<\/p>\n<p>When using your IDE of choice, you\u2019ll need some preamble code to explicitly load a project:<br \/>\n<code>##<br \/>\n# identical to scade -script   ...<br \/>\nimport scade_env<br \/>\nscade_env.load_project('path\/to\/my\/scade\/project.etp')<br \/>\n##<br \/>\n<\/code><\/p>\n<blockquote><p><em>Note: SCADE supports running Python scripts directly from its user interface (known as SCADE Studio) or from the command line:<br \/>\n<code>scade.exe -script   ...  []<\/code><br \/>\nIn both cases, the project is automatically loaded by the SCADE executable before the Python script starts. Therefore, there is no need to explicitly load a project and the <code>load_project<\/code> call is simply ignored.<\/p>\n<\/blockquote>\n<p><\/em><\/p>\n<p>Next, we\u2019ll import some API modules:<\/p>\n<p><code>import scade.model.project.stdproject as project<br \/>\nfrom scade import output<br \/>\n<\/code><\/p>\n<blockquote><p><em><\/p>\n<p>Note: SCADE comes with two project APIs:<\/p>\n<ul>\n<li><code>scade.model.project.natproject<\/code>: Pure Python API.<\/li>\n<li><code>scade.model.project.stdproject<\/code>: SCADE Studio API.<\/li>\n<\/ul>\n<p>The documentation advises importing <code>scade.model.project<\/code> which resolves to one of the above APIs depending on the name of the executable running the script: <code>vcs.exe<\/code> or <code>stdtcl.exe<\/code> on one hand, any other executable like <code>python.exe<\/code> on the other hand.<br \/>\nIn our sample script however, importing <code>stdproject<\/code> directly ensures that scripts behave the exact same way, whether they are launched from a SCADE environment or an external Python environment.\n<\/p>\n<p><\/em><\/p><\/blockquote>\n<p>Finally, we can add some basic statements to navigate the model and print some information:<\/p>\n<p><code>projects = project.get_roots()<br \/>\nfor p in projects:<br \/>\n&nbsp;&nbsp;assert isinstance(p, project.Project)<br \/>\n&nbsp;&nbsp;output(p.pathname + 'n')<br \/>\n<\/code><\/p>\n<blockquote><p><em>Note: The <code>assert<\/code> statement may help your IDE\u2019s IntelliSense \/ auto-completion realize that <code>p<\/code> is of type <code>Project<\/code>.<\/em><\/p><\/blockquote>\n<p>Here is the final script, all put together:<\/p>\n<p><code>##<br \/>\n# identical to scade -script   ...<br \/>\nimport scade_env<br \/>\nscade_env.load_project('path\/to\/my\/scade\/project.etp')<br \/>\n##<br \/>\n&zwj;<br \/>\nimport scade.model.project.stdproject as project<br \/>\nfrom scade import output<br \/>\n&zwj;<br \/>\nprojects = project.get_roots()<br \/>\nfor p in projects:<br \/>\n&nbsp;&nbsp;assert isinstance(p, project.Project)<br \/>\n&nbsp;&nbsp;output(p.pathname + 'n')<br \/>\n<\/code><\/p>\n<h3  id=\"SETTING-UP-YOUR-FAVORITE-IDE\">Setting up your favorite IDE<\/h3>\n<p>Now that we have the basics, let\u2019s look at setting up your IDE. In this article, we\u2019ll look at three popular ones: Visual Studio Code (or VS Code), PyCharm and Visual Studio.<\/p>\n<h4  id=\"VISUAL-STUDIO-CODE\">Visual Studio Code<\/h4>\n<p>Visual Studio Code (<a href=\"https:\/\/code.visualstudio.com\" target=\"_blank\" rel=\"noopener\">code.visualstudio.com<\/a>) is a free open-source IDE. It is popular because of its customizability through numerous extensions.<\/p>\n<h5  id=\"INSTALLATION\">Installation<\/h5>\n<p>Install &amp; launch the IDE, then install the Python extension:<\/p>\n<ul>\n<li>Open the Extensions panel with Ctrl + Shift + X<\/li>\n<li>Type &#8216;python&#8217; in the search field<\/li>\n<li>Click &#8216;install&#8217; for extension &#8216;Python&#8217;<\/li>\n<\/ul>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-vscode-install.gif\" \/><br \/>\n  <em>Installing the Python extension<\/em>\n<\/p>\n<h5  id=\"PROJECT-CREATION-CONFIGURATION\">Project creation &amp; configuration<\/h5>\n<p>A VSCode project, or more precisely a workspace, is a directory. Simply drag and drop a directory containing the above test script onto the IDE window to initialize a Python project.<br \/>\nIn your project, create a folder called <code>.vscode<\/code>.<br \/>\nInside folder <code>.vscode<\/code>, create a file called <code>settings.json<\/code> with the following content:<\/p>\n<p><code>{<br \/>\n&nbsp;&nbsp;\/\/ Use Python interpreter embedded with SCADE<br \/>\n&nbsp;&nbsp;\"python.defaultInterpreterPath\": \"\/contrib\/Python310\/python.exe\",<br \/>\n&nbsp;&nbsp;\/\/ Enable Intellisense autocomplete<br \/>\n&nbsp;&nbsp;\"python.autoComplete.extraPaths\": [<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"\/SCADE\/bin\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"\/SCADE\/APIs\/Python\/lib\"<br \/>\n&nbsp;&nbsp;],<br \/>\n&nbsp;&nbsp;\"python.analysis.extraPaths\": [<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\"\/SCADE\/APIs\/Python\/lib\"<br \/>\n&nbsp;&nbsp;]<br \/>\n}<br \/>\n<\/code><\/p>\n<p>This tells VSCode to use the Python interpreter embedded with SCADE and enables autocomplete for the SCADE API.<br \/>\nInside folder <code>.vscode<\/code>, create a file called <code>launch.json<\/code> with the following content:<\/p>\n<p><code>{<br \/>\n&nbsp;&nbsp;\"version\": \"0.2.0\",<br \/>\n&nbsp;&nbsp;\"configurations\": [<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"name\": \"Python: Current File\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"type\": \"python\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"request\": \"launch\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"program\": \"${file}\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"console\": \"integratedTerminal\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"justMyCode\": true,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"env\": {\"PYTHONPATH\": \"\/SCADE\/APIs\/Python\/lib;\/SCADE\/bin\"},<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;]<br \/>\n}<br \/>\n<\/code><\/p>\n<p>This ensures that the SCADE API library is used at runtime.<br \/>\nYour VSCode project structure should now look as follows:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-vscode-project-structure.png\" \/><br \/>\n  <em>VSCode project structure<\/em>\n<\/p>\n<h5  id=\"RESULTS\">Results<\/h5>\n<p>You may run and\/or debug your Python script. The debug view allows you to peek into the variables and their structure:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-vscode-debug.png\" \/><br \/>\n  <em>Debugging a SCADE Python script in VSCode<\/em>\n<\/p>\n<h4  id=\"PYCHARM\">PyCharm<\/h4>\n<p>PyCharm (<a href=\"https:\/\/jetbrains.com\/pycharm\" target=\"_blank\" rel=\"noopener\">jetbrains.com\/pycharm<\/a>) is another popular IDE, specific to Python.<\/p>\n<h5  id=\"PROJECT-CREATION-CONFIGURATION\">Project creation &amp; configuration<\/h5>\n<p>Click &#8220;File&#8221; then &#8220;New Project\u2026&#8221; to open this dialog:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-pycharm-new-project.png\" \/><br \/>\n  <em>New PyCharm project<\/em>\n<\/p>\n<p>Click &#8220;Add Interpreter&#8221; to add the Python executable delivered with your installation of SCADE:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-pycharm-add-interpreter.png\" \/><br \/>\n  <em>Adding a Python interpreter to PyCharm<\/em>\n<\/p>\n<p>Complete project creation by clicking &#8220;OK&#8221;, then &#8220;Create&#8221;.<br \/>\nClick &#8220;File&#8221; then &#8220;Settings&#8230;&#8221; to open the settings window. Configure &#8220;Project Structure&#8221; as follows:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-pycharm-project-structure.png\" \/><br \/>\n  <em>PyCharm project structure to work with SCADE APIs<\/em>\n<\/p>\n<p>Use button &#8220;+ Add Content Root&#8221; to add the following folders:<\/p>\n<ul>\n<li><code>SCADEAPIsPythonlib<\/code><\/li>\n<li><code>SCADEbin<\/code><\/li>\n<\/ul>\n<p>This tells PyCharm to use the SCADE API sources for autocompletion services.<\/p>\n<h5  id=\"RESULTS\">Results<\/h5>\n<p>You may run and\/or debug your Python script. The debug view allows you to peek into the variables and their structure:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-pycharm-debug.png\" \/><br \/>\n  <em>Debugging a SCADE Python script in PyCharm<\/em>\n<\/p>\n<h4  id=\"VISUAL-STUDIO\">Visual Studio<\/h4>\n<p>Visual Studio (<a href=\"https:\/\/visualstudio.microsoft.com\" target=\"_blank\" rel=\"noopener\">visualstudio.microsoft.com<\/a>) is a popular IDE for .NET and C++ development on Windows. It can support Python development thanks to extensions.<\/p>\n<h5  id=\"INSTALLATION\">Installation<\/h5>\n<p>Upon installation of Visual Studio, make sure to tick the Python Workload:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-visual-studio-install-python-workload.png\" \/><br \/>\n  <em>Visual Studio installation with Python Workload<\/em>\n<\/p>\n<h5  id=\"PROJECT-CREATION-CONFIGURATION\">Project creation &amp; configuration<\/h5>\n<p>Click &#8220;File&#8221;, &#8220;New&#8221;, then &#8220;Project&#8221; to open the new project dialog. Select &#8220;From Existing Python code&#8221; then click &#8220;Next&#8221;:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-visual-studio-new-project.png\" \/><br \/>\n  <em>Creating a new Python project in Visual Studio<\/em>\n<\/p>\n<p>Fill in your preferred project name and path, then finalize by clicking &#8220;Create&#8221;.<br \/>\nYour Solution Explorer should look like this:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-visual-studio-project-structure.png\" \/><br \/>\n  <em>Solution Explorer before project configuration<\/em>\n<\/p>\n<p>By default, the project uses one of your standard installed Python interpreters. We recommend using the Python environment installed with SCADE.<br \/>\nTo do so, right click &#8220;Python Environments&#8221;, then &#8220;View All Python Environments&#8221;. Click &#8220;Add Environment&#8221; and fill the pop-up as follows:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-visual-studio-add-python-environment.png\" \/><br \/>\n  <em>Adding the SCADE Python environment to Visual Studio (shown for SCADE 2024 R1)<\/em>\n<\/p>\n<p>Click &#8220;Add&#8221; to apply your changes. The new Python environment should now be selected in your Solution Explorer view.<br \/>\nNext, right-click &#8220;Search Paths&#8221; and select &#8220;Add Folder to Search Path\u2026&#8221; twice to add the following folders:<\/p>\n<ul>\n<li><code>SCADEAPIsPythonlib<\/code><\/li>\n<li><code>SCADEbin<\/code><\/li>\n<\/ul>\n<p>This tells Visual Studio to use the SCADE API sources for autocompletion services.<br \/>\nMake sure to drop the sample Python script into your project folder.<br \/>\nYour Solution Explorer view should now look as follows:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-visual-studio-project-structure-2.png\" \/><br \/>\n  <em>Complete project structure<\/em>\n<\/p>\n<h5  id=\"RESULTS\">Results<\/h5>\n<p>You may run and\/or debug your Python script. The debug view allows you to peek into the variables and their structure:<\/p>\n<p style=\"text-align: center\">\n  <img decoding=\"async\" src=\"\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/01\/scade-005-visual-studio-debug.png\" \/><br \/>\n  <em>Debugging a SCADE Python script in Visual Studio<\/em>\n<\/p>\n<h5  id=\"QUESTIONS\">Questions?<\/h5>\n<p>Once this small test script works in your IDE of choice, you are ready to go! Remember to refer to the API documentation over at <a href=\"https:\/\/developer.ansys.com\/scade\" target=\"_blank\" rel=\"noopener\">developer.ansys.com\/scade<\/a> as you develop.<br \/>\nIf you have questions or feedback, don\u2019t hesitate to ask our <a href=\"\/forum\/forums\/forum\/discuss-simulation\/embedded-software\/\" target=\"_blank\" rel=\"noopener\">Ansys expert community<\/a>.<\/p>\n<h3  id=\"ABOUT-THE-AUTHOR\">About the author<\/h3>\n<table style=\"border: none !important\">\n<tr>\n<td style=\"border: none !important\">\n<p style=\"text-align: center\">\n    <img decoding=\"async\" src=\"https:\/\/innovationspace.ansys.com\/knowledge\/wp-content\/uploads\/sites\/4\/2024\/09\/scade-jean-henry.png\" style=\"max-height: 200px !important\" \/>\n        <\/td>\n<td style=\"border: none !important\">\n<p><strong>Jean Henry<\/strong> (<a href=\"https:\/\/www.linkedin.com\/in\/jean-henry-bb16792\/\">LinkedIn<\/a>) is a Senior Principal Product Specialist at Ansys. He has been working on the SCADE product since its inception, more than 30 years ago. He has expertise in software development, testing and integration.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n","protected":false},"template":"","class_list":["post-180947","topic","type-topic","status-publish","hentry","topic-tag-api","topic-tag-ide","topic-tag-pycharm","topic-tag-python","topic-tag-scade","topic-tag-visual-studio","topic-tag-vscode"],"aioseo_notices":[],"acf":[],"custom_fields":[{"0":{"_wp_page_template":["default"],"_btv_view_count":["6113"],"_bbp_likes_count":["17"],"_bbp_engagement":[""],"_bbp_reply_count":[""],"_bbp_reply_count_hidden":[""],"_bbp_last_active_time":["1\/3\/2024 20:20"],"_bbp_last_active_id":[""],"_bbp_last_reply_id":[""],"_bbp_author_ip":["23.56.168.181"],"_bbp_topic_id":["183561"],"_bbp_forum_id":["27825"],"_bbp_voice_count":[""],"_wp_desired_post_slug":[""],"_edit_lock":["1726215535:1769"],"_edit_last":["1769"],"filter_by_optics_product":["Lumerical"],"_filter_by_optics_product":["field_64fb192ba3121"],"application_name":[""],"_application_name":["field_64a80903c8e15"],"family":[""],"_family":["field_64a809229a857"],"siebel_km_number":[""],"_siebel_km_number":["field_63ecbffce60db"],"salesforce_km_number":[""],"_salesforce_km_number":["field_63ecc018e60dc"],"km_published_date":[""],"_km_published_date":["field_64c77704499dd"],"product_version":[""],"_product_version":["field_64c776cb4fd2e"],"_yoast_wpseo_content_score":["90"],"_yoast_wpseo_estimated-reading-time-minutes":["7"],"_yoast_wpseo_wordproof_timestamp":[""]},"test":"solution"}],"_links":{"self":[{"href":"https:\/\/innovationspace.ansys.com\/knowledge\/wp-json\/wp\/v2\/topics\/180947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/innovationspace.ansys.com\/knowledge\/wp-json\/wp\/v2\/topics"}],"about":[{"href":"https:\/\/innovationspace.ansys.com\/knowledge\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":8,"href":"https:\/\/innovationspace.ansys.com\/knowledge\/wp-json\/wp\/v2\/topics\/180947\/revisions"}],"predecessor-version":[{"id":183561,"href":"https:\/\/innovationspace.ansys.com\/knowledge\/wp-json\/wp\/v2\/topics\/180947\/revisions\/183561"}],"wp:attachment":[{"href":"https:\/\/innovationspace.ansys.com\/knowledge\/wp-json\/wp\/v2\/media?parent=180947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}