XmlMenuData.cs 15 KB

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