<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Section in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6444579#M64618</link>
    <description>&lt;P&gt;do you have any idea, instead of deleting can hide this sections.&lt;/P&gt;</description>
    <pubDate>Mon, 18 Jul 2016 10:57:23 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2016-07-18T10:57:23Z</dc:date>
    <item>
      <title>Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6414412#M64607</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any possibilities to delete all the section marks except live section I means placed sections in sheets.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Richard&amp;nbsp;Mass&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2016 08:26:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6414412#M64607</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-01T08:26:37Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6415719#M64608</link>
      <description>&lt;P&gt;Here you are&lt;/P&gt;
&lt;PRE&gt;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&amp;lt;ViewSection&amp;gt; sections = SectionCollector.Cast&amp;lt;ViewSection&amp;gt;().Where(sh =&amp;gt; sh.ViewType == ViewType.Section).ToList();

                FilteredElementCollector SheetCollector = new FilteredElementCollector(m_Document);
                SheetCollector.OfClass(typeof(ViewSheet));
                List&amp;lt;ViewSheet&amp;gt; sheets = SheetCollector.Cast&amp;lt;ViewSheet&amp;gt;().ToList();

                List&amp;lt;ElementId&amp;gt; sectionsIds = sections.Select( s =&amp;gt; s.Id).ToList();
                List&amp;lt;ElementId&amp;gt; viewsInSheetsIds = new List&amp;lt;ElementId&amp;gt;();

                foreach (ViewSheet sh in sheets)
                {
                    List&amp;lt;ElementId&amp;gt; viewsIds = sh.GetAllPlacedViews().ToList();

                    if (viewsIds.Count &amp;gt; 0)
                    {
                        viewsInSheetsIds.AddRange(viewsIds);
                    }
                }

                List&amp;lt;ElementId&amp;gt; sectionsToDeleteIds = sectionsIds.Except(viewsInSheetsIds).ToList();
                ElementId Remain = ElementId.InvalidElementId;

                if(sectionsToDeleteIds.Count &amp;gt; 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 =&amp;gt; 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

    }
}&lt;/PRE&gt;
&lt;P&gt;Don't forget to mark this reply as an answer. Kudo will be appreciated. &lt;img id="heart" class="emoticon emoticon-heart" src="https://forums.autodesk.com/i/smilies/16x16_heart.png" alt="Heart" title="Heart" /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2016 19:59:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6415719#M64608</guid>
      <dc:creator>Mustafa.Salaheldin</dc:creator>
      <dc:date>2016-07-01T19:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6416902#M64609</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your support and help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am getting some errors can you please check the error in attached VS file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Mass&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jul 2016 11:51:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6416902#M64609</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-03T11:51:04Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6417049#M64610</link>
      <description>&lt;P&gt;Please provide me the error message, snapshot would be better.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Enjoy&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jul 2016 16:34:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6417049#M64610</guid>
      <dc:creator>Mustafa.Salaheldin</dc:creator>
      <dc:date>2016-07-03T16:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6417413#M64611</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for your reply.&lt;/P&gt;&lt;P&gt;I am using revit 2014 version and below i have attached error screen shot for your further investigation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Mass&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jul 2016 05:58:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6417413#M64611</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-04T05:58:59Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6417426#M64612</link>
      <description>I see. This code works for Revit 2016. I will see if it can be translated to 2014.</description>
      <pubDate>Mon, 04 Jul 2016 06:18:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6417426#M64612</guid>
      <dc:creator>Mustafa.Salaheldin</dc:creator>
      <dc:date>2016-07-04T06:18:40Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6418079#M64613</link>
      <description>&lt;P&gt;Thanks for your reply and waiting for your positive suggestion.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jul 2016 14:27:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6418079#M64613</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-04T14:27:08Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6419253#M64614</link>
      <description>&lt;P&gt;Here you are and don't forget the Kudos and Marking the answer: &lt;img id="smileyvery-happy" class="emoticon emoticon-smileyvery-happy" src="https://forums.autodesk.com/i/smilies/16x16_smiley-very-happy.png" alt="Smiley Very Happy" title="Smiley Very Happy" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;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
{
    /// &amp;lt;summary&amp;gt;
    /// A class inherits IExternalCommand interface.
    /// This class show how to create Generic Model Family by Revit API.
    /// &amp;lt;/summary&amp;gt;
    [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&amp;lt;ViewSection&amp;gt; sections = SectionCollector.Cast&amp;lt;ViewSection&amp;gt;().Where(sh =&amp;gt; sh.ViewType == ViewType.Section).ToList();

                FilteredElementCollector ScheduleCollector = new FilteredElementCollector(m_Document);
                ScheduleCollector.OfClass(typeof(ViewSheet));
                List&amp;lt;ViewSheet&amp;gt; sheets = ScheduleCollector.Cast&amp;lt;ViewSheet&amp;gt;().ToList();

                List&amp;lt;ElementId&amp;gt; sectionsIds = sections.Select( s =&amp;gt; s.Id).ToList();
                List&amp;lt;ElementId&amp;gt; viewsInSheetsIds = new List&amp;lt;ElementId&amp;gt;();

                foreach (ViewSheet sh in sheets)
                {
                    List&amp;lt;ElementId&amp;gt; viewsIds = sh.GetAllViewports().ToList();

                    if (viewsIds.Count &amp;gt; 0)
                    {
                        foreach(ElementId id in viewsIds)
                        {
                            Viewport vp = m_Document.GetElement(id) as Viewport;
                            viewsInSheetsIds.Add(vp.ViewId);
                        }
                        
                    }
                }

                List&amp;lt;ElementId&amp;gt; sectionsToDeleteIds = sectionsIds.Except(viewsInSheetsIds).ToList();
                ElementId Remain = ElementId.InvalidElementId;

                if(sectionsToDeleteIds.Count &amp;gt; 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 =&amp;gt; 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

    }
}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2016 11:46:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6419253#M64614</guid>
      <dc:creator>Mustafa.Salaheldin</dc:creator>
      <dc:date>2016-07-05T11:46:00Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6423471#M64615</link>
      <description>&lt;P&gt;Is there any possibilities to terminate the external command if all sections are placed in sheets with warning dialog like no sections to purge...&lt;/P&gt;&lt;P&gt;I tried to modify the code but it is not working as per expected manner.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2016 12:00:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6423471#M64615</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-07T12:00:44Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6423596#M64616</link>
      <description>&lt;P&gt;For the conditional statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if(sectionsToDeleteIds.Count &amp;gt; 0)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;add an "else" statement then let it shows the Task dialog and return Result.Cancelled.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example&lt;/P&gt;
&lt;PRE&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TaskDialog.Show("Revit", "No purge needed.");&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return Result.Cancelled;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;
&lt;P&gt;Please if this reply satisfies your need mark it as an answer.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2016 13:14:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6423596#M64616</guid>
      <dc:creator>Mustafa.Salaheldin</dc:creator>
      <dc:date>2016-07-07T13:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6428322#M64617</link>
      <description>&lt;P&gt;Thank you so much....&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2016 05:44:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6428322#M64617</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-11T05:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6444579#M64618</link>
      <description>&lt;P&gt;do you have any idea, instead of deleting can hide this sections.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2016 10:57:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6444579#M64618</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-18T10:57:23Z</dc:date>
    </item>
    <item>
      <title>Re: Section</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6444639#M64619</link>
      <description>&lt;P&gt;Replace the line&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;m_document.Delete(id)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;with a a command to hide the element id.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2016 11:40:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/section/m-p/6444639#M64619</guid>
      <dc:creator>stever66</dc:creator>
      <dc:date>2016-07-18T11:40:00Z</dc:date>
    </item>
  </channel>
</rss>

