Monday, November 18, 2013

Quick Macro that creates 5 Dependent Views for the Current View

I had a user that needed to create 5 dependent views for 5 areas in a building across several views and disciplines. To save him time, I created this quick macro that will create 5 dependent views for the active view. This will save a lot of right-clicking on each view.

Code:


        public void CreateDependentViews()
        {
            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;

            using(Transaction t = new Transaction(doc, "Duplicate View 5x"))
            {
                t.Start();
              
                int i = 0;
              
                while (i < 5)
                {
                    uidoc.ActiveView.Duplicate(ViewDuplicateOption.AsDependent);
                    i++;
                }               
                t.Commit();                   
            }           
        }


I expanded it a bit more and did some renaming based on Areas just to enhance it a bit.

Code:

        public void CreateDependentViews()
        {
            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;

            using(Transaction t = new Transaction(doc, "Duplicate View 5x"))
            {
                t.Start();
               
                int i = 0;
               
                while (i < 5)
                {
                    ElementId dupViewId = uidoc.ActiveView.Duplicate(ViewDuplicateOption.AsDependent);
                    View dupView = doc.GetElement(dupViewId) as View;
                    char c = (char) (i + 65);
                    dupView.Name = uidoc.ActiveView.Name + " - AREA " + c.ToString();
                   
                    i++;
                }
               
                t.Commit();                   
            }           
        }

1 comment:

  1. Great macro, Troy! Thanks!

    For my use, I adapted it to add a building name to it:

    public void Dependent_All_Five()
    {
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;

    using(Transaction t = new Transaction(doc, "Duplicate for All Buildings"))
    {
    t.Start();

    ArrayList list = new ArrayList();
    list.Add("Broadway");
    list.Add("Dakota");
    list.Add("Lincoln");
    list.Add("Riverside");
    list.Add("Queens");

    foreach (string value in list)
    {
    ElementId dupViewId = uidoc.ActiveView.Duplicate(ViewDuplicateOption.AsDependent);
    View dupView = doc.GetElement(dupViewId) as View;
    dupView.Name = uidoc.ActiveView.Name + " - " + value;
    }

    t.Commit();
    }
    }

    ReplyDelete