lixiaojun
2024-08-13 0e0709e63ed50093d09fb88ac1ea4eb9b5a37afc
Hydro/Yw.EPAnet.Calcu.Core/01-network/00-core/Network_Calc.cs
@@ -9,111 +9,111 @@
{
    public partial class Network
    {
        /// <summary>
        /// 根据Inp文件计算
        /// </summary>
        /// <returns></returns>
        public virtual CalcuResult Calc()
        {
            EPAcore.Core.HydraulicCore epanet = new EPAcore.Core.HydraulicCore(true);
            var cResult = new CalcuResult();
            //获取系统临时文件目录,并且创建一个临时文件
            string InpPath = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".inp";
            string InpTxt=InpInteropHelper.ToInpString(this);
            System.IO.File.WriteAllText(InpPath, InpTxt);
            var err = epanet.open(InpPath, "", "");
            if (err != 0)
            {
                StringBuilder sbb=new StringBuilder();
                epanet.geterror(err,sbb, 256);
                cResult.Succeed = false;
                cResult.FailedList.Add(new CalcuFailed()
                {
                    Code = err,
                    Message =$"加载管网失败,{sbb}"
                });
            }
            var errCal = epanet.solveH();
            if (errCal != 0)
            {
                StringBuilder sbb = new StringBuilder();
                epanet.geterror(errCal, sbb, 256);
                cResult.Succeed = false;
                cResult.FailedList.Add(new CalcuFailed()
                {
                    Code = errCal,
                    Message = $"计算失败,{sbb}"
                });
            }
            int nodeCount = 0, linkCount = 0;
            epanet.getcount((int)CountType.Node, ref nodeCount);
            epanet.getcount((int)CountType.Link, ref linkCount);
            const int MAXID = 31;
        ///// <summary>
        ///// 根据Inp文件计算
        ///// </summary>
        ///// <returns></returns>
        //public virtual CalcuResult Calc()
        //{
        //    EPAcore.Core.HydraulicCore epanet = new EPAcore.Core.HydraulicCore(true);
        //    var cResult = new CalcuResult();
        //    //获取系统临时文件目录,并且创建一个临时文件
        //    string InpPath = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".inp";
        //    string InpTxt = InpInteropHelper.ToInpString(this);
        //    System.IO.File.WriteAllText(InpPath, InpTxt);
        //    var err = epanet.open(InpPath, "", "");
            var sb = new StringBuilder(MAXID);
        //    if (err != 0)
        //    {
        //        StringBuilder sbb = new StringBuilder();
        //        epanet.geterror(err, sbb, 256);
        //        cResult.Succeed = false;
        //        cResult.FailedList.Add(new CalcuFailed()
        //        {
        //            Code = err,
        //            Message = $"加载管网失败,{sbb}"
        //        });
        //    }
        //    var errCal = epanet.solveH();
        //    if (errCal != 0)
        //    {
        //        StringBuilder sbb = new StringBuilder();
        //        epanet.geterror(errCal, sbb, 256);
        //        cResult.Succeed = false;
        //        cResult.FailedList.Add(new CalcuFailed()
        //        {
        //            Code = errCal,
        //            Message = $"计算失败,{sbb}"
        //        });
        //    }
        //    int nodeCount = 0, linkCount = 0;
        //    epanet.GetCount((int)CountType.Node, ref nodeCount);
        //    epanet.GetCount((int)CountType.Link, ref linkCount);
        //    const int MAXID = 31;
            for (int i = 1; i <= nodeCount; i++)
            {
                epanet.getnodeid(i, sb);
                var arr = new string[] { "Press", "Head", "Demand" }; //System.Enum.GetValues(typeof(HydraulicModel.NodeValueType));
                var arrnum = new int[] { 10, 11, 9 };
                var result = new CalcuNode()
                {
                    Id = sb.ToString(),
                };
                for (var j = 0; j < arr.Length; j++)
                {
                    float v = 0;
                    var t = (EPAcore.Core.NodeValueType)j;
                    epanet.getnodevalue(i, arrnum[j], ref v);
                    switch (arr[j])
                    {
                        case "Press":
                            result.Press = v;
                            break;
                        case "Head":
                            result.Head = v;
                            break;
                        case "Demand":
                            result.Demand = v;
                            break;
                    }
                }
                cResult.NodeList.Add(result);
            }
        //    var sb = new StringBuilder(MAXID);
            for (int i = 1; i <= linkCount; i++)
            {
                epanet.getlinkid(i, sb);
                //var arr = System.Enum.GetValues(typeof(HydraulicModel.LinkValueType));
                var arr = new string[] { "Flow", "Velocity", "Headloss" }; //System.Enum.GetValues(typeof(HydraulicModel.NodeValueType));
                var arrnum = new int[] { 8, 9, 10 };
                var result = new CalcuLink()
                {
                    Id = sb.ToString(),
                };
                for (var j = 0; j < arr.Length; j++)
                {
                    float v = 0;
                    var t = (EPAcore.Core.NodeValueType)j;
                    epanet.getnodevalue(i, arrnum[j], ref v);
                    switch (arr[j])
                    {
                        case "Flow":
                            result.Flow = v;
                            break;
                        case "Velocity":
                            result.Velocity = v;
                            break;
                        case "Headloss":
                            result.Headloss = v;
                            break;
                    }
                }
                cResult.LinkList.Add(result);
            }
            return cResult;
        }
        //    for (int i = 1; i <= nodeCount; i++)
        //    {
        //        epanet.getnodeid(i, sb);
        //        var arr = new string[] { "Press", "Head", "Demand" }; //System.Enum.GetValues(typeof(HydraulicModel.NodeValueType));
        //        var arrnum = new int[] { 10, 11, 9 };
        //        var result = new CalcuNode()
        //        {
        //            Id = sb.ToString(),
        //        };
        //        for (var j = 0; j < arr.Length; j++)
        //        {
        //            float v = 0;
        //            var t = (EPAcore.Core.NodeValueType)j;
        //            epanet.getnodevalue(i, arrnum[j], ref v);
        //            switch (arr[j])
        //            {
        //                case "Press":
        //                    result.Press = v;
        //                    break;
        //                case "Head":
        //                    result.Head = v;
        //                    break;
        //                case "Demand":
        //                    result.Demand = v;
        //                    break;
        //            }
        //        }
        //        cResult.NodeList.Add(result);
        //    }
        //    for (int i = 1; i <= linkCount; i++)
        //    {
        //        epanet.getlinkid(i, sb);
        //        //var arr = System.Enum.GetValues(typeof(HydraulicModel.LinkValueType));
        //        var arr = new string[] { "Flow", "Velocity", "Headloss" }; //System.Enum.GetValues(typeof(HydraulicModel.NodeValueType));
        //        var arrnum = new int[] { 8, 9, 10 };
        //        var result = new CalcuLink()
        //        {
        //            Id = sb.ToString(),
        //        };
        //        for (var j = 0; j < arr.Length; j++)
        //        {
        //            float v = 0;
        //            var t = (EPAcore.Core.NodeValueType)j;
        //            epanet.getnodevalue(i, arrnum[j], ref v);
        //            switch (arr[j])
        //            {
        //                case "Flow":
        //                    result.Flow = v;
        //                    break;
        //                case "Velocity":
        //                    result.Velocity = v;
        //                    break;
        //                case "Headloss":
        //                    result.Headloss = v;
        //                    break;
        //            }
        //        }
        //        cResult.LinkList.Add(result);
        //    }
        //    return cResult;
        //}
    }
}