Creating Custom Tag Family with Dynamic Parameters via Revit API C#

Creating Custom Tag Family with Dynamic Parameters via Revit API C#

baristankut
Observer Observer
621 Views
2 Replies
Message 1 of 3

Creating Custom Tag Family with Dynamic Parameters via Revit API C#

baristankut
Observer
Observer

Hello Revit API experts,

I'm working on automating the creation of tag families using C# in Revit API. My specific goal is to:

  1. Either create a new tag family from scratch OR modify/copy an existing tag family from Revit 2022
  2. Add custom labels to this tag that can be linked to dynamic parameters
  3. Configure these labels to display parameter values from the tagged elements

My main challenges are:

  • What's the correct approach to create/modify a tag family programmatically?
  • How to properly add and configure label parameters within the tag family?
  • How to establish the connection between the label and the parameters we want to display?

Here's what I've tried so far:

FamilyManager famMan = doc.FamilyManager;
FamilyParameter label = famMan.AddParameter(
"Label",
BuiltInParameterGroup.PG_IDENTITY_DATA,
ParameterType.Text,
false);

 

However, I'm unsure about:

  • The correct sequence of API calls needed for tag creation
  • How to properly set up the label-parameter associations
  • Whether we need special handling for tag families compared to regular families
  • The best practices for handling shared parameters in this context

Has anyone successfully implemented something similar? Any code examples or guidance would be greatly appreciated, particularly regarding:

  1. The proper API calls for tag family creation/modification
  2. Setting up labels and their parameter associations
  3. Any specific considerations for tag families vs regular families

Thank you in advance for your help!

Technical Environment:

  • Revit 2022
  • C# (.NET Framework)
  • Revit API
0 Likes
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni

Dear Baris,

  

I enjoy playing with AI, and am retiring soon, so I am testing whether AI can help us here in the forum. So, I added a persona prompt to your question and passed it in to Gemini 1.5 Flash. Please take a look at the answer it generated and let us know whether it helps resolve your questions.

  

Jeremy:  You are a knowledgeable and resourceful BIM expert, Revit application engineer, and skilled .NET and Revit API programmer. Your task is to address new questions raised in the Revit API discussion forum. Use insights from The Building Coder blog and other respected Revit API resources to provide clear, innovative solutions: {original question}

  

Gemini: 

Creating Custom Tag Families with Dynamic Parameters via Revit API C#

Understanding the Challenges

You're facing a multi-faceted challenge:

  1. Tag Family Creation/Modification: Revit's API doesn't provide a direct method to create a tag family from scratch. You'll likely need to:

    • Copy an Existing Tag Family: This is generally the most feasible approach.
    • Modify the Copied Family: Use the FamilyManager to add, modify, and delete parameters and family types.
  2. Label Parameter Configuration:

    • Add Label Parameters: Use FamilyManager.AddParameter() to create text parameters for your labels.
    • Associate Labels with Parameters: Define formulas within the tag family to link label values to the parameters of the tagged elements.
  3. Tag Family vs. Regular Families: Tag families have specific requirements and behaviors within the Revit environment. You'll need to understand these nuances to ensure your custom tags function correctly.

Proposed Solution

1. Copy an Existing Tag Family

Code snippet
 
// Get a reference to an existing tag family
Family existingTagFamily = doc.LoadFamily("Path/to/existing/tag/family.rfa"); 

// Create a new family document
FamilyDocument newFamilyDoc = doc.NewFamilyDocument(existingTagFamily); 

// Get access to the FamilyManager
FamilyManager famMan = newFamilyDoc.FamilyManager;

2. Add Custom Label Parameters

Code snippet
 
// Add a parameter for the label
FamilyParameter labelParam = famMan.AddParameter(
    "MyCustomLabel", 
    BuiltInParameterGroup.PG_IDENTITY_DATA, 
    ParameterType.Text, 
    false); 

// Add a parameter for the value to display (example: Length)
FamilyParameter valueParam = famMan.AddParameter(
    "MyValue", 
    BuiltInParameterGroup.PG_IDENTITY_DATA, 
    ParameterType.Length, 
    false); 

3. Create a Formula for the Label

  • Access the Tag's Label: You'll need to identify the specific label within the tag family that you want to modify. This might involve traversing the family's geometry and finding the appropriate text element.

  • Create a Formula: Within the tag family editor, use the Formula editor to link the MyCustomLabel parameter to the MyValue parameter. For example:

    MyCustomLabel = "Length: " + MyValue.ToString() 
    

4. Save the New Tag Family

Code snippet
 
// Save the new tag family
newFamilyDoc.SaveAs("Path/to/save/new/tag/family.rfa"); 

5. Considerations

  • Shared Parameters: If you need to use shared parameters, you'll need to create and assign them before modifying the tag family.
  • Tag Family Specifics: Refer to the Revit API documentation for specific details on tag family creation, modification, and the available methods within the FamilyManager class.
  • Testing: Thoroughly test your custom tags in various scenarios to ensure they function correctly and display the expected values.

Code Example (Simplified)

Code snippet
 
// (Assuming you have the FamilyDocument and FamilyManager)

// Add a parameter for the label
FamilyParameter labelParam = famMan.AddParameter(
    "MyLabel", 
    BuiltInParameterGroup.PG_IDENTITY_DATA, 
    ParameterType.Text, 
    false); 

// Add a parameter for the value to display (e.g., Area)
FamilyParameter valueParam = famMan.AddParameter(
    "MyValue", 
    BuiltInParameterGroup.PG_AREA, 
    ParameterType.Area, 
    false); 

// (This is a simplified representation of accessing and modifying the label's formula)
// Access the label's formula (requires finding the specific label element within the tag family)
string labelFormula = "MyLabel = \"Area: \" + MyValue.ToString()"; 

// Set the label's formula (requires appropriate API calls to modify the label element's properties)
// ... (Simplified for illustration) 

Important Notes

  • This is a basic example. The actual implementation will require more in-depth knowledge of the Revit API and tag family structure.
  • Refer to the Building Coder blog and other reputable Revit API resources for more detailed examples, best practices, and advanced techniques.
  • Thoroughly test your implementation to ensure it meets your specific requirements.

I hope this comprehensive response provides a solid foundation for your custom tag family creation endeavors!

    

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

philip_adlergard
Contributor
Contributor

The snippet for loading family and creating a newfamilydocument, which methods are they and where are they placed? When I search for them and use them loadfamily() returns a bool and newfamilydocument() requires a path, not a family.

 

What's is "doc" in your snippet? 

For loadFamily I have: commandData.Application.ActiveUIDocument.Document;

For NewFamilyDocument I have: commandData.Application.Application;

0 Likes