Ability to attach a Macro (or Macros) to a Document as a Spreadsheet is attached to a Document.

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
Post Reply
DanielLeeWenger
Posts: 53
Joined: Sun Feb 02, 2020 4:02 am
Location: Santa Cruz, California
Contact:

Ability to attach a Macro (or Macros) to a Document as a Spreadsheet is attached to a Document.

Post by DanielLeeWenger »

I have been designing objects where a Macro is used to create and/or modify objects in a Document. My list of Macros is growing and growing and it is a drag to open a Document of interest and then search through my Macro list for the Macro (Macros) associated with the Document.

I would like to be able to create a Document, and then a Spreadsheet perhaps, and then a Macro that would be associated with the Document in the same way a Spreadsheet is associated with a Document.

Has this feature been considered and discussed on the forum? Would others like this feature?

The groundwork exists already for such a feature. See 'Tools/Add a text document'. If the added text document could be made 'executable' then the desired feature would exist.

I hope to hear the thoughts of others on this subject.
User avatar
bambuko
Veteran
Posts: 2161
Joined: Thu Oct 24, 2019 12:53 pm
Location: UK, England, North Devon

Re: Ability to attach a Macro (or Macros) to a Document as a Spreadsheet is attached to a Document.

Post by bambuko »

Yes, I agree that it would be good if it was possible.
I am using Link branch and Assembly3
you can also download ... and try it here
excellent Assembly3 tutorials here
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Ability to attach a Macro (or Macros) to a Document as a Spreadsheet is attached to a Document.

Post by TheMarkster »

Update: 0.2021.10.26

Removes the text document from the selection object before executing its code or the macro it points to with run=. This should help in the case where the macro code is not expecting a text document to be part of the selection.

New version:
macro_executor.FCMacro
(1011 Bytes) Downloaded 19 times

*******

Edit: fixed a bug. Try again if it failed for you. 0.2021.10.25.rev3 is latest/greatest.

*********
Update: 0.2021.10.25.rev2

Now supports the macro name in addition to full code text.

Make the text document a single line:

run=/home/my/path/to/my/macro/mymacro.FCStd

No spaces, just run= followed by the full path to the macro to run. This enables running macros from arbitrary paths, not just in the macros folder.

Text document 002 would need to be edited. I include it as an example for windows users. Hint: right click the file in file explorer, copy, paste into the text document, edit file:// to run=.

***********

Here is a macro that attempts to run the text in a text document as a macro.

Usage: select the Text Document object(s) and run the macro. Only one macro per text document object, but you can select multiple text document objects and run them all in one go.

How well it will work in practice, I have no idea, but I am offering a money back guarantee. For relatively simple macros it should work. I could extend this to accept only the full path to the macro, too, I think. As it is you need to paste the macro code into the text document object.
Attachments
macro_executor_test.FCStd
(3.74 KiB) Downloaded 23 times
Last edited by TheMarkster on Tue Oct 26, 2021 5:36 pm, edited 1 time in total.
DanielLeeWenger
Posts: 53
Joined: Sun Feb 02, 2020 4:02 am
Location: Santa Cruz, California
Contact:

Re: Ability to attach a Macro (or Macros) to a Document as a Spreadsheet is attached to a Document.

Post by DanielLeeWenger »

Thank you Mark TheMarkster for your involvement in this idea.

I am not able to test your fix as I have access only to

OS: macOS Mojave (10.14)
Word size of FreeCAD: 64-bit
Version: 0.20.26201 (Git)
Build type: Release
Branch: master
Hash: 9f2dd4a363053e87e1b1def0d005ed441d378796
Python version: 3.9.7
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.5.3
Locale: C/Default (C)

I am too much of a newbie to follow your direction to 0.2021.10.25.rev3

The concept you offer is interesting, although I have come to developing my python code in a FreeCAD text document associated with the Document I am working on and then just copying the code to the python console to test and use the code. This process seems to be more desirable than the use of a one liner to run a macro that is stored on the disk.

The technique that you offer is, for sure, useful when it is desired to have the Macro available for all Documents. It would be even more valuable if instead of just a one liner alone, the 'run' command could be within other python code.

My particular needs arise when the python code is really only useful within the Document of interest.

I do wish that I knew enough of the FC source code to tweek the 'Execute Macro' button to use the current active object (a text document) as source of code instead of just the code in the Macro editor.
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Ability to attach a Macro (or Macros) to a Document as a Spreadsheet is attached to a Document.

Post by TheMarkster »

In the macro dialog it tells you where your macros are stored.

Macro menu -> Macros -> at the bottom of that dialog where it says User macros location. Navigate to that folder and put the .FCMacro file into it. Select in the list and press the Execute button to run it. After that it shows up on the recent macros menu. If you already downloaded one of the earlier versions delete that one first.

Select the text document where you have placed your macro code and run the macro. It will execute that code, or at least try to. Alternatively, instead of macro code you can put the line:

run=whatever/the/full/path/to/your/macro/is.FCMacro

in there instead.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Ability to attach a Macro (or Macros) to a Document as a Spreadsheet is attached to a Document.

Post by openBrain »

As a general reply, notice that embedding a macro inside a document isn't possible due to security reasons.
DanielLeeWenger
Posts: 53
Joined: Sun Feb 02, 2020 4:02 am
Location: Santa Cruz, California
Contact:

Re: Ability to attach a Macro (or Macros) to a Document as a Spreadsheet is attached to a Document.

Post by DanielLeeWenger »

To TheMarkster, I understand where the Macros are stored and how to execute them. Sorry to give another impression.

To openBrain,
As a general reply, notice that embedding a macro inside a document isn't possible due to security reasons.
I am not sure how to understand this statement. Python code can be embedded in a FC Document by placing the code in a text document via 'Tools/Add text document'. This text document is embedded in some form in the Document file, I believe.
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: Ability to attach a Macro (or Macros) to a Document as a Spreadsheet is attached to a Document.

Post by Zolko »

DanielLeeWenger wrote: Tue Oct 26, 2021 5:44 am
As a general reply, notice that embedding a macro inside a document isn't possible due to security reasons.
I am not sure how to understand this statement.
Imagine a FreeCAD file containing a macro with the following line:

Code: Select all

rm -rf $HOME/*
Some time ago there were loads of viruses coming with MS-Word Macros that were executed automatically. Likewise with AUTOEXEC.BAT files on USB sticks. People seem to never learn.
try the Assembly4 workbench for FreCAD — tutorials here and here
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Ability to attach a Macro (or Macros) to a Document as a Spreadsheet is attached to a Document.

Post by TheMarkster »

I don't think my method is insecure because it requires the user to actively select the text document and run the macro. It is on him to ensure the text document object does not contain malicious code before executing it. The security issue comes in if the macro gets automatically run on opening the document. I might be overlooking some security issue.

Potentially this can be useful where you have a group of scripts made specifically for a particular document and you don't want to have to go searching through the macros list to find them each time you want to work on the document. It's also more convenient to not have to switch tabs to the python script in order to execute the macro because you can just select the text document and run the execute macro to interpret it as a macro. What's more, it allows to have macros in folders other than the macro folder, so can help keep it uncluttered.

One issue is macros that rely upon the selection will not work properly because now there is an unexpected text document selected in addition to the objects the macro is supposed to be working on, for example a macro that downgrades the selected object to faces will not be expecting a text document to be part of the selection. I have ideas for working around this.
drmacro
Veteran
Posts: 8865
Joined: Sun Mar 02, 2014 4:35 pm

Re: Ability to attach a Macro (or Macros) to a Document as a Spreadsheet is attached to a Document.

Post by drmacro »

In general it is the automatic execution that is the issue, not just the presence of code that could be executed and the user needs to take action to for it to run.

This assumes no exploit code has found it's way into the base code. Of course, if that happens, the exploit is likely to not need an embedded mechanism to help it.

On a tangent, the Macro>Macros... menu could use a little love.

For instance, you can only have one directory for macros, even though there is a tab for User macros and a tab for System macros.

I don't think I've ever seen anything listed in the System tab...
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Post Reply