{"id":869,"date":"2019-12-22T09:34:52","date_gmt":"2019-12-22T08:34:52","guid":{"rendered":"https:\/\/pythoncvc.net\/?p=869"},"modified":"2021-02-15T22:51:36","modified_gmt":"2021-02-15T21:51:36","slug":"freecad-external-editor-with-code-oss","status":"publish","type":"post","link":"https:\/\/pythoncvc.net\/?p=869","title":{"rendered":"FreeCAD external editor with Code &#8211; OSS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I&rsquo;m not a big fan of M$ but Code &#8211; OSS is quite good and is the one I managed to set up with FreeCAD with a working debugger. I am very open to try another documented way if you have one to suggest !<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Set up<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To allow auto-completion and your linter like pylint to work partially you need to reference FreeCAD libraries. Examples below shows path for Arch\/Manjaro with <a href=\"https:\/\/aur.archlinux.org\/packages\/freecad-git\">freecad-git<\/a> installed. To get better auto-completion you can also add reference to <a href=\"https:\/\/github.com\/CyrilWaechter\/freecad-stubs\">freecad-stubs<\/a> folder below I cloned it in my home git folder with <code>git clone https:\/\/github.com\/CyrilWaechter\/freecad-stubs.gi<\/code>t.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Open your working folder in Code &#8211; OSS eg. :<ul><li>macro folder : <code>~\/.FreeCAD\/Macro<\/code><\/li><li>your in progress workbench folder<\/li><\/ul><\/li><li>Create an .env file referencing FreeCAD lib folder and optionally your stubs folder :<br><code>FREECAD_LIB=\/usr\/lib\/freecad\/lib<\/code><br><code>FREECAD_STUBS=\/home\/&lt;user>\/git\/freecad-stubs\/out<\/code><br><code>PYTHONPATH=${FREECAD_MOD}:${FREECAD_LIB}:${PYTHONPATH}<\/code><br>(Note that on windows you need to replace <code>:<\/code> by <code>;<\/code>)<\/li><li>If you use git, add .vscode and .env to gitignore to avoid dirtyness<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">.env file concept is explained in Code &#8211; OSS help : <a href=\"https:\/\/code.visualstudio.com\/docs\/python\/environments#_environment-variable-definitions-file\">Environment variable definitions file<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><s>Unfortunately auto-completion and linter do not work for everything eg. Part. Shall we generate <a href=\"https:\/\/github.com\/gtalarico\/ironpython-stubs\">stubs like Gui Talarico did for Revit API<\/a> or is there a better way ?<\/s> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Update: I have generated <a href=\"https:\/\/github.com\/CyrilWaechter\/freecad-stubs\">stubs<\/a> with mypy stubgen. Still unperfect but far better than before. You might want to keep an eye on Vanuan <a href=\"https:\/\/github.com\/Vanuan\/freecad-python-stubs\">freecad-python-stubs<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once set up you have multiple options :<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.freecadweb.org\/wiki\/Embedding_FreeCAD\">Embedding FreeCAD<\/a><\/li><li>Use your script <a href=\"https:\/\/www.freecadweb.org\/wiki\/Macros\">as a macro<\/a> or as <a href=\"https:\/\/www.freecadweb.org\/wiki\/Workbench_creation\">a full workbench<\/a><\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Embedding FreeCAD<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As explained on the wiki page FreeCAD can be embedded in another application sharing the host event loop. Let&rsquo;s take a <a href=\"https:\/\/wiki.qt.io\/Qt_for_Python\">very basic example of application with PySide2 on their website<\/a> :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import sys\n\nfrom PySide2.QtWidgets import QApplication, QLabel\n                                                    \nif __name__ == \"__main__\":\n    app = QApplication(sys.argv)\n    label = QLabel(\"Hello World\")\n    label.show()\n    sys.exit(app.exec_())<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let&rsquo;s replace the hello world label with the two lines from FreeCAD wiki and create a box :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import FreeCAD\nimport FreeCADGui\nimport Part\n\nimport sys\n\nfrom PySide2.QtWidgets import QApplication\n\n\nif __name__ == \"__main__\":\n\n    app = QApplication(sys.argv)\n\n    FreeCADGui.showMainWindow()\n    \n    doc = FreeCAD.newDocument()\n    box = Part.makeBox(100, 100, 100)\n    Part.show(box)\n\n    sys.exit(app.exec_())<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&rsquo;s it, nothing more. The sad thing is that I don&rsquo;t know yet how to interact with an already running application like you do with eg. Libre Office. Maybe with <a href=\"https:\/\/doc.qt.io\/qt-5\/qprocess.html\">QProcess<\/a> ? I saw multiple reference to this on Stackoverflow : <a href=\"https:\/\/stackoverflow.com\/questions\/21780196\/read-output-from-python-script-in-c-qt-app\">Read output from python script in C++ Qt app<\/a>, <a href=\"https:\/\/stackoverflow.com\/questions\/43859810\/communicating-with-qprocess-python-program\">Communicating with QProcess Python program<\/a>). I saw on FreeCAD forum that some people are doing it using a webserver : <a href=\"https:\/\/forum.freecadweb.org\/viewtopic.php?t=23671#p184692\">Re: Remote editor possible ?<\/a>, <a href=\"https:\/\/jirivalasek.github.io\/Animate\/Guide\/server.html\">Animate &#8211; Server<\/a>. But for good reason or not it seems weird to me to use a webserver to communicate between 2 local applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Macro \/ workbench<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Nothing much to say here. As you modify you <a href=\"https:\/\/www.freecadweb.org\/wiki\/Macros\">macro<\/a> or <a href=\"https:\/\/www.freecadweb.org\/wiki\/Workbench_creation\">workbench<\/a> you can then use it in FreeCAD as usual.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Debugging<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Embedding FreeCAD<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As you embed FreeCAD in your own application you can use your usual debugger.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Macro \/ workbench<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To debug a script running from FreeCAD check <a href=\"http:\/\/Debugging wiki page \">Debugging wiki page<\/a>. I described the process for Code &#8211; OSS at <a href=\"https:\/\/www.freecadweb.org\/wiki\/Debugging#Visual_Studio_Code_.28VS_Code.29\">Visual Studio Code (VS Code)<\/a> paragraph. You need <code>ptvsd<\/code> installed :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install ptvsd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add a piece of code to your script :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import ptvsd\nprint(\"Waiting for debugger attach\")\n# 5678 is the default attach port in the VS Code debug configurations\nptvsd.enable_attach(address=('localhost', 5678), redirect_output=True)\nptvsd.wait_for_attach()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And add a new debug configuration : <strong>Debug \u2192 Add Configurations\u2026<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \"configurations\": [\n        {\n            \"name\": \"Python: Attacher\",\n            \"type\": \"python\",\n            \"request\": \"attach\",\n            \"port\": 5678,\n            \"host\": \"localhost\",\n            \"pathMappings\": [\n                {\n                    \"localRoot\": \"${workspaceFolder}\",\n                    \"remoteRoot\": \".\"\n                }\n            ]\n        },<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then start your script from FreeCAD which freeze waiting for the debugger to start.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Video demo<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Code - OSS as a FreeCAD external editor\" width=\"620\" height=\"349\" src=\"https:\/\/www.youtube.com\/embed\/1Pn2ok_22O8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>I&rsquo;m not a big fan of M$ but Code &#8211; OSS is quite good and is the one I managed to set up with FreeCAD with a working debugger. I am very open to try another documented way if you have one to suggest ! Set up To allow auto-completion and your linter like pylint [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[203],"tags":[193],"class_list":["post-869","post","type-post","status-publish","format-standard","hentry","category-freecad","tag-freecad"],"_links":{"self":[{"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/posts\/869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=869"}],"version-history":[{"count":8,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/posts\/869\/revisions"}],"predecessor-version":[{"id":922,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=\/wp\/v2\/posts\/869\/revisions\/922"}],"wp:attachment":[{"href":"https:\/\/pythoncvc.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythoncvc.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}