应用程序名称:SampleCommandsSteelElements
Revit平台:所有
Revit版本:2019.0
首次发布版本:2019.0
编程语言:C#
技能级别:高级
类别:结构
类型:外部命令
主题:钢结构元素的示例命令
摘要:针对钢结构元素的示例命令。这些API示例允许创建、修改和删除钢结构元素。
相关类:
Autodesk.Revit.UI.IExternalCommand
Autodesk.Revit.DB.Structure.StructuralConnectionHandler
Autodesk.Revit.DB.Structure.StructuralConnectionHandlerType
Autodesk.Revit.DB.Steel.SteelElementProperties
Autodesk.Revit.DB.Transaction
Autodesk.Revit.DB.Element
Autodesk.Revit.DB.ElementId
Autodesk.Revit.DB.Reference
Autodesk.Revit.DB.Document
Autodesk.Revit.UI.Events
Autodesk.Revit.UI.UIDocument
项目文件:
AddElementsToConnection.cs - 添加元素到结构连接元素的命令。
AddElementsToCustomConnection.cs - 添加元素至自定义连接的命令。
CreateAnchorPattern.cs - 创建锚定花纹的命令。
CreateBoltPattern.cs - 创建螺栓花纹的命令。
CreateContourCut.cs - 创建轮廓切割修饰的命令。
CreateCopeSkewed.cs - 创建倾斜切割修饰的命令。
CreateCornerCut.cs - 创建角落切割修饰的命令。
CreatePlate.cs - 创建板元素的命令。
CreatePlateHole.cs - 在板元素上创建一些孔的命令。
CreateShearStudPattern.cs - 创建剪切销花纹的命令。
CreateShortening.cs - 在结构框架或结构柱上创建缩短修饰的命令。
CreateWeldPoint.cs - 创建焊缝的命令。
DeleteConnection.cs - 删除结构连接的命令。
Functions.cs - 此文件包含一些工具方法。
RemoveElementsFromConnection.cs - 从结构连接中移除元素的命令。
RemoveSubelementsFromCustomConnection.cs - 从自定义连接中移除元素的命令。
UpdateConnectionDetailedParameters.cs - 读取详细连接参数的命令。
BackgroundCalculation.cs - 显示如何检查后台计算以及在后台计算期间尝试启动制造事务时如何处理引发的异常的命令。
描述:
此示例提供以下功能:
- 如何使用Revit和Advance Steel API创建不同类型的钢结构元素(板、螺栓、锚定件、焊缝);
- 如何使用 Revit 和 Advance Steel API 在钢结构元素上添加不同类型的修饰(缩短、轮廓切割、角落切割、孔);
- 如何添加和删除自定义连接中的元素;
- 如何添加和删除结构连接中的元素。
源代码:
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
AddElementsToConnection.cs
//
// (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 Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using System.Linq;
namespace Revit.SDK.Samples.SampleCommandsSteelElements.AddElementsToConnection.CS
{
/// <summary>
/// Implements the Revit add-in interface IExternalCommand
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class Command : IExternalCommand
{
/// <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 virtual Result Execute(ExternalCommandData commandData
, ref string message, ElementSet elements)
{
// Get the document from external command data.
UIDocument activeDoc = commandData.Application.ActiveUIDocument;
Autodesk.Revit.DB.Document doc = activeDoc.Document;
if (null == doc)
{
return Result.Failed;
}
// The transaction and its status. We use Revit's Transaction class for this purpose
Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(doc, "Update structural connection");
TransactionStatus ts = TransactionStatus.Uninitialized;
try
{
// Select the connection to add input elements to, using Revit's StructuralConnectionHandler class
StructuralConnectionHandler conn = Utilities.Functions.SelectConnection(activeDoc);
if (null == conn)
{
return Result.Failed;
}
// Select elements to add to connection
IList<ElementId> ids = Utilities.Functions.SelectConnectionElements(activeDoc, "Select elements to add to connection :");
if (ids.Count() <= 0)
{
return Result.Failed;
}
// Start the transaction
trans.Start();
// Add the elements to the connection
conn.AddElementIds(ids);
// Commit the transaction
ts = trans.Commit();
if (ts != TransactionStatus.Committed)
{
message = "Failed to commit the current transaction !";
trans.RollBack();
return Result.Failed;
}
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
if (ts != TransactionStatus.Uninitialized)
{
trans.RollBack();
}
trans.Dispose();
return Result.Cancelled;
}
catch (Autodesk.Revit.Exceptions.ArgumentException)
{
if (ts != TransactionStatus.Uninitialized)
{
trans.RollBack();
}
trans.Dispose();
message = "No or already existing input elements selected!";
return Result.Failed;
}
return Result.Succeeded;
}
}
}
AddElementsToCustomConnection.cs
//
// (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 Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using System.Linq;
namespace Revit.SDK.Samples.SampleCommandsSteelElements.AddElementsToCustomConnection.CS
{
/// <summary>
/// Implements the Revit add-in interface IExternalCommand
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class Command : IExternalCommand
{
/// <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 virtual Result Execute(ExternalCommandData commandData
, ref string message, ElementSet elements)
{
// Get the document from external command data.
UIDocument activeDoc = commandData.Application.ActiveUIDocument;
Autodesk.Revit.DB.Document doc = activeDoc.Document;
if (null == doc)
{
return Result.Failed;
}
// The transaction and its status. We use Revit's Transaction class for this purpose
Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(doc, "Add element(s) to custom connection");
TransactionStatus ts = TransactionStatus.Uninitialized;
try
{
// Selecting the custom connection, using Revit's StructuralConnectionHandler class
StructuralConnectionHandler conn = Utilities.Functions.SelectConnection(activeDoc);
if (null == conn)
{
return Result.Failed;
}
if (!(conn.IsCustom()))
{
return Result.Failed;
}
// Select elements to add to connection.
IList<Reference> refs = Utilities.Functions.SelectConnectionElementsCustom(activeDoc);
if (refs.Count() <= 0)
{
return Result.Failed;
}
// Start transaction
trans.Start();
// Adding the elements to the custom connection, using Revit's StructuralConnectionHandlerType class
StructuralConnectionHandlerType.AddElementsToCustomConnection(conn, refs);
// Commit the transaction
ts = trans.Commit();
if (ts != TransactionStatus.Committed)
{
message = "Failed to commit the current transaction !";
trans.RollBack();
return Result.Failed;
}
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
if (ts != TransactionStatus.Uninitialized)
{
trans.RollBack();
}
trans.Dispose();
return Result.Cancelled;
}
catch (Autodesk.Revit.Exceptions.ArgumentException)
{
if (ts != TransactionStatus.Uninitialized)
{
trans.RollBack();
}
trans.Dispose();
message = "Custom connection already contains the selected element(s)!";
return Result.Failed;
}
return Result.Succeeded;
}
}
}
BackgroundCalculation.cs
//
// (C) Copyright 2003-2018 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 Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.DB.Steel;
using RvtDwgAddon;
using Autodesk.AdvanceSteel.Geometry;
using Autodesk.AdvanceSteel.Modelling;
using Autodesk.AdvanceSteel.CADLink.Database;
using Autodesk.AdvanceSteel.CADAccess;
namespace Revit.SDK.Samples.SampleCommandsSteelElements.BackgroundCalculation.CS
{
/// <summary>
/// Implements the Revit add-in interface IExternalCommand
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class Command : IExternalCommand
{
// Revit Id of a steel plate.
private ElementId _plateId;
// Current Revit document.
private Document _doc;
/// <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 virtual Result Execute(ExternalCommandData commandData
, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
//Get the document from external command data.
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
_doc = uiDoc.Document;
if (null == _doc)
{
return Result.Failed;
}
try
{
// Create a steel plate. The view should be in Fine mode to see the plate on screen.
// It's better to switch to 3d view (current view could be Level view and the plate could not be visible on that level)
_plateId = CreatePlate(XYZ.Zero);
// This would generate background calculation (You can see the task running in the Revit "Background processes" window).
MovePlate();
// Check for active background calculations.
bool backgroundCalc = _doc.IsBackgroundCalculationInProgress(); // It should be true.
// Try to create another plate while background calculation are in progress. This should not succeed !
CreatePlate(new XYZ(10, 10, 0));
// Register for the idling event and wait for the background calculations to finish.
uiApp.Idling += IdlingHandler;
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
return Result.Cancelled;
}
return Result.Succeeded;
}
/// <summary>
/// Handle the idling event.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
public void IdlingHandler(object sender, IdlingEventArgs args)
{
UIApplication uiApp = sender as UIApplication;
if (uiApp != null)
{
UIDocument uiDoc = uiApp.ActiveUIDocument;
if (uiDoc != null)
{
Document doc = uiDoc.Document;
// If we still have background calculation then return and wait for them to finish.
if (!doc.IsBackgroundCalculationInProgress())
{
// Now we can safely create a new plate.
CreatePlate(new XYZ(-10, -10, 0));
// Deregister from idling event.
uiApp.Idling -= IdlingHandler;
}
}
}
return;
}
/// <summary>
/// Creates steel plate.
/// </summary>
/// <returns>Returns Revit id of the created plate.</returns>
private ElementId CreatePlate(XYZ plateCenter)
{
ElementId ret = ElementId.InvalidElementId;
Guid plateUniqueId = Guid.Empty;
try
{
// Start detailed steel modeling transaction
using (FabricationTransaction trans = new FabricationTransaction(_doc, false, "Create structural plate"))
{
// Create a plate using Advance Steel API (internal Advance Steel units are in mm)
Point3d orig = new Point3d(Utilities.Functions.FEET_TO_MM * plateCenter.X,
Utilities.Functions.FEET_TO_MM * plateCenter.Y,
Utilities.Functions.FEET_TO_MM * plateCenter.Z);
Plate plate = new Plate(new Autodesk.AdvanceSteel.Geometry.Plane(Point3d.kOrigin, Vector3d.kZAxis), orig, 2000, 1000);
plate.Thickness = 10;
// Write plate to database.
plate.WriteToDb();
plateUniqueId = plate.GetUniqueId();
trans.Commit();
}
}
catch (System.InvalidOperationException ex)
{
//Cannot copy plate due to background calculation in progress !
System.Diagnostics.Debug.WriteLine(ex.Message);
}
// Get Revit element id from Advance Steel plate unique id.
Reference elem = SteelElementProperties.GetReference(_doc, plateUniqueId);
if(elem != null)
{
ret = elem.ElementId;
}
return ret;
}
/// <summary>
/// Move steel plate.
/// </summary>
private void MovePlate()
{
// Start Revit transaction.
using (Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(_doc, "Move plate"))
{
trans.Start();
// internal Revit units are in feet
ElementTransformUtils.MoveElement(_doc, _plateId, new XYZ(-50, 0, 0));
trans.Commit();
}
}
}
}
CreateAnchorPattern.cs
//
// (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 Autodesk.Revit.UI.Selection;
using Autodesk.AdvanceSteel.CADAccess;
using Autodesk.AdvanceSteel.Geometry;
using Autodesk.AdvanceSteel.Modelling;
using RvtDwgAddon;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
namespace Revit.SDK.Samples.SampleCommandsSteelElements.CreateAnchorPattern.CS
{
/// <summary>
/// Implements the Revit add-in interface IExternalCommand
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class Command : IExternalCommand
{
/// <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 virtual Result Execute(ExternalCommandData commandData
, ref string message, ElementSet elements)
{
// Get the document from external command data.
UIDocument activeDoc = commandData.Application.ActiveUIDocument;
Autodesk.Revit.DB.Document doc = activeDoc.Document;
if (null == doc)
{
return Result.Failed;
}
try
{
// Selecting the elements to create the anchor pattern on
Reference eRef = activeDoc.Selection.PickObject(ObjectType.Element, "Pick an element to create the anchor pattern on");
// Start detailed steel modeling transaction
using (FabricationTransaction trans = new FabricationTransaction(activeDoc.Document, false, "Create anchor pattern"))
{
// We create the anchor pattern using Advance Steel classes and objects only.
HashSet<FilerObject> filerObjectHashSet = new HashSet<FilerObject>();
FilerObject filerObj = Utilities.Functions.GetFilerObject(doc, eRef);
if (null == filerObj)
{
return Result.Failed;
}
filerObjectHashSet.Add(filerObj);
// Point of reference for the anchor pattern. We use GlobalPoint to create the pattern on the plate. GlobalPoint is the point where the plate is being hit when selected.
Point3d p1 = new Point3d(eRef.GlobalPoint.X, eRef.GlobalPoint.Y, eRef.GlobalPoint.Z);
Point3d p2 = new Point3d(p1.x + 0.5, p1.y + 0.5, p1.z + 0.5);
AnchorPattern anchorPattern = new AnchorPattern(p1 * Utilities.Functions.FEET_TO_MM, p2 * Utilities.Functions.FEET_TO_MM, new Vector3d(1, 0, 0), new Vector3d(0, 1, 0));
anchorPattern.Connect(filerObjectHashSet, Autodesk.AdvanceSteel.ConstructionTypes.AtomicElement.eAssemblyLocation.kOnSite);
anchorPattern.WriteToDb();
trans.Commit();
}
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
return Result.Cancelled;
}
return Result.Succeeded;
}
}
}