EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

萧闫子 发表于 2014-1-9 12:18:57

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.

龙龙..! 发表于 2014-2-20 15:03:20

顶!!!!!!!!!!!!!!!!!!!!!!!!!

页: [1]
查看完整版本: Parameter of Revit API – 28: Truly Erase Shared Parameter