zhangyuekai
2024-08-17 2ea53b9fbd3fc318f1c02c1888c22aec5abab8ef
HStation.RevitDev/RevitDataExport/Common/GlobalResource.cs
@@ -6,6 +6,7 @@
using HStation.RevitDev.RevitDataExport.Entity;
using HStation.RevitDev.RevitDataExport.Entity.ElementModels;
using HStation.RevitDev.RevitDataExport.Forms;
using HStation.RevitDev.RevitDataExport.Utility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@@ -31,7 +32,8 @@
        public static string LastFilePath = string.Empty;
        public static Dictionary<RevitType, ObservableCollection<ElementModel>> RevitModels = new Dictionary<RevitType, ObservableCollection<ElementModel>>();
        public static List<Tuple<string, Dictionary<RevitType, List<string>>>> RevitModels =
            new List<Tuple<string, Dictionary<RevitType, List<string>>>>();
        public static RevitType PlacingType = RevitType.RFT_Unknown;
@@ -156,26 +158,14 @@
            }
        }
        public static ElementModel GetElementModel(string id)
        public static bool Contains(this List<Tuple<string, Dictionary<RevitType, List<string>>>> tuples, string id)
        {
            foreach (var pair in RevitModels)
            {
                var elemModels = pair.Value;
                var matchModels = elemModels.Where(x => x.Id == id);
                if (matchModels == null || matchModels.Count() == 0)
                {
                    continue;
                }
                return matchModels.First();
            }
            return null;
        }
            var dict = tuples.Find(x => x.Item1 == CurrentDocument.Title)?.Item2;
            if (dict == null) { return false; }
        public static bool Contains(this Dictionary<RevitType, ObservableCollection<ElementModel>> dict, string id)
        {
            foreach (var pair in dict)
            {
                if (pair.Value.Any(x => x.Id == id))
                if (pair.Value.Contains(id))
                {
                    return true;
                }
@@ -183,7 +173,7 @@
            return false;
        }
        public static void Add(this Dictionary<RevitType, ObservableCollection<ElementModel>> dict, Element elem, RevitType type = RevitType.RFT_Others)
        public static void Add(this List<Tuple<string, Dictionary<RevitType, List<string>>>> tuples, Element elem, RevitType type = RevitType.RFT_Others)
        {
            if (elem == null)
            {
@@ -191,63 +181,71 @@
            }
            var id = elem.Id.IntegerValue.ToString();
            if (!dict.Contains(id))
            if (!tuples.Contains(id))
            {
                var dict = tuples.Find(x => x.Item1 == CurrentDocument.Title)?.Item2;
                if (dict == null)
                {
                    dict = new Dictionary<RevitType, List<string>>();
                    tuples.Add(new Tuple<string, Dictionary<RevitType, List<string>>>(elem.Document.Title, dict));
                }
                if (!dict.ContainsKey(type))
                {
                    dict.Add(type, new ObservableCollection<ElementModel>());
                    dict.Add(type, new List<string>());
                }
                if (!dict[type].Any(x => x.Id == id))
                if (!dict[type].Contains(id))
                {
                    dict[type].Add(new ElementModel
                    {
                        Id = id,
                        Name = elem.Name,
                        LinkIds = string.Empty
                    });
                    dict[type].Add(id);
                }
            }
        }
        private static ElementModel CreateElementModel(Element elem, RevitType revitType)
        {
            if (revitType == RevitType.RFT_Valve)
            {
                return new ValveModel();
            }
            return new ElementModel();
        }
        public static void Remove(this Dictionary<RevitType, ObservableCollection<ElementModel>> dict, string id)
        public static void Remove(this List<Tuple<string, Dictionary<RevitType, List<string>>>> tuples, string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return;
            }
            if (dict.Contains(id))
            var dict = tuples.Find(x => x.Item1 == CurrentDocument.Title)?.Item2;
            if (dict == null) { return; }
            foreach (var pair in dict)
            {
                var models = dict.Values.Where(x => x.Any(y => y.Id == id))?.ToList();
                if (models != null && models.Count() == 0)
                if (pair.Value.Contains(id))
                {
                    for (int i = 0; i < models.Count(); i++)
                    {
                        models[i].RemoveAt(models[i].FindIndex(x=>x.Id == id));
                    }
                    pair.Value.Remove(id);
                }
            }
        }
        public static List<string> GetIds(this Dictionary<RevitType, ObservableCollection<ElementModel>> dict)
        public static List<string> GetIds(this List<Tuple<string, Dictionary<RevitType, List<string>>>> tuples)
        {
            var ret = new List<string>();
            var dict = tuples.Find(x => x.Item1 == CurrentDocument.Title)?.Item2;
            if (dict == null) { return ret; }
            foreach (var pair in dict)
            {
                var subList = pair.Value.Select(x => x.Id);
                var subList = pair.Value;
                ret.AddRange(subList);
            }
            return ret;
        }
        public static bool ContainsKey(this List<Tuple<string, Dictionary<RevitType, List<string>>>> tuples, RevitType type)
        {
            var dict = tuples.Find(x => x.Item1 == CurrentDocument.Title)?.Item2;
            if (dict == null) { return false; }
            return dict.ContainsKey(type);
        }
    }
}