Advertisement
danfalck

cornerTrim.py

Sep 29th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 KB | None | 0 0
  1. #***************************************************************************                                                                    
  2. #*   Copyright (c) 2013                                                  
  3. #*   Daniel Falck <ddfalck@gmail.com>                                    
  4. #*                                                                        
  5. #*   This program is free software; you can redistribute it and/or modify
  6. #*   it under the terms of the GNU Lesser General Public License (LGPL)    
  7. #*   as published by the Free Software Foundation; either version 2 of    
  8. #*   the License, or (at your option) any later version.                  
  9. #*   for detail see the LICENCE text file.                                
  10. #*                                                                        
  11. #*   This program is distributed in the hope that it will be useful,      
  12. #*   but WITHOUT ANY WARRANTY; without even the implied warranty of        
  13. #*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        
  14. #*   GNU Library General Public License for more details.                
  15. #*                                                                      
  16. #*   You should have received a copy of the GNU Library General Public    
  17. #*   License along with this program; if not, write to the Free Software  
  18. #*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
  19. #*   USA                                                                  
  20. #***************************************************************************
  21. from FreeCADGui import Selection
  22. from FreeCAD import Base
  23. from PyQt4 import QtCore
  24. import DraftGeomUtils
  25.  
  26. def cornerTrim(line1,line2,nearpoint1,nearpoint2):
  27.     '''extends a Draft Line to another line -very similar to extend command in AutoCAD and it's clones '''
  28.     edge1=line1.Object.Shape.Edges[0]
  29.     edge2=line2.Object.Shape.Edges[0]
  30.     #boundary =edge1,line to extend = edge2,nearpoint catches nearest vertex on line to extend
  31.     #find the intersection point first
  32.     intpt = DraftGeomUtils.findIntersection(edge1,edge2,True,True)
  33.     #find index of closest end point of line to extend
  34.     closestpointindex1 = DraftGeomUtils.findClosest(nearpoint1,line1.Object.Points)
  35.     #extend the line from the closest point
  36.     if closestpointindex1==0:
  37.         line1.Object.Start = ( intpt[0].x,intpt[0].y,intpt[0].z)
  38.     else:
  39.         line1.Object.End = ( intpt[0].x,intpt[0].y,intpt[0].z)
  40.     closestpointindex2 = DraftGeomUtils.findClosest(nearpoint2,line2.Object.Points)
  41.     #extend the line from the closest point
  42.     if closestpointindex2==0:
  43.         line2.Object.Start = ( intpt[0].x,intpt[0].y,intpt[0].z)
  44.     else:
  45.         line2.Object.End = ( intpt[0].x,intpt[0].y,intpt[0].z)
  46.  
  47. #selection stuff
  48. class SelObserver:
  49.     selected=False
  50.     def addSelection(self, doc, object, sub, point):
  51.         self.selected=True
  52.         self.point=point
  53.  
  54. def QtWait(ms):
  55.     "QtWait(ms) Wait some time, leaving QT do its job."
  56.     timer=QtCore.QTimer()
  57.     timer.setSingleShot(True)
  58.     loop=QtCore.QEventLoop()
  59.     QtCore.QObject.connect(timer,QtCore.SIGNAL("timeout()"),loop,QtCore.SLOT("quit()"))
  60.     timer.start(ms)
  61.     loop.exec_(QtCore.QEventLoop.AllEvents)
  62.  
  63. def wait4sel(gate):
  64.     "wait4sel('gate') Wait for a selection filtered by the gate and return the clicked point"
  65.     so=SelObserver()
  66.     if (gate !=""):
  67.         Selection.addSelectionGate(gate)
  68.     Selection.addObserver(so)
  69.     while (not so.selected):
  70.         QtWait(100)
  71.  
  72.     Selection.removeSelectionGate()
  73.     Selection.removeObserver(so)
  74.     return so.point
  75.  
  76. Msg("Select boundary Edges\r\n")
  77. pt=wait4sel("SELECT Part::Feature SUBELEMENT Edge")
  78. theObject=Selection.getSelectionEx()[0]
  79.  
  80. Msg("Select line to extend\r\n")
  81. pt2=wait4sel("SELECT Part::Feature SUBELEMENT Edge")
  82. theObject2=Selection.getSelectionEx()[0]
  83.  
  84. cornerTrim(theObject,theObject2,Base.Vector(pt),Base.Vector(pt2))
  85. App.activeDocument().recompute()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement