Example #1
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            Document      doc   = uiapp.ActiveUIDocument.Document;

            try
            {
                Reference pickedref = null;

                Selection       sel       = uiapp.ActiveUIDocument.Selection;
                GroupPickFilter selFilter = new GroupPickFilter();
                pickedref = sel.PickObject(ObjectType.Element, selFilter, "Please select a group");
                Element elem  = doc.GetElement(pickedref);
                Group   group = elem as Group;

                // Get the group's center point
                XYZ origin = GetElementCenter(group);
                // Get the room that the picked group is located in
                Room room = GetRoomOfGroup(doc, origin);
                // Get the room's center point
                XYZ    sourceCenter = GetRoomCenter(room);
                string coords       = $"X = {sourceCenter.X}\nY = {sourceCenter.Y}\nZ = {sourceCenter.Z}";
                TaskDialog.Show("Source Room Center", coords);

                //XYZ point = sel.PickPoint("Please pick a point to place group");

                Transaction trans = new Transaction(doc);
                trans.Start("ConsiderablyEnhancedPlaceGroupCommand");
                XYZ groupLocation = sourceCenter + new XYZ(20, 0, 0);
                doc.Create.PlaceGroup(groupLocation, group.GroupType);
                trans.Commit();

                return(Result.Succeeded);
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                return(Result.Cancelled);
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return(Result.Failed);
            }
        }
        // Main method for executing the command
        // =====================================
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // setup the main handles for the application and the active document
            UIApplication uiapp = commandData.Application;
            Document      doc   = uiapp.ActiveUIDocument.Document;

            try  // business logic
            {
                // Ask the user to select a group
                Selection       sel       = uiapp.ActiveUIDocument.Selection;
                GroupPickFilter selFilter = new GroupPickFilter();
                Reference       pickedref = sel.PickObject(ObjectType.Element, selFilter, "Please select a group");
                Element         elem      = doc.GetElement(pickedref);
                Group           group     = elem as Group; // should be safe after using the GroupPickFilter on the Selection

                // Calculate the center location of the room the group is located in
                XYZ  origin       = GetElementCenter(group);
                Room room         = GetRoomOfGroup(doc, origin);
                XYZ  sourceCenter = GetRoomCenter(room);

                // Ask the user to pick target rooms
                RoomPickFilter    roomPickFilter = new RoomPickFilter();
                IList <Reference> rooms          = sel.PickObjects(ObjectType.Element, roomPickFilter, "Select target rooms for duplicating the group");

                // Place groups of the previously selected types in the selected target rooms
                Transaction trans = new Transaction(doc);
                trans.Start("MostEnhancedPlaceGroupCommand");
                PlaceFurnitureInRooms(doc, rooms, sourceCenter, group.GroupType, origin);
                trans.Commit();

                return(Result.Succeeded);
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                return(Result.Cancelled);
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return(Result.Failed);
            }
        }
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // Get application and documnet objects
            UIApplication uiapp = commandData.Application;
            Document      doc   = uiapp.ActiveUIDocument.Document; // could be null if no project is open!

            try
            {
                // Define a reference Object to accept the pick result
                Reference pickedref = null;

                // Pick a group
                Selection       sel       = uiapp.ActiveUIDocument.Selection;
                GroupPickFilter selFilter = new GroupPickFilter();
                pickedref = sel.PickObject(ObjectType.Element, selFilter, "Please select a group"); // could be terminated by the user!, user could select non group objects
                Element elem  = doc.GetElement(pickedref);
                Group   group = elem as Group;                                                      // could be null if type cast fails (elem is not a group)!

                // Pick point
                XYZ point = sel.PickPoint("Please pick a point to place group");  // could be terminated by the user!

                // Place the group
                Transaction trans = new Transaction(doc);
                trans.Start("EnhancedPlaceGroupCommand");
                doc.Create.PlaceGroup(point, group.GroupType);
                trans.Commit();

                // Return the result
                return(Result.Succeeded);
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)  // if the user right clicks or presses ESC button
            {
                return(Result.Cancelled);
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return(Result.Failed);
            }
        }