diff --git a/src/Mod/Arch/ArchSite.py b/src/Mod/Arch/ArchSite.py index a989a23e8..deb201d82 100644 --- a/src/Mod/Arch/ArchSite.py +++ b/src/Mod/Arch/ArchSite.py @@ -23,7 +23,7 @@ #* * #*************************************************************************** -import FreeCAD,Draft,ArchCommands,ArchFloor +import FreeCAD,Draft,ArchCommands,ArchFloor,math,re,datetime if FreeCAD.GuiUp: import FreeCADGui from PySide import QtCore, QtGui @@ -67,6 +67,172 @@ def makeSite(objectslist=None,baseobj=None,name="Site"): return obj +def makeSolarDiagram(longitude,latitude,scale=1,complete=False): + """makeSolarDiagram(longitude,latitude,[scale,complete]): + returns a solar diagram as a pivy node. If complete is + True, the 12 months are drawn""" + + try: + import Pysolar + except: + print("Pysolar is not installed. Unable to generate solar diagrams") + return None + from pivy import coin + + if not scale: + return None + + def toNode(shape): + "builds a pivy node from a simple linear shape" + from pivy import coin + buf = shape.writeInventor(2,0.01) + buf = buf.replace("\n","") + pts = re.findall("point \[(.*?)\]",buf)[0] + pts = pts.split(",") + pc = [] + for p in pts: + v = p.strip().split() + pc.append([float(v[0]),float(v[1]),float(v[2])]) + coords = coin.SoCoordinate3() + coords.point.setValues(0,len(pc),pc) + line = coin.SoLineSet() + line.numVertices.setValue(-1) + item = coin.SoSeparator() + item.addChild(coords) + item.addChild(line) + return item + + circles = [] + sunpaths = [] + hourpaths = [] + circlepos = [] + hourpos = [] + + # build the base circle + number positions + import Part + for i in range(1,9): + circles.append(Part.makeCircle(scale*(i/8.0))) + for ad in range(0,360,15): + a = math.radians(ad) + p1 = FreeCAD.Vector(math.cos(a)*scale,math.sin(a)*scale,0) + p2 = FreeCAD.Vector(math.cos(a)*scale*0.125,math.sin(a)*scale*0.125,0) + p3 = FreeCAD.Vector(math.cos(a)*scale*1.08,math.sin(a)*scale*1.08,0) + circles.append(Part.Line(p1,p2).toShape()) + circlepos.append((ad,p3)) + + # build the sun curves at solstices and equinoxe + year = datetime.datetime.now().year + hpts = [ [] for i in range(24) ] + m = [(6,21),(7,21),(8,21),(9,21),(10,21),(11,21),(12,21)] + if complete: + m.extend([(1,21),(2,21),(3,21),(4,21),(5,21)]) + for i,d in enumerate(m): + pts = [] + for h in range(24): + dt = datetime.datetime(year,d[0],d[1],h) + alt = math.radians(Pysolar.solar.GetAltitudeFast(latitude,longitude,dt)) + az = Pysolar.solar.GetAzimuth(latitude,longitude,dt) + az = -90 + az # pysolar's zero is south + if az < 0: + az = 360 + az + az = math.radians(az) + zc = math.sin(alt)*scale + ic = math.cos(alt)*scale + xc = math.cos(az)*ic + yc = math.sin(az)*ic + p = FreeCAD.Vector(xc,yc,zc) + pts.append(p) + hpts[h].append(p) + if i in [0,6]: + ep = FreeCAD.Vector(p) + ep.multiply(1.08) + if ep.z >= 0: + if h == 12: + if i == 0: + h = "SUMMER" + else: + h = "WINTER" + if latitude < 0: + if h == "SUMMER": + h = "WINTER" + else: + h = "SUMMER" + hourpos.append((h,ep)) + if i < 7: + sunpaths.append(Part.makePolygon(pts)) + for h in hpts: + if complete: + h.append(h[0]) + hourpaths.append(Part.makePolygon(h)) + + # cut underground lines + sz = 2.1*scale + cube = Part.makeBox(sz,sz,sz) + cube.translate(FreeCAD.Vector(-sz/2,-sz/2,-sz)) + sunpaths = [sp.cut(cube) for sp in sunpaths] + hourpaths = [hp.cut(cube) for hp in hourpaths] + + # build nodes + ts = 0.005*scale # text scale + mastersep = coin.SoSeparator() + circlesep = coin.SoSeparator() + numsep = coin.SoSeparator() + pathsep = coin.SoSeparator() + hoursep = coin.SoSeparator() + hournumsep = coin.SoSeparator() + mastersep.addChild(circlesep) + mastersep.addChild(numsep) + mastersep.addChild(pathsep) + mastersep.addChild(hoursep) + for item in circles: + circlesep.addChild(toNode(item)) + for item in sunpaths: + for w in item.Edges: + pathsep.addChild(toNode(w)) + for item in hourpaths: + for w in item.Edges: + hoursep.addChild(toNode(w)) + for p in circlepos: + text = coin.SoText2() + s = p[0]-90 + s = -s + if s > 360: + s = s - 360 + if s < 0: + s = 360 + s + if s == 0: + s = "N" + elif s == 90: + s = "E" + elif s == 180: + s = "S" + elif s == 270: + s = "W" + else: + s = str(s) + text.string = s + text.justification = coin.SoText2.CENTER + coords = coin.SoTransform() + coords.translation.setValue([p[1].x,p[1].y,p[1].z]) + coords.scaleFactor.setValue([ts,ts,ts]) + item = coin.SoSeparator() + item.addChild(coords) + item.addChild(text) + numsep.addChild(item) + for p in hourpos: + text = coin.SoText2() + s = str(p[0]) + text.string = s + text.justification = coin.SoText2.CENTER + coords = coin.SoTransform() + coords.translation.setValue([p[1].x,p[1].y,p[1].z]) + coords.scaleFactor.setValue([ts,ts,ts]) + item = coin.SoSeparator() + item.addChild(coords) + item.addChild(text) + numsep.addChild(item) + return mastersep + class _CommandSite: "the Arch Site command definition" @@ -130,6 +296,7 @@ class _Site(ArchFloor._Floor): obj.addProperty("App::PropertyString","Country","Arch",QT_TRANSLATE_NOOP("App::Property","The country of this site")) obj.addProperty("App::PropertyFloat","Latitude","Arch",QT_TRANSLATE_NOOP("App::Property","The latitude of this site")) obj.addProperty("App::PropertyFloat","Longitude","Arch",QT_TRANSLATE_NOOP("App::Property","The latitude of this site")) + obj.addProperty("App::PropertyAngle","NorthDeviation","Arch",QT_TRANSLATE_NOOP("App::Property","Angle between the true North and the North direction in this document")) obj.addProperty("App::PropertyLength","Elevation","Arch",QT_TRANSLATE_NOOP("App::Property","The elevation of level 0 of this site")) obj.addProperty("App::PropertyString","Url","Arch",QT_TRANSLATE_NOOP("App::Property","An url that shows this site in a mapping website")) obj.addProperty("App::PropertyLinkList","Group","Arch",QT_TRANSLATE_NOOP("App::Property","The objects that are part of this site")) @@ -269,6 +436,12 @@ class _ViewProviderSite(ArchFloor._ViewProviderFloor): def __init__(self,vobj): ArchFloor._ViewProviderFloor.__init__(self,vobj) + vobj.addProperty("App::PropertyBool","SolarDiagram","Arch",QT_TRANSLATE_NOOP("App::Property","Show solar diagram or not")) + vobj.addProperty("App::PropertyFloat","SolarDiagramScale","Arch",QT_TRANSLATE_NOOP("App::Property","The scale of the solar diagram")) + vobj.addProperty("App::PropertyVector","SolarDiagramPosition","Arch",QT_TRANSLATE_NOOP("App::Property","The position of the solar diagram")) + vobj.addProperty("App::PropertyColor","SolarDiagramColor","Arch",QT_TRANSLATE_NOOP("App::Property","The color of the solar diagram")) + vobj.SolarDiagramScale = 1 + vobj.SolarDiagramColor = (0.16,0.16,0.25) def getIcon(self): import Arch_rc @@ -296,6 +469,52 @@ class _ViewProviderSite(ArchFloor._ViewProviderFloor): def unsetEdit(self,vobj,mode): FreeCADGui.Control.closeDialog() return False + + def attach(self,vobj): + ArchFloor._ViewProviderFloor.attach(self,vobj) + from pivy import coin + self.diagramsep = coin.SoSeparator() + self.color = coin.SoBaseColor() + self.coords = coin.SoTransform() + self.diagramswitch = coin.SoSwitch() + self.diagramswitch.whichChild = -1 + self.diagramswitch.addChild(self.diagramsep) + self.diagramsep.addChild(self.coords) + self.diagramsep.addChild(self.color) + vobj.Annotation.addChild(self.diagramswitch) + + def updateData(self,obj,prop): + if prop in ["Longitude","Latitude"]: + self.onChanged(obj.ViewObject,"SolarDiagram") + elif prop == "NorthDeviation": + self.onChanged(obj.ViewObject,"SolarDiagramPosition") + + def onChanged(self,vobj,prop): + if prop == "SolarDiagramPosition": + if hasattr(vobj,"SolarDiagramPosition"): + p = vobj.SolarDiagramPosition + self.coords.translation.setValue([p.x,p.y,p.z]) + if hasattr(vobj.Object,"NorthDeviation"): + from pivy import coin + self.coords.rotation.setValue(coin.SbVec3f((0,0,1)),math.radians(vobj.Object.NorthDeviation.Value)) + elif prop == "SolarDiagramColor": + if hasattr(vobj,"SolarDiagramColor"): + l = vobj.SolarDiagramColor + self.color.rgb.setValue([l[0],l[1],l[2]]) + elif "SolarDiagram" in prop: + if hasattr(self,"diagramnode"): + self.diagramsep.removeChild(self.diagramnode) + del self.diagramnode + if hasattr(vobj,"SolarDiagram") and hasattr(vobj,"SolarDiagramScale"): + if vobj.SolarDiagram: + self.diagramnode = makeSolarDiagram(vobj.Object.Longitude,vobj.Object.Latitude,vobj.SolarDiagramScale) + if self.diagramnode: + self.diagramsep.addChild(self.diagramnode) + self.diagramswitch.whichChild = 0 + else: + del self.diagramnode + else: + self.diagramswitch.whichChild = -1 if FreeCAD.GuiUp: