// Назначение стадии для спецификации по заданному имени стадии
        static void SetPhaseByName(ViewSchedule viewSchedule, string phaseName)
        {
            Document doc = viewSchedule.Document;

            FilteredElementCollector phaseCollector = new FilteredElementCollector(doc);

            phaseCollector.OfClass(typeof(Phase));

            // Нужно намутить проверку на null,
            // хотя в Command есть cath {}
            Phase phase = (from item in phaseCollector
                           where item.Name.Equals(phaseName)
                           select item).First() as Phase;

            viewSchedule.get_Parameter(BuiltInParameter.VIEW_PHASE).Set(phase.Id);

            FilteredElementCollector phaseFilterCollector =
                new FilteredElementCollector(doc);

            phaseFilterCollector.OfClass(typeof(PhaseFilter));

            // Нужно намутить проверку на null,
            // хотя в Command есть cath {}
            PhaseFilter phaseFilter = (from item in phaseFilterCollector
                                       where item.Name.Equals("Текущая и предыдущая")
                                       select item).First() as PhaseFilter;

            viewSchedule.get_Parameter(BuiltInParameter.VIEW_PHASE_FILTER)
            .Set(phaseFilter.Id);
        }
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;

            _itemizar    = false;
            _linked      = false;
            _numerar     = true;
            _prefijo     = "-";
            _sufijo      = "";
            _marca       = false;
            _comentarios = false;
            _descripcion = true;
            _modelo      = false;
            _marcaTipo   = false;

            System.Windows.Forms.DialogResult resultado = (new frmCrearTablas(uidoc.Document)).ShowDialog();

            if (resultado == System.Windows.Forms.DialogResult.OK)
            {
                List <ViewSchedule> tablas = new List <ViewSchedule>();

                // Creamos y abrimos una transacción
                using (Transaction t = new Transaction(doc, "Crear Tablas"))
                {
                    t.Start();
                    // Ordenar Categorias
                    Tools.categorias = Tools.categorias.OrderBy(x => x.Name).ToList();

                    int count     = 1;
                    int countMats = 1;
                    try
                    {
                        // Crear una Tabla para cada Categoría
                        foreach (Category cat in Tools._selectedCategories)
                        {
                            string nombre = "";
                            if (_numerar)
                            {
                                nombre += count.ToString();
                            }
                            if (_prefijo != "")
                            {
                                nombre += _prefijo;
                            }
                            nombre += " " + cat.Name;
                            if (_sufijo != "")
                            {
                                nombre += _sufijo;
                            }

                            ViewSchedule tabla = ViewSchedule.CreateSchedule(doc, cat.Id);
                            tabla.Name = nombre;
                            count++;
                            // ver 1.4 MEP
                            bool mep        = false;
                            bool pipeSystem = false;
                            bool ductSystem = false;
                            if (cat.Id.IntegerValue == -2008044) //Pipes
                            {
                                mep        = true;
                                pipeSystem = true;
                            }
                            if (cat.Id.IntegerValue == -2008049) //PipeFittings
                            {
                                mep        = true;
                                pipeSystem = true;
                            }
                            if (cat.Id.IntegerValue == -2008055) //Pipe Accesories
                            {
                                mep        = true;
                                pipeSystem = true;
                            }
                            if (cat.Id.IntegerValue == -2008050) //FlexPipe
                            {
                                //mep = true; // DEBE SER TAMAÑO TOTAL
                                pipeSystem = true;
                            }
                            if (cat.Id.IntegerValue == -2008000) // Duct
                            {
                                mep        = true;
                                ductSystem = true;
                            }
                            if (cat.Id.IntegerValue == -2008010) // DuctFitting
                            {
                                mep        = true;
                                ductSystem = true;
                            }
                            if (cat.Id.IntegerValue == -2008016) // Duct Accesories
                            {
                                mep        = true;
                                ductSystem = true;
                            }
                            if (cat.Id.IntegerValue == -2008020) // FlexDuct
                            {
                                //mep = true; // DEBE SER TAMAÑO TOTAL
                                ductSystem = true;
                            }
                            if (cat.Id.IntegerValue == -2008132) // Conduit
                            {
                                mep = true;
                            }
                            if (cat.Id.IntegerValue == -2008128) // ConduitFitting
                            {
                                mep = true;
                            }
                            if (cat.Id.IntegerValue == -2008130) // CableTray
                            {
                                mep = true;
                            }
                            if (cat.Id.IntegerValue == -2008126) // CableTrayFittings
                            {
                                mep = true;
                            }
                            Tools.FillSchedule(tabla, _itemizar, _linked, mep, pipeSystem, ductSystem);
                            // Phase Filter
                            Parameter tablaFilter = tabla.get_Parameter(BuiltInParameter.VIEW_PHASE_FILTER);
                            tablaFilter.Set(new ElementId(-1));
                            tablas.Add(tabla);
                        }
                        // Create Material Schedules
                        foreach (Category cat in Tools._selectedMatCategories)
                        {
                            int createMat = 0;

                            if (cat.HasMaterialQuantities)// && cat.Material != null)
                            {
                                if (cat.Id == new ElementId(BuiltInCategory.OST_Walls))
                                {
                                    createMat = 1;
                                }
                                if (cat.Id == new ElementId(BuiltInCategory.OST_Floors))
                                {
                                    createMat = 1;
                                }
                                if (cat.Id == new ElementId(BuiltInCategory.OST_Roofs))
                                {
                                    createMat = 1;
                                }
                                if (cat.Id == new ElementId(BuiltInCategory.OST_Ceilings))
                                {
                                    createMat = 1;
                                }
                                if (cat.Id == new ElementId(BuiltInCategory.OST_BuildingPad))
                                {
                                    createMat = 1;
                                }

                                if (createMat == 1)
                                {
                                    string nombre = "M";
                                    if (_numerar)
                                    {
                                        nombre += countMats.ToString();
                                    }
                                    if (_prefijo != "")
                                    {
                                        nombre += _prefijo;
                                    }
                                    nombre += " " + cat.Name;
                                    if (_sufijo != "")
                                    {
                                        nombre += _sufijo;
                                    }

                                    ViewSchedule tablaMat = ViewSchedule.CreateMaterialTakeoff(doc, cat.Id);
                                    tablaMat.Name = nombre + " (Materiales)";
                                    // Phase Filter
                                    Parameter tablaFilter = tablaMat.get_Parameter(BuiltInParameter.VIEW_PHASE_FILTER);
                                    tablaFilter.Set(new ElementId(-1));
                                    countMats++;
                                    Tools.FillScheduleMaterial(tablaMat, _itemizar, _linked);
                                    tablas.Add(tablaMat);
                                }
                            }
                        }
                        t.Commit();
                    }
                    catch (Exception ex)
                    {
                        TaskDialog.Show("Universo BIM", "Error: " + ex.Message);
                        t.RollBack();
                    }
                }
                TaskDialog.Show("Universo BIM", "Se han creado " + tablas.Count + " Tablas");
            }

            return(Result.Succeeded);
        }