Files
2024-12-12 09:51:40 +01:00

435 lines
18 KiB
C#

//
// (C) Copyright 2003-2009 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;
using Autodesk.Revit.Geometry;
using Autodesk.Revit.Elements;
namespace Revit.SDK.Samples.NewForm.CS
{
/// <summary>
/// A class inherits IExternalCommand interface.
/// This class show how to create extrusion form by Revit API.
/// </summary>
public class MakeExtrusionForm : IExternalCommand
{
#region Class Interface Implementation
/// <summary>
/// Implement this method as an external command for Revit.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.</param>
/// <param name="message">A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.</param>
/// <param name="elements">A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.</param>
/// <returns>Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user cancelled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.</returns>
public IExternalCommand.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
ExternalCommandData cdata = commandData;
Autodesk.Revit.Application app = commandData.Application;
app = commandData.Application;
Document doc = app.ActiveDocument;
// Create one profile
ReferenceArray ref_ar = new ReferenceArray();
XYZ ptA = new XYZ(10, 10, 0);
XYZ ptB = new XYZ(90, 10, 0);
ModelCurve modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ptA = new XYZ(90, 10, 0);
ptB = new XYZ(10, 90, 0);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ptA = new XYZ(10, 90, 0);
ptB = new XYZ(10, 10, 0);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
// The extrusion form direction
XYZ direction = new XYZ(0, 0, 50);
Autodesk.Revit.Elements.Form form = doc.FamilyCreate.NewExtrusionForm(true, ref_ar, direction);
return IExternalCommand.Result.Succeeded;
}
#endregion
}
/// <summary>
/// A class inherits IExternalCommand interface.
/// This class show how to create cap form by Revit API.
/// </summary>
public class MakeCapForm : IExternalCommand
{
#region Class Interface Implementation
/// <summary>
/// Implement this method as an external command for Revit.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.</param>
/// <param name="message">A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.</param>
/// <param name="elements">A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.</param>
/// <returns>Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user cancelled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.</returns>
public IExternalCommand.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
ExternalCommandData cdata = commandData;
Autodesk.Revit.Application app = commandData.Application;
app = commandData.Application;
Document doc = app.ActiveDocument;
// Create one profile
ReferenceArray ref_ar = new ReferenceArray();
XYZ ptA = new XYZ(10, 10, 0);
XYZ ptB = new XYZ(100, 10, 0);
Line line = app.Create.NewLine(ptA, ptB, true);
ModelCurve modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ptA = new XYZ(100, 10, 0);
ptB = new XYZ(50, 50, 0);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ptA = new XYZ(50, 50, 0);
ptB = new XYZ(10, 10, 0);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
Autodesk.Revit.Elements.Form form = doc.FamilyCreate.NewFormByCap(true, ref_ar);
return IExternalCommand.Result.Succeeded;
}
#endregion
}
/// <summary>
/// A class inherits IExternalCommand interface.
/// This class show how to create revolve form by Revit API.
/// </summary>
public class MakeRevolveForm : IExternalCommand
{
#region Class Interface Implementation
/// <summary>
/// Implement this method as an external command for Revit.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.</param>
/// <param name="message">A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.</param>
/// <param name="elements">A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.</param>
/// <returns>Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user cancelled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.</returns>
public IExternalCommand.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
ExternalCommandData cdata = commandData;
Autodesk.Revit.Application app = commandData.Application;
app = commandData.Application;
Document doc = app.ActiveDocument;
// Create one profile
ReferenceArray ref_ar = new ReferenceArray();
XYZ ptA = new XYZ(0, 0, 10);
XYZ ptB = new XYZ(100, 0, 10);
Line line = app.Create.NewLine(ptA, ptB, true);
ModelCurve modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ptA = new XYZ(100, 0, 10);
ptB = new XYZ(100, 100, 10);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ptA = new XYZ(100, 100, 10);
ptB = new XYZ(0, 0, 10);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
// Create axis for revolve form
ptA = new XYZ(-5, 0, 10);
ptB = new XYZ(-5, 10, 10);
ModelCurve axis = FormUtils.MakeLine(app, ptA, ptB);
axis.ChangeToReferenceLine();
Autodesk.Revit.FormArray form = doc.FamilyCreate.NewRevolveForms(true, ref_ar, axis.GeometryCurve.Reference, 0, Math.PI / 4);
return IExternalCommand.Result.Succeeded;
}
#endregion
}
/// <summary>
/// A class inherits IExternalCommand interface.
/// This class show how to create swept blend form by Revit API.
/// </summary>
public class MakeSweptBlendForm : IExternalCommand
{
#region Class Interface Implementation
/// <summary>
/// Implement this method as an external command for Revit.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.</param>
/// <param name="message">A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.</param>
/// <param name="elements">A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.</param>
/// <returns>Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user cancelled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.</returns>
public IExternalCommand.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
ExternalCommandData cdata = commandData;
Autodesk.Revit.Application app = commandData.Application;
app = commandData.Application;
Document doc = app.ActiveDocument;
// Create first profile
ReferenceArray ref_ar = new ReferenceArray();
XYZ ptA = new XYZ(10, 10, 0);
XYZ ptB = new XYZ(50, 10, 0);
ModelCurve modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ptA = new XYZ(50, 10, 0);
ptB = new XYZ(10, 50, 0);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ptA = new XYZ(10, 50, 0);
ptB = new XYZ(10, 10, 0);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
// Create second profile
ReferenceArray ref_ar2 = new ReferenceArray();
ptA = new XYZ(10, 10, 90);
ptB = new XYZ(80, 10, 90);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar2.Append(modelcurve.GeometryCurve.Reference);
ptA = new XYZ(80, 10, 90);
ptB = new XYZ(10, 50, 90);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar2.Append(modelcurve.GeometryCurve.Reference);
ptA = new XYZ(10, 50, 90);
ptB = new XYZ(10, 10, 90);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
ref_ar2.Append(modelcurve.GeometryCurve.Reference);
// Add profiles
ReferenceArrayArray profiles = new ReferenceArrayArray();
profiles.Append(ref_ar);
profiles.Append(ref_ar2);
// Create path for swept blend form
ReferenceArray path = new ReferenceArray();
ptA = new XYZ(10, 10, 0);
ptB = new XYZ(10, 10, 90);
modelcurve = FormUtils.MakeLine(app, ptA, ptB);
path.Append(modelcurve.GeometryCurve.Reference);
Autodesk.Revit.Elements.Form form = doc.FamilyCreate.NewSweptBlendForm(true, path, profiles);
return IExternalCommand.Result.Succeeded;
}
#endregion
}
/// <summary>
/// A class inherits IExternalCommand interface.
/// This class show how to create loft form by Revit API.
/// </summary>
public class MakeLoftForm : IExternalCommand
{
#region Class Interface Implementation
/// <summary>
/// Implement this method as an external command for Revit.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.</param>
/// <param name="message">A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.</param>
/// <param name="elements">A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.</param>
/// <returns>Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user cancelled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.</returns>
public IExternalCommand.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
ExternalCommandData cdata = commandData;
Autodesk.Revit.Application app = commandData.Application;
app = commandData.Application;
Document doc = app.ActiveDocument;
// Create profiles array
ReferenceArrayArray ref_ar_ar = new ReferenceArrayArray();
// Create first profile
ReferenceArray ref_ar = new ReferenceArray();
int y = 100;
int x = 50;
XYZ ptA = new XYZ(-x, y, 0);
XYZ ptB = new XYZ(x, y, 0);
XYZ ptC = new XYZ(0, y + 10, 10);
ModelCurve modelcurve = FormUtils.MakeArc(app, ptA, ptB, ptC);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ref_ar_ar.Append(ref_ar);
// Create second profile
ref_ar = new ReferenceArray();
y = 40;
ptA = new XYZ(-x, y, 5);
ptB = new XYZ(x, y, 5);
ptC = new XYZ(0, y, 25);
modelcurve = FormUtils.MakeArc(app, ptA, ptB, ptC);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ref_ar_ar.Append(ref_ar);
// Create third profile
ref_ar = new ReferenceArray();
y = -20;
ptA = new XYZ(-x, y, 0);
ptB = new XYZ(x, y, 0);
ptC = new XYZ(0, y, 15);
modelcurve = FormUtils.MakeArc(app, ptA, ptB, ptC);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ref_ar_ar.Append(ref_ar);
// Create fourth profile
ref_ar = new ReferenceArray();
y = -60;
ptA = new XYZ(-x, y, 0);
ptB = new XYZ(x, y, 0);
ptC = new XYZ(0, y + 10, 20);
modelcurve = FormUtils.MakeArc(app, ptA, ptB, ptC);
ref_ar.Append(modelcurve.GeometryCurve.Reference);
ref_ar_ar.Append(ref_ar);
ref_ar = new ReferenceArray();
ref_ar_ar.Append(ref_ar);
Autodesk.Revit.Elements.Form form = doc.FamilyCreate.NewLoftForm(true, ref_ar_ar);
return IExternalCommand.Result.Succeeded;
}
#endregion
}
/// <summary>
/// This class is utility class for form creation.
/// </summary>
public class FormUtils
{
#region Class Implementation
/// <summary>
/// Create arc element by three points
/// </summary>
/// <param name="app">revit application</param>
/// <param name="ptA">point a</param>
/// <param name="ptB">point b</param>
/// <param name="ptC">point c</param>
/// <returns></returns>
public static ModelCurve MakeArc(Application app, XYZ ptA, XYZ ptB, XYZ ptC)
{
Document doc = app.ActiveDocument;
Arc arc = app.Create.NewArc(ptA, ptB, ptC);
// Create three lines and a plane by the points
Line line1 = app.Create.NewLine(ptA, ptB, true);
Line line2 = app.Create.NewLine(ptB, ptC, true);
Line line3 = app.Create.NewLine(ptC, ptA, true);
CurveArray ca = new CurveArray();
ca.Append(line1);
ca.Append(line2);
ca.Append(line3);
Plane plane = app.Create.NewPlane(ca);
SketchPlane skplane = doc.FamilyCreate.NewSketchPlane(plane);
// Create arc here
ModelCurve modelcurve = doc.FamilyCreate.NewModelCurve(arc, skplane);
return modelcurve;
}
/// <summary>
/// Create line element
/// </summary>
/// <param name="app">revit application</param>
/// <param name="ptA">start point</param>
/// <param name="ptB">end point</param>
/// <returns></returns>
public static ModelCurve MakeLine(Application app, XYZ ptA, XYZ ptB)
{
Document doc = app.ActiveDocument;
// Create plane by the points
Line line = app.Create.NewLine(ptA, ptB, true);
XYZ norm = ptA.Cross(ptB);
if (norm.Length == 0) norm = XYZ.BasisZ;
Plane plane = app.Create.NewPlane(norm, ptB);
SketchPlane skplane = doc.FamilyCreate.NewSketchPlane(plane);
// Create line here
ModelCurve modelcurve = doc.FamilyCreate.NewModelCurve(line, skplane);
return modelcurve;
}
#endregion
}
}