应用程序名称: SpotDimension

Revit平台: 所有

Revit版本: 2011.0

首次发布版本: 2008.0

编程语言: C#

技能级别: 初级

类别: 注释

类型: ExternalCommand

主题: 显示标注尺寸

摘要:

该示例演示如何检索Revit文档中所有视图中的所有标注尺寸及其属性。

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.DB.SpotDimension

Autodesk.Revit.DB.Parameter

项目文件:

SpotDimensionsData.cs

该文件包含如何获取所有标注尺寸的函数。

SpotDimensionParams.cs

该文件包含如何获取单个标注尺寸属性的函数。

描述:

- 在所有视图中获取标注尺寸。可以通过扫描文档元素来获取所有标注尺寸。

- 有一个对话框来显示这些标注尺寸及其视图属性和一些信息。

- 如果在对话框中选择了一个标注尺寸并返回到Revit,该标注尺寸将被突出显示。

- 根据其视图属性对标注尺寸进行分类。同一视图中的标注尺寸将一起显示。

- 从列表中选择视图,然后显示其中的标注尺寸。

- 从列表中选择一个标注尺寸,然后显示其某些参数。

- 可以通过DimensionType.get_Parameter()SpotDimension类的一些属性来获取参数。

操作说明:

1. 首先绘制一个元素(例如一堵墙),然后使用Revit UI中的“草图 - > 标注尺寸”命令绘制一些标注尺寸。

2. 加载并运行SpotDimension.dll

3. 选择一个视图以显示所有在该选定视图中的标注尺寸。然后在对话框底部的DataGridView中选择一个标注尺寸以显示其属性。

4. 选择一个标注尺寸,然后关闭对话框返回Revit,然后该标注尺寸将被突出显示。

源代码:
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

SpotDimensionParams.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.Data;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;

using Autodesk.Revit;
using Autodesk.Revit.DB;

namespace Revit.SDK.Samples.SpotDimension.CS
{
/// <summary>
/// this class is used to get some information
/// of SpotDimension's Parameters
/// </summary>
public class SpotDimensionParams
{
const double ToFractionalInches = 0.08333333; //const number to convert number to FractionalInches
static List<string> s_elevationOrigin = new List<string>(); //list store information about Elevation origin
static List<string> s_textOrientation = new List<string>(); //list store information about Text Orientation
static List<string> s_indicator = new List<string>(); //list store information about s_indicator
static List<string> s_topBottomValue = new List<string>(); //list store information about Top and Bottom Value
static List<string> s_textBackground = new List<string>(); //list store information about Text background
Document m_document; //a reference to Revit's document

/// <summary>
/// static constructor used to initialize lists
/// </summary>
static SpotDimensionParams()
{
//add string elements to lists
s_elevationOrigin.Add("Project");
s_elevationOrigin.Add("Shared");
s_elevationOrigin.Add("Relative");

s_textOrientation.Add("Horizontal Above");
s_textOrientation.Add("Horizontal Below");

s_indicator.Add("Prefix");
s_indicator.Add("Suffix");

s_topBottomValue.Add("None");
s_topBottomValue.Add("North / South");
s_topBottomValue.Add("East / West");

s_textBackground.Add("Opaque");
s_textBackground.Add("Transparent");
}

/// <summary>
/// a constructor of class SpotDimensionParams
/// </summary>
/// <param name="document">external command document of Revit</param>
public SpotDimensionParams(Document document)
{
m_document = document;
}

/// <summary>
/// get a datatable contains parameters'information of SpotDimension
/// here, almost only get Type Parameters of SpotDimension
/// </summary>
/// <param name="spotDimension">the SpotDimension need to be dealt with</param>
/// <returns>a DataTable store Parameter information</returns>
public DataTable GetParameterTable(Autodesk.Revit.DB.SpotDimension spotDimension)
{
try
{
//check whether is null
if (null == spotDimension)
{
return null;
}

//create an empty datatable
DataTable parameterTable = CreateTable();

//get DimensionType
Autodesk.Revit.DB.DimensionType dimensionType = spotDimension.DimensionType;

//begin to get Parameters and add them to a DataTable
Parameter temporaryParam = null;
string temporaryValue = "";

#region Get all SpotDimension element parameters

//string formatter
string formatter = "#0.000";

//Property Category of SpotDimension
AddDataRow("Category", spotDimension.Category.Name, parameterTable);

//Leader Arrowhead
temporaryParam =
dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_LEADER_ARROWHEAD);
Autodesk.Revit.DB.ElementId elementId = temporaryParam.AsElementId();
//if not found that element, add string "None" to DataTable
if (-1 == elementId.IntegerValue)
{
temporaryValue = "None";
}
else
{
temporaryValue = m_document.GetElement(elementId).Name;
}
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Leader Line Weight
temporaryParam = dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_LINE_PEN);
temporaryValue = temporaryParam.AsInteger().ToString();
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Leader Arrowhead Line Weight
temporaryParam =
dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_TICK_MARK_PEN);
temporaryValue = temporaryParam.AsInteger().ToString();
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Symbol
temporaryParam = dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_SYMBOL);
elementId = temporaryParam.AsElementId();
//if not found that element, add string "None" to DataTable
if (-1 == elementId.IntegerValue)
{
temporaryValue = "None";
}
else
{
temporaryValue = m_document.GetElement(elementId).Name;
}
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Text Size
temporaryParam = dimensionType.get_Parameter(BuiltInParameter.TEXT_SIZE);
temporaryValue =
(temporaryParam.AsDouble() / ToFractionalInches).ToString(formatter) + "''";
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Text Offset from Leader
temporaryParam =
dimensionType.get_Parameter(BuiltInParameter.SPOT_TEXT_FROM_LEADER);
temporaryValue =
(temporaryParam.AsDouble() / ToFractionalInches).ToString(formatter) + "''";
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Text Offset from Symbol
temporaryParam =
dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_TEXT_HORIZ_OFFSET);
temporaryValue =
(temporaryParam.AsDouble() / ToFractionalInches).ToString(formatter) + "''";
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//for Spot Coordinates, add some other Parameters
if ("Spot Coordinates" == spotDimension.Category.Name)
{
//Coordinate Origin
temporaryParam =
dimensionType.get_Parameter(BuiltInParameter.SPOT_COORDINATE_BASE);
temporaryValue = temporaryParam.AsInteger().ToString();
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Top Value
temporaryParam =
dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_TOP_VALUE);
temporaryValue = s_topBottomValue[temporaryParam.AsInteger()];
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Bottom Value
temporaryParam =
dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_BOT_VALUE);
temporaryValue = s_topBottomValue[temporaryParam.AsInteger()];
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//North / South s_indicator
temporaryParam =
dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_IND_NS);
temporaryValue = temporaryParam.AsString();
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//East / West s_indicator
temporaryParam =
dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_IND_EW);
temporaryValue = temporaryParam.AsString();
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);
}
//for Spot Elevation, add some other Parameters
else
{
//Instance Parameter----Value
temporaryParam =
spotDimension.get_Parameter(BuiltInParameter.DIM_VALUE_LENGTH);
temporaryValue = temporaryParam.AsDouble().ToString(formatter) + "'";
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Elevation Origin
temporaryParam = dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_BASE);
temporaryValue = s_elevationOrigin[temporaryParam.AsInteger()];
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Elevation s_indicator
temporaryParam =
dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_IND_ELEVATION);
temporaryValue = temporaryParam.AsString();
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);
}

//Text Orientation
temporaryParam =
dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_TEXT_ORIENTATION);
temporaryValue = s_textOrientation[temporaryParam.AsInteger()];
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//s_indicator as Prefix / Suffix
temporaryParam = dimensionType.get_Parameter(BuiltInParameter.SPOT_ELEV_IND_TYPE);
temporaryValue = s_indicator[temporaryParam.AsInteger()];
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Text Font
temporaryParam = dimensionType.get_Parameter(BuiltInParameter.TEXT_FONT);
temporaryValue = temporaryParam.AsString();
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

//Text Background
temporaryParam = dimensionType.get_Parameter(BuiltInParameter.DIM_TEXT_BACKGROUND);
temporaryValue = s_textBackground[temporaryParam.AsInteger()];
AddDataRow(temporaryParam.Definition.Name, temporaryValue, parameterTable);

#endregion
return parameterTable;
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message, "A error in Function 'GetParameterTable':");
return null;
}
}

/// <summary>
/// Create an empty table with parameter's name column and value column
/// </summary>
/// <returns>a DataTable be initialized</returns>
private DataTable CreateTable()
{
// Create a new DataTable.
DataTable propDataTable = new DataTable("ParameterTable");

// Create parameter column and add to the DataTable.
DataColumn paraDataColumn = new DataColumn();
paraDataColumn.DataType = System.Type.GetType("System.String");
paraDataColumn.ColumnName = "Parameter";
paraDataColumn.Caption = "Parameter";
paraDataColumn.ReadOnly = true;
// Add the column to the DataColumnCollection.
propDataTable.Columns.Add(paraDataColumn);

// Create value column and add to the DataTable.
DataColumn valueDataColumn = new DataColumn();
valueDataColumn.DataType = System.Type.GetType("System.String");
valueDataColumn.ColumnName = "Value";
valueDataColumn.Caption = "Value";
valueDataColumn.ReadOnly = true;
propDataTable.Columns.Add(valueDataColumn);

return propDataTable;
}

/// <summary>
/// add one row to datatable
/// </summary>
/// <param name="parameterName">name of parameter</param>
/// <param name="Value">value of parameter</param>
/// <param name="parameterTable">datatable to be added row</param>
private void AddDataRow(string parameterName, string Value, DataTable parameterTable)
{
DataRow newRow = parameterTable.NewRow();
newRow["Parameter"] = parameterName;
newRow["Value"] = Value;
parameterTable.Rows.Add(newRow);
}
}
}

SpotDimensionsData.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.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
namespace Revit.SDK.Samples.SpotDimension.CS
{
    
    /// <summary>
    /// Store all the views and spot dimensions in Revit.
    /// </summary>
    public class SpotDimensionsData
    {
        UIApplication m_revit;  // Store the reference of the application in Revit
        List<string>          m_views         = new List<string>();
        List<Autodesk.Revit.DB.SpotDimension> m_spotDimensions = new List<Autodesk.Revit.DB.SpotDimension>(); //a list to store all SpotDimensions in the project
        /// <summary>
        /// a list of all the SpotDimensions in the project
        /// </summary>
        public ReadOnlyCollection<Autodesk.Revit.DB.SpotDimension> SpotDimensions
        {
            get
            {
                return new ReadOnlyCollection<Autodesk.Revit.DB.SpotDimension>(m_spotDimensions);
            }
        }
        /// <summary>
        /// a list of all the views that have SpotDimentions in the project
        /// </summary>
        public ReadOnlyCollection<string> Views
        {
            get
            {
                return new ReadOnlyCollection<string>(m_views);
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="commandData"></param>
        public SpotDimensionsData(ExternalCommandData commandData)
        {
             m_revit = commandData.Application;
             GetSpotDimensions();
        }
        
        /// <summary>
        /// try to find all the SpotDimensions and add them to the list
        /// </summary>
        private void GetSpotDimensions()
        {
            //get the active document 
            Document document = m_revit.ActiveUIDocument.Document;
            FilteredElementIterator elementIterator = (new FilteredElementCollector(document)).OfClass(typeof(Autodesk.Revit.DB.SpotDimension)).GetElementIterator();
            elementIterator.Reset();
            while (elementIterator.MoveNext())
            {
                //find all the SpotDimensions and views
                Autodesk.Revit.DB.SpotDimension tmpSpotDimension = elementIterator.Current as Autodesk.Revit.DB.SpotDimension;
                if (null != tmpSpotDimension)
                {
                    m_spotDimensions.Add(tmpSpotDimension);
                    if (m_views.Contains(tmpSpotDimension.View.Name) == false)
                    {
                        m_views.Add(tmpSpotDimension.View.Name);
                    }
                }
            }
        }
    }
}