Interrupt macro for userinput point

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

Interrupt macro for userinput point

Postby Chri1 » Fri May 10, 2019 2:53 pm

Hi

I want a macro / dialog be interrupted to allow the user to specify a point by pointing with the mouse.
I know "Observing mouse events in the 3D viewer via Python" but I want the snaps of existing objects to be caught.

(In AutoCAD/Lisp it would be getpoint)

Thanks for help

Chri1
User avatar
Joel_graff
Posts: 1545
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: Interrupt macro for userinput point

Postby Joel_graff » Sun May 12, 2019 10:59 am

I've never worked with snapping in Python, so I know nothing about it, I'm afraid.

However, snapping is managed in DraftSnap.py:

https://github.com/FreeCAD/FreeCAD/blob ... aftSnap.py

You might want to look at the source code there to get a feel for how it works. You can play with those functions from the FreeCAD console by importing DraftSnap in the console, then calling functions.
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
User avatar
Chris_G
Posts: 1146
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Interrupt macro for userinput point

Postby Chris_G » Sun May 12, 2019 12:08 pm

A selection observer should be good for that.
Copy/paste this in the python console and pick some objects in the 3D view to test.
Uncomment last line to remove observer.

Code: Select all

class my_selection_observer:
    """Selection observer"""
    def __init__(self):
        self.selection = []
    def addSelection(self,doc,obj,sub,pnt):
        self.selection.append(Gui.Selection.getSelectionObject(doc,obj,sub,pnt))
        print("%s.%s.%s picked at point %s"%(doc, obj, sub, pnt))
    def removeSelection(self,doc,obj,sub):
        self.selection.remove(Gui.Selection.getSelectionObject(doc,obj,sub))
    def clearSelection(self,doc):
        self.Selection = []

obs = my_selection_observer()
FreeCADGui.Selection.addObserver(obs)
#FreeCADGui.Selection.removeObserver(obs)

Chri1
Posts: 54
Joined: Wed Oct 17, 2018 9:00 am

Re: Interrupt macro for userinput point

Postby Chri1 » Mon May 13, 2019 10:54 am

Thanks
A first attempt worked out:

I typed in Phyton-Console:

Code: Select all

import DraftSnap

def cb(point):
    if point:
        print (point)
and with every

Code: Select all

FreeCADGui.Snapper.getPoint(callback=cb)
I get the the snapped point of an existing object
(Depending on the current snap-settings)

There seem to be a lot more options to discover

Chri1