Railing and API REVIT

Hello, I have a problem, I am creating a Revit macro and I would like to find the length of the guardrail which is drawing the war in 3D. But I can’t do it.

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using Settings;

namespace Calculs_ACV
{
	public class GardeDeCorpProcessor
	{
		private List<string> resultatList;

		public GardeDeCorpProcessor(List<string> resultatList)
		{
			this.resultatList = resultatList;
		}

		public void ProcessCategoryGardeDeCorp(Document doc, string typologie_cgd, string NameElem, string NameParam)
		{
			// Collecteurs pour différentes catégories
			FilteredElementCollector collectorOssature = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralFraming);
			FilteredElementCollector collectorGeneric = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_GenericModel);
			FilteredElementCollector collectorRailings = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StairsRailingBaluster);

			// Combiner les collecteurs en une seule liste
			IEnumerable<Element> combinedCollectors = collectorOssature.Cast<Element>().Concat(collectorGeneric.Cast<Element>()).Concat(collectorRailings.Cast<Element>());

			double totalLength = 0; // Longueur pour les garde-corps normaux
			double totalLengthToiture = 0; // Longueur pour les garde-corps de toiture
			

			// Parcourir tous les éléments combinés
			foreach (Element elem in combinedCollectors)
			{
				// Vérifier si l'élément est une instance de famille
				FamilyInstance familyInstance = elem as FamilyInstance;

				if (familyInstance != null)
				{
					// Récupérer le type de famille
					FamilySymbol familySymbol = familyInstance.Symbol;

					if (familySymbol != null)
					{
						Parameter typologieParam = familySymbol.LookupParameter("Typologie CGD");

						if (typologieParam != null && typologieParam.HasValue)
						{
							string typologieValue = typologieParam.AsString();
							TaskDialog.Show("Debug",string.Format("Typologie trouvée : {0}",typologieValue));

							if (typologieValue.Trim().Equals("Garde corps toiture", StringComparison.OrdinalIgnoreCase) == true)
							{
								TaskDialog.Show("Debug",string.Format( "Élément toiture : {0}",familyInstance.Name));
								Parameter lengthParamToiture = elem.LookupParameter("Longueur");
								if (lengthParamToiture != null && lengthParamToiture.HasValue)
								{
									totalLengthToiture += ConvertFeetToMeters(lengthParamToiture.AsDouble());
								}
								else
								{
									TaskDialog.Show("Debug",string.Format( "Paramètre Longueur manquant pour : {0}",familyInstance.Name));
								}
							}
							else if (typologieValue.Trim().Equals("Garde corps", StringComparison.OrdinalIgnoreCase) == true)
							{
								Parameter lengthParam = elem.LookupParameter("Longueur");
								if (lengthParam != null && lengthParam.HasValue)
								{
									totalLength += ConvertFeetToMeters(lengthParam.AsDouble());
								}
							}
						}
						else
						{
							TaskDialog.Show("Debug", string.Format("Pas de paramètre Typologie CGD pour : {0}",familyInstance.Name));
						}
					}

				}
			}

			// Ajouter les résultats à la liste
			resultatList.Add("GARDE CORPS,Longueur (Ml),<GARDE CORP>");
			resultatList.Add(string.Format("Longueur totale des garde-corps normaux (Ml), {0},<GARDE CORP>",Math.Round(totalLength, 0)));
			resultatList.Add(string.Format("Longueur totale des garde-corps de toiture (Ml), {0},<GARDE CORP>",totalLengthToiture));//Math.Round(totalLengthToiture, 2)));
			
			if (AppSettings.setting.parameter.DetailTypoSetting())
			{
				resultatList.Add("");
			}
		}

		// Méthode pour convertir des pieds en mètres
		private static double ConvertFeetToMeters(double feet)
		{
			return feet * 0.3048; // Conversion de pieds à mètres
		}
	}
}

In this macro I will search in different categories, this is normal. Who can help me for my problems? :sob:

Hi, I’m not an expert in C# or anything else.
but it seems to me that in your collector you can already exclude the element types with the WhereElementIsNotElementType() method

I don’t see concat() in the link which should be more reserved for string.

No longer uses the AddRange method

Your question is more related to the Revit API forum

Wait for a response from someone more knowledgeable than me
Otherwise I would have made people laugh at least

cordially
christian.stan

2 Likes

This is best asked at revit api forums. At the least please indicate how much of this is working and which part isnt. Try rebugging in stages to see which step doesn’t work as expected.

Personally I would try to use more returns to avoid the nesting depth of loops, e.g. returning Result.Failed if the collectors are empty, the parameter isn’t found etc. Or using continue statements to stop an iteration early.