Duplicating view

Duplicating view

neelDZKPU
Explorer Explorer
267 Views
1 Reply
Message 1 of 2

Duplicating view

neelDZKPU
Explorer
Explorer

I have created some script to duplicate a view by selecting it. after selecting the view using pickobject method, Its showing error, my view is null. I think problem is in converting reference to view. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using System.Xml.Linq;

namespace MyRevitCommands
{
[TransactionAttribute(TransactionMode.Manual)]
internal class DuplicateView : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
//Get UIdoc
UIDocument uidoc = commandData.Application.ActiveUIDocument;

// Get doc
Document doc = uidoc.Document;
try
{
// pick view
Reference pickobj = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element);

if (pickobj != null)
{
View view = doc.GetElement(pickobj) as View;
using (Transaction transaction = new Transaction(doc, "Duplicate View"))
{
transaction.Start();
ElementId newViewId = view.Duplicate(ViewDuplicateOption.Duplicate);
transaction.Commit();
TaskDialog.Show("View ", view.Name + "is duplicated");

}

}
return Result.Succeeded;
}

catch (Exception e)
{
message = e.Message;
return Result.Failed;

}


}
}
}

0 Likes
Accepted solutions (1)
268 Views
1 Reply
Reply (1)
Message 2 of 2

Mohamed_Arshad
Advisor
Advisor
Accepted solution

HI @neelDZKPU 
   

    Your problem is not in Conversion, problem in PickObject Method (ObjectType) Method. You can't be able to pick views because it is outside the UIDocument. So instead of PickObject Method (ObjectType) you can use GetElementIds Method Kindly refer the below code.

 

1. First you need to select the View and then click on the plugin.

 

Reference Code :

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using System.Xml.Linq;

namespace MyRevitCommands
{
    [TransactionAttribute(TransactionMode.Manual)]
    public class DuplicateView : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //Get UIdoc
            UIDocument uidoc = commandData.Application.ActiveUIDocument;

            // Get doc
            Document doc = uidoc.Document;
            try
            {
                // pick view
               List<ElementId> ids = uidoc.Selection.GetElementIds().ToList(); //Replaced from PickObject

                if (ids.Count>0 )
                {
                    View view = doc.GetElement(ids[0]) as View;
                    using (Transaction transaction = new Transaction(doc, "Duplicate View"))
                    {
                        transaction.Start();
                        ElementId newViewId = view.Duplicate(ViewDuplicateOption.Duplicate);
                        transaction.Commit();
                        TaskDialog.Show("View ", view.Name + "is duplicated");

                    }

                }
                return Result.Succeeded;
            }

            catch (Exception e)
            {
                message = e.Message;
                return Result.Failed;

            }


        }
    }
}

 

 

Hope this will Helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)