Advertisement
danfalck

Draft_Extend.py

Sep 29th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. from FreeCADGui import Selection
  2. from FreeCAD import Base
  3. from PyQt4 import QtCore
  4. import DraftGeomUtils
  5.  
  6. def extend(line1,line2,nearpoint):
  7.     '''extends a Draft Line to another line -very similar to extend command in AutoCAD and it's clones '''
  8.     edge1=line1.Object.Shape.Edges[0]
  9.     edge2=line2.Object.Shape.Edges[0]
  10.     #boundary =edge1,line to extend = edge2,nearpoint catches nearest vertex on line to extend
  11.     #find the intersection point first
  12.     intpt = DraftGeomUtils.findIntersection(edge1,edge2,True,True)
  13.     #find closest end point of line to extend
  14.     closestpointindex = DraftGeomUtils.findClosest(nearpoint,line2.Object.Points)
  15.     #extend the line from the closest point
  16.     if closestpointindex==0:
  17.         line2.Object.Start = ( intpt[0].x,intpt[0].y,intpt[0].z)
  18.     else:
  19.         line2.Object.End = ( intpt[0].x,intpt[0].y,intpt[0].z)
  20.  
  21. #selection stuff
  22.  
  23. class SelObserver:
  24.     selected=False
  25.  
  26.     def addSelection(self, doc, object, sub, point):
  27.         self.selected=True
  28.         self.point=point
  29.  
  30.  
  31. def QtWait(ms):
  32.     "QtWait(ms) Wait some time, leaving QT do its job."
  33.     timer=QtCore.QTimer()
  34.     timer.setSingleShot(True)
  35.     loop=QtCore.QEventLoop()
  36.     QtCore.QObject.connect(timer,QtCore.SIGNAL("timeout()"),loop,QtCore.SLOT("quit()"))
  37.     timer.start(ms)
  38.     loop.exec_(QtCore.QEventLoop.AllEvents)
  39.  
  40. def wait4sel(gate):
  41.     "wait4sel('gate') Wait for a selection filtered by the gate and return the clicked point"
  42.     so=SelObserver()
  43.     if (gate !=""):
  44.         Selection.addSelectionGate(gate)
  45.     Selection.addObserver(so)
  46.  
  47.     while (not so.selected):
  48.         QtWait(100)
  49.  
  50.     Selection.removeSelectionGate()
  51.     Selection.removeObserver(so)
  52.     return so.point
  53.  
  54. Msg("Select boundary Edges\r\n")
  55. pt=wait4sel("SELECT Part::Feature SUBELEMENT Edge")
  56. theObject=Gui.Selection.getSelectionEx()[0]
  57.  
  58. Msg("Select line to extend\r\n")
  59. pt2=wait4sel("SELECT Part::Feature SUBELEMENT Edge")
  60. theObject2=Gui.Selection.getSelectionEx()[0]
  61.  
  62. extend(theObject,theObject2,Base.Vector(pt2))
  63. App.activeDocument().recompute()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement