DistanceBolt.py

Go to the documentation of this file.
00001 """
00002 An example for a high-level cutsom feature object to form a full-parametric distance bolt.
00003 
00004 ***************************************************************************
00005 *   Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00006 *                                                                         *
00007 *   This file is part of the FreeCAD CAx development system.              *
00008 *                                                                         *
00009 *   This program is free software; you can redistribute it and/or modify  *
00010 *   it under the terms of the GNU General Public License (GPL)            *
00011 *   as published by the Free Software Foundation; either version 2 of     *
00012 *   the License, or (at your option) any later version.                   *
00013 *   for detail see the LICENCE text file.                                 *
00014 *                                                                         *
00015 *   FreeCAD is distributed in the hope that it will be useful,            *
00016 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00017 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00018 *   GNU Library General Public License for more details.                  *
00019 *                                                                         *
00020 *   You should have received a copy of the GNU Library General Public     *
00021 *   License along with FreeCAD; if not, write to the Free Software        *
00022 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
00023 *   USA                                                                   *
00024 *                                                                         *
00025 ***************************************************************************
00026 """
00027 
00028 __author__ = "Werner Mayer <wmayer@users.sourceforge.net>"
00029 
00030 import FreeCAD, Part, math
00031 from FreeCAD import Base
00032 
00033 class DistanceBolt:
00034         def __init__(self, obj):
00035                 ''' Add the properties: Length, Edges, Radius, Height '''
00036                 obj.addProperty("App::PropertyInteger","Edges","Bolt","Number of edges of the outline").Edges=6
00037                 obj.addProperty("App::PropertyLength","Length","Bolt","Length of the edges of the outline").Length=10.0
00038                 obj.addProperty("App::PropertyLength","Radius","Bolt","Radius of the inner circle").Radius=4.0
00039                 obj.addProperty("App::PropertyLength","Height","Bolt","Height of the extrusion").Height=20.0
00040                 obj.Proxy = self
00041 
00042         def onChanged(self, fp, prop):
00043                 if prop == "Edges" or prop == "Length" or prop == "Radius" or prop == "Height":
00044                         self.execute(fp)
00045 
00046         def execute(self, fp):
00047                 edges = fp.Edges
00048                 if edges < 3:
00049                         edges = 3
00050                 length = fp.Length
00051                 radius = fp.Radius
00052                 height = fp.Height
00053 
00054                 m=Base.Matrix()
00055                 m.rotateZ(math.radians(360.0/edges))
00056 
00057                 # create polygon
00058                 polygon = []
00059                 v=Base.Vector(length,0,0)
00060                 for i in range(edges):
00061                         polygon.append(v)
00062                         v = m.multiply(v)
00063                 polygon.append(v)
00064                 wire = Part.makePolygon(polygon)
00065 
00066                 # create circle
00067                 circ=Part.makeCircle(radius)
00068 
00069                 # Create the face with the polygon as outline and the circle as hole
00070                 face=Part.Face([wire,Part.Wire(circ)])
00071 
00072                 # Extrude in z to create the final solid
00073                 extrude=face.extrude(Base.Vector(0,0,height))
00074                 fp.Shape = extrude
00075 
00076 def makeDistanceBolt():
00077         doc = FreeCAD.activeDocument()
00078         if doc == None:
00079                 doc = FreeCAD.newDocument()
00080         bolt=doc.addObject("Part::FeaturePython","Distance_Bolt")
00081         bolt.Label = "Distance bolt"
00082         DistanceBolt(bolt)
00083         bolt.ViewObject.Proxy=0
00084 

Generated on Wed Nov 23 19:00:05 2011 for FreeCAD by  doxygen 1.6.1