Run a second Zen macro from main macro

Everything OAD-related that won't fit in the other categories: share general aspects of macro programming and discuss the OAD environment
Post Reply
user-6033
Posts: 89
Joined: Thu Jan 01, 1970 1:00 am

Run a second Zen macro from main macro

Post by user-6033 »

Hi,

I tried to use Zen.Application.RunMacro('My Macro') within an existing macro but the system complaints about recursively starting a macro. Is there a way to do that? If not, do you have any recommendation for a workaround? The OADs tend to consist of a mixture of Zen-specific commands and ironpython so as far as I can see it is not just to transform them into separate .py files which I could call from my main OAD macro.

Kind Regards
Fredrik Olsson
user-8
Posts: 63
Joined: Thu Jan 01, 1970 1:00 am

Post by user-8 »

Hi,

yes, you can place code in an external *.py file and import that as a module. Version 2.5 and higher lets you also use *.py instead *.czmac
user-6033
Posts: 89
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6033 »

Hi,

I made some tests. I started with an OAD script, removed intitial and end info within brackets and change file type to .py since in my Zen 2 Core v2.5 I could not see anyway to save the macro directly as a .py file. I then imported the py module into an OAD macro. Ordinary ironpython stuff in the py module works fine when calling functions but Zen-related commands fail. Any suggestion?

BR
Fredrik Olsson
user-4
Posts: 397
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

Hello Fredrik,

Zen.Application.RunMacro does not work under Zen Core and is not recommended in Zen blue.
Ordinary ironpython stuff in the py module works fine when calling functions but Zen-related commands fail.
In a module Zen is not defined and you have to pass Zen like a variable to the module function.
user-6033
Posts: 89
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6033 »

Hi,

Do you mean passing "Ze i s s . Micro . S c r i p t i n g . ZenWrapperLM"? and then define set Zen within the py-file which I have seen in some examples?

My main problem is that my OAD library tend to grow and needs to be separated into separate modules which can be called from a main OAD macro.

BR
Fredrik Olsson
user-4
Posts: 397
Joined: Thu Jan 01, 1970 1:00 am

Post by user-4 »

What I mean is something like

Code: Select all

class MyClass:
    def MyFunction(self, Zen):
       Zen.Windows.Show('Test')

myClass = MyClass()
myClass.MyFunction(Zen)
I hope this helps!
user-6033
Posts: 89
Joined: Thu Jan 01, 1970 1:00 am

Post by user-6033 »

It works excellent! Thanks!

BR
Fredrik Olsson
Dan Stevens
Posts: 7
Joined: Sat Aug 15, 2020 5:18 am

Re: Run a second Zen macro from main macro

Post by Dan Stevens »

Revisiting this post from long ago...

I have repeatedly tried this , and always get "No module named..." error when trying this.

For example, I have "temp.czmac";

Code: Select all

import temp1

myClass = MyClass()
myClass.MyFunction(Zen)
and also I have "temp1.czmac", saved in the same macro folder.

Code: Select all

class MyClass:
    def MyFunction(self, Zen):
       Zen.Windows.Show('Test')
I tried also saving the temp1.czmac in the Zen.exe folder, but no luck.

What obvious thing am I missing here?
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Run a second Zen macro from main macro

Post by CarlZeissMicroscopy3 »

Hello Dan,

there are mainly two ways to do it:

One file:

Code: Select all

class MyClass:
    def MyFunction(self, Zen):
       Zen.Windows.Show('Test')

myClass = MyClass()
myClass.MyFunction(Zen)
Two files:

MyClassLib.py

Code: Select all

class MyClass:
    def MyFunction(self, Zen):
       Zen.Windows.Show('Test')
MyClassProg.czmac

Code: Select all

from MyClassLib import MyClass

myClass = MyClass()
myClass.MyFunction(Zen)
For simplicity MyClassLib.py is in the same folder as MyClassProg.czmac

Now, starting MyClassProg should work.
Post Reply