Event Handler Order For New Objects in FreeCAD

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
SBlade
Posts: 40
Joined: Fri May 01, 2020 8:38 pm

Event Handler Order For New Objects in FreeCAD

Post by SBlade »

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.1.29410 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: f5d13554ecc7a456fb6e970568ae5c74ba727563
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.3
Installed mods: 
  * BIM 2021.12.0
  * Design456 0.0.1
  * Manipulator 1.4.5
DEar All,
I have the following code snippet which is working and executes in FC Python Console

Code: Select all

import draftguitools.gui_trackers as DraftTrackers
import FreeCADGui, FreeCAD, Part  

class _MyTracker(DraftTrackers.boxTracker):
    def __init__(self,val):
        self.pts = []
        self.pts.append(FreeCAD.Vector(1000,5000,0))
        self.tracker = DraftTrackers.boxTracker()
        self.tracker.width(200)
        self.tracker.height(50)
        self.tracker.length(100)
        self.tracker.on()
        FreeCAD.activeDocument().recompute()
        self.tracker.setRotation(FreeCAD.DraftWorkingPlane.getRotation().Rotation)
        FreeCADGui.Snapper.getPoint(callback=self.PointClick, movecallback=self.mousemove)
    def PointClick(self, point, extra):
        self.pts.append(point)
        print(self.pts)
        self.tracker.pos(point)
        
    def mousemove(self, point, extra):
        if len(self.pts) == 0: 
            return 
            print(len(self.pts))    
        if hasattr(FreeCAD,"DraftWorkingPlane"):
            delta = 2000
            
        #self.tracker.update([self.pts[0].add(delta),point.add(delta)])
        if len(self.pts) > 0 : 
            print( self.pts[0] )
        else: 
            print( len(self.pts) )
            Self.tracker.finalize()
       
    def execute(self, obj):
        """
         Code comment 
        """        
        if self.clone(obj):
            return 

a=FreeCAD.ActiveDocument.addObject("Part::Feature","Tracker")     
mtrack = _MyTracker(a)
But the problem with this is that: The code run only once, that is If I copy paste and press enter then code directly enters into mousemove event handler, but normally I would expect it to enter first on mouseclick handler, then on mousemove handler. But this never happens because all the handlers are run at once, when object is created. If you noticed on the code I wrote conditional if else statement at the beginning of mousemove handler to avoid of entering the program into mouvemove handler with no help at all.

Aside from that I'd like to emphasize that derived MyTracker directly from DraftTrackers.boxTracker Class, probably the way it handles was not appropriate in my case, can you shed some light on that ?

Regards,
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Event Handler Order For New Objects in FreeCAD

Post by onekk »

Hello, probably it is better to ask on "Python Scripting and Macro" section of this forum.

I don't know too much, but at the end if __init__ you call explicitly a fuction with a mousemove callback, and in your class there is no mouseclick method.

Side note when you are speaking about "Python Console" you intend that you copy and paste code in "Python Console" (The window on FreeCAD)?

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/
SBlade
Posts: 40
Joined: Fri May 01, 2020 8:38 pm

Re: Event Handler Order For New Objects in FreeCAD

Post by SBlade »

It is better to elaborate the thing that I'm trying to achieve: I simply mimic the behaviour of creating the wall object which is in Arch WB, as you know when we draw wall, boxtracker emerges and guides on 3D scene where we can place it with 2 mouse clicks where first is starting point and 2nd is where it ends and then wall object is placed in scene graph. I was trying to the the same box tracker (MyTracker) and then place my custom object with second mouse click where I couldn't achieve with my code snippet.

onekk wrote: Wed Feb 01, 2023 6:13 am Hello, probably it is better to ask on "Python Scripting and Macro" section of this forum.
Already notified to moderator to move post to Python Scripting Section
onekk wrote: Wed Feb 01, 2023 6:13 am I don't know too much, but at the end if __init__ you call explicitly a fuction with a mousemove callback, and in your class there is no mouseclick method.
I used __init__ to make sure that I don't miss any ancestor method which needs to be initialized accurately during object constructor. At least this is the standart way of doing it in stand alone programming, I believe this serves the same purpose as well here.

When it comes to the mouseclick do I really need to create mouseclick handle ? PointClick(self, point, extra) already produces the correct results and handles the mouseclicks accurately.

onekk wrote: Wed Feb 01, 2023 6:13 am Side note when you are speaking about "Python Console" you intend that you copy and paste code in "Python Console" (The window on FreeCAD)?
Yes
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Event Handler Order For New Objects in FreeCAD

Post by onekk »

SBlade wrote: Wed Feb 01, 2023 8:53 am It is better to elaborate the thing that I'm trying to achieve: I simply mimic the behaviour of creating the wall object which is in Arch WB, as you know when we draw wall, boxtracker emerges and guides on 3D scene where we can place it with 2 mouse clicks where first is starting point and 2nd is where it ends and then wall object is placed in scene graph. I was trying to the the same box tracker (MyTracker) and then place my custom object with second mouse click where I couldn't achieve with my code snippet.
...
Sadly I don't know very much Arch WB, even if one day I want to learn how to use it. I has contributed with some code some years ago, but my code was mainly to make mockup windows in a wall, the other part of code was done by other guys.

I usually made experiments and some more structured code simply making a text file with .py extension and loading in FreeCAD, it open in the "Macro Editor" (that in reality is a "Python Editor" with some syntax highlighting and some limited functionality) and experiment where, and usually I edit the code in a "real editor". When you save on disk a new version, FreeCAD is smart enough to detect the change and popup a window asking if you want to reload the code.

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/
Post Reply