FeaturePython.py

Go to the documentation of this file.
00001 """
00002 Examples for a feature class and its view provider.
00003 
00004 ***************************************************************************
00005 *   Copyright (c) 2009 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 from pivy import coin
00033 
00034 class PartFeature:
00035         def __init__(self, obj):
00036                 obj.Proxy = self
00037 
00038 class Box(PartFeature):
00039         def __init__(self, obj):
00040                 PartFeature.__init__(self, obj)
00041                 ''' Add some custom properties to our box feature '''
00042                 obj.addProperty("App::PropertyLength","Length","Box","Length of the box").Length=1.0
00043                 obj.addProperty("App::PropertyLength","Width","Box","Width of the box").Width=1.0
00044                 obj.addProperty("App::PropertyLength","Height","Box", "Height of the box").Height=1.0
00045 
00046         def onChanged(self, fp, prop):
00047                 ''' Print the name of the property that has changed '''
00048                 FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
00049                 if prop == "Length" or prop == "Width" or prop == "Height":
00050                         fp.Shape = Part.makeBox(fp.Length,fp.Width,fp.Height)
00051 
00052         def execute(self, fp):
00053                 ''' Print a short message when doing a recomputation, this method is mandatory '''
00054                 FreeCAD.Console.PrintMessage("Recompute Python Box feature\n")
00055                 fp.Shape = Part.makeBox(fp.Length,fp.Width,fp.Height)
00056 
00057 class ViewProviderBox:
00058         def __init__(self, obj):
00059                 ''' Set this object to the proxy object of the actual view provider '''
00060                 obj.Proxy = self
00061 
00062         def attach(self, obj):
00063                 ''' Setup the scene sub-graph of the view provider, this method is mandatory '''
00064                 return
00065 
00066         def updateData(self, fp, prop):
00067                 ''' If a property of the handled feature has changed we have the chance to handle this here '''
00068                 return
00069 
00070         def getDisplayModes(self,obj):
00071                 ''' Return a list of display modes. '''
00072                 modes=[]
00073                 return modes
00074 
00075         def getDefaultDisplayMode(self):
00076                 ''' Return the name of the default display mode. It must be defined in getDisplayModes. '''
00077                 return "Shaded"
00078 
00079         def setDisplayMode(self,mode):
00080                 ''' Map the display mode defined in attach with those defined in getDisplayModes.
00081                 Since they have the same names nothing needs to be done. This method is optinal.
00082                 '''
00083                 return mode
00084 
00085         def onChanged(self, vp, prop):
00086                 ''' Print the name of the property that has changed '''
00087                 FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
00088 
00089         def getIcon(self):
00090                 ''' Return the icon in XMP format which will appear in the tree view. This method is optional
00091                 and if not defined a default icon is shown.
00092                 '''
00093                 return """
00094                         /* XPM */
00095                         static const char * ViewProviderBox_xpm[] = {
00096                         "16 16 6 1",
00097                         "       c None",
00098                         ".      c #141010",
00099                         "+      c #615BD2",
00100                         "@      c #C39D55",
00101                         "#      c #000000",
00102                         "$      c #57C355",
00103                         "        ........",
00104                         "   ......++..+..",
00105                         "   .@@@@.++..++.",
00106                         "   .@@@@.++..++.",
00107                         "   .@@  .++++++.",
00108                         "  ..@@  .++..++.",
00109                         "###@@@@ .++..++.",
00110                         "##$.@@$#.++++++.",
00111                         "#$#$.$$$........",
00112                         "#$$#######      ",
00113                         "#$$#$$$$$#      ",
00114                         "#$$#$$$$$#      ",
00115                         "#$$#$$$$$#      ",
00116                         " #$#$$$$$#      ",
00117                         "  ##$$$$$#      ",
00118                         "   #######      "};
00119                         """
00120 
00121         def __getstate__(self):
00122                 ''' When saving the document this object gets stored using Python's cPickle module.
00123                 Since we have some un-pickable here -- the Coin stuff -- we must define this method
00124                 to return a tuple of all pickable objects or None.
00125                 '''
00126                 return None
00127 
00128         def __setstate__(self,state):
00129                 ''' When restoring the pickled object from document we have the chance to set some
00130                 internals here. Since no data were pickled nothing needs to be done here.
00131                 '''
00132                 return None
00133 
00134 
00135 def makeBox():
00136         FreeCAD.newDocument()
00137         a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Box")
00138         Box(a)
00139         ViewProviderBox(a.ViewObject)
00140 
00141 # -----------------------------------------------------------------------------
00142 
00143 class Line:
00144         def __init__(self, obj):
00145                 ''' Add two point properties '''
00146                 obj.addProperty("App::PropertyVector","p1","Line","Start point")
00147                 obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(1,0,0)
00148                 obj.Proxy = self
00149 
00150         def execute(self, fp):
00151                 ''' Print a short message when doing a recomputation, this method is mandatory '''
00152                 fp.Shape = Part.makeLine(fp.p1,fp.p2)
00153 
00154 class ViewProviderLine:
00155         def __init__(self, obj):
00156                 ''' Set this object to the proxy object of the actual view provider '''
00157                 obj.Proxy = self
00158 
00159         def getDefaultDisplayMode(self):
00160                 ''' Return the name of the default display mode. It must be defined in getDisplayModes. '''
00161                 return "Flat Lines"
00162 
00163 def makeLine():
00164         FreeCAD.newDocument()
00165         a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Line")
00166         Line(a)
00167         #ViewProviderLine(a.ViewObject)
00168         a.ViewObject.Proxy=0 # just set it to something different from None
00169 
00170 # -----------------------------------------------------------------------------
00171 
00172 class Octahedron:
00173         def __init__(self, obj):
00174                 "Add some custom properties to our box feature"
00175                 obj.addProperty("App::PropertyLength","Length","Octahedron","Length of the octahedron").Length=1.0
00176                 obj.addProperty("App::PropertyLength","Width","Octahedron","Width of the octahedron").Width=1.0
00177                 obj.addProperty("App::PropertyLength","Height","Octahedron", "Height of the octahedron").Height=1.0
00178                 obj.addProperty("Part::PropertyPartShape","Shape","Octahedron", "Shape of the octahedron")
00179                 obj.Proxy = self
00180  
00181         def execute(self, fp):
00182                 # Define six vetices for the shape
00183                 v1 = FreeCAD.Vector(0,0,0)
00184                 v2 = FreeCAD.Vector(fp.Length,0,0)
00185                 v3 = FreeCAD.Vector(0,fp.Width,0)
00186                 v4 = FreeCAD.Vector(fp.Length,fp.Width,0)
00187                 v5 = FreeCAD.Vector(fp.Length/2,fp.Width/2,fp.Height/2)
00188                 v6 = FreeCAD.Vector(fp.Length/2,fp.Width/2,-fp.Height/2)
00189                 
00190                 # Make the wires/faces
00191                 f1 = self.make_face(v2,v1,v5)
00192                 f2 = self.make_face(v4,v2,v5)
00193                 f3 = self.make_face(v3,v4,v5)
00194                 f4 = self.make_face(v1,v3,v5)
00195                 f5 = self.make_face(v1,v2,v6)
00196                 f6 = self.make_face(v2,v4,v6)
00197                 f7 = self.make_face(v4,v3,v6)
00198                 f8 = self.make_face(v3,v1,v6)
00199                 shell=Part.makeShell([f1,f2,f3,f4,f5,f6,f7,f8])
00200                 solid=Part.makeSolid(shell)
00201                 fp.Shape = solid
00202         # helper mehod to create the faces
00203         def make_face(self,v1,v2,v3):
00204                 wire = Part.makePolygon([v1,v2,v3,v1])
00205                 face = Part.Face(wire)
00206                 return face
00207 
00208 class ViewProviderOctahedron:
00209         def __init__(self, obj):
00210                 "Set this object to the proxy object of the actual view provider"
00211                 obj.addProperty("App::PropertyColor","Color","Octahedron","Color of the octahedron").Color=(1.0,0.0,0.0)
00212                 obj.Proxy = self
00213  
00214         def attach(self, obj):
00215                 "Setup the scene sub-graph of the view provider, this method is mandatory"
00216                 self.shaded = coin.SoGroup()
00217                 self.wireframe = coin.SoGroup()
00218                 self.color = coin.SoBaseColor()
00219 
00220                 self.data=coin.SoCoordinate3()
00221                 self.face=coin.SoIndexedFaceSet()
00222  
00223                 self.shaded.addChild(self.color)
00224                 self.shaded.addChild(self.data)
00225                 self.shaded.addChild(self.face)
00226                 obj.addDisplayMode(self.shaded,"Shaded");
00227                 style=coin.SoDrawStyle()
00228                 style.style = coin.SoDrawStyle.LINES
00229                 self.wireframe.addChild(style)
00230                 self.wireframe.addChild(self.color)
00231                 self.wireframe.addChild(self.data)
00232                 self.wireframe.addChild(self.face)
00233                 obj.addDisplayMode(self.wireframe,"Wireframe");
00234                 self.onChanged(obj,"Color")
00235  
00236         def updateData(self, fp, prop):
00237                 "If a property of the handled feature has changed we have the chance to handle this here"
00238                 # fp is the handled feature, prop is the name of the property that has changed
00239                 if prop == "Shape":
00240                         s = fp.getPropertyByName("Shape")
00241                         self.data.point.setNum(6)
00242                         cnt=0
00243                         for i in s.Vertexes:
00244                                 self.data.point.set1Value(cnt,i.X,i.Y,i.Z)
00245                                 cnt=cnt+1
00246                         
00247                         self.face.coordIndex.set1Value(0,0)
00248                         self.face.coordIndex.set1Value(1,2)
00249                         self.face.coordIndex.set1Value(2,1)
00250                         self.face.coordIndex.set1Value(3,-1)
00251 
00252                         self.face.coordIndex.set1Value(4,3)
00253                         self.face.coordIndex.set1Value(5,2)
00254                         self.face.coordIndex.set1Value(6,0)
00255                         self.face.coordIndex.set1Value(7,-1)
00256  
00257                         self.face.coordIndex.set1Value(8,4)
00258                         self.face.coordIndex.set1Value(9,2)
00259                         self.face.coordIndex.set1Value(10,3)
00260                         self.face.coordIndex.set1Value(11,-1)
00261  
00262                         self.face.coordIndex.set1Value(12,1)
00263                         self.face.coordIndex.set1Value(13,2)
00264                         self.face.coordIndex.set1Value(14,4)
00265                         self.face.coordIndex.set1Value(15,-1)
00266  
00267                         self.face.coordIndex.set1Value(16,1)
00268                         self.face.coordIndex.set1Value(17,5)
00269                         self.face.coordIndex.set1Value(18,0)
00270                         self.face.coordIndex.set1Value(19,-1)
00271  
00272                         self.face.coordIndex.set1Value(20,0)
00273                         self.face.coordIndex.set1Value(21,5)
00274                         self.face.coordIndex.set1Value(22,3)
00275                         self.face.coordIndex.set1Value(23,-1)
00276  
00277                         self.face.coordIndex.set1Value(24,3)
00278                         self.face.coordIndex.set1Value(25,5)
00279                         self.face.coordIndex.set1Value(26,4)
00280                         self.face.coordIndex.set1Value(27,-1)
00281  
00282                         self.face.coordIndex.set1Value(28,4)
00283                         self.face.coordIndex.set1Value(29,5)
00284                         self.face.coordIndex.set1Value(30,1)
00285                         self.face.coordIndex.set1Value(31,-1)
00286 
00287         def getDisplayModes(self,obj):
00288                 "Return a list of display modes."
00289                 modes=[]
00290                 modes.append("Shaded")
00291                 modes.append("Wireframe")
00292                 return modes
00293  
00294         def getDefaultDisplayMode(self):
00295                 "Return the name of the default display mode. It must be defined in getDisplayModes."
00296                 return "Shaded"
00297  
00298         def setDisplayMode(self,mode):
00299                 return mode
00300 
00301         def onChanged(self, vp, prop):
00302                 "Here we can do something when a single property got changed"
00303                 FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
00304                 if prop == "Color":
00305                         c = vp.getPropertyByName("Color")
00306                         self.color.rgb.setValue(c[0],c[1],c[2])
00307 
00308         def getIcon(self):
00309                 return """
00310                         /* XPM */
00311                         static const char * ViewProviderBox_xpm[] = {
00312                         "16 16 6 1",
00313                         "       c None",
00314                         ".      c #141010",
00315                         "+      c #615BD2",
00316                         "@      c #C39D55",
00317                         "#      c #000000",
00318                         "$      c #57C355",
00319                         "        ........",
00320                         "   ......++..+..",
00321                         "   .@@@@.++..++.",
00322                         "   .@@@@.++..++.",
00323                         "   .@@  .++++++.",
00324                         "  ..@@  .++..++.",
00325                         "###@@@@ .++..++.",
00326                         "##$.@@$#.++++++.",
00327                         "#$#$.$$$........",
00328                         "#$$#######      ",
00329                         "#$$#$$$$$#      ",
00330                         "#$$#$$$$$#      ",
00331                         "#$$#$$$$$#      ",
00332                         " #$#$$$$$#      ",
00333                         "  ##$$$$$#      ",
00334                         "   #######      "};
00335                         """
00336  
00337         def __getstate__(self):
00338                 return None
00339  
00340         def __setstate__(self,state):
00341                 return None
00342 
00343 def makeOctahedron():
00344         FreeCAD.newDocument()
00345         a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Octahedron")
00346         Octahedron(a)
00347         ViewProviderOctahedron(a.ViewObject)
00348 
00349 # -----------------------------------------------------------------------------
00350 
00351 class PointFeature:
00352         def __init__(self, obj):
00353                 obj.Proxy = self
00354 
00355         def onChanged(self, fp, prop):
00356                 ''' Print the name of the property that has changed '''
00357                 return
00358 
00359         def execute(self, fp):
00360                 ''' Print a short message when doing a recomputation, this method is mandatory '''
00361                 return
00362 
00363 class ViewProviderPoints:
00364         def __init__(self, obj):
00365                 ''' Set this object to the proxy object of the actual view provider '''
00366                 obj.Proxy = self
00367 
00368         def attach(self, obj):
00369                 ''' Setup the scene sub-graph of the view provider, this method is mandatory '''
00370                 return
00371 
00372         def updateData(self, fp, prop):
00373                 ''' If a property of the handled feature has changed we have the chance to handle this here '''
00374                 return
00375 
00376         def getDisplayModes(self,obj):
00377                 ''' Return a list of display modes. '''
00378                 modes=[]
00379                 return modes
00380 
00381         def getDefaultDisplayMode(self):
00382                 ''' Return the name of the default display mode. It must be defined in getDisplayModes. '''
00383                 return "Points"
00384 
00385         def setDisplayMode(self,mode):
00386                 ''' Map the display mode defined in attach with those defined in getDisplayModes.
00387                 Since they have the same names nothing needs to be done. This method is optinal.
00388                 '''
00389                 return mode
00390 
00391         def onChanged(self, vp, prop):
00392                 ''' Print the name of the property that has changed '''
00393                 return
00394 
00395         def getIcon(self):
00396                 ''' Return the icon in XMP format which will appear in the tree view. This method is optional
00397                 and if not defined a default icon is shown.
00398                 '''
00399                 return """
00400                         /* XPM */
00401                         static const char * ViewProviderBox_xpm[] = {
00402                         "16 16 6 1",
00403                         "       c None",
00404                         ".      c #141010",
00405                         "+      c #615BD2",
00406                         "@      c #C39D55",
00407                         "#      c #000000",
00408                         "$      c #57C355",
00409                         "        ........",
00410                         "   ......++..+..",
00411                         "   .@@@@.++..++.",
00412                         "   .@@@@.++..++.",
00413                         "   .@@  .++++++.",
00414                         "  ..@@  .++..++.",
00415                         "###@@@@ .++..++.",
00416                         "##$.@@$#.++++++.",
00417                         "#$#$.$$$........",
00418                         "#$$#######      ",
00419                         "#$$#$$$$$#      ",
00420                         "#$$#$$$$$#      ",
00421                         "#$$#$$$$$#      ",
00422                         " #$#$$$$$#      ",
00423                         "  ##$$$$$#      ",
00424                         "   #######      "};
00425                         """
00426 
00427         def __getstate__(self):
00428                 ''' When saving the document this object gets stored using Python's cPickle module.
00429                 Since we have some un-pickable here -- the Coin stuff -- we must define this method
00430                 to return a tuple of all pickable objects or None.
00431                 '''
00432                 return None
00433 
00434         def __setstate__(self,state):
00435                 ''' When restoring the pickled object from document we have the chance to set some
00436                 internals here. Since no data were pickled nothing needs to be done here.
00437                 '''
00438                 return None
00439 
00440 
00441 def makePoints():
00442         FreeCAD.newDocument()
00443         import Mesh
00444         m=Mesh.createSphere(5.0).Points
00445         import Points
00446         p=Points.Points()
00447 
00448         l=[]
00449         for s in m:
00450                 l.append(s.Vector)
00451 
00452         p.addPoints(l)
00453 
00454 
00455         a=FreeCAD.ActiveDocument.addObject("Points::FeaturePython","Points")
00456         a.Points=p
00457         PointFeature(a)
00458         ViewProviderPoints(a.ViewObject)
00459 
00460 # -----------------------------------------------------------------------------
00461 
00462 class MeshFeature:
00463         def __init__(self, obj):
00464                 obj.Proxy = self
00465 
00466         def onChanged(self, fp, prop):
00467                 ''' Print the name of the property that has changed '''
00468                 return
00469 
00470         def execute(self, fp):
00471                 ''' Print a short message when doing a recomputation, this method is mandatory '''
00472                 return
00473 
00474 class ViewProviderMesh:
00475         def __init__(self, obj):
00476                 ''' Set this object to the proxy object of the actual view provider '''
00477                 obj.Proxy = self
00478 
00479         def attach(self, obj):
00480                 ''' Setup the scene sub-graph of the view provider, this method is mandatory '''
00481                 return
00482 
00483         def getDefaultDisplayMode(self):
00484                 ''' Return the name of the default display mode. It must be defined in getDisplayModes. '''
00485                 return "Shaded"
00486 
00487         def getIcon(self):
00488                 ''' Return the icon in XMP format which will appear in the tree view. This method is optional
00489                 and if not defined a default icon is shown.
00490                 '''
00491                 return """
00492                         /* XPM */
00493                         static const char * ViewProviderBox_xpm[] = {
00494                         "16 16 6 1",
00495                         "       c None",
00496                         ".      c #141010",
00497                         "+      c #615BD2",
00498                         "@      c #C39D55",
00499                         "#      c #000000",
00500                         "$      c #57C355",
00501                         "        ........",
00502                         "   ......++..+..",
00503                         "   .@@@@.++..++.",
00504                         "   .@@@@.++..++.",
00505                         "   .@@  .++++++.",
00506                         "  ..@@  .++..++.",
00507                         "###@@@@ .++..++.",
00508                         "##$.@@$#.++++++.",
00509                         "#$#$.$$$........",
00510                         "#$$#######      ",
00511                         "#$$#$$$$$#      ",
00512                         "#$$#$$$$$#      ",
00513                         "#$$#$$$$$#      ",
00514                         " #$#$$$$$#      ",
00515                         "  ##$$$$$#      ",
00516                         "   #######      "};
00517                         """
00518 
00519         def __getstate__(self):
00520                 ''' When saving the document this object gets stored using Python's cPickle module.
00521                 Since we have some un-pickable here -- the Coin stuff -- we must define this method
00522                 to return a tuple of all pickable objects or None.
00523                 '''
00524                 return None
00525 
00526         def __setstate__(self,state):
00527                 ''' When restoring the pickled object from document we have the chance to set some
00528                 internals here. Since no data were pickled nothing needs to be done here.
00529                 '''
00530                 return None
00531 
00532 
00533 def makeMesh():
00534         FreeCAD.newDocument()
00535         import Mesh
00536 
00537         a=FreeCAD.ActiveDocument.addObject("Mesh::FeaturePython","Mesh")
00538         a.Mesh=Mesh.createSphere(5.0)
00539         MeshFeature(a)
00540         ViewProviderMesh(a.ViewObject)
00541         
00542 # -----------------------------------------------------------------------------
00543 
00544 class Molecule:
00545         def __init__(self, obj):
00546                 ''' Add two point properties '''
00547                 obj.addProperty("App::PropertyVector","p1","Line","Start point")
00548                 obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(1,0,0)
00549 
00550                 obj.Proxy = self
00551 
00552         def onChanged(self, fp, prop):
00553                 if prop == "p1" or prop == "p2":
00554                         ''' Print the name of the property that has changed '''
00555                         fp.Shape = Part.makeLine(fp.p1,fp.p2)
00556 
00557         def execute(self, fp):
00558                 ''' Print a short message when doing a recomputation, this method is mandatory '''
00559                 fp.Shape = Part.makeLine(fp.p1,fp.p2)
00560 
00561 class ViewProviderMolecule:
00562         def __init__(self, obj):
00563                 ''' Set this object to the proxy object of the actual view provider '''
00564                 obj.Proxy = self
00565                 sep1=coin.SoSeparator()
00566                 self.trl1=coin.SoTranslation()
00567                 sep1.addChild(self.trl1)
00568                 sep1.addChild(coin.SoSphere())
00569                 sep2=coin.SoSeparator()
00570                 self.trl2=coin.SoTranslation()
00571                 sep2.addChild(self.trl2)
00572                 sep2.addChild(coin.SoSphere())
00573                 obj.RootNode.addChild(sep1)
00574                 obj.RootNode.addChild(sep2)
00575 
00576         def updateData(self, fp, prop):
00577                 "If a property of the handled feature has changed we have the chance to handle this here"
00578                 # fp is the handled feature, prop is the name of the property that has changed
00579                 if prop == "p1":
00580                         p = fp.getPropertyByName("p1")
00581                         self.trl1.translation=(p.x,p.y,p.z)
00582                 elif prop == "p2":
00583                         p = fp.getPropertyByName("p2")
00584                         self.trl2.translation=(p.x,p.y,p.z)
00585 
00586         def __getstate__(self):
00587                 return None
00588  
00589         def __setstate__(self,state):
00590                 return None
00591 
00592 def makeMolecule():
00593         FreeCAD.newDocument()
00594         a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Molecule")
00595         Molecule(a)
00596         ViewProviderMolecule(a.ViewObject)
00597 
00598 # -----------------------------------------------------------------------------
00599 
00600 class CircleSet:
00601         def __init__(self, obj):
00602                 obj.addProperty("Part::PropertyPartShape","Shape","Circle","Shape")
00603                 obj.Proxy = self
00604 
00605         def execute(self, fp):
00606                 pass
00607 
00608 
00609 class ViewProviderCircleSet:
00610         def __init__(self, obj):
00611                 ''' Set this object to the proxy object of the actual view provider '''
00612                 obj.Proxy = self
00613 
00614         def attach(self, obj):
00615                 self.coords=coin.SoCoordinate3()
00616                 self.lines=coin.SoLineSet()
00617                 obj.RootNode.addChild(self.coords)
00618                 obj.RootNode.addChild(self.lines)
00619 
00620         def updateData(self, fp, prop):
00621                         if prop == "Shape":
00622                                 edges=p = fp.getPropertyByName("Shape").Edges
00623                                 pts=[]
00624                                 ver=[]
00625                                 for i in edges:
00626                                         length=i.Length
00627                                         ver.append(10)
00628                                         for j in range(10):
00629                                                 v=i.valueAt(j/9.0*length)
00630                                                 pts.append((v.x,v.y,v.z))
00631                                 
00632                                 self.coords.point.setValues(pts)
00633                                 self.lines.numVertices.setValues(ver)
00634 
00635         def __getstate__(self):
00636                 return None
00637  
00638         def __setstate__(self,state):
00639                 return None
00640 
00641 def makeCircleSet():
00642         x=0.5
00643         comp=Part.Compound([])
00644         for j in range (630):
00645                 y=0.5
00646                 for i in range (630):
00647                         c = Part.makeCircle(0.1, Base.Vector(x,y,0), Base.Vector(0,0,1))
00648                         #Part.show(c)
00649                         comp.add(c)
00650                         y=y+0.5
00651                 x=x+0.5
00652 
00653         FreeCAD.newDocument()
00654         a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Circles")
00655         c=CircleSet(a)
00656         v=ViewProviderCircleSet(a.ViewObject)
00657         a.Shape=comp
00658 
00659 # -----------------------------------------------------------------------------
00660 
00661 class EnumTest:
00662         def __init__(self, obj):
00663                 ''' Add enum properties '''
00664                 obj.addProperty("App::PropertyEnumeration","Enum","","Enumeration").Enum=["One","Two","Three"]
00665                 obj.addProperty("App::PropertyEnumeration","Enum2","","Enumeration2").Enum2=["One","Two","Three"]
00666                 obj.Proxy = self
00667 
00668         def execute(self, fp):
00669                 return                                                                            
00670                                                                                
00671 class ViewProviderEnumTest:
00672         def __init__(self, obj):
00673                 ''' Set this object to the proxy object of the actual view provider '''           
00674                 obj.addProperty("App::PropertyEnumeration","Enum3","","Enumeration3").Enum3=["One","Two","Three"]
00675                 obj.addProperty("App::PropertyEnumeration","Enum4","","Enumeration4").Enum4=["One","Two","Three"]
00676                 obj.Proxy = self
00677 
00678         def updateData(self, fp, prop):
00679                 print "prop updated:",prop
00680 
00681         def __getstate__(self):
00682                 return None
00683 
00684         def __setstate__(self,state):
00685                 return None
00686 
00687 def makeEnumTest():
00688         FreeCAD.newDocument()
00689         a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Enum")
00690         EnumTest(a)
00691         ViewProviderEnumTest(a.ViewObject)
00692 
00693 # -----------------------------------------------------------------------------
00694 
00695 class DistanceBolt:
00696         def __init__(self, obj):
00697                 ''' Add the properties: Length, Edges, Radius, Height '''
00698                 obj.addProperty("App::PropertyInteger","Edges","Bolt","Number of edges of the outline").Edges=6
00699                 obj.addProperty("App::PropertyLength","Length","Bolt","Length of the edges of the outline").Length=10.0
00700                 obj.addProperty("App::PropertyLength","Radius","Bolt","Radius of the inner circle").Radius=4.0
00701                 obj.addProperty("App::PropertyLength","Height","Bolt","Height of the extrusion").Height=20.0
00702                 obj.Proxy = self
00703 
00704         def onChanged(self, fp, prop):
00705                 if prop == "Edges" or prop == "Length" or prop == "Radius" or prop == "Height":
00706                         self.execute(fp)
00707 
00708         def execute(self, fp):
00709                 edges = fp.Edges
00710                 if edges < 3:
00711                         edges = 3
00712                 length = fp.Length
00713                 radius = fp.Radius
00714                 height = fp.Height
00715 
00716                 m=Base.Matrix()
00717                 m.rotateZ(math.radians(360.0/edges))
00718 
00719                 # create polygon
00720                 polygon = []
00721                 v=Base.Vector(length,0,0)
00722                 for i in range(edges):
00723                         polygon.append(v)
00724                         v = m.multiply(v)
00725                 polygon.append(v)
00726                 wire = Part.makePolygon(polygon)
00727 
00728                 # create circle
00729                 circ=Part.makeCircle(radius)
00730 
00731                 # Create the face with the polygon as outline and the circle as hole
00732                 face=Part.Face([wire,Part.Wire(circ)])
00733 
00734                 # Extrude in z to create the final solid
00735                 extrude=face.extrude(Base.Vector(0,0,height))
00736                 fp.Shape = extrude
00737 
00738 def makeDistanceBolt():
00739         FreeCAD.newDocument()
00740         bolt=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Distance_Bolt")
00741         bolt.Label = "Distance bolt"
00742         DistanceBolt(bolt)
00743         bolt.ViewObject.Proxy=0
00744 

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