Add an option to incorporate Macro in the CAD file

A forum for research and development of the user interface of FreeCAD
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
lordsansui
Posts: 45
Joined: Thu Sep 15, 2022 8:54 pm

Add an option to incorporate Macro in the CAD file

Post by lordsansui »

Hello,

I'd like to suggest and new feature for FreeCAD that might improve the life for new user or for the ones that will use Freecad for one specif purpose and like convenience.

Would it be possible to incorporate the macro script into the CAD file *.FCStd?

Currently, macros are manage as a separate file and rely on the Software not on the model, if you change your computer, you need to set up all macro again. Offering and option to incorporate the macro in the file itself will give user some convenience as Microsoft Excel Or Libre Office Calc do with incorporate macro, and this can improve UX.

The current Macro as library can remain as it is, and maybe just offer a second option to built-in macro in the model.
vm4dim
Posts: 129
Joined: Tue Nov 23, 2021 1:05 am

Re: Add an option to incorporate Macro in the CAD file

Post by vm4dim »

You may save python code to Text_document (Menu > Tools > Add text document) and run it:
    eval(FreeCAD.activeDocument().getObject("Text_document").Text)
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Add an option to incorporate Macro in the CAD file

Post by onekk »

lordsansui wrote: Tue Sep 27, 2022 2:08 pm ...
Have you taken in account the "risk of malware" when you permit to execute a code from a "zipped container" like a FCStd file is?

From what I know this has been discussed and reject in the past to avoid this risk.

It could be handy, but you could even use a taylor made Wb and use ScriptedObjects to achieve what you want in a "relatively easy" way.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Add an option to incorporate Macro in the CAD file

Post by adrianinsaval »

onekk wrote: Tue Sep 27, 2022 3:36 pm It could be handy, but you could even use a taylor made Wb and use ScriptedObjects to achieve what you want in a "relatively easy" way.
the whole point of embedding the macro is to be able to run in stock FreeCAD elsewhere without having to setup addons/macros. IMO this could be a handy feature as long as the macro can't run automatically. Perhaps a context menu item in the text object mentioned above "Open as macro" which would open the the macro editor with that text, with this the user will be forced to at least have a passing look at the macro he's about to run.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Add an option to incorporate Macro in the CAD file

Post by onekk »

adrianinsaval wrote: Tue Sep 27, 2022 3:40 pm ...
the whole point of embedding the macro is to be able to run in stock FreeCAD elsewhere without having to setup addons/macros. IMO this could be a handy feature as long as the macro can't run automatically.
I know this and I agree that this could be handy.

If I remember well sometime ago I have had a discussion with some developers about an easy way to extend FCStd format, as example having the possibility to populate some styles, stored in a sqlite3 database or even a way to use directly the sqlite3 db to store "binary blobs" like images or macro and and the proposition was rejected due to this risk of "malware", but this was done some time ago when @yorick was integrating "Annotations Styles", so I could remember wrong.

I admit that prohibit the "automatic execution" is a "measure of security" but how much users confirm blindly a "warning message" without reading it?

I could make a file that do the equivalent of "format C:" in a python macro, and if the user has installed FC with "administrator privileges" like some people are suggesting even in this forum ....

Probably something like a warning that some "addon module" is required to "use the file" should be a "decent compromise", as addons are checked or at least if an addon is making something harmful it could be "blacklisted".

But probably here is not the correct place to discuss these things, more on "developers" forum.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
lordsansui
Posts: 45
Joined: Thu Sep 15, 2022 8:54 pm

Re: Add an option to incorporate Macro in the CAD file

Post by lordsansui »

Thank for the feedbacks. I hope you can take a good decision in order to implement the tool in the best way.
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: Add an option to incorporate Macro in the CAD file

Post by heron »

vm4dim wrote: Tue Sep 27, 2022 3:16 pm You may save python code to Text_document (Menu > Tools > Add text document) and run it:
eval(FreeCAD.activeDocument().getObject("Text_document").Text)
Hello, I'm trying this and get - > SyntaxError: invalid syntax
test_eval.png
test_eval.png (64.9 KiB) Viewed 1186 times
do you know why it is happening?
chrisb
Veteran
Posts: 53930
Joined: Tue Mar 17, 2015 9:14 am

Re: Add an option to incorporate Macro in the CAD file

Post by chrisb »

A sufficiently convenient process could be:
- distribute the model together with the macros, e.g. in a zipped folder
- unzip the folder somwhere else
- run the macro from the menu

This is already possible, but it is unconvenient, that the configured macro path changes. I would like to see the possibility implemented to call a single macro and leave the path as configured in the preferences. This would not open a security hole, as it is not just confirming one of these never read messages.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Add an option to incorporate Macro in the CAD file

Post by Roy_043 »

heron wrote: Wed Sep 28, 2022 6:55 am do you know why it is happening?
This works:

Code: Select all

eval(compile(FreeCAD.activeDocument().getObject("Text_document").Text, "<string>", "exec"))
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: Add an option to incorporate Macro in the CAD file

Post by heron »

Roy_043 wrote: Wed Sep 28, 2022 7:49 amThis works:
Wow!!!
I have no clue the code means, but it works of course.

I believe this is an useful thing.

Many thanks @Roy_043 !!!
Post Reply