1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

New Draft Snap framework

Info about new implemented features, classes, modules or APIs.

New Draft Snap framework

Postby yorikvanhavre » Fri Dec 16, 2011 8:10 pm

In rev 5316 I more or less finished implementing the new Draft Snap system. The old all-in-one function is now a Snapper object that stays in FreeCADGui and can be called whenever, from anywhere. The Draft module is already using it, there might still be some little bugs or old functionality that is still not working, though (gonna fix it on the way).

To use it in your own scripts and macros, basically, you just need to do this:
Code: Select all
mysnappedpoint = FreeCADGui.Snapper.snap([mouseX,mouseY])

and all the rest (snapping, setting the cursor, etc...) is taken care of automatically. When you are finished, you just do:
Code: Select all
FreeCADGui.Snapper.off()

To turn off all the screen stuff (grid, cross cursor, etc).

Here goes a little macro that illustrates how it works (it's a variation of the Line example from the wiki):
Code: Select all
import FreeCADGui, Part
from pivy.coin import *

class line:
    "this class will create a line after the user clicked 2 points on the screen"
    def __init__(self):
        self.view = FreeCADGui.ActiveDocument.ActiveView
        self.stack = []
        self.point = None

        # adding 2 callback functions
        self.callbackClick = self.view.addEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.click)
        self.callbackMove = self.view.addEventCallbackPivy(SoLocation2Event.getClassTypeId(),self.move)

    def move(self,event_cb):
        event = event_cb.getEvent()
        mousepos = event.getPosition()
        ctrl = event.wasCtrlDown()
        self.point = FreeCADGui.Snapper.snap(mousepos,active=ctrl)

    def click(self,event_cb):
        event = event_cb.getEvent()
        if event.getState() == SoMouseButtonEvent.DOWN:
            if self.point:
                self.stack.append(self.point)
            if len(self.stack) == 2:
                l = Part.Line(self.stack[0],self.stack[1])
                shape = l.toShape()
                Part.show(shape)
                self.view.removeEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.callbackClick)
                self.view.removeEventCallbackPivy(SoLocation2Event.getClassTypeId(),self.callbackMove)
                FreeCADGui.Snapper.off()

line()


Hope you like!
Yorik
yorikvanhavre
Site Admin
 
Posts: 3100
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil

Re: New Draft Snap framework

Postby jmaustpc » Sat Dec 17, 2011 2:36 am

I tried it, it works, I made a triangle! :)

thanks Yorik :)
jmaustpc
 
Posts: 2102
Joined: Tue Jul 26, 2011 6:28 am

Re: New Draft Snap framework

Postby sanguinariojoe » Sun Dec 18, 2011 4:37 pm

You are the best Yorik!

In order to reduce the FreeCAD developers knowledge maybe you can include two virtual methods at TaskView, for example:

needPoint()
Return True if 3D snapped points are requested, and all snap point stuff must be activated (FreeCADGui.Snapper.on() and maybe a toolbar to insert points). False otherwise.

newPoint(Base.Vector p)
Executed when user clicks on 3D view point, providing a snapped point (depending active snapping options).

When the TaskView is destroyed, callback are automatically destroyed too, and FreeCADGui.Snapper.off() is not needed to be called anymore.

What do you think?

Regards,
Jose Luis Cercós Pita
User avatar
sanguinariojoe
 
Posts: 144
Joined: Mon Jun 20, 2011 4:10 pm

Re: New Draft Snap framework

Postby yorikvanhavre » Mon Dec 19, 2011 12:01 pm

Hm good ideas... Indeed we should take it a step further and include taskview interaction. In either cases I wanted to refine a bit the Draft taskviews (they are a bit ugly now, they were simply derived from the draft toolbar), and make a couple of pre-made dialogs (get a 3d point, write a text, etc...)

About setting a callback system, that is interesting... We could have something like this: you call Gui.Snapper.getPoint(myfunc), which would setup and do everything, and call myfunc(3dpoint) when it's done... Would be dead simple to use!
Yorik
yorikvanhavre
Site Admin
 
Posts: 3100
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil

Re: New Draft Snap framework

Postby sanguinariojoe » Sat Dec 24, 2011 11:00 am

Runs really well! Congratulations!

I detected a problem when the snapper is not able to determine a snapped point, but some objects (a surface for example) has been clicked. Snapper returns:

Code: Select all
<type 'exceptions.KeyError'>
Traceback (most recent call last):
  File "/usr/Mod/Surfaces/surfISOCurve/PointTracker.py", line 56, in mouseMove
    point = Gui.Snapper.snap(screen, ctrl)
  File "/usr/Mod/Draft/DraftSnap.py", line 237, in snap
    self.tracker.setMarker(self.mk[winner[1]])
KeyError: None


And stop the function that calls to snap a point. I think that if snapper can't find a snapped point simply must return window point (View.getPoint(screen[0],screen[1])).

What do you think?

Regards,
Jose Luis Cercós Pita
User avatar
sanguinariojoe
 
Posts: 144
Joined: Mon Jun 20, 2011 4:10 pm

Re: New Draft Snap framework

Postby yorikvanhavre » Sat Dec 24, 2011 1:43 pm

Ok, I see, this is probably a geometry type I forgot to support... I'll fix that.
Yorik
yorikvanhavre
Site Admin
 
Posts: 3100
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil

Re: New Draft Snap framework

Postby yorikvanhavre » Sat Dec 24, 2011 4:34 pm

Okay I fixed that so if the edge type is not recognized it simply doesn't snap. Later on this can be extended.
I also added a new function to the Draft Snapper that just takes care of everything. Now, to get a 3D point on screen, you just need to do this:
Code: Select all
def clicked(point):
    print "got a 3D point: ", point

FreeCADGui.Snapper.getPoint(callback=clicked)

The clicked() function will be called when a point has been chosen on screen.
Yorik
yorikvanhavre
Site Admin
 
Posts: 3100
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil

Re: New Draft Snap framework

Postby yorikvanhavre » Mon Mar 12, 2012 8:32 pm

I just added a new Draft Snap toolbar according to what José did here: viewtopic.php?f=10&t=2323
The toolbar will appear whenever one of the Draft tools that use snapping is used (this can be disabled in the preferences). It allows to turn snapping locations on or off individually, or all at once. There is also a global freecad command (shortcut Shift+S) that does the same thing.

I also did a new set of snap icons, but I'm still not too happy with them... Maybe a thicker border?
Yorik
yorikvanhavre
Site Admin
 
Posts: 3100
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil


Return to Feature Announcements

Who is online

Users browsing this forum: No registered users and 1 guest