Exemplo n.º 1
0
        //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 Session / Project from Revit
            UIApplication uiapp = commandData.Application;

            //Get the Current Document from the Current Session
            Document doc = uiapp.ActiveUIDocument.Document;

            //Create a new instance of the SheetSelection form.
            //This implementation is a Windows Form. For WPF see WPF.SheetSelectionWPF
            WPF.SheetSelectionWPF form = new WPF.SheetSelectionWPF(doc);

            //Show the Form as a modal window
            if (form.ShowDialog().Value)
            {
                //If the DialogResult is True, return a Successful Result
                return(Result.Succeeded);
            }
            else
            {
                //If the DialogResult is False, return a Cancelled Result and the Entire Command will be rolled back
                return(Result.Cancelled);
            }
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
        //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 Session / Project from Revit
            UIApplication uiapp = commandData.Application;

            //Get the Current Document from the Current Session
            Document doc = uiapp.ActiveUIDocument.Document;

            //Create a new instance of the SheetSelection form.
            //This implementation is a Windows Form. For WPF see WPF.SheetSelectionWPF
            WPF.SheetSelectionWPF form = new WPF.SheetSelectionWPF(doc);
            //Make sure the Sheet Selection Form returns the correct DialogResult
            if (form.ShowDialog().Value)
            {
                //Make sure the user selected at least 1 sheet
                if (form.ViewSheetIds.Count >= 1)
                {
                    //Check to see if more than one sheet was selected and alert the user if so.
                    if (form.ViewSheetIds.Count > 1)
                    {
                        TaskDialog.Show("Information", "More than one Sheet was selected.\nThe first Sheet in the selection will be used.");
                    }

                    //Use this try to make sure Revit doesn't crash when trying to place or update the 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 Active View");
                            ElementId viewSheetId = form.ViewSheetIds[0];

                            //Get the ViewSheet (Sheet) for the Element Id in the Document
                            ViewSheet viewSheet = doc.GetElement(viewSheetId) as ViewSheet;

                            //Get the Title Block on the sheet to extract Height and Width for View Placement
                            FamilyInstance TitleBlock = new FilteredElementCollector(doc, viewSheetId).OfCategory(BuiltInCategory.OST_TitleBlocks).Cast <FamilyInstance>().FirstOrDefault();

                            //Use the built in parameters for Sheet Width and Height to get their values
                            double SheetWidth  = TitleBlock.get_Parameter(BuiltInParameter.SHEET_WIDTH).AsDouble();
                            double SheetHeight = TitleBlock.get_Parameter(BuiltInParameter.SHEET_HEIGHT).AsDouble();

                            //Check to see if the active view can be placed on the selected sheet
                            if (Viewport.CanAddViewToSheet(doc, viewSheetId, doc.ActiveView.Id))
                            {
                                //Create a viewport for the active view on the sheet selected.
                                //Viewport will be centered on the sheet based on the Height and Width parameters
                                Viewport.Create(doc, viewSheetId, doc.ActiveView.Id, new XYZ(SheetWidth / 2, SheetHeight / 2, 0));
                            }
                            else
                            {
                                TaskDialog.Show("Information", "Active View cannot be placed on the selected Sheet");
                                return(Result.Cancelled);
                            }

                            //Commit the Transaction to keep the changes
                            Trans.Commit();

                            //Check to see if the user would like to change views to the sheet after the viewport was created
                            if (TaskDialog.Show("Change View", "Would you like to go to Sheet:\n" + viewSheet.SheetNumber + " - " + viewSheet.Name + "?", TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No) == TaskDialogResult.Yes)
                            {
                                uiapp.ActiveUIDocument.ActiveView = viewSheet;
                            }

                            //Return a successful result to Revit so the changes are kept
                            return(Result.Succeeded);
                        }
                    }
                    //Catch an exceptions when adding or updating the Viewports. Tell the user and return a Failed Result so Revit does not keep any changes
                    catch (Exception ex)
                    {
                        TaskDialog.Show("Viewport 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);
            }
        }