Please check this code for Getting Family Parameter GUID

Please check this code for Getting Family Parameter GUID

KCIM_고영빈
Explorer Explorer
353 Views
3 Replies
Message 1 of 4

Please check this code for Getting Family Parameter GUID

KCIM_고영빈
Explorer
Explorer
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.ApplicationServices;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System;

namespace NewJeans
{
    [Transaction(TransactionMode.Manual)]
    public class ExtractSharedParamsGUID : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            Document doc = uiDoc.Document;
            Application app = commandData.Application.Application;

            List<string> result = new List<string>();

            var familyInstances = new FilteredElementCollector(doc)
                               .OfClass(typeof(FamilyInstance))
                               .Cast<FamilyInstance>()
                               .ToList();

           
            List<Family> families = familyInstances
                            .Select(fi => fi.Symbol.Family)
                            .Distinct().ToList();


            foreach (Family fam in families)
            {
                Document famDoc = doc.EditFamily(fam);
                FamilyManager fm = famDoc.FamilyManager;

                using (Transaction trans = new Transaction(famDoc, "ExtractFamilyParameter"))
                {
                    trans.Start();

                    foreach (FamilyParameter p in fm.Parameters)
                    {
                        string name = p.Definition.Name;
                        string defType = p.Definition.GetType().Name;

                        if (p.IsShared)
                        {
                            if (p.Definition is ExternalDefinition extDef)
                            {
                                string guid = extDef.GUID.ToString();
                                result.Add($" {fam.Name} - {name} : Shared, GUID: {guid}");
                            }
                            else if ((p.Definition is InternalDefinition intDef))
                            {
                                string type = intDef.ToString();
                                result.Add($" {fam.Name} - {name} : Shared, but {type}");
                            }
                        }
                        else
                        {
                            result.Add($" {fam.Name} - {name} : Not Shared");
                        }
                    }

                    trans.RollBack();
                }

                famDoc.Close(false);
            }

            TaskDialog.Show("Shared Parameters", string.Join("\n", result));

            return Result.Succeeded;

        }
    }
}

 

this is the code and result is 

KCIM__0-1744635638778.png

 

I correctly added the Shared Parameter to the family("shared2", "shared@").

When I access the parameter in the API, it shows up as InternalDefinition, even though the IsShared property returns True.

 

 

Has anyone encountered this issue before? What am I missing? How can I ensure that the Shared Parameter is recognized properly as a Shared Parameter in the API, rather than an InternalDefinition? please.... Help me

 

0 Likes
Accepted solutions (1)
354 Views
3 Replies
Replies (3)
Message 2 of 4

jeremy_tammik
Alumni
Alumni

Little side note: this command can probably be run in read-only mode, with no transaction. I always prefer read-only TransactionMode when it is possible. Please try it out and let us know whether that works as well.

   

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

jeremy_tammik
Alumni
Alumni
Accepted solution

It seems to me that Gemini captures, understands and explains the issue quite well. Please check this answer and let us know whether it helps resolve the issue:

  

  

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

KCIM_고영빈
Explorer
Explorer

That was incredibly helpful...!! Your answer helped me so much

0 Likes