Code: Select all
import FreeCAD,Part
class ParametricRectangle:
def __init__(self,obj):
obj.Proxy = self
obj.addProperty("App::PropertyFloat","Length").Length = 10.0
obj.addProperty("App::PropertyFloat","Width").Width = 5.0
def execute(self,obj):
# first we need to make sure the values of Length and Width are not 0
# otherwise the Part.Line will complain that both points are equal
if (obj.Length == 0) or (obj.Width == 0):
# if yes, exit this method without doing anything
return
# we create 4 points for the 4 corners
v1 = FreeCAD.Vector(0,0,0)
v2 = FreeCAD.Vector(obj.Length,0,0)
v3 = FreeCAD.Vector(obj.Length,obj.Width,0)
v4 = FreeCAD.Vector(0,obj.Width,0)
# we create 4 edges
e1 = Part.Line(v1,v2).toShape()
e2 = Part.Line(v2,v3).toShape()
e3 = Part.Line(v3,v4).toShape()
e4 = Part.Line(v4,v1).toShape()
# we create a wire
w = Part.Wire([e1,e2,e3,e4])
# we create a face
f = Part.Face(w)
# All shapes have a Placement too. We give our shape the value of the placement
# set by the user. This will move/rotate the face automatically.
f.Placement = obj.Placement
# all done, we can attribute our shape to the object!
obj.Shape = f
# MAIN SCRIPT: Create a parametric object
myObj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Rectangle")
ParametricRectangle(myObj)
myObj.ViewObject.Proxy = 0 # this is mandatory unless we code the ViewProvider too
App.ActiveDocument.recompute()
OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.10460 (Git)
Build type: Release
Branch: master
Hash: 91c59c7910436c44ede608e29d9a90a287121a11
Python version: 2.7.8
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.0.0