Revit shared parameters are defined in an external text file and once the file is created and set up it will be used for all models from that moment on until the setting is changed to some other file later.
From the API point of view, the Revit Application object provides a method, OpenSharedParameterFile(), to get the DefinitionFile for all shared parameters. The DefinitionFile contains DefinitionGroup and ExternalDefinition instances. Each shared parameter has both name and GUID identifiers in its definition object (ExternalDefinition) and belongs to a specific share parameter group (DefinitionGroup).
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = Value
End Set
End Property
Private m_Name As String
Public Property GUID() As String
Get
Return m_GUID
End Get
Set(ByVal value As String)
m_GUID = Value
End Set
End Property
Private m_GUID As String
Public Property Owner() As String
Get
Return m_Owner
End Get
Set(ByVal value As String)
m_Owner = Value
End Set
End Property
Private m_Owner As String
Public Property Group() As BuiltInParameterGroup
Get
Return m_Group
End Get
Set(ByVal value As BuiltInParameterGroup)
m_Group = Value
End Set
End Property
Private m_Group As BuiltInParameterGroup
Public Property Type() As ParameterType
Get
Return m_Type
End Get
Set(ByVal value As ParameterType)
m_Type = Value
End Set
End Property
Private m_Type As ParameterType
Public Property [ReadOnly]() As Boolean
Get
Return m_ReadOnly
End Get
Set(ByVal value As Boolean)
m_ReadOnly = value
End Set
End Property
Private m_ReadOnly As Boolean
Public Property Visible() As Boolean
Get
Return m_Visible
End Get
Set(ByVal value As Boolean)
m_Visible = value
End Set
End Property
Private m_Visible As Boolean
End Class
Public Shared Function RawGetSharedParametersInfo(ByVal defFile As DefinitionFile) As List(Of RawSharedParameterInfo)
RawSharedParameterInfo.FileName = defFile.Filename
Dim paramList As List(Of RawSharedParameterInfo) = (From dg As DefinitionGroup In defFile.Groups Select dg _
From d In dg.Definitions Select New RawSharedParameterInfo() _
With { _
.Name = d.Name, _
.GUID = d.GUID.ToString(), _
.Owner = d.OwnerGroup.Name, _
.Group = d.ParameterGroup, _
.Type = d.ParameterType, _
.ReadOnly = d.IsReadOnly, _
.Visible = d.Visible _
}).ToList()
Return paramList
End Function
And the following code will convert the informative object List into a single string:
Public Shared Function RawParametersInfoToCSVString(ByVal infoList As List(Of RawSharedParameterInfo), ByRef title As String) As String
Dim sb As New StringBuilder()
Dim propInfoArrary As PropertyInfo() = GetType(RawSharedParameterInfo).GetProperties()
For Each pi As PropertyInfo In propInfoArrary
title += pi.Name + ","
Next
title = title.Remove(title.Length - 1)
For Each info As RawSharedParameterInfo In infoList
For Each pi As PropertyInfo In propInfoArrary
Dim obj As Object = info.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, Nothing, info, Nothing)
sb.Append((If(obj Is Nothing, String.Empty, obj.ToString())) & ",")
Next
sb.Remove(sb.Length - 1, 1).Append(Environment.NewLine)
Next
Return sb.ToString()
End Function
As a bonus, the following generic method can apply to any type which wants to export its public properties:
Public Shared Function RawParametersInfoToCSVString(Of T)(ByVal infoList As List(Of T), ByRef title As String) As String
Dim sb As New StringBuilder()
Dim propInfoArrary As PropertyInfo() = GetType(T).GetProperties()
For Each pi As PropertyInfo In propInfoArrary
title += pi.Name + ","
Next
title = title.Remove(title.Length - 1)
For Each info As T In infoList
For Each pi As PropertyInfo In propInfoArrary
Dim obj As Object = info.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, Nothing, info, Nothing)
sb.Append((If(obj Is Nothing, String.Empty, obj.ToString())) & ",")
Next
sb.Remove(sb.Length - 1, 1).Append(Environment.NewLine)
Next
Return sb.ToString()
End Function
Then we can write all the shared parameter information of a Revit application to a CSV file.
…
If CachedApp.OpenSharedParameterFile() IsNot Nothing Then
Dim paramsInfo As List(Of RawSharedParameterInfo) = RawGetSharedParametersInfo(CachedApp.OpenSharedParameterFile())
Using sw As New StreamWriter("c:\temp\SharedParametersInfo.csv")
Dim title As String = String.Empty
Dim rows As String = RawParametersInfoToCSVString(paramsInfo, title)
sw.WriteLine(title)
sw.Write(rows)
End Using
End If
…
Finally the CSV file can be read into a spreadsheet of Excel.
By the way the sample shared parameter external definition file is appended below for reference:
# This is a Revit shared parameter file.
# Do not edit manually.
*META VERSION MINVERSION
META 2 1
*GROUP ID NAME
GROUP 1 Group1
GROUP 2 Group2
GROUP 3 Group3
*PARAM GUID NAME DATATYPE DATACATEGORY GROUP VISIBLE
PARAM 5d4dcb0d-49db-4ac3-a618-98431e8e5f39 Area11 AREA 1 1
PARAM 18daaa5a-1a42-45b0-8330-14eccaa3283c LengthTest11 LENGTH 1 1
PARAM cec3106d-5a45-4191-91e1-f18f3f8c58dd AngleTest21 FORCE 2 1
PARAM a2143977-eecc-4e87-9953-6afaf1bcbddf ForceTest21 FORCE 2 1
PARAM 23e79d90-7f17-4f02-bcdd-aee0fa41f22e Force11 FORCE 1 1
PARAM b3bbc191-68d6-4992-b9d8-c9b81006711b ColorTemprature11 COLOR_TEMPERATURE 1 1
PARAM d29fac95-7a0e-439f-97bd-124a2100ba62 WattageTest21 ELECTRICAL_WATTAGE 2 1
PARAM 7e689e9f-d2ac-46bc-a04f-3741518586f1 YesNoTest31 YESNO 3 1
PARAM e08272bb-d2e1-4be8-8955-50e27f560649 TextTest31 LENGTH 3 1
PARAM a5a433d4-11bd-48c3-8fa0-a59af5281c8a MaterialTest21 MATERIAL 2 1
PARAM 966d79df-9baa-45ec-9334-f49e395ca903 URLTest31 URL 3 1
SharedParameter Infoer of RevitAddinWizard can help do all of these in a configurable and flexible way in either C# or VB.NET automatically depending on what the language of the current project is.
Expression of type 'Autodesk.Revit.DB.DefinitionGroups' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider.
Posted by: Gregory Mertens | 10/01/2012 at 02:47 PM
Please add the following imports to the top of your source and reference those LINQ related assemblies and any missing ones into your project.
Imports System
Imports System.Text
Imports System.Linq
Imports System.Xml
Imports System.Reflection
Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports System.Windows.Forms
Imports System.IO
Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.DB.Events
Imports Autodesk.Revit.DB.Architecture
Imports Autodesk.Revit.DB.Structure
Imports Autodesk.Revit.DB.Mechanical
Imports Autodesk.Revit.DB.Electrical
Imports Autodesk.Revit.DB.Plumbing
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.UI.Selection
Imports Autodesk.Revit.UI.Events
Imports Autodesk.Revit.Collections
Imports Autodesk.Revit.Exceptions
Imports Autodesk.Revit.Utility
Imports RvtApplication = Autodesk.Revit.ApplicationServices.Application
Imports RvtDocument = Autodesk.Revit.DB.Document
Or, you can simply use the RevitAddinWizard to create the Revit Addin project for you and everything should be just fine there.
Posted by: Spiderinnet | 10/01/2012 at 10:32 PM