Beispiel #1
0
        /// <summary>
        /// 为实体赋值-excel模式
        /// </summary>
        /// <param name="model"></param>
        private void SetPlotModel_EXCEL(PlotModel model, string filePath, bool recursive = false)
        {
            List <ISheet> sheetList = new List <ISheet>();
            ISheet        sheet;
            FileStream    fs = null;

            try
            {
                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                HSSFWorkbook wk         = new HSSFWorkbook(fs);
                int          sheetCount = wk.NumberOfSheets;
                for (int i = 0; i < sheetCount; i++)
                {
                    string sheetName = wk.GetSheetName(i);
                    sheet = wk.GetSheet(sheetName);
                    sheetList.Add(sheet);
                }
            }
            catch
            {
                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                XSSFWorkbook wk         = new XSSFWorkbook(fs);
                int          sheetCount = wk.NumberOfSheets;
                for (int i = 0; i < sheetCount; i++)
                {
                    string sheetName = wk.GetSheetName(i);
                    sheet = wk.GetSheet(sheetName);
                    sheetList.Add(sheet);
                }
            }
            finally
            {
                fs.Close();
                fs.Dispose();
            }
            try
            {
                model.CoordinateList = new List <CoordinatesModel>();
                model.PlotName       = sheetList[0].GetRow(2).Cells[2].StringCellValue; //地块名称
                model.CbfDBName      = sheetList[0].GetRow(4).Cells[2].StringCellValue; //承包方代表名称

                #region 处理界址点检查记录信息
                List <string> list = new List <string>();
                for (int j = 0; j < sheetList.Count; j++)
                {
                    for (int i = 10; i <= sheetList[j].LastRowNum - 1; i++)
                    {
                        IRow row = sheetList[j].GetRow(i);

                        //当界址点号不是数字时终止循环
                        if (string.IsNullOrEmpty(Regex.Replace(row.Cells[2].StringCellValue.Trim(), @"[^\d|.]+", "")))
                        {
                            break;
                        }
                        var coordinate = new CoordinatesModel();
                        coordinate.OrderNum         = (i - 9).ToString();
                        coordinate.SerialNumber     = row.Cells[0].StringCellValue.Trim();
                        coordinate.BoundaryPointNum = row.Cells[2].StringCellValue.Trim();
                        //对界址点编号中已存在的坐标点 不去重新计算赋值 直接添加入列表
                        if (!list.Contains(coordinate.BoundaryPointNum) && plotDic.ContainsKey(coordinate.BoundaryPointNum) && !recursive)//非递归模式
                        {
                            list.Add(coordinate.BoundaryPointNum);
                            coordinate          = plotDic[coordinate.BoundaryPointNum];
                            coordinate.OrderNum = (i - 9).ToString();
                            model.CoordinateList.Add(coordinate);
                            continue;
                        }

                        coordinate.X = Convert.ToDouble(row.Cells[3].StringCellValue.Trim()).ToString("f3");
                        coordinate.Y = Convert.ToDouble(row.Cells[4].StringCellValue.Trim()).ToString("f3");
                        //先随机 ∆L
                        coordinate.difL       = GetdifL();
                        coordinate.difSquareL = Math.Pow(Convert.ToDouble(coordinate.difL), 2.0).ToString("f3");
                        //再随机 X'
                        coordinate.cX = (Convert.ToDouble(coordinate.X) + MathCode.GetRandomNumber(-Convert.ToDouble(coordinate.difL) / 2, Convert.ToDouble(coordinate.difL) / 2, 3)).ToString("f3");

                        #region 随机三次X取样值
                        bool flagX = true;
                        while (flagX)
                        {
                            coordinate.X1 = (Convert.ToDouble(coordinate.X) + MathCode.GetRandomNumber(-0.75, 0.75, 3)).ToString("f3");
                            coordinate.X2 = (Convert.ToDouble(coordinate.X) + MathCode.GetRandomNumber(-0.75, 0.75, 3)).ToString("f3");
                            coordinate.X3 = (3 * Convert.ToDouble(coordinate.X) - Convert.ToDouble(coordinate.X1) - Convert.ToDouble(coordinate.X2)).ToString("f3");
                            if (Math.Abs(Convert.ToDouble(coordinate.X3) - Convert.ToDouble(coordinate.X)) < 0.8) //超出范围
                            {
                                flagX = false;
                            }
                        }
                        #endregion

                        coordinate.difX = (Convert.ToDouble(coordinate.X) - Convert.ToDouble(coordinate.cX)).ToString("f3");

                        //根据公式求出 ∆y ∆y = 开平方(∆L2-∆x2)  每次随机正负值
                        Random rd = new Random();
                        var    r  = rd.Next(0, 2);
                        coordinate.difY = ((r == 0 ? -1 : 1) * Math.Sqrt(Convert.ToDouble(coordinate.difSquareL) - Math.Pow(Convert.ToDouble(coordinate.difX), 2.0))).ToString("f3");

                        coordinate.cY = (Convert.ToDouble(coordinate.Y) - Convert.ToDouble(coordinate.difY)).ToString("f3");
                        #region 随机三次Y取样值
                        bool flagY = true;
                        while (flagY)
                        {
                            coordinate.Y1 = (Convert.ToDouble(coordinate.Y) + MathCode.GetRandomNumber(-0.75, 0.75, 3)).ToString("f3");
                            coordinate.Y2 = (Convert.ToDouble(coordinate.Y) + MathCode.GetRandomNumber(-0.75, 0.75, 3)).ToString("f3");
                            coordinate.Y3 = (3 * Convert.ToDouble(coordinate.Y) - Convert.ToDouble(coordinate.Y1) - Convert.ToDouble(coordinate.Y2)).ToString("f3");
                            if (Math.Abs(Convert.ToDouble(coordinate.Y3) - Convert.ToDouble(coordinate.Y)) < 0.8) //超出范围
                            {
                                flagY = false;
                            }
                        }
                        #endregion


                        if (!plotDic.ContainsKey(coordinate.BoundaryPointNum))//界址点编号字典添加坐标实体
                        {
                            plotDic.Add(coordinate.BoundaryPointNum, coordinate);
                        }

                        if (!list.Contains(coordinate.BoundaryPointNum))//不把最后一个回归原点的坐标统计入内
                        {
                            list.Add(coordinate.BoundaryPointNum);
                            model.CoordinateList.Add(coordinate);
                        }
                    }
                }

                #endregion

                #region 处理面积检查记录信息
                model.PlotArea = Convert.ToDouble(Regex.Replace(sheetList[0].GetRow(6).Cells[2].StringCellValue.Trim(), @"[^\d|.]+", "")).ToString("f2");
                #region 计算P'
                //计算之前先判断是否有内环
                bool isHaveNei    = false;
                int  bigAreaCount = 0;
                for (int i = 0; i < model.CoordinateList.Count; i++)
                {
                    string serialNumber = model.CoordinateList[i].SerialNumber;
                    if (serialNumber.Contains("内环"))
                    {
                        isHaveNei    = true;
                        bigAreaCount = i;
                        break;
                    }
                }

                string realArea = "";
                if (!isHaveNei) //如果没有内环
                {
                    double[] difXArr = new double[model.CoordinateList.Count];
                    double[] difYArr = new double[model.CoordinateList.Count];
                    for (int i = 0; i < model.CoordinateList.Count; i++)
                    {
                        difXArr[i] = Convert.ToDouble(model.CoordinateList[i].cX);
                        difYArr[i] = Convert.ToDouble(model.CoordinateList[i].cY);
                    }
                    model.PlotCheckArea = MathCode.AoArea(model.CoordinateList.Count, difXArr, difYArr).ToString("f2");

                    for (int i = 0; i < model.CoordinateList.Count; i++)
                    {
                        difXArr[i] = Convert.ToDouble(model.CoordinateList[i].X);
                        difYArr[i] = Convert.ToDouble(model.CoordinateList[i].Y);
                    }
                    realArea = MathCode.AoArea(model.CoordinateList.Count, difXArr, difYArr).ToString("f2");
                }
                else//如果有内环
                {
                    #region 大环
                    double[] difXArr = new double[bigAreaCount];
                    double[] difYArr = new double[bigAreaCount];
                    for (int i = 0; i < bigAreaCount; i++)
                    {
                        difXArr[i] = Convert.ToDouble(model.CoordinateList[i].cX);
                        difYArr[i] = Convert.ToDouble(model.CoordinateList[i].cY);
                    }
                    double bigArea = MathCode.AoArea(bigAreaCount, difXArr, difYArr);//大面积

                    for (int i = 0; i < bigAreaCount; i++)
                    {
                        difXArr[i] = Convert.ToDouble(model.CoordinateList[i].X);
                        difYArr[i] = Convert.ToDouble(model.CoordinateList[i].Y);
                    }
                    double realBigArea = MathCode.AoArea(bigAreaCount, difXArr, difYArr);//真实大面积
                    #endregion

                    #region 小环
                    int      smallAreaCount = model.CoordinateList.Count - bigAreaCount;
                    double[] difXArr2       = new double[smallAreaCount];
                    double[] difYArr2       = new double[smallAreaCount];
                    for (int i = 0; i < smallAreaCount; i++)
                    {
                        difXArr2[i] = Convert.ToDouble(model.CoordinateList[i + bigAreaCount].cX);
                        difYArr2[i] = Convert.ToDouble(model.CoordinateList[i + bigAreaCount].cY);
                    }
                    double smallArea = Math.Abs(MathCode.AoArea(smallAreaCount, difXArr2, difYArr2));//内环面积
                    for (int i = 0; i < smallAreaCount; i++)
                    {
                        difXArr2[i] = Convert.ToDouble(model.CoordinateList[i + bigAreaCount].X);
                        difYArr2[i] = Convert.ToDouble(model.CoordinateList[i + bigAreaCount].Y);
                    }
                    double realSmallArea = Math.Abs(MathCode.AoArea(smallAreaCount, difXArr2, difYArr2));//内环面积
                    #endregion

                    model.PlotCheckArea = (bigArea - smallArea).ToString("f2");
                    realArea            = (realBigArea - realSmallArea).ToString("f2");
                }



                #endregion
                model.DifArea = Math.Abs(Convert.ToDouble(model.PlotArea) - Convert.ToDouble(model.PlotCheckArea)).ToString("f2");
                //R=∆P÷P′×100%
                model.PercentageError = (Convert.ToDouble(model.DifArea) / Convert.ToDouble(model.PlotCheckArea) * 100).ToString("f1");

                #endregion

                #region 计算界址点中误差
                //界址点中误差m= sqrt(∑[∆L2]/2n);高精度检查时,界址点中误差m= sqrt(∑[∆L2]/ n)
                double difLSum = 0;
                foreach (var coordinate in model.CoordinateList)
                {
                    difLSum += Convert.ToDouble(coordinate.difSquareL);
                }
                model.PlotM = Math.Sqrt(difLSum / (2 * model.CoordinateList.Count)).ToString("f4");

                #endregion

                calcCount++;
                if (calcCount > 20)//如果计算次数超过40,说明有异常发生
                {
                    //errorDic.Add(model.PlotCode, filePath + " 文件中面积值为:" + model.PlotArea + ",实际运算值为:" + realArea);
                    calcCount        = 0;
                    model.IsGenerate = false;
                    return;
                }
                //最后判断如果 误差>5  或计算界址点中误差>=0.4  或面积P与P'相同  或面积P'小于零    则重新计算
                if (Convert.ToDouble(model.PercentageError) >= 5 || Convert.ToDouble(model.PlotM) >= 0.4 || model.PlotArea == model.PlotCheckArea || Convert.ToDouble(model.PlotCheckArea) < 0)
                {
                    SetPlotModel_EXCEL(model, filePath, true);
                }

                //standM = GetStandM();
                calcCount = 0;
                return;
            }
            catch (Exception ex)
            {
                errorDic.Add(model.PlotCode, filePath);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 为实体赋值-doc模式
        /// </summary>
        /// <param name="model"></param>
        private void SetPlotModel_DOC(PlotModel model, string filePath, bool recursive = false)
        {
            Document        document = new Document();
            TableCollection tables   = null;

            document.LoadFromFile(filePath);
            tables = document.Sections[0].Tables;
            if (tables.Count == 0)
            {
                return;
            }
            if (tables[0].Rows.Count == 0)
            {
                return;
            }
            if (tables.Count == 0)
            {
                return;
            }
            if (tables[0].Rows.Count == 0)
            {
                return;
            }
            try
            {
                model.CoordinateList = new List <CoordinatesModel>();
                model.PlotName       = tables[0].Rows[2].Cells[1].Paragraphs[0].Text.Trim(); //地块名称
                model.CbfDBName      = tables[0].Rows[4].Cells[1].Paragraphs[0].Text.Trim(); //承包方代表名称

                #region 处理界址点检查记录信息
                List <string> list = new List <string>();
                //循环多页
                for (int j = 0; j < tables.Count; j++)
                {
                    for (int i = 10; i < tables[j].Rows.Count; i++)
                    {
                        var row = tables[j].Rows[i];
                        if (string.IsNullOrEmpty(row.Cells[0].Paragraphs[0].Text))
                        {
                            break;
                        }
                        var coordinate = new CoordinatesModel();
                        coordinate.OrderNum         = (i - 9).ToString();
                        coordinate.SerialNumber     = row.Cells[0].Paragraphs[0].Text.Trim();
                        coordinate.BoundaryPointNum = row.Cells[1].Paragraphs[0].Text.Trim();

                        //对界址点编号中已存在的坐标点 不去重新计算赋值 直接添加入列表
                        if (!list.Contains(coordinate.BoundaryPointNum) && plotDic.ContainsKey(coordinate.BoundaryPointNum) && !recursive)//非递归模式
                        {
                            list.Add(coordinate.BoundaryPointNum);
                            coordinate          = plotDic[coordinate.BoundaryPointNum];
                            coordinate.OrderNum = (i - 9).ToString();
                            model.CoordinateList.Add(coordinate);
                            continue;
                        }

                        coordinate.X = Convert.ToDouble(row.Cells[2].Paragraphs[0].Text.Trim()).ToString("f3");
                        coordinate.Y = Convert.ToDouble(row.Cells[3].Paragraphs[0].Text.Trim()).ToString("f3");
                        //先随机 ∆L
                        coordinate.difL       = GetdifL();
                        coordinate.difSquareL = Math.Pow(Convert.ToDouble(coordinate.difL), 2.0).ToString("f3");
                        //再随机 X'
                        coordinate.cX = (Convert.ToDouble(coordinate.X) + MathCode.GetRandomNumber(-Convert.ToDouble(coordinate.difL) / 2, Convert.ToDouble(coordinate.difL) / 2, 3)).ToString("f3");

                        #region 随机三次X取样值
                        bool flagX = true;
                        while (flagX)
                        {
                            coordinate.X1 = (Convert.ToDouble(coordinate.X) + MathCode.GetRandomNumber(-0.75, 0.75, 3)).ToString("f3");
                            coordinate.X2 = (Convert.ToDouble(coordinate.X) + MathCode.GetRandomNumber(-0.75, 0.75, 3)).ToString("f3");
                            coordinate.X3 = (3 * Convert.ToDouble(coordinate.X) - Convert.ToDouble(coordinate.X1) - Convert.ToDouble(coordinate.X2)).ToString("f3");
                            if (Math.Abs(Convert.ToDouble(coordinate.X3) - Convert.ToDouble(coordinate.X)) < 0.8) //超出范围
                            {
                                flagX = false;
                            }
                        }
                        #endregion

                        coordinate.difX = (Convert.ToDouble(coordinate.X) - Convert.ToDouble(coordinate.cX)).ToString("f3");

                        //根据公式求出 ∆y ∆y = 开平方(∆L2-∆x2)  每次随机正负值
                        Random rd = new Random();
                        var    r  = rd.Next(0, 2);
                        coordinate.difY = ((r == 0 ? -1 : 1) * Math.Sqrt(Convert.ToDouble(coordinate.difSquareL) - Math.Pow(Convert.ToDouble(coordinate.difX), 2.0))).ToString("f3");

                        coordinate.cY = (Convert.ToDouble(coordinate.Y) - Convert.ToDouble(coordinate.difY)).ToString("f3");
                        #region 随机三次Y取样值
                        bool flagY = true;
                        while (flagY)
                        {
                            coordinate.Y1 = (Convert.ToDouble(coordinate.Y) + MathCode.GetRandomNumber(-0.75, 0.75, 3)).ToString("f3");
                            coordinate.Y2 = (Convert.ToDouble(coordinate.Y) + MathCode.GetRandomNumber(-0.75, 0.75, 3)).ToString("f3");
                            coordinate.Y3 = (3 * Convert.ToDouble(coordinate.Y) - Convert.ToDouble(coordinate.Y1) - Convert.ToDouble(coordinate.Y2)).ToString("f3");
                            if (Math.Abs(Convert.ToDouble(coordinate.Y3) - Convert.ToDouble(coordinate.Y)) < 0.8) //超出范围
                            {
                                flagY = false;
                            }
                        }
                        #endregion


                        if (!plotDic.ContainsKey(coordinate.BoundaryPointNum))//界址点编号字典添加坐标实体
                        {
                            plotDic.Add(coordinate.BoundaryPointNum, coordinate);
                        }

                        if (!list.Contains(coordinate.BoundaryPointNum))//不把最后一个回归原点的坐标统计入内
                        {
                            list.Add(coordinate.BoundaryPointNum);
                            model.CoordinateList.Add(coordinate);
                        }
                    }
                }


                #endregion

                #region 处理面积检查记录信息
                model.PlotArea = Convert.ToDouble(tables[0].Rows[6].Cells[1].Paragraphs[0].Text).ToString("f2");
                #region 计算P'
                //计算之前先判断是否有内环
                bool isHaveNei    = false;
                int  bigAreaCount = 0;
                for (int i = 0; i < model.CoordinateList.Count; i++)
                {
                    string serialNumber = model.CoordinateList[i].SerialNumber;
                    if (serialNumber.Contains("内环"))
                    {
                        isHaveNei    = true;
                        bigAreaCount = i;
                        break;
                    }
                }

                string realArea = "";
                if (!isHaveNei) //如果没有内环
                {
                    double[] difXArr = new double[model.CoordinateList.Count];
                    double[] difYArr = new double[model.CoordinateList.Count];
                    for (int i = 0; i < model.CoordinateList.Count; i++)
                    {
                        difXArr[i] = Convert.ToDouble(model.CoordinateList[i].cX);
                        difYArr[i] = Convert.ToDouble(model.CoordinateList[i].cY);
                    }
                    model.PlotCheckArea = MathCode.AoArea(model.CoordinateList.Count, difXArr, difYArr).ToString("f2");

                    for (int i = 0; i < model.CoordinateList.Count; i++)
                    {
                        difXArr[i] = Convert.ToDouble(model.CoordinateList[i].X);
                        difYArr[i] = Convert.ToDouble(model.CoordinateList[i].Y);
                    }
                    realArea = MathCode.AoArea(model.CoordinateList.Count, difXArr, difYArr).ToString("f2");
                }
                else//如果有内环
                {
                    #region 大环
                    double[] difXArr = new double[bigAreaCount];
                    double[] difYArr = new double[bigAreaCount];
                    for (int i = 0; i < bigAreaCount; i++)
                    {
                        difXArr[i] = Convert.ToDouble(model.CoordinateList[i].cX);
                        difYArr[i] = Convert.ToDouble(model.CoordinateList[i].cY);
                    }
                    double bigArea = MathCode.AoArea(bigAreaCount, difXArr, difYArr);//大面积

                    for (int i = 0; i < bigAreaCount; i++)
                    {
                        difXArr[i] = Convert.ToDouble(model.CoordinateList[i].X);
                        difYArr[i] = Convert.ToDouble(model.CoordinateList[i].Y);
                    }
                    double realBigArea = MathCode.AoArea(bigAreaCount, difXArr, difYArr);//真实大面积
                    #endregion

                    #region 小环
                    int      smallAreaCount = model.CoordinateList.Count - bigAreaCount;
                    double[] difXArr2       = new double[smallAreaCount];
                    double[] difYArr2       = new double[smallAreaCount];
                    for (int i = 0; i < smallAreaCount; i++)
                    {
                        difXArr2[i] = Convert.ToDouble(model.CoordinateList[i + bigAreaCount].cX);
                        difYArr2[i] = Convert.ToDouble(model.CoordinateList[i + bigAreaCount].cY);
                    }
                    double smallArea = Math.Abs(MathCode.AoArea(smallAreaCount, difXArr2, difYArr2));//内环面积
                    for (int i = 0; i < smallAreaCount; i++)
                    {
                        difXArr2[i] = Convert.ToDouble(model.CoordinateList[i + bigAreaCount].X);
                        difYArr2[i] = Convert.ToDouble(model.CoordinateList[i + bigAreaCount].Y);
                    }
                    double realSmallArea = Math.Abs(MathCode.AoArea(smallAreaCount, difXArr2, difYArr2));//内环面积
                    #endregion

                    model.PlotCheckArea = (bigArea - smallArea).ToString("f2");
                    realArea            = (realBigArea - realSmallArea).ToString("f2");
                }

                #endregion
                model.DifArea = Math.Abs(Convert.ToDouble(model.PlotArea) - Convert.ToDouble(model.PlotCheckArea)).ToString("f2");
                //R=∆P÷P′×100%
                model.PercentageError = (Convert.ToDouble(model.DifArea) / Convert.ToDouble(model.PlotCheckArea) * 100).ToString("f1");
                #endregion

                #region 计算界址点中误差
                //界址点中误差m= sqrt(∑[∆L2]/2n);高精度检查时,界址点中误差m= sqrt(∑[∆L2]/ n)
                double difLSum = 0;
                foreach (var coordinate in model.CoordinateList)
                {
                    difLSum += Convert.ToDouble(coordinate.difSquareL);
                }
                model.PlotM = Math.Sqrt(difLSum / (2 * model.CoordinateList.Count)).ToString("f4");

                #endregion

                calcCount++;
                if (calcCount > 20)//如果计算次数超过40,说明有异常发生
                {
                    //errorDic.Add(model.PlotCode, filePath + " 文件中面积值为:" + model.PlotArea + ",实际运算值为:" + realArea);
                    calcCount        = 0;
                    model.IsGenerate = false;
                    return;
                }
                //最后判断如果 误差>5  或计算界址点中误差>=0.4  或面积P与P'相同  或P'小于零    则重新计算
                if (Convert.ToDouble(model.PercentageError) >= 5 || Convert.ToDouble(model.PlotM) >= 0.4 || model.PlotArea == model.PlotCheckArea || Convert.ToDouble(model.PlotCheckArea) < 0)
                {
                    SetPlotModel_DOC(model, filePath, true);
                }
                //standM = GetStandM();
                calcCount = 0;
                return;
            }
            catch (Exception ex)
            {
                errorDic.Add(model.PlotCode, filePath);
            }
        }