Revit API - C# - 太阳和阴影设置 - 计算一年中每小时的太阳方向
Revit API - C# - Sun and shadow setting - calculating sun direction for every hour in a year period
我正在使用 Jeremy Tammik 的示例代码通过 Revit 计算一年内的太阳方向 API。 https://thebuildingcoder.typepad.com/blog/2013/06/sun-direction-shadow-calculation-and-wizard-update...
所以我基本上循环它以获得全年8760小时的太阳方向。
但是,当我将数据导出到excel时,数据似乎不正确。太阳方向的矢量在很多小时内保持不变,这是不对的。 PS Activeframe 时间正确。
下面是我的代码(附有 visual studio 版本),所附图像是我数据的一部分,它显示了问题。谢谢!
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.stack.imgur.com/ldFTc.png
正如我在 Revit API 论坛帖子中向您建议的那样
在同一问题上,Revit API - C# - Sun and shadow setting - calculating sun direction,
请通过用户界面手动执行相同的分析。
你在那里看到了什么结果?
同时,您已经确认成功,说:
Thanks for the reply! I got it solved by change the datetimespecify.Utc
to datetimespecify.local
.
恭喜并感谢您告知我们。
我正在使用 Jeremy Tammik 的示例代码通过 Revit 计算一年内的太阳方向 API。 https://thebuildingcoder.typepad.com/blog/2013/06/sun-direction-shadow-calculation-and-wizard-update...
所以我基本上循环它以获得全年8760小时的太阳方向。
但是,当我将数据导出到excel时,数据似乎不正确。太阳方向的矢量在很多小时内保持不变,这是不对的。 PS Activeframe 时间正确。
下面是我的代码(附有 visual studio 版本),所附图像是我数据的一部分,它显示了问题。谢谢!
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.stack.imgur.com/ldFTc.png
正如我在 Revit API 论坛帖子中向您建议的那样 在同一问题上,Revit API - C# - Sun and shadow setting - calculating sun direction, 请通过用户界面手动执行相同的分析。
你在那里看到了什么结果?
同时,您已经确认成功,说:
Thanks for the reply! I got it solved by change the
datetimespecify.Utc
todatetimespecify.local
.
恭喜并感谢您告知我们。