Get Element and set parameter value inside RoomTag Family using C# in the Family Editor

revitdeveloper
Contributor
Contributor

Get Element and set parameter value inside RoomTag Family using C# in the Family Editor

revitdeveloper
Contributor
Contributor

The idea is to open a "roomtag family", change the "lable texts" parameter "Textsize" and reload the "roomtag family" into an active Revit project all automatically.

 

The problem I am facing is that I cant access the "lable text family" wich is inside the "roomtag family".

 

What already works:

1.) Get the "roomtag family"

2.) Open the "roomtag family" document

3.) Get the all parameters from the "roomtag family" (only to test and see what the result is)

 

6.) Load the "roomtag family" back into the active Revit project

7.) Closing the "roomtag family" document

 

 

What does not work:

4.) Get the "lable text family" that is inside the "roomtag family"

5.) Change the parameter value of "Textsize" parameter

 

 

 

Here you can see screenshots to better understand what I am trying to do:

 

I am using the Revit sample project file:  "rac_basic_sample_project.rvt"

Here is the link: 

https://help.autodesk.com/view/RVT/2023/ENU/?guid=GUID-61EF2F22-3A1F-4317-B925-1E85F138BE88

 

Navigate to the "Level 1" floor plan. Now you can see the "room tag" instance shown in the screenshot.

revitdeveloper_0-1699003200413.png

 

Here you see that I was able to get the "roomtag family parameters" from steps 1-3 described above

revitdeveloper_1-1699003220921.png

 

 

Now click "Edit Family" and select a lable text.In the screenshot I highlighted the parameter i need to get to:

revitdeveloper_2-1699003281961.png

 

 

 

Now for steps 4-5 I have tried to apply a filter to get the "textlable family", I have also already tried to get the specific element with its id, I have tried to open another familydoc inside the already opened familydoc. As well as have tried multiple other things that did not work, even ChatGPT was not helpful.

 

 

This is the full code:

 

 

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{

    // Get the UIApplication object
    UIApplication uiApp = commandData.Application;
    // Get the current document
    Document doc = uiApp.ActiveUIDocument.Document;

    string familyName = "M_Room Tag";
    double textSize = 10;
    string typeName = "M_Room Tag_10";


    // 1.) Get the roomtag family

    // Create a new FilteredElementCollector for the current document
    FilteredElementCollector collector = new FilteredElementCollector(doc);

    // Filter the elements in the collector to get only Family elements
    ICollection<Element> families = collector.OfClass(typeof(Family)).ToElements();


    // Declare a variable to store the found family
    Family family = null;
    family = families.OfType<Family>().FirstOrDefault(fam => fam.Name == familyName);



    // 2.) Open the roomtag family document 
    Document familyDoc = doc.EditFamily(family);
    FamilyManager mgr = familyDoc.FamilyManager;



    FamilyType familyType;
    using (Transaction trans = new Transaction(familyDoc, "Create New Type"))
    {
        trans.Start();



        // x.) Create a new family type
        // familyType = mgr.NewType(typeName);



        // 3.) Get the all parameters from roomtag family (only to test and see what the result is)
        IList <FamilyParameter> familyParameters = mgr.GetParameters();

        string printRoomTagParameters = "";
        foreach(FamilyParameter familyParameter in familyParameters)
        {
            printRoomTagParameters += familyParameter.Definition.Name.ToString() + "\n";
        }
        TaskDialog.Show("parametersPrint", printRoomTagParameters);




        // 4.) Get the lable text somehow
        ElementCategoryFilter filterRT = new ElementCategoryFilter(BuiltInCategory.OST_RoomTags);
        FilteredElementCollector collectorRT = new FilteredElementCollector(familyDoc);
        var roomTags = collectorRT.WherePasses(filterRT).WhereElementIsNotElementType().ToElements();

        string roomTagss = "";
        foreach (var roomTag in roomTags)
        {
            roomTagss += roomTag.Name.ToString() + "\n";
        }
        TaskDialog.Show("roomTagss", roomTagss);




        // 5.) Change the parameter value of "Textsize" parameter



        trans.Commit();
    }

    // 6.) Load the roomtag family back into the active revit project
    familyDoc.LoadFamily(doc, new FamilyLoadOptions());
    // 7.) Finally, closing the document (False = without saving)
    familyDoc.Close(false); 

    return Result.Succeeded;
}

 

 

 

 

 

 

Result of step 4.)

 

revitdeveloper_0-1699007700687.png

 

 

 

 

I have already looked at the following links:

https://www.revitapidocs.com/2016/56e636ee-5008-0ee5-9d6c-5f622dedfbcb.htm

https://thebuildingcoder.typepad.com/blog/2009/11/family-parameter-value.html

https://www.revitapidocs.com/2018/7fc40346-6188-66ff-4c00-bd4360e70c6f.htm

https://www.revitapidocs.com/2018/86e30f63-4894-aed9-c6df-0074cdfa89a7.htm

https://help.autodesk.com/view/RVT/2023/ENU/?guid=Revit_API_Revit_API_Developers_Guide_Revit_Geometr...

 

 

I know that I could just create a new "roomtag family type" with the desired textsize, but we want to develop a more automated/easier way to create famiy types as well as perform different actions on it such as changing the text size.

 

 

It would be great if someone has a solution or can point me in the right direction. I am also wondering if this is even possible?

 

0 Likes
Reply
Accepted solutions (1)
393 Views
2 Replies
Replies (2)

Mohamed_Arshad
Advisor
Advisor
Accepted solution

Hi @revitdeveloper 

I think you're facing problem in text size changing

 

1. The below code will explain you how to change the Text Type, for example how to change the 2.5mm Opaque type to 5mm type.

 

Reference Image:

Mohamed_Arshad_0-1699013306753.png

 

 

Reference Code

 

            //Filter labels
            var labelList = new FilteredElementCollector(doc)
                .OfClass(typeof(TextElement))
                .ToList();

            List<TextElement> textElements = new List<TextElement>();

            //Cast Label to its types
            if(labelList.Count>0)
            {
                textElements.AddRange(labelList.Cast<TextElement>());
            }


            //Filter New Text Size (Filter 5mm Text)
            TextElementType textType = new FilteredElementCollector(doc)
                .OfClass(typeof(TextElementType))
                .Where(x => x.Name.Equals("5mm"))
                .Cast<TextElementType>()
                .First();

            //Changing the Text Type Sizes from 2.5mm to 5mm
            using (Transaction changeLableText=new Transaction(doc,"Change Label Text"))
            {
                changeLableText.Start();

                foreach(TextElement textNote in labelList)
                {
                    textNote.ChangeTypeId(textType.Id);
                }

                changeLableText.Commit();
            }

 

 

2. Then next below code will explain you how to change the Text Size and how to rename the Existing Type name.

 

Reference Image:

Mohamed_Arshad_0-1699014028452.png

 

Reference Code:

 //Filter New Text Size (Filter 5mm Text)
            TextElementType textType = new FilteredElementCollector(doc)
                .OfClass(typeof(TextElementType))
                .Where(x => x.Name.Equals("2.5mm Transparent"))
                .Cast<TextElementType>()
                .First();

            //Changing the Text Size
            using (Transaction editTextSize=new Transaction(doc,"Edit the Text Size"))
            {
                editTextSize.Start();

                //Change the Text Type Name
                textType.Name = "5mm";

                //Change the Text Size
                textType.get_Parameter(BuiltInParameter.TEXT_SIZE).Set(5 / 304.8);

                editTextSize.Commit();
            }

 

 

Hope this will helps 🙂

Thanks & Regards,
Mohamed Arshad K
0 Likes

revitdeveloper
Contributor
Contributor

This was the solution @Mohamed_Arshad , thanks a lot!

0 Likes