Structure.py

Go to the documentation of this file.
00001 #***************************************************************************
00002 #*                                                                         *
00003 #*   Copyright (c) 2011                                                    *  
00004 #*   Yorik van Havre <yorik@uncreated.net>                                 *  
00005 #*                                                                         *
00006 #*   This program is free software; you can redistribute it and/or modify  *
00007 #*   it under the terms of the GNU General Public License (GPL)            *
00008 #*   as published by the Free Software Foundation; either version 2 of     *
00009 #*   the License, or (at your option) any later version.                   *
00010 #*   for detail see the LICENCE text file.                                 *
00011 #*                                                                         *
00012 #*   This program is distributed in the hope that it will be useful,       *
00013 #*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014 #*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015 #*   GNU Library General Public License for more details.                  *
00016 #*                                                                         *
00017 #*   You should have received a copy of the GNU Library General Public     *
00018 #*   License along with this program; if not, write to the Free Software   *
00019 #*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
00020 #*   USA                                                                   *
00021 #*                                                                         *
00022 #***************************************************************************
00023 
00024 import FreeCAD,FreeCADGui,Part,Draft,Component
00025 from draftlibs import fcgeo,fcvec
00026 from FreeCAD import Vector
00027 from PyQt4 import QtCore
00028 
00029 __title__="FreeCAD Structure"
00030 __author__ = "Yorik van Havre"
00031 __url__ = "http://free-cad.sourceforge.net"
00032 
00033 def makeStructure(baseobj=None,length=None,width=None,height=None,name="Structure"):
00034     '''makeStructure([obj],[length],[width],[heigth],[swap]): creates a
00035     structure element based on the given profile object and the given
00036     extrusion height. If no base object is given, you can also specify
00037     length and width for a cubic object.'''
00038     obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython",name)
00039     Structure(obj)
00040     ViewProviderStructure(obj.ViewObject)
00041     if baseobj: obj.Base = baseobj
00042     if length: obj.Length = length
00043     if width: obj.Width = width
00044     if height: obj.Height = height
00045     if obj.Base:
00046         obj.Base.ViewObject.hide()
00047     else:
00048         if (not obj.Width) and (not obj.Length):
00049             obj.Width = 1
00050             obj.Height = 1
00051     p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Arch")
00052     c = p.GetUnsigned("StructureColor")
00053     r = float((c>>24)&0xFF)/255.0
00054     g = float((c>>16)&0xFF)/255.0
00055     b = float((c>>8)&0xFF)/255.0
00056     obj.ViewObject.ShapeColor = (r,g,b,1.0)
00057     return obj
00058 
00059 class CommandStructure:
00060     "the Arch Structure command definition"
00061     def GetResources(self):
00062         return {'Pixmap'  : 'Arch_Structure',
00063                 'MenuText': QtCore.QT_TRANSLATE_NOOP("Arch_Structure","Structure"),
00064                 'Accel': "S, T",
00065                 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Arch_Structure","Creates a structure object from scratch or from a selected object (sketch, wire, face or solid)")}
00066         
00067     def Activated(self):
00068         sel = FreeCADGui.Selection.getSelection()
00069         if sel:
00070             FreeCAD.ActiveDocument.openTransaction("Structure")
00071             for obj in sel:
00072                 makeStructure(obj)
00073             FreeCAD.ActiveDocument.commitTransaction()
00074         else:
00075             makeStructure()
00076        
00077 class Structure(Component.Component):
00078     "The Structure object"
00079     def __init__(self,obj):
00080         Component.Component.__init__(self,obj)
00081         obj.addProperty("App::PropertyLength","Length","Base",
00082                         "The length of this element, if not based on a profile")
00083         obj.addProperty("App::PropertyLength","Width","Base",
00084                         "The width of this element, if not based on a profile")
00085         obj.addProperty("App::PropertyLength","Height","Base",
00086                         "The height or extrusion depth of this element. Keep 0 for automatic")
00087         self.Type = "Structure"
00088         
00089     def execute(self,obj):
00090         self.createGeometry(obj)
00091         
00092     def onChanged(self,obj,prop):
00093         if prop in ["Base","Length","Width","Height","Normal","Additions","Subtractions"]:
00094             self.createGeometry(obj)
00095 
00096     def createGeometry(self,obj):
00097         # getting default values
00098         height = normal = None
00099         if obj.Length:
00100             length = obj.Length
00101         else:
00102             length = 1
00103         if obj.Width:
00104             width = obj.Width
00105         else:
00106             width = 1
00107         if obj.Height:
00108             height = obj.Height
00109         else:
00110             for p in obj.InList:
00111                 if Draft.getType(p) == "Floor":
00112                     height = p.Height
00113         if not height: height = 1
00114         if obj.Normal == Vector(0,0,0):
00115             normal = Vector(0,0,1)
00116         else:
00117             normal = Vector(obj.Normal)
00118 
00119         # creating shape
00120         pl = obj.Placement
00121         norm = normal.multiply(height)
00122         base = None
00123         if obj.Base:
00124             if obj.Base.isDerivedFrom("Part::Feature"):
00125                 base = obj.Base.Shape.copy()
00126                 if base.Solids:
00127                     pass
00128                 elif base.Faces:
00129                     base = base.extrude(normal)
00130                 elif (len(base.Wires) == 1) and base.Wires[0].isClosed():
00131                     base = Part.Face(base.Wires[0])
00132                     base = base.extrude(normal)                
00133         else:
00134             l2 = length/2 or 0.5
00135             w2 = width/2 or 0.5
00136             v1 = Vector(-l2,-w2,0)
00137             v2 = Vector(l2,-w2,0)
00138             v3 = Vector(l2,w2,0)
00139             v4 = Vector(-l2,w2,0)
00140             base = Part.makePolygon([v1,v2,v3,v4,v1])
00141             base = Part.Face(base)
00142             base = base.extrude(normal)
00143         if base:
00144             obj.Shape = base
00145             if not fcgeo.isNull(pl): obj.Placement = pl
00146 
00147 class ViewProviderStructure(Component.ViewProviderComponent):
00148     "A View Provider for the Structure object"
00149 
00150     def __init__(self,vobj):
00151         Component.ViewProviderComponent.__init__(self,vobj)
00152 
00153     def getIcon(self):          
00154         return """
00155                 /* XPM */
00156                 static char * Arch_Structure_xpm[] = {
00157                 "16 16 9 1",
00158                 "       c None",
00159                 ".      c #2A303B",
00160                 "+      c #484F58",
00161                 "@      c #506573",
00162                 "#      c #617887",
00163                 "$      c #7A98A6",
00164                 "%      c #A5A4A8",
00165                 "&      c #C1C5C5",
00166                 "*      c #ECEEEB",
00167                 "......++++@##%%@",
00168                 "@$@************%",
00169                 "@$@************%",
00170                 "@$@********&%#+ ",
00171                 "@$@****%%@+.%%  ",
00172                 ".@+%#+.+ @$@*&  ",
00173                 "   #$+*% @$@*&  ",
00174                 "   #$@*% @$@*&  ",
00175                 "   #$@*% @$@*&  ",
00176                 "   #$@*% @$@*&  ",
00177                 "   #$@*% @$@*&  ",
00178                 "   #$@*% @$@*&  ",
00179                 "   #$@*% .@#%+  ",
00180                 "   #$@*%   .    ",
00181                 "   +#@*@        ",
00182                 "    .++         "};
00183                 """
00184 
00185 FreeCADGui.addCommand('Arch_Structure',CommandStructure())

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