XmlMenuData.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* ==============================================================================
  2. * 功能描述:加载Menu下的配置文件,转化为TszTabData
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/9/17 17:56:32
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Xml;
  13. using SAGA.DotNetUtils;
  14. using SAGA.DotNetUtils.Others;
  15. using SAGA.RevitUtils;
  16. using TSZ.RevitMenu.Configuration;
  17. namespace SAGA.RevitMenu.Configuration
  18. {
  19. /// <summary>
  20. /// MenuData
  21. /// </summary>
  22. class XmlMenuData
  23. {
  24. /// <summary>
  25. /// 根据选择的项目 确定加载的菜单 建筑 结构 水暖电
  26. /// </summary>
  27. /// <param name="strRevitVersion"></param>
  28. /// <param name="blnRevit"></param>
  29. /// <returns></returns>
  30. public static TszTabData[] GetConfigTabs(string strRevitVersion, out bool blnRevit)
  31. {
  32. blnRevit = false;
  33. List<string> listFea = new List<string>();
  34. FileInfo[] files = new DirectoryInfo(Path.Combine(AppBaseInfo.AppRunPath, "Menu")).GetFiles("*.xml");
  35. if (files.Length == 0)
  36. {
  37. MessageShowBase.Infomation(@"菜单加载异常,请查看安装目录DLL\OperateLogs下的操作日志。");
  38. return null;
  39. }
  40. Dictionary<int, string> dictionary = new Dictionary<int, string>();
  41. #region 读取所有的文件,对所有的模块进行排序
  42. foreach (FileInfo info3 in files)
  43. {
  44. try
  45. {
  46. FileInfo info4 = info3;
  47. XmlDocument document = new XmlDocument();
  48. document.Load(info4.FullName);
  49. XmlElement xelem = document.DocumentElement;
  50. if (xelem == null) continue;
  51. int key = xelem.SelectSingleNode("App").SelectSingleNode("AppIndex").InnerText.Trim().ToInt();
  52. if (!dictionary.ContainsKey(key))
  53. {
  54. dictionary.Add(key, info4.FullName);
  55. }
  56. }
  57. catch (Exception exception)
  58. {
  59. MessageShow.Show(exception, false, "");
  60. }
  61. }
  62. #endregion
  63. List<TszTabData> list2 = new List<TszTabData>();
  64. List<int> pushButtonList = dictionary.Keys.ToList<int>();
  65. pushButtonList.Sort();
  66. foreach (int num2 in pushButtonList)
  67. {
  68. TszTabData[] collection = GetConfigTabs(strRevitVersion, listFea, dictionary[num2]);
  69. if ((collection != null) && (collection.Length > 0))
  70. {
  71. list2.AddRange(collection);
  72. }
  73. }
  74. return list2.ToArray();
  75. }
  76. /// <summary>
  77. /// 从菜单配置文件中读取数据,转化为为TszTabData
  78. /// Tab-Panels-Buttons
  79. /// </summary>
  80. /// <param name="strRevitVersion"></param>
  81. /// <param name="listFea"></param>
  82. /// <param name="fileName"></param>
  83. /// <returns></returns>
  84. public static TszTabData[] GetConfigTabs(string strRevitVersion, List<string> listFea, string fileName)
  85. {
  86. try
  87. {
  88. string str = Path.Combine(AppBaseInfo.AppRunPath, "RibbonImage");
  89. XmlDocument document = new XmlDocument();
  90. document.Load(fileName);
  91. XmlElement documentElement = document.DocumentElement;
  92. List<TszPanelData> list = new List<TszPanelData>();
  93. #region 获取所有的命令
  94. foreach (XmlNode node in documentElement.SelectNodes("Panel"))
  95. {
  96. string strPanelName = node.Attributes["PanelName"].Value.Trim();
  97. bool boolGroupFlag = bool.Parse(node.Attributes["GroupFlag"].Value.Trim());
  98. string str3 = node.Attributes["GroupImage"].Value.Trim();
  99. string strBtnsTyle = node.Attributes["ButtonStyles"].Value.Trim();
  100. string str5 = "Normal";
  101. string strRevitVer = node.Attributes["RevitVer"].Value.Trim();
  102. RevitVer revitVer = GetRevitVer(strRevitVer);
  103. ButtonStyles buttonStyle = GetButtonStyle(strBtnsTyle);
  104. StackStyles stackStyle = GetStackStyle(str5);
  105. if (str3.Length <= 0)
  106. {
  107. str3 = Path.Combine(str, "LargeImage.png");
  108. }
  109. else
  110. {
  111. str3 = Path.Combine(str, str3);
  112. }
  113. TszPanelData penelData = new TszPanelData(strPanelName, boolGroupFlag, str3, buttonStyle);
  114. #region 命令是否可见
  115. XmlAttribute attribute = node.Attributes["Visible"];
  116. if (attribute != null)
  117. {
  118. penelData.Visible = attribute.Value.Trim().ToBool();
  119. }
  120. #endregion
  121. XmlNodeList pushButtonList = node.SelectNodes("Button");
  122. List<TszButtonData> list4 = new List<TszButtonData>();
  123. foreach (XmlNode node2 in pushButtonList)
  124. {
  125. ButtonStyles styles3 = buttonStyle;
  126. StackStyles styles4 = stackStyle;
  127. RevitVer ver2 = revitVer;
  128. XmlAttribute attribute3 = node2.Attributes["ButtonStyles"];
  129. if (attribute3 != null)
  130. {
  131. styles3 = GetButtonStyle(attribute3.Value.Trim());
  132. }
  133. XmlAttribute attribute4 = node2.Attributes["StackStyles"];
  134. if (attribute4 != null)
  135. {
  136. styles4 = GetStackStyle(attribute4.Value.Trim());
  137. }
  138. XmlAttribute attribute5 = node2.Attributes["RevitVer"];
  139. if (attribute5 != null)
  140. {
  141. ver2 = GetRevitVer(attribute5.Value.Trim());
  142. }
  143. #region 调试的命令不添加
  144. var debugAttri = node2.Attributes["IsDebug"];
  145. if (debugAttri != null)
  146. {
  147. if (debugAttri.Value.ToBool())
  148. continue;
  149. }
  150. #endregion
  151. string menuTab = node2.SelectSingleNode("MenuTab").InnerText.Trim();
  152. string[] strArray = node2.SelectSingleNode("Modules").InnerText.Trim().Split(new char[] { ',' });
  153. TszButtonData data2 = null;
  154. if (node2.ChildNodes.Count > 6)
  155. {
  156. string strButtonName = node2.SelectSingleNode("ButtonName").InnerText.Trim();
  157. string strButtonText = node2.SelectSingleNode("ButtonText").InnerText.Trim();
  158. string str14 = node2.SelectSingleNode("ImageName").InnerText.Trim();
  159. str14 = Path.Combine(str, str14);
  160. string path = node2.SelectSingleNode("DllName").InnerText.Trim();
  161. //..\OutputDll\SpacePlugin.dll
  162. string strAssemblyName = Path.Combine(AppBaseInfo.AppRunPath, Path.GetFileName(path));
  163. string addStr = @"..\";
  164. if (path.Contains(addStr))
  165. {
  166. path = path.Replace(addStr, "");
  167. strAssemblyName = Path.Combine(AppBaseInfo.AppRunPath, path);
  168. }
  169. string strClassName = node2.SelectSingleNode("ClassName").InnerText.Trim();
  170. string str18 = node2.SelectSingleNode("ToolTip").InnerText.Trim();
  171. string str19 = node2.SelectSingleNode("LongDescription").InnerText.Trim();
  172. data2 = new TszButtonData(penelData, menuTab, strButtonName, strButtonText, str14,
  173. strAssemblyName, strClassName)
  174. {
  175. ToolTip = str18,
  176. LongDescription = str19
  177. };
  178. }
  179. else if (node2.ChildNodes.Count > 3)
  180. {
  181. string str20 = node2.SelectSingleNode("ButtonText").InnerText.Trim();
  182. string str21 = node2.SelectSingleNode("ImageName").InnerText.Trim();
  183. string str23 = node2.SelectSingleNode("ToolTip").InnerText.Trim();
  184. str21 = Path.Combine(str, str21);
  185. data2 = new TszButtonData(menuTab)
  186. {
  187. TszPanelData = penelData,
  188. ToolTip = str23,
  189. ButtonText = str20,
  190. ImageName = str21
  191. };
  192. }
  193. else
  194. {
  195. data2 = new TszButtonData(menuTab);
  196. }
  197. XmlAttribute btnVisibleAttri = node2.Attributes["Visible"];
  198. if (btnVisibleAttri != null)
  199. {
  200. data2.Visible = btnVisibleAttri.Value.Trim().ToBool();
  201. }
  202. data2.Styles = styles3;
  203. data2.StackStyle = styles4;
  204. data2.UseVersion = ver2;
  205. list4.Add(data2);
  206. }
  207. if (list4.Any())
  208. {
  209. penelData.Buttons.AddRange(list4);
  210. list.Add(penelData);
  211. }
  212. }
  213. #endregion
  214. List<TszTabData> list6 = new List<TszTabData>();
  215. #region 对命令进行重新分组
  216. XmlNodeList list5 = documentElement.SelectNodes("Tab");
  217. foreach (XmlNode node3 in list5)
  218. {
  219. string strTabName = node3.Attributes["TabName"].Value.Trim();
  220. string str25 = node3.SelectSingleNode("MenuTab").InnerText.Trim();
  221. string[] strArray2 = node3.SelectSingleNode("Modules").InnerText.Trim().Split(new char[] { ',' });
  222. List<TszPanelData> list7 = new List<TszPanelData>();
  223. foreach (TszPanelData data5 in list)
  224. {
  225. TszButtonData[] collection = data5.FilterButtons(str25);
  226. if ((collection != null) && (collection.Length > 0))
  227. {
  228. TszPanelData data6 =
  229. new TszPanelData(data5.PanelName, data5.GroupFlag, data5.GroupImage, data5.Styles)
  230. {
  231. Visible = data5.Visible
  232. };
  233. data6.Buttons.AddRange(collection);
  234. list7.Add(data6);
  235. }
  236. }
  237. TszTabData item = new TszTabData(strTabName)
  238. {
  239. TabText = str25,
  240. Modules = strArray2
  241. };
  242. if (list7.Any())
  243. {
  244. item.Panels.AddRange(list7);
  245. list6.Add(item);
  246. }
  247. }
  248. #endregion
  249. return list6.ToArray();
  250. }
  251. catch (Exception exception)
  252. {
  253. MessageShow.Show(exception, false, "");
  254. return null;
  255. }
  256. }
  257. /// <summary>
  258. /// 根据配置文件设置 获取Botton样式
  259. /// </summary>
  260. /// <param name="strBtnsTyle"></param>
  261. /// <returns></returns>
  262. public static ButtonStyles GetButtonStyle(string strBtnsTyle)
  263. {
  264. if (strBtnsTyle == null)
  265. return ButtonStyles.Pushdown;
  266. string str = strBtnsTyle.ToLower();
  267. if (str == "separator")
  268. {
  269. return ButtonStyles.Separator;
  270. }
  271. if (str == "large")
  272. {
  273. return ButtonStyles.Large;
  274. }
  275. if (str == "pushdown")
  276. {
  277. return ButtonStyles.Pushdown;
  278. }
  279. if (str == "stacked")
  280. {
  281. return ButtonStyles.Stacked;
  282. }
  283. if (str != "stackedbox")
  284. {
  285. return ButtonStyles.StackedBox;
  286. }
  287. return ButtonStyles.Pushdown;
  288. }
  289. /// <summary>
  290. /// 获取有哪些版本的命令可用 R14,R15,R16,R17,R18
  291. /// </summary>
  292. /// <param name="strRevitVer"></param>
  293. /// <returns></returns>
  294. public static RevitVer GetRevitVer(string strRevitVer)
  295. {
  296. RevitVer unkonw = RevitVer.Unkonw;
  297. if (strRevitVer.ToUpper().IndexOf("R14", StringComparison.Ordinal) > -1)
  298. {
  299. unkonw |= RevitVer.R14;
  300. }
  301. if (strRevitVer.ToUpper().IndexOf("R15", StringComparison.Ordinal) > -1)
  302. {
  303. unkonw |= RevitVer.R15;
  304. }
  305. if (strRevitVer.ToUpper().IndexOf("R16", StringComparison.Ordinal) > -1)
  306. {
  307. unkonw |= RevitVer.R16;
  308. }
  309. if (strRevitVer.ToUpper().IndexOf("R17", StringComparison.Ordinal) > -1)
  310. {
  311. unkonw |= RevitVer.R17;
  312. }
  313. return unkonw;
  314. }
  315. /// <summary>
  316. ///
  317. /// </summary>
  318. /// <param name="strBtnsTyle"></param>
  319. /// <returns></returns>
  320. public static StackStyles GetStackStyle(string strBtnsTyle)
  321. {
  322. StackStyles normal = StackStyles.Normal;
  323. if (strBtnsTyle == null) return normal;
  324. string str = strBtnsTyle.ToLower();
  325. if (str == "pulldown")
  326. {
  327. normal = StackStyles.Pulldown;
  328. }
  329. if (str == "normal")
  330. {
  331. normal = StackStyles.Normal;
  332. }
  333. return normal;
  334. }
  335. }
  336. }