/// <summary> /// Get 1 truss or 2 columns from selection /// </summary> /// <returns>return false if selection incorrect</returns> private bool GetSelectTrussOrColumns() { if (m_activeDocument.Selection.GetElementIds().Count > 2 || 0 == m_activeDocument.Selection.GetElementIds().Count) { return(false); } ElementSet es = new ElementSet(); foreach (ElementId elementId in m_activeDocument.Selection.GetElementIds()) { es.Insert(m_activeDocument.Document.GetElement(elementId)); } IEnumerator iter = es.GetEnumerator(); iter.Reset(); while (iter.MoveNext()) { if (iter.Current is Autodesk.Revit.DB.Structure.Truss) { if (null == m_truss) { m_truss = iter.Current as Autodesk.Revit.DB.Structure.Truss; } else { return(false); } } else if (iter.Current is Autodesk.Revit.DB.FamilyInstance) { FamilyInstance familyInstance = iter.Current as FamilyInstance; if (StructuralType.Column == familyInstance.StructuralType) { if (null == column1) { column1 = familyInstance; } else { column2 = familyInstance; } } else { return(false); } } else { return(false); } } if (null == m_truss && (null == column1 || null == column2)) { return(false); } return(true); }
/// <summary> /// Run this sample /// </summary> public void Run() { ElementSet collection = new ElementSet(); foreach (var eid in m_doc.Selection.GetElementIds()) { collection.Insert(m_doc.Document.GetElement(eid)); } // check user selection if (collection.Size < 1) { MessageBox.Show("Please select an object to delete.", "DeleteObject"); return; } bool error = true; try { error = true; // delete selection IEnumerator e = collection.GetEnumerator(); bool MoreValue = e.MoveNext(); while (MoreValue) { Element component = e.Current as Element; m_doc.Document.Delete(component.Id); MoreValue = e.MoveNext(); } error = false; } catch { // if revit threw an exception, try to catch it List <ElementId> CollectionId = new List <ElementId>(); foreach (Element c in collection) { CollectionId.Add(c.Id); } m_doc.Selection.SetElementIds(CollectionId); MessageBox.Show("Element(s) can't be deleted.", "DeleteObject"); return; } finally { // if revit threw an exception, display error and return failed if (error) { MessageBox.Show("Deletion failed."); } } return; }
/// <summary> /// Display the value of Unique ID parameter in a list box /// </summary> /// <returns></returns> public System.Collections.ArrayList SendValueToListBox() { ElementSet elements = m_revit.ActiveUIDocument.Selection.Elements; // all the elements of current document IEnumerator i = elements.GetEnumerator(); ArrayList parameterValueArrangeBox = new ArrayList(); // if the selections include beams and slabs, find out their Unique ID's value for display i.Reset(); bool moreElements = i.MoveNext(); while (moreElements) { // Get beams and slabs from selections Element component = i.Current as Autodesk.Revit.DB.Element; if (null == component) { moreElements = i.MoveNext(); continue; } if (null == component.Category) { moreElements = i.MoveNext(); continue; } if (("Structural Framing" != component.Category.Name) && ("Floors" != component.Category.Name)) { moreElements = i.MoveNext(); continue; } // Get "Unique ID" parameter and display its value in a list box ParameterSet attributes = component.Parameters; foreach (object o in attributes) { Parameter attribute = o as Parameter; if ("Unique ID" == attribute.Definition.Name) { if (null == attribute.AsString()) { break; } parameterValueArrangeBox.Add(attribute.AsString()); break; } } moreElements = i.MoveNext(); } return(parameterValueArrangeBox); }
///<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( Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { Autodesk.Revit.UI.UIApplication revit = commandData.Application; ElementSet collection = new ElementSet(); foreach (ElementId elementId in revit.ActiveUIDocument.Selection.GetElementIds()) { collection.Insert(revit.ActiveUIDocument.Document.GetElement(elementId)); } // check user selection if (collection.Size < 1) { message = "Please select object(s) before delete."; return(Autodesk.Revit.UI.Result.Cancelled); } bool error = true; try { error = true; // delete selection IEnumerator e = collection.GetEnumerator(); bool MoreValue = e.MoveNext(); while (MoreValue) { Element component = e.Current as Element; revit.ActiveUIDocument.Document.Delete(component.Id); MoreValue = e.MoveNext(); } error = false; } catch { // if revit threw an exception, try to catch it foreach (Element c in collection) { elements.Insert(c); } message = "object(s) can't be deleted."; return(Autodesk.Revit.UI.Result.Failed); } finally { // if revit threw an exception, display error and return failed if (error) { TaskDialog.Show("Error", "Delete failed."); } } return(Autodesk.Revit.UI.Result.Succeeded); }
/// <summary> /// get selected floor (slab) /// </summary> /// <param name="commandData">object which contains reference of Revit Application.</param> /// <returns>selected floor (slab)</returns> private Floor GetSelectFloor(ExternalCommandData commandData) { ElementSet eleSet = new ElementSet(); foreach (ElementId elementId in commandData.Application.ActiveUIDocument.Selection.GetElementIds()) { eleSet.Insert(commandData.Application.ActiveUIDocument.Document.GetElement(elementId)); } if (eleSet.Size != 1) { return(null); } IEnumerator iter = eleSet.GetEnumerator(); iter.Reset(); while (iter.MoveNext()) { return(iter.Current as Floor); } return(null); }
/// <summary> /// found the element which using the GUID /// that was assigned to the shared parameter in the shared parameters file. /// </summary> /// <param name="UniqueIdValue"></param> public void FindElement(string UniqueIdValue) { ElementSet seleElements = new ElementSet(); foreach (ElementId elementId in m_revit.ActiveUIDocument.Selection.GetElementIds()) { seleElements.Insert(m_revit.ActiveUIDocument.Document.GetElement(elementId)); } // all the elements of current document IEnumerator i = seleElements.GetEnumerator(); // if the selections include beams and slabs, // find out the element using the select value for display i.Reset(); bool moreElements = i.MoveNext(); while (moreElements) { // Get beams and slabs from selections Element component = i.Current as Autodesk.Revit.DB.Element; if (null == component) { moreElements = i.MoveNext(); continue; } if (null == component.Category) { moreElements = i.MoveNext(); continue; } if (("Structural Framing" != component.Category.Name) && ("Floors" != component.Category.Name)) { moreElements = i.MoveNext(); continue; } // Get "Unique ID" parameter ParameterSet attributes = component.Parameters; foreach (object o in attributes) { Parameter attribute = o as Parameter; if ("Unique ID" == attribute.Definition.Name) { if (null == attribute.AsString()) { break; } // compare if the parameter's value is the same as the selected value. // Clear the SelElementSet and add the found element into it. // So this element will highlight in Revit UI if (UniqueIdValue == attribute.AsString()) { seleElements.Clear(); seleElements.Insert(component); return; } break; } } moreElements = i.MoveNext(); } }
/// <summary> /// Doors related rooms: update doors' geometry according to its To/From room information. /// </summary> /// <param name="creFilter">One element filter utility object.</param> /// <param name="doc">Revit project.</param> /// <param name="onlyUpdateSelect"> /// true means only update selected doors' information else false. /// </param> public static void UpdateDoorsGeometry(Document doc, bool onlyUpdateSelect) { IEnumerator iter; int doorCount = 0; if (onlyUpdateSelect) // update doors in select elements { UIDocument newUIdoc = new UIDocument(doc); ElementSet es = new ElementSet(); foreach (ElementId elementId in newUIdoc.Selection.GetElementIds()) { es.Insert(newUIdoc.Document.GetElement(elementId)); } iter = es.GetEnumerator(); } else // update all doors in current Revit document { ElementClassFilter familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance)); ElementCategoryFilter doorsCategoryfilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors); LogicalAndFilter doorInstancesFilter = new LogicalAndFilter(familyInstanceFilter, doorsCategoryfilter); iter = new FilteredElementCollector(doc).WherePasses(doorInstancesFilter).GetElementIterator(); } iter.Reset(); while (iter.MoveNext()) { // find door instance FamilyInstance door = iter.Current as FamilyInstance; if (onlyUpdateSelect) { if (null == door) { continue; } if (null == door.Category) { continue; } if (!door.Category.Name.Equals("Doors")) { continue; } } // find one door. doorCount++; // update one door. UpdateFromToRoomofOneDoor(door, true); doc.Regenerate(); } if (onlyUpdateSelect) { Autodesk.Revit.UI.TaskDialog.Show("Door Swing", "Updated all selected doors (" + doorCount + " doors).\r\n (Selection may include miscellaneous elements.)"); } else { Autodesk.Revit.UI.TaskDialog.Show("Door Swing", "Updated all doors of this project (" + doorCount + " doors)."); } }
/// <summary> /// update door instances information: Left/Right information, related rooms information. /// </summary> /// <param name="creFilter">One element filter utility object.</param> /// <param name="doc">Revit project.</param> /// <param name="onlyUpdateSelect"> /// true means only update selected doors' information otherwise false. /// </param> /// <param name="showUpdateResultMessage"> /// this parameter is used for invoking this method in Application's events (document save and document saveAs). /// update door infos in Application level events should not show unnecessary messageBox. /// </param> public static Autodesk.Revit.UI.Result UpdateDoorsInfo(Document doc, bool onlyUpdateSelect, bool showUpdateResultMessage, ref string message) { if ((!AssignedAllRooms(doc)) && showUpdateResultMessage) { TaskDialogResult dialogResult = TaskDialog.Show("Door Swing", "One or more eligible areas of this level " + "have no assigned room(s). Doors bounding these areas " + "will be designated as external doors. Proceed anyway?", TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No); if (TaskDialogResult.No == dialogResult) { message = "Update cancelled. Please assign rooms for all eligible areas first."; return(Autodesk.Revit.UI.Result.Cancelled); } } // begin update door parameters. IEnumerator iter; int doorCount = 0; bool checkSharedParameters = false; if (onlyUpdateSelect) // update doors in select elements { UIDocument newUIdoc = new UIDocument(doc); ElementSet es = new ElementSet(); foreach (ElementId elementId in newUIdoc.Selection.GetElementIds()) { es.Insert(newUIdoc.Document.GetElement(elementId)); } iter = es.GetEnumerator(); } else // update all doors in current Revit project. { ElementClassFilter familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance)); ElementCategoryFilter doorsCategoryfilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors); LogicalAndFilter doorInstancesFilter = new LogicalAndFilter(familyInstanceFilter, doorsCategoryfilter); iter = new FilteredElementCollector(doc).WherePasses(doorInstancesFilter).GetElementIterator(); } iter.Reset(); while (iter.MoveNext()) { // find door instance FamilyInstance door = iter.Current as FamilyInstance; if (onlyUpdateSelect) { if (null == door) { continue; } if (null == door.Category) { continue; } if (!door.Category.Name.Equals("Doors")) { continue; } } // check if has needed parameters. if (!checkSharedParameters) { checkSharedParameters = true; if (!(door.Symbol.ParametersMap.Contains("BasalOpening") && door.ParametersMap.Contains("InstanceOpening") && door.ParametersMap.Contains("Internal Door"))) { message = "Cannot update door parameters. Please customize door opening expression first."; return(Autodesk.Revit.UI.Result.Failed); } } // get one door. doorCount++; // update one door's Opening parameter value. if (UpdateOpeningFeatureOfOneDoor(door) == Autodesk.Revit.UI.Result.Failed) { message = "Cannot update door parameters. Please customize door opening expression first."; return(Autodesk.Revit.UI.Result.Failed); } // update one door's from/to room. UpdateFromToRoomofOneDoor(door, false); // update one door's internalDoor flag UpdateInternalDoorFlagFeatureofOneDoor(door); } if (showUpdateResultMessage) { if (onlyUpdateSelect) { Autodesk.Revit.UI.TaskDialog.Show("Door Swing", "Updated all selected doors of " + doc.Title + " (" + doorCount + " doors).\r\n (Selection may " + "include miscellaneous elements.)"); } else { Autodesk.Revit.UI.TaskDialog.Show("Door Swing", "Updated all doors of " + doc.Title + " (" + doorCount + " doors)."); } } return(Autodesk.Revit.UI.Result.Succeeded); }
///<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, Autodesk.Revit.DB.ElementSet elements) { try { Wall wall = null; Floor floor = null; ElementSet elems = commandData.Application.ActiveUIDocument.Selection.Elements; #region selection handle -- select one Slab (or Structure Wall) //if user had some wrong selection, give user an Error message string errorMessage = "Please select one Slab (or Structure Wall) to create PathReinforcement."; if (1 != elems.Size) { message = errorMessage; return(Autodesk.Revit.UI.Result.Cancelled); } Autodesk.Revit.DB.Element selectElem = null; IEnumerator iter = elems.GetEnumerator(); iter.Reset(); if (iter.MoveNext()) { selectElem = (Autodesk.Revit.DB.Element)iter.Current; } if (selectElem is Wall) { wall = selectElem as Wall; if (null == wall.GetAnalyticalModel()) { message = errorMessage; return(Autodesk.Revit.UI.Result.Cancelled); } } else if (selectElem is Floor) { floor = selectElem as Floor; if (null == floor.GetAnalyticalModel()) { message = errorMessage; return(Autodesk.Revit.UI.Result.Cancelled); } } else { message = errorMessage; return(Autodesk.Revit.UI.Result.Cancelled); } #endregion try { if (null != wall) { ProfileWall profileWall = new ProfileWall(wall, commandData); NewPathReinforcementForm newPathReinforcementForm = new NewPathReinforcementForm(profileWall); newPathReinforcementForm.ShowDialog(); } else if (null != floor) { ProfileFloor profileFloor = new ProfileFloor(floor, commandData); NewPathReinforcementForm newPathReinforcementForm = new NewPathReinforcementForm(profileFloor); newPathReinforcementForm.ShowDialog(); } } catch (Exception ex) { message = ex.Message; return(Autodesk.Revit.UI.Result.Cancelled); } return(Autodesk.Revit.UI.Result.Succeeded); } catch (Exception e) { message = e.Message; return(Autodesk.Revit.UI.Result.Failed); } }
///<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, Autodesk.Revit.DB.ElementSet elements) { try { Wall wall = null; Floor floor = null; FamilyInstance familyInstance = null; ElementSet elems = commandData.Application.ActiveUIDocument.Selection.Elements; #region selection handle -- select one floor, wall, beam or nothing //if user had some wrong selection, give user an Error message string errorMessage = "Please select one Floor (Beam or Wall) to create opening or select nothing to create Shaft Opening"; if (elems.Size > 1) { message = errorMessage; return(Autodesk.Revit.UI.Result.Cancelled); } Autodesk.Revit.DB.Element selectElem = null; if (1 == elems.Size) { IEnumerator iter = elems.GetEnumerator(); iter.Reset(); if (iter.MoveNext()) { selectElem = (Autodesk.Revit.DB.Element)iter.Current; } if (selectElem is Wall) { wall = selectElem as Wall; } else if (selectElem is Floor) { floor = selectElem as Floor; } else if (selectElem is FamilyInstance) { familyInstance = selectElem as FamilyInstance; if (familyInstance.StructuralType != Autodesk.Revit.DB.Structure.StructuralType.Beam) { message = errorMessage; return(Autodesk.Revit.UI.Result.Cancelled); } } else { message = errorMessage; return(Autodesk.Revit.UI.Result.Cancelled); } } #endregion try { if (null != wall) { ProfileWall profileWall = new ProfileWall(wall, commandData); ShaftHolePuncherForm shaftHolePuncherForm = new ShaftHolePuncherForm(profileWall); shaftHolePuncherForm.ShowDialog(); } else if (null != floor) { ProfileFloor profileFloor = new ProfileFloor(floor, commandData); ShaftHolePuncherForm shaftHolePuncherForm = new ShaftHolePuncherForm(profileFloor); shaftHolePuncherForm.ShowDialog(); } else if (null != familyInstance) { ProfileBeam profileBeam = new ProfileBeam(familyInstance, commandData); ShaftHolePuncherForm shaftHolePuncherForm = new ShaftHolePuncherForm(profileBeam); shaftHolePuncherForm.ShowDialog(); } else { ProfileNull profileNull = new ProfileNull(commandData); ShaftHolePuncherForm shaftHolePuncherForm = new ShaftHolePuncherForm(profileNull); shaftHolePuncherForm.ShowDialog(); } } catch (Exception ex) { message = ex.Message; return(Autodesk.Revit.UI.Result.Cancelled); } return(Autodesk.Revit.UI.Result.Succeeded); } catch (Exception e) { message = e.Message; return(Autodesk.Revit.UI.Result.Failed); } }