Debugging macros with VS 2017

Need help, or want to share a macro? Post here!
TheMarkster
Posts: 1318
Joined: Thu Apr 05, 2018 1:53 am

Debugging macros with VS 2017

Postby TheMarkster » Wed May 23, 2018 10:35 pm

Is anybody else using VS 2017 for debugging FreeCAD macros? I have found it to be quite useful, so I decided to put together a post describing how to set it up and use it.

Download VS2017 here: https://www.visualstudio.com/downloads/

There are versions for Windows and macOS (but I don't think you can debug FreeCAD macros on the mac version). I'm using the free community edition for Windows.

Download and run the installer. I selected every python option I could find in the installer because I wasn't sure which, if any, would be needed. I presume at least the ability to do python projects would be required.

Once you get VS2017 installed, debugging FreeCAD macros is as simple as loading the macro in VS2017, attaching the debugger to FreeCAD.exe, setting up your breakpoints (in VS2017, not in FreeCAD), and executing the macro in FreeCAD as you would normally execute it.

You will need to add a .py extension to the macro in order for VS2017 to recognize it as a python source file. It will still work in FreeCAD with the .py extension, (e.g. MyMacro.FCMacro becomes MyMacro.FCMacro.py).

Create this macro and name it test.FCMacro.py:

Code: Select all

import FreeCAD
import Part

x,y,z = 3,5,7
box = Part.makeBox(x,y,z)
Part.show(box)
Open the file in VS2017 and add a breakpoint on the last line: Part.show(box).
Open FreeCAD if it's not already running.
In the debug menu in VS2017, select Attach to Process...
In the Attach To: section select Python as the type of code to debug.
Find FreeCAD.exe in the list of running processes and click Attach.
Execute the macro in FreeCAD as you would normally execute it. Macro -> Macros -> testFCMacro.py -> Execute. (Don't choose debug, choose execute.)

If all goes well you should be in the VS2017 debugger with a screen similar to this one:
screenshot1.png
screenshot1.png (54.28 KiB) Viewed 1741 times
If it doesn't work (or if it stops working) try restarting FreeCAD and re-attaching.

In the Debug -> Windows menu you will find options to bring up the various windows. I have found locals, autos, watch1, and the Immediate Window to be useful.

The locals window gives the most objects.
The autos window narrows down the list to focus on the ones you're most likely to be interested in.
In the watch1 window you can specify only those objects you're interested in.
The Immediate window can be used to modify values on the fly, call a function, etc.

Here is a screenshot of the autos window:
screenshot_autos.png
screenshot_autos.png (36.84 KiB) Viewed 1741 times
In the Watch1 window you can browse FreeCAD objects. For example, here I've entered:

Code: Select all

FreeCADGui.Selection
as a watch.
screenshot-selection.png
screenshot-selection.png (57.44 KiB) Viewed 1741 times
This is a good way to browse to see which properties and methods are available, or at least some of them.

The VS2017 editor has some nice features. It can, for example, comment out (or uncomment) an entire block of selected code. (Ctrl+k,c to comment, Ctrl+k,u to uncomment). It can indent / unindent blocks of selected code: Edit -> Advanced -> Increase Line indent. Set a bookmark in your file: Ctrl+k,k. Navigate to next bookmark: Ctrl+k,n (or Ctrl+k,p for the previous bookmark). Another nifty feature is an ability to rename an object and have all references updated to the new name. For example, highlight the word box in the 2nd line, and do Ctrl+r, Ctrl+r (do it twice). Give box a new name called newBox and hit ok. It not only renames box to newBox, but it also updates the reference to box in the next line: Part.show(box) becomes Part.show(newBox). Ctrl+f to open a find dialog. Ctrl+h for a find and replace dialog.
My FreeCAD video series on youtube: https://www.youtube.com/c/mwganson
User avatar
DeepSOIC
Posts: 7600
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Debugging macros with VS 2017

Postby DeepSOIC » Fri Jul 27, 2018 11:26 pm

Thanks for sharing! To my bookmarks it goes.
User avatar
wandererfan
Posts: 3841
Joined: Tue Nov 06, 2012 5:42 pm

Re: Debugging macros with VS 2017

Postby wandererfan » Sat Jul 28, 2018 1:24 pm

Is there an easy way to turn a forum post into wiki format? I've added this link as a temporary measure:

PythonDebuggingWiki.png
PythonDebuggingWiki.png (31.67 KiB) Viewed 1611 times
User avatar
Markymark
Posts: 159
Joined: Sun Nov 03, 2019 4:54 pm

Re: Debugging macros with VS 2017

Postby Markymark » Tue Nov 26, 2019 6:33 am

Uuups ... have to edit this because I mixed up the languages ... sorry!

Tried to get this up and running but Freecad crashes every time at the end of the debugging session. This is the signature I get from the "no response"-window:

Problemsignatur:
Problemereignisname: APPCRASH
Anwendungsname: FreeCAD.exe
Anwendungsversion: 0.0.0.0
Anwendungszeitstempel: 5dbdbdb8
Fehlermodulname: python36.dll
Fehlermodulversion: 3.6.6150.1013
Fehlermodulzeitstempel: 5b59b527
Ausnahmecode: c0000005
Ausnahmeoffset: 000000000001d153
Betriebsystemversion: 6.3.9600.2.0.0.256.48
Gebietsschema-ID: 1031
Zusatzinformation 1: b96a
Zusatzinformation 2: b96a0b5cceac32919c4a358d7e6e3eb9
Zusatzinformation 3: 7cbe
Zusatzinformation 4: 7cbeaa3792cec7fef6e62c81a44b0d9b

Environment:

OS: Windows 8.1
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.4 (GitTag)
Build type: Release
Branch: releases/FreeCAD-0-18
Hash: 980bf9060e28555fecd9e3462f68ca74007b70f8
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: German/Germany (de_DE)

Its the same with the current 0.19. Wonder if there is an idea what to try next. Would be great to have a powerful debug environment with GUI.

Thanks in advance.

Mark
wmayer
Site Admin
Posts: 15701
Joined: Thu Feb 19, 2009 10:32 am

Re: Debugging macros with VS 2017

Postby wmayer » Tue Nov 26, 2019 11:10 am

Do you have set the build option FREECAD_RELEASE_PDB in CMake? With this you will get a release version with minimum debug information and for a crash you should see a more meaningful call stack with function names instead of hex numbers.
User avatar
Kunda1
Posts: 7308
Joined: Thu Jan 05, 2017 9:03 pm

Re: Debugging macros with VS 2017

Postby Kunda1 » Tue Nov 26, 2019 12:25 pm

#documentation
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
User avatar
Markymark
Posts: 159
Joined: Sun Nov 03, 2019 4:54 pm

Re: Debugging macros with VS 2017

Postby Markymark » Wed Nov 27, 2019 12:59 pm

wmayer wrote:
Tue Nov 26, 2019 11:10 am
Do you have set the build option FREECAD_RELEASE_PDB in CMake? With this you will get a release version with minimum debug information and for a crash you should see a more meaningful call stack with function names instead of hex numbers.
I don't build FreeCAD ... I am just trying to debug macros.
#documentation
Pardon?

Meanwhile, I got a pretty good workflow up and running with Visual Studio Code. So, no need to get further into this at the moment from my point of view. If this is of general interest I will be happy to do some investigation. Just let me know ...

Thanks so far ... Mark
dan-miel
Posts: 124
Joined: Thu Sep 13, 2018 12:29 am
Location: Spokane WA. USA

Re: Debugging macros with VS 2017

Postby dan-miel » Wed Jan 22, 2020 8:53 pm

To TheMarkster,
I used the Visual Studio Editor to program MS Visual Basic for years. A year ago I started playing in FreeCAD using the VS editor as a dumb text editor. Although I had read that it was easy to setup for Python after many internet searches, many hours of trying to understand MS instructions and hours and frustrating hours of debugging code because a month ago I misspelled a variable: I ran across this post. Plain English, to the point, directed at FreeCAD users. I once more have a debugging tool. Many thanks for posting this. I wish I had found it a year ago.
Dan