Using Visual Studio as Python editor

Need help, or want to share a macro? Post here!
chakotay65
Posts: 6
Joined: Tue Jun 21, 2016 2:04 pm

Using Visual Studio as Python editor

Post by chakotay65 »

I am new to FreeCAD (and Python). My experience is using C# with Visual Studio. My problem: typing in all commands to create objects in the python console is tedious and the next day all my scripting is lost. I hoped to write my python scripts/code in VS (2015) and execute them in FreeCAD. Opening the *.py samples in VS is simple but VS keeps complaining about not able to find dependencies like Part or FreeCAD. Am I thinking the right way? missing anything?

I read I should append my FreeCAD data:

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.16.6704 (Git)
Build type: Release
Branch: releases/FreeCAD-0-16
Hash: 0c449d7e8f9b2b1fb93e3f8d1865e2f59d7ed253
Python version: 2.7.8
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17
mario52
Veteran
Posts: 4542
Joined: Wed May 16, 2012 2:13 pm

Re: Using Visual Studio as Python editor

Post by mario52 »

hi
for macro Python use the editor Image

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
chakotay65
Posts: 6
Joined: Tue Jun 21, 2016 2:04 pm

Re: Using Visual Studio as Python editor

Post by chakotay65 »

Thanks!

Meanwhile watched this on YouTube, very good: https://www.youtube.com/playlist?list=P ... 26cn9idJrj
shadybob
Posts: 18
Joined: Wed Jun 06, 2018 5:50 pm

Re: Using Visual Studio as Python editor

Post by shadybob »

mario52 wrote: Tue Jun 21, 2016 5:35 pm hi
for macro Python use the editor Image

mario
What? That's like telling me to use vi as my text editor instead of Notepad++! Seriously though, I have the same idea using visual studio 2017 for macro programming (AND testing). I just think it would have more better options than running the macro editor in freecad. I don't even necessarily need access to the freecad workbenches for now, but I need a bit more than I currently have. I setup a custom python environment to point to the needed items inside the freecad installation folder, but it would seem that not all the installed packages are showing up (the ones under FreeCAD_0.19\bin\Lib\site-packages folder. Perhaps I have to modify the PYTHONPATH variable to include other locations? One of the modules I call in my first macro is PySide. It works from Freecad, but VS2017 doesn't know where it is (I found it in the Ext folder right under the main Freecad folder. But how do I reference that? Then would also need to add the Mod folder for all the workbenchs.
shadybob
Posts: 18
Joined: Wed Jun 06, 2018 5:50 pm

Re: Using Visual Studio as Python editor

Post by shadybob »

I did a print(os.environ) from inside Freecad, and the PATH variable looks to have everything I would need to somehow add to my custom python env. for Freecad. Is there a script somewhere inside freecad that sets that all up?

'PATH': 'E:\\FreeCAD_0.19\\bin\\lib\\site-packages\\PySide2;E:\\FreeCAD_0.19\\bin;E:\\FreeCAD_0.19\\Mod\\AddonManager;
E:\\FreeCAD_0.19\\Mod\\Arch;E:\\FreeCAD_0.19\\Mod\\Complete;E:\\FreeCAD_0.19\\Mod\\Draft;
E:\\FreeCAD_0.19\\Mod\\Drawing;E:\\FreeCAD_0.19\\Mod\\Fem;E:\\FreeCAD_0.19\\Mod\\Idf;
E:\\FreeCAD_0.19\\Mod\\Image;E:\\FreeCAD_0.19\\Mod\\Import;E:\\FreeCAD_0.19\\Mod\\Inspection;
E:\\FreeCAD_0.19\\Mod\\Material;E:\\FreeCAD_0.19\\Mod\\Measure;E:\\FreeCAD_0.19\\Mod\\Mesh;
E:\\FreeCAD_0.19\\Mod\\MeshPart;E:\\FreeCAD_0.19\\Mod\\OpenSCAD;E:\\FreeCAD_0.19\\Mod\\Part;
E:\\FreeCAD_0.19\\Mod\\PartDesign;E:\\FreeCAD_0.19\\Mod\\Path;E:\\FreeCAD_0.19\\Mod\\Points;
E:\\FreeCAD_0.19\\Mod\\Raytracing;E:\\FreeCAD_0.19\\Mod\\ReverseEngineering;
E:\\FreeCAD_0.19\\Mod\\Robot;E:\\FreeCAD_0.19\\Mod\\Show;
E:\\FreeCAD_0.19\\Mod\\Sketcher;E:\\FreeCAD_0.19\\Mod\\Spreadsheet;E:\\FreeCAD_0.19\\Mod\\Start;
E:\\FreeCAD_0.19\\Mod\\Surface;E:\\FreeCAD_0.19\\Mod\\TechDraw;E:\\FreeCAD_0.19\\Mod\\Test;
E:\\FreeCAD_0.19\\Mod\\Tux;E:\\FreeCAD_0.19\\Mod\\Web;C:\\Users\\Bob\\AppData\\Roaming\\FreeCAD\\Mod\\A2plus;
C:\\Users\\Bob\\AppData\\Roaming\\FreeCAD\\Mod\\Assembly4;
C:\\Users\\Bob\\AppData\\Roaming\\FreeCAD\\Mod\\BOLTSFC;
C:\\Users\\Bob\\AppData\\Roaming\\FreeCAD\\Mod\\CommandPanel;
C:\\Users\\Bob\\AppData\\Roaming\\FreeCAD\\Mod\\fasteners;
C:\\Users\\Bob\\AppData\\Roaming\\FreeCAD\\Mod\\parts_library;
C:\\Users\\Bob\\AppData\\Roaming\\FreeCAD\\Mod\\Silk;
C:\\Users\\Bob\\AppData\\Roaming\\FreeCAD\\Mod\\workfeature;
C:\\Users\\Bob\\AppData\\Roaming\\FreeCAD\\Mod\\workfeature-macro;
+more system stuff
User avatar
naxq0
Posts: 50
Joined: Wed Apr 25, 2018 7:45 am

Re: Using Visual Studio as Python editor

Post by naxq0 »

Hi Bob,

I'm on Ubuntu, but I guess the steps would be similar on Windows

You can modify the PYTHONPATH or modify it at the top of the script using sys.path.append('/your/path'). But I'm not a fan of either of those options

I'm guessing you're working in a python virtual environment. In that case you have to do is add the paths to your external dependencies as pth files to the site packages

In other words:
make a file called /path/to/your/venv/lib/python3.6/site-packages/freecad.pth which should contain the paths to the external freecad libraries (for me it looks like:
/usr/lib/freecad-python3/lib
/usr/share/freecad/Mod

Afterwards, you should be able to import it without messing with the environment variables
mario52
Veteran
Posts: 4542
Joined: Wed May 16, 2012 2:13 pm

Re: Using Visual Studio as Python editor

Post by mario52 »

shadybob wrote: Thu Feb 06, 2020 11:46 pm
mario52 wrote: Tue Jun 21, 2016 5:35 pm hi
for macro Python use the editor Image

mario
What? That's like telling me to use vi as my text editor instead of Notepad++! Seriously though, .........
and why not ? all my macros (see my signature Gist) are made only with the FreeCAD editor Image and the GUI with QtCreator

the only problem there is no search function in the editor

or see eric - python, eclipse and other

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
shadybob
Posts: 18
Joined: Wed Jun 06, 2018 5:50 pm

Re: Using Visual Studio as Python editor

Post by shadybob »

mario52 wrote: Fri Feb 07, 2020 10:13 am
and why not ? all my macros (see my signature Gist) are made only with the FreeCAD editor Image and the GUI with QtCreator

the only problem there is no search function in the editor
My main problem with the built in editor is that I am currently doing alot of testing, using print statements, which all appear to go to the tiny report window. I suppose I could just resize that window. Not sure why the printing doesn't happen in the python console window? If I type a print command in the python console, the output prints right in the console window, not the report view?
User avatar
Kunda1
Veteran
Posts: 13443
Joined: Thu Jan 05, 2017 9:03 pm

Re: Using Visual Studio as Python editor

Post by Kunda1 »

Checkout: FreeCAD External Editor With Code – OSS: https://pythoncvc.net/?p=869
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
shadybob
Posts: 18
Joined: Wed Jun 06, 2018 5:50 pm

Re: Using Visual Studio as Python editor

Post by shadybob »

Kunda1 wrote: Fri Feb 07, 2020 12:22 pm Checkout: FreeCAD External Editor With Code – OSS: https://pythoncvc.net/?p=869
Looks interesting! I'm going to try it out. Do you know offhand if the .env file setup works basically the same on Windows as shown on that page for linux (of course using slightly different path syntax)?
Post Reply