Advertisement
danfalck

python_console_experiments_1

Nov 13th, 2011
3,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 68.43 KB | None | 0 0
  1. #these import statements are always good:
  2. import Part,Drawing,FreeCADGui,FreeCAD
  3. from FreeCAD import Base
  4.  
  5. #make a line
  6. l1 = Part.makeLine((0,1,0),(0,0,0))
  7. Part.show(l1)
  8. #do it with one line:
  9. Part.show(Part.makeLine((10,10,0),(0,0,0)))
  10.  
  11. #make a simple circle:
  12. c1 = Part.makeCircle(3,Base.Vector(0,1,0))
  13. Part.show(c1)
  14.  
  15. #make an arc with radius,center point, direction vector, start and end angle
  16. c1 = Part.makeCircle(10, Base.Vector(0,0,0), Base.Vector(0,0,1),0,90)
  17. Part.show(c1)
  18.  
  19. #make it a one liner:
  20. Part.show(Part.makeCircle(10, Base.Vector(0,0,0), Base.Vector(0,0,1),0,90))
  21.  
  22. #make a simple circle that is rotated about Y axis:
  23. c2 = Part.makeCircle(3,Base.Vector(0,1,0),Base.Vector(0,1,0))
  24. Part.show(c2)
  25.  
  26. #create an arc along points:
  27. arc = Part.Arc(Base.Vector(5,0,0),Base.Vector(0,5,0),Base.Vector(-5,0,0))
  28. arc_edge = arc.toShape()
  29. Part.show(arc_edge)
  30.  
  31. #creation of elements from lines and arcs:
  32. from FreeCAD import Base
  33. V1 = Base.Vector(0,10,0)
  34. V2 = Base.Vector(30,10,0)
  35. V3 = Base.Vector(30,-10,0)
  36. V4 = Base.Vector(0,-10,0)
  37. #create arc
  38. VC1 = Base.Vector(-10,0,0)
  39. C1 = Part.Arc(V1,VC1,V4)
  40. # and the second one
  41. VC2 = Base.Vector(40,0,0)
  42. C2 = Part.Arc(V2,VC2,V3)
  43. #two lines
  44. L1 = Part.Line(V1,V2)
  45. # and the second one
  46. L2 = Part.Line(V4,V3)
  47. S1 = Part.Shape([C1,C2,L1,L2])
  48. W = Part.Wire(S1.Edges)
  49. f = Part.Face(W)
  50. P = f.extrude(Base.Vector(0,0,2))
  51. Part.show(P)
  52.  
  53. v5 = Base.Vector(0,0,0)
  54. v6 = Base.Vector(30,0,0)
  55. l3 = Part.Line(V1,v5)
  56. l4 = Part.Line(v5,v6)
  57. l5 = Part.Line(v6,V2)
  58. s2 = Part.Shape([L1,l3,l4,l5])
  59.  
  60. Part.show(s2)
  61. w2 = Part.Wire(s2.Edges)
  62. f2 = Part.Face(w2)
  63.  
  64. #revolve a profile:
  65. p2 = f2.revolve(Base.Vector(0,0,0),Base.Vector(1,0,0),360) #revolve a closed shape 360 degrees
  66.  
  67.  
  68.  
  69. #select an object in the gui and use python to do something with it:
  70. s1 = Gui.Selection.getSelectionEx()
  71. box = s1.extrude(Base.Vector(0,0,2))
  72.  
  73. #select two objects and give the python console access to them:
  74. s=FreeCADGui.Selection.getSelection()
  75. try:
  76.     shape1=s[0].Shape
  77.     shape2=s[1].Shape
  78. except:
  79.     print "Wrong selection"
  80.  
  81. #select an object in the gui and parse it in python:
  82. for o in Gui.Selection.getSelectionEx():
  83.     print o.ObjectName
  84.     for s in o.SubElementNames:
  85.         print "name: ",s
  86.     for s in o.SubObjects:
  87.         print "object: ",s
  88.  
  89. #select an object in the gui and parse it in python:
  90. length = 0.0
  91. for o in Gui.Selection.getSelectionEx() :
  92.     for s in o.SubObjects:
  93.         length = s.Length
  94.         print "Length of the selected edges:" ,length
  95.  
  96. s.Vertexes[0].X, s.Vertexes[0].Y, s.Vertexes[0].Z
  97. s.Vertexes[1].X, s.Vertexes[1].Y, s.Vertexes[1].Z
  98.  
  99. for o in  Gui.Selection.getSelectionEx():
  100.     for s in o.SubObjects:
  101.         print s.Vertexes[0].X, s.Vertexes[0].Y, s.Vertexes[0].Z
  102.         print s.Vertexes[1].X, s.Vertexes[1].Y, s.Vertexes[1].Z
  103.  
  104. for obj in FreeCAD.ActiveDocument.Objects:
  105.     if hasattr(obj,"Shape"):
  106.         print "Object: ", obj, "Shape type: ", obj.Shape.Type
  107.  
  108.  
  109. for o in Gui.Selection.getSelectionEx():
  110.     print o.ObjectName, o.Type
  111.  
  112.  
  113.  
  114.  
  115. #selection stuff again:
  116. import FreeCAD,FreeCADGui,Part
  117.  
  118. sel = FreeCADGui.Selection.getSelection()
  119. if not sel:
  120.    FreeCAD.Console.PrintWarning("Select something first!")
  121. else:
  122.    elist = []
  123.    for obj in sel:
  124.        if hasattr(obj,"Shape"):
  125.            elist.append(obj.Shape.Edges[0])
  126.            print obj
  127.  
  128.  
  129. #make a pipe:
  130. z=theWire.makePipe(myFaceProfile)
  131.  
  132. from FreeCAD import Base
  133. V1 = Base.Vector(0,10,0)
  134. V2 = Base.Vector(30,10,0)
  135. V3 = Base.Vector(30,-10,0)
  136. V4 = Base.Vector(0,-10,0)
  137. VC1 = Base.Vector(-10,0,0)
  138. C1 = Part.Arc(V1,VC1,V4)
  139. # and the second one
  140. VC2 = Base.Vector(40,0,0)
  141. C2 = Part.Arc(V2,VC2,V3)
  142. L1 = Part.Line(V1,V2)
  143. # and the second one
  144. L2 = Part.Line(V4,V3)
  145. S1 = Part.Shape([C1,C2,L1,L2])
  146. W = Part.Wire(S1.Edges)
  147. Part.show(W)
  148.  
  149. V5 = Base.Vector(0,10,10)
  150. V6 = Base.Vector(0,10,20)
  151. L3 = Part.Line(V1,V5)
  152. L4 = Part.Line(V5,V6)
  153.  
  154. S2 = Part.Shape([L3,L4])
  155. W2 = Part.Wire(S2.Edges)
  156. Part.show(W2)
  157. pipe1=W2.makePipe(W)
  158. Part.show(pipe1)
  159.  
  160.  
  161. makeSolid=1 #change to 1 to make a solid, but it will take a while
  162. isFrenet=1
  163. pipe2 = Part.Wire(W2).makePipeShell([W],makeSolid,isFrenet)
  164. Part.show(pipe2)
  165.  
  166.  
  167. #DXF manipulation:
  168. #open a *dxf file:
  169. import Part,importDXF
  170. s = Part.Shape([importDXF.insert("/home/danfalck/Documents/My Drawings/rect_freecad_test14.dxf","Unnamed")])
  171.  
  172.  
  173. import importDXF
  174.  
  175. importDXF.insert("/home/danfalck/Documents/My Drawings/rect_freecad_test14.dxf","Unnamed")
  176. importDXF.open("/home/danfalck/Documents/My Drawings/rect_freecad_test14.dxf") #opens a new document with the dxf in it
  177.  
  178. import os
  179. import importDXF
  180. #sub class insert from importDXF
  181. def insert(filename,docname):
  182.     "called when freecad imports a file"
  183.     groupname = os.path.splitext(os.path.basename(filename))[0]
  184.     try:
  185.         doc=FreeCAD.getDocument(docname)
  186.     except:
  187.         doc=FreeCAD.newDocument(docname)
  188.     importgroup = doc.addObject("App::DocumentObjectGroup",groupname)
  189.     importgroup.Label = importDXF.decodeName(groupname)
  190.     importDXF.processdxf(doc,filename)
  191.     for l in importDXF.layers:
  192.         importgroup.addObject(l)
  193.  
  194.  
  195. insert("/home/danfalck/Documents/My Drawings/rect_freecad_test14.dxf","Unnamed")
  196.  
  197. #open a *step file:
  198. import Part
  199. s = Part.Shape()
  200. s.read("test.stp")
  201.  
  202.  
  203.  
  204. #lofting
  205. #make two closed wires- one raised up in Z from the other
  206. #fill in the names of the two wires (Wire and Wire001)
  207. topWireOut = Part.Wire(App.ActiveDocument.Wire.Shape.Edges)
  208. botWireOut = Part.Wire(App.ActiveDocument.Wire001.Shape.Edges)
  209. loftOut = Part.makeLoft([topWireOut,botWireOut],True)
  210. Part.show(loftOut)
  211.  
  212.  
  213. App.activeDocument().addObject('Drawing::FeaturePage','Page')
  214. App.activeDocument().Page.Template = App.getResourceDir()+'Mod/Drawing/Templates/A3_Landscape.svg'
  215. App.activeDocument().addObject('Drawing::FeatureViewPart','View02')
  216. App.activeDocument().View.Source = App.activeDocument().Shape #change Shape by your object name, or pass it directly one of your created objects
  217. App.activeDocument().View.Direction = (0.0,0.0,1.0) # this is where you define the projection direction
  218. App.activeDocument().View.X = 150.0 # the x offset
  219. App.activeDocument().View.Y = 150.0 # the y offset
  220. App.activeDocument().Page.addObject(App.activeDocument().View)
  221. App.activeDocument().recompute()
  222.  
  223.  
  224. App.activeDocument().addObject('Drawing::FeaturePage','Page')
  225. App.activeDocument().Page.Template = App.getResourceDir()+'Mod/Drawing/Templates/A3_Landscape.svg'
  226. App.activeDocument().addObject('Drawing::FeatureViewPart','ViewIso')
  227. App.activeDocument().ViewIso.Source = App.activeDocument().Fillet002
  228. App.activeDocument().ViewIso.Direction = (1.0,.75,.75)
  229. App.activeDocument().ViewIso.X = 300.0
  230. App.activeDocument().ViewIso.Y = 140.0
  231. App.activeDocument().ViewIso.ShowHiddenLines = True
  232. App.activeDocument().Page.addObject(App.activeDocument().ViewIso)
  233. App.activeDocument().ViewIso.Scale = 8.0
  234. App.activeDocument().recompute()
  235.  
  236. l=App.ActiveDocument.addObject("App::AnnotationLabel","Annotation")
  237. l.LabelText=["Hello","World!"]
  238. l.TextPosition = (3.0,0,0)
  239. l.BasePosition = (-3,-3,0)
  240. l.LabelText = ['changed']
  241.  
  242.  
  243. import Mesh
  244.  s = Gui.Selection.getSelectionEx()
  245.  Mesh.export(s,"/home/danfalck/Documents/My Drawings/sphere_test.stl")
  246.  
  247.  
  248. FreeCAD.ActiveDocument.getObject("Mesh").Mesh.write("/home/danfalck/Documents/sphere_test.stl","STL")
  249.  
  250. #One way to make a non parametric copy of any Part-based object (change MyObjectName by the name of your object)
  251. import Part
  252. myShape = FreeCAD.ActiveDocument.MyObjectName.Shape
  253. Part.show(myShape)
  254.  
  255. import Part
  256. myShape = FreeCAD.ActiveDocument.Box.Shape
  257. Part.show(myShape)
  258.  
  259. transfMatrix = FreeCAD.Matrix()
  260. scaleVector = FreeCAD.Vector(2,2,2)
  261. transfMatrix.scale(scaleVector)
  262. myShape = myShape.transformGeometry(transfMatrix)
  263. Part.show(myShape)
  264.  
  265.  
  266.  
  267.  
  268. p0 = Base.Vector(0,0,-10)
  269. b1 = Part.makeBox ( 50,50,10,p0 )
  270. Part.show(b1)
  271.  
  272. p1 = Base.Vector(15,15,-12)
  273. d1 = Base.Vector(0,0,1)
  274. c1 = Part.makeCylinder(2.5,12,p1)
  275. Part.show(c1)
  276.  
  277. diff = b1.cut(c1)
  278. Part.show(diff)
  279. App.activeDocument().recompute()
  280.  
  281.  
  282. App.ActiveDocument.Shape001.ViewObject.Visibility = False #turn off visibility of an object
  283.  
  284. FreeCADGui.addIcon()#add icon with absolute path to it
  285. FreeCADGui.addIcon("test", "/home/danfalck/git/heekscad/icons/HeeksCAD.svg")
  286.  
  287.  
  288. len(App.ActiveDocument.ObjectName.Shape.Vertexes)
  289. len(App.ActiveDocument.Circle.Shape.Vertexes)
  290. App.ActiveDocument.ObjectName.Shape.Vertexes
  291. App.ActiveDocument.Circle.Shape.Vertexes
  292. App.ActiveDocument.ObjectName.Shape.Edges[0].Curve
  293. App.ActiveDocument.Circle.Shape.Edges[0].Curve
  294. App.ActiveDocument.Sketch.Shape.Vertexes
  295. App.ActiveDocument.Sketch.Shape.Edges[0].Curve
  296.  
  297. import Part
  298. from draftlibs import fcgeo
  299. edgeslist = []
  300. for obj in App.ActiveDocument.Objects:
  301.     edgeslist.append(obj.Shape.Edges[0])
  302. w = fcgeo.superWire(edgeslist)
  303. Part.show(w)
  304.  
  305.  
  306. for obj in Gui.Selection.getSelectionEx():
  307.     edgeslist.append(obj.Shape.Edges[0])
  308.  
  309.  
  310. #to make splines:
  311. import Part
  312. from FreeCAD import Base
  313.  
  314. nodes=[]
  315. nodes.append(Base.Vector(-2.08299, 1.5702, 0))
  316. nodes.append(Base.Vector(-0.381617, 1.68945, 0))
  317. nodes.append(Base.Vector(0.882489, 1.6974, 0))
  318. nodes.append(Base.Vector(1.48672, 0.162982, 0))
  319. nodes.append(Base.Vector(0.946093, -0.878514, 0))
  320. nodes.append(Base.Vector(-0.628078, -0.950068, 0))
  321. nodes.append(Base.Vector(-0.954043, 0.043727, 0))
  322. nodes.append(Base.Vector(-2.50436, -0.465096, 0))
  323. nodes.append(Base.Vector(-2.49641, -1.30783, 0))
  324. nodes.append(Base.Vector(-3.19604, -0.862614, 0))
  325. nodes.append(Base.Vector(-3.18809, 0.99777, 0))
  326.  
  327. b=Part.BezierCurve()
  328. b.setPoles(nodes)
  329. Part.show(b.toShape())
  330. Part.show(Part.makePolygon(nodes))
  331.  
  332. spline=Part.BSplineCurve()
  333. spline.buildFromPoles(nodes)
  334. Part.show(spline.toShape())
  335.  
  336.  
  337. >>> spline.getPoles()
  338. [Vector (-2.08299, 1.5702, 0), Vector (-0.381617, 1.68945, 0), Vector (0.882489, 1.6974, 0), Vector (1.48672, 0.162982, 0), Vector (0.946093, -0.878514, 0), Vector (-0.628078, -0.950068, 0), Vector (-0.954043, 0.043727, 0), Vector (-2.50436, -0.465096, 0), Vector (-2.49641, -1.30783, 0), Vector (-3.19604, -0.862614, 0), Vector (-3.18809, 0.99777, 0)]
  339. >>>
  340. >>> spline.getKnots()
  341. [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]
  342.  
  343. >>> spline.getKnot(1)
  344. 0.0
  345. >>> spline.getKnot(2)
  346. 0.125
  347. >>> spline.getPole(3)
  348. Vector (0.882489, 1.6974, 0)
  349. >>>
  350. >>> spline.tangent(2)
  351. (Vector (0.323401, 0.946262, 0),)
  352. >>> spline.tangent(0)
  353. (Vector (0.997553, 0.0699189, 0),)
  354.  
  355. #look at operations available with from different functions:
  356. dir(FreeCAD)
  357. ['ActiveDocument', 'Base', 'BoundBox', 'ConfigDump', 'ConfigGet', 'ConfigSet', 'Console', 'DraftWorkingPlane', 'EndingAdd', 'EndingGet', 'Gui', 'GuiUp', 'Matrix', 'ParamGet', 'Placement', 'Rotation', 'Units', 'Vector', 'Version', '__doc__', '__name__', '__package__', '__path__', 'activeDocument', 'activeDraftCommand', 'addDocumentObserver', 'addExportType', 'addImportType', 'closeDocument', 'getDocument', 'getExportType', 'getHomePath', 'getImportType', 'getResourceDir', 'listDocuments', 'loadFile', 'newDocument', 'open', 'openDocument', 'removeDocumentObserver', 'setActiveDocument', 'svgpatterns']
  358. dir(Part)
  359. ['Arc', 'ArcOfCircle', 'BSplineCurve', 'BSplineSurface', 'BezierCurve', 'BezierSurface', 'Circle', 'CompSolid', 'Compound', 'Cone', 'Cylinder', 'Edge', 'Ellipse', 'Face', 'Feature', 'Hyperbola', 'Line', 'OffsetCurve', 'OffsetSurface', 'Parabola', 'Plane', 'RectangularTrimmedSurface', 'Shape', 'Shell', 'Solid', 'Sphere', 'SurfaceOfExtrusion', 'SurfaceOfRevolution', 'Toroid', 'Vertex', 'Wire', '__doc__', '__file__', '__fromPythonOCC__', '__name__', '__package__', '__sortEdges__', '__toPythonOCC__', 'cast_to_shape', 'export', 'getSortedClusters', 'insert', 'makeBox', 'makeCircle', 'makeCompound', 'makeCone', 'makeCylinder', 'makeFilledFace', 'makeHelix', 'makeLine', 'makeLoft', 'makePlane', 'makePolygon', 'makeRevolution', 'makeRuledSurface', 'makeShell', 'makeSolid', 'makeSphere', 'makeSweepSurface', 'makeTorus', 'makeTube', 'makeWedge', 'open', 'read', 'show']
  360. dir(Part.BSplineCurve)
  361. ['Construction', 'Degree', 'EndPoint', 'FirstParameter', 'FirstUKnotIndex', 'KnotSequence', 'LastParameter', 'LastUKnotIndex', 'MaxDegree', 'NbKnots', 'NbPoles', 'StartPoint', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'approximate', 'buildFromPoles', 'discretize', 'getKnot', 'getKnots', 'getMultiplicities', 'getMultiplicity', 'getPole', 'getPoles', 'getResolution', 'getWeight', 'getWeights', 'increaseDegree', 'increaseMultiplicity', 'incrementMultiplicity', 'insertKnot', 'insertKnots', 'interpolate', 'isClosed', 'isPeriodic', 'isRational', 'join', 'makeC1Continuous', 'makeRuledSurface', 'mirror', 'movePoint', 'parameter', 'removeKnot', 'rotate', 'scale', 'segment', 'setKnot', 'setKnots', 'setNotPeriodic', 'setOrigin', 'setPeriodic', 'setPole', 'setWeight', 'tangent', 'toBezier', 'toShape', 'transform', 'translate', 'value']
  362. >>> dir(Part.BezierCurve)
  363. ['Construction', 'Degree', 'EndPoint', 'FirstParameter', 'LastParameter', 'MaxDegree', 'NbPoles', 'StartPoint', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'discretize', 'getPole', 'getPoles', 'getResolution', 'getWeight', 'getWeights', 'increase', 'insertPoleAfter', 'insertPoleBefore', 'isClosed', 'isPeriodic', 'isRational', 'makeRuledSurface', 'mirror', 'parameter', 'removePole', 'rotate', 'scale', 'segment', 'setPole', 'setPoles', 'setWeight', 'tangent', 'toShape', 'transform', 'translate', 'value']
  364.  
  365. >>> dir(Part.Edge)
  366. ['Area', 'BoundBox', 'CenterOfMass', 'Closed', 'CompSolids', 'Compounds', 'Content', 'Curve', 'Degenerated', 'Edges', 'Faces', 'Length', 'Matrix', 'MemSize', 'Module', 'Orientation', 'ParameterRange', 'Placement', 'ShapeType', 'Shells', 'Solids', 'Type', 'Vertexes', 'Volume', 'Wires', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'centerOfCurvatureAt', 'check', 'common', 'complement', 'copy', 'curvatureAt', 'cut', 'derivative1At', 'derivative2At', 'derivative3At', 'exportBrep', 'exportIges', 'exportStep', 'exportStl', 'extrude', 'fix', 'fuse', 'getAllDerivedFrom', 'hashCode', 'isClosed', 'isDerivedFrom', 'isEqual', 'isInside', 'isNull', 'isSame', 'isValid', 'makeChamfer', 'makeFillet', 'makeOffsetShape', 'makeShapeFromMesh', 'makeThickness', 'mirror', 'normalAt', 'nullify', 'oldFuse', 'project', 'read', 'removeInternalWires', 'removeShape', 'replaceShape', 'reverse', 'revolve', 'rotate', 'scale', 'section', 'setTolerance', 'sewShape', 'slice', 'slices', 'tangentAt', 'tessellate', 'toNurbs', 'transformGeometry', 'transformShape', 'translate', 'valueAt', 'writeInventor']
  367. >>>
  368.  
  369. >>> dir(Part.Edge.Curve)
  370. ['__class__', '__delattr__', '__delete__', '__doc__', '__format__', '__get__', '__getattribute__', '__hash__', '__init__', '__name__', '__new__', '__objclass__', '__reduce__', '__reduce_ex__', '__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
  371. >>>
  372. >>> dir(Part.Edge.Vertexes)
  373. ['__class__', '__delattr__', '__delete__', '__doc__', '__format__', '__get__', '__getattribute__', '__hash__', '__init__', '__name__', '__new__', '__objclass__', '__reduce__', '__reduce_ex__', '__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
  374. >>>
  375. >>> sel = FreeCADGui.Selection.getSelection()
  376. >>> sel = sel[0]
  377. >>> name = sel.Name
  378. >>> shape = sel.Shape
  379. >>> name
  380. 'Fillet'
  381. >>> shape
  382. <Compound object at 0x137e830>
  383.  
  384. #using my modified ProjectionAlgos routines:
  385. import Part,Drawing,FreeCADGui,FreeCAD
  386. from FreeCAD import Base
  387. filename = "/home/danfalck/Documents/freecad/test100904.dxf"
  388. FILE = open(filename,"w")
  389. sel = FreeCADGui.Selection.getSelection()
  390. sel = sel[0]
  391. shape = sel.Shape
  392. dxf_output = Drawing.projectToDXF(shape,Base.Vector(0,0,1))
  393. FILE.write(dxf_output)
  394. FILE.close()
  395.  
  396. import Part,Drawing,FreeCADGui,FreeCAD
  397. from FreeCAD import Base
  398. filename = "/home/danfalck/Documents/freecad/test102801.dxf"#change the file name and directory here
  399. FILE = open(filename,"w")
  400. sel = FreeCADGui.Selection.getSelection()
  401. sel = sel[0]
  402. shape = sel.Shape
  403. dxf_output = Drawing.projectToDXF(shape,Base.Vector(0,0,1)) #change direction of the projection here
  404. FILE.write(dxf_output)
  405. FILE.close()
  406.  
  407.  
  408. #to make a helical thread
  409. import Part, FreeCAD, math
  410. from FreeCAD import Base
  411.  
  412. helix = Part.makeHelix(1,10,3)
  413. edge1 = Part.makeLine((2.5,0,-0.125), (2.5,0,0.125))
  414. edge2 = Part.makeLine((2.5,0,0.125), (3.1,0,0.419))
  415. edge3 = Part.makeLine((3.1,0,0.419), (3.1,0,-0.419))
  416. edge4 = Part.makeLine((3.1,0,-0.419), (2.5,0,-0.125))
  417. section = Part.Wire([edge1,edge2,edge3,edge4])
  418. makeSolid=0 #change to 1 to make a solid, but it will take a while
  419. isFrenet=1
  420. pipe = Part.Wire(helix).makePipeShell([section],makeSolid,isFrenet)
  421. Part.show(pipe)
  422.  
  423.  
  424. helix = Part.makeHelix(1,10,3)
  425. edge1 = Part.makeLine((3.1,0,-0.125), (3.1,0,0.125))
  426. edge2 = Part.makeLine((3.1,0,0.125), (2.5,0,0.419))
  427. edge3 = Part.makeLine((2.5,0,0.419), (2.5,0,-0.419))
  428. edge4 = Part.makeLine((2.5,0,-0.419), (3.1,0,-0.125))
  429. section = Part.Wire([edge1,edge2,edge3,edge4])
  430. makeSolid=1 #change to 1 to make a solid, but it will take a while
  431. isFrenet=1
  432. pipe = Part.Wire(helix).makePipeShell([section],makeSolid,isFrenet)
  433. Part.show(pipe)
  434.  
  435. helix = Part.makeHelix(1,-10,3)
  436. edge1 = Part.makeLine((3.1,0,-0.125), (3.1,0,0.125))
  437. edge2 = Part.makeLine((3.1,0,0.125), (2.5,0,0.419))
  438. edge3 = Part.makeLine((2.5,0,0.419), (2.5,0,-0.419))
  439. edge4 = Part.makeLine((2.5,0,-0.419), (3.1,0,-0.125))
  440. section = Part.Wire([edge1,edge2,edge3,edge4])
  441. makeSolid=1 #change to 1 to make a solid, but it will take a while
  442. isFrenet=1
  443. pipe = Part.Wire(helix).makePipeShell([section],makeSolid,isFrenet)
  444. Part.show(pipe)
  445.  
  446.  
  447.  
  448.  
  449. #pythonOCC stuff:
  450. #For instance you can create a box and convert that into the pythonOCC wrapper class like:
  451. import Part
  452. from OCC import TopoDS
  453. s=Part.makeBox(10,10,10)
  454. occ=Part.__toPythonOCC__(s)
  455. # now you can use all classes/functions which are wrapped in pythonOCC
  456.  
  457. s=Part.__fromPythonOCC__(occ)
  458. Part.show(s)
  459.  
  460. import Part, FreeCAD, math, PartGui, FreeCADGui
  461. from FreeCAD import Base
  462.  
  463. #from Norman's twisted bar example:
  464. # pick selected objects, whre 1st selection is the trajectory and the 2nd is the section to sweep
  465. s = FreeCADGui.Selection.getSelection()
  466. try:
  467.     shape1=s[0].Shape
  468.     shape2=s[1].Shape
  469. except:
  470.     print "Wrong selection"
  471.  
  472. traj = Part.Wire([shape1])
  473. section = Part.Wire([shape2])
  474.  
  475. # create a Part object into the active document
  476. myObject=App.ActiveDocument.addObject("Part::Feature","Sweep")
  477.  
  478. makeSolid = 1
  479. isFrenet = 1
  480.  
  481. # Create the 3D shape and set it to the Part object
  482. Sweep = Part.Wire(traj).makePipeShell([section],makeSolid,isFrenet)
  483. myObject.Shape = Sweep
  484.  
  485. #slicing objects to look at cross sections:
  486. #That's correct but it is a bit inconvenient when making many slices because then you must for #each slice prepare an own plane object. With the latest sources I have added two commands.
  487. #slices -- which accepts a Vector (the normal of the plane) and a list of doubles. The doubles #mark the distances of the current plane to the origin. The returned object is a compound #containing all slices.
  488.  
  489. #slice -- which accepts a Vector and a single double to make only one slice. The returned
  490. #object is a list of wires.
  491. from FreeCAD import Base
  492. shape=Part.makeCylinder(5,15)
  493. slices=shape.slices(Base.Vector(0,0,1),[0,0.5,1.0])
  494. slice=shape.slice(Base.Vector(0,1,0),1.5)
  495. Part.show(slices)
  496. comp=Part.Compound(slice)
  497. Part.show(comp)
  498.  
  499. #name your object:
  500. import Part
  501. doc = App.activeDocument()
  502. c = Part.Circle()
  503. c.Radius=10.0  
  504. f = doc.addObject("Part::Feature", "Name_of_Circle_here") # create a document with a circle feature
  505. f.Shape = c.toShape() # Assign the circle shape to the shape property
  506. doc.recompute()
  507. #hide it:
  508. Gui.activeDocument().hide('Name_of_Circle_here')
  509. #show it:
  510. Gui.activeDocument().show('Name_of_Circle_here')
  511.  
  512.  
  513. #Adding and removing an object to a group
  514. doc=App.activeDocument()
  515. grp=doc.addObject("App::DocumentObjectGroup", "Group1")
  516.  
  517. grp.addObject(f) # adds the circle (f) object to the group grp
  518. grp.removeObject(f) # removes the circle object from the group grp
  519.  
  520.  
  521. #in draft mode, create and select an arc
  522. for o in  Gui.Selection.getSelectionEx():
  523.     for s in o.SubObjects:
  524.         print s.Vertexes[0].X, s.Vertexes[0].Y, s.Vertexes[0].Z
  525.         print s.Vertexes[1].X, s.Vertexes[1].Y, s.Vertexes[1].Z
  526.         print s.Curve.Center.x,s.Curve.Center.y, s.Curve.Center.z
  527.  
  528. for o in  Gui.Selection.getSelectionEx():
  529.     for c in o.SubObjects:
  530.         print c.Vertexes[0].X, c.Vertexes[0].Y, c.Vertexes[0].Z
  531.  
  532. c.Curve.__class__
  533. #returns: <type 'Part.GeomCircle'>
  534.  
  535. #select several arcs and lines (from a sketch element)
  536. sel= Gui.Selection.getSelectionEx()
  537. a = sel[0]
  538. b = sel[1]
  539.  
  540. a.ObjectName
  541. #returns Name such as 'Arc'
  542. b.ObjectName
  543. #returns Name such as 'Line002'
  544.  
  545. #sorting edges from wmayer:
  546. edges=[]
  547. s=Gui.Selection.getSelectionEx()
  548. for i in s:
  549.     for e in i.SubElementNames:
  550.         edges.append(getattr(i.Object.Shape,e))
  551. #then you can see what the edges are:
  552. edges[0].Curve
  553. #might return:
  554. #Circle (Radius : 3, Position : (3, 3, 0), Direction : (0, 0, 1))
  555. edges[0].Curve.Center.x
  556. edges[0].Curve.Center.y
  557.  
  558. >>> edges[0].Vertexes[0].X
  559. 0.0
  560. >>> edges[0].Vertexes[0].Y
  561. 3.0000000000000004
  562. >>>
  563.  
  564.  
  565. edges2=[]
  566. k=Gui.Selection.getSelectionEx()
  567. for i in k:
  568.     for e in i.SubElementNames:
  569.         edges2.append(getattr(i.Object.Shape,e))
  570. edges2[0].Curve
  571.  
  572.  
  573. edges=[]
  574. s=Gui.Selection.getSelectionEx()
  575. for i in s:
  576.     for e in i.SubElementNames:
  577.         edges.append(getattr(i.Object.Shape,e))
  578.  
  579. #export a shape to a *.stl file
  580. #select it first
  581. from FreeCAD import Part
  582. s=Gui.Selection.getSelectionEx()
  583. s[0].Object.Shape.exportStl('/home/danfalck/test2.stl')
  584.  
  585.  
  586. inx = 0
  587. while (inx <len(edges)):
  588.     print edges[inx].Curve
  589.     inx = inx+1
  590.  
  591. #might return:
  592. #Circle (Radius : 3, Position : (3, 3, 0), Direction : (0, 0, 1))
  593. #<Line (10;0,0) (10;10,0) >
  594. #<Line (3;0,0) (10;0,0) >
  595. #<Line (0;3,0) (0;10,0) >
  596. #<Line (0;10,0) (10;10,0) >
  597. edges.reverse()
  598. # do this again:
  599. inx = 0
  600. while (inx <len(edges)):
  601.     print edges[inx].Curve
  602.     inx = inx+1
  603.  
  604. #returns this:
  605. #<Line (0;10,0) (10;10,0) >
  606. #<Line (0;3,0) (0;10,0) >
  607. #<Line (3;0,0) (10;0,0) >
  608. #<Line (10;0,0) (10;10,0) >
  609. #Circle (Radius : 3, Position : (3, 3, 0), Direction : (0, 0, 1))
  610.  
  611.  
  612. edges=[]
  613. s=Gui.Selection.getSelectionEx()
  614. s.sort() # <<<<<sorting edges
  615. for i in s:
  616.      for e in i.SubElementNames:
  617.          edges.append(getattr(i.Object.Shape,e))
  618.  
  619. inx = 0
  620. while (inx <len(edges)):
  621.      print edges[inx].Curve
  622.      inx = inx+1
  623.  
  624. #returns:
  625. <Line (3;0,0) (10;0,0) >
  626. Circle (Radius : 3, Position : (3, 3, 0), Direction : (0, 0, 1))
  627. <Line (0;3,0) (0;10,0) >
  628. <Line (0;10,0) (10;10,0) >
  629. <Line (10;0,0) (10;10,0) >
  630.  
  631.  
  632. s.pop() #<<<<<<<<<removes last item from list
  633. edges=[]
  634. for i in s:
  635.      for e in i.SubElementNames:
  636.          edges.append(getattr(i.Object.Shape,e))
  637.  
  638. inx = 0
  639. while (inx <len(edges)):
  640.      print edges[inx].Curve
  641.      inx = inx+1
  642.  
  643. <Line (10;0,0) (10;10,0) >
  644. <Line (0;10,0) (10;10,0) >
  645. <Line (3;0,0) (10;0,0) >
  646. Circle (Radius : 3, Position : (3, 3, 0), Direction : (0, 0, 1))
  647.  
  648. s.reverse()
  649. edges=[]
  650. for i in s:
  651.      for e in i.SubElementNames:
  652.          edges.append(getattr(i.Object.Shape,e))
  653.  
  654. inx = 0
  655. while (inx <len(edges)):
  656.      print edges[inx].Curve
  657.      inx = inx+1
  658.  
  659. #returns:
  660. Circle (Radius : 3, Position : (3, 3, 0), Direction : (0, 0, 1))
  661. <Line (3;0,0) (10;0,0) >
  662. <Line (0;10,0) (10;10,0) >
  663. <Line (10;0,0) (10;10,0) >
  664.  
  665. s.sort()
  666. edges=[]
  667. for i in s:
  668.      for e in i.SubElementNames:
  669.          edges.append(getattr(i.Object.Shape,e))
  670.  
  671. inx = 0
  672. while (inx <len(edges)):
  673.      print edges[inx].Curve
  674.      inx = inx+1
  675. #returns:
  676. <Line (10;0,0) (10;10,0) >
  677. <Line (0;10,0) (10;10,0) >
  678. Circle (Radius : 3, Position : (3, 3, 0), Direction : (0, 0, 1))
  679. <Line (3;0,0) (10;0,0) >
  680.  
  681. edges=[]
  682. s=Gui.Selection.getSelectionEx()
  683. for i in s:
  684.     for e in i.SubElementNames:
  685.         edges.append(getattr(i.Object.Shape,e))
  686.         print type(s)
  687.  
  688. for obj in FreeCAD.ActiveDocument.Objects:
  689.     if hasattr(obj,"Shape"):
  690.         print "Object: ", obj, "Shape type: ", obj.Shape.Type
  691.  
  692.  
  693.  
  694. #sorting with __sortEdges__
  695. >>> edge1 = Part.makeLine((0,0,0), (10,0,0))
  696. >>> edge2 = Part.makeLine((10,0,0), (10,10,0))
  697. >>> wire1 = Part.Wire([edge1,edge2])
  698. >>> edge3 = Part.makeLine((10,10,0), (0,10,0))
  699. >>> edge4 = Part.makeLine((0,10,0), (0,0,0))
  700. >>> wire2 = Part.Wire([edge3,edge4])
  701. >>> wire3 = Part.Wire([wire1,wire2])
  702. >>> wire3.Edges
  703. [<Edge object at 0x17cb070>, <Edge object at 0x188b730>, <Edge object at 0xb16270>, <Edge object at 0x17ce970>]
  704. >>> face = Part.Face(wire3)
  705. >>> face2 = Part.__sortEdges__(wire3)
  706. Traceback (most recent call last):
  707.   File "<input>", line 1, in <module>
  708. Exception: list of edges expected
  709. >>> face3 = Part.__sortEdges__(edge1,edge2,edge3,edge4)
  710. Traceback (most recent call last):
  711.   File "<input>", line 1, in <module>
  712. Exception: list of edges expected
  713. >>> face3 = Part.__sortEdges__([edge1,edge2,edge3,edge4])
  714. >>> face3
  715. [<Edge object at 0x18ae080>, <Edge object at 0x17dc3d0>, <Edge object at 0x1888760>, <Edge object at 0xd045c0>]
  716. >>> face4 = Part.__sortEdges___([edge3,edge1,edge2,edge4])
  717. Traceback (most recent call last):
  718.   File "<input>", line 1, in <module>
  719. AttributeError: 'module' object has no attribute '__sortEdges___'
  720. >>> face3 = Part.__sortEdges__([edge1,edge2,edge3,edge4])
  721. >>> face4 = Part.__sortEdges__([edge1,edge2,edge3,edge4])
  722. >>> face4 = Part.__sortEdges__([edge2,edge3,edge4,edge1])
  723. >>> face4
  724. [<Edge object at 0x18aa220>, <Edge object at 0x18b3c60>, <Edge object at 0x13fb670>, <Edge object at 0x18afa30>]
  725. >>> face4
  726. [<Edge object at 0x18aa220>, <Edge object at 0x18b3c60>, <Edge object at 0x13fb670>, <Edge object at 0x18afa30>]
  727. >>> face4[0]
  728. <Edge object at 0x18aa220>
  729. >>> face4[0].Curve
  730. <Line (10;0,0) (10;10,0) >
  731. >>> face4[1].Curve
  732. <Line (10;10,0) (0;10,0) >
  733. >>> face4[2].Curve
  734. <Line (0;10,0) (0;0,0) >
  735. >>> face4[3].Curve
  736. <Line (0;0,0) (10;0,0) >
  737. #select from the gui window- not the tree
  738. edges=[]
  739. s=Gui.Selection.getSelectionEx()
  740. for i in s:
  741.      for e in i.SubElementNames:
  742.          edges.append(getattr(i.Object.Shape,e))
  743.  
  744. S1 = Part.Wire(edges)
  745. S2.__sortEdges__([edges[0], edges[1], edges[2], edges[3], edges[4]])
  746. [<Edge object at 0x18c9910>, <Edge object at 0x18a83d0>, <Edge object at 0x188be00>, <Edge object at 0x186e0e0>, <Edge object at 0x18bfd10>]
  747. S3.__sortEdges__(edges)
  748.  
  749. >>> S3 =Part
  750. >>> S3.__sortEdges__(edges)
  751. [<Edge object at 0x13c3280>, <Edge object at 0x18c95c0>, <Edge object at 0x18659c0>, <Edge object at 0x18c9470>, <Edge object at 0x18a9090>]
  752. >>>
  753.  
  754.  
  755. S4 = S3.__sortEdges__(edges)
  756. S4[0].Curve
  757. <Line (3;0,0) (10;0,0) >
  758. >>> S4[1].Curve
  759. Circle (Radius : 3, Position : (3, 3, 0), Direction : (0, 0, 1))
  760. >>> S4[2].Curve
  761. <Line (0;3,0) (0;10,0) >
  762. >>> S4[3].Curve
  763. <Line (0;10,0) (10;10,0) >
  764. >>> S4[4].Curve
  765. <Line (10;0,0) (10;10,0) >
  766. >>>
  767.  
  768. inx = 0
  769. while (inx <len(S4)):
  770.      print S4[inx].Curve
  771.      inx = inx+1
  772.  
  773. <Line (3;0,0) (10;0,0) >
  774. Circle (Radius : 3, Position : (3, 3, 0), Direction : (0, 0, 1))
  775. <Line (0;3,0) (0;10,0) >
  776. <Line (0;10,0) (10;10,0) >
  777. <Line (10;0,0) (10;10,0) >
  778.  
  779. >>> S4[0].Curve.__class__
  780. <type 'Part.GeomLineSegment'>
  781. >>> S4[3].Curve.__class__
  782. <type 'Part.GeomLineSegment'>
  783. >>> S4[2].Curve.__class__
  784. <type 'Part.GeomLineSegment'>
  785. >>> S4[1].Curve.__class__
  786. <type 'Part.GeomCircle'>
  787. >>>
  788. >>> S4[1].Curve.__class__.__name__
  789. 'GeomCircle'
  790. >>> S4[0].Curve.__class__.__name__
  791. 'GeomLineSegment'
  792.  
  793. >>> S4[0].Vertexes
  794. [<Vertex object at 0x18bf8d0>, <Vertex object at 0x18a3ab0>]
  795. >>> S4[0].Vertexes[0]
  796. <Vertex object at 0x18cac10>
  797. >>>
  798. >>> S4[0].Vertexes[0].X
  799. 3.0
  800. print S4[1].Curve.__class__.__name__, S4[1].Vertexes[0].X,S4[1].Vertexes[0].Y
  801. print S4[1].Curve.__class__.__name__, S4[1].Vertexes[1].X,S4[1].Vertexes[1].Y
  802. print S4[1].Curve.__class__.__name__, S4[1].Curve.Center.x,S4[1].Curve.Center.y
  803. GeomCircle3.03.0
  804.  
  805. print S4[1].Curve.__class__.__name__
  806. print "start pt: X"+str(S4[1].Vertexes[0].X)+', Y'+str(S4[1].Vertexes[0].Y)
  807. print "center pt: X"+str(S4[1].Curve.Center.x)+', Y'+str(S4[1].Curve.Center.y)
  808. print "end pt: X"+str(S4[1].Vertexes[1].X)+', Y'+str(S4[1].Vertexes[1].Y)
  809.  
  810. import Part,Drawing,FreeCADGui,FreeCAD
  811. from FreeCAD import Base
  812. #select from the gui window- not the tree
  813. edges=[]
  814. s=Gui.Selection.getSelectionEx()
  815. for i in s:
  816.     for e in i.SubElementNames:
  817.         edges.append(getattr(i.Object.Shape,e))
  818.  
  819. S3 =Part
  820. S4 = S3.__sortEdges__(edges)
  821.  
  822. inx = 0
  823. while (inx <len(S4)):
  824.     if (S4[inx].Curve.__class__.__name__ == 'GeomCircle'):
  825.         if S4[0].Closed:
  826.             print '--Circle--'
  827.             print "center pt: X "+str(S4[inx].Curve.Center.x)+', Y '+str(S4[inx].Curve.Center.y)\
  828.             +" Radius: "+str(S4[inx].Curve.Radius)
  829.         else:
  830.             print '--Arc--'
  831.             print "start pt: X "+str(S4[inx].Vertexes[0].X)+', Y '+str(S4[inx].Vertexes[0].Y)\
  832.             +" center pt: X "+str(S4[inx].Curve.Center.x)+', Y '+str(S4[inx].Curve.Center.y)\
  833.             +" end pt: X "+str(S4[inx].Vertexes[inx].X)+', Y '+str(S4[inx].Vertexes[1].Y)\
  834.             +" Radius: "+str(S4[inx].Curve.Radius)
  835.     else:
  836.         print '--Line--'
  837.         print "start pt: X "+str(S4[inx].Vertexes[0].X)+', Y '+str(S4[inx].Vertexes[0].Y)\
  838.         +" end pt: X "+str(S4[inx].Vertexes[1].X)+', Y '+str(S4[inx].Vertexes[1].Y)
  839.    
  840.     inx = inx+1
  841.  
  842.  
  843. S4[0].Curve.Radius #get the radius of a circle or arc
  844.  
  845.  
  846. #check to see if Circle is really a 'Circle' or an 'Arc'
  847. >>> edges=[]
  848. >>> s=Gui.Selection.getSelectionEx()
  849. >>> for i in s:
  850. ...     for e in i.SubElementNames:
  851. ...         edges.append(getattr(i.Object.Shape,e))
  852. ...
  853. >>> S3 =Part
  854. >>> S4 = S3.__sortEdges__(edges)
  855. >>>
  856. >>> S4
  857. [<Edge object at 0x18e9620>]
  858. >>> S4[0].Closed
  859. True  #<<<<<<<<<<<it's a circle because it's closed
  860.  
  861.  
  862.  
  863. import Part,Drawing,FreeCADGui,FreeCAD
  864. from FreeCAD import Base
  865. #select from the gui window- not the tree
  866. edges=[]
  867. s=Gui.Selection.getSelectionEx()
  868. for i in s:
  869.     for e in i.SubElementNames:
  870.         edges.append(getattr(i.Object.Shape,e))
  871.  
  872. S3 =Part
  873. S4 = S3.__sortEdges__(edges)
  874.  
  875. inx = 0
  876. while (inx <len(S4)):
  877.     if (S4[inx].Curve.__class__.__name__ == 'GeomCircle'):
  878.         if S4[0].Closed:
  879.             print '--Circle--'
  880.             print "center pt: X "+str(S4[inx].Curve.Center.x)+', Y '+str(S4[inx].Curve.Center.y)\
  881.             +" Radius: "+str(S4[inx].Curve.Radius)
  882.         else:
  883.             print '--Arc--'
  884.             print "start pt: X "+str(S4[inx].Vertexes[0].X)+', Y '+str(S4[inx].Vertexes[0].Y)\
  885.             +" center pt: X "+str(S4[inx].Curve.Center.x)+', Y '+str(S4[inx].Curve.Center.y)\
  886.             +" end pt: X "+str(S4[inx].Vertexes[1].X)+', Y '+str(S4[inx].Vertexes[1].Y)\
  887.             +" Radius: "+str(S4[inx].Curve.Radius)\
  888.             +" Direction: "+str(S4[inx].Curve.Axis)
  889.     else:
  890.         print '--Line--'
  891.         print "start pt: X "+str(S4[inx].Vertexes[0].X)+', Y '+str(S4[inx].Vertexes[0].Y)\
  892.         +" end pt: X "+str(S4[inx].Vertexes[1].X)+', Y '+str(S4[inx].Vertexes[1].Y)
  893.    
  894.     inx = inx+1
  895.  
  896.  
  897. --Line--
  898. start pt: X 24.42911, Y 15.25673 end pt: X -18.1368, Y 15.25673
  899. --Arc--
  900. start pt: X -18.1368, Y 15.25673 center pt: X -18.1368, Y 9.086716 end pt: X -24.306814, Y 9.086716 Radius: 6.170014 Direction: Vector (0, 0, 1)
  901. --Line--
  902. start pt: X -24.306814, Y 9.086716 end pt: X -24.306814, Y -10.557453
  903. --Arc--
  904. start pt: X -24.306814, Y -10.557453 center pt: X -18.62915, Y -10.557453 end pt: X -18.62915, Y -16.235117 Radius: 5.677664 Direction: Vector (0, 0, 1)
  905. --Line--
  906. start pt: X -18.62915, Y -16.235117 end pt: X 23.017835, Y -16.235117
  907. --Arc--
  908. start pt: X 23.017835, Y -16.235117 center pt: X 23.017835, Y -10.054202 end pt: X 29.19875, Y -10.054202 Radius: 6.180915 Direction: Vector (0, 0, 1)
  909. --Line--
  910. start pt: X 29.19875, Y -10.054202 end pt: X 29.19875, Y 10.48709
  911. --Arc--
  912. start pt: X 29.19875, Y 10.48709 center pt: X 24.42911, Y 10.48709 end pt: X 24.42911, Y 15.25673 Radius: 4.76964 Direction: Vector (0, 0, 1)
  913.  
  914. #############################################
  915. ### HeeksCNC related scripting
  916. #############################################
  917. import Part,Drawing,FreeCADGui,FreeCAD
  918. from FreeCAD import Base
  919. edges=[]
  920. s=Gui.Selection.getSelectionEx()
  921. for i in s:
  922.     for e in i.SubElementNames:
  923.         edges.append(getattr(i.Object.Shape,e))
  924.  
  925. S3 =Part
  926. S4 = S3.__sortEdges__(edges)
  927. w1 = Part.Wire(S4)
  928. #check to see if shape is closed
  929. #w1.isClosed()
  930. #make an offset
  931. #o1 = w1.makeOffset(10)
  932. #Part.show(o1)
  933.  
  934. if (S4[0].Vertexes[0].X == S4[1].Vertexes[1].X) and (S4[0].Vertexes[0].Y == S4[1].Vertexes[1].Y):
  935.     print 'curve = area.Curve()\n' \
  936.     +'curve.append(area.Point(' +str(S4[0].Vertexes[1].X) + ',' + str(S4[0].Vertexes[1].Y)+'))\n'\
  937.     +'curve.append(area.Point(' +str(S4[0].Vertexes[0].X) + ',' + str(S4[0].Vertexes[0].Y)+'))\n'
  938. else:
  939.     print 'curve = area.Curve()\n' \
  940.     +'curve.append(area.Point(' +str(S4[0].Vertexes[0].X) + ',' + str(S4[0].Vertexes[0].Y)+'))\n'\
  941.     +'curve.append(area.Point(' +str(S4[0].Vertexes[1].X) + ',' + str(S4[0].Vertexes[1].Y)+'))\n'
  942.  
  943. inx = 1
  944. while (inx <len(S4)):
  945.     if (S4[inx].Curve.__class__.__name__ == 'GeomCircle'):
  946.         if S4[inx].Closed:
  947.             print '#--Warning->Circle--'
  948.             #print "center pt: X "+str(S4[inx].Curve.Center.x)+', Y '+str(S4[inx].Curve.Center.y)\
  949.             #+" Radius: "+str(S4[inx].Curve.Radius)
  950.         else:
  951.             if (S4[inx-1].Vertexes[0].X == S4[inx].Vertexes[1].X) and (S4[inx-1].Vertexes[0].Y == S4[inx].Vertexes[1].Y):
  952.                 print 'curve.append(area.Vertex('+str(int(1*(S4[inx].Curve.Axis[2])))+', area.Point('+str(S4[inx].Vertexes[0].X)+', '+str(S4[inx].Vertexes[0].Y)\
  953.                 +'), area.Point('+str(S4[inx].Curve.Center.x)+', '+str(S4[inx].Curve.Center.y)\
  954.                 +')))\n'
  955.                 #+" Radius: "+str(S4[inx].Curve.Radius)\
  956.                 #+" Direction: "+str(S4[inx].Curve.Axis)
  957.             else:
  958.                 print 'curve.append(area.Vertex('+str(1*(int(S4[inx].Curve.Axis[2])))+', area.Point('+str(S4[inx].Vertexes[1].X)+', '+str(S4[inx].Vertexes[1].Y)\
  959.                 +'), area.Point('+str(S4[inx].Curve.Center.x)+', '+str(S4[inx].Curve.Center.y)\
  960.                 +')))\n'
  961.     else:
  962.         #print '--Line--'
  963.         if (S4[inx-1].Vertexes[0].X == S4[inx].Vertexes[1].X) and (S4[inx-1].Vertexes[0].Y == S4[inx].Vertexes[1].Y):
  964.             print 'curve.append(area.Point(' +str(S4[inx].Vertexes[0].X) + ',' + str(S4[inx].Vertexes[0].Y)+'))\n'
  965.         else:
  966.             print 'curve.append(area.Point(' +str(S4[inx].Vertexes[1].X) + ',' + str(S4[inx].Vertexes[1].Y)+'))\n'
  967.    
  968.     inx = inx+1
  969.  
  970.  
  971.  
  972. edges=[]
  973. s=Gui.Selection.getSelectionEx()
  974. for i in s:
  975.     for e in i.SubElementNames:
  976.         edges.append(getattr(i.Object.Shape,e))
  977.  
  978. S1 =Part
  979. S2 = S1.__sortEdges__(edges)
  980. w1 = Part.Wire(S2)
  981. o1 = w1.makeOffset(1)
  982. Part.show(o1)
  983.  
  984. import Part,Drawing,FreeCADGui,FreeCAD
  985. from FreeCAD import Base
  986.  
  987.  
  988. edges=[]
  989. s=Gui.Selection.getSelectionEx()
  990. for i in s:
  991.     for e in i.SubElementNames:
  992.         edges.append(getattr(i.Object.Shape,e))
  993.  
  994.  
  995. S3 =Part
  996. S4 = S3.__sortEdges__(edges)
  997. w1 = Part.Wire(S4)
  998. o1 = w1.makeOffset(1.5)
  999. Part.show(o1)
  1000.  
  1001. #One way to make a non parametric copy of any Part-based object (change MyObjectName by the name of your object)
  1002. import Part
  1003. myShape = FreeCAD.ActiveDocument.Sketch.Shape
  1004. Part.show(myShape)
  1005.  
  1006. #select an object in the gui and parse it in python:
  1007. for o in Gui.Selection.getSelectionEx():
  1008.     print o.ObjectName
  1009.     for s in o.SubElementNames:
  1010.         print "name: ",s
  1011.     for s in o.SubObjects:
  1012.         print "object: ",s
  1013.  
  1014.  
  1015. #Adding and removing an object to a group
  1016. doc=App.activeDocument()
  1017. grp=doc.addObject("App::DocumentObjectGroup", "Group1")
  1018.  
  1019. grp.addObject(f) # adds the circle (f) object to the group grp
  1020. grp.removeObject(f) # removes the circle object from the group grp
  1021.  
  1022.  
  1023. #create a pyqt dialog and display it:
  1024. from PyQt4 import QtGui
  1025. import sys
  1026. sys.path.insert(0,'/home/danfalck/cadcam/freecad/')
  1027. import create_plane
  1028. d = QtGui.QWidget()
  1029. d.ui = create_plane.Ui_Dialog()
  1030. d.ui.setupUi(d)
  1031. d.show()
  1032.  
  1033. import sys
  1034. sys.path.insert(0,'/home/danfalck/cadcam/freecad/')
  1035. import create_plane
  1036. create_plane.plane()
  1037.  
  1038.  
  1039. #rotate view 90 degrees
  1040. import math
  1041. from pivy import coin
  1042. cam = Gui.ActiveDocument.ActiveView.getCameraNode()
  1043. rot = coin.SbRotation()
  1044. rot.setValue(coin.SbVec3f(0,0,1),math.pi/2)
  1045. nrot = cam.orientation.getValue() * rot
  1046. cam.orientation = nrot
  1047.  
  1048. import Part, FreeCAD, math
  1049. from FreeCAD import Base
  1050.  
  1051. helix = Part.makeHelix(1,10,3)
  1052. edge1 = Part.makeLine((2.5,0,-0.125), (2.5,0,0.125))
  1053. edge2 = Part.makeLine((2.5,0,0.125), (3.1,0,0.419))
  1054. edge3 = Part.makeLine((3.1,0,0.419), (3.1,0,-0.419))
  1055. edge4 = Part.makeLine((3.1,0,-0.419), (2.5,0,-0.125))
  1056. section = Part.Wire([edge1,edge2,edge3,edge4])
  1057. makeSolid=0
  1058. isFrenet=1
  1059. pipe = Part.Wire(helix).makePipeShell([section],makeSolid,isFrenet)
  1060. Part.show(pipe)
  1061.  
  1062.  
  1063. import Part, FreeCAD, math
  1064. from FreeCAD import Base
  1065.  
  1066. V1 = Base.Vector(0,0,0)
  1067. V2 = Base.Vector(0,0,10)
  1068. line = Part.Line(V1,V2)
  1069. edge = line.toShape()
  1070.  
  1071. edge1 = Part.makeLine((-1.0,-1.0,0.0), (1.0,-1.0,0.0))
  1072. edge2 = Part.makeLine((1.0,-1.0,0.0), (1.0,1.0,0))
  1073. edge3 = Part.makeLine((1.0,1.0,0), (-1.0,1.0,-0))
  1074. edge4 = Part.makeLine((-1.0,1.0,-0), (-1.0,-1.0,-0))
  1075. section = Part.Wire([edge1,edge2,edge3,edge4])
  1076.  
  1077. Part.show(c1)
  1078. Part.show(wire1)
  1079. makeSolid=0
  1080. isFrenet=1
  1081. pipe = Part.Wire(edge).makePipeShell([section],makeSolid,isFrenet)
  1082. Part.show(pipe)
  1083.  
  1084.  
  1085. #<<<<<<<<<<<<<<<<<<<<<<<<<<<arcs
  1086. import Part, FreeCAD, math
  1087. from FreeCAD import Base
  1088.  
  1089. V1 = Base.Vector(0,0,0)
  1090. V2 = Base.Vector(0,0,10)
  1091. line = Part.Line(V1,V2)
  1092. edge = line.toShape()
  1093.  
  1094. vc1 = Base.Vector(0,-10,0)
  1095. vc2 = Base.Vector(10,0,0)
  1096. vc3 = Base.Vector(0, 10, 0)
  1097. vc4 = Base.Vector(-10,0,0)
  1098. arc1 = Part.Arc(vc1,vc2,vc3)
  1099. arc2 = Part.Arc(vc1,vc4,vc3)
  1100. circ1= Part.Shape([arc1,arc2])
  1101. s1 = Part.Wire(circ1.Edges)
  1102.  
  1103. makeSolid=1
  1104. isFrenet=1
  1105. pipe = Part.Wire(edge).makePipeShell([s1],makeSolid,isFrenet)
  1106. Part.show(pipe)
  1107.  
  1108. object=App.ActiveDocument.addObject("Part::Feature","G1")
  1109. object.Shape=Part.makeLine((4.0 ,4.0 ,0.0 ),( 3.204 ,-19.641 ,0.0))
  1110. object.ViewObject.LineColor = (1.0,0.0,0.0) #red
  1111.  
  1112. object=App.ActiveDocument.addObject("Part::Feature","G2")
  1113. object.Shape=Part.makeCircle(0.254, Base.Vector(2.95,-19.641,-0.793), Base.Vector(0,0,1),0.0,90.0)
  1114. object.ViewObject.LineColor = (0.0,1.0,0.0)#green
  1115. object.ViewObject.LineWidth = .50
  1116.  
  1117.  
  1118.  
  1119. #make a compound:
  1120. shapes = []
  1121. l1 = (Part.makeLine((3.9089 ,0.1209 ,-0.5999 ),( 0.8871 ,0.1209 ,-0.5999)))
  1122. shapes.append(l1)
  1123. l2 = (Part.makeLine((0.8871 ,0.1209 ,-0.5999 ),( 0.8871 ,0.1209 ,0.2218)))
  1124. shapes.append(l2)
  1125. l3 = (Part.makeLine((0.8871 ,0.1209 ,0.2218 ),( -1.0 ,1.0 ,0.2218)))
  1126. shapes.append(l3)
  1127. comp = Part.makeCompound(shapes)
  1128.  
  1129. #make a compound with change line color and line weight
  1130. shapes = []
  1131. l = (Part.makeLine((3.9089 ,0.1209 ,-0.5999 ),( 0.8871 ,0.1209 ,-0.5999)))
  1132. shapes.append(l)
  1133. l = (Part.makeLine((0.8871 ,0.1209 ,-0.5999 ),( 0.8871 ,0.1209 ,0.2218)))
  1134. shapes.append(l)
  1135. l = (Part.makeLine((0.8871 ,0.1209 ,0.2218 ),( -1.0 ,1.0 ,0.2218)))
  1136. shapes.append(l)
  1137. object=App.ActiveDocument.addObject("Part::Feature","path")
  1138. object.Shape=Part.makeCompound(shapes)
  1139. object.ViewObject.LineColor = (0.0,1.0,0.0)#green
  1140. object.ViewObject.LineWidth = 4.0
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146. def arc_angle(start_pt,end_pt,cen_pt):
  1147.     """Return correct direction of arc- for arcs less than 180 degrees """
  1148.     #move the arc so that center is at origin for easy calcs
  1149.     new_start = start_pt.sub(cen_pt))
  1150.     new_end   = end_pt.sub(cen_pt))
  1151.     angle1= fcvec.angle(new_start,(0,0,0))
  1152.  
  1153.     angle2= fcvec.angle(new_end,(0,0,0))
  1154.     #subtract angle1 from angle2
  1155.     angle_diff= (angle2-angle1)
  1156.     return angle_diff
  1157.  
  1158. def arc_angle2(start_pt,mid_pt,cen_pt):
  1159.     """Return direction of arc- for CNC operations-works for arcs less than 180 degrees """
  1160.     #move the arc so that center is at origin for easy calcs
  1161.     new_start=sub_pt(start_pt,cen_pt)
  1162.     new_mid  =sub_pt(mid_pt,cen_pt)
  1163.    
  1164.     angle1= p2p_angle((0,0),new_start)
  1165.     angle2= p2p_angle((0,0),new_mid)
  1166.    
  1167.     #subtract angle1 from angle2
  1168.     angle_diff= (angle2-angle1)
  1169.     return angle_diff
  1170.  
  1171.  
  1172. #parse a profile
  1173. import math
  1174. from draftlibs import fcvec
  1175. from FreeCAD import Vector
  1176.  
  1177. for o in Gui.Selection.getSelectionEx():
  1178.     print o.ObjectName
  1179.     for s in o.SubElementNames:
  1180.         print "name: ",s
  1181.     for s in o.SubObjects:
  1182.         print "object: ",s
  1183.  
  1184. ve1 =s.Vertexes[0].Point
  1185. ve2 =s.Vertexes[-1].Point
  1186. ce = s.Curve.Center
  1187. ang1=-math.degrees(fcvec.angle(ve1.sub(ce)))
  1188. ang2=-math.degrees(fcvec.angle(ve2.sub(ce)))
  1189. cross1 = Vector.cross(ce,ve1)
  1190. cross2 = Vector.cross(ce,ve2)
  1191. cross3 = Vector.cross(ve1,ve2)
  1192. cross4 = Vector.cross(ve2,ce)
  1193. print cross1, cross2,cross3,cross4
  1194. angle = abs(fcvec.angle(ve1.sub(ce),ve2.sub(ce)))
  1195. bul = math.tan(angle/4) #bulge
  1196. cross_product1 = Vector.cross(ce,ve1)
  1197. cross_product2 = Vector.cross(ce,ve2)
  1198. print ce, ve1, ve2
  1199. print cross_product1,cross_product2
  1200.  
  1201.  
  1202.  
  1203. print fcvec.angle(ve1)
  1204. print fcvec.angle(ve2)
  1205. print fcvec.angle(ve1,ve2)
  1206. print s.centerOfCurvatureAt(.5)#midpoint of arc
  1207.  
  1208. if (fcvec.angle(ve2.sub(ce)) < fcvec.angle(ve1.sub(ce))):
  1209.     print 'clockwise'
  1210.  
  1211.  
  1212. import math
  1213. from draftlibs import fcvec,fcgeo
  1214. from FreeCAD import Vector
  1215.  
  1216. for o in Gui.Selection.getSelectionEx():
  1217.     for s in o.SubObjects:
  1218.         if (isinstance(s.Curve,Part.Circle)):
  1219.             mp = fcgeo.findMidpoint(s)
  1220.             ce = s.Curve.Center
  1221.             print "circle-start_pt: ",s.Vertexes[0].Point," center_pt:", s.Curve.Center , "mid_pt: ",mp, " end_pt: ",s.Vertexes[-1].Point
  1222.         elif (isinstance(s.Curve,Part.Line)):
  1223.             print "line-start_pt: ",s.Vertexes[0].Point, " end_pt: ",s.Vertexes[-1].Point
  1224.         else:
  1225.             print "unknown"
  1226.  
  1227.  
  1228. import math
  1229. from draftlibs import fcvec,fcgeo
  1230. from FreeCAD import Vector
  1231.  
  1232.  
  1233. edges_e = Gui.Selection.getSelectionEx()
  1234. sorted_edges = fcgeo.sortEdges(edges_e, edges_e[0].Vertexes[0] )
  1235.  
  1236. #select first edge
  1237. edge1 = []
  1238. for edges in Gui.Selection.getSelectionEx():
  1239.     for s in o.SubObjects:
  1240.         edge1.append(s)
  1241.         first = edge1[0].Curve
  1242.        
  1243.  
  1244. print edge1
  1245. #select group of connected edges
  1246. olEdges = [] # ol stands for ordered list
  1247. for edges in Gui.Selection.getSelectionEx():
  1248.     for s in edges.SubObjects:
  1249.         olEdges.append(s)
  1250.         if (isinstance(s.Curve,Part.Circle)):
  1251.             mp = fcgeo.findMidpoint(s)
  1252.             ce = s.Curve.Center
  1253.             print "circle-start_pt: ",s.Vertexes[0].Point," center_pt:", s.Curve.Center , "mid_pt: ",mp, " end_pt: ",s.Vertexes[-1].Point
  1254.         elif (isinstance(s.Curve,Part.Line)):
  1255.             print "line-start_pt: ",s.Vertexes[0].Point, " end_pt: ",s.Vertexes[-1].Point
  1256.         else:
  1257.             print "unknown"
  1258. sorted_edges = []
  1259. sorted_edges = fcgeo.sortEdges(olEdges,olEdges[0].Vertexes[0])
  1260. for s in sorted_edges:
  1261.     olEdges.append(s)
  1262.     if (isinstance(s.Curve,Part.Circle)):
  1263.         mp = fcgeo.findMidpoint(s)
  1264.         ce = s.Curve.Center
  1265.         print "circle-start_pt: ",s.Vertexes[0].Point," center_pt:", s.Curve.Center , "mid_pt: ",mp, " end_pt: ",s.Vertexes[-1].Point
  1266.     elif (isinstance(s.Curve,Part.Line)):
  1267.         print "line-start_pt: ",s.Vertexes[0].Point, " end_pt: ",s.Vertexes[-1].Point
  1268.     else:
  1269.         print "unknown"
  1270. #from an open profile:
  1271. #unsorted:
  1272. circle-start_pt: Vector (-20.024, 59.6741, 0) center_pt:Vector (-20.024, 48.4448, 0)mid_pt: Vector (-27.9644, 56.3851, 0) end_pt: Vector (-31.2534, 48.4448, 0)
  1273. line-start_pt: Vector (-67.02, -47.7771, 0) end_pt: Vector (2.73822, -47.7771, 0)
  1274. line-start_pt: Vector (-38.7431, 33.4251, 0) end_pt: Vector (-67.1174, 33.4251, 0)
  1275. circle-start_pt: Vector (-38.7431, 33.4251, 0) center_pt:Vector (-38.7431, 40.9147, 0)mid_pt: Vector (-33.4471, 35.6187, 0) end_pt: Vector (-31.2534, 40.9147, 0)
  1276. line-start_pt: Vector (-31.2534, 48.4448, 0) end_pt: Vector (-31.2534, 40.9147, 0)
  1277. line-start_pt: Vector (49.5321, 59.6741, 0) end_pt: Vector (-20.024, 59.6741, 0)
  1278. circle-start_pt: Vector (55.0475, 54.1588, 0) center_pt:Vector (49.5321, 54.1588, 0)mid_pt: Vector (53.4321, 58.0587, 0) end_pt: Vector (49.5321, 59.6741, 0)
  1279. line-start_pt: Vector (55.0475, -8.87558, 0) end_pt: Vector (55.0475, 54.1588, 0)
  1280. circle-start_pt: Vector (49.3822, -14.5408, 0) center_pt:Vector (49.3822, -8.87558, 0)mid_pt: Vector (53.3882, -12.8815, 0) end_pt: Vector (55.0475, -8.87558, 0)
  1281. line-start_pt: Vector (16.1482, -14.5408, 0) end_pt: Vector (49.3822, -14.5408, 0)
  1282. circle-start_pt: Vector (16.1482, -14.5408, 0) center_pt:Vector (16.1482, -22.6633, 0)mid_pt: Vector (10.4048, -16.9198, 0) end_pt: Vector (8.0258, -22.6633, 0)
  1283. line-start_pt: Vector (8.0258, -42.4895, 0) end_pt: Vector (8.0258, -22.6633, 0)
  1284. circle-start_pt: Vector (2.73822, -47.7771, 0) center_pt:Vector (2.73822, -42.4895, 0)mid_pt: Vector (6.4771, -46.2284, 0) end_pt: Vector (8.0258, -42.4895, 0)
  1285.  
  1286. #sorted:
  1287. line-start_pt: Vector (-67.02, -47.7771, 0) end_pt: Vector (2.73822, -47.7771, 0)
  1288. circle-start_pt: Vector (2.73822, -47.7771, 0) center_pt:Vector (2.73822, -42.4895, 0)mid_pt: Vector (6.4771, -46.2284, 0) end_pt: Vector (8.0258, -42.4895, 0)
  1289. line-start_pt: Vector (8.0258, -42.4895, 0) end_pt: Vector (8.0258, -22.6633, 0)
  1290. circle-start_pt: Vector (8.0258, -22.6633, 0) center_pt:Vector (16.1482, -22.6633, 0)mid_pt: Vector (10.4048, -16.9198, 0) end_pt: Vector (16.1482, -14.5408, 0)
  1291. line-start_pt: Vector (16.1482, -14.5408, 0) end_pt: Vector (49.3822, -14.5408, 0)
  1292. circle-start_pt: Vector (49.3822, -14.5408, 0) center_pt:Vector (49.3822, -8.87558, 0)mid_pt: Vector (53.3882, -12.8815, 0) end_pt: Vector (55.0475, -8.87558, 0)
  1293. line-start_pt: Vector (55.0475, -8.87558, 0) end_pt: Vector (55.0475, 54.1588, 0)
  1294. circle-start_pt: Vector (55.0475, 54.1588, 0) center_pt:Vector (49.5321, 54.1588, 0)mid_pt: Vector (53.4321, 58.0587, 0) end_pt: Vector (49.5321, 59.6741, 0)
  1295. line-start_pt: Vector (49.5321, 59.6741, 0) end_pt: Vector (-20.024, 59.6741, 0)
  1296. circle-start_pt: Vector (-20.024, 59.6741, 0) center_pt:Vector (-20.024, 48.4448, 0)mid_pt: Vector (-27.9644, 56.3851, 0) end_pt: Vector (-31.2534, 48.4448, 0)
  1297. line-start_pt: Vector (-31.2534, 48.4448, 0) end_pt: Vector (-31.2534, 40.9147, 0)
  1298. circle-start_pt: Vector (-31.2534, 40.9147, 0) center_pt:Vector (-38.7431, 40.9147, 0)mid_pt: Vector (-33.4471, 35.6187, 0) end_pt: Vector (-38.7431, 33.4251, 0)
  1299. line-start_pt: Vector (-38.7431, 33.4251, 0) end_pt: Vector (-67.1174, 33.4251, 0)
  1300.  
  1301.  
  1302.  
  1303.  
  1304. #select a sketch in the tree:
  1305. lEdges = []
  1306. for edges in Gui.Selection.getSelectionEx():
  1307.     for s in edges.Object.Geometry:
  1308.         lEdges.append(s)
  1309.         #print s
  1310. print lEdges
  1311.  
  1312.  
  1313. import math
  1314. from draftlibs import fcvec,fcgeo
  1315. from FreeCAD import Vector,Base
  1316. #select sketch from the gui window- not the tree
  1317. edges=[]
  1318.  
  1319. for i in Gui.Selection.getSelectionEx():
  1320.     if i.ObjectName == 'Sketch':
  1321.         for e in i.Object.Geometry:
  1322.             if type(e) is Part.ArcOfCircle:
  1323.                 tang1 = e.tangent(e.FirstParameter) ; tang2 = e.tangent(e.LastParameter)
  1324.                
  1325.                 print "Arc -radius: " , e.Radius, " center: ", e.Center, " start angle: ", e.FirstParameter, " end angle: ", e.LastParameter, " tang1: " ,tang1, " tang2: " , tang2
  1326.                 print "Cross Product: " , Vector.cross(Base.Vector(tang1[0][0],tang1[0][1],tang1[0][2]),Base.Vector(tang2[0][0],tang2[0][1],tang2[0][2]))
  1327.                
  1328.             elif type(e)is Part.Line:
  1329.                 print "Line - start point: ", e.StartPoint , " end point: " , e.EndPoint
  1330.  
  1331. i.Object.Shape.Edges
  1332. i.Object.Shape.Edges[0].Vertexes[0].X
  1333. i.Object.Shape.Edges[0].Curve
  1334.  
  1335.  
  1336. for i in Gui.Selection.getSelectionEx():
  1337.     if i.ObjectName == 'Sketch':
  1338.         print "true"
  1339.  
  1340. i.Object.Shape.Edges[0].Curve
  1341. i.Object.GeometryCount
  1342.  
  1343.  
  1344. import math
  1345. from draftlibs import fcvec,fcgeo
  1346. from FreeCAD import Vector,Base
  1347. olEdges = []
  1348. for s in Gui.Selection.getSelectionEx():
  1349.     i = 0
  1350.     if s.ObjectName == 'Sketch':
  1351.         for e in s.Object.Geometry:
  1352.             olEdges.append(s.Object.Shape.Edges[i])
  1353.             i = i+1
  1354.  
  1355. sorted_edges = []
  1356. sorted_edges = fcgeo.sortEdges(olEdges,olEdges[0].Vertexes[0])
  1357. for s in sorted_edges:
  1358.     olEdges.append(s)
  1359.     if (isinstance(s.Curve,Part.Circle)):
  1360.         mp = fcgeo.findMidpoint(s)
  1361.         ce = s.Curve.Center
  1362.         tang1 = s.Curve.tangent(s.ParameterRange[0]) ; tang2 = s.Curve.tangent(s.ParameterRange[1])
  1363.         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]))
  1364.        
  1365.         print "circle - start_pt: ",s.Vertexes[0].Point," center_pt:", s.Curve.Center , "mid_pt: ",mp, " end_pt: ",s.Vertexes[-1].Point
  1366.         print "tangent1: " , tang1, " tangent2: " , tang2, " cross product: ", cross1
  1367.         if cross1[2] > 0:
  1368.             print 'counter clockwise arc'
  1369.         else:
  1370.             print 'clockwise arc'
  1371.     elif (isinstance(s.Curve,Part.Line)):
  1372.         print "line - start_pt: ",s.Vertexes[0].Point, " end_pt: ",s.Vertexes[-1].Point
  1373.     else:
  1374.         print "unknown"
  1375.  
  1376.  
  1377. #export curve elements to heekscnc
  1378. import math
  1379. from draftlibs import fcvec,fcgeo
  1380. from FreeCAD import Vector,Base
  1381. olEdges = []
  1382.  
  1383. shape1 = Part.Shape
  1384. for s in Gui.Selection.getSelectionEx():
  1385.     i = 0
  1386.     if s.ObjectName == 'Sketch':
  1387.         for e in s.Object.Geometry:
  1388.             olEdges.append(s.Object.Shape.Edges[i])
  1389.            
  1390.             i = i+1
  1391.  
  1392. print "comment('Sketch from FreeCAD')"
  1393. print "curve = area.Curve()"
  1394.  
  1395. sorted_edges = []
  1396.  
  1397. sorted_edges = fcgeo.sortEdges(olEdges,olEdges[0].Vertexes[0])
  1398.  
  1399. print "curve.append(area.Point(" , olEdges[0].Vertexes[-1].X , "," , olEdges[0].Vertexes[-1].Y, "))"
  1400.  
  1401. for s in sorted_edges:
  1402.     olEdges.append(s)
  1403.     if (isinstance(s.Curve,Part.Circle)):
  1404.         mp = fcgeo.findMidpoint(s)
  1405.         ce = s.Curve.Center
  1406.         tang1 = s.Curve.tangent(s.ParameterRange[0]) ; tang2 = s.Curve.tangent(s.ParameterRange[1])
  1407.         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]))
  1408.         if cross1[2] > 0:
  1409.             direct = '1 ' #we seem to be working in a rh system in FreeCAD
  1410.         else:
  1411.             direct = '-1 '
  1412.         print "curve.append(area.Vertex(",direct, ", area.Point( ", s.Vertexes[-1].Point[0],", ",s.Vertexes[-1].Point[1], "), area.Point(", s.Curve.Center [0], ", " , s.Curve.Center[1], ")))"
  1413.  
  1414.     elif (isinstance(s.Curve,Part.Line)):
  1415.         print "curve.append(area.Point( ",s.Vertexes[-1].Point[0],", " ,s.Vertexes[-1].Point[1], "))"
  1416.     else:
  1417.         pass
  1418. #export curve elements to heekscnc
  1419. #to reverse the curve:
  1420. print "curve.Reverse()"
  1421.  
  1422. 24"
  1423. #sketch stuff:
  1424. o.Object.Label
  1425. #gives the user assigned label of the sketch
  1426.  
  1427. o.Object.Shape.Edges[0].Curve #gives geo info about element in sketch (ignores construction lines)
  1428.  
  1429.  
  1430. #constraints stuff
  1431. from Sketcher import *
  1432. from Part import *
  1433. from FreeCAD import *
  1434.  
  1435. # set some constances for the constraints
  1436. StartPoint = 1
  1437. EndPoint = 2
  1438. CenterPoint = 3
  1439. # create a document and a Sketch object
  1440. #if(App.activeDocument() == None):App.newDocument()
  1441. f = App.activeDocument().addObject("Sketcher::SketchObject","newSketch")
  1442. # add geometry to the sketch
  1443. #f.Geometry = ()
  1444. #f.Geometry = (Line(Vector(0,20,0),Vector(0,0,0)))
  1445. f.addGeometry(Line(Vector(0,20,0),Vector(0,0,0)))
  1446. f.addGeometry(Line(Vector(0,0,0),Vector(20,0,0)))
  1447. #addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(2.738215,-33.047379,0),App.Vector(0,0,1),68.307899),0.674741,2.256331))
  1448. #App.ActiveDocument.Sketch.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(0.000000,-0.000000,0),App.Vector(0,0,1),20.000000),0.000000,3.141593))
  1449. #f.Geometry = [Circle(Vector(2.738215,-33.047379,0),Vector(0,0,1),2.0)]
  1450. #f.addGeometry(fillet(0,1,Vector(-0.000000,1.000000,0),Vector(1.000000,-0.000000,0),1.000000))
  1451. #f.addGeometry(ArcOfCircle(Part.Circle(App.Vector(0.000000,-0.000000,0),App.Vector(0,0,1),20.000000),0.000000,1.5707963267948966))
  1452. f.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(0.000000,-0.000000,0),App.Vector(0,0,1),20.000000),0.000000,1.5707963267948966))
  1453. f.Constraints = [Constraint('Vertical',0),Constraint('Horizontal',1)]
  1454. l = f.Constraints
  1455. #l.append(Constraint('DistanceX',0,0.0))
  1456. l.append(Constraint('Coincident',0,EndPoint,1,StartPoint))
  1457. l.append(Constraint('Coincident',0,StartPoint,2,EndPoint))
  1458. l.append(Constraint('Coincident',1,EndPoint,2,StartPoint))
  1459. l.append(Constraint('Coincident',2,CenterPoint,0,EndPoint))
  1460. l.append(Constraint('Radius',2,3.333))
  1461. l.append(Constraint('DistanceX',0,2,6.819825))
  1462. l.append(Constraint('DistanceY',0,2,6.038286))
  1463. f.Constraints = l
  1464. p = App.activeDocument().addObject("PartDesign::Pad","Pad")
  1465. p.Sketch =f
  1466. p.Length=1.0
  1467. p.Reversed = 0
  1468. p.Midplane = 0
  1469. p.Length2 = 100.00
  1470. p.Type = 0
  1471. p.UpToFace = None
  1472.  
  1473. App.activeDocument().recompute()
  1474.  
  1475.  
  1476. #import DXF to Sketcher
  1477. import toSketch
  1478. import FreeCAD
  1479. dxf = "/home/danfalck/Documents/freecad/dxf_import/test_entities.dxf"
  1480. doc = FreeCAD.ActiveDocument.Name
  1481. reload(toSketch) #just for now
  1482. toSketch.insert(dxf,doc)
  1483.  
  1484. #lists
  1485.  
  1486. olEdges = []
  1487. i = 0
  1488. for s in Gui.Selection.getSelectionEx():
  1489.    if s.Object.Type == 'Sketcher::SketchObject':
  1490.        for e in s.Object.Geometry:
  1491.            while i < 8:
  1492.                if not e.Construction:
  1493.                    olEdges.append(s.Object.Shape.Edges[i])
  1494.                    print 'non construction'
  1495.                    print s.Object.Geometry[i]
  1496.                    print i
  1497.                    
  1498.                else:
  1499.                    print 'construction'
  1500.                    print s.Object.Geometry[i]
  1501.                    print i
  1502.                i= i + 1
  1503.  
  1504. olEdges = []
  1505.  
  1506. for s in Gui.Selection.getSelectionEx():
  1507.    i = 0
  1508.    if s.Object.Type == 'Sketcher::SketchObject':
  1509.        for e in s.Object.Geometry:
  1510.            while i < len(s.Object.Geometry):
  1511.                if not s.Object.Geometry[i].Construction:
  1512.                    olEdges.append(s.Object.Geometry[i])
  1513.                    print s.Object.Geometry[i]
  1514.                    print i
  1515.                i= i + 1
  1516.  
  1517.  
  1518. <Line (-61.8648;2.25316,0) (-85.2121;7.39871,0) >
  1519. 0
  1520. <Line (-86.501;7.31343,0) (-100.627;2.1721,0) >
  1521. 1
  1522. <Line (-100.627;-2.17444,0) (-86.5026;-7.31894,0) >
  1523. 2
  1524. <Line (-85.2138;-7.40451,0) (-61.8653;-2.26422,0) >
  1525. 3
  1526. ArcOfCircle (Radius : 2.31284, Position : (-99.8356, -0.00125464, 0), Direction : (0, 0, 1), Parameter : (1.91986, 4.3631))
  1527. 16
  1528. ArcOfCircle (Radius : 2.31284, Position : (-62.3625, -0.00547331, 0), Direction : (0, 0, 1), Parameter : (4.92909, 7.63706))
  1529. 17
  1530. ArcOfCircle (Radius : 2.31284, Position : (-85.7099, 5.14007, 0), Direction : (0, 0, 1), Parameter : (1.35387, 1.91986))
  1531. 18
  1532. ArcOfCircle (Radius : 2.31284, Position : (-85.7111, -5.14576, 0), Direction : (0, 0, 1), Parameter : (4.3631, 4.92909))
  1533. 19
  1534.  
  1535. shape1 = Part.Shape
  1536. for s in Gui.Selection.getSelectionEx():
  1537.    i = 0
  1538.    j = 0
  1539.    if s.Object.Type == 'Sketcher::SketchObject':
  1540.        for e in s.Object.Geometry:
  1541.            while i < (len(s.Object.Geometry)):
  1542.                if not s.Object.Geometry[i].Construction:
  1543.                    olEdges.append(s.Object.Shape.Edges[j])
  1544.                    j = j+1
  1545.                i = i+1
  1546.    elif s.Object.Type == 'Part::TopoShape'
  1547.    else:
  1548.        for e in s.Object.Shape.Edges:
  1549.                olEdges.append(s.Object.Shape.Edges[i])
  1550.                i = i+1
  1551.  
  1552. for s in Gui.Selection.getSelectionEx():
  1553.    for e in s.Object.Shape.Edges:
  1554.        print e
  1555.  
  1556.  
  1557. #get a point from the screen
  1558.  
  1559. #Basically you need to do this:
  1560. #- create a callback function, that will be called whenever a coin "event" occurs (a mouse button is pressed, a key is pressed, etc)
  1561. #- register that function so coin knows it needs to execute it
  1562. #- inside that function, check if the event is what you want (a left mouse click)
  1563. #- if yes, check the position of the mouse, and take the corresponding 3D point
  1564. #- unregister the callback function
  1565.  
  1566.  
  1567. import FreeCAD
  1568. import FreeCADGui, Part
  1569. from pivy.coin import *
  1570. class point:
  1571.    "this class will print out the coords of a point after the user clicked a point on the screen"
  1572.    def __init__(self):
  1573.        self.view = FreeCADGui.ActiveDocument.ActiveView
  1574.        self.stack = []
  1575.        self.callback = self.view.addEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.getpoint)
  1576.    def getpoint(self,event_cb):
  1577.        event = event_cb.getEvent()
  1578.        if event.getState() == SoMouseButtonEvent.DOWN:
  1579.            pos = event.getPosition()
  1580.            point = self.view.getPoint(pos[0],pos[1])
  1581.            self.stack.append(point)
  1582.            if len(self.stack) == 2:
  1583.                FreeCAD.Console.PrintMessage("a point at" +str(self.stack[0])+","+str(self.stack[1])+ "\n")
  1584.                l = Part.Line(self.stack[0],self.stack[1])
  1585.                shape = l.toShape()
  1586.                Part.show(shape)
  1587.                self.view.removeEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.callback)
  1588.  
  1589. point()
  1590.  
  1591. from pivy import coin
  1592. class PointTracker:
  1593.    def __init__(self,view):
  1594.        self.view=view
  1595.        self.__cb__ = self.view.addEventCallbackPivy(coin.SoMouseButtonEvent.getClassTypeId(),self.mouseButton)
  1596.  
  1597.    def mouseButton(self, event_cb):
  1598.        FreeCAD.Console.PrintMessage("Point clicked")
  1599.        self.result=event_cb.getEvent().getPosition()
  1600.        self.view.removeEventCallbackPivy(coin.SoMouseButtonEvent.getClassTypeId(),self.__cb__)
  1601.  
  1602. tracker=PointTracker(Gui.ActiveDocument.ActiveView)
  1603.  
  1604. import Draft
  1605. from FreeCAD import Vector
  1606. #trying out Shape2DView
  1607. for ob in Draft.getSelection():
  1608.    Draft.move(Draft.makeShape2DView(ob,Vector(0,0,1)),Vector(0,35,0))
  1609.    Draft.move(Draft.rotate(Draft.makeShape2DView(ob,Vector(0,-1,0)),-90.0,Vector(0,0,0),Vector(0,0,1),False),Vector(0,20,0))
  1610.    Draft.move(Draft.rotate(Draft.makeShape2DView(ob,Vector(1,0,0)),90.0,Vector(0,0,0),Vector(0,0,1),False),Vector(15,20,0))
  1611.  
  1612.  
  1613. #working on exporting blocks
  1614. for ob in Draft.getSelection():
  1615.    print ob
  1616. isinstance (edge.Curve,Part.Circle)
  1617.  
  1618. #clicking on points again
  1619. from pivy import coin
  1620. v=Gui.activeDocument().activeView()
  1621.  
  1622. #This class logs any mouse button events. As the registered callback function fires twice for 'down' and
  1623. #'up' events we need a boolean flag to handle this.
  1624. class ViewObserver:
  1625.   def logPosition(self, info):
  1626.       down = (info["State"] == "DOWN")
  1627.       pos = info["Position"]
  1628.       if (down):
  1629.           FreeCAD.Console.PrintMessage("Clicked on position: ("+str(pos[0])+", "+str(pos[0])+")\n")
  1630.  
  1631. o = ViewObserver()
  1632. c = v.addEventCallback("SoMouseButtonEvent",o.logPosition)
  1633. #kill the function with this:
  1634. v.removeEventCallback("SoMouseButtonEvent",c)
  1635.  
  1636.  
  1637.  
  1638. #coin stuff
  1639. from pivy import coin
  1640. sg = Gui.ActiveDocument.ActiveView.getSceneGraph()
  1641. co = coin.SoCoordinate3()
  1642. pts = [[0,0,0],[10,0,0]]
  1643. co.point.setValues(0,len(pts),pts)
  1644. ma = coin.SoBaseColor()
  1645. ma.rgb = (1,0,0)
  1646. li = coin.SoLineSet()
  1647. li.numVertices.setValue(2)
  1648. no = coin.SoSeparator()
  1649. no.addChild(co)
  1650. no.addChild(ma)
  1651. no.addChild(li)
  1652. sg.addChild(no)
  1653.  
  1654. #remove it:
  1655. sg.removeChild(no)
  1656.  
  1657.  
  1658.  
  1659. from pivy import coin
  1660. sg = Gui.ActiveDocument.ActiveView.getSceneGraph()
  1661. color = coin.SoBaseColor()
  1662. color.rgb = (0,0,1)
  1663. marker = coin.SoMarkerSet() # this is the marker symbol
  1664. marker.markerIndex = coin.SoMarkerSet.CIRCLE_FILLED_9_9
  1665. coords = coin.SoCoordinate3() # this is the coordinate
  1666. #coords.point.setValue((0,0,0)) just one point
  1667. pts = [[0,0,0],[10,0,0]]#skip this for just one point
  1668. coords.point.setValues(0,len(pts),pts)
  1669. node = coin.SoAnnotation()
  1670. node.addChild(coords)
  1671. node.addChild(color)
  1672. node.addChild(marker)
  1673. sg.addChild(node)
  1674.  
  1675. #remove them:
  1676. sg.removeChild(node)
  1677.  
  1678. from pivy import coin
  1679. def makeDot(point,sg):
  1680.    color = coin.SoBaseColor()
  1681.    color.rgb = (0,0,1)
  1682.    marker = coin.SoMarkerSet() # this is the marker symbol
  1683.    marker.markerIndex = coin.SoMarkerSet.CIRCLE_FILLED_9_9
  1684.    coords = coin.SoCoordinate3() # this is the coordinate
  1685.    x,y,z = point
  1686.    coords.point.setValue(x,y,z)
  1687.    node = coin.SoAnnotation()
  1688.    node.addChild(coords)
  1689.    node.addChild(color)
  1690.    node.addChild(marker)
  1691.    sg.addChild(node)
  1692.  
  1693. sg = Gui.ActiveDocument.ActiveView.getSceneGraph()
  1694. makeDot((0,0,0),sg)
  1695. makeDot((10,0,0),sg)
  1696. makeDot((10,10,0),sg)
  1697. makeDot((0,10,0),sg)
  1698. sg.removeAllChildren()
  1699.  
  1700. #now let's try that with the mouse:
  1701. from pivy import coin
  1702. v=Gui.ActiveDocument.ActiveView
  1703. #sg = v.getSceneGraph()
  1704. #This class logs any mouse button events. As the registered callback function fires twice for 'down' and
  1705. #'up' events we need a boolean flag to handle this.
  1706. class ViewObserver:
  1707.    def logPosition(self, info):
  1708.        down = (info["State"] == "DOWN")
  1709.        pos = info["Position"]
  1710.        if (down):
  1711.            FreeCAD.Console.PrintMessage("Clicked on position: ("+str(pos[0])+", "+str(pos[1])+")\n")
  1712.            x,y = pos[0],pos[1]
  1713.            #print pos[0], pos[1]
  1714.            makeDot((x,y,0),Gui.ActiveDocument.ActiveView.getSceneGraph())
  1715.  
  1716. def makeDot(point,sg):
  1717.    color = coin.SoBaseColor()
  1718.    color.rgb = (0,0,1)
  1719.    marker = coin.SoMarkerSet() # this is the marker symbol
  1720.    marker.markerIndex = coin.SoMarkerSet.CIRCLE_FILLED_9_9
  1721.    coords = coin.SoCoordinate3() # this is the coordinate
  1722.    x,y,z = point
  1723.    coords.point.setValue(x,y,z)
  1724.    node = coin.SoAnnotation()
  1725.    node.addChild(coords)
  1726.    node.addChild(color)
  1727.    node.addChild(marker)
  1728.    sg.addChild(node)
  1729.  
  1730. o = ViewObserver()
  1731. c = v.addEventCallback("SoMouseButtonEvent",o.logPosition)
  1732. #kill the function with this:
  1733. v.removeEventCallback("SoMouseButtonEvent",c)
  1734.  
  1735. #kill the dots:
  1736. Gui.ActiveDocument.ActiveView.getSceneGraph().removeAllChildren()
  1737.  
  1738. #create dots on the screen with the mouse:
  1739. import FreeCAD
  1740. import FreeCADGui, Part
  1741. from pivy.coin import *
  1742. class point:
  1743.    "this class will print out the coords of a point after the user clicked a point on the screen"
  1744.    def __init__(self):
  1745.        self.color = coin.SoBaseColor()
  1746.        self.color.rgb = (0,0,1)
  1747.        self.marker = coin.SoMarkerSet()
  1748.        self.marker.markerIndex = coin.SoMarkerSet.CIRCLE_FILLED_9_9
  1749.        self.view = FreeCADGui.ActiveDocument.ActiveView
  1750.        self.sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
  1751.        self.node = coin.SoAnnotation()
  1752.        self.stack = []
  1753.        self.callback = self.view.addEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.getpoint)
  1754.    def getpoint(self,event_cb):
  1755.        event = event_cb.getEvent()
  1756.        if event.getState() == SoMouseButtonEvent.DOWN:
  1757.            pos = event.getPosition()
  1758.            point = self.view.getPoint(pos[0],pos[1])
  1759.            self.stack.append(point)
  1760.            if len(self.stack) == 1:
  1761.                FreeCAD.Console.PrintMessage("a point at" +str(self.stack[0])+ "\n")
  1762.                self.coords = coin.SoCoordinate3()
  1763.                #self.coords.point.setValue(0,0,0)
  1764.                self.coords.point.setValue(self.stack[0][0],self.stack[0][1],0)
  1765.                self.node.addChild(self.coords)
  1766.                self.node.addChild(self.color)
  1767.                self.node.addChild(self.marker)
  1768.                self.sg.addChild(self.node)#<<<
  1769.                self.view.removeEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.callback)
  1770. point()
  1771.  
  1772.  
  1773.  
  1774. #create dots on the screen with the mouse and remove them or restore them:
  1775. import FreeCAD
  1776. import FreeCADGui, Part
  1777. from pivy.coin import *
  1778. class point:
  1779.    "this class will print out the coords of a point after the user clicked a point on the screen and do some point manipulation"
  1780.    def __init__(self,path_pt):
  1781.        self.start_end = path_pt
  1782.        self.color = coin.SoBaseColor()
  1783.        if self.start_end =='start':
  1784.            self.color.rgb = (0,0,1)
  1785.        else:
  1786.            self.color.rgb = (0,1,0)
  1787.        self.marker = coin.SoMarkerSet()
  1788.        self.marker.markerIndex = coin.SoMarkerSet.CROSS_9_9
  1789.        self.view = FreeCADGui.ActiveDocument.ActiveView
  1790.        self.sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
  1791.        self.node = coin.SoAnnotation()
  1792.        self.stack = []
  1793.        self.callback = self.view.addEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.clickPoint)
  1794.        #FreeCAD.Console.PrintMessage("this is a "+ str(self.start_end)+ " point\n")
  1795.    def clickPoint(self,event_cb):
  1796.        "this is how the mouse event gets recorded"
  1797.        event = event_cb.getEvent()
  1798.        if event.getState() == SoMouseButtonEvent.DOWN:
  1799.            pos = event.getPosition()
  1800.            point = self.view.getPoint(pos[0],pos[1])
  1801.            self.stack.append(point)
  1802.            if len(self.stack) == 1:
  1803.                FreeCAD.Console.PrintMessage("a point at" +str(self.stack[0])+ "\n")
  1804.                self.coords = coin.SoCoordinate3()
  1805.                #self.coords.point.setValue(0,0,0)
  1806.                self.coords.point.setValue(self.stack[0][0],self.stack[0][1],0)
  1807.                self.node.addChild(self.coords)
  1808.                self.node.addChild(self.color)
  1809.                self.node.addChild(self.marker)
  1810.                self.sg.addChild(self.node)#<<<
  1811.                self.view.removeEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.callback)
  1812.                FreeCAD.Console.PrintMessage("this is a "+ str(self.start_end)+ " point\n")
  1813.    def hidePoint(self):
  1814.        "hides the point on the screen"
  1815.        self.sg.removeChild(self.node)#clears node
  1816.    def showPoint(self):#show it again, after being erased
  1817.        "shows the point on the screen"
  1818.        self.coords.point.setValue(self.stack[0][0],self.stack[0][1],0)
  1819.        self.node.addChild(self.coords)
  1820.        self.node.addChild(self.color)
  1821.        self.node.addChild(self.marker)
  1822.        self.sg.addChild(self.node)#<<<
  1823.    def movePoint(self,newCoords):
  1824.        "moves the point to a new location - movePoint((new coordinate))"
  1825.        self.sg.removeChild(self.node)#clears node
  1826.        self.coords.point.setValue(newCoords[0],newCoords[1],0)
  1827.        self.node.addChild(self.coords)
  1828.        self.node.addChild(self.color)
  1829.        self.node.addChild(self.marker)
  1830.        self.sg.addChild(self.node)#<<<
  1831.        self.stack[0][0] = float(newCoords[0])
  1832.        self.stack[0][1] = float(newCoords[1])
  1833.        
  1834.    def coordLocation(self):
  1835.        "returns the coordinate location of the point"
  1836.        return self.stack[0][0],self.stack[0][1]
  1837.  
  1838.  
  1839.  
  1840. p1 = point('start') #click on the screen
  1841. p1.hidePoint() #erases the point you just created
  1842. p1.showPoint() #shows the point again
  1843. p1.movePoint((1,0,0))#move it somewhere else
  1844. p1.Coords() #gives it's coordinates
  1845.  
  1846.  
  1847. #compound objects
  1848. import Part
  1849. cmpnd=Part.Compound()
  1850. selection = FreeCADGui.Selection.getSelection()
  1851. for obj in selection:
  1852.    cmpnd.add(obj.Shape) # add all the shape data from the objects you need
  1853.  
  1854. Part.show(cmpnd)
  1855.  
  1856. #grouping again:
  1857. #Adding and removing an object to a group
  1858. doc=App.activeDocument()
  1859. grp=doc.addObject("App::DocumentObjectGroup", "Group1")
  1860. for o in Gui.Selection.getSelectionEx():
  1861.    for s in o.SubObjects:
  1862.        grp.addObject(s)
  1863.  
  1864.  
  1865.  
  1866.  
  1867. grp.removeObject(f) # removes the circle object from the group grp
  1868.  
  1869. import FreeCAD
  1870. from FreeCAD import Part
  1871. class Line:
  1872.    def __init__(self, obj):
  1873.        "App two point properties"
  1874.        obj.addProperty("App::PropertyVector","p1","Line","Start point")
  1875.        obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(1,0,0)
  1876.        obj.Proxy = self
  1877.  
  1878.    def execute(self, fp):
  1879.        "Print a short message when doing a recomputation, this method is mandatory"
  1880.        fp.Shape = Part.makeLine(fp.p1,fp.p2)
  1881.  
  1882. a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Line")
  1883. Line(a)
  1884. a.ViewObject.Proxy=0 # just set it to something different from None (this assignment is needed to run an internal notification)
  1885.  
  1886. #find the area of a solid part:
  1887. import Part
  1888. s = Gui.Selection.getSelectionEx()
  1889. p = s[0].Object.Shape
  1890. p.Area
  1891.  
  1892. #here's an even easier way that Yorik showed me:
  1893. FreeCAD.ActiveDocument.ActiveObject.Shape.Area
  1894.  
  1895.  
  1896. #animation example from werner:
  1897. from PyQt4 import QtCore
  1898. from FreeCAD import Base
  1899.  
  1900. loop=QtCore.QEventLoop()
  1901. timer=QtCore.QTimer()
  1902. timer.setSingleShot(True)
  1903. QtCore.QObject.connect(timer,QtCore.SIGNAL("timeout()"),loop,QtCore.SLOT("quit()"))
  1904.  
  1905. duration = 3000 # in ms
  1906. steps=300
  1907. ms=duration/steps
  1908. obj=App.ActiveDocument.ActiveObject
  1909. start=obj.Placement.Base
  1910. end=Base.Vector(12,23,34)
  1911. for i in range(steps):
  1912.    s=float(i)/steps
  1913.    px=start.x * (1-s) + end.x * s
  1914.    py=start.y * (1-s) + end.y * s
  1915.    pz=start.z * (1-s) + end.z * s
  1916.    obj.Placement.Base=Base.Vector(px,py,pz)
  1917.    timer.start(ms)
  1918.    loop.exec_(QtCore.QEventLoop.ExcludeUserInputEvents)
  1919.  
  1920. obj.Placement.Base=end
  1921.  
  1922.  
  1923. # use freetype extension to make text string
  1924. String = 'FreeCAD'
  1925. FontPath = '/usr/share/fonts/truetype/freefont/'
  1926. FontName = 'FreeMonoBoldOblique.ttf'
  1927. Height  = 2000
  1928. Track = 0
  1929. s = Part.makeWireString(String,FontPath,FontName,Height,Track)
  1930.  
  1931. for char in s:
  1932.    for contour in char:
  1933.            Part.show(contour)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement