BuildingCoder.JtNamedGuidStorage.Get C# (CSharp) Метод

Get() публичный статический Метод

Retrieve an existing named Guid in the specified Revit document or optionally create and return a new one if it does not yet exist.
public static Get ( Document doc, string name, System.Guid &guid, bool create = true ) : bool
doc Document
name string
guid System.Guid
create bool
Результат bool
        public static bool Get(
            Document doc,
            string name,
            out Guid guid,
            bool create = true)
        {
            bool rc = false;

              guid = Guid.Empty;

              // Retrieve a DataStorage element with our
              // extensible storage entity attached to it
              // and the specified element name. Only zero
              // or one should exist.

              ExtensibleStorageFilter f
            = new ExtensibleStorageFilter(
              JtNamedGuidStorageSchema.SchemaGuid );

              DataStorage dataStorage
            = new FilteredElementCollector( doc )
              .OfClass( typeof( DataStorage ) )
              .WherePasses( f )
              .Where<Element>( e => name.Equals( e.Name ) )
              .FirstOrDefault<Element>() as DataStorage;

              if( dataStorage == null )
              {
            if( create )
            {
              using( Transaction t = new Transaction(
            doc, "Create named Guid storage" ) )
              {
            t.Start();

            // Create named data storage element

            dataStorage = DataStorage.Create( doc );
            dataStorage.Name = name;

            // Create entity to store the Guid data

            Entity entity = new Entity(
              JtNamedGuidStorageSchema.GetSchema() );

            entity.Set( "Guid", guid = Guid.NewGuid() );

            // Set entity to the data storage element

            dataStorage.SetEntity( entity );

            t.Commit();

            rc = true;
              }
            }
              }
              else
              {
            // Retrieve entity from the data storage element.

            Entity entity = dataStorage.GetEntity(
              JtNamedGuidStorageSchema.GetSchema( false ) );

            Debug.Assert( entity.IsValid(),
              "expected a valid extensible storage entity" );

            if( entity.IsValid() )
            {
              guid = entity.Get<Guid>( "Guid" );

              rc = true;
            }
              }
              return rc;
        }

Usage Example

        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Document      doc   = uidoc.Document;
            Result        rslt  = Result.Failed;

            string name = "TrackChanges_project_identifier";
            Guid   named_guid;

            bool rc = JtNamedGuidStorage.Get(doc,
                                             name, out named_guid, false);

            if (rc)
            {
                Util.InfoMsg(string.Format(
                                 "This document already has a project "
                                 + "identifier: {0} = {1}",
                                 name, named_guid.ToString()));

                rslt = Result.Succeeded;
            }
            else
            {
                rc = JtNamedGuidStorage.Get(doc,
                                            name, out named_guid, true);

                if (rc)
                {
                    Util.InfoMsg(string.Format(
                                     "Created a new project identifier "
                                     + "for this document: {0} = {1}",
                                     name, named_guid.ToString()));

                    rslt = Result.Succeeded;
                }
                else
                {
                    Util.ErrorMsg("Something went wrong");
                }
            }
            return(rslt);
        }
JtNamedGuidStorage