应用程序:单位
Revit平台:所有
Revit版本:2014.0
首次发布:2014.0
编程语言:C#
技能水平:初级
分类:数据交换,基础知识
类型:ExternalCommand
主题:UnitsAPI
摘要:
添加一个命令,列出当前项目中所有的单位并显示它们的格式信息,用户可以设置每种单位类型的格式选项;显示当前项目单位的小数符号类型、数字分组符号、数字分组数量,用户可以设置它们。
相关类:
Autodesk.Revit.UI.IExternalCommand
Autodesk.Revit.DB.Units
Autodesk.Revit.DB.FormatOptions
Autodesk.Revit.DB.UnitUtils
Autodesk.Revit.DB.UnitFormatUtils
项目文件:
Command.cs
.dll文件的主入口点,其中包含实现接口IExternalCommand的类Command。从Revit获取必要数据并显示表格。
ProjectUnitData.cs
此文件包含一个准备项目单位数据的类。
ProjectUnitForm.cs
此文件包含一个表单类,通过API检索或更改项目单位设置,包括小数符号类型、数字分组符号、数字分组数量。
FormatForm.cs
此文件包含一个表单类,检索或更改每种单位类型的格式选项,包括显示单位类型、单位符号类型和一些其他格式信息。
描述:
此示例应提供以下功能:
- 显示当前项目单位的小数符号类型、数字分组符号、数字分组数量,用户可以设置它们,然后获取显示格式示例。
- 列出当前项目中的所有单位,并按单位组显示每个单位的格式示例。
- 列出当前项目的所有可用单位学科
- 根据单位学科/单位组对项目单位进行分类
- 当用户在列表框中选择一个单位学科时,显示相应的项目单位名称(例如长度或面积)和其格式示例。
- 显示每种单位类型的格式选项,用户可以设置它们,然后获取显示格式示例。
- 当用户单击格式按钮时,显示格式对话框
- 格式信息包括显示单位类型(例如DUT_KIP_FEET_PER_DEGREE_PER_FOOT)、单位符号类型(例如UST_NONE)和精度(例如0.2)。
说明:
1. 打开Revit。
2. 调用此命令。
3. 项目单位表单列出当前项目中的所有单位并显示每个单位的格式示例。
4. 用户可以选择当前项目单位的小数符号类型、数字分组符号、数字分组数量
5. 用户可以通过单击单位列表中的格式按钮设置每个单位类型的格式选项
6. 单击示例的主要UI中的OK按钮。
7. 应用程序将重置当前文档的单位。
源代码:
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
Command.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 System;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.Units.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)
{
try
{
Document document = commandData.Application.ActiveUIDocument.Document;
Autodesk.Revit.DB.Units units = document.GetUnits();
// show UI
using (UnitsForm displayForm = new UnitsForm(units))
{
DialogResult result = displayForm.ShowDialog();
if (DialogResult.OK == result)
{
using (Autodesk.Revit.DB.Transaction tran = new Autodesk.Revit.DB.Transaction(document, "SetUnits"))
{
tran.Start();
document.SetUnits(units);
tran.Commit();
}
}
else
{
return Autodesk.Revit.UI.Result.Cancelled;
}
}
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
}
}