I'm using the example code from Jeremy Tammik to calculate the sun directions for a year period through Revit API. https://thebuildingcoder.typepad.com/blog/2013/06/sun-direction-shadow-calculation-and-wizard-update...
So I basically loop it in order to get the whole year 8760 hour's sun direction.
However, when I export the data to excel, the data seems not right. the vector for sun direction stays the same for many hours, which is not right. PS the Activeframe time is correct.
Below is my code (with visual studio version attached) and the attached image is part of my data, which shows the problem. Thank you!
Autodesk.Revit.DB.View view = doc.ActiveView;
SunAndShadowSettings sunsetting = view.SunAndShadowSettings;
Transaction transun = new Transaction(doc);
transun.Start("sun1");
IList<DateTime> datetime = new List<DateTime>();
IList<DateTime> activeframe = new List<DateTime>();
//create list of 0 to 23
IList<double> timesss = test3.EnumerableUtilities.RangePython(0, 24, 1).ToList();
IList<XYZ> revitsun = new List<XYZ>();
DateTime StartDate = new DateTime (2018,1,1);
DateTime EndDate = new DateTime(2018,12,31);
foreach (DateTime days in EachDay(StartDate, EndDate))
{
datetime.Add(days);
}
foreach (var dat in datetime )
{
DateTime datee = dat;
int year = dat.Year;
int month = dat.Month;
int date = dat.Day;
foreach (var da in timesss)
{
double ti = da;
DateTime sunstart = DateTime.SpecifyKind(new DateTime(year,month,date), DateTimeKind.Utc);
sunsetting.SunAndShadowType = SunAndShadowType.StillImage;
sunsetting.StartDateAndTime = sunstart.AddHours(ti);
if (sunsetting.IsTimeIntervalValid(SunStudyTimeInterval.Hour)) // check that this interval is valid for this SunAndShadowType
sunsetting.TimeInterval = SunStudyTimeInterval.Hour;
// check for validity of start and end times
if (!(sunsetting.IsAfterStartDateAndTime(sunsetting.EndDateAndTime)
&& sunsetting.IsBeforeEndDateAndTime(sunsetting.StartDateAndTime)))
TaskDialog.Show("Error", "Start and End dates are invalid");
// Set the initial direction of the sun at ground level (like sunrise level)
XYZ initialDirection = XYZ.BasisY;
DateTime time = sunsetting.GetFrameTime(sunsetting.ActiveFrame);
activeframe.Add(time);
//string frametime = time.ToString();
// Get the altitude of the sun from the sun settings
double altitude = sunsetting.GetFrameAltitude(
sunsetting.ActiveFrame);
// Create a transform along the X axis based on the altitude of the sun
Transform altitudeRotation = Transform
.CreateRotation(XYZ.BasisX, altitude);
// Create a rotation vector for the direction of the altitude of the sun
XYZ altitudeDirection = altitudeRotation
.OfVector(initialDirection);
// Get the azimuth from the sun settings of the scene
double azimuth = sunsetting.GetFrameAzimuth(
sunsetting.ActiveFrame);
// Correct the value of the actual azimuth with true north
// Get the true north angle of the project
Element projectInfoElement
= new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_ProjectBasePoint)
.FirstElement();
BuiltInParameter bipAtn
= BuiltInParameter.BASEPOINT_ANGLETON_PARAM;
Parameter patn = projectInfoElement.get_Parameter(
bipAtn);
double trueNorthAngle = patn.AsDouble();
// Add the true north angle to the azimuth
double actualAzimuth = 2 * Math.PI - azimuth + trueNorthAngle;
// Create a rotation vector around the Z axis
Transform azimuthRotation = Transform
.CreateRotation(XYZ.BasisZ, actualAzimuth);
// Finally, calculate the direction of the sun
XYZ sunDirection = azimuthRotation.OfVector(
altitudeDirection);
revitsun.Add(sunDirection);
string countsundirection = sunDirection.ToString();
}
}```
[![enter image description here][1]][1]
[1]: https://i.sstatic.net/ldFTc.png
As I suggested yto you in the Revit API discussion forum thread on the same issue, Revit API - C# - Sun and shadow setting - calculating sun direction, please execute the same analysis manually through the user interface.
What results do you see there?
Meanwhile, you already confirmed success, saying:
Thanks for the reply! I got it solved by change the
datetimespecify.Utc
todatetimespecify.local
.
Congratulations and thank you for letting us know.