使用ProjectInfo保存文件级别的额外信息

为了保存文件级别的额外信息,可以将共享数据添加到 ProjectInfo 对象中。
ProjectInfo 在一个 Document 中只有一个对象。 ProjectInfo BuildInCategory  成员是 OST_ProjectInformation.
复制代码
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  RevitApp  =  Autodesk.Revit.ApplicationServices;

using  System.Data;
using  System.Data.SQLite;
using  System.Data.Common;
using  System.Xml;
using  Autodesk.Revit.DB.Analysis;
using  Autodesk.Revit.DB.ExtensibleStorage;

namespace  RevitCodes
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    
// [Journaling(JournalingMode.NoCommandData)]
     public   class  cmdInfo : IExternalCommand
    {
        
public  Result Execute(ExternalCommandData cmdData,  ref   string  msg, ElementSet elements)
        {
            UIDocument uiDoc 
=  cmdData.Application.ActiveUIDocument;
            Selection selection 
=  uiDoc.Selection;
            Document document 
=  uiDoc.Document;


            XYZ xyzTmp 
=   new  XYZ( 10 20 30 );

            Transaction ts 
=   new  Transaction(uiDoc.Document,  " ts " );
            ts.Start();
            
/*
            
*/
            SchemaBuilder schemaBuilder 
=   new  SchemaBuilder(System.Guid.NewGuid());

            
//  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, xyzTmp, DisplayUnitType.DUT_METERS);  //  set the value for this entity 

            document.ProjectInformation.SetEntity(entity);


            
// Schema schema = null;
            Entity retrievedEntity  =  document.ProjectInformation.GetEntity(schema);

            XYZ retrievedData 
=  retrievedEntity.Get < XYZ > (schema.GetField( " WireSpliceLocation " ), DisplayUnitType.DUT_METERS);

            TaskDialog.Show(
" xyz " , retrievedData.X  +   " , "   +  retrievedData.Y  +   " , "   +  retrievedData.Z);

            ts.Commit();
            
return  Result.Succeeded;
        }
    }
}
复制代码
from: http://revit.5d6d.com/thread-1218-1-1.html

凡事以大气象去面对,优秀是一种习惯。

你可能感兴趣的:(Revit,schema,object,class)