We talked about erasing Shared Parameter groups before. Now let’s see how SharedParameter Group Eraser of RevitAddinWizard can assist us to do the same work. The SharedParameter Group Eraser can be found from either the Revit Addin Coder sub-menu under the Tools or from Revit Addin Coder toolbar.
When the menu item or button is clicked, the SharedParameter Group Eraser window will show up:
If the OK button is clicked, the following SharedParameter Group Eraser code will be created and added into the chosen class:
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;
}
In terms of how to use these methods, please refer to previous posts for ideas and code examples.
SharedParameter Group Eraser of RevitAddinWizard can create all these in a second.
Recent Comments