We talked about erasing Shared Parameters before. Now let’s see how SharedParameter Eraser of RevitAddinWizard can assist us to do the same work. The SharedParameter 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 Eraser window will show up:
If the OK button is clicked, the following SharedParameter Eraser code will be created and added into the chosen class:
public static bool RemoveSharedParameter(RvtApplication app, string paramName)
{
if ( !string.IsNullOrEmpty(app.SharedParametersFilename))
{
return RemoveSharedParameterFromFile(app.SharedParametersFilename, paramName);
}
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.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 Eraser of RevitAddinWizard can create all these in a second.
Recent Comments