mirror of
https://github.com/jeremytammik/RevitSdkSamples.git
synced 2026-08-13 16:27:42 +00:00
189 lines
7.0 KiB
VB.net
189 lines
7.0 KiB
VB.net
'
|
|
' (C) Copyright 2003-2007 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
|
|
|
|
Public Class ExportFireRating
|
|
' All Autodesk Revit external commands must support this interface
|
|
Implements Autodesk.Revit.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 ElementSet) As Autodesk.Revit.IExternalCommand.Result Implements Autodesk.Revit.IExternalCommand.Execute
|
|
|
|
Dim guid As Guid = FindGUID(commandData.Application)
|
|
|
|
Dim iter As IEnumerator = commandData.Application.ActiveDocument.Elements
|
|
Dim doors As Autodesk.Revit.ElementSet = commandData.Application.Create.NewElementSet
|
|
|
|
Do While (iter.MoveNext())
|
|
|
|
Dim element As Autodesk.Revit.Element = iter.Current
|
|
If Not (TypeOf element Is Autodesk.Revit.Symbol) 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, doors, guid)
|
|
|
|
Return Autodesk.Revit.IExternalCommand.Result.Succeeded
|
|
|
|
End Function
|
|
|
|
Function FindGUID(ByVal application As Autodesk.Revit.Application) As Guid
|
|
|
|
Dim sharedParamFile As Autodesk.Revit.Parameters.DefinitionFile = application.OpenSharedParameterFile
|
|
Dim group As Autodesk.Revit.Parameters.DefinitionGroup = sharedParamFile.Groups.Item(mGroupName)
|
|
Dim definition As Autodesk.Revit.Parameters.Definition = group.Definitions.Item(mParameterName)
|
|
|
|
Dim externalDefinition As Autodesk.Revit.Parameters.ExternalDefinition = definition
|
|
|
|
Return externalDefinition.GUID
|
|
|
|
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.Application, ByVal elementSet As Autodesk.Revit.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.Element
|
|
Dim row As Integer = 2
|
|
For Each element In elementSet
|
|
|
|
'ID
|
|
worksheet.Cells(row, 1).Value = element.Id.Value
|
|
|
|
'Level
|
|
Dim level As Autodesk.Revit.Elements.Level
|
|
level = element.Level
|
|
If Not (level Is Nothing) Then
|
|
worksheet.Cells(row, 2).Value = level.Name
|
|
End If
|
|
|
|
'Tag
|
|
Dim tagParameter As Autodesk.Revit.Parameter = element.Parameter(Autodesk.Revit.Parameters.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.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
|