public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { // Get application and document objects UIDocument UIdoc = commandData.Application.ActiveUIDocument; Document doc = UIdoc.Document; try { IList <Element> pickedRef = null; List <ElementId> selectedElements = UIdoc.Selection.GetElementIds().ToList(); if (selectedElements.Count == 0) { pickedRef = UIdoc.Selection.PickObjects(ObjectType.Element, new LineSelectionFilter(), "Выберите линии детализации") .Select(q => doc.GetElement(q.ElementId)) .ToList(); } else { pickedRef = selectedElements.Select(q => doc.GetElement(q)).ToList(); } if (pickedRef.Count == 0) { return(Result.Failed); } // Measure their total length List <double> lengthList = new List <double>(); foreach (Element e in pickedRef) { if (e is DetailLine line) { if (line != null) { lengthList.Add(line.GeometryCurve.Length); } } if (e is ModelLine m_line) { lengthList.Add(m_line.GeometryCurve.Length); } } string lengthMm = Tools.RealString(Math.Round(lengthList.Sum() * 304.8, 2)) + " мм"; StringBuilder sb = new StringBuilder(); sb.AppendLine("Общая длина:"); sb.AppendLine(lengthMm); // Return a message window that displays total length to user TaskDialog.Show("Длина", sb.ToString()); // Assuming that everything went right return Result.Succeeded return(Result.Succeeded); } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return(Result.Cancelled); } catch (Exception e) { message = e.Message; return(Result.Failed); } }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument UIdoc = commandData.Application.ActiveUIDocument; Document doc = UIdoc.Document; using (Transaction t = new Transaction(doc)) { try { IList <Element> pickedRef = null; List <ElementId> selectedElements = UIdoc.Selection.GetElementIds().ToList(); if (selectedElements.Count == 0) { pickedRef = UIdoc.Selection.PickObjects(ObjectType.Element, new HatchSelectionFilter(), "Выберите штриховки") .Select(q => doc.GetElement(q.ElementId)) .ToList(); } else { pickedRef = selectedElements.Select(q => doc.GetElement(q)).ToList(); } if (pickedRef.Count == 0) { return(Result.Failed); } // Measure their total length List <double> areaList = new List <double>(); foreach (Element element in pickedRef) { Parameter param = element.get_Parameter(BuiltInParameter.HOST_AREA_COMPUTED); if (param != null && StorageType.Double == param.StorageType) { areaList.Add(param.AsDouble() * 0.092903); } else { areaList.Add(0.0); } } string areaM2 = Tools.RealString(Math.Round(areaList.Sum(), 2)) + " м2"; StringBuilder sb = new StringBuilder(); sb.AppendLine("Общая площадь:"); sb.AppendLine(areaM2); // Return a message window that displays total length to user TaskDialog.Show("Площадь штриховок", sb.ToString()); return(Result.Succeeded); } catch (OperationCanceledException exceptionCancelled) { message = exceptionCancelled.Message; if (t.HasStarted()) { t.RollBack(); } return(Result.Cancelled); } catch (ErrorMessageException errorException) { message = errorException.Message; if (t.HasStarted()) { t.RollBack(); } return(Result.Failed); } catch (Exception ex) { message = "Неожиданная ошибка: " + ex.Message; if (t.HasStarted()) { t.RollBack(); } return(Result.Failed); } } }