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.
Revit Parameter Organizer can organize various Revit parameters such as Shared Parameters, Family Parameters, Project Parameters and Built-in Parameter in many good ways.
Through checking on 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 static List<T> RawConvertSetToList<T>(IEnumerable set)
{
List<T> list = (from T p in set select p).ToList<T>();
return list;
}
public static bool RawClearSharedParameterGroup(RvtApplication app, string group)
{
DefinitionFile defFile = app.OpenSharedParameterFile();
if (defFile != null)
{
DefinitionGroup dg = RawConvertSetToList<DefinitionGroup>(defFile.Groups).FirstOrDefault(g => g.Name == group);
if (dg != null)
{
dg.Definitions.Clear();
return true;
}
}
return false;
}
…
RawClearSharedParameterGroup(CachedApp, "Group4");
…
However, some secret is uncovered:
Autodesk.Revit.Exceptions.InvalidOperationException: Collection is read-only
at Autodesk.Revit.DB.DefinitionsBase.Clear()
Any other straightforward API approaches? Maybe use BindingMap again?
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 static bool RemoveSharedParameterGroupFromFile(string groupName, string fileName)
{
List<string[]> lines = ReadSharedParameterFile(fileName);
List<int> indexes = new List<int>();
if (lines.Count > 6)
{
string groupNum = null;
for (int i = 5; i < lines.Count; i++)
{
if (lines[i].Length == 3 && lines[i][0] == "GROUP" && lines[i][2] == groupName)
{
indexes.Add(i);
groupNum = lines[i][1];
}
if (lines[i].Length == 7 && !string.IsNullOrEmpty(groupNum) && lines[i][5] == groupNum)
{
indexes.Add(i);
}
}
}
if (indexes.Count > 0)
{
indexes.Reverse();
foreach (int index in indexes)
{
lines.RemoveAt(index);
}
WriteContentBackToTxtFile(lines, fileName);
return true;
}
return false;
}
public static bool RemoveSharedParameterFromFile(string paramName, string fileName)
{
List<string[]> lines = ReadSharedParameterFile(fileName);
int index = -1;
if (lines.Count > 6)
{
for (int i = 6; i < lines.Count; i++)
{
if (lines[i][2] == paramName)
{
index = i;
break;
}
}
}
if (index != -1)
{
lines.RemoveAt(index);
WriteContentBackToTxtFile(lines, fileName);
return true;
}
return false;
}
public static void WriteContentBackToTxtFile(List<string[]> lines, string path)
{
using (StreamWriter writer = new StreamWriter(path))
{
foreach (string[] line in lines)
{
string lineContent = string.Empty;
foreach (string str in line)
{
lineContent += string.Format("{0}\t", str);
}
lineContent = lineContent.Remove(lineContent.Length - 1);
writer.WriteLine(lineContent);
}
}
}
public static List<string[]> ReadSharedParameterFile(string path)
{
List<string[]> lines = new List<string[]>();
using (StreamReader reader = new StreamReader(path))
{
while (!reader.EndOfStream)
{
string[] line = reader.ReadLine().Split('\t');
if (line != null && line.Length > 0)
{
lines.Add(line);
}
}
}
return lines;
}
Give it a try and you will not miss it.
SharedParameter DefinitionGroup Eraser of RevitAddinWizard helps generate the code in no time.
Recent Comments