Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #***************************************************************************
- #* *
- #* Copyright (c) 2014 Daniel Falck <ddfalck@gmail.com> *
- #* *
- #* This program is free software; you can redistribute it and/or modify *
- #* it under the terms of the GNU Lesser General Public License (LGPL) *
- #* as published by the Free Software Foundation; either version 2 of *
- #* the License, or (at your option) any later version. *
- #* for detail see the LICENCE text file. *
- #* *
- #* This program is distributed in the hope that it will be useful, *
- #* but WITHOUT ANY WARRANTY; without even the implied warranty of *
- #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- #* GNU Library General Public License for more details. *
- #* *
- #* You should have received a copy of the GNU Library General Public *
- #* License along with this program; if not, write to the Free Software *
- #* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
- #* USA *
- #* *
- #***************************************************************************
- '''
- Module to extract useable curve geometry from FreeCAD to CAM scripts such as HeeksCNC
- rev2 - added way of extracting edges from faces and wires.
- 11/09/14
- '''
- from DraftGeomUtils import sortEdges, findMidpoint,findWires, isReallyClosed, isClockwise
- from FreeCAD import Vector,Base,Part
- import FreeCADGui
- import FreeCAD
- import FreeCAD as App
- import FreeCADGui as Gui
- #from PyQt4 import QtGui,QtCore
- from PySide import QtGui,QtCore
- #cw ccw functions
- def segments(poly):
- """A sequence of (x,y) numeric coordinates pairs """
- return zip(poly, poly[1:] + [poly[0]])
- def check_clockwise(poly):
- clockwise = False
- if (sum(x0*y1 - x1*y0 for ((x0, y0), (x1, y1)) in segments(poly))) < 0:
- clockwise = not clockwise
- return clockwise
- #'''
- #poly = [(2,2),(6,2),(6,6),(2,6)]
- #check_clockwise(poly)
- #False
- # l1 = []
- #for v in wire1.Vertexes:
- # l1.append((v.X,v.Y))
- #check_clockwise(l1)
- #'''
- #doc = App.activeDocument()
- #grp1 = doc.getObject("outer_profile")
- #sel = grp1.OutList
- def getGeom():
- sel=Gui.Selection.getSelection()
- group= sel[0]
- geom = group.Group
- name = group.Label
- objs = []
- item = "# ***** "+name+" *****\n"
- edges=[]
- points = []
- for s in geom:
- if s.Shape.ShapeType =='Vertex':
- points.append(s)
- elif s.Shape.ShapeType =='Wire':
- edges.extend(s.Shape.Edges)
- elif s.Shape.ShapeType =='Face':
- edges.extend(s.Shape.OuterWire.Edges)
- else:
- #objs.append(s.Object)
- edges.append(s.Shape)
- sorted_edges = []
- sorted_edges = sortEdges(edges)
- wire1 = findWires(sorted_edges)
- #find direction of wire for milling purposes (inside profile, outside profile etc)
- w1 = wire1[0]
- l1 = []
- for v in w1.Vertexes:
- l1.append((v.X,v.Y))
- if check_clockwise(l1):
- FreeCAD.Console.PrintMessage('clockwise path')
- else:
- FreeCAD.Console.PrintMessage('counter clockwise path')
- # def isSameVertex(V1, V2):#borrowed from yorik's fcgeo.py- thanks yorik!
- # ''' Test if vertexes have same coordinates with precision 10E(-precision)'''
- # if round(V1.X-V2.X,1)==0 and round(V1.Y-V2.Y,1)==0 and round(V1.Z-V2.Z,1)==0 :
- # return True
- # else :
- # return False
- start=sorted_edges[0]
- end=sorted_edges[-1]
- startingZ = start.Vertexes[0].Z
- #set starting depth to same Z as starting curve element
- #self.form.lineEditStartDepth.setText(str(start.Vertexes[0].Z))
- item += name+" = area.Curve()\n"
- if isReallyClosed(wire1[0]):
- item += '#closed path\n'
- path = 'closedpath'
- else:
- item += '#open path\n'
- path = 'openpath'
- # if isSameVertex(start.Vertexes[0],end.Vertexes[1]) :
- # item += '#closed path\n'
- # path = 'closedpath'
- # else:
- # item += '#open path\n'
- # path = 'openpath'
- if path == 'openpath' :
- item += name+".append(area.Point(" + str(start.Vertexes[0].X) + "," + str(start.Vertexes[0].Y)+ "))\n"
- for s in sorted_edges:
- #edges.append(s)
- if (isinstance(s.Curve,Part.Circle)):
- mp = findMidpoint(s)
- ce = s.Curve.Center
- # tang1 = s.Curve.tangent(s.ParameterRange[0]) ; tang2 = s.Curve.tangent(s.ParameterRange[1])
- # cross1 = Vector.cross(Base.Vector(tang1[0][0],tang1[0][1],tang1[0][2]),Base.Vector(tang2[0][0],tang2[0][1],tang2[0][2]))
- #look at isClockwise in DraftGeomUtils.py
- # if cross1[2] > 0:
- if isClockwise(s):
- direct = '1 ' #we seem to be working in a rh system in FreeCAD
- else:
- direct = '-1 '
- item += name+".append(area.Vertex("+str(direct)+ ", area.Point( "+ str(s.Vertexes[-1].Point[0])+", "+str(s.Vertexes[-1].Point[1])+ "), area.Point("+str(s.Curve.Center [0])+ ", " + str(s.Curve.Center[1])+ ")))\n"
- elif (isinstance(s.Curve,Part.Line)):
- item += name+".append(area.Point( "+str(s.Vertexes[-1].Point[0])+", " +str(s.Vertexes[-1].Point[1])+ "))\n"
- else:
- pass
- if path == 'closedpath':
- item += name+".append(area.Point(" + str(start.Vertexes[1].X) + "," + str(start.Vertexes[1].Y)+ "))\n"
- item+= name+".Reverse()\n"
- #return item
- if points:
- item+= "kurve_funcs.make_smaller( "+ name+ ", start = area.Point(" + str(points[0].Shape.X)+","+str(points[0].Shape.Y)+"))\n"
- item+= name+"_startparams = {'key':'value'}\n"
- item+= name+"_startparams['startpt'] = True\n"
- item+= name+"_startparams['startptX'] = "+str(points[0].Shape.X)+"\n"
- item+= name+"_startparams['startptY'] = "+str(points[0].Shape.Y)+"\n"
- clipboard = QtGui.QApplication.clipboard()
- clipboard.setText(item)
- getGeom()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement