+ further work on DraftSnap

git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5325 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d
This commit is contained in:
yorikvanhavre 2011-12-19 15:17:22 +00:00
parent 92233011a9
commit 7ed0b0ae49
3 changed files with 73 additions and 50 deletions

View File

@ -924,24 +924,49 @@ class DraftToolBar:
self.textline -= 1
self.textValue.setText(self.textbuffer[self.textline])
def displayPoint(self, point, last=None, plane=None):
def displayPoint(self, point, last=None, plane=None, mask=None):
"this function displays the passed coords in the x, y, and z widgets"
# get coords to display
dp = point
if self.isRelative.isChecked() and (last != None):
if plane:
dp = plane.getLocalCoords(FreeCAD.Vector(point.x-last.x, point.y-last.y, point.z-last.z))
else:
dp = FreeCAD.Vector(point.x-last.x, point.y-last.y, point.z-last.z)
# set widgets
self.xValue.setText("%.2f" % dp.x)
self.yValue.setText("%.2f" % dp.y)
if self.zValue.isEnabled(): self.zValue.setText("%.2f" % dp.z)
if self.xValue.isEnabled():
self.zValue.setText("%.2f" % dp.z)
# set masks
if mask == "x":
self.xValue.setEnabled(True)
self.yValue.setEnabled(False)
self.zValue.setEnabled(False)
self.xValue.setFocus()
self.xValue.selectAll()
else:
elif mask == "y":
self.xValue.setEnabled(False)
self.yValue.setEnabled(True)
self.zValue.setEnabled(False)
self.yValue.setFocus()
self.yValue.selectAll()
elif mask == "z":
self.xValue.setEnabled(False)
self.yValue.setEnabled(False)
self.zValue.setEnabled(True)
self.zValue.setFocus()
self.zValue.selectAll()
else:
self.xValue.setEnabled(True)
self.yValue.setEnabled(True)
self.zValue.setEnabled(True)
self.xValue.setFocus()
self.xValue.selectAll()
def getDefaultColor(self,type,rgb=False):
"gets color from the preferences or toolbar"
if type == "snap":
@ -1045,7 +1070,6 @@ class DraftToolBar:
#---------------------------------------------------------------------------
def setWatchers(self):
print "adding draft widgets to taskview..."
class DraftCreateWatcher:
def __init__(self):
self.commands = ["Draft_Line","Draft_Wire",
@ -1074,7 +1098,6 @@ class DraftToolBar:
def shouldShow(self):
return True
print "setting tray"
self.traywidget = QtGui.QWidget()
self.tray = QtGui.QVBoxLayout(self.traywidget)
self.tray.setObjectName("traylayout")

View File

@ -82,14 +82,22 @@ class Snapper:
'ortho':':/icons/Constraint_Perpendicular.svg',
'intersection':':/icons/Constraint_Tangent.svg'}
def snap(self,screenpos,lastpoint=None,active=True,constrain=None):
"""snap(screenpos,lastpoint=None,active=True,constrain=None): returns a snapped
def snap(self,screenpos,lastpoint=None,active=True,constrain=False):
"""snap(screenpos,lastpoint=None,active=True,constrain=False): returns a snapped
point from the given (x,y) screenpos (the position of the mouse cursor), active is to
activate active point snapping or not (passive), lastpoint is an optional
other point used to draw an imaginary segment and get additional snap locations. Constrain can
be set to 0 (horizontal) or 1 (vertical), for more additional snap locations.
be True to constrain the point against the closest working plane axis.
Screenpos can be a list, a tuple or a coin.SbVec2s object."""
def cstr(point):
"constrains if needed"
if constrain:
return self.constrain(point,lastpoint)
else:
self.unconstrain()
return point
# type conversion if needed
if isinstance(screenpos,list):
screenpos = tuple(screenpos)
@ -139,7 +147,7 @@ class Snapper:
# nothing has been snapped, check fro grid snap
point = self.snapToGrid(point)
return point
return cstr(point)
else:
@ -148,11 +156,11 @@ class Snapper:
obj = FreeCAD.ActiveDocument.getObject(info['Object'])
if not obj:
return point
return cstr(point)
if hasattr(obj.ViewObject,"Selectable"):
if not obj.ViewObject.Selectable:
return point
return cstr(point)
if not active:
@ -229,9 +237,9 @@ class Snapper:
self.tracker.setMarker(self.mk[winner[1]])
self.tracker.on()
self.setCursor(winner[1])
# return the final point
return winner[2]
return cstr(winner[2])
def snapToExtensions(self,point,last):
"returns a point snapped to extension or parallel line to last object, if any"
@ -453,13 +461,17 @@ class Snapper:
point = Vector(point)
# setup trackers if needed
if not self.constrainLine:
self.constrainLine = DraftTrackers.lineTracker(dotted=True)
# setting basepoint
if not basepoint:
if not self.basepoint:
self.basepoint = point
else:
self.basepoint = basepoint
delta = point.sub(basepoint)
delta = point.sub(self.basepoint)
# setting constraint axis
self.affinity = FreeCAD.DraftWorkingPlane.getClosestAxis(delta)
@ -484,12 +496,13 @@ class Snapper:
npoint = self.basepoint.add(cdelta)
# setting constrain line
if point != npoint:
self.constrainLine.p1(point)
self.constrainLine.p2(npoint)
self.constrainLine.on()
else:
self.constrainLine.off()
if self.constrainLine:
if point != npoint:
self.constrainLine.p1(point)
self.constrainLine.p2(npoint)
self.constrainLine.on()
else:
self.constrainLine.off()
return npoint

View File

@ -117,16 +117,20 @@ def getPoint(target,args,mobile=False,sym=False,workingplane=True):
point. If sym=True, x and y values stay always equal. If workingplane=False,
the point wont be projected on the Working Plane.
'''
ui = FreeCADGui.draftToolBar
view = FreeCADGui.ActiveDocument.ActiveView
# point = view.getPoint(args["Position"][0],args["Position"][1])
# point = snapPoint(target,point,args["Position"],hasMod(args,MODSNAP))
# get point
if target.node:
last = target.node[-1]
else:
last = None
point = FreeCADGui.Snapper.snap(args["Position"],lastpoint=last,active=hasMod(args,MODSNAP))
amod = hasMod(args,MODSNAP)
cmod = hasMod(args,MODCONSTRAIN)
point = FreeCADGui.Snapper.snap(args["Position"],lastpoint=last,active=amod,constrain=cmod)
# project onto working plane if needed
if (not plane.weak) and workingplane:
# working plane was explicitely selected - project onto it
viewDirection = view.getViewDirection()
@ -143,21 +147,13 @@ def getPoint(target,args,mobile=False,sym=False,workingplane=True):
else:
point = plane.projectPoint(point, viewDirection)
ctrlPoint = Vector(point)
if (hasMod(args,MODCONSTRAIN)): # constraining
if mobile and (target.constrain == None):
target.node.append(point)
point = constrainPoint(target,point,mobile=mobile,sym=sym)
else:
target.constrain = None
ui.xValue.setEnabled(True)
ui.yValue.setEnabled(True)
ui.zValue.setEnabled(True)
mask = FreeCADGui.Snapper.affinity
if target.node:
if target.featureName == "Rectangle":
ui.displayPoint(point, target.node[0], plane=plane)
ui.displayPoint(point, target.node[0], plane=plane, mask=mask)
else:
ui.displayPoint(point, target.node[-1], plane=plane)
else: ui.displayPoint(point, plane=plane)
ui.displayPoint(point, target.node[-1], plane=plane, mask=mask)
else: ui.displayPoint(point, plane=plane, mask=mask)
return point,ctrlPoint
def getSupport(args):
@ -383,7 +379,7 @@ class Line(Creator):
'MenuText': QtCore.QT_TRANSLATE_NOOP("Draft_Line", "Line"),
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Draft_Line", "Creates a 2-point line. CTRL to snap, SHIFT to constrain")}
def Activated(self,name="Line"):
def Activated(self,name=str(translate("draft","Line"))):
Creator.Activated(self,name)
if self.doc:
self.obj = None
@ -430,20 +426,16 @@ class Line(Creator):
def action(self,arg):
"scene event handler"
if arg["Type"] == "SoKeyboardEvent":
# key detection
if arg["Key"] == "ESCAPE":
self.finish()
elif arg["Type"] == "SoLocation2Event": #mouse movement detection
elif arg["Type"] == "SoLocation2Event":
# mouse movement detection
point,ctrlPoint = getPoint(self,arg)
self.ui.cross(True)
self.linetrack.p2(point)
# Draw constraint tracker line.
if hasMod(arg,MODCONSTRAIN):
self.constraintrack.p1(point)
self.constraintrack.p2(ctrlPoint)
self.constraintrack.on()
else:
self.constraintrack.off()
elif arg["Type"] == "SoMouseButtonEvent":
# mouse button detection
if (arg["State"] == "DOWN") and (arg["Button"] == "BUTTON1"):
if (arg["Position"] == self.pos):
self.finish(False,cont=True)
@ -457,11 +449,6 @@ class Line(Creator):
if (not self.isWire and len(self.node) == 2):
self.finish(False,cont=True)
if (len(self.node) > 2):
# DNC: allows to close the curve
# by placing ends close to each other
# with tol = Draft tolerance
# old code has been to insensitive
# if fcvec.equals(point,self.node[0]):
if ((point-self.node[0]).Length < Draft.tolerance()):
self.undolast()
self.finish(True,cont=True)