Detect when external geo is selected in Sketcher

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
freedman
Veteran
Posts: 3441
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Detect when external geo is selected in Sketcher

Post by freedman »

heda, I was hoping to find a standard method to use for Sketcher and Sketcher in general. I don't like using a work-around for something knowing that the next person that wants to do this will need to hack up something. What about all the other Sketcher icons.......

My long term goals are to get the FreeCAD GUI as good as it can be and as modifiably as I can get it, at some point I might need to build a branch but if I can get enough control then I can do it all in a macro. I really don't want to branch the source code.

I will read-up on pivy, maybe it's an option.
Thanks
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Detect when external geo is selected in Sketcher

Post by heda »

honourable goal, and all the best for the adventures ahead of you then.
paullee
Veteran
Posts: 5098
Joined: Wed May 04, 2016 3:58 pm

Re: Detect when external geo is selected in Sketcher

Post by paullee »

freedman
Veteran
Posts: 3441
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Detect when external geo is selected in Sketcher

Post by freedman »

Using left mouse button or ESC is a complexity of other issues. Esc is an option so I will need to monitor in Preferences, and then another way to exit External geo is to select any other icon in Sketcher, to do that I would need to monitor all the other icons. Simple is my best option or I will just bag the whole idea.
abdullah
Veteran
Posts: 4935
Joined: Sun May 04, 2014 3:16 pm
Contact:

Re: Detect when external geo is selected in Sketcher

Post by abdullah »

I do not know if it is possible to see from Python whether a command is active or not.

All functions have a command. Many commands have a drawsketchhandler. The latter is only in c++ inside ViewProviderSketch. I do not see how to get it from Python. This is why I am suggesting maybe the command is the way to go...
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Detect when external geo is selected in Sketcher

Post by heda »

freedman wrote: Mon Jun 21, 2021 7:43 pm I have considered looking at the cursor pointer, it changes when Exterrnal geo is selected and not. This could be a good choice because many of Sketchers functions change the cursor pointer. That looks hard.
seems not to be prohibitive hard though, once the entry point to a cursor that actually changes is traced down...

this catches all that is going on in the 3d regarding mouse, and whole app for keystrokes as it looks,
and that should be more than halfway there
just did simplest detection of cursor that had a chance to work, and it seems like it does, could do cross diagonals or something (like whole pixmap) if not.
to make it work with different colour schemes, suppose one would have to create the recognition sum at start-up, and adjust for actual pixmap size for full robustness.

this does not take the initial entry to the mode when done through toolbar, but that you had sorted out already...

the code seems to behave as expected when using keystrokes,
but much less so when using toolbar,
so that needs attention to make it overall well behaved.
like hammering out needed flags, up/down events, etc.
although it is not as expected (for whatever reason, either qt or sketcher c) it seems to be consistent, and then it should be possible to figure it out.

Code: Select all

view = Gui.activeDocument().activeView()
viewqt = view.graphicsView()
#cursor = viewqt.cursor()

class ViewObserver3D:
    """3d view events with purpose to detect mouse cursor images"""
    count = 0
    externalgeo = sum((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                       4292214784, 3551592448, 0, 4292214784, 0, 0, 0, 0,
                       0, 0, 0, 0, 0, 0, 0)) # diagonal pixels of pixmap

    def useraction(self, info):
        up = info['State'] == 'UP'
        if up:
            self.count += 1
            cursor = viewqt.cursor() # needs to be picked up every time
#            eventtype = info['Type']
#            print(eventtype)
            if 'Key' in info:
                print(info['Key'], cursor.pixmap())
            else:
                print(info['Button'], cursor.pixmap())
                
            # unfortunately the pixmap id seems to be same
            # so resorting to an arbitrary characteristic of pixmap
            # might need tweaks to be a robust detection
            pixm = cursor.pixmap()
            if not pixm.isNull():
                im = pixm.toImage()
                pixsum = sum((im.pixel(i, i) for i in range(32)))
                print(pixsum, pixsum == self.externalgeo)
                ## to see what is picked up
                #im.save('/location/curico{}'.format(count))


observer = ViewObserver3D()
cb = view.addEventCallback('SoButtonEvent', observer.useraction)

#view.removeEventCallback('SoButtonEvent', cb)

ps: would be nice though if there was something like "modeObserver" in the Sketcher reachable from python.
freedman
Veteran
Posts: 3441
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Detect when external geo is selected in Sketcher

Post by freedman »

I will look closer at that, maybe there is something unique. Thanks for the base-line code.
Post Reply