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;
}
}
}
}
Solved! Go to Solution.
Solved by Mohamed_Arshad. Go to 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 🙂
Can't find what you're looking for? Ask the community or share your knowledge.