public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Debug.Listeners.Clear();
            Debug.Listeners.Add(new RbsLogger.Logger("FloorAreaRebar"));
            Debug.WriteLine("Floor reinforcement start");
            App.ActivateConfigFolder();
            Document     doc    = commandData.Application.ActiveUIDocument.Document;
            Selection    sel    = commandData.Application.ActiveUIDocument.Selection;
            List <Floor> floors = new List <Floor>();

            foreach (ElementId id in sel.GetElementIds())
            {
                Element elem = doc.GetElement(id);
                if (elem is Floor)
                {
                    floors.Add(elem as Floor);
                }
            }
            Debug.WriteLine("Selected floors: " + floors.Count);
            if (floors.Count == 0)
            {
                message = "Выберите плиты для армирования";
                return(Result.Failed);
            }

            ElementId areaTypeId = SupportDocumentGetter.GetDefaultArea(doc).Id;

            Debug.WriteLine("AreaTypeId: " + areaTypeId.IntegerValue);
            RebarCoverType zeroCover = SupportDocumentGetter.GetRebarCoverType(doc, 0);

            Debug.WriteLine("Zero cover type id: " + zeroCover.Id.IntegerValue);

            List <string> rebarTypes = SupportDocumentGetter.GetRebarTypes(doc);

            Debug.WriteLine("Rebar types: " + rebarTypes.Count);

            if (floors.Count == 0)
            {
                message = "Нет подходящих перекрытий для армирования";
                return(Result.Failed);
            }

            foreach (Floor fl in floors)
            {
                Parameter floorIsStructuralParam = fl.get_Parameter(BuiltInParameter.FLOOR_PARAM_IS_STRUCTURAL);
                if (floorIsStructuralParam != null)
                {
                    if (floorIsStructuralParam.AsInteger() != 1)
                    {
                        elements.Insert(fl);
                    }
                }
            }
            Debug.WriteLine("Structural floors: " + (floors.Count - elements.Size));
            if (elements.Size > 0)
            {
                message = "Найдены не несущие плиты, армирование не будет выполнено";
                return(Result.Failed);
            }

            string         floorPath  = System.IO.Path.Combine(App.localFolder, "floor.xml");
            RebarInfoFloor rif        = null;
            XmlSerializer  serializer = new XmlSerializer(typeof(RebarInfoFloor));

            if (System.IO.File.Exists(floorPath))
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(floorPath))
                {
                    try
                    {
                        rif = (RebarInfoFloor)serializer.Deserialize(reader);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Unable to deserialize, create new one: " + ex.Message);
                        rif = RebarInfoFloor.GetDefault(doc);
                    }
                    if (rif == null)
                    {
                        Debug.WriteLine("Deserialize error: " + floorPath);
                        throw new Exception("Не удалось десериализовать: " + floorPath);
                    }
                }
            }
            else
            {
                Debug.WriteLine("No xml file, create new one");
                rif = RebarInfoFloor.GetDefault(doc);
            }

            DialogWindowFloor form = new DialogWindowFloor(rif, rebarTypes);

            form.ShowDialog();
            if (form.DialogResult != System.Windows.Forms.DialogResult.OK)
            {
                Debug.WriteLine("Cancelled by user");
                return(Result.Cancelled);
            }

            if (File.Exists(floorPath))
            {
                Debug.WriteLine("File is deleted: " + floorPath);
                File.Delete(floorPath);
            }
            using (FileStream writer = new FileStream(floorPath, FileMode.OpenOrCreate))
            {
                serializer.Serialize(writer, form.rif);
                Debug.WriteLine("New file is created: " + floorPath);
            }

            List <string> rebarMessages = new List <string>();

            using (Transaction t = new Transaction(doc))
            {
                t.Start("Армирование плит");

                foreach (Floor floor in floors)
                {
                    Debug.WriteLine("Current reinforcement floor: " + floor.Id.IntegerValue);
                    List <string> curRebarMessages = RebarWorkerFloor.Generate(doc, floor, rif, areaTypeId);
                    rebarMessages.AddRange(curRebarMessages);
                }
                t.Commit();
            }

            if (rebarMessages.Count > 0)
            {
                foreach (string msg in rebarMessages)
                {
                    message += msg + System.Environment.NewLine;
                }
                Debug.WriteLine("Errors: " + message);
                return(Result.Failed);
            }
            Debug.WriteLine("All done");
            return(Result.Succeeded);
        }
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Debug.Listeners.Clear();
            Debug.Listeners.Add(new RbsLogger.Logger("WallAreaRebar"));
            Debug.WriteLine("Wall reinforcement start");

            App.ActivateConfigFolder();

            Document doc = commandData.Application.ActiveUIDocument.Document;

            Selection   sel   = commandData.Application.ActiveUIDocument.Selection;
            List <Wall> walls = new List <Wall>();


            foreach (ElementId id in sel.GetElementIds())
            {
                Element elem = doc.GetElement(id);

                if (elem is Wall)
                {
                    walls.Add(elem as Wall);
                }
            }
            if (walls.Count == 0)
            {
                message = "Предварительно выберите стены для армирования";
                return(Result.Failed);
            }

            Debug.WriteLine("Selected walls count: " + walls.Count.ToString());
            foreach (Wall w in walls)
            {
                Parameter isStructural = w.get_Parameter(BuiltInParameter.WALL_STRUCTURAL_SIGNIFICANT);
                if (isStructural == null)
                {
                    continue;
                }
                if (isStructural.AsInteger() != 1)
                {
                    elements.Insert(w);
                }
            }
            if (elements.Size > 0)
            {
                message = "Найдены не несущие стены, армирование не может быть выполнено";
                Debug.WriteLine("Non-structural walls were found");
                return(Result.Failed);
            }

            ElementId      areaTypeId         = SupportDocumentGetter.GetDefaultArea(doc).Id;
            RebarCoverType zeroCover          = SupportDocumentGetter.GetRebarCoverType(doc, 0);
            List <string>  rebarTypes         = SupportDocumentGetter.GetRebarTypes(doc);
            List <string>  rebarTypes2        = rebarTypes.ToList();
            bool           wallsHaveRebarInfo = SupportDocumentGetter.CheckWallsHaveRebarInfo(walls);


            RebarInfoWall riw      = new RebarInfoWall(); //RebarInfoWall.GetDefault(doc);
            string        wallPath = System.IO.Path.Combine(App.localFolder, "wall.xml");

            Debug.WriteLine("Try to deserialize xml: " + wallPath);
            XmlSerializer serializer = new XmlSerializer(typeof(RebarInfoWall));

            if (System.IO.File.Exists(wallPath))
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(wallPath))
                {
                    try
                    {
                        riw = (RebarInfoWall)serializer.Deserialize(reader);
                    }
                    catch
                    {
                        Debug.WriteLine("Deserialize fault!");
                    }
                }
            }

            if (wallsHaveRebarInfo)
            {
                //TaskDialog.Show("Внимание!", "Армирование будет выполнено по данным, указанным в стенах, без вывода диалогового окна.");
                Debug.WriteLine("DialogWindow for auto-reinforcement");
                DialogWindowWallAuto dialogWallAuto = new DialogWindowWallAuto(riw);
                dialogWallAuto.ShowDialog();
                if (dialogWallAuto.DialogResult != System.Windows.Forms.DialogResult.OK)
                {
                    return(Result.Cancelled);
                }
                riw = dialogWallAuto.rebarInfo;
                Debug.WriteLine("RebarInfo created");
            }
            else
            {
                Debug.WriteLine("Dialog window for manual-reinforcement");
                DialogWindowWall form = new DialogWindowWall(riw, rebarTypes, rebarTypes2);
                form.ShowDialog();
                if (form.DialogResult != System.Windows.Forms.DialogResult.OK)
                {
                    return(Result.Cancelled);
                }
            }

            Debug.Write("Delete xml file and rewrite: " + wallPath);
            if (File.Exists(wallPath))
            {
                File.Delete(wallPath);
            }
            using (FileStream writer = new FileStream(wallPath, FileMode.OpenOrCreate))
            {
                serializer.Serialize(writer, riw);
            }
            Debug.WriteLine("... Success!");


            using (Transaction t = new Transaction(doc))
            {
                t.Start("Армирование стен");
                Debug.WriteLine("Start transaction");

                foreach (Wall wall in walls)
                {
                    if (wallsHaveRebarInfo)
                    {
                        Debug.WriteLine("Start auto-reinforcement");
                        RebarInfoWall newRiw = new RebarInfoWall(doc, wall);
                        newRiw.topOffset    = riw.topOffset;
                        newRiw.bottomOffset = riw.bottomOffset;

                        newRiw.horizontalAddInterval         = riw.horizontalAddInterval;
                        newRiw.horizontalAdditionalStepSpace = riw.horizontalAdditionalStepSpace;
                        newRiw.verticalSectionText           = riw.verticalSectionText;
                        newRiw.horizontalSectionText         = riw.horizontalSectionText;
                        newRiw.verticalOffset = riw.verticalOffset;

                        newRiw.useUnification         = riw.useUnification;
                        newRiw.lengthsUnification     = riw.lengthsUnification;
                        newRiw.autoVerticalFreeLength = riw.autoVerticalFreeLength;

                        RebarWorkerWall.GenerateRebar(doc, wall, newRiw, zeroCover, areaTypeId);
                    }
                    else
                    {
                        Debug.WriteLine("Start manual reinforcement");
                        RebarWorkerWall.GenerateRebar(doc, wall, riw, zeroCover, areaTypeId);
                    }
                }
                t.Commit();
                Debug.WriteLine("Finish transaction");
            }
            return(Result.Succeeded);
        }