get parameter GUID directly from shared parameter file

get parameter GUID directly from shared parameter file

Ning_Zhou
Advocate Advocate
6,564 Views
8 Replies
Message 1 of 9

get parameter GUID directly from shared parameter file

Ning_Zhou
Advocate
Advocate

is parsing that text file the only way out?

 

seems Definition only have properties of Name, ParameterGroup, ParameterType, and UnitType; no GUID.

 

i'm writing a utility tool to check multiple files (RVT, RFA, RTE, RFT) against our standard SP file, so getting GUIDs from SP file is necessary step.

0 Likes
Accepted solutions (1)
6,565 Views
8 Replies
Replies (8)
Message 2 of 9

Mustafa.Salaheldin
Collaborator
Collaborator

Try this

 

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;

//using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace RevitAddinCS5
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class ExtCmd : IExternalCommand
    {
        #region Cached Variables

        private static ExternalCommandData _cachedCmdData;

        public static UIApplication CachedUiApp
        {
            get
            {
                return _cachedCmdData.Application;
            }
        }

        public static RvtApplication CachedApp
        {
            get
            {
                return CachedUiApp.Application;
            }
        }

        public static RvtDocument CachedDoc
        {
            get
            {
                return CachedUiApp.ActiveUIDocument.Document;
            }
        }

        #endregion

        #region IExternalCommand Members

        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
        {
            _cachedCmdData = cmdData;

            try
            {
                   FilteredElementCollector collector = new FilteredElementCollector(CachedDoc);
                collector.OfClass(typeof(FamilyInstance));

                List<Element> els = collector.Cast<Element>().ToList();

                Element el = els.First();

                string parameter = el.LookupParameter(sharedParameterName).GUID.ToString();

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                return Result.Failed;
            }
        }

        #endregion
    }
}

if it satisfies your needs please mark this reply as an answer.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Message 3 of 9

Ning_Zhou
Advocate
Advocate
thanks Mustafa for your quick reply.
 
well, what i want is getting GUID directly from shared parameter file, not from any existing element, for instance
 
app.SharedParametersFilename = @"C:\SP.txt";
// get SP from SP file
DefinitionFile df = app.OpenSharedParameterFile();
DefinitionGroups dgs = df.Groups;
foreach (DefinitionGroup dg in dgs)
   foreach (Definition d in dg.Definitions)
   {
      string n = d.Name;
      // how to get GUID from d?
   }
0 Likes
Message 4 of 9

Mustafa.Salaheldin
Collaborator
Collaborator
Accepted solution

Here you are the solution and don't forget to mark the reply as answer Smiley Wink

 

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;

//using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace RevitAddinCS2
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class ExtCmd : IExternalCommand
    {
        #region Cached Variables

        private static ExternalCommandData _cachedCmdData;

        public static UIApplication CachedUiApp
        {
            get
            {
                return _cachedCmdData.Application;
            }
        }

        public static RvtApplication CachedApp
        {
            get
            {
                return CachedUiApp.Application;
            }
        }

        public static RvtDocument CachedDoc
        {
            get
            {
                return CachedUiApp.ActiveUIDocument.Document;
            }
        }

        #endregion

        #region IExternalCommand Members

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

            if (CachedDoc.IsFamilyDocument)
            {
                message = "The document must be a project document.";
                return Result.Failed;
            }

            CachedApp.SharedParametersFilename = @"C:\shared.txt";
            // get SP from SP file
            DefinitionFile df = CachedApp.OpenSharedParameterFile();
            DefinitionGroups dgs = df.Groups;

            StringBuilder sb = new StringBuilder();


            foreach (DefinitionGroup dg in dgs)
            {
                sb.AppendLine(string.Format("Group Name: {0}", dg.Name));

                foreach (Definition d in dg.Definitions)
                {
                    if (d != null)
                    {
                        ExternalDefinition ed = d as ExternalDefinition;
                        sb.AppendLine(string.Format("Parameter {0} with Guid: {1}", ed.Name, ed.GUID.ToString()));
                    }
                }
            }

            if(sb.Length > 0)
            {
                TaskDialog.Show("Revit", sb.ToString());
            }

            return Result.Succeeded;
        }
        #endregion
    }
}

Smiley Wink 


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Message 5 of 9

Ning_Zhou
Advocate
Advocate
right, downcasting is needed, thanks again Mustafa for your quick reply and solution!
0 Likes
Message 6 of 9

Revitalizer
Advisor
Advisor

Hi,

 

no need to read the shared parameters text file, and also no need of FamilyInstances to get the Guid of a shared parameter.

 

From Revit 2016 on, there is a SharedParameterElement class, providing a GuidValue property.

So just get these Elements, detect the right one by its Name property and get its GuidValue.

 

That's all.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 7 of 9

Mustafa.Salaheldin
Collaborator
Collaborator

This is right. But as a shared parameter has been loaded into a document, information about it can be retrieved from the SharedParameterElement class.

So you will have to read it directly from the txt file.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Message 8 of 9

Revitalizer
Advisor
Advisor

Hi,

 

you are right, he needs to read the SP text file in each case, to get the parameter guids to look for.

 

For each of the Revit files, he just needs to search for the SharedParameterElements of the guids.

 

 

By the way, nice to see somebody supporting others so much.

Kudo, too.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 9 of 9

Ning_Zhou
Advocate
Advocate

definitely Kudo for both of you!

 

yep, comparing GUID of txt file against GUID of SharedParameterElement from model is the whole idea, in fact, lots of duplicates (same name w/ different GUID) and redundant SPs (existed in model but not in txt file) already found using Revit Lookup, most likely from 3rd party content, model, etc.