/// <summary> /// Do some check for the selection elements, includes geometry check. /// If the data doesn't meet our need, Exception will be thrown. /// </summary> private void Assert() { // Reserve all element ids for following iteration List <ElementId> selectedIds = new List <ElementId>(); foreach (Autodesk.Revit.DB.ElementId elemId in m_rvtUIDoc.Selection.GetElementIds()) { Autodesk.Revit.DB.Element elem = m_rvtUIDoc.Document.GetElement(elemId); selectedIds.Add(elem.Id); } if (selectedIds.Count == 0) { throw new Exception("Please select a concrete beam or column to create rebar."); } // // Construct filter to find expected rebar host // Structural type filters firstly LogicalOrFilter stFilter = new LogicalOrFilter( new ElementStructuralTypeFilter(StructuralType.Beam), new ElementStructuralTypeFilter(StructuralType.Column)); // + StructuralMaterial LogicalAndFilter hostFilter = new LogicalAndFilter(stFilter, new StructuralMaterialTypeFilter(StructuralMaterialType.Concrete)); // Expected rebar host: it should be family instance FilteredElementCollector collector = new FilteredElementCollector(m_rvtUIDoc.Document, selectedIds); FamilyInstance rebarHost = collector.OfClass(typeof(FamilyInstance)).WherePasses(hostFilter).FirstElement() as FamilyInstance; // Make sure the selected beam or column is rectangular. try { m_geometryData = new GeometrySupport(rebarHost); } catch { throw new Exception("Please select a beam or column in rectangular shape."); } m_rebarHost = rebarHost; // Judge the rebar host is a valid host. RebarHostData rebarHostData = RebarHostData.GetRebarHostData(rebarHost); if (rebarHostData == null || !rebarHostData.IsValidHost()) { throw new Exception("The selected element is not a valid rebar host."); } // Make sure the selected beam or column doesn't contain any rebar. if (rebarHostData.GetRebarsInHost().Count > 0) { throw new Exception("Please select a beam or a column which doesn't contain any rebar."); } }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Application app = commandData.Application.Application; UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds(); List<Element> hostElements = new List<Element>(); if (selectedIds.Any()) { foreach (ElementId id in selectedIds) { Element e = doc.GetElement(id); if (RebarHostData.IsValidHost(e)) { hostElements.Add(e); } } } else { try { Reference pickedRef = uidoc.Selection.PickObject(ObjectType.Element, new RebarHostSelectionFilter(), "Pick a rebar host to selct rebar in bottom, TAB to cycle, ESC to cancel"); if (pickedRef == null) { message = "Nothing was selected"; return Result.Failed; } else { hostElements.Add(doc.GetElement(pickedRef)); } } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return Result.Cancelled; } } ICollection<ElementId> idsToSelect = new List<ElementId>(); foreach (Element host in hostElements) { ICollection<ElementId> rebarsInBottomLayer = SelectBottomLayer.GetElementIdsInLayer(doc, host, false); idsToSelect = idsToSelect.Concat(rebarsInBottomLayer).ToList(); } uidoc.Selection.SetElementIds(idsToSelect); return Result.Succeeded; }
Stream(ArrayList data, RebarHostData rebarHostData) { data.Add(new Snoop.Data.ClassSeparator(typeof(RebarHostData))); data.Add(new Snoop.Data.Bool("IsValidHost", rebarHostData.IsValidHost())); }