As noticed, most of coders especially those parameter coders were disabled in previous builds of RevitAddinCoder when the current Visual Studio project was VB.NET since those coders were not ready to work with VB.NET projects yet at that time.
Good news: All coders support VB.NET now except for Object Picker, Ribbon Creator, and FailureProcessing Handler.
Here is what RevitAddinCoder toolbar looks like when a VB.NET project is active:
Here is what RevitAddinCoder menu looks like when a VB.NET project is active:
So except that Object Picker, Ribbon Creator, and FailureProcessing Handler are still disabled for VB.NET projects, all others work pretty well with both C# and VB.NET.
Let’s take the Parameter Converter for example. Supposing a new class file named ParameterConverters.VB has already been created, necessary namespaces imported, the ParameterConverters class specified in the Parameter Converter dialog, all converters checked, and the OK button pressed, the VB.NET source file will end up looking like this:
Imports System
Imports System.Text
Imports System.Xml
Imports System.Linq
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.DB.Analysis
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
Public Class ParameterConverters
Public Class RawParameterInfo
Private m_Name As String
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_ID As String
Public Property [ID]() As String
Get
Return m_ID
End Get
Set(ByVal value As String)
m_ID = value
End Set
End Property
Private m_Value As String
Public Property [Value]() As String
Get
Return m_Value
End Get
Set(ByVal value As String)
m_Value = value
End Set
End Property
Private m_Group As BuiltInParameterGroup
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_Type As ParameterType
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_Storage As StorageType
Public Property [Storage]() As StorageType
Get
Return m_Storage
End Get
Set(ByVal value As StorageType)
m_Storage = value
End Set
End Property
Private m_Unit As String
Public Property [Unit]() As String
Get
Return m_Unit
End Get
Set(ByVal value As String)
m_Unit = value
End Set
End Property
Private m_Shared As Boolean
Public Property [Shared]() As Boolean
Get
Return m_Shared
End Get
Set(ByVal value As Boolean)
m_Shared = value
End Set
End Property
Private m_ReadOnly As Boolean
Public Property [ReadOnly]() As Boolean
Get
Return m_ReadOnly
End Get
Set(ByVal value As Boolean)
m_ReadOnly = value
End Set
End Property
End Class
Public Shared Function RawGetDUTString(ByVal p As Parameter) As String
Dim unitType As String = String.Empty
Try
unitType = p.DisplayUnitType.ToString()
Catch
End Try
Return unitType
End Function
Public Shared Function RawGetParametersInfo(ByVal e As Autodesk.Revit.DB.Element) As List(Of RawParameterInfo)
Dim paramList As List(Of RawParameterInfo) = (From p In e.Parameters Select New RawParameterInfo() With { _
.Name = p.Definition.Name, _
.ID = If(p.IsShared, p.GUID.ToString(), TryCast(p.Definition, InternalDefinition).BuiltInParameter.ToString()), _
.Value = p.AsValueString(), _
.Group = p.Definition.ParameterGroup, _
.Type = p.Definition.ParameterType, _
.Storage = p.StorageType, _
.Unit = RawGetDUTString(p), _
.Shared = p.IsShared, _
.ReadOnly = p.IsReadOnly _
})
Return paramList
End Function
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
Public Shared Function RawConvertSetToList(Of T)(ByVal setVar As IEnumerable) As List(Of T)
Dim list As List(Of T) = (From p In setVar Select p)
Return list
End Function
Public Shared Function RawCreateSharedParameter(ByVal app As RvtApplication, ByVal name As String, ByVal group As String, ByVal type As ParameterType, ByVal visible As Boolean) As Definition
Dim defFile As DefinitionFile = app.OpenSharedParameterFile()
If defFile Is Nothing Then
Throw New Exception("No SharedParameter File!")
End If
Dim dg As DefinitionGroup = RawConvertSetToList(Of DefinitionGroup)(defFile.Groups).FirstOrDefault(Function(g) g.Name = group)
If dg Is Nothing Then
dg = defFile.Groups.Create(group)
End If
Dim def As Definition = RawConvertSetToList(Of Definition)(dg.Definitions).FirstOrDefault(Function(d) d.Name = name)
If def IsNot Nothing Then
Return def
End If
'dg.Definitions.Erase(def); //ReadOnly Exception!!
def = dg.Definitions.Create(name, type, visible)
Return def
End Function
Public Shared Sub ConvertParameterToSharedParameter(ByVal e As Autodesk.Revit.DB.Element, ByVal name As String, ByVal convertSharedOnly As Boolean, ByVal visibilityOverride As Boolean)
Dim pList As List(Of RawParameterInfo) = RawGetParametersInfo(e)
Dim pListOfInterest As List(Of RawParameterInfo) = pList.FindAll(Function(p) p.Name = name)
For Each info As RawParameterInfo In pListOfInterest
If info.Type = ParameterType.Invalid Then
'It's weird that a valid Parameter can have an invalid ParameterType! We set it as the most popular Text type in this case. Otherwise, the DefinitionGroup.Definitions.Create() would fail!
info.Type = ParameterType.Text
End If
If convertSharedOnly Then
If info.Shared Then
RawCreateSharedParameter(e.Document.Application, name, "For_" & Convert.ToString(e.Category.Name), info.Type, visibilityOverride)
End If
Else
RawCreateSharedParameter(e.Document.Application, name, "For_" & Convert.ToString(e.Category.Name), info.Type, visibilityOverride)
End If
Next
End Sub
Public Shared Sub ConvertAllParametersToSharedParameters(ByVal e As Autodesk.Revit.DB.Element, ByVal convertSharedOnly As Boolean, ByVal visibilityOverride As Boolean)
Dim pList As List(Of RawParameterInfo) = RawGetParametersInfo(e)
For Each info As RawParameterInfo In pList
If info.Type = ParameterType.Invalid Then
'It's weird that a valid Parameter can have an invalid ParameterType! We set it as the most popular Text type in this case. Otherwise, the DefinitionGroup.Definitions.Create() would fail!
info.Type = ParameterType.Text
End If
If convertSharedOnly Then
If info.Shared Then
RawCreateSharedParameter(e.Document.Application, info.Name, "For_" & Convert.ToString(e.Category.Name), info.Type, visibilityOverride)
End If
Else
RawCreateSharedParameter(e.Document.Application, info.Name, "For_" & Convert.ToString(e.Category.Name), info.Type, visibilityOverride)
End If
Next
End Sub
Public Class RawFamilyParameterInfo
Private m_Name As String
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_ElementID As String
Public Property [ElementID]() As String
Get
Return m_ElementID
End Get
Set(ByVal value As String)
m_ElementID = value
End Set
End Property
Private m_GUID 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_BuiltinID As String
Public Property [BuiltinID]() As String
Get
Return m_BuiltinID
End Get
Set(ByVal value As String)
m_BuiltinID = value
End Set
End Property
Private m_Group As BuiltInParameterGroup
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_Type As ParameterType
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_Storage As StorageType
Public Property [Storage]() As StorageType
Get
Return m_Storage
End Get
Set(ByVal value As StorageType)
m_Storage = value
End Set
End Property
Private m_Unit As String
Public Property [Unit]() As String
Get
Return m_Unit
End Get
Set(ByVal value As String)
m_Unit = value
End Set
End Property
Private m_Shared As Boolean
Public Property [Shared]() As Boolean
Get
Return m_Shared
End Get
Set(ByVal value As Boolean)
m_Shared = value
End Set
End Property
Private m_ReadOnly As Boolean
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_Instance As Boolean
Public Property [Instance]() As Boolean
Get
Return m_Instance
End Get
Set(ByVal value As Boolean)
m_Instance = value
End Set
End Property
Private m_Reporting As Boolean
Public Property [Reporting]() As Boolean
Get
Return m_Reporting
End Get
Set(ByVal value As Boolean)
m_Reporting = value
End Set
End Property
Private m_FormulaDetermined As Boolean
Public Property [FormulaDetermined]() As Boolean
Get
Return m_FormulaDetermined
End Get
Set(ByVal value As Boolean)
m_FormulaDetermined = value
End Set
End Property
Private m_CanAssignFormula As Boolean
Public Property [CanAssignFormula]() As Boolean
Get
Return m_CanAssignFormula
End Get
Set(ByVal value As Boolean)
m_CanAssignFormula = value
End Set
End Property
End Class
Public Shared Function RawGetDUTString(ByVal p As FamilyParameter) As String
Dim unitType As String = String.Empty
Try
unitType = p.DisplayUnitType.ToString()
Catch
End Try
Return unitType
End Function
Public Shared Function RawGetFamilyParametersInfo(ByVal doc As Document) As List(Of RawFamilyParameterInfo)
Dim paramList As List(Of RawFamilyParameterInfo) = (From p In doc.FamilyManager.Parameters Select New RawFamilyParameterInfo() With { _
.Name = p.Definition.Name, _
.ElementID = p.Id.IntegerValue.ToString(), _
.GUID = If(TryCast(p.Definition, ExternalDefinition) IsNot Nothing, TryCast(p.Definition, ExternalDefinition).GUID.ToString(), String.Empty), _
.BuiltinID = If(TryCast(p.Definition, InternalDefinition) IsNot Nothing, TryCast(p.Definition, InternalDefinition).BuiltInParameter.ToString(), String.Empty), _
.Group = p.Definition.ParameterGroup, _
.Type = p.Definition.ParameterType, _
.Storage = p.StorageType, _
.Unit = RawGetDUTString(p), _
.Shared = TryCast(p.Definition, ExternalDefinition) IsNot Nothing, _
.ReadOnly = p.IsReadOnly, _
.Instance = p.IsInstance, _
.Reporting = p.IsReporting, _
.FormulaDetermined = p.IsDeterminedByFormula, _
.CanAssignFormula = p.CanAssignFormula _
}).ToList()
Return paramList
End Function
Public Shared Sub ConvertFamilyParameterInfoToSharedParameter(ByVal familyDoc As Document, ByVal info As RawFamilyParameterInfo, ByVal convertSharedOnly As Boolean, ByVal visibilityOverride As Boolean)
If info.Type = ParameterType.Invalid Then
info.Type = ParameterType.Text
End If
If convertSharedOnly Then
If info.Shared Then
RawCreateSharedParameter(familyDoc.Application, info.Name, "For_" & Convert.ToString(familyDoc.Title), info.Type, visibilityOverride)
End If
Else
RawCreateSharedParameter(familyDoc.Application, info.Name, "For_" & Convert.ToString(familyDoc.Title), info.Type, visibilityOverride)
End If
End Sub
Public Shared Sub ConvertFamilyParameterToSharedParameter(ByVal familyDoc As Document, ByVal name As String, ByVal convertSharedOnly As Boolean, ByVal visibilityOverride As Boolean)
Dim pList As List(Of RawFamilyParameterInfo) = RawGetFamilyParametersInfo(familyDoc)
Dim pListOfInterest As List(Of RawFamilyParameterInfo) = pList.FindAll(Function(p) p.Name = name)
For Each info As RawFamilyParameterInfo In pListOfInterest
ConvertFamilyParameterInfoToSharedParameter(familyDoc, info, convertSharedOnly, visibilityOverride)
Next
End Sub
Public Shared Sub ConvertAllFamilyParametersToSharedParameters(ByVal familyDoc As Document, ByVal convertSharedOnly As Boolean, ByVal visibilityOverride As Boolean)
Dim pList As List(Of RawFamilyParameterInfo) = RawGetFamilyParametersInfo(familyDoc)
For Each info As RawFamilyParameterInfo In pList
ConvertFamilyParameterInfoToSharedParameter(familyDoc, info, convertSharedOnly, visibilityOverride)
Next
End Sub
Public Class RawProjectParameterInfo
Private Shared m_FileName As String
Public Shared Property [FileName]() As String
Get
Return m_FileName
End Get
Set(ByVal value As String)
m_FileName = value
End Set
End Property
Private m_Name As String
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_Group As BuiltInParameterGroup
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_Type As ParameterType
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_ReadOnly As Boolean
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_BoundToInstance As Boolean
Public Property [BoundToInstance]() As Boolean
Get
Return m_BoundToInstance
End Get
Set(ByVal value As Boolean)
m_BoundToInstance = value
End Set
End Property
Private m_BoundCategories As String()
Public Property [BoundCategories]() As String()
Get
Return m_BoundCategories
End Get
Set(ByVal value As String())
m_BoundCategories = value
End Set
End Property
Private m_FromShared As Boolean
Public Property [FromShared]() As Boolean
Get
Return m_FromShared
End Get
Set(ByVal value As Boolean)
m_FromShared = value
End Set
End Property
Private m_GUID 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_Owner 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_Visible 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
End Class
Public Shared Function RawGetProjectParametersInfo(ByVal doc As Document) As List(Of RawProjectParameterInfo)
RawProjectParameterInfo.FileName = doc.Title
Dim paramList As New List(Of RawProjectParameterInfo)()
Dim map As BindingMap = doc.ParameterBindings
Dim it As DefinitionBindingMapIterator = map.ForwardIterator()
it.Reset()
While it.MoveNext()
Dim eleBinding As ElementBinding = TryCast(it.Current, ElementBinding)
Dim insBinding As InstanceBinding = TryCast(eleBinding, InstanceBinding)
Dim def As Definition = it.Key
If def IsNot Nothing Then
Dim extDef As ExternalDefinition = TryCast(def, ExternalDefinition)
Dim [shared] As Boolean = extDef IsNot Nothing
Dim param As New RawProjectParameterInfo() With { _
.Name = def.Name, _
.Group = def.ParameterGroup, _
.Type = def.ParameterType, _
.ReadOnly = def.IsReadOnly, _
.BoundToInstance = If(insBinding Is Nothing, False, True), _
.BoundCategories = RawConvertSetToList(Of Autodesk.Revit.DB.Category)(eleBinding.Categories).Select(Function(c) c.Name).ToArray(), _
.FromShared = [shared], _
.GUID = If([shared], extDef.GUID.ToString(), String.Empty), _
.Owner = If([shared], extDef.OwnerGroup.Name, String.Empty), _
.Visible = If([shared], extDef.Visible, True) _
}
paramList.Add(param)
End If
End While
Return paramList
End Function
Public Shared Sub ConvertProjectParameterInfoToSharedParameter(ByVal doc As Document, ByVal info As RawProjectParameterInfo, ByVal visibilityOverride As Boolean)
If info.Type = ParameterType.Invalid Then
info.Type = ParameterType.Text
End If
RawCreateSharedParameter(doc.Application, info.Name, "For_" & Convert.ToString(doc.Title), info.Type, visibilityOverride)
End Sub
Public Shared Sub ConvertProjectParameterToSharedParameter(ByVal doc As Document, ByVal name As String, ByVal visibilityOverride As Boolean)
Dim pList As List(Of RawProjectParameterInfo) = RawGetProjectParametersInfo(doc)
Dim pListOfInterest As List(Of RawProjectParameterInfo) = pList.FindAll(Function(p) p.Name = name)
For Each info As RawProjectParameterInfo In pListOfInterest
ConvertProjectParameterInfoToSharedParameter(doc, info, visibilityOverride)
Next
End Sub
Public Shared Sub ConvertAllProjectParametersToSharedParameters(ByVal doc As Document, ByVal visibilityOverride As Boolean)
Dim pList As List(Of RawProjectParameterInfo) = RawGetProjectParametersInfo(doc)
For Each info As RawProjectParameterInfo In pList
ConvertProjectParameterInfoToSharedParameter(doc, info, visibilityOverride)
Next
End Sub
Public Shared Sub RawAddFamilyParamterAndSetValue(ByVal dimension As Autodesk.Revit.DB.Dimension, ByVal famMan As FamilyManager, ByVal ft As FamilyType, ByVal name As String, ByVal group As BuiltInParameterGroup, ByVal type As ParameterType, _
ByVal isInstance As Boolean, ByVal value As Object)
Dim fp As FamilyParameter = famMan.AddParameter(name, group, type, isInstance)
If value IsNot Nothing Then
RawSetFamilyParameterValue(famMan, ft, fp, value)
End If
If dimension IsNot Nothing Then
dimension.Label = fp
End If
End Sub
Public Shared Function RawFindFamilyParameter(ByVal fm As FamilyManager, ByVal parameterName As String) As FamilyParameter
Dim fp As FamilyParameter = RawConvertSetToList(Of FamilyParameter)(fm.Parameters).FirstOrDefault(Function(e) e.Definition.Name.Equals(parameterName, StringComparison.CurrentCultureIgnoreCase))
If fp Is Nothing Then
Throw New Exception("Invalid ParameterName Input!")
End If
Return fp
End Function
Public Shared Function RawFindFamilyType(ByVal fm As FamilyManager, ByVal familyTypeName As String) As FamilyType
Dim famType As FamilyType = RawConvertSetToList(Of FamilyType)(fm.Types).FirstOrDefault(Function(e) e.Name.Equals(familyTypeName, StringComparison.CurrentCultureIgnoreCase))
If famType Is Nothing Then
Throw New Exception("Invalid FamilyTypeName Input!")
End If
Return famType
End Function
Public Shared Sub RawSetFamilyParameterValue(ByVal fm As FamilyManager, ByVal familyTypeName As String, ByVal parameterName As String, ByVal value As Object)
RawSetFamilyParameterValue(fm, RawFindFamilyType(fm, familyTypeName), RawFindFamilyParameter(fm, parameterName), value)
End Sub
Public Shared Sub RawSetFamilyParameterValue(ByVal fm As FamilyManager, ByVal ft As FamilyType, ByVal parameterName As String, ByVal value As Object)
RawSetFamilyParameterValue(fm, ft, RawFindFamilyParameter(fm, parameterName), value)
End Sub
Public Shared Sub RawSetFamilyParameterValue(ByVal fm As FamilyManager, ByVal familyTypeName As String, ByVal fp As FamilyParameter, ByVal value As Object)
RawSetFamilyParameterValue(fm, RawFindFamilyType(fm, familyTypeName), fp, value)
End Sub
Public Shared Sub RawSetFamilyParameterValue(ByVal fm As FamilyManager, ByVal ft As FamilyType, ByVal fp As FamilyParameter, ByVal value As Object)
Dim curFamType As FamilyType = fm.CurrentType
fm.CurrentType = ft
Try
Select Case fp.StorageType
Case StorageType.None
Exit Select
Case StorageType.Double
If value.GetType().Equals(GetType(String)) Then
fm.Set(fp, Double.Parse(TryCast(value, String)))
Else
fm.Set(fp, Convert.ToDouble(value))
End If
Exit Select
Case StorageType.Integer
If value.GetType().Equals(GetType(String)) Then
fm.Set(fp, Integer.Parse(TryCast(value, String)))
Else
fm.Set(fp, Convert.ToInt32(value))
End If
Exit Select
Case StorageType.ElementId
If value.GetType().Equals(GetType(Autodesk.Revit.DB.ElementId)) Then
fm.Set(fp, TryCast(value, Autodesk.Revit.DB.ElementId))
ElseIf value.GetType().Equals(GetType(String)) Then
fm.Set(fp, New Autodesk.Revit.DB.ElementId(Integer.Parse(TryCast(value, String))))
Else
fm.Set(fp, New Autodesk.Revit.DB.ElementId(Convert.ToInt32(value)))
End If
Exit Select
Case StorageType.String
fm.Set(fp, value.ToString())
Exit Select
End Select
Catch
Throw New Exception("Invalid Value Input!")
Finally
fm.CurrentType = curFamType
End Try
End Sub
Public Shared Function RawFindExternalDefinition(ByVal defFile As DefinitionFile, ByVal name As String) As ExternalDefinition
Dim v As List(Of Definition) = (From dg As DefinitionGroup In defFile.Groups From d In dg.Definitions Where d.Name = name)
If v Is Nothing OrElse v.Count() < 1 Then
Return Nothing
Else
Return v.First()
End If
End Function
Public Shared Sub ConvertSharedParameterToFamilyParameter(ByVal extDef As ExternalDefinition, ByVal famMan As FamilyManager, ByVal instance As Boolean, ByVal type As String, ByVal value As Object)
Dim fp As FamilyParameter = famMan.AddParameter(extDef, extDef.ParameterGroup, instance)
If value IsNot Nothing Then
RawSetFamilyParameterValue(famMan, type, fp, value)
End If
End Sub
End Class
These VB.NET parameter converter coders just compile and build as those C# coders. Enjoy! VB.NET lovers!
The various Parameter Coders of RevitAddinCoder now support VB.NET. So VB.NET programmers do not have to do error-prone code conversions from C# to VB.NET anymore either manually or so-called automatically with other tools.
Recent Comments