Revit二次開發——“Family”存儲擴展數據

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using System.Windows.Forms;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace FamilyTest
{
    [TransactionAttribute(TransactionMode.Manual)]
    class Tools : IExternalCommand
    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;
 
            using (Transaction tran = new Transaction(doc, "FamilyTest"))
            {
                tran.Start();
 
                Family family = doc.OwnerFamily;

                SchemaBuilder bulder = new SchemaBuilder(new Guid("11111111-aaaa-2222-bbbb-333333333333"));

                bulder.SetReadAccessLevel(AccessLevel.Public);

                bulder.SetWriteAccessLevel(AccessLevel.Public);

                bulder.SetSchemaName("FamilyTest");

                bulder.SetDocumentation("Data store for socket related info in a FamilyTest");

                FieldBuilder fieldbuilder = bulder.AddSimpleField("FamilyTest", typeof(string));
                Schema schema = bulder.Finish();
                Entity ent = new Entity(schema);
                Field socketNumber = schema.GetField("FamilyTest");
                ent.Set(socketNumber, "Family______________Test");
                family.SetEntity(ent);
                MessageBox.Show("保存成功");
                tran.Commit();


            }
 
            return Result.Succeeded;
        }
    }
}

以上是存儲好了一個字段,以下是測試讀取該字段,把該文件加載到項目文件或者族文件字段都是存在的。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using System.Windows.Forms;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace FamilyTest
{
    [TransactionAttribute(TransactionMode.Manual)]
    class Tool2 : IExternalCommand
    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;
            using (Transaction tran = new Transaction(doc, "FamilyTest"))
            {
                tran.Start();
                Reference reference = null;
                reference = uidoc.Selection.PickObject(ObjectType.Element, "請拾取一個族實例");

                Element element = doc.GetElement(reference);

                if (element is FamilyInstance)
                {
                    FamilyInstance familyinstance = element as FamilyInstance;
                    Family family = familyinstance.Symbol.Family;

                    IList<Guid> listGuid = family.GetEntitySchemaGuids();

                    Schema sa = Schema.Lookup(listGuid[0]);

                    Entity entity = family.GetEntity(sa);

                    string str = entity.Get<string>(sa.GetField("FamilyTest"));

                    MessageBox.Show(str);

                }
 
                tran.Commit();

            }
 
            return Result.Succeeded;
        }
    }
}

技術羣:1090519856

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章