PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/VisioAutomation_2007/VisioAutomation/UserDefinedCells/UserDefinedCellHelper.cs

#
C# | 340 lines | 252 code | 67 blank | 21 comment | 57 complexity | 2cd97f8ea9260b7371c81afcb5998903 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using VisioAutomation.Extensions;
  5. using VA = VisioAutomation;
  6. using IVisio = Microsoft.Office.Interop.Visio;
  7. namespace VisioAutomation.UserDefinedCells
  8. {
  9. public static class UserDefinedCellsHelper
  10. {
  11. public static string GetRowName(string name)
  12. {
  13. return "User." + name;
  14. }
  15. public static void DeleteUserDefinedCell(IVisio.Shape shape, string name)
  16. {
  17. if (shape == null)
  18. {
  19. throw new ArgumentNullException("shape");
  20. }
  21. if (name == null)
  22. {
  23. throw new ArgumentNullException("name");
  24. }
  25. CheckValidUserDefinedCellName(name);
  26. string full_prop_name = GetRowName(name);
  27. short row = shape.CellsU[full_prop_name].Row;
  28. shape.DeleteRow(UserDefinedCell.query.Section, row);
  29. }
  30. public static void UpdateUserDefinedCell(IVisio.Shape shape, string name, string val)
  31. {
  32. if (shape == null)
  33. {
  34. throw new ArgumentNullException("shape");
  35. }
  36. CheckValidUserDefinedCellName(name);
  37. if (val == null)
  38. {
  39. throw new ArgumentNullException("val");
  40. }
  41. if (!HasUserDefinedCell(shape, name))
  42. {
  43. throw new AutomationException("user Property does not exist");
  44. }
  45. string full_prop_name = GetRowName(name);
  46. var cell = shape.CellsU[full_prop_name];
  47. if (cell == null)
  48. {
  49. string msg = String.Format("Could not retrieve cell for user property \"{0}\"", full_prop_name);
  50. throw new AutomationException(msg);
  51. }
  52. var update = new VA.ShapeSheet.Update.SRCUpdate();
  53. var src = new VA.ShapeSheet.SRC(UserDefinedCell.query.Section, cell.Row, (short)IVisio.VisCellIndices.visUserValue);
  54. update.SetFormula(src, val);
  55. update.Execute(shape);
  56. }
  57. public static void SetUserDefinedCell(IVisio.Shape shape, string name, string value, string prompt)
  58. {
  59. if (shape == null)
  60. {
  61. throw new ArgumentNullException("shape");
  62. }
  63. CheckValidUserDefinedCellName(name);
  64. if (HasUserDefinedCell(shape, name))
  65. {
  66. DeleteUserDefinedCell(shape, name);
  67. }
  68. short row = shape.AddNamedRow(
  69. UserDefinedCell.query.Section,
  70. name,
  71. (short)IVisio.VisRowIndices.visRowUser);
  72. var update = new VA.ShapeSheet.Update.SRCUpdate();
  73. if (value != null)
  74. {
  75. string value_formula = Convert.StringToFormulaString(value);
  76. var src = new VA.ShapeSheet.SRC(UserDefinedCell.query.Section, row, (short)IVisio.VisCellIndices.visUserValue);
  77. update.SetFormula(src, value_formula);
  78. }
  79. if (prompt != null)
  80. {
  81. string prompt_formula = Convert.StringToFormulaString(prompt);
  82. var src = new VA.ShapeSheet.SRC(UserDefinedCell.query.Section, row, (short)IVisio.VisCellIndices.visUserPrompt);
  83. update.SetFormula(src, prompt_formula);
  84. }
  85. update.Execute(shape);
  86. }
  87. /// <summary>
  88. /// Gets all the user properties defined on a shape
  89. /// </summary>
  90. /// <remarks>
  91. /// If there are no user properties then null will be returned</remarks>
  92. /// <param name="shape"></param>
  93. /// <returns>A list of user properties</returns>
  94. public static IList<UserDefinedCell> GetUserDefinedCells(IVisio.Shape shape)
  95. {
  96. if (shape == null)
  97. {
  98. throw new ArgumentNullException("shape");
  99. }
  100. var prop_count = GetUserDefinedCellCount(shape);
  101. if (prop_count < 1)
  102. {
  103. return new List<UserDefinedCell>(0);
  104. }
  105. var prop_names = GetUserDefinedCellNames(shape);
  106. if (prop_names.Count != prop_count)
  107. {
  108. throw new AutomationException("Unexpected number of prop names");
  109. }
  110. var formulas = UserDefinedCell.query.GetFormulas(shape);
  111. var rows = new List<int>(formulas.Count);
  112. for (int row = 0; row < formulas.Count; row++)
  113. {
  114. rows.Add(row);
  115. }
  116. var custom_props = create_userdefined_cell_list(prop_names, formulas, rows);
  117. return custom_props;
  118. }
  119. public static IList<List<UserDefinedCell>> GetUserDefinedCells(IVisio.Page page, IList<IVisio.Shape> shapes)
  120. {
  121. if (page == null)
  122. {
  123. throw new ArgumentNullException("page");
  124. }
  125. if (shapes == null)
  126. {
  127. throw new ArgumentNullException("shapes");
  128. }
  129. var shapeids = shapes.Select(s => s.ID).ToList();
  130. var formulas = UserDefinedCell.query.GetFormulas(page, shapeids);
  131. var custom_props = new List<List<UserDefinedCell>>(shapeids.Count);
  132. for (int shape_index = 0; shape_index < shapeids.Count; shape_index++)
  133. {
  134. var group = formulas.Groups[shape_index];
  135. var shape = shapes[shape_index];
  136. var prop_names = GetUserDefinedCellNames(shape);
  137. var custom_props_for_shape = create_userdefined_cell_list(prop_names, formulas, group.RowIndices.ToList());
  138. custom_props.Add(custom_props_for_shape);
  139. }
  140. return custom_props;
  141. }
  142. public static List<UserDefinedCell> create_userdefined_cell_list(
  143. IList<string> prop_names,
  144. VA.ShapeSheet.Data.Table<string> formulas,
  145. IList<int> rows)
  146. {
  147. var custom_props = new List<UserDefinedCell>();
  148. int name_index = 0;
  149. foreach (int row in rows)
  150. {
  151. var prop_name = prop_names[name_index];
  152. var custom_prop = new UserDefinedCell(prop_name);
  153. custom_prop.Value = formulas[row, UserDefinedCell.query.Value];
  154. custom_prop.Prompt = formulas[row, UserDefinedCell.query.Prompt];
  155. custom_props.Add(custom_prop);
  156. name_index++;
  157. }
  158. return custom_props;
  159. }
  160. /// <summary>
  161. /// Get the number of user-defined cells for the shape.
  162. /// </summary>
  163. /// <param name="shape"></param>
  164. /// <returns></returns>
  165. public static int GetUserDefinedCellCount(IVisio.Shape shape)
  166. {
  167. if (shape == null)
  168. {
  169. throw new ArgumentNullException("shape");
  170. }
  171. // If the User Property section does not exist then return zero immediately
  172. if (0 == shape.SectionExists[UserDefinedCell.query.Section, (short)IVisio.VisExistsFlags.visExistsAnywhere])
  173. {
  174. return 0;
  175. }
  176. var section = shape.Section[UserDefinedCell.query.Section];
  177. if (section == null)
  178. {
  179. string msg = String.Format("Could not find the user-defined section for shape {0}", shape.NameU);
  180. throw new AutomationException(msg);
  181. }
  182. int row_count = section.Shape.RowCount[UserDefinedCell.query.Section];
  183. return row_count;
  184. }
  185. /// <summary>
  186. /// Returns all the Names of the user-defined cells
  187. /// </summary>
  188. /// <remarks>
  189. /// names of user defined cells are not queryable get GetResults & GetFormulas
  190. /// </remarks>
  191. /// <param name="shape"></param>
  192. /// <returns></returns>
  193. public static IList<string> GetUserDefinedCellNames(IVisio.Shape shape)
  194. {
  195. if (shape == null)
  196. {
  197. throw new ArgumentNullException("shape");
  198. }
  199. int user_prop_row_count = GetUserDefinedCellCount(shape);
  200. if (user_prop_row_count < 1)
  201. {
  202. return new List<string>(0);
  203. }
  204. var prop_names = new List<string>(user_prop_row_count);
  205. var prop_section = shape.Section[UserDefinedCell.query.Section];
  206. var query_names = prop_section.AsEnumerable().Select(row => row.NameU);
  207. prop_names.AddRange(query_names);
  208. if (user_prop_row_count != prop_names.Count)
  209. {
  210. throw new AutomationException("Unexpected number of property names");
  211. }
  212. return prop_names;
  213. }
  214. public static bool IsValidName(string name)
  215. {
  216. if (name == null)
  217. {
  218. return false;
  219. }
  220. if (name.Length < 1)
  221. {
  222. return false;
  223. }
  224. return true;
  225. }
  226. public static void CheckValidName(string name)
  227. {
  228. if (!IsValidName(name))
  229. {
  230. throw new System.ArgumentException("name");
  231. }
  232. }
  233. public static bool IsValidUserDefinedCellName(string name)
  234. {
  235. if (name == null)
  236. {
  237. return false;
  238. }
  239. if (name.Length < 1)
  240. {
  241. return false;
  242. }
  243. if (name.Contains(" ") || name.Contains("\t") || name.Contains("\r") || name.Contains("\n"))
  244. {
  245. return false;
  246. }
  247. return true;
  248. }
  249. public static void CheckValidUserDefinedCellName(string name)
  250. {
  251. if (!IsValidUserDefinedCellName(name))
  252. {
  253. string msg = String.Format("Invalid Property Name: \"{0}\"", name);
  254. throw new VA.AutomationException(msg);
  255. }
  256. }
  257. public static bool HasUserDefinedCell(IVisio.Shape shape, string name)
  258. {
  259. if (shape == null)
  260. {
  261. throw new System.ArgumentNullException("shape");
  262. }
  263. if (name == null)
  264. {
  265. throw new System.ArgumentNullException("name");
  266. }
  267. CheckValidUserDefinedCellName(name);
  268. string full_prop_name = GetRowName(name);
  269. var exists = (short)IVisio.VisExistsFlags.visExistsAnywhere;
  270. return 0 != (shape.CellExistsU[full_prop_name, exists]);
  271. }
  272. }
  273. }