Can i get sun direction for each hour via Revit API?

Hi everyone.
I want to get sun direction for each hour form Revit sunsetting but in Dynamo it can get only 1 time frame.
I didn’t found “Sun Direction” property in Revit API.
How to get it ?

Thank a lot for your recommend.

Maybe try iterating through the dates/times you need using Python and getting the altitude/azimuth angles as you do. Pretty sure they can be used to make the sun direction.

1 Like

I try to follow your recommend but data from API and dynamo different.
help me please. :smiling_face_with_tear:

1 Like

SunAndShadowSettings has a StartDateAndTime property which can be set.

Note that in your screenshot the times are different, likely because Dynamo didn’t reexecute.

3 Likes

Hi,
here an example

import clr
import sys
import System
from System import DateTime

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS

#import Revit API
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.GeometryReferences)

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument

def get_Sun_Direction(view, dateTime):
	"""
	get sun direction by date Time
	"""
	sunSettings = view.SunAndShadowSettings
	# set the DateTime
	TransactionManager.Instance.EnsureInTransaction(doc)
	if dateTime.Kind == System.DateTimeKind.Unspecified:
		dateTime = DateTime.SpecifyKind(dateTime, System.DateTimeKind.Local)
	sunSettings.StartDateAndTime = dateTime
	sunSettings.SunAndShadowType = SunAndShadowType.StillImage
	TransactionManager.Instance.TransactionTaskDone()
	uidoc.RefreshActiveView()
	doc.Regenerate()
	# compute the sun direction
	initialDirection = XYZ.BasisY
	altitude = sunSettings.Altitude
	altitude = sunSettings.GetFrameAltitude(sunSettings.ActiveFrame)
	altitudeRotation = Transform.CreateRotation(XYZ.BasisX, altitude)
	altitudeDirection = altitudeRotation.OfVector(initialDirection)
	azimuth = sunSettings.Azimuth
	azimuth = sunSettings.GetFrameAzimuth(sunSettings.ActiveFrame)
	actualAzimuth = 2 * System.Math.PI - azimuth
	azimuthRotation = Transform.CreateRotation( XYZ.BasisZ, actualAzimuth)
	sunDirection = azimuthRotation.OfVector(altitudeDirection)
	return sunDirection
	
	
inputView = UnwrapElement(IN[0])
inputDateTime = UnwrapElement(IN[1])
sunDirection = get_Sun_Direction(inputView, inputDateTime)

OUT = sunDirection.ToVector().Reverse()

get_sun_direction_byTime

4 Likes

Thank you very much.
I have some problem when Revit model is set true north i understand in Dynamo vector refer from project base point and i don’t know how to fix this code and how to convert data is get from API to Dynamo node(value). can you explained it to me? i am beginner

:pray: :pray:

I get it just convert degree to radius right? i confuse because date in python and dynamo are difference.
Thank you very much :smile: :smile:

1 Like

Hi!
I am actually using sandbox
how can I generate sun vectors on sandbox?

Try the package “Solar Analysis for Dynamo” from the dynamo team - which can be found on the package manager.

Yes I am using it but there are no solar vectors or solar position

Maybe look into the Ladybug tools libraries which have an EPW file reader in their EPW namespace:

They are informally recognized by most as the industry standard for these types of workflows in AEC.

Hi,

there are interesting Python packages

a rapid test with suncalc

import sys
import clr
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('Python.Included')
import Python.Included as pyInc
path_py3_lib = pyInc.Installer.EmbeddedPythonHome
sys.path.append(path_py3_lib + r'\Lib\site-packages')

import suncalc
from suncalc import get_position, get_times
from datetime import datetime, timezone, timedelta

def get_Sun_Direction(sun_loc):
    """
    get sun direction by sun location
    """
    # compute the  altitude Vector
    initialDirection = Vector.YAxis()
    altitude = sun_loc["altitude"]
    print(f"{altitude=}")
    altitudeDirection = initialDirection.Rotate(Vector.XAxis(), altitude * (180 / System.Math.PI) )
    #
    # compute the  full Vector
    azimuth = sun_loc["azimuth"]
    print(f"{azimuth=}")
    sunDirection = altitudeDirection.Rotate(Vector.ZAxis(), azimuth * (180 / System.Math.PI)).Reverse()
    return sunDirection
	
inputDateTime = IN[0]
DS_Location = IN[1]
	
date = datetime(inputDateTime.Year, inputDateTime.Month, inputDateTime.Day, hour= inputDateTime.Hour)
print(f"date=")

lon = DS_Location.Longitude
lat = DS_Location.Latitude
sun_loc = get_position(date, lat, lon)

OUT = get_Sun_Direction(sun_loc)
4 Likes