using System.Transactions; namespace HStation.Service { /// /// Revit Json辅助类 /// public class RevitJsonHelper { /// /// 从json字符串中解析 /// public static HStation.Model.RevitModel FromJsonString ( string revitJsonString, string revitOthersJsonString, out string msg ) { msg = string.Empty; if (string.IsNullOrEmpty(revitJsonString)) { msg = "Revit json 字符串为空"; return default; } try { var model = new Model.RevitModel(); //解析结构json var jarray = JArray.Parse(revitJsonString); for (int i = 0; i < jarray.Count; i++) { var jobject = JObject.Parse(jarray[i].ToString()); var catalog = jobject[RevitJsonProp.Catalog].ToString(); switch (catalog) { case RevitJsonCatalog.Reservoir: { var reservoir = new Model.RevitReservoir(); reservoir.Id = jobject[RevitJsonProp.Id].ToString(); reservoir.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(reservoir.Code)) { reservoir.Code = reservoir.Id; } reservoir.Name = jobject[RevitJsonProp.Name].ToString(); reservoir.ModelType = jobject[RevitJsonProp.ModelType].ToString(); reservoir.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); reservoir.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); reservoir.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { reservoir.Quality = quality; } reservoir.Position = reservoir.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.PoolElev].MatchNumeric(out double poolElev)) { reservoir.PoolElev = poolElev; } if (jobject[RevitJsonProp.Head].MatchNumeric(out double head)) { reservoir.Head = head; } reservoir.HeadPattern = jobject[RevitJsonProp.HeadPattern].ToString(); if (!string.IsNullOrEmpty(reservoir.HeadPattern)) { reservoir.HeadPattern = string.Empty; } model.Reservoirs.Add(reservoir); } break; case RevitJsonCatalog.Tank: { var tank = new Model.RevitTank(); tank.Id = jobject[RevitJsonProp.Id].ToString(); tank.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(tank.Code)) { tank.Code = tank.Id; } tank.Name = jobject[RevitJsonProp.Name].ToString(); tank.ModelType = jobject[RevitJsonProp.ModelType].ToString(); tank.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); tank.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); tank.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { tank.Quality = quality; } tank.Position = tank.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.PoolElev].MatchNumeric(out double poolElev)) { tank.PoolElev = poolElev; } if (jobject[RevitJsonProp.InitLevel].MatchNumeric(out double initLevel)) { tank.InitLevel = initLevel; } if (jobject[RevitJsonProp.MinLevel].MatchNumeric(out double minLevel)) { tank.MinLevel = minLevel; } if (jobject[RevitJsonProp.MaxLevel].MatchNumeric(out double maxLevel)) { tank.MaxLevel = maxLevel; } if (jobject[RevitJsonProp.Diameter].MatchNumeric(out double diameter)) { tank.Diameter = diameter; } if (jobject[RevitJsonProp.MinVol].MatchNumeric(out double minVol)) { tank.MinVol = minVol; } tank.VolCurve = jobject[RevitJsonProp.VolCurve].ToString(); if (!string.IsNullOrEmpty(tank.VolCurve)) { tank.VolCurve = string.Empty; } model.Tanks.Add(tank); } break; case RevitJsonCatalog.Waterbox: { var waterbox = new Model.RevitWaterbox(); waterbox.Id = jobject[RevitJsonProp.Id].ToString(); waterbox.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(waterbox.Code)) { waterbox.Code = waterbox.Id; } waterbox.Name = jobject[RevitJsonProp.Name].ToString(); waterbox.ModelType = jobject[RevitJsonProp.ModelType].ToString(); waterbox.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); waterbox.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); waterbox.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { waterbox.Quality = quality; } waterbox.Position = waterbox.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.PoolElev].MatchNumeric(out double poolElev)) { waterbox.PoolElev = poolElev; } if (jobject[RevitJsonProp.InitLevel].MatchNumeric(out double initLevel)) { waterbox.InitLevel = initLevel; } if (jobject[RevitJsonProp.MinLevel].MatchNumeric(out double minLevel)) { waterbox.MinLevel = minLevel; } if (jobject[RevitJsonProp.MaxLevel].MatchNumeric(out double maxLevel)) { waterbox.MaxLevel = maxLevel; } if (jobject[RevitJsonProp.Diameter].MatchNumeric(out double diameter)) { waterbox.Diameter = diameter; } if (jobject[RevitJsonProp.MinVol].MatchNumeric(out double minVol)) { waterbox.MinVol = minVol; } waterbox.VolCurve = jobject[RevitJsonProp.VolCurve].ToString(); if (!string.IsNullOrEmpty(waterbox.VolCurve)) { waterbox.VolCurve = string.Empty; } model.Waterboxs.Add(waterbox); } break; case RevitJsonCatalog.Junction: { var junction = new Model.RevitJunction(); junction.Id = jobject[RevitJsonProp.Id].ToString(); junction.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(junction.Code)) { junction.Code = junction.Id; } junction.Name = jobject[RevitJsonProp.Name].ToString(); junction.ModelType = jobject[RevitJsonProp.ModelType].ToString(); junction.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); junction.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); junction.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { junction.Quality = quality; } junction.Position = junction.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.Elev].MatchNumeric(out double elev)) { junction.Elev = elev; } if (jobject[RevitJsonProp.Demand].MatchNumeric(out double demand)) { junction.Demand = demand; } junction.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString(); if (!string.IsNullOrEmpty(junction.DemandPattern)) { junction.DemandPattern = string.Empty; } model.Junctions.Add(junction); } break; case RevitJsonCatalog.Elbow: { var elbow = new Model.RevitElbow(); elbow.Id = jobject[RevitJsonProp.Id].ToString(); elbow.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(elbow.Code)) { elbow.Code = elbow.Id; } elbow.Name = jobject[RevitJsonProp.Name].ToString(); elbow.ModelType = jobject[RevitJsonProp.ModelType].ToString(); elbow.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); elbow.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); elbow.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { elbow.Quality = quality; } elbow.Position = elbow.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.Elev].MatchNumeric(out double elev)) { elbow.Elev = elev; } if (jobject[RevitJsonProp.Demand].MatchNumeric(out double demand)) { elbow.Demand = demand; } elbow.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString(); if (!string.IsNullOrEmpty(elbow.DemandPattern)) { elbow.DemandPattern = string.Empty; } model.Elbows.Add(elbow); } break; case RevitJsonCatalog.Threelink: { var threelink = new Model.RevitThreelink(); threelink.Id = jobject[RevitJsonProp.Id].ToString(); threelink.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(threelink.Code)) { threelink.Code = threelink.Id; } threelink.Name = jobject[RevitJsonProp.Name].ToString(); threelink.ModelType = jobject[RevitJsonProp.ModelType].ToString(); threelink.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); threelink.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); threelink.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { threelink.Quality = quality; } threelink.Position = threelink.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.Elev].MatchNumeric(out double elev)) { threelink.Elev = elev; } if (jobject[RevitJsonProp.Demand].MatchNumeric(out double demand)) { threelink.Demand = demand; } threelink.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString(); if (!string.IsNullOrEmpty(threelink.DemandPattern)) { threelink.DemandPattern = string.Empty; } model.Threelinks.Add(threelink); } break; case RevitJsonCatalog.Fourlink: { var fourlink = new Model.RevitFourlink(); fourlink.Id = jobject[RevitJsonProp.Id].ToString(); fourlink.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(fourlink.Code)) { fourlink.Code = fourlink.Id; } fourlink.Name = jobject[RevitJsonProp.Name].ToString(); fourlink.ModelType = jobject[RevitJsonProp.ModelType].ToString(); fourlink.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); fourlink.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); fourlink.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { fourlink.Quality = quality; } fourlink.Position = fourlink.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.Elev].MatchNumeric(out double elev)) { fourlink.Elev = elev; } if (jobject[RevitJsonProp.Demand].MatchNumeric(out double demand)) { fourlink.Demand = demand; } fourlink.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString(); if (!string.IsNullOrEmpty(fourlink.DemandPattern)) { fourlink.DemandPattern = string.Empty; } model.Fourlinks.Add(fourlink); } break; case RevitJsonCatalog.Meter: { var meter = new Model.RevitMeter(); meter.Id = jobject[RevitJsonProp.Id].ToString(); meter.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(meter.Code)) { meter.Code = meter.Id; } meter.Name = jobject[RevitJsonProp.Name].ToString(); meter.ModelType = jobject[RevitJsonProp.ModelType].ToString(); meter.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); meter.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); meter.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { meter.Quality = quality; } meter.Position = meter.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.Elev].MatchNumeric(out double elev)) { meter.Elev = elev; } if (jobject[RevitJsonProp.Demand].MatchNumeric(out double demand)) { meter.Demand = demand; } meter.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString(); if (!string.IsNullOrEmpty(meter.DemandPattern)) { meter.DemandPattern = string.Empty; } model.Meters.Add(meter); } break; case RevitJsonCatalog.Flowmeter: { var flowmeter = new Model.RevitFlowmeter(); flowmeter.Id = jobject[RevitJsonProp.Id].ToString(); flowmeter.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(flowmeter.Code)) { flowmeter.Code = flowmeter.Id; } flowmeter.Name = jobject[RevitJsonProp.Name].ToString(); flowmeter.ModelType = jobject[RevitJsonProp.ModelType].ToString(); flowmeter.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); flowmeter.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); flowmeter.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { flowmeter.Quality = quality; } flowmeter.Position = flowmeter.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.Elev].MatchNumeric(out double elev)) { flowmeter.Elev = elev; } if (jobject[RevitJsonProp.Demand].MatchNumeric(out double demand)) { flowmeter.Demand = demand; } flowmeter.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString(); if (!string.IsNullOrEmpty(flowmeter.DemandPattern)) { flowmeter.DemandPattern = string.Empty; } model.Flowmeters.Add(flowmeter); } break; case RevitJsonCatalog.Pressmeter: { var pressmeter = new Model.RevitPressmeter(); pressmeter.Id = jobject[RevitJsonProp.Id].ToString(); pressmeter.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(pressmeter.Code)) { pressmeter.Code = pressmeter.Id; } pressmeter.Name = jobject[RevitJsonProp.Name].ToString(); pressmeter.ModelType = jobject[RevitJsonProp.ModelType].ToString(); pressmeter.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); pressmeter.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); pressmeter.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { pressmeter.Quality = quality; } pressmeter.Position = pressmeter.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.Elev].MatchNumeric(out double elev)) { pressmeter.Elev = elev; } if (jobject[RevitJsonProp.Demand].MatchNumeric(out double demand)) { pressmeter.Demand = demand; } pressmeter.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString(); if (!string.IsNullOrEmpty(pressmeter.DemandPattern)) { pressmeter.DemandPattern = string.Empty; } model.Pressmeters.Add(pressmeter); } break; case RevitJsonCatalog.Blunthead: { var blunthead = new Model.RevitBlunthead(); blunthead.Id = jobject[RevitJsonProp.Id].ToString(); blunthead.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(blunthead.Code)) { blunthead.Code = blunthead.Id; } blunthead.Name = jobject[RevitJsonProp.Name].ToString(); blunthead.ModelType = jobject[RevitJsonProp.ModelType].ToString(); blunthead.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); blunthead.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); blunthead.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { blunthead.Quality = quality; } blunthead.Position = blunthead.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.Elev].MatchNumeric(out double elev)) { blunthead.Elev = elev; } if (jobject[RevitJsonProp.Demand].MatchNumeric(out double demand)) { blunthead.Demand = demand; } blunthead.DemandPattern = jobject[RevitJsonProp.DemandPattern].ToString(); if (!string.IsNullOrEmpty(blunthead.DemandPattern)) { blunthead.DemandPattern = string.Empty; } model.Bluntheads.Add(blunthead); } break; case RevitJsonCatalog.Nozzle: { var nozzle = new Model.RevitNozzle(); nozzle.Id = jobject[RevitJsonProp.Id].ToString(); nozzle.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(nozzle.Code)) { nozzle.Code = nozzle.Id; } nozzle.Name = jobject[RevitJsonProp.Name].ToString(); nozzle.ModelType = jobject[RevitJsonProp.ModelType].ToString(); nozzle.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); nozzle.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); nozzle.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { nozzle.Quality = quality; } nozzle.Position = nozzle.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.Elev].MatchNumeric(out double elev)) { nozzle.Elev = elev; } if (jobject[RevitJsonProp.Demand].MatchNumeric(out double demand)) { 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; } model.Nozzles.Add(nozzle); } break; case RevitJsonCatalog.Hydrant: { var hydrant = new Model.RevitHydrant(); hydrant.Id = jobject[RevitJsonProp.Id].ToString(); hydrant.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(hydrant.Code)) { hydrant.Code = hydrant.Id; } hydrant.Name = jobject[RevitJsonProp.Name].ToString(); hydrant.ModelType = jobject[RevitJsonProp.ModelType].ToString(); hydrant.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); hydrant.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); hydrant.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); if (jobject[RevitJsonProp.Quality].MatchNumeric(out double quality)) { hydrant.Quality = quality; } hydrant.Position = hydrant.ConnectList.GetCenterPosition(); if (jobject[RevitJsonProp.Elev].MatchNumeric(out double elev)) { hydrant.Elev = elev; } if (jobject[RevitJsonProp.Demand].MatchNumeric(out double demand)) { 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; } model.Hydrants.Add(hydrant); } break; case RevitJsonCatalog.Exchanger: { var exchanger = new Model.RevitExchanger(); exchanger.Id = jobject[RevitJsonProp.Id].ToString(); exchanger.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(exchanger.Code)) { exchanger.Code = exchanger.Id; } exchanger.Name = jobject[RevitJsonProp.Name].ToString(); exchanger.ModelType = jobject[RevitJsonProp.ModelType].ToString(); exchanger.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); exchanger.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); exchanger.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); var startConnect = exchanger.ConnectList.GetStartConnect(); var endConnect = exchanger.ConnectList.GetEndConnect(); exchanger.StartCode = startConnect.Id; exchanger.EndCode = endConnect.Id; exchanger.StartPosition = startConnect.Position; exchanger.EndPosition = endConnect.Position; exchanger.LinkStatus = jobject[RevitJsonProp.LinkStatusPipe].ToString(); if (jobject[RevitJsonProp.StartElev].MatchNumeric(out double startElev)) { exchanger.StartElev = startElev; } if (jobject[RevitJsonProp.EndElev].MatchNumeric(out double endElev)) { exchanger.EndElev = endElev; } if (jobject[RevitJsonProp.StartQuality].MatchNumeric(out double startQuality)) { exchanger.StartQuality = startQuality; } if (jobject[RevitJsonProp.EndQuality].MatchNumeric(out double endQuality)) { exchanger.EndQuality = endQuality; } if (jobject[RevitJsonProp.Length].MatchNumeric(out double length)) { exchanger.Length = length; } if (jobject[RevitJsonProp.Diameter].MatchNumeric(out double diameter)) { exchanger.Diameter = diameter; } else if (jobject[RevitJsonProp.InternalDiameter].MatchNumeric(out double internalDiameter)) { exchanger.Diameter = internalDiameter; } if (jobject[RevitJsonProp.MinorLoss].MatchNumeric(out double minorLoss)) { exchanger.MinorLoss = minorLoss; } if (jobject[RevitJsonProp.Roughness].MatchNumeric(out double roughness)) { exchanger.Roughness = roughness; } if (exchanger.Length <= 0) { exchanger.Length = exchanger.StartPosition.Distance(exchanger.EndPosition); } if (exchanger.Diameter < 0.1) { exchanger.Diameter = 500; } if (exchanger.Roughness < 0.1) { exchanger.Roughness = 110; } model.Exchangers.Add(exchanger); } break; case RevitJsonCatalog.Pipe: { var pipe = new Model.RevitPipe(); pipe.Id = jobject[RevitJsonProp.Id].ToString(); pipe.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(pipe.Code)) { pipe.Code = pipe.Id; } pipe.Name = jobject[RevitJsonProp.Name].ToString(); pipe.ModelType = jobject[RevitJsonProp.ModelType].ToString(); pipe.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); pipe.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); pipe.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); var startConnect = pipe.ConnectList.GetStartConnect(); var endConnect = pipe.ConnectList.GetEndConnect(); pipe.StartCode = startConnect.Id; pipe.EndCode = endConnect.Id; pipe.StartPosition = startConnect.Position; pipe.EndPosition = endConnect.Position; pipe.LinkStatus = jobject[RevitJsonProp.LinkStatusPipe].ToString(); if (jobject[RevitJsonProp.StartElev].MatchNumeric(out double startElev)) { pipe.StartElev = startElev; } if (jobject[RevitJsonProp.EndElev].MatchNumeric(out double endElev)) { pipe.EndElev = endElev; } if (jobject[RevitJsonProp.StartQuality].MatchNumeric(out double startQuality)) { pipe.StartQuality = startQuality; } if (jobject[RevitJsonProp.EndQuality].MatchNumeric(out double endQuality)) { pipe.EndQuality = endQuality; } if (jobject[RevitJsonProp.Length].MatchNumeric(out double length)) { pipe.Length = length; } if (jobject[RevitJsonProp.Diameter].MatchNumeric(out double diameter)) { pipe.Diameter = diameter; } else if (jobject[RevitJsonProp.InternalDiameter].MatchNumeric(out double internalDiameter)) { pipe.Diameter = internalDiameter; } if (jobject[RevitJsonProp.MinorLoss].MatchNumeric(out double minorLoss)) { pipe.MinorLoss = minorLoss; } if (jobject[RevitJsonProp.Roughness].MatchNumeric(out double roughness)) { pipe.Roughness = roughness; } if (pipe.Length <= 0) { pipe.Length = pipe.StartPosition.Distance(pipe.EndPosition); } if (pipe.Diameter < 0.1) { pipe.Diameter = 500; } if (pipe.Roughness < 0.1) { pipe.Roughness = 110; } model.Pipes.Add(pipe); } break; case RevitJsonCatalog.Translation: { var translation = new Model.RevitTranslation(); translation.Id = jobject[RevitJsonProp.Id].ToString(); translation.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(translation.Code)) { translation.Code = translation.Id; } translation.Name = jobject[RevitJsonProp.Name].ToString(); translation.ModelType = jobject[RevitJsonProp.ModelType].ToString(); translation.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); translation.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); translation.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); var startConnect = translation.ConnectList.GetStartConnect(); var endConnect = translation.ConnectList.GetEndConnect(); translation.StartCode = startConnect.Id; translation.EndCode = endConnect.Id; translation.StartPosition = startConnect.Position; translation.EndPosition = endConnect.Position; translation.LinkStatus = jobject[RevitJsonProp.LinkStatusPipe].ToString(); if (jobject[RevitJsonProp.StartElev].MatchNumeric(out double startElev)) { translation.StartElev = startElev; } if (jobject[RevitJsonProp.EndElev].MatchNumeric(out double endElev)) { translation.EndElev = endElev; } if (jobject[RevitJsonProp.StartQuality].MatchNumeric(out double startQuality)) { translation.StartQuality = startQuality; } if (jobject[RevitJsonProp.EndQuality].MatchNumeric(out double endQuality)) { translation.EndQuality = endQuality; } if (jobject[RevitJsonProp.Length].MatchNumeric(out double length)) { translation.Length = length; } if (jobject[RevitJsonProp.Diameter].MatchNumeric(out double diameter)) { translation.Diameter = diameter; } else if (jobject[RevitJsonProp.InternalDiameter].MatchNumeric(out double internalDiameter)) { translation.Diameter = internalDiameter; } if (jobject[RevitJsonProp.MinorLoss].MatchNumeric(out double minorLoss)) { translation.MinorLoss = minorLoss; } if (jobject[RevitJsonProp.Roughness].MatchNumeric(out double roughness)) { translation.Roughness = roughness; } if (translation.Length <= 0) { translation.Length = translation.StartPosition.Distance(translation.EndPosition); } if (translation.Diameter < 0.1) { translation.Diameter = 500; } if (translation.Roughness < 0.1) { translation.Roughness = 110; } model.Translations.Add(translation); } break; case RevitJsonCatalog.Pump: { var pump = new Model.RevitPump(); pump.Id = jobject[RevitJsonProp.Id].ToString(); pump.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(pump.Code)) { pump.Code = pump.Id; } pump.Name = jobject[RevitJsonProp.Name].ToString(); pump.ModelType = jobject[RevitJsonProp.ModelType].ToString(); pump.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); pump.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); pump.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); var startConnect = pump.ConnectList.GetStartConnect(); var endConnect = pump.ConnectList.GetEndConnect(); pump.StartCode = startConnect.Id; pump.EndCode = endConnect.Id; pump.StartPosition = startConnect.Position; pump.EndPosition = endConnect.Position; pump.LinkStatus = jobject[RevitJsonProp.LinkStatusPump].ToString(); if (jobject[RevitJsonProp.StartElev].MatchNumeric(out double startElev)) { pump.StartElev = startElev; } if (jobject[RevitJsonProp.EndElev].MatchNumeric(out double endElev)) { pump.EndElev = endElev; } if (jobject[RevitJsonProp.StartQuality].MatchNumeric(out double startQuality)) { pump.StartQuality = startQuality; } if (jobject[RevitJsonProp.EndQuality].MatchNumeric(out double endQuality)) { pump.EndQuality = endQuality; } if (jobject[RevitJsonProp.RatedPower].MatchNumeric(out double ratedPower)) { 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; case RevitJsonCatalog.Valve: { var valve = new Model.RevitValve(); valve.Id = jobject[RevitJsonProp.Id].ToString(); valve.Code = jobject[RevitJsonProp.Code].ToString(); if (string.IsNullOrEmpty(valve.Code)) { valve.Code = valve.Id; } valve.Name = jobject[RevitJsonProp.Name].ToString(); valve.ModelType = jobject[RevitJsonProp.ModelType].ToString(); valve.Flags = Yw.Untity.FlagsHelper.ToList(jobject[RevitJsonProp.Flags].ToString()); valve.Description = jobject[RevitJsonProp.Description].ToString(); var connectList = JsonHelper.Json2Object>(jobject[RevitJsonProp.Connects].ToString()); valve.ConnectList = connectList?.Select(x => new Model.RevitConnect() { Id = x.ConnectId, Direction = x.Dirction, Position = new Model.RevitPosition() { X = x.Point.X, Y = x.Point.Y, Z = x.Point.Z } }).ToList(); var startConnect = valve.ConnectList.GetStartConnect(); var endConnect = valve.ConnectList.GetEndConnect(); valve.StartCode = startConnect.Id; valve.EndCode = endConnect.Id; valve.StartPosition = startConnect.Position; valve.EndPosition = endConnect.Position; valve.LinkStatus = jobject[RevitJsonProp.LinkStatusValve].ToString(); if (jobject[RevitJsonProp.StartElev].MatchNumeric(out double startElev)) { valve.StartElev = startElev; } if (jobject[RevitJsonProp.EndElev].MatchNumeric(out double endElev)) { valve.EndElev = endElev; } if (jobject[RevitJsonProp.StartQuality].MatchNumeric(out double startQuality)) { valve.StartQuality = startQuality; } if (jobject[RevitJsonProp.EndQuality].MatchNumeric(out double endQuality)) { valve.EndQuality = endQuality; } if (jobject[RevitJsonProp.Diameter].MatchNumeric(out double diameter)) { valve.Diameter = diameter; } if (jobject[RevitJsonProp.MinorLoss].MatchNumeric(out double minorLoss)) { 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); } } return model; } catch (Exception ex) { msg = ex.Message; return default; } } /// /// 从json文件中解析 /// public static Model.RevitModel FromJsonFile(string revitJsonFile, string revitOthersJsonFile, out string msg) { if (!File.Exists(revitJsonFile)) { msg = "Json 文件不存在"; return default; } var revitJson = File.ReadAllText(revitJsonFile); var revitOthersJson = string.Empty; if (File.Exists(revitOthersJsonFile)) { revitOthersJson = File.ReadAllText(revitOthersJsonFile); } var revitModel = FromJsonString(revitJson, revitOthersJson, out msg); if (revitModel == null) { return default; } revitModel.Name = Path.GetFileNameWithoutExtension(revitJsonFile); revitModel.Description = $"解析【{Path.GetFileName(revitJsonFile)}】文件"; return revitModel; } } }