Parking Component

Parking Component

rahalabi
Participant Participant
766 Views
4 Replies
Message 1 of 5

Parking Component

rahalabi
Participant
Participant

Dear Sir,

 

I need to know how to access Parking component while scripting as I reference for example Room , I will list down the script I have and where I am stuck(Red Color)

***************************************************************************

             if (Current is Room)

what should i use instead of <room>  to access <parking component>

***************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI.Selection;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace CarPark_Renumbering
{


    [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 Command : IExternalCommand
    {
        #region IExternalCommand Members

        Document doc;
        UIDocument uiDoc;
        UIApplication uiApp;
        List<CarParkObject> allParks = new List<CarParkObject>();

        public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            try
            {
                //getting the document
                uiApp = commandData.Application;
                String licMsg = "";
                if (isLic(out licMsg))
                {
                    uiDoc = uiApp.ActiveUIDocument;
                    doc = uiDoc.Document;

                    //renumbering option (Selection or Insertion)
                    MainWindow selectionWindow = new MainWindow();

                    selectionWindow.ShowDialog();

                    if (SelectionResult.SelectedResult == SelectionResult.Result.RenumberInsert)
                    {
                        selectionWindow.Close();
                        renumberInsPark();
                    }
                    else if (SelectionResult.SelectedResult == SelectionResult.Result.IndividualRooms)
                    {
                        selectionWindow.Close();
                        pickParksIndividual(allParks);
                        if (allParks.Count == 0)
                        {
                            message = "No Car Parks Available";
                            return Result.Cancelled;
                        }
                        renumberSelectedPark();
                    }
                    else if (SelectionResult.SelectedResult == SelectionResult.Result.WindowSelect)
                    {
                        selectionWindow.Close();
                        pickParksByWindow(allParks);
                        if (allParks.Count == 0)
                        {
                            message = "No Parks Available";
                            return Result.Cancelled;
                        }
                        allParks.Sort();
                        renumberSelectedPark();
                    }
                    else
                    {
                        selectionWindow.Close();
                        return Result.Cancelled;
                    }

                    return Result.Succeeded;
                }
                else
                {
                    message = "Invalid License\n" + licMsg;
                    return Result.Cancelled;
                }
                
            }
            catch (OperationCanceledException x)
            {
                message = x.Message;
                return Result.Cancelled;
            }
            catch (Exception x)
            {
                message = x.Message;
                return Result.Failed;
            }
        }


        /// <summary>
        /// check if the application is to be run
        /// </summary>
        /// <param name="message">the message if an error occurs</param>
        /// <returns></returns>
        private bool isLic(out String message)
        {
            bool retval = false;
            try
            {
                retval = true;
                message = "";
            }
            catch (Exception x)
            {
                message = "Error reading the License\n" + x.Message;
            }

            return retval;
        }

        //renumber parks based on the selection alg.
        private void renumberSelectedPark()
        {
            //display the park renumbering form and renumber parks
            CarParkRenumber renumberDisplay = new CarParkRenumber(ref allParks, uiApp);
            if (renumberDisplay.ShowDialog() == DialogResult.OK)
            {
                using (Transaction trans = new Transaction(doc))
                {
                    if (trans.Start("Renumbering Parks") == TransactionStatus.Started)
                    {
                        if (!RenumberParks())
                        {
                            throw new Exception("Cannot Have an Empty Park Number");
                        }

                        trans.Commit();
                    }
                }
            }
        }

        //pop up a task dialog to get information from the user regarding the renumbering option (by selection or based on room insertion)
        private TaskDialogResult renumberOption()
        {
            TaskDialogResult result;

            try
            {
                TaskDialog renumberOptionDialog = new TaskDialog("KA Arch Application | Car Park Renumbering");
                renumberOptionDialog.TitleAutoPrefix = false;
                renumberOptionDialog.MainInstruction = "Renumber Options";
                //add commandlinks
                renumberOptionDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Renumber Parks in a Selected Order");
                renumberOptionDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "Insert a Park Number and Renumber Affected Parks");
                //show the task dialog
                result = renumberOptionDialog.Show();
            }
            catch (Exception x)
            {
                throw new Exception(x.Message);
            }


            return result;
        }

        //pop up a taks dialog to get information about the selection method
        private TaskDialogResult renumberSelection()
        {
            TaskDialogResult result;

            try
            {
                TaskDialog renumberSelectionDialog = new TaskDialog("KA Arch Application | Car Park Renumbering");
                renumberSelectionDialog.TitleAutoPrefix = false;
                renumberSelectionDialog.MainInstruction = "Renumber Selection Method";
                //add commandlinks
                renumberSelectionDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Pick Individual Park or Park Tags");
                renumberSelectionDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "Window Select Parks or Park Tags");
                //show the task dialog
                result = renumberSelectionDialog.Show();
            }
            catch (Exception x)
            {
                throw new Exception(x.Message);
            }

            return result;
        }

        //pick parks individually
        private void pickParksIndividual(List<CarParkObject> parks)
        {
            try
            {
                //filter parks and park tags
                ParkSelectionFilter parkFilter = new ParkSelectionFilter();
                //pick objects
                Reference r = uiDoc.Selection.PickObject(ObjectType.Element, parkFilter, "Pick Park or Park Tag to renumber, based on the selection order");

                while (r != null)
                {
                    Element current = doc.GetElement(r);

                    if (current is Room)
                    {
                        addParkToList((current as Room), parks);
                    }
                    else if (current is RoomTag)
                    {
                        addParkToList((current as RoomTag).Room, parks);
                    }
                    try
                    {
                        r = uiDoc.Selection.PickObject(ObjectType.Element, parkFilter, "Pick Park or Park Tag to renumber, based on the selection order");
                    }
                    catch (Exception)
                    {
                        r = null;
                    }
                }


            }

            catch (Exception x)
            {
                throw new OperationCanceledException(x.Message);
            }

        }


        //pic parks by window
        private void pickParksByWindow(List<CarParkObject> parks)
        {

            try
            {
                //filter parks and park tags
                ParkSelectionFilter parkFilter = new ParkSelectionFilter();
                //pick objects
                IList<Element> pickedObjects = uiDoc.Selection.PickElementsByRectangle(parkFilter, "pick parks by rectangle");
                for (int i = 0; i < pickedObjects.Count; i++)
                {
                    Element current = pickedObjects[i];

                    if (current is Room)
                    {
                        addParkToList((current as Room), parks);
                    }
                    else if (current is RoomTag)
                    {
                        addParkToList((current as RoomTag).Room, parks);
                    }
                }
            }

            catch (Exception x)
            {
                throw new OperationCanceledException(x.Message);
            }
        }

        private void addParkToList(Room r, List<CarParkObject> list)
        {
            try
            {
                CarParkObject obj = new CarParkObject(r);
                bool exists = false;
                //compare based on the ID
                foreach (CarParkObject temp in list)
                {
                    if (temp.ID == obj.ID)
                        exists = true;
                }
                //add if does not exist
                if (!exists)
                {
                    list.Add(obj);
                }

            }
            catch (Exception x)
            {
                throw new Exception("Error adding Car Park to the list: \"" + r.Name + "\" to the list\n" + x.Message);
            }
        }

        //renumber the parks
        private bool RenumberParks()
        {
            try
            {
                // Find all views in the document by using category filter
                ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Parking);
                // Apply the filter to the elements in the active document,
                FilteredElementCollector collector = new FilteredElementCollector(doc);
                FilteredElementIterator iter = collector.WherePasses(filter).GetElementIterator();

                iter.Reset();
                //Iterate through the parks in the document
                while (iter.MoveNext())
                {

                    Room CurrentPark = iter.Current as Room;
                    //The selected instance is a door
                    if (CurrentPark != null)
                    {

                        foreach (CarParkObject x in allParks)
                        {
                            if (x.ID == CurrentPark.Id.IntegerValue.ToString())
                            {
                                if (x.NewNumber == "")
                                {
                                    return false;
                                }
                                else
                                {
                                    //Renumber the parks
                                    CurrentPark.Number = x.NewNumber;

                                    // modified by Ramzi Al Halaby on 06-06-2015 while upgrading to Revit 2016
                                    // ***********************************************************************
                                    CurrentPark.LookupParameter("FN").Set(x.LevelNumber);
                                    CurrentPark.LookupParameter("DP").Set(x.DepartmentNumber);
                                    CurrentPark.LookupParameter("RN").Set(x.RoomNum);
                                    //CurrentPark.get_Parameter("FN").Set(x.LevelNumber);
                                    //CurrentPark.get_Parameter("DP").Set(x.DepartmentNumber);
                                    //CurrentPark.get_Parameter("RN").Set(x.RoomNum);


                                }
                            }
                        }
                    }
                }
                return true;
            }


            catch (Exception x)
            {
                throw new Exception("Error in renumbering\n" + x.Message);

            }
        }


        //renumber parks based on the insertion alg.
        private void renumberInsPark()
        {
            RetreiveallParks();
            if (allParks.Count == 0)
                throw new Exception("No parks Available");
            allParks.Sort();
            Room selected = null;
            if (PickTheParkToBeInserted(ref selected))
            {
                int index = GetIndexOfSelectedPark(selected);
                if (index != -1)
                {
                    CarParkInsert insertDisplay = new CarParkInsert(ref allParks, uiApp, index);
                    if (insertDisplay.ShowDialog() == DialogResult.OK)
                    {
                        using (Transaction trans = new Transaction(doc))
                        {
                            if (trans.Start("Renumbering parks") == TransactionStatus.Started)
                            {
                                if (!RenumberParks())
                                {
                                    throw new Exception("Cannot Have an Empty Park Number");
                                }
                                trans.Commit();
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception("Selected Park is invalid");
                }
            }
        }


        //retrieve all parks in the view
        private void RetreiveallParks()
        {
            try
            {
                // Find all views in the document by using category filter
                ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Parking);
                // Apply the filter to the elements in the active document,
                FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id);
                FilteredElementIterator iter = collector.WherePasses(filter).GetElementIterator();
                iter.Reset();
                //Iterate through the parks in the document
                while (iter.MoveNext())
                {
                    Room CurrentPark = iter.Current as Room;
                    if (CurrentPark != null)
                    {
                        CarParkObject CurrentCarParkObject = new CarParkObject(CurrentPark);
                        allParks.Add(CurrentCarParkObject);
                    }
                }
            }

            catch (Exception)
            {
                TaskDialog.Show("KA Arch | parks Renumbering", "Error in Retreiving parks from the Revit Model!!", TaskDialogCommonButtons.Ok);
            }
        }

        //pick the park to be inserted
        private bool PickTheParkToBeInserted(ref Room SelectedPark)
        {
            try
            {
                Reference PickedObject = uiDoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new ParkSelectionFilter(), "Pick the park to be inserted for renumbering.");
                //***********************************************************************************
                // Modified by Ramzi on 08-06-2015 (changed method for getting current selected park)
                // **********************************************************************************
                //if (PickedObject.Element != null)
                if (doc.GetElement(PickedObject) != null)
                {
                    //Element Current = PickedObject.Element;
                    Element Current = doc.GetElement(PickedObject);

                    if (Current is Room)
                    {
                        SelectedPark = Current as Room;
                    }

                    else if (Current is RoomTag)
                    {
                        SelectedPark = (Current as RoomTag).Room;
                    }

                    else
                    {
                        TaskDialog.Show("KA Arch App | parks Renumbering", "The Selected item is not a Park!", TaskDialogCommonButtons.Ok);
                        return false;
                    }
                }

                return true;
            }
            catch (Exception x)
            {
                throw new OperationCanceledException(x.Message);
            }
        }

        //Get the index of the selected park to be inserted.
        private int GetIndexOfSelectedPark(Room SelectedPark)
        {
            try
            {
                int index = -1;
                for (int i = 0; i < allParks.Count; i++)
                {
                    if (allParks[i].ID == SelectedPark.Id.IntegerValue.ToString())
                    {
                        index = i;
                        break;
                    }
                }
                return index;
            }

            catch (Exception)
            {
                TaskDialog.Show("KA Arch App | parks Renumbering", "The park doesn't exist in the list..", TaskDialogCommonButtons.Ok);
                return -1;
            }

        }


        #endregion
    }

    //implements the ISelection filter for parks and park tags
    public class ParkSelectionFilter : ISelectionFilter
    {
        #region ISelectionFilter Members

        public bool AllowElement(Element elem)
        {
            if (elem.Category.Name == "Rooms" || elem.Category.Name == "Room Tags")
            {
                return true;
            }
            return false;
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            return true;
        }

        #endregion
    }

}

***************************************************************************

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

Anonymous
Not applicable

Thanks for sharing this code!

0 Likes
Message 3 of 5

andrew_parrella
Autodesk
Autodesk

Hi Rahalabi,

Parking components are category OST_Parking, I think you have to access it through the category. Hope that helps.

Andy



Andy P

Principal QA Analyst - Revit

0 Likes
Message 4 of 5

rahalabi
Participant
Participant

I have used it as shown below:

*************************************

  private bool RenumberParks()
        {
            try
            {
                // Find all views in the document by using category filter
                ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Parking);
                // Apply the filter to the elements in the active document,
                FilteredElementCollector collector = new FilteredElementCollector(doc);
                FilteredElementIterator iter = collector.WherePasses(filter).GetElementIterator();

                iter.Reset();
                //Iterate through the parks in the document
                while (iter.MoveNext())
                {

                    Room CurrentPark = iter.Current as Room;
                    //The selected instance is a door
                    if (CurrentPark != null)
                    {

                        foreach (CarParkObject x in allParks)
                        {
                            if (x.ID == CurrentPark.Id.IntegerValue.ToString())
                            {
                                if (x.NewNumber == "")
                                {
                                    return false;
                                }
                                else
                                {
                                    //Renumber the parks
                                    CurrentPark.Number = x.NewNumber;

                                    // modified by Ramzi Al Halaby on 06-06-2015 while upgrading to Revit 2016
                                    // ***********************************************************************
                                    CurrentPark.LookupParameter("FN").Set(x.LevelNumber);
                                    CurrentPark.LookupParameter("DP").Set(x.DepartmentNumber);
                                    CurrentPark.LookupParameter("RN").Set(x.RoomNum);
                                    //CurrentPark.get_Parameter("FN").Set(x.LevelNumber);
                                    //CurrentPark.get_Parameter("DP").Set(x.DepartmentNumber);
                                    //CurrentPark.get_Parameter("RN").Set(x.RoomNum);


                                }
                            }
                        }
                    }
                }
                return true;
            }


            catch (Exception x)
            {
                throw new Exception("Error in renumbering\n" + x.Message);

            }
        }

*************************************

but if I want to  create something like this

 

Room CurrentPark = iter.Current as Room;

 

how will I do it?

***************************************

0 Likes
Message 5 of 5

Revitalizer
Advisor
Advisor

Hi,

 

it's a FamilyInstance (of BuiltInCategory.OST_Parking, as Andrew already pointed out).

 

You may use RevitLookup to find that out by yourself.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes