更新
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.DB.ExtensibleStorage;
|
||||
|
||||
using System.IO;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.DB.ExtensibleStorage;
|
||||
|
||||
namespace ShrlAlgoToolkit.Revit.Assists;
|
||||
|
||||
/// <summary>
|
||||
/// 可扩展存储工具Schema框架->由字段Field组成,由Schema可以生成、构建Entity实体
|
||||
/// 可扩展存储工具Schema框架->由字段Field组成,是定义
|
||||
/// 由Schema可以生成、构建Entity(实体)对象,Entity是Schema的实例化对象,包含了Schema中定义的字段Field的值。
|
||||
/// 层级关系:Entity->(Schema/Field定义)/Field属性-值
|
||||
/// 元素阵列不会复制扩展数据
|
||||
/// </summary>
|
||||
@@ -38,7 +39,97 @@ namespace ShrlAlgoToolkit.Revit.Assists;
|
||||
/// </example>
|
||||
public static class ExtensibleStorageExtensions
|
||||
{
|
||||
private static Entity GetEntity(this Schema schema, Element element)
|
||||
#region Schema
|
||||
/// <summary>
|
||||
/// 创建 Schema
|
||||
/// </summary>
|
||||
/// <param name="schemaName"></param>
|
||||
/// <param name="accessLevel"></param>
|
||||
/// <param name="description"></param>
|
||||
/// <returns></returns>
|
||||
public static Schema CreateSchemaBuilder(string schemaName, AccessLevel accessLevel = AccessLevel.Public, string description = null)
|
||||
{
|
||||
if (schemaName is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(schemaName));
|
||||
}
|
||||
|
||||
SchemaBuilder builder = new(Guid.NewGuid());
|
||||
builder.SetReadAccessLevel(accessLevel);
|
||||
builder.SetWriteAccessLevel(accessLevel);
|
||||
builder.SetSchemaName(schemaName);
|
||||
description ??= schemaName;
|
||||
builder.SetDocumentation(description);
|
||||
#if USE_FORGETYPEID
|
||||
//roleFieldBuilder.SetSpec(SpecTypeId.HCLight);//设置字段值的单位,并不是所有类型都需要单位
|
||||
|
||||
#else
|
||||
//roleFieldBuilder.SetUnitType(UnitType.UT_Undefined);//设置字段值的单位,并不是所有类型都需要单位
|
||||
//roleFieldBuilder1.SetUnitType(UnitType.UT_Undefined);//设置字段值的单位,并不是所有类型都需要单位
|
||||
//roleFieldBuilder2.SetUnitType(UnitType.UT_Undefined);//设置字段值的单位,并不是所有类型都需要单位
|
||||
|
||||
#endif
|
||||
return builder?.Finish();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过字段名称获取Schema
|
||||
/// </summary>
|
||||
/// <param name="elm"></param>
|
||||
/// <param name="fieldName"></param>
|
||||
/// <returns></returns>
|
||||
public static Schema GetSchemaByFieldName(this Element elm, string fieldName)
|
||||
{
|
||||
if (elm is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(elm));
|
||||
}
|
||||
|
||||
if (fieldName is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(fieldName));
|
||||
}
|
||||
|
||||
var schemaGuids = elm.GetEntitySchemaGuids();
|
||||
|
||||
return schemaGuids.Select(Schema.Lookup).FirstOrDefault(f => f.GetField(fieldName) != null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过Schema名称获取Schema
|
||||
/// </summary>
|
||||
/// <param name="elm"></param>
|
||||
/// <param name="schemaName"></param>
|
||||
/// <returns></returns>
|
||||
public static Schema GetSchemaByName(this Element elm, string schemaName)
|
||||
{
|
||||
if (elm is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(elm));
|
||||
}
|
||||
|
||||
if (schemaName is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(schemaName));
|
||||
}
|
||||
|
||||
var schemaGuids = elm.GetEntitySchemaGuids();
|
||||
|
||||
return schemaGuids.Select(Schema.Lookup).FirstOrDefault(f => f.SchemaName == schemaName);
|
||||
}
|
||||
/// <summary>
|
||||
/// 通过guid名称获取Schema
|
||||
/// </summary>
|
||||
/// <param name="elm"></param>
|
||||
/// <param name="schemaName"></param>
|
||||
/// <returns></returns>
|
||||
public static Schema GetSchemaByGuid(Guid schemaGuid)
|
||||
{
|
||||
return Schema.Lookup(schemaGuid);
|
||||
}
|
||||
#endregion
|
||||
#region Entity
|
||||
public static Entity GetEntity(this Element element, Schema schema)
|
||||
{
|
||||
if (element is null)
|
||||
{
|
||||
@@ -55,6 +146,86 @@ public static class ExtensibleStorageExtensions
|
||||
return entity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建元素的扩展数据Entity
|
||||
/// </summary>
|
||||
/// <param name="element"></param>
|
||||
/// <param name="schema"></param>
|
||||
/// <returns></returns>
|
||||
public static Entity CreateEntity(this Element element, Schema schema)
|
||||
{
|
||||
Entity entity = new(schema);
|
||||
element.SetEntity(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除扩展数据Entity
|
||||
/// </summary>
|
||||
/// <param name="element">元素</param>
|
||||
/// <param name="schemaName">表名</param>
|
||||
public static void DeleteEntity(this Element element, string schemaName)
|
||||
{
|
||||
var schema = element.GetSchemaByName(schemaName);
|
||||
if (schema != null)
|
||||
{
|
||||
element.DeleteEntity(schema);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 从元素中读取Entity中存储的值
|
||||
/// </summary>
|
||||
/// <param name="element">存储数据的元素</param>
|
||||
/// <param name="schema">Existing schema</param>
|
||||
/// <param name="fieldName">The Field name</param>
|
||||
/// <typeparam name="T">schema中要存储的数据类型。该类型必须与schemabuilder中指定的数据类型相匹配。</typeparam>
|
||||
/// <returns>存储在元素中的数据。如果字段不存在或数据尚未保存,则返回空值</returns>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// document.ProjectInformation.LoadEntity<string<(schema, "schemaField")
|
||||
/// </code>
|
||||
/// </example>
|
||||
public static T LoadEntity<T>(this Element element, Schema schema, string fieldName)
|
||||
{
|
||||
var field = schema.GetField(fieldName);
|
||||
var entity = element.GetEntity(schema);
|
||||
return entity is null || field is null ? default : entity.Get<T>(field);
|
||||
}
|
||||
/// <summary>
|
||||
/// 在元素中存储数据。现有数据将被覆盖
|
||||
/// </summary>
|
||||
/// <param name="element">存储数据的元素</param>
|
||||
/// <param name="schema">Existing schema</param>
|
||||
/// <param name="data">Type of data</param>
|
||||
/// <param name="fieldName">The Field name</param>
|
||||
/// <typeparam name="T">The type of data to be stored in the schema. The type must match the type of data specified in the SchemaBuilder</typeparam>
|
||||
/// <returns>True if entity save succeeded</returns>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// document.ProjectInformation.SaveEntity(schema, "data", "schemaField")
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <summary>
|
||||
public static bool SaveEntity<T>(this Element element, Schema schema, T data, string fieldName)
|
||||
{
|
||||
var field = schema.GetField(fieldName);
|
||||
if (field is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var entity = element.GetEntity(schema);
|
||||
if (entity is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
entity.Set(field, data);
|
||||
element.SetEntity(entity);
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
#region Field
|
||||
/// <summary>
|
||||
/// 创建字典类型的字段
|
||||
/// </summary>
|
||||
@@ -158,111 +329,35 @@ public static class ExtensibleStorageExtensions
|
||||
result.SetDocumentation(description);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建元素扩展数据实体
|
||||
/// 设置可扩展参数的值:简单类型、集合、字典(这一步骤涉及对文档的修改,需在事务中进行)
|
||||
/// </summary>
|
||||
/// <param name="element"></param>
|
||||
/// <param name="schema"></param>
|
||||
/// <returns></returns>
|
||||
public static Entity CreateEntity(this Element element, Schema schema)
|
||||
/// <param name="element">元素</param>
|
||||
/// <param name="fieldName">字段</param>
|
||||
/// <param name="value">字段值</param>
|
||||
public static void SetFieldValue<T>(this Element element, string fieldName, T value)
|
||||
{
|
||||
Entity entity = new(schema);
|
||||
Schema schema = element.GetSchemaByFieldName(fieldName);
|
||||
var entity = element.GetEntity(schema);
|
||||
Field field = schema.GetField(fieldName);
|
||||
entity.Set(field, value);
|
||||
element.SetEntity(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建Schema
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <returns></returns>
|
||||
public static Schema CreateSchema(this SchemaBuilder builder)
|
||||
{
|
||||
return builder?.Finish();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建参数的表
|
||||
/// </summary>
|
||||
/// <param name="schemaName"></param>
|
||||
/// <param name="accessLevel"></param>
|
||||
/// <param name="description"></param>
|
||||
/// <returns></returns>
|
||||
public static SchemaBuilder CreateSchemaBuilder(string schemaName, AccessLevel accessLevel = AccessLevel.Public, string description = null)
|
||||
{
|
||||
if (schemaName is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(schemaName));
|
||||
}
|
||||
|
||||
SchemaBuilder builder = new(Guid.NewGuid());
|
||||
builder.SetReadAccessLevel(accessLevel);
|
||||
builder.SetWriteAccessLevel(accessLevel);
|
||||
builder.SetSchemaName(schemaName);
|
||||
description ??= schemaName;
|
||||
builder.SetDocumentation(description);
|
||||
#if USE_FORGETYPEID
|
||||
//roleFieldBuilder.SetSpec(SpecTypeId.HCLight);//设置字段值的单位,并不是所有类型都需要单位
|
||||
|
||||
#else
|
||||
//roleFieldBuilder.SetUnitType(UnitType.UT_Undefined);//设置字段值的单位,并不是所有类型都需要单位
|
||||
//roleFieldBuilder1.SetUnitType(UnitType.UT_Undefined);//设置字段值的单位,并不是所有类型都需要单位
|
||||
//roleFieldBuilder2.SetUnitType(UnitType.UT_Undefined);//设置字段值的单位,并不是所有类型都需要单位
|
||||
|
||||
#endif
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除实体
|
||||
/// 设置可扩展参数的值:简单类型、集合、字典(这一步骤涉及对文档的修改,需在事务中进行)
|
||||
/// </summary>
|
||||
/// <param name="element">元素</param>
|
||||
/// <param name="fieldName">字段名</param>
|
||||
public static void DeleteEntityByFieldName(this Element element, string fieldName)
|
||||
/// <param name="schema"></param>
|
||||
/// <param name="fieldName">字段</param>
|
||||
/// <param name="value">字段值</param>
|
||||
public static void SetFieldValue<T>(this Element element, Schema schema, string fieldName, T value)
|
||||
{
|
||||
var schema = element.GetSchemaByFieldName(fieldName);
|
||||
if (schema != null)
|
||||
{
|
||||
element.DeleteEntity(schema);
|
||||
}
|
||||
var entity = element.GetEntity(schema);
|
||||
Field field = schema.GetField(fieldName);
|
||||
entity.Set(field, value);
|
||||
element.SetEntity(entity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除实体
|
||||
/// </summary>
|
||||
/// <param name="element">元素</param>
|
||||
/// <param name="schemaName">表名</param>
|
||||
public static void DeleteEntityBySchemaName(this Element element, string schemaName)
|
||||
{
|
||||
var schema = element.GetSchemaBySchemaName(schemaName);
|
||||
if (schema != null)
|
||||
{
|
||||
element.DeleteEntity(schema);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件级数据存储
|
||||
/// </summary>
|
||||
/// <param name="doc"></param>
|
||||
/// <param name="schemaName"></param>
|
||||
/// <param name="fieldName"></param>
|
||||
/// <param name="fileName"></param>
|
||||
public static void GetDateStorage(Document doc, string schemaName, string fieldName, string fileName)
|
||||
{
|
||||
DataStorage storage = new FilteredElementCollector(doc).OfClass(typeof(DataStorage)).Cast<DataStorage>().First();
|
||||
Schema sch = Schema.ListSchemas().First(o => o.SchemaName == schemaName);
|
||||
Entity ent = storage.GetEntity(sch);
|
||||
byte[] bytes = ent.Get<byte[]>(sch.GetField(fieldName)).ToArray();
|
||||
using (FileStream fs = new(fileName, FileMode.Create, FileAccess.Write))
|
||||
{
|
||||
fs.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
MessageBox.Show("文件提取完毕!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取字段参数值
|
||||
/// </summary>
|
||||
@@ -348,105 +443,30 @@ public static class ExtensibleStorageExtensions
|
||||
|
||||
return storages;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DataStorage
|
||||
/// <summary>
|
||||
/// 通过字段名称获取Schema
|
||||
/// 获取文件级数据存储
|
||||
/// </summary>
|
||||
/// <param name="elm"></param>
|
||||
/// <param name="fieldName"></param>
|
||||
/// <returns></returns>
|
||||
public static Schema GetSchemaByFieldName(this Element elm, string fieldName)
|
||||
{
|
||||
if (elm is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(elm));
|
||||
}
|
||||
|
||||
if (fieldName is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(fieldName));
|
||||
}
|
||||
|
||||
var schemaGuids = elm.GetEntitySchemaGuids();
|
||||
|
||||
return schemaGuids.Select(Schema.Lookup).FirstOrDefault(f => f.GetField(fieldName) != null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过Schema名称获取Schema
|
||||
/// </summary>
|
||||
/// <param name="elm"></param>
|
||||
/// <param name="doc"></param>
|
||||
/// <param name="schemaName"></param>
|
||||
/// <returns></returns>
|
||||
public static Schema GetSchemaBySchemaName(this Element elm, string schemaName)
|
||||
/// <param name="fieldName"></param>
|
||||
/// <param name="fileName"></param>
|
||||
public static void GetDateStorage(Document doc, string schemaName, string fieldName, string fileName)
|
||||
{
|
||||
if (elm is null)
|
||||
DataStorage storage = new FilteredElementCollector(doc).OfClass(typeof(DataStorage)).Cast<DataStorage>().First();
|
||||
Schema sch = Schema.ListSchemas().First(o => o.SchemaName == schemaName);
|
||||
Entity ent = storage.GetEntity(sch);
|
||||
byte[] bytes = ent.Get<byte[]>(sch.GetField(fieldName)).ToArray();
|
||||
using (FileStream fs = new(fileName, FileMode.Create, FileAccess.Write))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(elm));
|
||||
fs.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
if (schemaName is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(schemaName));
|
||||
}
|
||||
|
||||
var schemaGuids = elm.GetEntitySchemaGuids();
|
||||
|
||||
return schemaGuids.Select(Schema.Lookup).FirstOrDefault(f => f.SchemaName == schemaName);
|
||||
MessageBox.Show("文件提取完毕!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从元素中读取schema中存储的值
|
||||
/// </summary>
|
||||
/// <param name="element">存储数据的元素</param>
|
||||
/// <param name="schema">Existing schema</param>
|
||||
/// <param name="fieldName">The Field name</param>
|
||||
/// <typeparam name="T">schema中要存储的数据类型。该类型必须与schemabuilder中指定的数据类型相匹配。</typeparam>
|
||||
/// <returns>存储在元素中的数据。如果字段不存在或数据尚未保存,则返回空值</returns>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// document.ProjectInformation.LoadEntity<string<(schema, "schemaField")
|
||||
/// </code>
|
||||
/// </example>
|
||||
public static T LoadEntity<T>(this Element element, Schema schema, string fieldName)
|
||||
{
|
||||
var field = schema.GetField(fieldName);
|
||||
var entity = schema.GetEntity(element);
|
||||
return entity is null || field is null ? default : entity.Get<T>(field);
|
||||
}
|
||||
/// <summary>
|
||||
/// 在元素中存储数据。现有数据将被覆盖
|
||||
/// </summary>
|
||||
/// <param name="element">存储数据的元素</param>
|
||||
/// <param name="schema">Existing schema</param>
|
||||
/// <param name="data">Type of data</param>
|
||||
/// <param name="fieldName">The Field name</param>
|
||||
/// <typeparam name="T">The type of data to be stored in the schema. The type must match the type of data specified in the SchemaBuilder</typeparam>
|
||||
/// <returns>True if entity save succeeded</returns>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// document.ProjectInformation.SaveEntity(schema, "data", "schemaField")
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <summary>
|
||||
public static bool SaveEntity<T>(this Element element, Schema schema, T data, string fieldName)
|
||||
{
|
||||
var field = schema.GetField(fieldName);
|
||||
if (field is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var entity = schema.GetEntity(element);
|
||||
if (entity is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
entity.Set(field, data);
|
||||
element.SetEntity(entity);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置文件级别数据存储
|
||||
@@ -488,34 +508,7 @@ public static class ExtensibleStorageExtensions
|
||||
MessageBox.Show("文件存储成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
tran.Commit();
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 设置可扩展参数的值:简单类型、集合、字典(这一步骤涉及对文档的修改,需在事务中进行)
|
||||
/// </summary>
|
||||
/// <param name="element">元素</param>
|
||||
/// <param name="fieldName">字段</param>
|
||||
/// <param name="value">字段值</param>
|
||||
public static void SetFieldValue<T>(this Element element, string fieldName, T value)
|
||||
{
|
||||
Schema schema = element.GetSchemaByFieldName(fieldName);
|
||||
var entity = element.GetEntity(schema);
|
||||
Field field = schema.GetField(fieldName);
|
||||
entity.Set(field, value);
|
||||
element.SetEntity(entity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置可扩展参数的值:简单类型、集合、字典(这一步骤涉及对文档的修改,需在事务中进行)
|
||||
/// </summary>
|
||||
/// <param name="element">元素</param>
|
||||
/// <param name="schema"></param>
|
||||
/// <param name="fieldName">字段</param>
|
||||
/// <param name="value">字段值</param>
|
||||
public static void SetFieldValue<T>(this Element element, Schema schema, string fieldName, T value)
|
||||
{
|
||||
var entity = element.GetEntity(schema);
|
||||
Field field = schema.GetField(fieldName);
|
||||
entity.Set(field, value);
|
||||
element.SetEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user