//This line has to be here in order for the command to execute in the current Revit context public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { //Get the Current Document from the Current Session doc = commandData.Application.ActiveUIDocument.Document; //Get the User Interface Document from the current Document UIDocument uiDoc = new UIDocument(doc); //Create a Selection variable for picking one or more things from the model Selection picked = uiDoc.Selection; //Initialize a new instance of the ViewSelectionFilter Class to control what can be picked ISelectionFilter filter = new ViewSelectionFilter(); //Use a Try block in case the user "Cancels" the selection it does not throw an Exception try { //Use the PickObject method to select one item. Include the Object Type and Selection Filter Reference selection = picked.PickObject(ObjectType.Element, filter, "Select Viewport on Sheet"); //Check to see if the selection is inded a Viewport if (doc.GetElement(selection) is Autodesk.Revit.DB.Viewport ViewPort) { //Get the View from the Viewport using the ViewId property Autodesk.Revit.DB.View view = doc.GetElement(ViewPort.ViewId) as Autodesk.Revit.DB.View; //Check to see if the View has a valid Referencing Sheet property if (CheckSheet(view.get_Parameter(BuiltInParameter.VIEW_REFERENCING_SHEET).AsString()) is ViewSheet sheet) { //If the View has a Referencing Sheet property, make that sheet the Active View uiDoc.ActiveView = sheet; } } //Let Revit know it executed successfully. This is also how you can roll back the entire feature. return(Result.Succeeded); } //Catch any exceptions in the Try block catch (Exception ex) { //Check to see if the Exception is an ArgumentNullException becuase the PickObject will throw this if the user "Escepes" the selection process if (ex.GetType() != typeof(Autodesk.Revit.Exceptions.ArgumentNullException)) { //If it is any other type of excpetion display a TaskDialog with the information TaskDialog.Show("Selection Error", ex.ToString(), TaskDialogCommonButtons.Ok, TaskDialogResult.Ok); //Return a failed result return(Result.Failed); } else { //If the user cancels the selection, return a Cancalled result return(Result.Cancelled); } } }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { //Get the current Document from the Command Data Document doc = commandData.Application.ActiveUIDocument.Document; //Check to make sure the user is on a sheet before continuing. if (doc.ActiveView.ViewType == ViewType.DrawingSheet) { //Get a new UIDocument from the current document to use to make a selection UIDocument uidoc = new UIDocument(doc); //Create a new instance of the ViewSelectionFilter to only allow the user to select a Legend Viewport ISelectionFilter selectionFilter = new ViewSelectionFilter(); //A selection of elements from the current Document Selection picked = uidoc.Selection; //Try block to catch if the user cancels the selection process try { //Prompt the user to make a single selection in the Revit interface. Only Legend Viewports will be available based onour SelectionFilter. The string is what the status bar displays in the Lower Left Reference selection = picked.PickObject(ObjectType.Element, selectionFilter, "Select Legend on Sheet"); //Make sure the user made a selection if (picked != null) { //Set the Class variable to the Viewport the user selected viewPort = doc.GetElement(selection) as Viewport; //Get the Location of the Legend Viewport selected XYZ locPt = viewPort.GetBoxCenter(); //Prompt the user with a model dialog to select the sheets to place or update the Legends on WPF.SheetSelectionWPF frm = new WPF.SheetSelectionWPF(doc); //Make sure the Sheet Selection Form returns the correct DialogResult if (frm.ShowDialog().Value) { //Make sure the user selected at least 1 sheet if (frm.ViewSheetIds.Count > 0) { //Use this try to make sure Revit doesn't crash when trying to place or update the Legend Viewports try { //Create a Transaction within a using block to dispose of everything when complete using (Transaction Trans = new Transaction(doc)) { //Provide a name for the transaction in the Undo / Redo List Trans.Start("Place Multiple Legends"); //Loop through each Sheet Element Id from the sheets selected by the User foreach (ElementId viewSheetId in frm.ViewSheetIds) { //Get the ViewSheet (Sheet) fro each Element Id in the Document ViewSheet viewSheet = doc.GetElement(viewSheetId) as ViewSheet; //Get all of the Viewport Element Ids on that ViewSheet ICollection <ElementId> viewPortIds = viewSheet.GetAllViewports(); //Use this check to see if the viewport already exists or if it needs to be created bool PlaceViewport = true; //Loop through each ElementId for all Viewports on the Sheet foreach (ElementId elementId in viewPortIds) { //Get the Viewport from the ElementId so we can get the ViewId Viewport vp = doc.GetElement(elementId) as Viewport; //If the Viewport ViewId of the Legend selected matches the ViewId of the current Viewport then we now have the matching legend on the Sheet if (vp.ViewId == viewPort.ViewId) { //Set the bool parameter to False since we will not need to Place a new viewport PlaceViewport = false; //Set the location on the sheet to the same location as the Legend selected vp.SetBoxCenter(locPt); //Break the loop so we don't have to loop through any extra Viewports break; } } //If the Legend Viewport wasn't found, then the bool parameters remains True and a new Viewport for the Legend will be created if (PlaceViewport) { //Create a new viewport at the correct location and sheet Viewport viewport = Viewport.Create(doc, viewSheetId, viewPort.ViewId, locPt); //Set the Viewport Type to the same as the Legend selected viewport.ChangeTypeId(viewPort.GetTypeId()); } } //Commit the Transaction to keep the changes Trans.Commit(); //Return a successful result to Revit so the changes are kept return(Result.Succeeded); } } //Catch an exceptions when adding or updating the Legend Viewports. Tell the user and return a Failed Result so Revit does not keep any changes catch (Exception ex) { TaskDialog.Show("Legend Placement Error", ex.ToString()); return(Result.Failed); } } //Tell the user that they did not select any sheets and return a Cancelled result so Revit does not keep any changes else { TaskDialog.Show("Sheet Selection", "No Sheets were selected"); return(Result.Cancelled); } } //If the Sheet Selection form was closed or cancelled then return a Cancelled result so Revit does not keep any changes else { return(Result.Cancelled); } } //Retrun a Cancelled Reseult if no eleemnts are picked return(Result.Cancelled); } //Catch the Operation canceled Exception which is raised when the user "Escapes" during a Pick Object operation and return a Failed result catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return(Result.Failed); } } //Tell the user that they need to be in a Sheet View to run this Command else { TaskDialog.Show("Sheet Required", "Active view must be a Sheet"); return(Result.Failed); } }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { //Get the current Document from the Command Data Document doc = commandData.Application.ActiveUIDocument.Document; //Check to make sure the user is on a sheet before continuing. if (doc.ActiveView.ViewType == ViewType.DrawingSheet) { //Get a new UIDocument from the current document to use to make a selection UIDocument uidoc = new UIDocument(doc); //Create a new instance of the ViewSelectionFilter to only allow the user to select a ScheduleSheetIntance ISelectionFilter filter = new ViewSelectionFilter(); //A selection of elements from the current Document Selection picked = uidoc.Selection; //Try block to catch if the user cancels the selection process try { //Prompt the user to make a single selection in the Revit interface. Only Shceule Instances will be available based onour SelectionFilter. //The string is what the status bar displays in the Lower Left Reference selection = picked.PickObject(ObjectType.Element, filter, "Select Schedule on Sheet"); //Make sure the user made a selection if (picked != null) { //Set the Class variable to the Viewport the user selected ScheduleInstanceSelected = doc.GetElement(selection) as ScheduleSheetInstance; //Prompt the user with a model dialog to select the sheets to place or update the Schedules on WPF.SheetSelectionWPF frm = new WPF.SheetSelectionWPF(doc); //Make sure the Sheet Selection Form returns the correct DialogResult if (frm.ShowDialog().Value) { //Make sure the user selected at least 1 sheet if (frm.ViewSheetIds.Count > 0) { //Use this try to make sure Revit doesn't crash when trying to place or update the Schedules try { //Create a Transaction within a using block to dispose of everything when complete using (Transaction Trans = new Transaction(doc)) { //Provide a name for the transaction in the Undo / Redo List Trans.Start("Place Multiple Schedules"); foreach (ElementId viewSheetId in frm.ViewSheetIds) { //Get the ViewSheet (Sheet) fro each Element Id in the Document ViewSheet viewSheet = doc.GetElement(viewSheetId) as ViewSheet; //Use a Filtered Element Collector on the Sheet to get all ScheduleSheetInstances which is the reference to the Schedule you see //We do this to check if the Sheet already contains the Schedule we are using ICollection <Element> ScheduleInstances = new FilteredElementCollector(doc, viewSheetId).OfClass(typeof(ScheduleSheetInstance)).ToElements(); //Use this check to see if the Schedule already exists or if it needs to be created bool PlaceSchedule = true; //Loop through each Shcedule Instance on the Sheet foreach (ScheduleSheetInstance ScheduleInstance in ScheduleInstances) { //Check to see if the 'Master' ScheduleId matches the one that was selected by the User if (ScheduleInstance.ScheduleId == ScheduleInstanceSelected.ScheduleId) { //Set the bool to False since the Sheet already contains the schedule, we don't need to create it PlaceSchedule = false; //Move the Schedule on the Sheet to match the same location as the one Selected ScheduleInstance.Point = ScheduleInstanceSelected.Point; } } //If the Schedule was not found on the sheet, the bool stayed True so this will execute if (PlaceSchedule) { //Create a new Sheet Schedule Intance on the Sheet based on the Schedule Selected and at the same location ScheduleSheetInstance.Create(doc, viewSheetId, ScheduleInstanceSelected.ScheduleId, ScheduleInstanceSelected.Point); } } //Commit the Transaction to keep the changes Trans.Commit(); //Return a successful result to Revit so the changes are kept return(Result.Succeeded); } } //Catch an exceptions when adding or updating the Sheet Schedule instances. Tell the user and return a Failed Result so Revit does not keep any changes catch (Exception ex) { TaskDialog.Show("Schedule Placement Error", ex.ToString()); return(Result.Failed); } } //Tell the user that they did not select any sheets and return a Cancelled result so Revit does not keep any changes else { TaskDialog.Show("Sheet Selection", "No Sheets were selected"); return(Result.Cancelled); } } //If the Sheet Selection form was closed or cancelled then return a Cancelled result so Revit does not keep any changes else { return(Result.Cancelled); } } return(Result.Cancelled); } //Catch the Operation canceled Exception which is raised when the user "Escapes" during a Pick Object operation and return a Failed result catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return(Result.Failed); } } //Tell the user that they need to be in a Sheet View to run this Command else { TaskDialog.Show("Sheet Required", "Active view must be a Sheet"); return(Result.Cancelled); } }