How to use macro with parameters?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
falviani
Posts: 250
Joined: Tue May 07, 2019 8:49 pm

How to use macro with parameters?

Post by falviani »

Hi,

The "weekly" version of Python I'm working with is:

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.21.29997 (Git)
Build type: Release
Branch: master
Hash: b52967d52ac46eff7c59e74d991f3f5b298944ef
Python 3.10.5, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.2
Locale: English/United States (en_US)
Installed mods: 
  * FC_TabbedBoxWorkbench
  * LCInterlocking
I have a macro I've downloaded that is expecting to be passed an object to work on. It is in its own file in the Macro folder, and shown in the Macro window from the menu.

Code: Select all

#Sample of macro header
def dumpObj(obj, maxlen=77, lindent=24, maxspew=600):
# many lines of code
I can't seem to find instructions in the wiki on how to invoke the macro from the console window with an object. If I have an object "Pad" in the Part Design model tree, the obvious approach in the console window:

Code: Select all

pa = App.ActiveDocument.getObject("Pad")
 dumpObj(pa)
doesn't work. My knowledge of Python is rather rusty - I'm a retired programmer who hasn't used Python in a number of years, and this is my first attempt at using macros in FreeCAd.

Thanks in advance!!
Frank
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to use macro with parameters?

Post by onekk »

Hello.

The answer is it depends.

What is returning the macro?

Is macro supposed to operate on an object of what type?

Where macro print his output (if there is any)?

have you active the "Report View"? Usually macro print something in this window.


So without knowing what the macro is expected to receive as parameters and what is expected to do is hard to tell.

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/
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: How to use macro with parameters?

Post by heda »

with the file "itest.py" (note .py not .FCMacro) in your designated macro folder (which you find with App.getUserMacroDir(True))

Code: Select all

def foo(a):
    print(a)
in console do

Code: Select all

>>> from itest import foo
>>> foo(2)
2
>>> 
falviani
Posts: 250
Joined: Tue May 07, 2019 8:49 pm

Re: How to use macro with parameters?

Post by falviani »

Hi Heda,

No matter what I do, trying to follow your instructions, it does not work.
The macro directory is:

Code: Select all

>>> App.getUserMacroDir(True)
'C:/Users/frank/AppData/Roaming/FreeCAD/Macro/'
I get the following error:

Code: Select all

>>> from printDictP3 import dumpObj
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "E:\FreeCAD\FreeCAD_weekly-builds-29997-2022-08-07-conda-Windows-x86_64-py310\FreeCAD_weekly-builds-29997-2022-08-07-conda-Windows-x86_64-py310\bin\Lib\site-packages\shiboken2\files.dir\shibokensupport\feature.py", line 139, in _import
    return original_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'printDictP3'
I tried doing an oschdir() to the macro directory, with the same result.

Any idea of what I've missed?

Thanks in advance,
Frank
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to use macro with parameters?

Post by onekk »

Macro is not a module, so it is not found by the importer.

You have to explicit append the module to sys.path.

Obviously with full path so:

Code: Select all


DirModPath = 'C:/Users/frank/AppData/Roaming/FreeCAD/Macro/'


if DirModPath not in sys.path:
    sys.path.insert(-1, DirModPath)
else:
    pass

import module

obviously the path could be any and not only the Macro directory.

The module must be importable, so probably a Macro has some chance to work, as it is made to be executed and not used "as a module", if the Macro expose some methods they could be used if not and is a simple sequence of commands, it will not expose any method so it is no usable "as a module".

Hope it helps.

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/
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: How to use macro with parameters?

Post by heda »

bummer, no - not really any clue - it worked on my box (not windows though)...
could of course be something silly like spelling/capitals...

did not need to append anything to sys.path, the macro folder was already there (on my box, so guess this is valid for all)...
did not need to add any __init__.py (suppose that is because fc has partly circumvented the vanilla import engine of py - afaikt)

na, only thing I did was to change file ending to ".py" (not "mymacro.FCMacro.py" but "mymacro.py")

so how does it look when you do

Code: Select all

os.listdir(App.getUserMacroDir(True))
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to use macro with parameters?

Post by onekk »

heda wrote: Tue Oct 04, 2022 4:32 pm ...
Hello, this is the result of the test code on my 0.20.1 AppImage.

Code: Select all

import os
>>> os.listdir(App.getUserMacroDir(True))
[]
>>> 
So probably there is something different across versions.

But it depends, as I'm using an AppIMage and using a different user directory for each version and language of FC I have installed, so probably it is my side that is "strange".

This is what I have in the "User macro location" using my "custom user directory".

Code: Select all

/home/common/FreeCAD/config-ap
The code I've posted is the code I use when dealing with scripting that has to import "libraries" in other word "collections of methods" that I use to do modelling.

So it is working, as I use it in my "strange environment" every day.

But is Linux, so on Windows I could not be sure at 100%.

Code: Select all

OS: Artix Linux (openbox)
Word size of FreeCAD: 64-bit
Version: 0.20.1.29410 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0.20.1)
Hash: f5d13554ecc7a456fb6e970568ae5c74ba727563
Python 3.10.5, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.2
Locale: Italian/Italy (it_IT)
Installed mods: 
  * Assembly4 0.12.4
  * toSketch 1.0.1
  * Curves 0.5.8
FC has not modified the import mechanism, there is a layer of abstraction, as the interpreter is running "inside FC" but usually the only drawback is that when dealing with WB as example is difficult to circumvent the "cache mechanism" so if you change sources sometimes they are not reloaded even if you use the importlib hack.

Kind 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/
falviani
Posts: 250
Joined: Tue May 07, 2019 8:49 pm

Re: How to use macro with parameters?

Post by falviani »

Hi,

The contents of the macro directory are listed, but the macro directory itself is NOT in the sys.path. I would consider this a bug, honestly. I have written a fragment I can copy into the console to make macros available.

I am now able to start converting this Python 2 macro for a general object to a Python 3 macro that can deal with the guts on a FreeCAD object. Many parts of an object, such as a body, are already functional, but other objects, such as a Feature, will need further work. But at least progress is possible.

I am doing this to try and make progress on a workbench I have started to generate everything needed to cut out a tabbed workbench on a CNC router. There are existing tools to generate SVGs for use with a laser cutter, but those don't seem to be able to handle the significant diameter of an end mill, which is often 2mm - 6mm in diameter. However, a typical 10W diode laser engraver is a poor choice for working with wood 1/4" or more thick. The documentation on the internals of a sketch structure, or of the Path workbench, are effectively non-existent. I have generated the dev docs from the GitHub sources, but those are (for me at least) useless for learning "how the pieces work together".

Thanks for your help. If I can get this macro updated, perhaps I should figure out how to make it available through the add-on manager.

-Frank
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: How to use macro with parameters?

Post by heda »

cool that you got it working.

not so cool that 3 different boxes behaves different...

anyways - hope you have less speedbumps for the rest of your journey
Post Reply