转载请复制以下信息:
原文链接: http://blog.csdn.net/joexiongjin/article/details/7776552
作者: 叶雄进 , Autodesk ADN
从Revit2012开始引入了扩展存储这个新技术,就是可以把一些数据存储到Revit中的某一个对象上。扩展数据只能通过编程的方式添加到对象上,通过Revit软件的界面无法添加。这是编程的一个大优势。你可以把任何的数据保存在Revit中的任何一个对象上。你自己的扩展数据始终跟着这个Rvt文件走,不会丢失。 而且存储的数据可以设置访问权限。对于保密数据可以只能是你自己的程序读取,或只有具有特殊的开发者代号的程序才能访问。对于数据的保密性非常有帮助。
创建共享参数步骤:
创建并命名新的数据架构(Schema)
设置数据架构(Schema)读写访问权限
向数据架构增加字段(一个或多个)
基于数据架构创建一个数据对象(Entity)
给数据对象Entity中的字段赋值
关联数据对象Entity和Revit模型中的对象
下面时段代码演示:
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uiDoc = commandData.Application.ActiveUIDocument; Document doc = uiDoc.Document; // Create transaction for working with schema Transaction trans = new Transaction(doc, "Extensible Storage"); trans.Start(); // Select a wall element Wall wall = null; try { Reference r = uiDoc.Selection.PickObject(ObjectType.Element, new WallSelectionFilter()); wall = doc.GetElement(r) as Wall; } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { message = "Nothing selected; please select a wall to attach extensible data to."; return Result.Failed; } Debug.Assert(null != wall, "expected a wall to be selected"); if (null == wall) { message = "Please select a wall to attach extensible data to."; return Result.Failed; } // Create a schema builder SchemaBuilder builder = new SchemaBuilder(_guid); // Set read and write access levels builder.SetReadAccessLevel(AccessLevel.Public); builder.SetWriteAccessLevel(AccessLevel.Public); // Note: if this was set as vendor or application access, // we would have been additionally required to use SetVendorId // Set name to this schema builder builder.SetSchemaName("WallSocketLocation"); builder.SetDocumentation("Data store for socket related info in a wall"); // Create field1 FieldBuilder fieldBuilder1 = builder.AddSimpleField("SocketLocation", typeof(XYZ)); // Set unit type fieldBuilder1.SetUnitType(UnitType.UT_Length); // Add documentation (optional) // Create field2 FieldBuilder fieldBuilder2 = builder.AddSimpleField("SocketNumber", typeof(string)); //fieldBuilder2.SetUnitType(UnitType.UT_Custom); // Register the schema object Schema schema = builder.Finish(); // Create an entity (object) for this schema (class) Entity ent = new Entity(schema); Field socketLocation = schema.GetField("SocketLocation"); ent.Set<XYZ>(socketLocation, new XYZ(2, 0, 0), DisplayUnitType.DUT_METERS); Field socketNumber = schema.GetField("SocketNumber"); ent.Set<string>(socketNumber, "200"); wall.SetEntity(ent); // Now create another entity (object) for this schema (class) Entity ent2 = new Entity(schema); Field socketNumber1 = schema.GetField("SocketNumber"); ent2.Set<String>(socketNumber1, "400"); wall.SetEntity(ent2); // Note: this will replace the previous entity on the wall // List all schemas in the document string s = string.Empty; IList<Schema> schemas = Schema.ListSchemas(); foreach (Schema sch in schemas) { s += "\r\nSchema Name: " + sch.SchemaName; } TaskDialog.Show("Schema details", s); // List all Fields for our schema s = string.Empty; Schema ourSchema = Schema.Lookup(_guid); IList<Field> fields = ourSchema.ListFields(); foreach (Field fld in fields) { s += "\r\nField Name: " + fld.FieldName; } TaskDialog.Show("Field details", s); // Extract the value for the field we created Entity wallSchemaEnt = wall.GetEntity(Schema.Lookup(_guid)); XYZ wallSocketPos = wallSchemaEnt.Get<XYZ>( Schema.Lookup(_guid).GetField("SocketLocation"), DisplayUnitType.DUT_METERS); s = "SocketLocation: " + Format.PointString(wallSocketPos); string wallSocketNumber = wallSchemaEnt.Get<String>( Schema.Lookup(_guid).GetField("SocketNumber")); s += "\r\nSocketNumber: " + wallSocketNumber; TaskDialog.Show("Field values", s); trans.Commit(); return Result.Succeeded; }