Section

Section

Anonymous
Not applicable
1,429 Views
12 Replies
Message 1 of 13

Section

Anonymous
Not applicable

Hi all,

 

Is there any possibilities to delete all the section marks except live section I means placed sections in sheets.

 

Regards

Richard Mass

0 Likes
Accepted solutions (2)
1,430 Views
12 Replies
Replies (12)
Message 2 of 13

Mustafa.Salaheldin
Collaborator
Collaborator

Here you are

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Linq;
using Autodesk.Revit.DB.Structure;

namespace RevitAddinCS2
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
    public class ExtCmd : IExternalCommand
    {
        #region Class Memeber Variables

        private Autodesk.Revit.ApplicationServices.Application m_revit;

        private Autodesk.Revit.DB.Document m_Document;
        #endregion

        #region Class Interface Implementation

        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
                                               ref string message,
                                               ElementSet elements)
        {
            try
            {
                m_revit = commandData.Application.Application;
                m_Document = commandData.Application.ActiveUIDocument.Document;

                FilteredElementCollector SectionCollector = new FilteredElementCollector(m_Document);
                SectionCollector.OfClass(typeof(ViewSection)).WhereElementIsNotElementType();
                List<ViewSection> sections = SectionCollector.Cast<ViewSection>().Where(sh => sh.ViewType == ViewType.Section).ToList();

                FilteredElementCollector SheetCollector = new FilteredElementCollector(m_Document);
                SheetCollector.OfClass(typeof(ViewSheet));
                List<ViewSheet> sheets = SheetCollector.Cast<ViewSheet>().ToList();

                List<ElementId> sectionsIds = sections.Select( s => s.Id).ToList();
                List<ElementId> viewsInSheetsIds = new List<ElementId>();

                foreach (ViewSheet sh in sheets)
                {
                    List<ElementId> viewsIds = sh.GetAllPlacedViews().ToList();

                    if (viewsIds.Count > 0)
                    {
                        viewsInSheetsIds.AddRange(viewsIds);
                    }
                }

                List<ElementId> sectionsToDeleteIds = sectionsIds.Except(viewsInSheetsIds).ToList();
                ElementId Remain = ElementId.InvalidElementId;

                if(sectionsToDeleteIds.Count > 0)
                {
                    using (Transaction t = new Transaction(m_Document,"delete"))
                    {
                        t.Start();
                        foreach (ElementId id in sectionsToDeleteIds)
                        {
                            ViewSection v = m_Document.GetElement(id) as ViewSection;

                            if (v != null)
                            {                               
                                //if (!commandData.Application.ActiveUIDocument.GetOpenUIViews().Select(v => v.ViewId).Contains(id))
                                if (commandData.Application.ActiveUIDocument.ActiveGraphicalView.Name != v.Name)
                                {
                                    using (SubTransaction st = new SubTransaction(m_Document))
                                    {
                                        st.Start();
                                        m_Document.Delete(id);
                                        st.Commit();
                                    }
                                }
                                else
                                {
                                    Remain = id;
                                }
                            }
                        }
                        t.Commit();
                    }
                }

                if(Remain != ElementId.InvalidElementId)
                {
                    TaskDialog.Show("Revit", string.Format("Section: {0} couldn't be deleted because it is currently opened.", m_Document.GetElement(Remain).Name));
                }

                return Autodesk.Revit.UI.Result.Succeeded;
            }
            catch (Exception e)
            {
                message = e.ToString();
                return Autodesk.Revit.UI.Result.Failed;
            }

        }
        #endregion

    }
}

Don't forget to mark this reply as an answer. Kudo will be appreciated. Heart


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 3 of 13

Anonymous
Not applicable

Hi,

 

Thanks for your support and help.

 

I am getting some errors can you please check the error in attached VS file.

 

Regards

Mass

0 Likes
Message 4 of 13

Mustafa.Salaheldin
Collaborator
Collaborator

Please provide me the error message, snapshot would be better.

 

I've modified some lines in the attached VS project, because it seems due to different encoding some letters in the code were not copied correctly.

 

Enjoy


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 5 of 13

Anonymous
Not applicable

Hi,

 

thanks for your reply.

I am using revit 2014 version and below i have attached error screen shot for your further investigation.

 

Regards

Mass 

0 Likes
Message 6 of 13

Mustafa.Salaheldin
Collaborator
Collaborator
I see. This code works for Revit 2016. I will see if it can be translated to 2014.

¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 7 of 13

Anonymous
Not applicable

Thanks for your reply and waiting for your positive suggestion. 

0 Likes
Message 8 of 13

Mustafa.Salaheldin
Collaborator
Collaborator
Accepted solution

Here you are and don't forget the Kudos and Marking the answer: Smiley Very Happy

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Linq;
using Autodesk.Revit.DB.Structure;

namespace RevitAddinCS2
{
    /// <summary>
    /// A class inherits IExternalCommand interface.
    /// This class show how to create Generic Model Family by Revit API.
    /// </summary>
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
    public class ExtCmd : IExternalCommand
    {
        #region Class Memeber Variables
        private Autodesk.Revit.ApplicationServices.Application m_revit;
        private Autodesk.Revit.DB.Document m_Document;

        #endregion

        #region Class Interface Implementation

        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
                                               ref string message,
                                               ElementSet elements)
        {
            try
            {
                m_revit = commandData.Application.Application;
                m_Document = commandData.Application.ActiveUIDocument.Document;

                FilteredElementCollector SectionCollector = new FilteredElementCollector(m_Document);
                SectionCollector.OfClass(typeof(ViewSection)).WhereElementIsNotElementType();
                List<ViewSection> sections = SectionCollector.Cast<ViewSection>().Where(sh => sh.ViewType == ViewType.Section).ToList();

                FilteredElementCollector ScheduleCollector = new FilteredElementCollector(m_Document);
                ScheduleCollector.OfClass(typeof(ViewSheet));
                List<ViewSheet> sheets = ScheduleCollector.Cast<ViewSheet>().ToList();

                List<ElementId> sectionsIds = sections.Select( s => s.Id).ToList();
                List<ElementId> viewsInSheetsIds = new List<ElementId>();

                foreach (ViewSheet sh in sheets)
                {
                    List<ElementId> viewsIds = sh.GetAllViewports().ToList();

                    if (viewsIds.Count > 0)
                    {
                        foreach(ElementId id in viewsIds)
                        {
                            Viewport vp = m_Document.GetElement(id) as Viewport;
                            viewsInSheetsIds.Add(vp.ViewId);
                        }
                        
                    }
                }

                List<ElementId> sectionsToDeleteIds = sectionsIds.Except(viewsInSheetsIds).ToList();
                ElementId Remain = ElementId.InvalidElementId;

                if(sectionsToDeleteIds.Count > 0)
                {
                    using (Transaction t = new Transaction(m_Document,"delete"))
                    {
                        t.Start();
                        foreach (ElementId id in sectionsToDeleteIds)
                        {
                            ViewSection v = m_Document.GetElement(id) as ViewSection;

                            if (v != null)
                            {                               
                                //if (!commandData.Application.ActiveUIDocument.GetOpenUIViews().Select(v => v.ViewId).Contains(id))
                                try
                                {
                                    using (SubTransaction st = new SubTransaction(m_Document))
                                    {
                                        st.Start();
                                        m_Document.Delete(id);
                                        st.Commit();
                                    }
                                }
                                catch(Exception ex)
                                {
                                    Remain = id;
                                }
                            }
                        }
                        t.Commit();
                    }
                }

                if(Remain != ElementId.InvalidElementId)
                {
                    TaskDialog.Show("Revit", string.Format("Section: {0} couldn't be deleted because it is currently opened.", m_Document.GetElement(Remain).Name));
                }

                return Autodesk.Revit.UI.Result.Succeeded;
            }
            catch (Exception e)
            {
                message = e.ToString();
                return Autodesk.Revit.UI.Result.Failed;
            }

        }
        #endregion

    }
}

 

 


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 9 of 13

Anonymous
Not applicable

Is there any possibilities to terminate the external command if all sections are placed in sheets with warning dialog like no sections to purge...

I tried to modify the code but it is not working as per expected manner.

 

 

0 Likes
Message 10 of 13

Mustafa.Salaheldin
Collaborator
Collaborator
Accepted solution

For the conditional statement:

 

if(sectionsToDeleteIds.Count > 0)

 add an "else" statement then let it shows the Task dialog and return Result.Cancelled.

 

For example

                else
                {
                    TaskDialog.Show("Revit", "No purge needed.");

                    return Result.Cancelled;
                }

Please if this reply satisfies your need mark it as an answer.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Message 11 of 13

Anonymous
Not applicable

Thank you so much....

0 Likes
Message 12 of 13

Anonymous
Not applicable

do you have any idea, instead of deleting can hide this sections.

0 Likes
Message 13 of 13

stever66
Advisor
Advisor

Replace the line 

 

m_document.Delete(id)

 

with a a command to hide the element id.

 

 

 

0 Likes