Debugging of Macros

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
usbhub
Posts: 280
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Debugging of Macros

Post by usbhub »

Hello,
is it possible to debug a macro (not a workbench) with FreeCAD? I use Linux, so I can't use Visual Studio.
It would be OK, if it is only possible about the command line (GUI would be nicer, but I will stay alive ;) ).

Thanks in advance!
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Debugging of Macros

Post by openBrain »

What kind of debugging do you expect?
Often printing manual debug traces in the console is enough. ;)
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Debugging of Macros

Post by vocx »

usbhub wrote: Fri Sep 20, 2019 8:46 pm is it possible to debug a macro (not a workbench) with FreeCAD?
...
What do you mean by "debug"? There is no standard interpretation for this word. It basically means "test that everything is okay, and that there are no errors (bugs)".

So, yes, you can debug any macro, just by looking at the code, and testing each line in sequence until you find a problem.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
usbhub
Posts: 280
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Re: Debugging of Macros

Post by usbhub »

With "debugging" I mean things like stepping, stop points, etc.
Of course, you can do a lot with console logging, but for bigger projects, it is a bit uncomfortable, so I asked.

usbhub
Buddy Hy
Posts: 32
Joined: Sat Aug 31, 2019 11:30 am

Re: Debugging of Macros

Post by Buddy Hy »

Sorry to hijack this thread, but I was about to ask the same question.
I get an error, but can't seem to set a break point so I and step through the code to inspect variables and objects.
Is there a separate workbench for this? Or is it all guess work?
wmayer
Founder
Posts: 20318
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Debugging of Macros

Post by wmayer »

An option is the program Winpdb. Just search for it in the forum to get some details how to use it.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Debugging of Macros

Post by vocx »

Buddy Hy wrote: Fri Sep 20, 2019 11:07 pm ...
I get an error, but can't seem to set a break point so I and step through the code to inspect variables and objects.
Is there a separate workbench for this? Or is it all guess work?
See this thread: Python workbenches debugging.

There is no difference between a workbench and a macro, it's the same thing essentially, it's Python code.

You should develop your code in such a way that is easy to test, using small functions and classes, so that it is obvious where the errors are, and you don't have to inspect every single variable and so on.

For example, I dislike the way many macros are programmed because they use a lot of global variables. Creating classes and modifying the attributes of a class instance would be much better than using a hundred global variables.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Buddy Hy
Posts: 32
Joined: Sat Aug 31, 2019 11:30 am

Re: Debugging of Macros

Post by Buddy Hy »

Thanks Vox, great post you sent us to - I'll be checking out 'pdbpp'.

The program I have trouble with only has 35 lines and I know exactly where the problem is - I just don't know why.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Debugging of Macros

Post by Kunda1 »

vocx wrote: Sat Sep 21, 2019 12:22 am For example, I dislike the way many macros are programmed because they use a lot of global variables. Creating classes and modifying the attributes of a class instance would be much better than using a hundred global variables.
@vocx would you mind finding examples illustrating your point of good coding standards and vice-versa ?
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
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Debugging of Macros

Post by vocx »

Kunda1 wrote: Sat Sep 21, 2019 8:10 pm @vocx would you mind finding examples illustrating your point of good coding standards and vice-versa ?
Well, that's just general good Python knowledge. Make things small and readable. For example, no more than 3 levels of indentation is good practice. If you need more levels, then encapsulate the inner levels into a function, and call that function.

In the Doxygen page I placed relevant documents for the reader. This write up by Moult summarizes many good programming practices; good for both C++ and Python that we use in FreeCAD.
carlopav wrote: Sat Aug 10, 2019 9:05 pm Any advice on codeformatting Yorik?
I did find very useful this article by @Moult https://sevenstrokes.net/learn-how-write-good-code .
Maybe it is off topic... But it was really helpful to me.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Post Reply