private void RefreshComponentsValues()
 {
     retb_widthWindow.Value = AcadTools.DoubleToFormattedString(
         _searchWindowSize.Width);
     retb_heightWindow.Value = AcadTools.DoubleToFormattedString(
         _searchWindowSize.Height);
 }
        public bool CreateNewFolder(string pathFolder)
        {
            bool result = false;

            var drawingName = Path.GetFileNameWithoutExtension(AcadTools.GetAbsolutePathWithName());

            var folderName = drawingName + "_" + GenerateNameForFolder();

            if ((folderName == "default"))
            {
                result = true;
                return(result);
            }
            else
            {
                if (Directory.Exists(pathFolder + folderName))
                {
                    MessageBox.Show(@"Такая папка уже существует!");
                    return(false);
                }

                result = true;
                var directoryInfo = Directory.CreateDirectory(pathFolder);
                return(result);
            }
        }
        public static SectionData[] ExtractSectionsData(Size searchWindowSize)
        {
            var axisPoints   = GetListBlocksByPrefix("axisPoint_");
            var heightPoints = GetListBlocksByPrefix("heightPoint_");
            var redPoints    = GetListBlocksByPrefix("redPoint_");
            var blackPoints  = GetListBlocksByPrefix("blackPoint_");

            var data = new SectionData[0];

            if (axisPoints.Count == 0)
            {
                return(data);
            }

            return(axisPoints.
                   Select(axisPoint =>
            {
                var axisPointPosition = AcadTools.GetBlockPosition(axisPoint);

                var heightSectionPoints = heightPoints
                                          .Where(point => CheckBlockInWindow(point, axisPointPosition, searchWindowSize));
                if (heightSectionPoints.Count() == 0)
                {
                    return null;
                }
                var heightSectionPoint = heightSectionPoints
                                         .OrderBy(point => (AcadTools.GetBlockPosition(point) - axisPointPosition).Length)
                                         .First();

                var sectionOrigin = new Point3d(
                    axisPointPosition.X,
                    AcadTools.GetBlockPosition(heightSectionPoint).Y,
                    .0);

                var redSectionPoints = redPoints
                                       .Where(point => CheckBlockInWindow(point, sectionOrigin, searchWindowSize))
                                       .ToList();
                if (redSectionPoints.Count < 2)
                {
                    return null;
                }

                var blackSectionPoints = blackPoints
                                         .Where(point => CheckBlockInWindow(point, sectionOrigin, searchWindowSize))
                                         .ToList();

                return new SectionData()
                {
                    AxisPoint = axisPoint,
                    HeightPoint = heightSectionPoint,
                    RedPoints = redSectionPoints,
                    BlackPoints = blackSectionPoints
                };
            })
                   .Where(sectionData => sectionData != null)
                   .ToArray());
        }
        public static bool CheckBlockInWindow(BlockTableRecord block, Point3d origin, Size windowSize)
        {
            var blockLocalPos = AcadTools.GetBlockPosition(block) - origin;

            var windowWidth  = windowSize.Width;
            var windowHeight = windowSize.Height;

            return(-windowWidth / 2.0 <= blockLocalPos.X &&
                   blockLocalPos.X < windowWidth / 2.0 &&
                   -windowHeight / 2.0 <= blockLocalPos.Y &&
                   blockLocalPos.Y < windowHeight / 2.0);
        }
        public string GenerateNameForFolder()
        {
            var drawingName = Path.GetFileNameWithoutExtension(AcadTools.GetAbsolutePathWithName());

            const string folderName = "default";

            if (string.IsNullOrEmpty(drawingName))
            {
                return(folderName);
            }
            else
            {
                return(DateTime.Now.ToString("dd.MM.yyyy_hh.mm.ss"));
            }
        }
        private void StartProcessForCreateDxfFile(string pathSettingsXml)
        {
            try
            {
                var process = new Process();

                var processStartInfo = new ProcessStartInfo()
                {
                    FileName = Path.Combine(AcadTools.GetAcadLocation(),
                                            @"SectionConverterPlugin\SectionsBlueprintGenerator\SectionsBlueprintGenerator.exe"),
                    Arguments = "\"" + pathSettingsXml + "\""
                };

                process = Process.Start(processStartInfo);

                process.WaitForExit();
            }
            catch
            {
                MessageBox.Show("Failed to execute");
            }
        }
        public string GenerateNameForBlueprintTsvFile(string fileName)
        {
            string tsvName = Path.GetFileNameWithoutExtension(AcadTools.GetAbsolutePathWithName());

            return(tsvName + "_blueprint_" + fileName);
        }
        public string GenerateNameForDxfFile(string fileName)
        {
            string dfxName = Path.GetFileNameWithoutExtension(AcadTools.GetAbsolutePathWithName());

            return(dfxName + "_" + "blueprint_" + fileName);
        }
        }  //button

        public void GenerateDataXmlFile(
            string fileName,
            Func <Vector3d> getFactPositionNoizeAddition = null)
        {
            bool factPossEnabled = true;

            if (getFactPositionNoizeAddition == null)
            {
                getFactPositionNoizeAddition = () => new Vector3d();
                factPossEnabled = false;
            }

            var pluginSettings = PluginSettings.GetInstance();
            var sectionMaxSize = pluginSettings.SectionMaxSize;

            var sectionsData = RoadSectionParametersExtractor
                               .ExtractSectionsData(sectionMaxSize);

            Func <double, double, string> CoordsToString = (x, y) =>
                                                           String.Format("{0} {1}",
                                                                         AcadTools.DoubleToFormattedString(x),
                                                                         AcadTools.DoubleToFormattedString(y));

            Func <Point3d, Point3d, int, XElement> GetPointElement =
                (projPos, factPos, number) =>
                new XElement(
                    "point",
                    new XElement(
                        "proj_position",
                        CoordsToString(projPos.X, projPos.Y)),
                    new XElement(
                        "fact_position",
                        CoordsToString(factPos.X, factPos.Y)),
                    new XElement("number", number));

            var extractedSectionDataDoc = new XDocument(
                new XElement(
                    "road_sections",
                    new XAttribute("fact_enabled", factPossEnabled),
                    sectionsData
                    .Select(sd =>
                            new XElement(
                                "road_section",
                                new XElement(
                                    "staEq",
                                    AcadTools.DoubleToFormattedString(
                                        RoadSectionParametersExtractor.GetStationFromPointBlock(sd.AxisPoint))),
                                new XElement(
                                    "origin",
                                    CoordsToString(
                                        AcadTools.GetBlockPosition(sd.AxisPoint).X,
                                        AcadTools.GetBlockPosition(sd.HeightPoint).Y)),
                                new XElement("origin_height",
                                             AcadTools.DoubleToFormattedString(
                                                 RoadSectionParametersExtractor.GetHeightFromPointBlock(sd.HeightPoint))),

                                new XElement(
                                    "red_points",
                                    sd.RedPoints
                                    .Select(redPoint =>
                                            GetPointElement(
                                                AcadTools.GetBlockPosition(redPoint),
                                                AcadTools.GetBlockPosition(redPoint) + getFactPositionNoizeAddition(),
                                                RoadSectionParametersExtractor.GetPointNumberFromPointBlock(redPoint)))),

                                new XElement(
                                    "black_points",
                                    sd.BlackPoints
                                    .Select(blackPoint =>
                                            GetPointElement(
                                                AcadTools.GetBlockPosition(blackPoint),
                                                AcadTools.GetBlockPosition(blackPoint) + getFactPositionNoizeAddition(),
                                                RoadSectionParametersExtractor.GetPointNumberFromPointBlock(blackPoint))))
                                ))));

            extractedSectionDataDoc.Save(fileName);
        }
        private void btn_ExportPoints_Click(object sender, EventArgs e)
        {
            ActiveControl = btn_ExportPoints;

            if (_dataReverted == true)
            {
                _dataReverted = false;

                MessageBox.Show(@"Invalid input");

                return;
            }

            #region Bad Blocks Cleanup
            string[] prefixes = { "axisPoint_", "heightPoint_", "redPoint_", "blackPoint_" };
            int      recordsErased;
            int      referencesErased;

            AcadTools.ClenUpDatabaseFromBadBlocks(
                prefixes,
                out recordsErased,
                out referencesErased);

            if (referencesErased != 0)
            {
                MessageBox.Show("Копий блоков удалено: " + referencesErased);
            }
            #endregion

            using (var dialog = new SaveFileDialog())
            {
                dialog.InitialDirectory = AcadTools.GetAbsolutePath();
                dialog.Title            = @"Экспортировать в...";

                string saveTime = GenerateNameForFolder();

                dialog.FileName = Path.GetFileNameWithoutExtension(AcadTools.GetAbsolutePathWithName()) +
                                  "_" + saveTime;

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string savePath = dialog.FileName.Replace(GenerateNameForFolder(), "");

                    CreateNewFolder(savePath);

                    Func <Vector3d> GetFactPossNoizeAddition = null;
                    if (_factPosEnable)
                    {
                        var randomGen = new Random();
                        var factPossNoizeAmplitude =
                            _factPosNoizeUpperBound - _factPosNoizeLowerBound / 2.0;
                        Func <double> GetNoize = () =>
                        {
                            return(_factPosNoizeLowerBound +
                                   factPossNoizeAmplitude * randomGen.NextDouble());
                        };

                        GetFactPossNoizeAddition = () => new Vector3d(GetNoize(), GetNoize(), .0);
                    }

                    GenerateDataXmlFile(savePath + "\\data.xml", GetFactPossNoizeAddition);

                    string savePathNameSettings = savePath + "\\settings.xml";

                    GenerateSettingXmlFile(
                        savePathNameSettings,
                        savePath + "\\data.xml",
                        savePath + "\\" + GenerateNameForListTsvFile(saveTime) + ".tsv",
                        Path.Combine(AcadTools.GetAcadLocation(), "SectionConverterPlugin\\SectionsBlueprintGenerator\\BlueprintTemplate" + ".dxf")
                        , savePath + "\\" + GenerateNameForDxfFile(saveTime) + ".dxf"
                        );

                    StartProcessForCreateTsvFile(savePathNameSettings);

                    StartProcessForCreateDxfFile(savePathNameSettings);
                }

                this.ActiveControl = btn_ExportPoints;

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }  //button