应用程序名称:ExtensibleStorageUtility

Revit 平台:所有

Revit 版本:2014.0

首次发布于:2014.0

编程语言:C#

技能水平:中等

类别:元素

类型:ExternalCommand

主题:ExtensibleStorageUtility

概要:

该示例演示了如何使用ExtensibleStorageFilter查询文档中是否存在给定架构的存储,以及如何删除它。

相关类:

StorageUtility - 提供查询、删除和显示可扩展存储的所有方法。

项目文件:

DeleteStorage.cs - 包含删除所有存储的命令类。

QueryStorage.cs - 包含查询所有存储的命令类。

Utility.cs - 提供查询、删除和显示可扩展存储的所有方法。

描述:

这个示例使用SchemaExtensibleStorageFilter类来获取包含可扩展存储的所有元素,并按架构类型对它们进行组织和删除。通过使用ExtensibleStorageFilter类,可以使用架构类型名称和GUID等查询条件来筛选出具有可扩展存储的元素。然后,可以使用Utility类中提供的方法来删除这些存储对象。在这个示例中,所有的存储对象都被组织成各自的架构类型,用于实现更好的可视化和安排。

说明:

1. 启动Revit,并打开包含可扩展存储数据的文档。

2. 选择“Query Storage”命令。注意具有可扩展存储的元素的架构GUIDElementIds的输出。

3. 选择“Delete Storage”命令。再次选择“Query Storage”命令,并注意不再有存储存在。

源代码:

完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

DeleteStorage.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notify appears in all copies and
// that both that copyright notify and the limited warranty and
// restricted rights notify below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace Revit.SDK.Samples.ExtensibleStorageUtility.CS
{
   /// <summary>
   /// Deletes all extensible storage created by any application all active documents.
   /// This command will also report if there is no storage in the active document to delete.
   /// The document must be saved after the storage is deleted to commit the deletion.
   /// </summary>
   [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
   [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
   public class DeleteStorage : IExternalCommand
   {
      public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
      {
         Document document = commandData.Application.ActiveUIDocument.Document;

         if (!(StorageUtility.DoesAnyStorageExist(document)))
            message = "No storage in this document to delete.";
         else
         {
            Transaction tErase = new Transaction(document, "Erase EStorage");
            tErase.Start();
            IList<Schema> schemas = Schema.ListSchemas();
            foreach (Schema schema in schemas)
            {
               //Note-this will delete storage of this schema in *all* open documents.
               Schema.EraseSchemaAndAllEntities(schema, true);
            }
            tErase.Commit();
            message = "All storage was deleted.";
         }

         TaskDialog.Show("ExtensibleStorageUtility", message);
         return Result.Succeeded;

         
      }
   }

}

QueryStorage.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notify appears in all copies and
// that both that copyright notify and the limited warranty and
// restricted rights notify below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.ExtensibleStorage;



namespace Revit.SDK.Samples.ExtensibleStorageUtility.CS
{

   /// <summary>
   /// Checks to see if any extensible storage in the document exists and displays elements
   /// containing storage to the user.
   /// </summary>
   [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
   [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
   public class QueryStorage: IExternalCommand
   {
      public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
      {
         Document document = commandData.Application.ActiveUIDocument.Document;
         string storageElements = StorageUtility.GetElementsWithAllSchemas(document);
         TaskDialog.Show("ExtensibleStorageUtility", storageElements);

  
         return Result.Succeeded;
      }
   }

}

Utility.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notify appears in all copies and
// that both that copyright notify and the limited warranty and
// restricted rights notify below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.ExtensibleStorage;



namespace Revit.SDK.Samples.ExtensibleStorageUtility.CS
{
   public class StorageUtility
   {
      /// <summary>
      /// Returns true if any extensible storage exists in the document, false otherwise.
      /// </summary>
      public static bool DoesAnyStorageExist(Document doc)
      {
         IList<Schema> schemas = Schema.ListSchemas();
         if (schemas.Count == 0)
            return false;
         else
         {
            foreach (Schema schema in schemas)
            {
               List<ElementId> ids = ElementsWithStorage(doc, schema);
               if (ids.Count > 0)
                  return true;
            }
            return false;
         }
      }


      /// <summary>
      /// Returns a formatted string containing schema guids and element info for all elements
      /// containing extensible storage.
      /// </summary>
      public static string GetElementsWithAllSchemas(Document doc)
      {
         StringBuilder sBuilder = new StringBuilder();
         IList<Schema> schemas = Schema.ListSchemas();
         if (schemas.Count == 0)
            return "No schemas or storage.";
         else
         {
            foreach (Schema schema in schemas)
            {
               sBuilder.Append(StorageUtility.GetElementsWithSchema(doc, schema));
            }
            return sBuilder.ToString();
         }
      }

      /// <summary>
      /// Returns a formatted string containing a schema guid and element info for all elements
      /// containing extensible storage of a given schema.
      /// </summary>
      private static string GetElementsWithSchema(Document doc, Schema schema)
      {
         StringBuilder sBuilder = new StringBuilder();
         sBuilder.AppendLine("Schema: " + schema.GUID.ToString() + ", " + schema.SchemaName);
         List<ElementId> elementsofSchema = ElementsWithStorage(doc, schema);
         if (elementsofSchema.Count == 0)
            sBuilder.AppendLine("No elements.");
         else
         {
            foreach (ElementId id in elementsofSchema)
            {
               sBuilder.AppendLine(PrintElementInfo(id, doc));
            }
         }
         return sBuilder.ToString();
      }

      /// <summary>
      /// Returns a list of ElementIds that contain extensible storage of a given schema using
      /// the ExtensibleStorageFilter ElementQuickFilter.
      /// </summary>
      private static List<ElementId> ElementsWithStorage(Document doc, Schema schema)
      {
         List<ElementId> ids = new List<ElementId>();
         FilteredElementCollector collector = new FilteredElementCollector(doc);
         collector.WherePasses(new ExtensibleStorageFilter(schema.GUID));
         ids.AddRange(collector.ToElementIds());
         return ids;
      }

      /// <summary>
      /// Writes basic element info to a string.
      /// </summary>
      private static string PrintElementInfo(ElementId id, Document document)
      {
         Element element = document.GetElement(id);
         string retval = (element.Id.ToString() + ", " + element.Name + ", " + element.GetType().FullName);
         Debug.WriteLine(retval);
         return retval;
      }

   }
}