//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
namespace Revit.SDK.Samples.StairsAutomation.CS
{
///
/// A stairs run consisting of a single sketched straight run.
///
public class SketchedStraightStairsRunComponent : TransformedStairsComponent, IStairsRunComponent
{
///
/// Creates a new sketched run configuration at the default location and orientation.
///
/// Stairs run boundary curves can be calculated before the run is created.
/// The number of risers.
/// The bottom elevation.
/// The desired tread depth.
/// The width of the run.
public SketchedStraightStairsRunComponent(int riserNumber, double bottomElevation, double desiredTreadDepth, double width):
base()
{
m_riserNumber = riserNumber;
m_bottomElevation = bottomElevation;
m_desiredTreadDepth = desiredTreadDepth;
m_width = width;
m_runExtent = new XYZ(0, (riserNumber - 1) * desiredTreadDepth, 0);
m_widthOffset = new XYZ(m_width, 0, 0);
}
///
/// Creates a new sketched run configuration at the specified location and orientation.
///
/// The number of risers.
/// The bottom elevation.
/// The desired tread depth.
/// The width of the run.
/// The transform (location and orientation).
public SketchedStraightStairsRunComponent(int riserNumber, double bottomElevation, double desiredTreadDepth, double width, Transform transform):
base(transform)
{
m_riserNumber = riserNumber;
m_bottomElevation = bottomElevation;
m_desiredTreadDepth = desiredTreadDepth;
m_width = width;
// This is the full extent of the run across all treads
m_runExtent = new XYZ(0, (riserNumber - 1) * desiredTreadDepth, 0);
m_widthOffset = new XYZ(m_width, 0, 0);
}
private int m_riserNumber;
private double m_bottomElevation;
private double m_desiredTreadDepth;
private double m_width;
private XYZ m_runExtent;
private XYZ m_widthOffset;
private StairsRun m_stairsRun;
#region IStairsRunConfiguration members
///
/// Implements the interface property.
///
public double RunElevation
{
get
{
return m_bottomElevation;
}
}
///
/// Implements the interface property.
///
public double TopElevation
{
get
{
if (m_stairsRun == null)
{
throw new NotSupportedException("Stairs run hasn't been constructed yet.");
}
return m_stairsRun.TopElevation;
}
}
///
/// Implements the interface method.
///
public double GetRunElevation()
{
return m_bottomElevation;
}
///
/// Implements the interface method.
///
public IList GetStairsPath()
{
// Proceed up the middle of the run to the run extent
XYZ start = new XYZ(m_width / 2.0, 0, m_bottomElevation);
XYZ end = start + m_runExtent;
Line curve1 = Line.CreateBound(start, end);
List ret = new List();
ret.Add(curve1);
return Transform(ret);
}
///
/// Implements the interface method.
///
public Curve GetFirstCurve()
{
return GenerateRunRiserCurves().First();
}
///
/// Implements the interface method.
///
public Curve GetLastCurve()
{
return GenerateRunRiserCurves().Last();
}
///
/// Implements the interface method.
///
public XYZ GetRunEndpoint()
{
return GetLastCurve().GetEndPoint(1);
}
///
/// Implements the interface method.
///
public IList GetRunBoundaryCurves()
{
return Transform(GenerateUntransformedRunBoundaryCurves());
}
///
/// Implements the interface method.
///
public Autodesk.Revit.DB.Architecture.StairsRun CreateStairsRun(Document document, ElementId stairsId)
{
m_stairsRun = StairsRun.CreateSketchedRun(document, stairsId, GetRunElevation(),
GetRunBoundaryCurves(), GenerateRunRiserCurves(),
GetStairsPath());
document.Regenerate();
return m_stairsRun;
}
///
/// Implements the interface property.
///
public double Width
{
get
{
return m_width;
}
set
{
if (m_stairsRun != null)
{
throw new InvalidOperationException("Cannot change width of sketched run.");
}
m_width = value;
}
}
#endregion
///
/// Generates the run boundary curves (not transformed by the stored transformation).
///
///
private IList GenerateUntransformedRunBoundaryCurves()
{
// Start at 0, 0 and extend to the run extent
XYZ start = new XYZ(0, 0, m_bottomElevation);
XYZ end = start + m_runExtent;
Line curve1 = Line.CreateBound(start, end);
List ret = new List();
ret.Add(curve1);
// Start offset along the width and extend to the run extent
start = new XYZ(m_width, 0, m_bottomElevation);
end = start + m_runExtent;
Line curve2 = Line.CreateBound(start, end);
ret.Add(curve2);
return ret;
}
///
/// Generates the riser curves for the sketch.
///
///
private IList GenerateRunRiserCurves()
{
List ret = new List();
// Generate all curves but the last by incrementing along the tread depth
for (int i = 0; i < m_riserNumber - 1; i++)
{
XYZ start = new XYZ(0, i * m_desiredTreadDepth, m_bottomElevation);
XYZ end = start + m_widthOffset;
ret.Add(Line.CreateBound(start, end));
}
// last one handled manually to ensure that it intersects the endpoints of the stairs curves
// Untransformed curves are used because transformation happens below
IList boundaryCurves = GenerateUntransformedRunBoundaryCurves();
ret.Add(Line.CreateBound(boundaryCurves[0].Evaluate(1.0, true), boundaryCurves[1].Evaluate(1.0, true)));
return Transform(ret);
}
}
}