duheng
2024-09-27 32b2fee0421e7997672fe4a14fe5bf9a6de926c2
Service/HStation.Service.Revit.Core/04-service/01-json/RevitJsonHelper.cs
@@ -10,9 +10,12 @@
        /// <summary>
        /// 从json字符串中解析
        /// </summary>
        /// <param name="revitJsonString">json字符串</param>
        /// <returns>格式错误,会抛出异常</returns>
        public static HStation.Model.RevitModel FromJsonString(string revitJsonString, out string msg)
        public static HStation.Model.RevitModel FromJsonString
            (
                string revitJsonString,
                string revitOthersJsonString,
                out string msg
            )
        {
            msg = string.Empty;
            if (string.IsNullOrEmpty(revitJsonString))
@@ -24,6 +27,7 @@
            {
                var model = new Model.RevitModel();
                //解析结构json
                var jarray = JArray.Parse(revitJsonString);
                for (int i = 0; i < jarray.Count; i++)
                {
@@ -71,6 +75,10 @@
                                    reservoir.Head = head;
                                }
                                reservoir.HeadPattern = jobject[RevitJsonProp.HeadPattern].ToString();
                                if (!string.IsNullOrEmpty(reservoir.HeadPattern))
                                {
                                    reservoir.HeadPattern = string.Empty;
                                }
                                model.Reservoirs.Add(reservoir);
                            }
                            break;
@@ -129,6 +137,10 @@
                                    tank.MinVol = minVol;
                                }
                                tank.VolCurve = jobject[RevitJsonProp.VolCurve].ToString();
                                if (!string.IsNullOrEmpty(tank.VolCurve))
                                {
                                    tank.VolCurve = string.Empty;
                                }
                                model.Tanks.Add(tank);
                            }
                            break;
@@ -187,6 +199,10 @@
                                    waterbox.MinVol = minVol;
                                }
                                waterbox.VolCurve = jobject[RevitJsonProp.VolCurve].ToString();
                                if (!string.IsNullOrEmpty(waterbox.VolCurve))
                                {
                                    waterbox.VolCurve = string.Empty;
                                }
                                model.Waterboxs.Add(waterbox);
                            }
                            break;
@@ -229,6 +245,10 @@
                                    junction.Demand = demand;
                                }
                                junction.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString();
                                if (!string.IsNullOrEmpty(junction.DemandPattern))
                                {
                                    junction.DemandPattern = string.Empty;
                                }
                                model.Junctions.Add(junction);
                            }
                            break;
@@ -271,6 +291,10 @@
                                    elbow.Demand = demand;
                                }
                                elbow.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString();
                                if (!string.IsNullOrEmpty(elbow.DemandPattern))
                                {
                                    elbow.DemandPattern = string.Empty;
                                }
                                model.Elbows.Add(elbow);
                            }
                            break;
@@ -313,6 +337,10 @@
                                    threelink.Demand = demand;
                                }
                                threelink.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString();
                                if (!string.IsNullOrEmpty(threelink.DemandPattern))
                                {
                                    threelink.DemandPattern = string.Empty;
                                }
                                model.Threelinks.Add(threelink);
                            }
                            break;
@@ -355,6 +383,10 @@
                                    fourlink.Demand = demand;
                                }
                                fourlink.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString();
                                if (!string.IsNullOrEmpty(fourlink.DemandPattern))
                                {
                                    fourlink.DemandPattern = string.Empty;
                                }
                                model.Fourlinks.Add(fourlink);
                            }
                            break;
@@ -397,6 +429,10 @@
                                    meter.Demand = demand;
                                }
                                meter.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString();
                                if (!string.IsNullOrEmpty(meter.DemandPattern))
                                {
                                    meter.DemandPattern = string.Empty;
                                }
                                model.Meters.Add(meter);
                            }
                            break;
@@ -439,6 +475,10 @@
                                    flowmeter.Demand = demand;
                                }
                                flowmeter.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString();
                                if (!string.IsNullOrEmpty(flowmeter.DemandPattern))
                                {
                                    flowmeter.DemandPattern = string.Empty;
                                }
                                model.Flowmeters.Add(flowmeter);
                            }
                            break;
@@ -481,6 +521,10 @@
                                    pressmeter.Demand = demand;
                                }
                                pressmeter.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString();
                                if (!string.IsNullOrEmpty(pressmeter.DemandPattern))
                                {
                                    pressmeter.DemandPattern = string.Empty;
                                }
                                model.Pressmeters.Add(pressmeter);
                            }
                            break;
@@ -523,6 +567,10 @@
                                    blunthead.Demand = demand;
                                }
                                blunthead.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString();
                                if (!string.IsNullOrEmpty(blunthead.DemandPattern))
                                {
                                    blunthead.DemandPattern = string.Empty;
                                }
                                model.Bluntheads.Add(blunthead);
                            }
                            break;
@@ -565,6 +613,10 @@
                                    nozzle.Demand = demand;
                                }
                                nozzle.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString();
                                if (!string.IsNullOrEmpty(nozzle.DemandPattern))
                                {
                                    nozzle.DemandPattern = string.Empty;
                                }
                                if (jobject[RevitJsonProp.CoefficientP].MatchNumeric(out double coeffient))
                                {
                                    nozzle.Coefficient = coeffient;
@@ -611,6 +663,10 @@
                                    hydrant.Demand = demand;
                                }
                                hydrant.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString();
                                if (!string.IsNullOrEmpty(hydrant.DemandPattern))
                                {
                                    hydrant.DemandPattern = string.Empty;
                                }
                                if (jobject[RevitJsonProp.CoefficientF].MatchNumeric(out double coeffient))
                                {
                                    hydrant.Coefficient = coeffient;
@@ -693,11 +749,11 @@
                                }
                                if (exchanger.Diameter < 0.1)
                                {
                                    exchanger.Diameter = 0.1;
                                    exchanger.Diameter = 500;
                                }
                                if (exchanger.Roughness < 0.1)
                                {
                                    exchanger.Roughness = 0.1;
                                    exchanger.Roughness = 110;
                                }
                                model.Exchangers.Add(exchanger);
                            }
@@ -755,14 +811,26 @@
                                {
                                    pipe.Length = length;
                                }
                                #region 直径
                                if (jobject[RevitJsonProp.Diameter].MatchNumeric(out double diameter))
                                {
                                    pipe.Diameter = diameter;
                                }
                                else if (jobject[RevitJsonProp.InternalDiameter].MatchNumeric(out double internalDiameter))
                                if (pipe.Diameter < 1)
                                {
                                    pipe.Diameter = internalDiameter;
                                    if (jobject[RevitJsonProp.InternalDiameter].MatchNumeric(out double internalDiameter))
                                    {
                                        pipe.Diameter = internalDiameter;
                                    }
                                }
                                #endregion
                                if (jobject[RevitJsonProp.MinorLoss].MatchNumeric(out double minorLoss))
                                {
                                    pipe.MinorLoss = minorLoss;
@@ -777,11 +845,11 @@
                                }
                                if (pipe.Diameter < 0.1)
                                {
                                    pipe.Diameter = 0.1;
                                    pipe.Diameter = 500;
                                }
                                if (pipe.Roughness < 0.1)
                                {
                                    pipe.Roughness = 0.1;
                                    pipe.Roughness = 110;
                                }
                                model.Pipes.Add(pipe);
                            }
@@ -861,11 +929,11 @@
                                }
                                if (translation.Diameter < 0.1)
                                {
                                    translation.Diameter = 0.1;
                                    translation.Diameter = 500;
                                }
                                if (translation.Roughness < 0.1)
                                {
                                    translation.Roughness = 0.1;
                                    translation.Roughness = 110;
                                }
                                model.Translations.Add(translation);
                            }
@@ -923,17 +991,30 @@
                                    pump.RatedPower = ratedPower;
                                }
                                pump.CurveQH = jobject[RevitJsonProp.CurveQH].ToString();
                                pump.CurveQH = "PumpDefault";
                                if (jobject[RevitJsonProp.SpeedRatio].MatchNumeric(out double speedRatio))
                                {
                                    pump.SpeedRatio = speedRatio;
                                }
                                pump.SpeedRatioPattern = jobject[RevitJsonProp.SpeedRatioPattern].ToString();
                                if (!string.IsNullOrEmpty(pump.SpeedRatioPattern))
                                {
                                    pump.SpeedRatioPattern = string.Empty;
                                }
                                if (jobject[RevitJsonProp.Price].MatchNumeric(out double price))
                                {
                                    pump.Price = price;
                                }
                                pump.PricePattern = jobject[RevitJsonProp.PricePattern].ToString();
                                if (!string.IsNullOrEmpty(pump.PricePattern))
                                {
                                    pump.PricePattern = string.Empty;
                                }
                                pump.CurveQE = jobject[RevitJsonProp.CurveQE].ToString();
                                if (!string.IsNullOrEmpty(pump.CurveQE))
                                {
                                    pump.CurveQE = string.Empty;
                                }
                                model.Pumps.Add(pump);
                            }
                            break;
@@ -994,13 +1075,63 @@
                                    valve.MinorLoss = minorLoss;
                                }
                                if (valve.Diameter < 0.1)
                                {
                                    valve.Diameter = 500;
                                }
                                valve.ValveType = jobject[RevitJsonProp.ValveType].ToString();
                                valve.ValveSetting = jobject[RevitJsonProp.ValveSetting].ToString();
                                model.Valves.Add(valve);
                            }
                            break;
                        case RevitJsonCatalog.Other:
                            {
                                var decorator = new Model.RevitDecorator();
                                decorator.Id = jobject[RevitJsonProp.Id].ToString();
                                decorator.Code = jobject[RevitJsonProp.Code].ToString();
                                if (string.IsNullOrEmpty(decorator.Code))
                                {
                                    decorator.Code = decorator.Id;
                                }
                                decorator.Name = string.Empty;
                                decorator.Category = jobject[RevitJsonProp.ClanAndType][RevitJsonProp.ClanName].ToString();
                                decorator.Decoration = string.Empty;
                                model.Decorators.Add(decorator);
                            }
                            break;
                        default: break;
                    }
                }
                //解析其他json
                var jothersArray = JArray.Parse(revitOthersJsonString);
                if (jothersArray != null && jothersArray.Count > 0)
                {
                    var allParterList = model.GetAllParters();
                    for (int i = 0; i < jothersArray.Count; i++)
                    {
                        var jobject = jothersArray[i];
                        var id = jobject[RevitOthersJsonProp.Id]?.ToString();
                        if (string.IsNullOrEmpty(id))
                        {
                            continue;
                        }
                        if (allParterList.Exists(x => x.Id == id))
                        {
                            continue;
                        }
                        if (model.Decorators.Exists(x => x.Id == id))
                        {
                            continue;
                        }
                        var decorator = new Model.RevitDecorator();
                        decorator.Id = id;
                        decorator.Code = id;
                        decorator.Name = jobject[RevitOthersJsonProp.Name]?.ToString();
                        decorator.Category = jobject[RevitOthersJsonProp.CategoryName]?.ToString();
                        decorator.Decoration = jobject[RevitOthersJsonProp.CategoryID]?.ToString();
                        model.Decorators.Add(decorator);
                    }
                }
@@ -1017,9 +1148,7 @@
        /// <summary>
        /// 从json文件中解析
        /// </summary>
        /// <param name="revitJsonFile">json文件路径</param>
        /// <returns></returns>
        public static Model.RevitModel FromJsonFile(string revitJsonFile, out string msg)
        public static Model.RevitModel FromJsonFile(string revitJsonFile, string revitOthersJsonFile, out string msg)
        {
            if (!File.Exists(revitJsonFile))
            {
@@ -1027,7 +1156,12 @@
                return default;
            }
            var revitJson = File.ReadAllText(revitJsonFile);
            var revitModel = FromJsonString(revitJson, out msg);
            var revitOthersJson = string.Empty;
            if (File.Exists(revitOthersJsonFile))
            {
                revitOthersJson = File.ReadAllText(revitOthersJsonFile);
            }
            var revitModel = FromJsonString(revitJson, revitOthersJson, out msg);
            if (revitModel == null)
            {
                return default;