COMObjManage.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using LabelManager2;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Windows.Forms;
  11. using UAS_MES_NEW.DataOperate;
  12. using UAS_MES_NEW.Entity;
  13. namespace UAS_MES_NEW.PublicMethod
  14. {
  15. internal static class COMObjManage
  16. {
  17. private static readonly object _lock = new object();
  18. private static ApplicationClass _lblApp;
  19. private static BarTender.Application _btApp;
  20. private static bool _isInitialized = false;
  21. static DataHelper dh = SystemInf.dh;
  22. static Document doc;
  23. static FileInfo info;
  24. static BarTender.Format doc2;
  25. public static void Initialize()
  26. {
  27. lock (_lock)
  28. {
  29. if (!_isInitialized)
  30. {
  31. try
  32. {
  33. _btApp = new BarTender.Application();
  34. _lblApp = new ApplicationClass();
  35. BaseUtil.WriteLbl();
  36. _isInitialized = true;
  37. }
  38. catch (Exception ex)
  39. {
  40. Cleanup();
  41. throw new Exception("初始化COM资源失败: " + ex.Message, ex);
  42. }
  43. }
  44. }
  45. }
  46. public static BarTender.Application GetBarTenderApplication()
  47. {
  48. lock (_lock)
  49. {
  50. if (!_isInitialized)
  51. {
  52. Initialize();
  53. }
  54. if (_btApp == null)
  55. {
  56. throw new InvalidOperationException("BarTender应用程序未初始化");
  57. }
  58. return _btApp;
  59. }
  60. }
  61. public static ApplicationClass GetLabelManagerApplication()
  62. {
  63. lock (_lock)
  64. {
  65. if (!_isInitialized)
  66. {
  67. Initialize();
  68. }
  69. if (_lblApp == null)
  70. {
  71. throw new InvalidOperationException("LabelManager应用程序未初始化");
  72. }
  73. return _lblApp;
  74. }
  75. }
  76. public static void Cleanup()
  77. {
  78. lock (_lock)
  79. {
  80. try
  81. {
  82. if (_btApp != null)
  83. {
  84. _btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
  85. Marshal.ReleaseComObject(_btApp);
  86. _btApp = null;
  87. }
  88. if (_lblApp != null)
  89. {
  90. _lblApp.Quit();
  91. Marshal.ReleaseComObject(_lblApp);
  92. _lblApp = null;
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. System.Diagnostics.Debug.WriteLine("清理COM资源时出错: " + ex.Message);
  98. }
  99. finally
  100. {
  101. _isInitialized = false;
  102. }
  103. }
  104. }
  105. public static void SafePrint(Action<BarTender.Application, ApplicationClass> printAction)
  106. {
  107. lock (_lock)
  108. {
  109. if (!_isInitialized)
  110. {
  111. Initialize();
  112. }
  113. try
  114. {
  115. printAction(_btApp, _lblApp);
  116. }
  117. catch (Exception ex)
  118. {
  119. throw new Exception("执行打印操作时出错: " + ex.Message, ex);
  120. }
  121. finally
  122. {
  123. // 根据需求决定是否在每次操作后清理资源, 如果频繁打印,不需要每次清理
  124. // Cleanup();
  125. }
  126. }
  127. }
  128. public static bool CodeSoft(string iCaller, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  129. {
  130. ErrorMessage = "";
  131. DataTable dt = new DataTable();
  132. if (IfRePrint != "-1")
  133. {
  134. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  135. {
  136. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  137. }
  138. else
  139. {
  140. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  141. }
  142. if (dh.getFieldDataByCondition("employee", "em_type", "em_code = '" + User.UserCode + "'").ToString() != "admin")
  143. {
  144. //如果已经打印过了,则不允许再打印
  145. if (dt.Rows.Count > 0)
  146. {
  147. ErrorMessage = SnCode + LabelType + "已打印";
  148. return false;
  149. }
  150. }
  151. }
  152. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  153. FileInfo PrintFile = new FileInfo(LabelName);
  154. //打开模板路径
  155. //查询模板对应的取值SQL和参数名称
  156. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  157. StringBuilder sb = new StringBuilder();
  158. if (!PrintFile.Exists)
  159. {
  160. MessageBox.Show("打印文件不存在");
  161. return false;
  162. }
  163. string filechangetime = PrintFile.LastWriteTime.ToString();
  164. if (filechangetime != filelastwritetime)
  165. {
  166. _lblApp.Quit();
  167. _lblApp = new ApplicationClass();
  168. BaseUtil.WriteLbl();
  169. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  170. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  171. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  172. }
  173. doc = _lblApp.Documents.Open(LabelName, true);
  174. if (doc == null)
  175. {
  176. MessageBox.Show("标签文件打开失败");
  177. return false;
  178. }
  179. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  180. {
  181. doc.Variables.FormVariables.Item(k + 1).Value = null;
  182. }
  183. //执行全部的SQL
  184. for (int i = 0; i < dt.Rows.Count; i++)
  185. {
  186. string sql = dt.Rows[i]["lp_sql"].ToString();
  187. try
  188. {
  189. Regex ConnoteA = new Regex("{\\w+}");
  190. foreach (Match mch in ConnoteA.Matches(sql))
  191. {
  192. string x = mch.Value.Trim();
  193. sql = sql.Replace(x, "'" + SnCode + "'");
  194. }
  195. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  196. if (Param.Rows.Count == 0)
  197. continue;
  198. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  199. if (Param.Rows.Count > 0)
  200. {
  201. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  202. for (int j = 0; j < LoopTime; j++)
  203. {
  204. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  205. {
  206. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  207. {
  208. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  209. }
  210. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  211. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  212. {
  213. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  214. }
  215. }
  216. }
  217. }
  218. }
  219. catch (System.Exception ex)
  220. {
  221. MessageBox.Show("SQL维护不正确");
  222. }
  223. }
  224. LogManager.DoLog(sb.ToString());
  225. //保存本次赋值进行打印
  226. doc.Printer.SwitchTo(PrinterName);
  227. doc.PrintDocument(PrintNum);
  228. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  229. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  230. {
  231. doc.Variables.FormVariables.Item(k + 1).Value = null;
  232. }
  233. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  234. return true;
  235. }
  236. public static bool SinglePrint(string iCaller, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  237. {
  238. ErrorMessage = "";
  239. DataTable dt = new DataTable();
  240. if (IfRePrint != "-1")
  241. {
  242. if (LabelType == "卡通箱标" || LabelType == "彩盒标" || LabelType == "栈板标" || LabelType == "机身标")
  243. {
  244. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  245. }
  246. else
  247. {
  248. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  249. }
  250. if (dh.getFieldDataByCondition("employee", "em_type", "em_code = '" + User.UserCode + "'").ToString() != "admin")
  251. {
  252. ////如果已经打印过了,则不允许再打印
  253. if (dt.Rows.Count > 0)
  254. {
  255. ErrorMessage = SnCode + LabelType + "已打印";
  256. return false;
  257. }
  258. }
  259. }
  260. // string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  261. // FileInfo PrintFile = new FileInfo(LabelName);
  262. //打开模板路径
  263. //查询模板对应的取值SQL和参数名称
  264. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  265. //StringBuilder sb = new StringBuilder();
  266. //if (!PrintFile.Exists)
  267. //{
  268. // MessageBox.Show("打印文件不存在");
  269. // return false;
  270. //}
  271. //string filechangetime = PrintFile.LastWriteTime.ToString();
  272. //if (filechangetime != filelastwritetime)
  273. //{
  274. // lbl.Dispose();
  275. // lbl = new Engine();
  276. // // BaseUtil.WriteLbl();
  277. // filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  278. // string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  279. // dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  280. //}
  281. doc2 = _btApp.Formats.Open(LabelName);
  282. if (doc2 == null)
  283. {
  284. MessageBox.Show("标签文件打开失败");
  285. return false;
  286. }
  287. //执行全部的SQL
  288. for (int i = 0; i < dt.Rows.Count; i++)
  289. {
  290. string sql = dt.Rows[i]["lp_sql"].ToString();
  291. try
  292. {
  293. Regex ConnoteA = new Regex("{\\w+}");
  294. foreach (Match mch in ConnoteA.Matches(sql))
  295. {
  296. string x = mch.Value.Trim();
  297. sql = sql.Replace(x, "'" + SnCode + "'");
  298. }
  299. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  300. if (Param.Rows.Count == 0)
  301. continue;
  302. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  303. if (Param.Rows.Count > 0)
  304. {
  305. int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count;
  306. for (int j = 0; j < LoopTime; j++)
  307. {
  308. for (int k = 0; k < doc2.NamedSubStrings.Count; k++)
  309. {
  310. if (j == 0 & doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  311. {
  312. doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[0][0].ToString());
  313. }
  314. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  315. //if (j != 0 & doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j))
  316. //{
  317. // doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[j][0].ToString());
  318. //}
  319. if (doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  320. {
  321. doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[j][0].ToString());
  322. }
  323. }
  324. }
  325. }
  326. }
  327. catch (System.Exception ex)
  328. {
  329. MessageBox.Show("SQL维护不正确" + ex.Message);
  330. }
  331. }
  332. // LogManager.DoLog(sb.ToString());
  333. //保存本次赋值进行打印
  334. // doc2.Printer.SwitchTo(PrinterName);
  335. // 同样标签的份数
  336. doc2.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  337. // 序列标签数
  338. doc2.PrintSetup.NumberSerializedLabels = 1;
  339. doc2.PrintSetup.Printer = PrinterName;
  340. doc2.PrintOut(false, false);
  341. doc2.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
  342. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  343. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  344. //打印完毕
  345. _btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
  346. return true;
  347. }
  348. }
  349. }