Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Duplicating view

1 REPLY 1
SOLVED
Reply
Message 1 of 2
neelDZKPU
147 Views, 1 Reply

Duplicating view

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;

}


}
}
}

1 REPLY 1
Message 2 of 2
Mohamed_Arshad
in reply to: neelDZKPU

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 🙂

Thanks & Regards,
Mohamed Arshad K

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


Autodesk Design & Make Report