Parameter of Revit API – 28: Truly Erase Shared Parameter
We have talked about three approaches to erase a shared parameter in the previous post. Two failed with exceptions and one seemingly succeeded but the result was not really what we wanted.In this post, let’s see how to work around the problem.
Through checking on the shared parameter external definition file, it is obvious that we can remove the entry from the text file directly. To achieve this goal, we need to parse the file format a bit, seek the shared parameter entry of interest, and modify the file programmatically.Here we go:…
RemoveSharedParameterFromFile(@"Volume41", @"c:\temp\sharedparameters.txt");
…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 == 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;
}The code works perfectly to remove the first found shared parameter definition with the given name from the external definition file. After it is run, we can verify from the Share Parameters user interface that the shared parameter will be gone.
However, due to the nature of shared parameters, i.e. duplicate names can exist, it may be necessary to improve the methods to compare parameter type and some other criteria to make sure the shared parameter of real concern is got rid of.Anyway, it proves to be a feasible way and can be made reliably enough.SharedParameter Eraser of RevitAddinWizardwill exercise the approach.
顶!!!!!!!!!!!!!!!!!!!!!!!!!
页:
[1]