Issue in ViewPlan.Create

Issue in ViewPlan.Create

Mariam.EmadLHT4U
Explorer Explorer
448 Views
4 Replies
Message 1 of 5

Issue in ViewPlan.Create

Mariam.EmadLHT4U
Explorer
Explorer

I am trying to create a function that Created multiple ViewPlans at once and but i have an issue that it only work  when the return of function is void when i am change the type of return to List <ViewPlan> or List<ElementId> it will  not work no views created, anyone face issue like that ?

 

0 Likes
449 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni

Nope. I have never been able to change the return type of a function. Normally, that is defined by the function itself and cannot be changed. How do you achieve that?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 5

Mohamed_Arshad
Advisor
Advisor

Hi @Mariam.EmadLHT4U 

 

     I wrote a custom method which will create multiple views, I have done it in Generic way. You can modify it according to your input. Kindly check the below code snippet for additional reference.

 

Custom Method for View Creation

 

 public List<ViewPlan> CreateMultipleViewPlan(Document doc,int numberOfView, ElementId levelId, ElementId viewFamilyTypeId,out string errorMessage)
 {
     errorMessage = string.Empty;
     List<ViewPlan>views=new List<ViewPlan>();

     Transaction createViews = new Transaction(doc, "Create Multiple View");
     createViews.Start();

     try
     {
         for (int i = 0; i < numberOfView; i++)
         {
             views.Add(ViewPlan.Create(doc, viewFamilyTypeId, levelId));
         }

         createViews.Commit();
         createViews.Dispose();
     }
     catch (Exception ex)
     {
         errorMessage=ex.Message;
         createViews.RollBack();
         createViews.Dispose();
         views = null;
     }

     return views;
 }

 

 

Complete Implementation

 

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 Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB.Architecture;

namespace Forum_Answering
{
    [Transaction(TransactionMode.Manual)]
    public class MultipleViewCreation : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;


            ElementId viewFamilyTypeId = new FilteredElementCollector(doc)
                .OfClass(typeof(ViewFamilyType))
                .FirstElementId();

            ElementId levelId= new FilteredElementCollector(doc)
                .OfClass(typeof(Level))
                .FirstElementId();

            CreateMultipleViewPlan(doc, 3, levelId, viewFamilyTypeId, out string errorMessage);



            return Result.Succeeded;
        }

        public List<ViewPlan> CreateMultipleViewPlan(Document doc,int numberOfView, ElementId levelId, ElementId viewFamilyTypeId,out string errorMessage)
        {
            errorMessage = string.Empty;
            List<ViewPlan>views=new List<ViewPlan>();

            Transaction createViews = new Transaction(doc, "Create Multiple View");
            createViews.Start();

            try
            {
                for (int i = 0; i < numberOfView; i++)
                {
                    views.Add(ViewPlan.Create(doc, viewFamilyTypeId, levelId));
                }

                createViews.Commit();
                createViews.Dispose();
            }
            catch (Exception ex)
            {
                errorMessage=ex.Message;
                createViews.RollBack();
                createViews.Dispose();
                views = null;
            }

            return views;
        }


    }
}

 

 

Plugin Demo Video

 

Hope this will Helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 4 of 5

Mariam.EmadLHT4U
Explorer
Explorer

you are running the addin without putting  the output of this function  CreateMultipleViewPlan in a List<ViewPlan> so can you please try to  run it after putting the variable like that

List<ViewPlan>views= CreateMultipleviewPlan(); and share with me the output

0 Likes
Message 5 of 5

Mohamed_Arshad
Advisor
Advisor

Hi @Mariam.EmadLHT4U 

 

     Just you need to assign output in variable, Kindly check the below code.

 

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 Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB.Architecture;

namespace Forum_Answering
{
    [Transaction(TransactionMode.Manual)]
    public class MultipleViewCreation : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;


            ElementId viewFamilyTypeId = new FilteredElementCollector(doc)
                .OfClass(typeof(ViewFamilyType))
                .FirstElementId();

            ElementId levelId = new FilteredElementCollector(doc)
                .OfClass(typeof(Level))
                .FirstElementId();

            List<ViewPlan> newlyCreatedViewPlans = CreateMultipleViewPlan(doc, 3, levelId, viewFamilyTypeId, out string errorMessage);


            return Result.Succeeded;
        }

        /// <summary>
        /// Multiple View Plan Creation Method
        /// </summary>
        /// <param name="doc"> Document </param>
        /// <param name="numberOfView"> Number of View need to be created</param>
        /// <param name="levelId"> Level Id</param>
        /// <param name="viewFamilyTypeId"> View Family Type Id</param>
        /// <param name="errorMessage"> Error message while creating view plan</param>
        /// <returns></returns>
        public List<ViewPlan> CreateMultipleViewPlan(Document doc, int numberOfView, ElementId levelId, ElementId viewFamilyTypeId, out string errorMessage)
        {
            errorMessage = string.Empty;
            List<ViewPlan> views = new List<ViewPlan>();

            Transaction createViews = new Transaction(doc, "Create Multiple View");
            createViews.Start();

            try
            {
                for (int i = 0; i < numberOfView; i++)
                {
                    views.Add(ViewPlan.Create(doc, viewFamilyTypeId, levelId));
                }

                createViews.Commit();
                createViews.Dispose();
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                createViews.RollBack();
                createViews.Dispose();
                views = null;
            }

            return views;
        }
    }
}

 

Hope this will Helps 🙂

 


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes