最近工作的时候有同事提出来想做一个插件,在Revit的族中加入属性,但是加入的属性不存在于属性栏中。用插件来插入属性和读取属性。族不管载入到哪个项目里都可以进行查看。我就想着试用Revit的扩展属性更合适一些。
在网上查资料用了黑夜de骑士的代码做了一部分在项目的文档下读取族文档的操作就完成了此功能。
首先传入的doc,需要是族的doc。
using (Autodesk.Revit.DB.Transaction tran = new Autodesk.Revit.DB.Transaction(doc, "添加属性"))
{
tran.Start();
//全局存储单元
DataStorage myElement = DataStorage.Create(doc);
//【1】建立存储框架(可以理解为类似于定义了一个类)
SchemaBuilder builder = new SchemaBuilder(new Guid("5A515D8E-B61A-4F26-BDA6-9CD859CB8C62"));
// {7B393325-0635-439E-8CD8-DD5380C23B20}{5A515D8E-B61A-4F26-BDA6-9CD859CB8C62}
//【1-1】权限设置
builder.SetReadAccessLevel(AccessLevel.Public);
builder.SetWriteAccessLevel(AccessLevel.Public);
//【1-2】基本信息
builder.SetSchemaName("WallStorge");
builder.SetDocumentation("Storge Information in wall");
//【1-3】创建字段
FieldBuilder fieldBuilder = builder.AddSimpleField("stringValue", typeof(string));
//【1-4】得到创建的框架
Schema schema = builder.Finish();
//【2】创建实体(可以理解为实例化这个类)
Entity entity = new Entity(schema);
Field stringValue = schema.GetField("stringValue");
entity.Set<string>(stringValue, "字符串123456789");
myElement.SetEntity(entity);
tran.Commit();
//【3】获取这个对象的值
Schema currentScheme = Schema.Lookup(new Guid("5A515D8E-B61A-4F26-BDA6-9CD859CB8C62"));
//过滤找到这个单元
Element CurrentElement = new FilteredElementCollector(doc).OfClass(typeof(DataStorage)).FirstElement();
Entity currentEntity = CurrentElement.GetEntity(currentScheme);
string currentStringValue = currentEntity.Get<String>(currentScheme.GetField("stringValue"));
TaskDialog.Show("提示信息", $"当前文件中存储的字符串的信息为{currentStringValue}");
}
然后再项目文件中获取族,打开族文档,读取族文档的属性
FamilyInstance familyInstance = doc.GetElement(new ElementId(211791)) as FamilyInstance;
//获取族文档
Document familyDoc = doc.EditFamily(familyInstance.Symbol.Family);
Schema currentScheme = Schema.Lookup(new Guid("5A515D8E-B61A-4F26-BDA6-9CD859CB8C62"));
//过滤找到这个单元
Element CurrentElement = new FilteredElementCollector(familyDoc).OfClass(typeof(DataStorage)).FirstElement();
Entity currentEntity = CurrentElement.GetEntity(currentScheme);
string currentStringValue = currentEntity.Get<String>(currentScheme.GetField("stringValue"));
TaskDialog.Show("提示信息", $"当前文件中存储的字符串的信息为{currentStringValue}");
很简单的功能,记录一下。