Accessing and setting the snapping mode.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
davecoventry
Posts: 288
Joined: Mon Dec 09, 2013 8:11 pm
Location: Johannesburg
Contact:

Accessing and setting the snapping mode.

Post by davecoventry »

I want the user to pick the centre point of an arc in my script.

How can I save the current snapping point configuration, set the snapping to centre point only and then restore the snapping configuration to what it was?
~ Dave
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Accessing and setting the snapping mode.

Post by carlopav »

It can be done with the Snapper, but for a script maybe it's easier to get the center point from the curve, isn't it?

Code: Select all

s = Gui.Selection.getSelectionEx()[0]
o = s.SubObjects[0]
center = o.Curve.Center
follow my experiments on BIM modelling for architecture design
User avatar
davecoventry
Posts: 288
Joined: Mon Dec 09, 2013 8:11 pm
Location: Johannesburg
Contact:

Re: Accessing and setting the snapping mode.

Post by davecoventry »

Very likely.

However, the arc is part of a wire with multiple fillets.

I might just look at that approach, though.

Thanks for the help!
~ Dave
User avatar
davecoventry
Posts: 288
Joined: Mon Dec 09, 2013 8:11 pm
Location: Johannesburg
Contact:

Re: Accessing and setting the snapping mode.

Post by davecoventry »

Does anyone know how to manipulate the snapping modes?

Thanks.
~ Dave
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Accessing and setting the snapping mode.

Post by carlopav »

davecoventry wrote: Fri Apr 03, 2020 10:52 am However, the arc is part of a wire with multiple fillets.
Yup, if you select just the segment or the fillet you want to process, the code should work.

Anyway, I was pretty sure about knowing what to answer you until i started to write...
How does your script work? Does it setup a callback to trap mouse movement? if so you are on the right way to use Snapper, else perhaps I suggest to go back to my initial suggestion and use a script that process a subObject that would be far more easy.

The Snapper is part of the Draft module, in its Gui, so first you have to activate the Draft workbench.
When done, you can access the snapper trough:

Code: Select all

Gui.Snapper
this is the documentation vocx provided:

Code: Select all

    """Classes to manage snapping in Draft and Arch.

    The Snapper objects contains all the functionality used by draft
    and arch module to manage object snapping. It is responsible for
    finding snap points and displaying snap markers. Usually You
    only need to invoke it's snap() function, all the rest is taken
    care of.

    3 functions are useful for the scriptwriter: snap(), constrain()
    or getPoint() which is an all-in-one combo.

    The individual snapToXXX() functions return a snap definition in
    the form [real_point,marker_type,visual_point], and are not
    meant to be used directly, they are all called when necessary by
    the general snap() function.

    The Snapper lives inside FreeCADGui once the Draft module has been
    loaded.

    """
The main used function is:

Code: Select all

Gui.Snapper.getPoint(self, last=None, callback=None, movecallback=None, extradlg=None, title=None, mode="point")
this is how it works:

Code: Select all

        """
        getPoint([last],[callback],[movecallback],[extradlg],[title]) : gets a 3D point
        from the screen. You can provide an existing point, in that case additional
        snap options and a tracker are available.
        You can also pass a function as callback, which will get called
        with the resulting point as argument, when a point is clicked, and optionally
        another callback which gets called when the mouse is moved.

        If the operation gets cancelled (the user pressed Escape), no point is returned.

        Example:

        def cb(point):
            if point:
                print "got a 3D point: ",point

        FreeCADGui.Snapper.getPoint(callback=cb)

        If the callback function accepts more than one argument, it will also receive
        the last snapped object. Finally, a qt widget can be passed as an extra taskbox.
        title is the title of the point task box
        mode is the dialog box you want (default is point, you can also use wire and line)

        If getPoint() is invoked without any argument, nothing is done but the callbacks
        are removed, so it can be used as a cancel function.
        """
-------------------------------
Anyway. I suggest you to have a look at how Draft Tools implement Snap:
https://github.com/FreeCAD/FreeCAD/blob ... ls.py#L396
follow my experiments on BIM modelling for architecture design
User avatar
davecoventry
Posts: 288
Joined: Mon Dec 09, 2013 8:11 pm
Location: Johannesburg
Contact:

Re: Accessing and setting the snapping mode.

Post by davecoventry »

Thanks.

If I use the FreeCADGui.Snapper.snapToCenter() function, I need to provide the shape, I think?

It would be nice if the function could simply turn off the snaps except for the snapToCenter() one.
~ Dave
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Accessing and setting the snapping mode.

Post by carlopav »

davecoventry wrote: Wed Apr 08, 2020 2:05 pm Thanks.

If I use the FreeCADGui.Snapper.snapToCenter() function, I need to provide the shape, I think?

It would be nice if the function could simply turn off the snaps except for the snapToCenter() one.
I had in mind to implement this. I have a pending PR where I did some refactoring, perhaps in the near future we can think about it.
I was thinking about a special way to let you termporary enable just one snap and bring back the previous configuration after the user clicks a point. But I still do not have clear how to do it.
follow my experiments on BIM modelling for architecture design
User avatar
davecoventry
Posts: 288
Joined: Mon Dec 09, 2013 8:11 pm
Location: Johannesburg
Contact:

Re: Accessing and setting the snapping mode.

Post by davecoventry »

Thanks for your help.

I want the user to enter the points of the arc as a start point, centre of the arc and finish point and I thought a good way of doing this would be to enable the endpoint and centrepoint snapping.

Still, maybe in the future.
~ Dave
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Accessing and setting the snapping mode.

Post by onekk »

try to type in the Python console.

Code: Select all

dir(Part)
If Snapper if there are a decent python interface you will obtain like in the example above, the "exposed" method.

A way to inspect better the method, could be:

Supose you have the arc and the arc is in the object tree and it isi named say "arc0001"

Code: Select all

obj = FreeCAD.activeDocument.getObject("arc0001")
obj.
hitting the . (poin) will make a window appear and suggest the "completion", if you move the mouse over the method name in this window, sometimes appear a tooltip box with if you are lucky the "docstring" of the method, that sometimes is very useful.

Sadly there are many "sometimes, and if you are lucky enough", but better than nothing, is some case it is really useful, I have discovered many treasure in this way.

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