We talked about a variety of ways to erase a shared parameter previously regardless of failure, success, or misconception. In this article we will be discussing how to erase a DefinitionGroup regarding shared parameters.
Through checking relevant classes and methods in the API, we can naturally think that the DefinitionsBase.Clear() is super proper for this objective. Nothing is wrong with the idea but let’s see what it will bring us.
The following methods and test code are expected to do the job:
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 RawClearSharedParameterGroup(ByVal app As RvtApplication, ByVal group As String) As Boolean
Dim defFile As DefinitionFile = app.OpenSharedParameterFile()
If defFile IsNot Nothing Then
Dim dg As DefinitionGroup = RawConvertSetToList(Of DefinitionGroup)(defFile.Groups).FirstOrDefault(Function(g) g.Name = group)
If dg IsNot Nothing Then
dg.Definitions.Clear()
Return True
End If
End If
Return False
End Function
…
RawClearSharedParameterGroup(CachedApp, "Group4")
…
However, some secret is uncovered:
Autodesk.Revit.Exceptions.InvalidOperationException: Collection is read-only
at Autodesk.Revit.DB.DefinitionsBase.Clear()
Q: Any other straightforward API approaches? Maybe use BindingMap again?
A: No way. The BindingMap does not carry any explicit information about the DefinitionGroup at all. Though maybe the OwnerGroup information of each bound shared parameter can still be retrieved with some means so that all the shared parameters in the DefinitionGroup can be unbound (removed from the BindingMap) or unassociated from a particular Revit project model (removed from the Project Parameter list), the shared parameters are still there. So is the group (DefinitionGroup) holding them!
It is not a bad thing in this regard and saves some confusion actually.
So now let’s see how to truly erase a shared parameter definition group (DefinitionGroup). As demonstrated in the previous post, we have to parse the shared parameter external definition file one more time.
…
RemoveSharedParameterGroupFromFile(@"Group1", CachedApp.SharedParametersFilename)
…
Public Shared Function RemoveSharedParameterGroupFromFile(ByVal groupName As String, ByVal fileName As String) As Boolean
Dim lines As List(Of String()) = ReadSharedParameterFile(fileName)
Dim indexes As New List(Of Integer)()
If lines.Count > 6 Then
Dim groupNum As String = Nothing
For i As Integer = 5 To lines.Count - 1
If lines(i).Length = 3 AndAlso lines(i)(0) = "GROUP" AndAlso lines(i)(2) = groupName Then
indexes.Add(i)
groupNum = lines(i)(1)
End If
If lines(i).Length = 7 AndAlso Not String.IsNullOrEmpty(groupNum) AndAlso lines(i)(5) = groupNum Then
indexes.Add(i)
End If
Next
End If
If indexes.Count > 0 Then
indexes.Reverse()
For Each index As Integer In indexes
lines.RemoveAt(index)
Next
WriteContentBackToTxtFile(lines, fileName)
Return True
End If
Return False
End Function
Public Shared Function RemoveSharedParameterFromFile(ByVal paramName As String, ByVal fileName As String) As Boolean
Dim lines As List(Of String()) = ReadSharedParameterFile(fileName)
Dim index As Integer = -1
If lines.Count > 6 Then
For i As Integer = 6 To lines.Count - 1
If lines(i)(2) = paramName Then
index = i
Exit For
End If
Next
End If
If index <> -1 Then
lines.RemoveAt(index)
WriteContentBackToTxtFile(lines, fileName)
Return True
End If
Return False
End Function
Public Shared Sub WriteContentBackToTxtFile(ByVal lines As List(Of String()), ByVal path As String)
Using writer As New StreamWriter(path)
For Each line As String() In lines
Dim lineContent As String = String.Empty
For Each str As String In line
lineContent += String.Format("{0}" & vbTab, str)
Next
lineContent = lineContent.Remove(lineContent.Length - 1)
writer.WriteLine(lineContent)
Next
End Using
End Sub
Public Shared Function ReadSharedParameterFile(ByVal path As String) As List(Of String())
Dim lines As New List(Of String())()
Using reader As New StreamReader(path)
While Not reader.EndOfStream
Dim line As String() = reader.ReadLine().Split(ControlChars.Tab)
If line IsNot Nothing AndAlso line.Length > 0 Then
lines.Add(line)
End If
End While
End Using
Return lines
End Function
Give it a try and you will not miss it.
SharedParameter DefinitionGroup Eraser of RevitAddinWizard helps generate the code in VB.NET or C# in no time.
Recent Comments