/// <summary> /// Initializes a new instance of TagBeamData. /// </summary> /// <param name="commandData">An object that is passed to the external application /// which contains data related to the command</param> public TagBeamData(ExternalCommandData commandData) { //Get beams selected m_revitDoc = commandData.Application.ActiveUIDocument; m_docCreator = m_revitDoc.Document.Create; m_view = m_revitDoc.Document.ActiveView; SelElementSet elementSet = m_revitDoc.Selection.Elements; ElementSetIterator itor = elementSet.ForwardIterator(); while (itor.MoveNext()) { FamilyInstance familyInstance = itor.Current as FamilyInstance; if ((familyInstance != null) && (familyInstance.StructuralType == Autodesk.Revit.DB.Structure.StructuralType.Beam)) { m_beamList.Add(familyInstance); } } if (m_beamList.Count < 1) { throw new ApplicationException("there is no beam selected"); } //Get the family symbols of tag in this document. FilteredElementCollector collector = new FilteredElementCollector(commandData.Application.ActiveUIDocument.Document); IList <Element> elements = collector.OfClass(typeof(Family)).ToElements(); foreach (Family family in elements) { if (family != null && family.Symbols != null) { FamilySymbolSetIterator it = family.Symbols.ForwardIterator(); while (it.MoveNext()) { try { FamilySymbol tagSymbol = it.Current as FamilySymbol; if (tagSymbol != null) { switch (tagSymbol.Category.Name) { case "Structural Framing Tags": m_categoryTagTypes.Add(new FamilySymbolWrapper(tagSymbol)); continue; case "Material Tags": m_materialTagTypes.Add(new FamilySymbolWrapper(tagSymbol)); continue; case "Multi-Category Tags": m_multiCategoryTagTypes.Add(new FamilySymbolWrapper(tagSymbol)); continue; default: continue; } } } catch (Exception) { continue; } } } } }
/// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="commandData">An object that is passed to the external application /// which contains data related to the command, /// such as the application object and active view.</param> /// <param name="message">A message that can be set by the external application /// which will be displayed if a failure or cancellation is returned by /// the external command.</param> /// <param name="elements">A set of elements to which the external application /// can add elements that are to be highlighted in case of failure or cancellation.</param> /// <returns>Return the status of the external command. /// A result of Succeeded means that the API external method functioned as expected. /// Cancelled can be used to signify that the user cancelled the external operation /// at some point. Failure should be returned if the application is unable to proceed with /// the operation.</returns> public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { // set out default result to failure. Autodesk.Revit.UI.Result retRes = Autodesk.Revit.UI.Result.Failed; Autodesk.Revit.UI.UIApplication app = commandData.Application; // get the elements selected // The current selection can be retrieved from the active // document via the selection object SelElementSet seletion = app.ActiveUIDocument.Selection.Elements; // we need to make sure that only one element is selected. if (seletion.Size == 1) { // we need to get the first and only element in the selection. Do this by getting // an iterator. MoveNext and then get the current element. ElementSetIterator it = seletion.ForwardIterator(); it.MoveNext(); Element element = it.Current as Element; // Next we need to iterate through the parameters of the element, // as we iterating, we will store the strings that are to be displayed // for the parameters in a string list "parameterItems" List <string> parameterItems = new List <string>(); ParameterSet parameters = element.Parameters; foreach (Parameter param in parameters) { if (param == null) { continue; } // We will make a string that has the following format, // name type value // create a StringBuilder object to store the string of one parameter // using the character '\t' to delimit parameter name, type and value StringBuilder sb = new StringBuilder(); // the name of the parameter can be found from its definition. sb.AppendFormat("{0}\t", param.Definition.Name); // Revit parameters can be one of 5 different internal storage types: // double, int, string, Autodesk.Revit.DB.ElementId and None. // if it is double then use AsDouble to get the double value // then int AsInteger, string AsString, None AsStringValue. // Switch based on the storage type switch (param.StorageType) { case Autodesk.Revit.DB.StorageType.Double: // append the type and value sb.AppendFormat("double\t{0}", param.AsDouble()); break; case Autodesk.Revit.DB.StorageType.ElementId: // for element ids, we will try and retrieve the element from the // document if it can be found we will display its name. sb.Append("Element\t"); // using ActiveDocument.GetElement(the element id) to // retrieve the element from the active document Autodesk.Revit.DB.ElementId elemId = new ElementId(param.AsElementId().IntegerValue); Element elem = app.ActiveUIDocument.Document.GetElement(elemId); // if there is an element then display its name, // otherwise display the fact that it is not set sb.Append(elem != null ? elem.Name : "Not set"); break; case Autodesk.Revit.DB.StorageType.Integer: // append the type and value sb.AppendFormat("int\t{0}", param.AsInteger()); break; case Autodesk.Revit.DB.StorageType.String: // append the type and value sb.AppendFormat("string\t{0}", param.AsString()); break; case Autodesk.Revit.DB.StorageType.None: // append the type and value sb.AppendFormat("none\t{0}", param.AsValueString()); break; default: break; } // add the completed line to the string list parameterItems.Add(sb.ToString()); } // Create our dialog, passing it the parameters array for display. PropertiesForm propertiesForm = new PropertiesForm(parameterItems.ToArray()); propertiesForm.StartPosition = FormStartPosition.CenterParent; propertiesForm.ShowDialog(); retRes = Autodesk.Revit.UI.Result.Succeeded; } else { message = "Please select only one element"; } return(retRes); }