pause macro till other macro is finished

Need help, or want to share a macro? Post here!
Chri1
Posts: 68
Joined: Wed Oct 17, 2018 9:00 am

pause macro till other macro is finished

Postby Chri1 » Wed Oct 09, 2019 6:38 pm

Hi

In a macro I use

Code: Select all

DrafTools.Rectangle
The problem is, that the code does not stop till rectangle is finished

Code: Select all

...
r=None
r = DraftTools.Rectangle().Activated()
###Here it should wait, till rectangle is finished
print("Rectangle finished") # This appears immediately before the rectangle exists  

... something with r, which causes error, because  r  does not exist yet
Would this be a solution?:
An Observer that watches keyboard and mouse and checks, weather "r" is not None
(Or weather "rec" is not None - When the rectangle is finished, it can be accessed by global variable rec, made by DraftTools)

Or is there a better solution?
vocx
Posts: 3923
Joined: Thu Oct 18, 2018 9:18 pm

Re: pause macro till other macro is finished

Postby vocx » Wed Oct 09, 2019 6:51 pm

Chri1 wrote:
Wed Oct 09, 2019 6:38 pm
...
Or is there a better solution?
Maybe you should describe what you really want to do, so other people can provide an alternative method.

The DraftTools commands are intended to be used by the buttons of the Draft Workbench. If you want to script the rectangle operation, I think it would be better to use the Draft.makeRectangle() function directly, instead of launching the graphical rectangle command.
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.
Chri1
Posts: 68
Joined: Wed Oct 17, 2018 9:00 am

Re: pause macro till other macro is finished

Postby Chri1 » Thu Oct 10, 2019 9:21 am

Before I can use Draft.makeRectangle(), I would to have to take care about userinput by my own (Observer,...), Drafttools-Rectangle does this anyway.
I could copy the code of Drafttools in an own macro and adjust it a little bit (insert a call to another function, after it's job is done) , but I'm reluctant to produce more code, if it's not necessary.

I also tried to make python wait (https://blog.miguelgrinberg.com/post/ho ... ython-wait).
In principle it worked, but after some time FreeCAD crashed. It's too uncertain for me, it's a bit too big for me.

After the rectangel is created, the code makes a lot of things with it (Add new properties, compare with other objects,..)
But before this, the rectangle has to exist.
vocx
Posts: 3923
Joined: Thu Oct 18, 2018 9:18 pm

Re: pause macro till other macro is finished

Postby vocx » Thu Oct 10, 2019 6:48 pm

Chri1 wrote:
Thu Oct 10, 2019 9:21 am
Before I can use Draft.makeRectangle(), I would to have to take care about userinput by my own (Observer,...), Drafttools-Rectangle does this anyway.
I'm not sure what you mean by Observer. Do you mean some sort of Coin callback that monitors the inputs, the position of the mouse, the 3D view, etc?
I could copy the code of Drafttools in an own macro and adjust it a little bit (insert a call to another function, after it's job is done) , but I'm reluctant to produce more code, if it's not necessary.
In my opinion this is the way to go.

I'd say graphical tools, like those in DraftTools, were not designed to be scriptable. You can launch them in a script, if you want, but I wouldn't recommend it. Only the tools under Draft.py are really scriptable. The DraftTools classes do all sorts of tricks with Qt interfaces and the Coin scenegraph, and in many cases they don't call directly the Draft.py functions, but set some sort of "delayed execution"; and yes, if you do things wrong, you end up with crashes.
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.
Chri1
Posts: 68
Joined: Wed Oct 17, 2018 9:00 am

Re: pause macro till other macro is finished

Postby Chri1 » Mon Oct 14, 2019 6:47 pm

I found a dirty solution:
Because
  • my macro requires, that a face of a prism has to be selected, before it starts.
  • When DraftTools.Rectangle().Activated() is finished, the new rectangle is selected and it has no subelements.
-> I made an observer (addEventCallback "SoEvent"), which observes:

Code: Select all

       ...
       sel = FreeCADGui.Selection.getSelectionEx()
       if len(sel) > 0:
           sel = sel[0].SubElementNames
           if len(sel)==0:
               v.removeEventCallback("SoEvent", c)
               print("Rectangle Finished")
               ... goOn()...
The observer could be formulated even more sharply, for the moment it is enough for me

Adjust the code of Drafttools:
vocx wrote:
Thu Oct 10, 2019 6:48 pm
In my opinion this is the way to go.
If it turns out that the dirty solution is unreliable, I will make this.
vocx
Posts: 3923
Joined: Thu Oct 18, 2018 9:18 pm

Re: pause macro till other macro is finished

Postby vocx » Mon Oct 14, 2019 10:33 pm

Chri1 wrote:
Mon Oct 14, 2019 6:47 pm
...
If it turns out that the dirty solution is unreliable, I will make this.
Well, I was thinking of making it unreliable because I wanted to rename the Rectangle class, so scripts like yours would break. This is the reason I think it's best to rely only on the Draft.make... functions.

Instead of re-implementing the DraftTools.Rectangle class, you may actually sub-class it. That is, define a new class that inherits most functions, and maybe just over override the Accept method to make the pause that you want (use a Coin callback). But again, I'd prefer to use Draft.makeRectangle directly when possible.
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.