这里是把数据附加到实体,参看附加到项目文件ProjectInfo示例
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinForm = System.Windows.Forms;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.ExtensibleStorage;
using System.Xml;
namespace RevitCodes
{
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdStore : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Reference refWall = app.ActiveUIDocument.Selection.PickObject(ObjectType.Element, " 选择墙 " );
Wall wall = doc.GetElement(refWall) as Wall;
XYZ xyz = app.ActiveUIDocument.Selection.PickPoint( " 选择一个点 " );
StoreDataInWall(wall, xyz);
return Result.Succeeded;
}
public void StoreDataInWall(Wall wall, XYZ dataToStore)
{
Transaction createSchemaAndStoreData = new Transaction(wall.Document, " tCreateAndStore " );
createSchemaAndStoreData.Start();
SchemaBuilder schemaBuilder =
new SchemaBuilder( new Guid( " 720080CB-DA99-40DC-9415-E53F280AA1F0 " ));
// allow anyone to read the object
schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
// restrict writing to this vendor only
schemaBuilder.SetWriteAccessLevel(AccessLevel.Vendor);
// required because of restricted write-access
schemaBuilder.SetVendorId( " ADSK " );
// create a field to store an XYZ
FieldBuilder fieldBuilder =
schemaBuilder.AddSimpleField( " WireSpliceLocation " , typeof (XYZ));
fieldBuilder.SetUnitType(UnitType.UT_Length);
fieldBuilder.SetDocumentation( " A stored location value representing a wiring splice in a wall. " );
schemaBuilder.SetSchemaName( " WireSpliceLocation " );
Schema schema = schemaBuilder.Finish(); // register the Schema object
// create an entity (object) for this schema (class)
Entity entity = new Entity(schema);
// get the field from the schema
Field fieldSpliceLocation = schema.GetField( " WireSpliceLocation " );
entity.Set < XYZ > (fieldSpliceLocation, dataToStore, DisplayUnitType.DUT_METERS); // set the value for this entity
wall.SetEntity(entity); // store the entity in the element
// get the data back from the wall
Entity retrievedEntity = wall.GetEntity(schema);
XYZ retrievedData =
retrievedEntity.Get < XYZ > (schema.GetField( " WireSpliceLocation " ), DisplayUnitType.DUT_METERS);
createSchemaAndStoreData.Commit();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinForm = System.Windows.Forms;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.ExtensibleStorage;
using System.Xml;
namespace RevitCodes
{
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdStore : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Reference refWall = app.ActiveUIDocument.Selection.PickObject(ObjectType.Element, " 选择墙 " );
Wall wall = doc.GetElement(refWall) as Wall;
XYZ xyz = app.ActiveUIDocument.Selection.PickPoint( " 选择一个点 " );
StoreDataInWall(wall, xyz);
return Result.Succeeded;
}
public void StoreDataInWall(Wall wall, XYZ dataToStore)
{
Transaction createSchemaAndStoreData = new Transaction(wall.Document, " tCreateAndStore " );
createSchemaAndStoreData.Start();
SchemaBuilder schemaBuilder =
new SchemaBuilder( new Guid( " 720080CB-DA99-40DC-9415-E53F280AA1F0 " ));
// allow anyone to read the object
schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
// restrict writing to this vendor only
schemaBuilder.SetWriteAccessLevel(AccessLevel.Vendor);
// required because of restricted write-access
schemaBuilder.SetVendorId( " ADSK " );
// create a field to store an XYZ
FieldBuilder fieldBuilder =
schemaBuilder.AddSimpleField( " WireSpliceLocation " , typeof (XYZ));
fieldBuilder.SetUnitType(UnitType.UT_Length);
fieldBuilder.SetDocumentation( " A stored location value representing a wiring splice in a wall. " );
schemaBuilder.SetSchemaName( " WireSpliceLocation " );
Schema schema = schemaBuilder.Finish(); // register the Schema object
// create an entity (object) for this schema (class)
Entity entity = new Entity(schema);
// get the field from the schema
Field fieldSpliceLocation = schema.GetField( " WireSpliceLocation " );
entity.Set < XYZ > (fieldSpliceLocation, dataToStore, DisplayUnitType.DUT_METERS); // set the value for this entity
wall.SetEntity(entity); // store the entity in the element
// get the data back from the wall
Entity retrievedEntity = wall.GetEntity(schema);
XYZ retrievedData =
retrievedEntity.Get < XYZ > (schema.GetField( " WireSpliceLocation " ), DisplayUnitType.DUT_METERS);
createSchemaAndStoreData.Commit();
}
}
}