Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Titleblock Schema and Store Data

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
danny.bentley
672 Views, 4 Replies

Titleblock Schema and Store Data

I’m working on a add-in which needs extra parameters.  I don’t want to clutter the user’s type parameters, therefore, I decided that I would use a “Schema and Store Data”.  I have everything working wonderfully, however, my add-in is only adding the Schema to one titleblock (current titleblock) instead of all the title blocks like a “type parameter”.   My code is included I’m using a FamilyInstance of the current title block.

 

        private string familyName = "";
        private FamilyInstance familyInstance;
        private FamilySymbol familySymbol;

        //Retrives the titleblock in the active view.  The method is used to compare the size of the titlblock.
        public void getTitleblockSize(Document doc)
        {
            FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id);
            collector.OfCategory(BuiltInCategory.OST_TitleBlocks).OfClass(typeof(FamilyInstance));

            //get the built in parameter parameters of titleblock in view. 
            foreach (FamilyInstance tbk in collector)
            {
               
                familyInstance = tbk;

                //titleblock information 
                FamilySymbol familySymbol = tbk.Symbol;
                string familyTypeName = familySymbol.Name;
                Family family = familySymbol.Family;
                familyName = family.Name;
            }
        }   


 class SchemaAndStoreData
    {
        //X values of cell
        private string titleblockName = "";
        private string cellSizeDistance_X = "";
        private string lowerGap_X = "";
        private string additionalEdge_X = "";
        private string fineTune_X = "";
        //Y values of cell 
        private string cellSizeDistance_Y = "";
        private string lowerGap_Y = "";
        private string additionalEdge_Y = "";
        private string fineTune_Y = "";

        //Size of grid in sheet
        private string cellGrid = "";

        // Create a data structure, attach it to a wall, populate it with data, and retrieve the data back from the wall
        public void CreateSchemeAndStoreData(FamilyInstance titleblock)
        {
            SchemaBuilder schemaBuilder = new SchemaBuilder(new Guid("1a68d420-96dd-44aa-a1de-000774a6104b"));
            schemaBuilder.SetReadAccessLevel(AccessLevel.Public); // allow anyone to read the object
            schemaBuilder.SetSchemaName("TitleBlockSettings");
            // create a field to store data 
            FieldBuilder fieldBuilder_titleblockName = schemaBuilder.AddSimpleField("titleblockName", typeof(string));

            FieldBuilder fieldBuilder_cellSizeDistance_X = schemaBuilder.AddSimpleField("cellSizeDistance_X", typeof(string));
            FieldBuilder fieldBuilder_lowerGap_X = schemaBuilder.AddSimpleField("lowerGap_X", typeof(string));
            FieldBuilder fieldBuilder_additionalEdge_X = schemaBuilder.AddSimpleField("additionalEdge_X", typeof(string));
            FieldBuilder fieldBuilder_fineTune_X = schemaBuilder.AddSimpleField("fineTune_X", typeof(string));

            FieldBuilder fieldBuilder_cellSizeDistance_Y = schemaBuilder.AddSimpleField("cellSizeDistance_Y", typeof(string));
            FieldBuilder fieldBuilder_lowerGap_Y = schemaBuilder.AddSimpleField("lowerGap_Y", typeof(string));
            FieldBuilder fieldBuilder_additionalEdge_Y = schemaBuilder.AddSimpleField("additionalEdge_Y", typeof(string));
            FieldBuilder fieldBuilder_fineTune_Y = schemaBuilder.AddSimpleField("fineTune_Y", typeof(string));

            FieldBuilder fieldBuilder_cellGrid = schemaBuilder.AddSimpleField("cellGrid", typeof(string));

            //fieldBuilder.SetDocumentation("store length of cell size in X direction.");

            Schema schema = schemaBuilder.Finish(); // register the Schema object
        }

        public void RetrieveData(FamilyInstance titleblock)
        {
            string[] objects = new string[9];
            
            Schema schema = Schema.Lookup(new Guid("1a68d420-96dd-44aa-a1de-000774a6104b"));
            // get the field from the schema
            Field field_titleblockName = schema.GetField("titleblockName");

            Field field_cellSizeDistance_X = schema.GetField("cellSizeDistance_X");
            Field field_lowerGap_X = schema.GetField("lowerGap_X");
            Field field_additionalEdge_X = schema.GetField("additionalEdge_X");
            Field field_fineTune_X = schema.GetField("fineTune_X");

            Field field_cellSizeDistance_Y = schema.GetField("cellSizeDistance_Y");
            Field field_lowerGap_Y = schema.GetField("lowerGap_Y");
            Field field_additionalEdge_Y = schema.GetField("additionalEdge_Y");
            Field field_fineTune_Y = schema.GetField("fineTune_Y");

            Field field_cellGrid = schema.GetField("cellGrid");

            // get the data back from the titleblock
            Entity retrievedEntity = titleblock.GetEntity(schema);
            string retrievedData_titleblockName = retrievedEntity.Get<string>(schema.GetField("titleblockName"));

            string retrievedData_cellSizeDistance_X = retrievedEntity.Get<string>(schema.GetField("cellSizeDistance_X"));
            string retrievedData_lowerGap_X = retrievedEntity.Get<string>(schema.GetField("lowerGap_X"));
            string retrievedData_additionalEdge_X = retrievedEntity.Get<string>(schema.GetField("additionalEdge_X"));
            string retrievedData_fineTune_X = retrievedEntity.Get<string>(schema.GetField("fineTune_X"));

            string retrievedData_cellSizeDistance_Y = retrievedEntity.Get<string>(schema.GetField("cellSizeDistance_Y"));
            string retrievedData_lowerGap_Y = retrievedEntity.Get<string>(schema.GetField("lowerGap_Y"));
            string retrievedData_additionalEdge_Y = retrievedEntity.Get<string>(schema.GetField("additionalEdge_Y"));
            string retrievedData_fineTune_Y = retrievedEntity.Get<string>(schema.GetField("fineTune_Y"));

            string retrievedData_cellGrid = retrievedEntity.Get<string>(schema.GetField("cellGrid"));

            titleblockName = retrievedData_titleblockName;

            cellSizeDistance_X = retrievedData_cellSizeDistance_X;
            lowerGap_X = retrievedData_lowerGap_X;
            additionalEdge_X = retrievedData_additionalEdge_X;
            fineTune_X = retrievedData_fineTune_X;

            cellSizeDistance_Y = retrievedData_cellSizeDistance_Y;
            lowerGap_Y = retrievedData_lowerGap_Y;
            additionalEdge_Y = retrievedData_additionalEdge_Y;
            fineTune_Y = retrievedData_fineTune_Y;

            cellGrid = retrievedData_cellGrid;
        }

        public void StoreData(FamilyInstance titleblock)
        {
            Schema schema = Schema.Lookup(new Guid("1a68d420-96dd-44aa-a1de-000774a6104b"));
            Entity entity = new Entity(schema);

            Field field_titleblockName = schema.GetField("titleblockName");

            Field field_cellSizeDistance_X = schema.GetField("cellSizeDistance_X");
            Field field_lowerGap_X = schema.GetField("lowerGap_X");
            Field field_additionalEdge_X = schema.GetField("additionalEdge_X");
            Field field_fineTune_X = schema.GetField("fineTune_X");

            Field field_cellSizeDistance_Y = schema.GetField("cellSizeDistance_Y");
            Field field_lowerGap_Y = schema.GetField("lowerGap_Y");
            Field field_additionalEdge_Y = schema.GetField("additionalEdge_Y");
            Field field_fineTune_Y = schema.GetField("fineTune_Y");

            Field field_cellGrid = schema.GetField("cellGrid");

            entity.Set<string>(field_titleblockName, titleblockName);

            entity.Set<string>(field_cellSizeDistance_X, cellSizeDistance_X);
            entity.Set<string>(field_lowerGap_X, lowerGap_X);
            entity.Set<string>(field_additionalEdge_X, additionalEdge_X);
            entity.Set<string>(field_fineTune_X, fineTune_X);

            entity.Set<string>(field_cellSizeDistance_Y, cellSizeDistance_Y);
            entity.Set<string>(field_lowerGap_Y, lowerGap_Y);
            entity.Set<string>(field_additionalEdge_Y, additionalEdge_Y);
            entity.Set<string>(field_fineTune_Y, fineTune_Y);

            entity.Set<string>(field_cellGrid, cellGrid);

            titleblock.SetEntity(entity); // store the entity in the element
        }

 

4 REPLIES 4
Message 2 of 5
dgurion
in reply to: danny.bentley

Since you stated that you're using the FamilyInstance, I think you are only adding the "schema" to one "instance" of the family:

 

 

public class FamilyInstance : Autodesk.Revit.DB.Instance
 
Summary:
This object represents a single instance of a family type, such as a single I beam.

 

 

As for a solution......... I don't know. I don't have enough experience with the Revit API.

Maybe try storing it in the element itself, not just the FamilyInstance?

 

 

 

I did a quick search through Jeremy Tammik's blog, and this is what I found:

 

http://thebuildingcoder.typepad.com/blog/2011/04/extensible-storage.html

 

And this PDF:

http://thebuildingcoder.typepad.com/files/cp6760-l_tammik_estorage.pdf

 

Particularly this section in the PDF:

 

FamilyStorage

The FamilyStorage sample shows how to create estorage in a family document and retrieve its data in a project containing an instance of that family.

It defines two external commands to implement this, AddDataToFamily and GetFamilyData.

 

But I'm not sure if that solves your issue.

Message 3 of 5

Dear Dgurion,

 

Thank you for your answer.

 

I believe you hit the nail on the head.

 

Dear Danny,

 

Thank you for your query.

 

The code you posted is much too voluminous for me poor biological organism to grasp at a glance, so I cannot tell what you are doing from that.

 

I fully agree with your verbal assessment, though.

 

What you describe is officially termed extensible storage.

 

It is definitely the method of choice to store Revit add-in data that neither the user nor Revit needs to see or care about.

 

Just as you and Dgurion both point out, you define a so-called Schema object to specify the extensible storage data structure, and then attach so-called Entity to each Revit element that you wish to equip with an instance of that data.

 

All this is described in full detail with numerous examples by The Building Coder topic group on extensible storage:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.23

 

Take a close look at that and all will become clear.

 

Good luck and have fun!

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 4 of 5

Sorry about the poor attempt to grab a snip of code from a large project.  I think that the instance is what need to be changed so that I can add it to the overall project titleblock.  I will read through the examples and maybe hunt through some of the Revit API Community to see if anyone has a example of changing all the titleblocks.  

 

Thank you.  I think this gives me a good place start my search.  

Message 5 of 5

In case anyone is interested my solution was to add the data schema to a FamilySymbol which added it to all my titleblocks. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community