应用程序:DesignOptionReader

Revit平台:所有版本

Revit版本:2011.0

首次发布版本:9.0

编程语言:VB.NET

技能水平:初学者

类别:元素,基础知识

类型:外部命令。

主题:显示设计选项。

摘要:此示例演示了如何获取Revit当前文档中的设计选项。

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.ApplicationServices.Application

Autodesk.Revit.DB.Element

Autodesk.Revit.DB.DesignOption

项目文件:

Command.vb

它包含了实现IExternalCommand接口的Command类。它还检索所有设计选项并将它们的名称发送到UI界面。

DesignOptionsDialog.vb

这个文件包含了一个窗体类DesignOptionsDialog,它包含一个列表框,显示设计选项。

描述:

此示例提供以下功能。

- 迭代当前项目中的所有元素并检索所有设计选项。

说明:

1. 准备您的Revit项目。

打开或新建一个Revit项目,并确保项目中有一些设计选项。示例项目文件DesignOptionReader.rvt在示例文件夹中可用。

[提示]可以通过菜单[工具] - [设计选项]来添加设计选项到项目中。

2. 执行外部命令。

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

Command.vb

' 
' (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.
'
Imports Autodesk
Imports Autodesk.Revit.DB

<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

    Implements Autodesk.Revit.UI.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 Function Execute(ByVal commandData As Autodesk.Revit.UI.ExternalCommandData, _
        ByRef message As String, ByVal elements As Autodesk.Revit.DB.ElementSet) _
        As Autodesk.Revit.UI.Result Implements Autodesk.Revit.UI.IExternalCommand.Execute

        ' Setup a default message in case any exceptions are thrown that we have not
        ' explicitly handled. On failure the message will be displayed by Revit
        message = "The sample failed"
        Execute = Autodesk.Revit.UI.Result.Failed

        Try
            Dim application As Autodesk.Revit.ApplicationServices.Application = commandData.Application.Application

            Dim filter As Autodesk.Revit.DB.ElementClassFilter
            filter = New Autodesk.Revit.DB.ElementClassFilter(GetType(Autodesk.Revit.DB.DesignOption))
            Dim collector As Autodesk.Revit.DB.FilteredElementCollector
            collector = New Autodesk.Revit.DB.FilteredElementCollector(commandData.Application.ActiveUIDocument.Document)
            collector.WherePasses(filter)
            Dim iter As IEnumerator
            iter = collector.GetElementIterator

            Dim element As Autodesk.Revit.DB.Element
            Dim designOptions As New ElementSet()

            'iterate through all the elements, looking for design option elements
            Do While iter.MoveNext()

                element = iter.Current
                If TypeOf element Is Autodesk.Revit.DB.DesignOption Then
                    designOptions.Insert(element)
                End If

            Loop

            'if we found any design options then display them in a dialog
            If designOptions.Size > 0 Then
                Dim dialog As New DesignOptionsDialog()

                For Each element In designOptions
                    dialog.DesignOptionsList.Items.Add(element.Name)
                Next

                dialog.ShowDialog()

            Else
                MsgBox("There are no design options in this document")
            End If

            ' change our result to successful
            Execute = Autodesk.Revit.UI.Result.Succeeded
            Return Execute
        Catch ex As Exception
            message = ex.Message
            Return Execute
        End Try

    End Function
End Class

DesignOptionsDialog.vb

' 
' (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.
'

Public Class DesignOptionsDialog
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    Friend WithEvents closeButton As System.Windows.Forms.Button

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents DesignOptionsList As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DesignOptionsList = New System.Windows.Forms.ListBox
        Me.closeButton = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'DesignOptionsList
        '
        Me.DesignOptionsList.Location = New System.Drawing.Point(8, 8)
        Me.DesignOptionsList.Name = "DesignOptionsList"
        Me.DesignOptionsList.Size = New System.Drawing.Size(272, 251)
        Me.DesignOptionsList.TabIndex = 0
        '
        'closeButton
        '
        Me.closeButton.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.closeButton.Location = New System.Drawing.Point(107, 267)
        Me.closeButton.Name = "closeButton"
        Me.closeButton.Size = New System.Drawing.Size(75, 23)
        Me.closeButton.TabIndex = 1
        Me.closeButton.Text = "&Close"
        Me.closeButton.UseVisualStyleBackColor = True
        '
        'DesignOptionsDialog
        '
        Me.AcceptButton = Me.closeButton
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.CancelButton = Me.closeButton
        Me.ClientSize = New System.Drawing.Size(290, 299)
        Me.Controls.Add(Me.closeButton)
        Me.Controls.Add(Me.DesignOptionsList)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "DesignOptionsDialog"
        Me.ShowInTaskbar = False
        Me.Text = "Design Options"
        Me.ResumeLayout(False)

    End Sub

#End Region

End Class