//
// (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;
namespace Revit.SDK.Samples.GeometryCreation_BooleanOperation.CS
{
class GeometryCreation
{
///
/// The singleton instance of GeometryCreation
///
private static GeometryCreation Instance;
///
/// revit application
///
private Autodesk.Revit.ApplicationServices.Application m_app;
///
/// The direction of cylinder, only on X, Y, Z three axes forward direction
///
public enum CylinderDirection
{
BasisX,
BasisY,
BasisZ
};
///
/// Constructor
///
/// Revit application
private GeometryCreation(Autodesk.Revit.ApplicationServices.Application app)
{
m_app = app;
}
///
/// Get the singleton instance of GeometryCreation
///
/// Revit application
/// The singleton instance of GeometryCreation
public static GeometryCreation getInstance(Autodesk.Revit.ApplicationServices.Application app)
{
if (Instance == null)
{
Instance = new GeometryCreation(app);
}
return Instance;
}
///
/// Create an extrusion geometry
///
/// The profile loops to be extruded
/// The direction in which to extrude the profile loops
/// The positive distance by which the loops are to be extruded
/// The created solid
private Solid CreateExtrusion(List profileLoops, XYZ extrusionDir, double extrusionDist)
{
return GeometryCreationUtilities.CreateExtrusionGeometry(profileLoops, extrusionDir, extrusionDist);
}
///
/// Create a revolved geometry
///
/// A right-handed orthonormal frame of vectors
/// The profile loops to be revolved
/// The start angle for the revolution
/// The end angle for the revolution
/// The created solid
private Solid CreateRevolved(Autodesk.Revit.DB.Frame coordinateFrame, List profileLoops, double startAngle, double endAngle)
{
return GeometryCreationUtilities.CreateRevolvedGeometry(coordinateFrame, profileLoops, startAngle, endAngle);
}
///
/// Create a swept geometry
///
/// The sweep path, consisting of a set of contiguous curves
/// The index of the curve in the sweep path where the profile loops are situated
/// Parameter of the path curve specified by pathAttachmentCrvIdx
/// The curve loops defining the planar domain to be swept along the path
/// The created solid
private Solid CreateSwept(CurveLoop sweepPath, int pathAttachmentCrvIdx, double pathAttachmentParam, List profileLoops)
{
return GeometryCreationUtilities.CreateSweptGeometry(sweepPath, pathAttachmentCrvIdx, pathAttachmentParam, profileLoops);
}
///
/// Create a blend geometry
///
/// The first curve loop
/// The second curve loop
/// This input specifies how the two profile loops should be connected
/// The created solid
private Solid CreateBlend(CurveLoop firstLoop, CurveLoop secondLoop, List vertexPairs)
{
return GeometryCreationUtilities.CreateBlendGeometry(firstLoop, secondLoop, vertexPairs);
}
///
/// Create a swept and blend geometry
///
/// The sweep path
/// An increasing sequence of parameters along the path curve
/// Closed, planar curve loops arrayed along the path
/// This input specifies how adjacent profile loops should be connected
/// The created solid
private Solid CreateSweptBlend(Curve pathCurve, List pathParams, List profileLoops, List> vertexPairs)
{
return GeometryCreationUtilities.CreateSweptBlendGeometry(pathCurve, pathParams, profileLoops, vertexPairs);
}
///
/// Create a centerbased box
///
/// The given box center
/// The given box's edge length
/// The created box
public Solid CreateCenterbasedBox(XYZ center, double edgelength)
{
double halfedgelength = edgelength / 2.0;
List profileloops = new List();
CurveLoop profileloop = new CurveLoop();
profileloop.Append(Line.CreateBound(
new XYZ(center.X - halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength),
new XYZ(center.X - halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength)));
profileloop.Append(Line.CreateBound(
new XYZ(center.X - halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength),
new XYZ(center.X + halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength)));
profileloop.Append(Line.CreateBound(
new XYZ(center.X + halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength),
new XYZ(center.X + halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength)));
profileloop.Append(Line.CreateBound(
new XYZ(center.X + halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength),
new XYZ(center.X - halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength)));
profileloops.Add(profileloop);
XYZ extrusiondir = new XYZ(0, 0, 1); // orthogonal
double extrusiondist = edgelength;
return GeometryCreationUtilities.CreateExtrusionGeometry(profileloops, extrusiondir, extrusiondist);
}
///
/// Create a centerbased sphere
///
/// The given sphere center
/// The given sphere's radius
/// The created sphere
public Solid CreateCenterbasedSphere(XYZ center, double radius)
{
Frame frame = new Frame(center,
Autodesk.Revit.DB.XYZ.BasisX,
Autodesk.Revit.DB.XYZ.BasisY,
Autodesk.Revit.DB.XYZ.BasisZ);
List profileloops = new List();
CurveLoop profileloop = new CurveLoop();
Curve cemiEllipse = Ellipse.CreateCurve(center, radius, radius,
Autodesk.Revit.DB.XYZ.BasisX,
Autodesk.Revit.DB.XYZ.BasisZ,
-Math.PI / 2.0, Math.PI / 2.0);
profileloop.Append(cemiEllipse);
profileloop.Append(Line.CreateBound(
new XYZ(center.X, center.Y, center.Z + radius),
new XYZ(center.X, center.Y, center.Z - radius)));
profileloops.Add(profileloop);
return GeometryCreationUtilities.CreateRevolvedGeometry(frame, profileloops, -Math.PI, Math.PI);
}
///
/// Create a centerbased cylinder, only on X, Y, Z three axes forward direction
///
/// The given cylinder center
/// The given cylinder's bottom radius
/// The given cylinder's height
/// Cylinder's extrusion direction
/// The created cylinder
public Solid CreateCenterbasedCylinder(XYZ center, double bottomradius, double height, CylinderDirection cylinderdirection)
{
double halfheight = height / 2.0;
XYZ bottomcenter = new XYZ(
cylinderdirection == CylinderDirection.BasisX ? center.X - halfheight : center.X,
cylinderdirection == CylinderDirection.BasisY ? center.Y - halfheight : center.Y,
cylinderdirection == CylinderDirection.BasisZ ? center.Z - halfheight : center.Z);
XYZ topcenter = new XYZ(
cylinderdirection == CylinderDirection.BasisX ? center.X + halfheight : center.X,
cylinderdirection == CylinderDirection.BasisY ? center.Y + halfheight : center.Y,
cylinderdirection == CylinderDirection.BasisZ ? center.Z + halfheight : center.Z);
CurveLoop sweepPath = new CurveLoop();
sweepPath.Append(Line.CreateBound(bottomcenter,
topcenter));
List profileloops = new List();
CurveLoop profileloop = new CurveLoop();
Curve cemiEllipse1 = Ellipse.CreateCurve(bottomcenter, bottomradius, bottomradius,
cylinderdirection == CylinderDirection.BasisX ? Autodesk.Revit.DB.XYZ.BasisY : Autodesk.Revit.DB.XYZ.BasisX,
cylinderdirection == CylinderDirection.BasisZ ? Autodesk.Revit.DB.XYZ.BasisY : Autodesk.Revit.DB.XYZ.BasisZ,
-Math.PI, 0);
Curve cemiEllipse2 = Ellipse.CreateCurve(bottomcenter, bottomradius, bottomradius,
cylinderdirection == CylinderDirection.BasisX ? Autodesk.Revit.DB.XYZ.BasisY : Autodesk.Revit.DB.XYZ.BasisX,
cylinderdirection == CylinderDirection.BasisZ ? Autodesk.Revit.DB.XYZ.BasisY : Autodesk.Revit.DB.XYZ.BasisZ,
0, Math.PI);
profileloop.Append(cemiEllipse1);
profileloop.Append(cemiEllipse2);
profileloops.Add(profileloop);
return GeometryCreationUtilities.CreateSweptGeometry(sweepPath, 0, 0, profileloops);
}
}
}