应用程序:FireRating
Revit平台:所有
Revit版本:2011.0
首次发布版本:8.0
编程语言:VB.NET
技能水平:入门级
类别:参数
类型:外部命令
主题:创建共享参数;从excle导出和导入。
总结:
这个程序定义了三个命令:ApplyParameter,用于添加新的共享参数;ExportFireRating,用于将防火等级值导出到Excel文件;ImportFireRating,用于从Excel文件中导入防火等级值并将它们应用于门。
相关类:
Autodesk.Revit.UI.IExternalCommand
Autodesk.Revit.DB.Category
项目文件:
ApplyParameter.vb
此文件包含一个从IExternalCommmand接口继承的Command类,用于将新的共享参数添加到Revit并应用于所有门。
ExportFireRating.vb
此文件包含一个从IExternalCommmand接口继承的Command类,用于将防火等级值导出到Excel文件。
ImportFireRating.vb
此文件包含一个从IExternalCommmand接口继承的Command类,用于从Excel文件中导入防火等级值并将它们应用于门。
MainModule.vb
模块包含名称和路径。
描述:
此程序定义了三个命令:
- ApplyParameter - 将新的共享参数添加到Revit,并将其应用于所有门。
- ExportFireRating - 将防火等级值导出到Excel文件。
- ImportFireRating - 从Excel文件中导入防火等级值,并将其应用于门。
说明:
1. 打开Revit。
2. 绘制几堵墙并放置几扇门。执行“应用参数”命令。这将在Revit中添加一个共享参数,并将其应用于所有门。选择任意一个级别,选择一扇门并查看其属性。您会发现“防火等级”属性已经被添加到门上。
3. 运行“导出防火等级”命令。这将启动Excel并将所有防火等级提取到Excel中。您可以在其中看到您更改的等级。选择任何一扇门,在防火等级列下设置新的值。(注意级别和ID。我们将在下一个命令中进行导入。)保存Excel文件并关闭Excel。
4. 运行“导入防火等级”命令。这将加载文件并从Excel中读取数据到模型中。打开元素属性,注意防火等级已经被更改。
源代码
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
ApplyParameter.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.Revit
Imports Autodesk.Revit.UI
<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 ApplyParameter
' All Autodesk Revit external commands must support this interface
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 ExternalCommandData, ByRef message As String, ByVal elements As Autodesk.Revit.DB.ElementSet) As Autodesk.Revit.UI.Result Implements Autodesk.Revit.UI.IExternalCommand.Execute
Dim sharedParametersFile As Autodesk.Revit.DB.DefinitionFile = OpenSharedParamFile(commandData.Application.Application)
If sharedParametersFile Is Nothing Then
message = "Unable to open shared parameters file"
Return Autodesk.Revit.UI.Result.Failed
End If
Dim fireGroup As Autodesk.Revit.DB.DefinitionGroup = OpenGroup(sharedParametersFile)
If (fireGroup Is Nothing) Then
message = "Unable to create fire group"
Return Autodesk.Revit.UI.Result.Failed
End If
Dim definition As Autodesk.Revit.DB.Definition = OpenDefinition(fireGroup)
If (definition Is Nothing) Then
message = "Unable to create fire rating parameter definition"
Return Autodesk.Revit.UI.Result.Failed
End If
Dim category As Autodesk.Revit.DB.Category = commandData.Application.ActiveUIDocument.Document.Settings.Categories.Item(mCategoryName)
Dim categorySet As Autodesk.Revit.DB.CategorySet = commandData.Application.Application.Create.NewCategorySet()
categorySet.Insert(category)
Dim tran As Autodesk.Revit.DB.Transaction = New Autodesk.Revit.DB.Transaction(commandData.Application.ActiveUIDocument.Document, "ApplyParameter")
tran.Start()
Dim binding As Autodesk.Revit.DB.Binding = commandData.Application.Application.Create.NewInstanceBinding(categorySet)
commandData.Application.ActiveUIDocument.Document.ParameterBindings.Insert(definition, binding)
tran.Commit()
MsgBox("Applied shared parameters to the doors. Check the properties of doors.")
Return Autodesk.Revit.UI.Result.Succeeded
End Function
Private Function OpenSharedParamFile(ByVal application As Autodesk.Revit.ApplicationServices.Application) As Autodesk.Revit.DB.DefinitionFile
Const fullPath As String = mPath + "\" + mFilename
Dim stream As StreamWriter
stream = New StreamWriter(fullPath)
stream.Close()
application.SharedParametersFilename = fullPath
Dim sharedParametersFile As Autodesk.Revit.DB.DefinitionFile
sharedParametersFile = Nothing
On Error Resume Next
sharedParametersFile = application.OpenSharedParameterFile
On Error GoTo 0
Return sharedParametersFile
End Function
Private Function OpenGroup(ByVal sharedParametersFile As Autodesk.Revit.DB.DefinitionFile) As Autodesk.Revit.DB.DefinitionGroup
Dim fireGroup As Autodesk.Revit.DB.DefinitionGroup
fireGroup = sharedParametersFile.Groups.Item(mGroupName)
If (fireGroup Is Nothing) Then
fireGroup = sharedParametersFile.Groups.Create(mGroupName)
End If
Return fireGroup
End Function
Private Function OpenDefinition(ByVal fireGroup As Autodesk.Revit.DB.DefinitionGroup) As Autodesk.Revit.DB.Definition
Dim definition As Autodesk.Revit.DB.Definition = fireGroup.Definitions.Item(mParameterName)
Dim externalDefinitonCreationOptions As Autodesk.Revit.DB.ExternalDefinitionCreationOptions
externalDefinitonCreationOptions = New DB.ExternalDefinitionCreationOptions(mParameterName, Autodesk.Revit.DB.ParameterType.Integer)
If definition Is Nothing Then
definition = fireGroup.Definitions.Create(externalDefinitonCreationOptions)
End If
Return definition
End Function
End Class
ExportFireRating.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 MsExcel = Microsoft.Office.Interop.Excel
Imports Autodesk.Revit
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI
Imports System.Windows.Forms
<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 ExportFireRating
' All Autodesk Revit external commands must support this interface
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 ExternalCommandData, ByRef message As String, ByVal elements As Autodesk.Revit.DB.ElementSet) As Autodesk.Revit.UI.Result Implements Autodesk.Revit.UI.IExternalCommand.Execute
Dim guid As Guid = FindGUID(commandData.Application.Application)
If (guid = guid.Empty) Then
Return Autodesk.Revit.UI.Result.Failed
End If
'get a set of element which is not elementType
Dim filter As Autodesk.Revit.DB.ElementIsElementTypeFilter
filter = New Autodesk.Revit.DB.ElementIsElementTypeFilter(True)
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 doors As Autodesk.Revit.DB.ElementSet = commandData.Application.Application.Create.NewElementSet
Do While (iter.MoveNext())
Dim element As Autodesk.Revit.DB.Element = iter.Current
If Not (TypeOf element Is Autodesk.Revit.DB.ElementType) Then
If Not (element.Category Is Nothing) Then
If (element.Category.Name = mCategoryName) Then
doors.Insert(element)
End If
End If
End If
Loop
Dim excel As MsExcel.Application
excel = Nothing
SendToExcel(excel, commandData.Application.Application, doors, guid)
Return Autodesk.Revit.UI.Result.Succeeded
End Function
Function FindGUID(ByVal application As Autodesk.Revit.ApplicationServices.Application) As Guid
Try
Dim sharedParamFile As Autodesk.Revit.DB.DefinitionFile = application.OpenSharedParameterFile
Dim group As Autodesk.Revit.DB.DefinitionGroup = sharedParamFile.Groups.Item(mGroupName)
Dim definition As Autodesk.Revit.DB.Definition = group.Definitions.Item(mParameterName)
Dim externalDefinition As Autodesk.Revit.DB.ExternalDefinition = definition
Return externalDefinition.GUID
Catch ex As Exception
Autodesk.Revit.UI.TaskDialog.Show("Revit", "ApplyParameter first")
Return Nothing
End Try
End Function
' LaunchExcel will try to launch Excel 2003 or later. It will then create an empty workbook
' and remove all of the work sheets except one which will form the bassis for the first
' category that is sent to Excel by this sample
Function LaunchExcel() As MsExcel.Application
Dim excel As MsExcel.Application = New MsExcel.ApplicationClass()
If (excel Is Nothing) Then
Return Nothing
End If
' make excel visible so that operations made are visible to the user
excel.Visible = True
' Add a new workbook which will default to having 3 work sheets
Dim workbook As MsExcel.Workbook = excel.Workbooks.Add()
Dim worksheet As MsExcel.Worksheet
' remove all the sheets except one
Do While workbook.Sheets.Count > 1
worksheet = workbook.Sheets.Item(1)
worksheet.Delete()
Loop
Return excel
End Function
Function SendToExcel(ByRef excel As MsExcel.Application, ByVal application As Autodesk.Revit.ApplicationServices.Application, ByVal elementSet As Autodesk.Revit.DB.ElementSet, ByVal guid As Guid) As Boolean
SendToExcel = False
'Excel can experience errors during this process.
On Error GoTo ExitSendToExcel
Dim worksheet As MsExcel.Worksheet
' If excel is not running, then launch it which will result in one sheet remaining. If excel
' is already running then we need to create a new sheet.
If (excel Is Nothing) Then
excel = LaunchExcel()
If (excel Is Nothing) Then
Exit Function
End If
worksheet = excel.ActiveSheet
Else
worksheet = excel.Worksheets.Add()
End If
worksheet.Name = mParameterName
worksheet.Cells(1, 1).Value = "ID"
worksheet.Cells(1, 2).Value = "Level"
worksheet.Cells(1, 3).Value = "Tag"
worksheet.Cells(1, 4).Value = mParameterName
Dim element As Autodesk.Revit.DB.Element
Dim row As Integer = 2
For Each element In elementSet
'ID
worksheet.Cells(row, 1).Value = element.Id.IntegerValue
'Level
Dim level As Autodesk.Revit.DB.Level
level = element.Document.GetElement(element.LevelId)
If Not (level Is Nothing) Then
worksheet.Cells(row, 2).Value = level.Name
End If
'Tag
Dim tagParameter As Autodesk.Revit.DB.Parameter = element.Parameter(Autodesk.Revit.DB.BuiltInParameter.ALL_MODEL_MARK)
If Not (tagParameter Is Nothing) Then
worksheet.Cells(row, 3).Value = tagParameter.AsString
End If
'Fire Rating
Dim parameter As Autodesk.Revit.DB.Parameter = element.Parameter(guid)
If Not (parameter Is Nothing) Then
worksheet.Cells(row, 4).Value = parameter.AsInteger
End If
row = row + 1
Next
SendToExcel = True
Dim filename As String = mPath & "\" & mExcelFilename
worksheet.SaveAs(filename)
ExitSendToExcel:
End Function
End Class
ImportFireRating.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 MsExcel = Microsoft.Office.Interop.Excel
Imports Autodesk.Revit
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI
Imports System.Windows.Forms
<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 ImportFireRating
' All Autodesk Revit external commands must support this interface
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 ExternalCommandData, ByRef message As String, ByVal elements As Autodesk.Revit.DB.ElementSet) As Autodesk.Revit.UI.Result Implements Autodesk.Revit.UI.IExternalCommand.Execute
'Dim excel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.ApplicationClass()
Dim excel As MsExcel.Application = New MsExcel.ApplicationClass()
If (excel Is Nothing) Then
message = "Failed to launch excel"
Return Autodesk.Revit.UI.Result.Failed
End If
excel.Visible = True
Dim filename As String = mPath & "\" & mExcelFilename
'Dim workbook As Microsoft.Office.Interop.Excel.Workbook = excel.Workbooks.Open(filename)
Dim workbook As MsExcel.Workbook = excel.Workbooks.Open(filename)
'Dim worksheet As Microsoft.Office.Interop.Excel.Worksheet
Dim worksheet As MsExcel.Worksheet
worksheet = workbook.ActiveSheet
Dim row As Integer = 2
Dim value As String
Do
value = worksheet.Cells(row, 1).Value
If Not (value Is Nothing) Then
Dim fireRatingValue As String
fireRatingValue = worksheet.Cells(row, 4).Value
If Not (fireRatingValue Is Nothing) Then
Dim idInteger As Integer
Dim fireRateDouble As Double
Try
idInteger = value
fireRateDouble = fireRatingValue
Catch ex As Exception
TaskDialog.Show("Revit", ex.ToString)
End Try
Dim elementId As Autodesk.Revit.DB.ElementId = New Autodesk.Revit.DB.ElementId(idInteger)
Dim element As Autodesk.Revit.DB.Element = commandData.Application.ActiveUIDocument.Document.GetElement(elementId)
If Not (element Is Nothing) Then
Dim parameters As Autodesk.Revit.DB.ParameterSet = element.Parameters
Dim parameter As Autodesk.Revit.DB.Parameter
For Each parameter In parameters
If (parameter.Definition.Name = mParameterName) Then
Dim tran As Autodesk.Revit.DB.Transaction = New Autodesk.Revit.DB.Transaction(commandData.Application.ActiveUIDocument.Document, "ImportParameter")
tran.Start()
parameter.Set(fireRateDouble)
tran.Commit()
Exit For
End If
Next
End If
End If
row = row + 1
End If
Loop Until (value Is Nothing)
Return Autodesk.Revit.UI.Result.Succeeded
End Function
End Class
MainModule.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.
'
Module MainModule
' adjust the path if you save this program in the different location.
'
Public Const mPath As String = "..\.."
Public Const mFilename As String = "FireRating.txt"
Public Const mExcelFilename As String = "FireRating.xls"
Public Const mGroupName As String = "Fire"
Public Const mParameterName As String = "Fire Rating"
Public Const mCategoryName As String = "Doors"
End Module
版权所有 :无锡模信建筑科技有限公司 苏ICP备2021028830号-1 BIM建模|BIM技术应用|BIM软件开发
联系地址:江苏省无锡市新吴区龙山路4号B座705 手机:18761516598