Startup.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. using PaintDotNet.Base.SettingModel;
  2. using PaintDotNet.Base.CommTool;
  3. using PaintDotNet.DbOpreate.DbBll;
  4. using PaintDotNet.DbOpreate.DbModel;
  5. using PaintDotNet.ImageCollect;
  6. using PaintDotNet.SystemLayer;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Data.Entity.Core.Mapping;
  10. using System.Data.Entity.Core.Metadata.Edm;
  11. using System.Data.Entity.Infrastructure;
  12. using System.Diagnostics;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Windows.Forms;
  20. using System.Configuration;
  21. using SenseShield;
  22. using TUCamera;
  23. using System.Drawing;
  24. using Metis.ParameterSet;
  25. using PaintDotNet.Annotation;
  26. using PaintDotNet.Base;
  27. using StageController;
  28. using System.IO.Ports;
  29. namespace PaintDotNet
  30. {
  31. internal sealed class Startup
  32. {
  33. public static Startup instance;
  34. public SplashForm splashForm = null;
  35. /// <summary>
  36. /// 相机是否合法
  37. /// </summary>
  38. public static bool cameraSNLegal = false;
  39. /// <summary>
  40. /// 启动时传进来的参数,需要进行解析
  41. /// 在自定义快捷方式的时候会用到
  42. /// 读取不同的配置路径的配置信息
  43. /// </summary>
  44. private string[] args;
  45. /// <summary>
  46. /// 主窗体
  47. /// </summary>
  48. private MainForm mainForm;
  49. /// <summary>
  50. /// 设置信息所在的文件夹
  51. /// </summary>
  52. public string SettingPrefix = "Default";
  53. /// <summary>
  54. /// 常规设置-用户信息
  55. /// </summary>
  56. public UserInfoModel userInfo;
  57. /// <summary>
  58. /// 常规设置-网格信息
  59. /// </summary>
  60. public GridModel gridModel;
  61. /// <summary>
  62. /// 系统配置
  63. /// </summary>
  64. public ConfigModel configModel;
  65. /// <summary>
  66. /// 载物台配置
  67. /// </summary>
  68. public LoadingStageModel loadingStageModel;
  69. /// <summary>
  70. /// 快捷键配置
  71. /// </summary>
  72. public HotkeyModel hotkeyModel;
  73. /// <summary>
  74. /// 工具栏
  75. /// </summary>
  76. public ToolbarModel toolbarModel;
  77. /// <summary>
  78. /// 工具栏坐标
  79. /// </summary>
  80. //public ToolbarLocationModel toolbarLocationModel;
  81. /// <summary>
  82. /// 水印配置
  83. /// </summary>
  84. public WatermarkModel watermarkModel;
  85. /// <summary>
  86. /// 工字线配置
  87. /// </summary>
  88. public WorkTypeStyleModel workTypeStyleModel;
  89. /// <summary>
  90. /// 标尺配置
  91. /// </summary>
  92. public RulerModel rulerModel;
  93. /// <summary>
  94. /// 工作流程
  95. /// </summary>
  96. public WorkFlowModel workFlowModel;
  97. /// <summary>
  98. /// 快捷栏
  99. /// </summary>
  100. public ShortcutbarModel shortcutbarModel;
  101. /// <summary>
  102. /// 标注样式信息
  103. /// </summary>
  104. public LabelStyleModel labelStyleModel;
  105. /// <summary>
  106. /// 测量样式信息
  107. /// </summary>
  108. public MeasureStyleModel measureStyleModel;
  109. /// <summary>
  110. /// 设置-常规设置-辅助线样式信息
  111. /// </summary>
  112. public GuideStyleModel guideStyleModel;
  113. /// <summary>
  114. /// 换算完的标尺信息,包含所有系统内得单位
  115. /// </summary>
  116. public Dictionary<MeasurementUnit, double> rules = new Dictionary<MeasurementUnit, double>();
  117. /// <summary>
  118. /// 系统内选定的单位
  119. /// </summary>
  120. public MeasurementUnit measurementUnit;
  121. /// <summary>
  122. /// 相机参数
  123. /// </summary>
  124. public CameraParamModel cameraParamModel;
  125. /// <summary>
  126. /// 当前系统标尺
  127. /// </summary>
  128. public mic_rulers ruleDB;
  129. /// <summary>
  130. /// 标尺集合
  131. /// </summary>
  132. public List<mic_rulers> mic_rulersAll;
  133. /// <summary>
  134. /// 单位对应的枚举数字
  135. /// </summary>
  136. public Dictionary<string, int> companyEnume;
  137. /// <summary>
  138. /// 工艺图比照
  139. /// </summary>
  140. public ArtworkModel artworkModel;
  141. /// <summary>
  142. /// 电动偏光设置
  143. /// </summary>
  144. public MotorizedLightModel motorizedLightModel;
  145. /// <summary>
  146. /// 电压设置
  147. /// </summary>
  148. public VoltageModel voltageModel;
  149. /// <summary>
  150. /// 图像处理参数
  151. /// </summary>
  152. public ImageMenu imageMenuModel;
  153. /// <summary>
  154. /// 二值处理参数
  155. /// </summary>
  156. public ImageMenu binaryActionModel;
  157. /// <summary>
  158. /// 是否开启加密锁验证
  159. /// </summary>
  160. private static/*const*/ bool useSenseShield = false;//true;//
  161. private static bool haveShowSenseAlert = false;
  162. /// <summary>
  163. /// 将value为FALSE的菜单隐藏显示,key为要隐藏的menuID
  164. /// </summary>
  165. private static Dictionary<uint, bool> shieldValue = new Dictionary<uint, bool>();
  166. public int step_length = 1;
  167. /// <summary>
  168. /// 构造函数
  169. /// </summary>
  170. /// <param name="args"></param>
  171. private Startup(string[] args)
  172. {
  173. this.args = args;
  174. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  175. config.ConnectionStrings.ConnectionStrings["DBEntities"].ConnectionString = "Data Source=" + Application.StartupPath + @"\microscope.db3";
  176. config.Save(ConfigurationSaveMode.Modified);
  177. ConfigurationManager.RefreshSection("connectionStrings");
  178. //用于EF暖机操作,以避免第一次度数据库过慢
  179. using (var dbcontext = new PaintDotNet.DbOpreate.DBEntities())
  180. {
  181. var objectContext = ((IObjectContextAdapter)dbcontext).ObjectContext;
  182. var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
  183. mappingCollection.GenerateViews(new List<EdmSchemaError>());
  184. }
  185. }
  186. /// <summary>
  187. /// 启动一个实例
  188. /// </summary>
  189. /// <param name="parent"></param>
  190. /// <param name="requireAdmin">是否需要管理员权限</param>
  191. /// <param name="args">传递进来的参数</param>
  192. public static void StartNewInstance(IWin32Window parent, bool requireAdmin, string[] args)
  193. {
  194. StringBuilder allArgsSB = new StringBuilder();
  195. foreach (string arg in args)
  196. {
  197. allArgsSB.Append(' ');
  198. if (arg.IndexOf(' ') != -1)
  199. {
  200. allArgsSB.Append('"');
  201. }
  202. allArgsSB.Append(arg);
  203. if (arg.IndexOf(' ') != -1)
  204. {
  205. allArgsSB.Append('"');
  206. }
  207. }
  208. string allArgs;
  209. if (allArgsSB.Length > 0)
  210. {
  211. allArgs = allArgsSB.ToString(1, allArgsSB.Length - 1);
  212. }
  213. else
  214. {
  215. allArgs = null;
  216. }
  217. Shell.Execute(
  218. parent,
  219. Application.ExecutablePath,
  220. allArgs,
  221. requireAdmin ? ExecutePrivilege.RequireAdmin : ExecutePrivilege.AsInvokerOrAsManifest,
  222. ExecuteWaitType.ReturnImmediately);
  223. }
  224. /// <summary>
  225. /// 检查需要的文件是否存在
  226. /// 并且尝试修复
  227. /// </summary>
  228. /// <returns>
  229. /// 返回true则说明缺少东西
  230. /// 返回false则说明都正常
  231. /// </returns>
  232. private bool CheckForImportantFiles()
  233. {
  234. string[] requiredFiles =
  235. new string[]
  236. {
  237. "ICSharpCode.SharpZipLib.dll",
  238. "PaintDotNet.Base.dll",
  239. "PaintDotNet.Core.dll",
  240. "PaintDotNet.Data.dll",
  241. "PaintDotNet.Resources.dll",
  242. "PaintDotNet.Strings.3.DE-DE.resources",
  243. "PaintDotNet.Strings.3.EN-US.resources",
  244. "PaintDotNet.Strings.3.ZH-CN.resources",
  245. "PaintDotNet.SystemLayer.dll",
  246. "SetupNgen.exe",
  247. "ShellExtension_x64.dll",
  248. "ShellExtension_x86.dll"
  249. /*"Squish_x64.dll",
  250. "Squish_x86.dll",
  251. "Squish_x86_SSE2.dll"*/
  252. };
  253. string dirName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  254. List<string> missingFiles = null;
  255. foreach (string requiredFile in requiredFiles)
  256. {
  257. bool missing;
  258. try
  259. {
  260. string pathName = Path.Combine(dirName, requiredFile);
  261. FileInfo fileInfo = new FileInfo(pathName);
  262. missing = !fileInfo.Exists;
  263. }
  264. catch (Exception)
  265. {
  266. missing = true;
  267. }
  268. if (missing)
  269. {
  270. if (missingFiles == null)
  271. {
  272. missingFiles = new List<string>();
  273. }
  274. missingFiles.Add(requiredFile);
  275. }
  276. }
  277. if (missingFiles == null)
  278. {
  279. return false;
  280. }
  281. else
  282. {
  283. if (Shell.ReplaceMissingFiles(missingFiles.ToArray()))
  284. {
  285. // Everything is repaired and happy.
  286. return true;
  287. }
  288. else
  289. {
  290. // Things didn't get fixed. Bail.
  291. Process.GetCurrentProcess().Kill();
  292. return false;
  293. }
  294. }
  295. }
  296. public void Start()
  297. {
  298. // Initialize some misc. Windows Forms settings
  299. Application.SetCompatibleTextRenderingDefault(false);
  300. Application.EnableVisualStyles();
  301. //StartAnimation();
  302. // If any files are missing, try to repair.
  303. // However, support /skipRepairAttempt for when developing in the IDE
  304. // so that we don't needlessly try to repair in that case.
  305. if (this.args.Length > 0 &&
  306. string.Compare(this.args[0], "/skipRepairAttempt", StringComparison.InvariantCultureIgnoreCase) == 0)
  307. {
  308. // do nothing: we need this so that we can run from IDE/debugger
  309. // without it trying to repair itself all the time
  310. }
  311. else
  312. {
  313. if (CheckForImportantFiles())
  314. {
  315. Startup.StartNewInstance(null, true, args);
  316. return;
  317. }
  318. }
  319. // The rest of the code is put in a separate method so that certain DLL's
  320. // won't get delay loaded until after we try to do repairs.
  321. StartPart2();
  322. }
  323. /// <summary>
  324. /// 设置语言、地区等
  325. /// </summary>
  326. private void StartPart2()
  327. {
  328. CheckCameraSN();
  329. InitStageController();
  330. string locale = Settings.CurrentUser.GetString(SettingNames.LanguageName, null);
  331. if (locale == null)
  332. {
  333. locale = Settings.SystemWide.GetString(SettingNames.LanguageName, null);
  334. }
  335. if (locale != null)
  336. {
  337. try
  338. {
  339. CultureInfo ci = new CultureInfo(locale, true);
  340. Thread.CurrentThread.CurrentUICulture = ci;
  341. }
  342. catch (Exception)
  343. {
  344. // Don't want bad culture name to crash us
  345. }
  346. }
  347. // Check system requirements
  348. if (!OS.CheckOSRequirement())
  349. {
  350. string message = PdnResources.GetString("Error.OSRequirement");
  351. Utility.ErrorBox(null, message);
  352. return;
  353. }
  354. // Parse command-line arguments
  355. if (this.args.Length == 1) // && this.args[0] == Updates.UpdatesOptionsDialog.CommandLineParameter
  356. {
  357. //Updates.UpdatesOptionsDialog.ShowUpdateOptionsDialog(null, false);
  358. }
  359. else
  360. {
  361. SingleInstanceManager singleInstanceManager = new SingleInstanceManager("hero");
  362. // If this is not the first instance of PDN.exe, then forward the command-line
  363. // parameters over to the first instance.
  364. if (!singleInstanceManager.IsFirstInstance)
  365. {
  366. singleInstanceManager.FocusFirstInstance();
  367. foreach (string arg in this.args)
  368. {
  369. singleInstanceManager.SendInstanceMessage(arg, 30);
  370. }
  371. singleInstanceManager.Dispose();
  372. singleInstanceManager = null;
  373. return;
  374. }
  375. // Create main window
  376. this.mainForm = new MainForm(this.args);
  377. this.mainForm.SingleInstanceManager = singleInstanceManager;
  378. singleInstanceManager = null; // mainForm owns it now
  379. if (useSenseShield)
  380. StartPart3();
  381. // 3 2 1 go
  382. Application.Run(this.mainForm);
  383. try
  384. {
  385. this.mainForm.Dispose();
  386. TUCameraManager.GetInstance().UnInitManager();
  387. AxisController.GetInstance().Close();
  388. }
  389. catch (Exception)
  390. {
  391. }
  392. this.mainForm = null;
  393. }
  394. }
  395. /// <summary>
  396. /// 设置定时方法
  397. /// </summary>
  398. private void StartPart3()
  399. {
  400. //主线程中启动一个支线程,执行doSomething这样的一个方法。
  401. Thread thread = new Thread(new ThreadStart(ThreadRun));
  402. thread.IsBackground = true;//这样能随主程序一起结束
  403. thread.Start();
  404. //Console.ReadKey();
  405. }
  406. delegate void Delegate_do();
  407. private void ThreadRun()
  408. {
  409. try
  410. {
  411. Delegate_do Delegate_do = new Delegate_do(FindAllProduct);
  412. Thread.Sleep(30000);//隔30秒后判断一下是否完成
  413. IAsyncResult result = Delegate_do.BeginInvoke(null, null);
  414. while (!result.IsCompleted)
  415. {
  416. Console.WriteLine("子线程未完成2");
  417. Thread.Sleep(10000);//每隔10秒判断一下是否完成
  418. }
  419. Console.WriteLine("-------子线程已完成-------");
  420. }
  421. catch (Exception ex)
  422. {
  423. Console.WriteLine(ex.Message);
  424. }
  425. }
  426. private void FindAllProduct()
  427. {
  428. List<uint> moduleList = new List<uint>() { 1 };
  429. while (true)
  430. {
  431. try
  432. {
  433. uint[] moduleIds = moduleList.ToArray();
  434. moduleIds = DogDecrypting.decryptingModules(4/*, moduleIds*/);//第一个参数固定为4
  435. Console.WriteLine("颁发许可发现。");
  436. }
  437. catch (Exception ex)
  438. {
  439. if (!haveShowSenseAlert)
  440. {
  441. Console.WriteLine("发现错误:" + ex.Message);
  442. haveShowSenseAlert = true;
  443. ShowMessage(ex.Message);
  444. }
  445. while (senseShieldAutoLoop(moduleList) == false)
  446. {
  447. Console.WriteLine("仍然错误:颁发许可未发现");
  448. System.Threading.Thread.Sleep(20);
  449. }
  450. }
  451. Console.WriteLine("子线程未完成1");
  452. Thread.Sleep(10000);//每隔10秒判断一下是否发现颁发许可
  453. }
  454. }
  455. private bool senseShieldAutoLoop(List<uint> moduleList)
  456. {
  457. try
  458. {
  459. uint[] moduleIds = moduleList.ToArray();
  460. moduleIds = DogDecrypting.decryptingModules(4/*, moduleIds*/);//第一个参数固定为4
  461. }
  462. catch (Exception ex)
  463. {
  464. if (!haveShowSenseAlert)
  465. {
  466. haveShowSenseAlert = true;
  467. ShowMessage(ex.Message);
  468. }
  469. return false;
  470. }
  471. return true;
  472. }
  473. private void ShowMessage(string msg)
  474. {
  475. this.mainForm.Invoke(new MessageBoxShow(MessageBoxShow_F), new object[] { msg });
  476. }
  477. delegate void MessageBoxShow(string msg);
  478. void MessageBoxShow_F(string msg)
  479. {
  480. if (MessageBox.Show(msg, "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
  481. haveShowSenseAlert = false;
  482. }
  483. /// <summary>
  484. /// 程序主入口
  485. /// </summary>
  486. [STAThread]
  487. public static int Main(string[] args)
  488. {
  489. //这是要求管理员权限的版本
  490. /*//当前用户是管理员的时候,直接启动应用程序
  491. //如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
  492. //获得当前登录的Windows用户标示
  493. System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
  494. //创建Windows用户主题
  495. Application.EnableVisualStyles();
  496. System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
  497. //判断当前登录用户是否为管理员
  498. if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
  499. {
  500. //如果是管理员,则直接运行
  501. //Application.EnableVisualStyles();
  502. //Application.Run(new Form1());
  503. instance = new Startup(args);
  504. instance.ModifySettingPrefix(args);
  505. instance.SystemReset();
  506. instance.InitSetting();
  507. instance.Start();
  508. // 为了测试用
  509. if (System.IO.File.Exists(Application.StartupPath + @"\1.txt"))
  510. useSenseShield = true;
  511. if (useSenseShield)
  512. {
  513. shieldValue.Clear();
  514. try
  515. {
  516. //1.生成目录下需要有4个运行库,分别在x64和x86下,每个里面两个
  517. //2.在C#项目里引用SlmRuntimeCsharp.dll
  518. //3.插入加密狗,执行代码测试
  519. uint[] moduleIds = DogDecrypting.decryptingModules(4*//*, moduleIds*//*);//第一个参数固定为4
  520. //this.label2.Text = string.Format("加密狗内许可模块号:{0}{1}", Environment.NewLine, string.Join(",", moduleIds));
  521. foreach (uint menuId in moduleIds)
  522. if (!shieldValue.ContainsKey(menuId))
  523. shieldValue.Add(menuId, true);
  524. else
  525. shieldValue[menuId] = true;
  526. }
  527. catch (Exception ex)
  528. {
  529. MessageBox.Show(ex.Message, "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);
  530. return 0;
  531. }
  532. }
  533. instance = new Startup(args);
  534. instance.ModifySettingPrefix(args);
  535. instance.SystemReset();
  536. instance.InitSetting();
  537. instance.Start();
  538. }
  539. else
  540. {
  541. //创建启动对象
  542. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  543. //设置运行文件
  544. startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
  545. //设置启动参数
  546. //startInfo.Arguments = String.Join(" ", Args);
  547. //设置启动动作,确保以管理员身份运行
  548. startInfo.Verb = "runas";
  549. try
  550. {
  551. //如果不是管理员,则启动UAC
  552. System.Diagnostics.Process.Start(startInfo);
  553. //退出
  554. System.Windows.Forms.Application.Exit();
  555. }
  556. catch
  557. {
  558. }
  559. }*/
  560. //这是不要求管理员权限的版本
  561. if (System.IO.File.Exists(Application.StartupPath + @"\1.txt"))
  562. useSenseShield = true;
  563. if (useSenseShield)
  564. {
  565. shieldValue.Clear();
  566. try
  567. {
  568. //1.生成目录下需要有4个运行库,分别在x64和x86下,每个里面两个
  569. //2.在C#项目里引用SlmRuntimeCsharp.dll
  570. //3.插入加密狗,执行代码测试
  571. uint[] moduleIds = DogDecrypting.decryptingModules(4/*, moduleIds*/);//第一个参数固定为4
  572. //this.label2.Text = string.Format("加密狗内许可模块号:{0}{1}", Environment.NewLine, string.Join(",", moduleIds));
  573. foreach (uint menuId in moduleIds)
  574. if (!shieldValue.ContainsKey(menuId))
  575. shieldValue.Add(menuId, true);
  576. else
  577. shieldValue[menuId] = true;
  578. }
  579. catch (Exception ex)
  580. {
  581. MessageBox.Show(ex.Message, "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);
  582. return 0;
  583. }
  584. }
  585. instance = new Startup(args);
  586. instance.ModifySettingPrefix(args);
  587. instance.SystemReset();
  588. instance.InitSetting();
  589. instance.Start();
  590. //if (instance.configModel.StartImgStatus == 2)
  591. //{
  592. // //this.splashForm = new SplashForm();
  593. // SplashForm splashForm = new SplashForm(Startup.instance.configModel.StartImg);
  594. // splashForm.TopMost = true;
  595. // splashForm.Show();
  596. // splashForm.Update();
  597. //}
  598. return 0;
  599. }
  600. /// <summary>
  601. /// 获取menuId对应的菜单是否显示
  602. /// </summary>
  603. /// <param name="menuId"></param>
  604. /// <returns></returns>
  605. public static bool getMenuIdVisible(int menuId)
  606. {
  607. if (shieldValue.ContainsKey((uint)menuId))
  608. return shieldValue[(uint)menuId];
  609. if (!useSenseShield)
  610. return true;
  611. return false;
  612. }
  613. public static bool senseShieldRunLoop()
  614. {
  615. try
  616. {
  617. uint[] moduleIds = DogDecrypting.decryptingModules(4/*, moduleIds*/);//第一个参数固定为4
  618. }
  619. catch (Exception ex)
  620. {
  621. if (!haveShowSenseAlert)
  622. {
  623. haveShowSenseAlert = true;
  624. if (MessageBox.Show(ex.Message, "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
  625. haveShowSenseAlert = false;
  626. }
  627. return false;
  628. }
  629. return true;
  630. }
  631. /// <summary>
  632. /// 获取menuId对应的菜单是否可点击
  633. /// </summary>
  634. /// <param name="menuId"></param>
  635. /// <param name="showAlert">是否显示提示框</param>
  636. /// <returns></returns>
  637. public static bool getMenuIdUsable(List<int> menuIdlists, bool showAlert = false)
  638. {
  639. if (useSenseShield)
  640. {
  641. try
  642. {
  643. uint[] moduleIds = DogDecrypting.decryptingModules(4/*, moduleIds*/);//第一个参数固定为4
  644. //this.label2.Text = string.Format("加密狗内许可模块号:{0}{1}", Environment.NewLine, string.Join(",", moduleIds));
  645. bool existMenuId = false;
  646. foreach (uint theMenuId in moduleIds)
  647. if (menuIdlists.Contains((int)theMenuId))
  648. {
  649. existMenuId = true;
  650. break;
  651. }
  652. if (!existMenuId && showAlert && !haveShowSenseAlert)
  653. {
  654. haveShowSenseAlert = true;
  655. if (MessageBox.Show("该模块颁发许可未发现", "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
  656. haveShowSenseAlert = false;
  657. }
  658. //haveShowSenseAlert = false;
  659. return existMenuId;
  660. }
  661. catch (Exception ex)
  662. {
  663. if (showAlert && !haveShowSenseAlert)
  664. {
  665. haveShowSenseAlert = true;
  666. if (MessageBox.Show(ex.Message, "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
  667. haveShowSenseAlert = false;
  668. }
  669. //return false;
  670. while (senseShieldRunLoop() == false)
  671. {
  672. System.Threading.Thread.Sleep(20);
  673. }
  674. }
  675. }
  676. return true;
  677. }
  678. /// <summary>
  679. /// 初始化配置信息
  680. /// </summary>
  681. private void InitSetting()
  682. {
  683. //App基础配置
  684. //configModel = XmlSerializeHelper.DESerializer<ConfigModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Config.xml", FileMode.Open));
  685. //载物台配置信息
  686. loadingStageModel = XmlSerializeHelper.DESerializer<LoadingStageModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\LoadingStage.xml", FileMode.Open));
  687. //设置->常规设置->用户信息
  688. userInfo = XmlSerializeHelper.DESerializer<UserInfoModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\UserInfo.xml", FileMode.Open));
  689. //设置->网格设置
  690. gridModel = XmlSerializeHelper.DESerializer<GridModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Grid.xml", FileMode.Open));
  691. //工具->自定义界面-快捷键配置
  692. hotkeyModel = XmlSerializeHelper.DESerializer<HotkeyModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Hotkey.xml", FileMode.Open));
  693. //工具->工作流程
  694. workFlowModel = XmlSerializeHelper.DESerializer<WorkFlowModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\WorkFlow.xml", FileMode.Open));
  695. //工具->自定义界面-快捷栏
  696. shortcutbarModel = XmlSerializeHelper.DESerializer<ShortcutbarModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Shortcutbar.xml", FileMode.Open));
  697. //工具->自定义界面-工具栏
  698. toolbarModel = XmlSerializeHelper.DESerializer<ToolbarModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Toolbar.xml", FileMode.Open));
  699. //toolbarLocationModel = XmlSerializeHelper.DESerializer<ToolbarLocationModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\ToolbarLocation.xml", FileMode.Open));
  700. //标注样式配置
  701. labelStyleModel = XmlSerializeHelper.DESerializer<LabelStyleModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Label\\" + configModel.LabelStyle, FileMode.Open));
  702. //测量样式配置
  703. measureStyleModel = XmlSerializeHelper.DESerializer<MeasureStyleModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Measure\\" + configModel.MeasurementStyle, FileMode.Open));
  704. // 设置-常规设置-辅助线样式信息
  705. guideStyleModel = XmlSerializeHelper.DESerializer<GuideStyleModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\SetUp\\" + "GuideStyle.xml", FileMode.Open)); ;
  706. //水印样式配置
  707. watermarkModel = XmlSerializeHelper.DESerializer<WatermarkModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Watermark\\" + configModel.Watermark, FileMode.Open));
  708. // 工字线配置
  709. workTypeStyleModel = XmlSerializeHelper.DESerializer<WorkTypeStyleModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\WorkType\\" + configModel.WorkType, FileMode.Open));
  710. //标尺样式配置
  711. rulerModel = XmlSerializeHelper.DESerializer<RulerModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Ruler.xml", FileMode.Open));
  712. //相机参数配置
  713. cameraParamModel = CameraConfigs.GetInstance().GetCurrentCameraParamModel(); // XmlSerializeHelper.DESerializer<CameraParamModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\CameraParam\\CameraParam.xml", FileMode.Open));
  714. //工艺图比照配置
  715. artworkModel = XmlSerializeHelper.DESerializer<ArtworkModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\ArtworkModel.xml", FileMode.Open));
  716. //电动偏光配置
  717. motorizedLightModel = XmlSerializeHelper.DESerializer<MotorizedLightModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\MotorizedLightModel.xml", FileMode.Open));
  718. //电压配置
  719. voltageModel = XmlSerializeHelper.DESerializer<VoltageModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\VoltageModel.xml", FileMode.Open));
  720. imageMenuModel = XmlSerializeHelper.DESerializer<ImageMenu>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\ParameterSaving\\ParameterSaving.xml", FileMode.Open));
  721. binaryActionModel = XmlSerializeHelper.DESerializer<ImageMenu>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\BinaryActionSaving\\ParameterSaving.xml", FileMode.Open));
  722. //初始化提取,DB中保存的的标尺
  723. mic_rulersAll = mic_rulers_BLL.FindAll();
  724. //初始化标尺信息
  725. InitRulerInfo();
  726. //初始化App内的单位
  727. InitUnitInfo();
  728. //初始化单位对应的枚举数字
  729. CompanyEnumeration();
  730. }
  731. /// <summary>
  732. /// 初始化系统选择的单位的信息
  733. /// 单位对应的枚举数字
  734. /// </summary>
  735. public void CompanyEnumeration()
  736. {
  737. companyEnume = new Dictionary<string, int>();
  738. companyEnume.Add(PdnResources.GetString("MeasurementUnit.Inch"), 1);//英寸
  739. companyEnume.Add(PdnResources.GetString("MeasurementUnit.Mil"), 7);//英寸
  740. companyEnume.Add(PdnResources.GetString("Menu.cm.text"), 2);//厘米
  741. companyEnume.Add(PdnResources.GetString("Menu.Mm.text"), 3);//毫米
  742. companyEnume.Add(PdnResources.GetString("Menu.Micron.text"), 4);//微米
  743. companyEnume.Add(PdnResources.GetString("Menu.nanometer.text"), 5);//纳米
  744. companyEnume.Add(PdnResources.GetString("Menu.Pixel.text"), 6);//像素
  745. }
  746. /// <summary>
  747. /// 初始化系统选择的单位的信息
  748. /// 如果初始安装没有设置过,则默认微米
  749. /// </summary>
  750. public void InitUnitInfo()
  751. {
  752. if (configModel != null && configModel.Unit > 0)
  753. {
  754. measurementUnit = (MeasurementUnit)Enum.Parse(typeof(MeasurementUnit), configModel.Unit.ToString());
  755. }
  756. else
  757. {
  758. measurementUnit = MeasurementUnit.Micron;
  759. }
  760. }
  761. /// <summary>
  762. /// 初始化标尺信息,从数据库中读取
  763. /// 然后根据标尺的单位,换算出所有单位的数据
  764. /// </summary>
  765. public void InitRulerInfo()
  766. {
  767. rules.Clear();
  768. if (configModel != null /*&& configModel.RulerId>0*/)
  769. {
  770. for (int i = 0; i < Startup.instance.mic_rulersAll.Count; i++)
  771. {
  772. if (Startup.instance.mic_rulersAll[i].id == configModel.RulerId)
  773. {
  774. ruleDB = Startup.instance.mic_rulersAll[i];
  775. break;
  776. }
  777. }
  778. //ruleDB = mic_rulers_BLL.FindDefault(configModel.RulerId);
  779. if (ruleDB != null)
  780. {
  781. //计算单位长度 比如0.05英寸/像素; 100纳米/像素;
  782. double unitLength = (double)(ruleDB.physical_length / (decimal)ruleDB.pixel_length);
  783. switch (ruleDB.ruler_units)
  784. {
  785. case (int)MeasurementUnit.Inch://英寸
  786. rules.Add(MeasurementUnit.Inch, unitLength); //英寸
  787. rules.Add(MeasurementUnit.Mil, 1000 * unitLength); //米尔
  788. rules.Add(MeasurementUnit.Centimeter, 2.54 * unitLength); //厘米
  789. rules.Add(MeasurementUnit.Millimeter, 25.4 * unitLength); //毫米
  790. rules.Add(MeasurementUnit.Micron, 25400 * unitLength); //微米
  791. rules.Add(MeasurementUnit.Nano, 25400000 * unitLength); //纳米
  792. break;
  793. case (int)MeasurementUnit.Mil://米尔
  794. rules.Add(MeasurementUnit.Mil, unitLength); //米尔
  795. rules.Add(MeasurementUnit.Inch, 0.001 * unitLength); //英寸
  796. rules.Add(MeasurementUnit.Centimeter, 0.00254 * unitLength); //厘米
  797. rules.Add(MeasurementUnit.Millimeter, 0.0254 * unitLength); //毫米
  798. rules.Add(MeasurementUnit.Micron, 25.4 * unitLength); //微米
  799. rules.Add(MeasurementUnit.Nano, 25400 * unitLength); //纳米
  800. break;
  801. case (int)MeasurementUnit.Centimeter://厘米
  802. rules.Add(MeasurementUnit.Inch, 0.3937008 * unitLength); //英寸
  803. rules.Add(MeasurementUnit.Mil, 393.7008 * unitLength); //米尔
  804. rules.Add(MeasurementUnit.Centimeter, unitLength); //厘米
  805. rules.Add(MeasurementUnit.Millimeter, 10 * unitLength); //毫米
  806. rules.Add(MeasurementUnit.Micron, 10000 * unitLength); //微米
  807. rules.Add(MeasurementUnit.Nano, 10000000 * unitLength); //纳米
  808. break;
  809. case (int)MeasurementUnit.Millimeter://毫米
  810. rules.Add(MeasurementUnit.Inch, 0.0393701 * unitLength); //英寸
  811. rules.Add(MeasurementUnit.Mil, 39.3701 * unitLength); //米尔
  812. rules.Add(MeasurementUnit.Centimeter, 0.1 * unitLength); //厘米
  813. rules.Add(MeasurementUnit.Millimeter, unitLength); //毫米
  814. rules.Add(MeasurementUnit.Micron, 1000 * unitLength); //微米
  815. rules.Add(MeasurementUnit.Nano, 1000000 * unitLength); //纳米
  816. break;
  817. case (int)MeasurementUnit.Micron://微米
  818. rules.Add(MeasurementUnit.Inch, 0.00003937007874 * unitLength); //英寸
  819. rules.Add(MeasurementUnit.Mil, 0.03937007874 * unitLength); //米尔
  820. rules.Add(MeasurementUnit.Centimeter, 0.0001 * unitLength); //厘米
  821. rules.Add(MeasurementUnit.Millimeter, 0.001 * unitLength); //毫米
  822. rules.Add(MeasurementUnit.Micron, unitLength); //微米
  823. rules.Add(MeasurementUnit.Nano, 1000 * unitLength); //纳米
  824. break;
  825. case (int)MeasurementUnit.Nano://纳米
  826. rules.Add(MeasurementUnit.Inch, 3.9370e-8 * unitLength); //英寸
  827. rules.Add(MeasurementUnit.Mil, 3.9370e-5 * unitLength); //米尔
  828. rules.Add(MeasurementUnit.Centimeter, 1e-7 * unitLength); //厘米
  829. rules.Add(MeasurementUnit.Millimeter, 1e-6 * unitLength); //毫米
  830. rules.Add(MeasurementUnit.Micron, 0.001 * unitLength); //微米
  831. rules.Add(MeasurementUnit.Nano, unitLength); //纳米
  832. break;
  833. default:
  834. ruleDB = null;
  835. break;
  836. }
  837. }
  838. }
  839. if (ruleDB == null)
  840. {
  841. //如果没有标尺的时候,默认1微米/像素
  842. rules.Add(MeasurementUnit.Inch, 0.0000394); //英寸
  843. rules.Add(MeasurementUnit.Mil, 0.0394); //米尔
  844. rules.Add(MeasurementUnit.Centimeter, 0.0001); //厘米
  845. rules.Add(MeasurementUnit.Millimeter, 0.001); //毫米
  846. rules.Add(MeasurementUnit.Micron, 1); //微米
  847. rules.Add(MeasurementUnit.Nano, 1000); //纳米
  848. }
  849. //设置文档标尺
  850. Document.defaultDpi = 1 / rules[MeasurementUnit.Inch];
  851. }
  852. /// <summary>
  853. /// 判断是否根据快捷方式参数修改界面
  854. /// </summary>
  855. /// <param name="argsArr"></param>
  856. /// <returns></returns>
  857. private bool ModifySettingPrefix(string[] argsArr)
  858. {
  859. if (argsArr.Count() == 2 && (0 == string.Compare(argsArr[0], "/personalConfig", true)))
  860. {
  861. string configName = argsArr[1].Replace("/", "");
  862. if (Directory.Exists(Application.StartupPath + "\\Config\\" + configName + "\\"))
  863. {
  864. this.SettingPrefix = configName;
  865. //MessageBox.Show("配置文件为:" + this.SettingPrefix);
  866. return true;
  867. }
  868. else
  869. {
  870. return false;
  871. }
  872. }
  873. else
  874. {
  875. return false;
  876. }
  877. }
  878. //启动动画
  879. public void StartAnimation()
  880. {
  881. if (Startup.instance.configModel.StartImgStatus == 2)
  882. {
  883. //this.splashForm = new SplashForm();
  884. this.splashForm = new SplashForm(Startup.instance.configModel.StartImg);
  885. this.splashForm.TopMost = true;
  886. this.splashForm.StartPosition = FormStartPosition.CenterParent;
  887. this.splashForm.Show();
  888. this.splashForm.Update();
  889. }
  890. }
  891. /// <summary>
  892. /// 系统复位
  893. /// </summary>
  894. /// <returns></returns>
  895. private void SystemReset()
  896. {
  897. //先单独获取App基础配置
  898. configModel = XmlSerializeHelper.DESerializer<ConfigModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Config.xml", FileMode.Open));
  899. MarkPointRect.markPointRectWidth = configModel.MarkpointWidth;
  900. PdnResources.Initialize(configModel);
  901. //需要复位
  902. if (configModel.LabelInformationReset == 1 || configModel.MeasurementInformationReset == 1 || configModel.LogReset == 1 ||
  903. configModel.ShortcutBarReset == 1 || configModel.ToolbarReset == 1 || configModel.HotKeyReset == 1)
  904. {
  905. try
  906. {
  907. //标注初始化
  908. if (configModel.LabelInformationReset == 1)
  909. {
  910. string originalFilePath = Application.StartupPath + "\\Config\\Original\\Label\\Default.xml";
  911. string destFilePath = Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Label\\Default.xml";
  912. System.IO.File.Copy(originalFilePath, destFilePath, true);
  913. configModel.LabelInformationReset = 0;
  914. }
  915. //测量初始化
  916. if (configModel.MeasurementInformationReset == 1)
  917. {
  918. string originalFilePath = Application.StartupPath + "\\Config\\Original\\Measure\\Default.xml";
  919. string destFilePath = Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Measure\\Default.xml";
  920. System.IO.File.Copy(originalFilePath, destFilePath, true);
  921. configModel.MeasurementInformationReset = 0;
  922. }
  923. //log初始化
  924. if (configModel.LogReset == 1)
  925. {
  926. //未处理
  927. configModel.LogReset = 0;
  928. }
  929. //快捷栏初始化
  930. if (configModel.ShortcutBarReset == 1)
  931. {
  932. string originalFilePath = Application.StartupPath + "\\Config\\Original\\Shortcutbar.xml";
  933. string destFilePath = Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Shortcutbar.xml";
  934. System.IO.File.Copy(originalFilePath, destFilePath, true);
  935. configModel.ShortcutBarReset = 0;
  936. }
  937. //工具栏初始化
  938. if (configModel.ToolbarReset == 1)
  939. {
  940. string originalFilePath = Application.StartupPath + "\\Config\\Original\\Toolbar.xml";
  941. string destFilePath = Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Toolbar.xml";
  942. System.IO.File.Copy(originalFilePath, destFilePath, true);
  943. configModel.ToolbarReset = 0;
  944. }
  945. //快捷键初始化
  946. if (configModel.HotKeyReset == 1)
  947. {
  948. string originalFilePath = Application.StartupPath + "\\Config\\Original\\Hotkey.xml";
  949. string destFilePath = Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Hotkey.xml";
  950. System.IO.File.Copy(originalFilePath, destFilePath, true);
  951. configModel.HotKeyReset = 0;
  952. }
  953. string configModelXml = XmlSerializeHelper.XmlSerialize<ConfigModel>(configModel);
  954. FileOperationHelper.WriteStringToFile(configModelXml, Application.StartupPath + "\\Config\\" + SettingPrefix + "\\Config.xml", FileMode.Create);
  955. //MessageBox.Show(PdnResources.GetString("Menu.emresetsuccessfulthesoftw.text"));
  956. }
  957. catch (Exception)
  958. {
  959. MessageBox.Show(PdnResources.GetString("Menu.Systemresetfailure.text"));
  960. }
  961. }
  962. }
  963. public void CheckCameraSN()
  964. {
  965. TUCameraManager m_cameraManager = TUCameraManager.GetInstance();
  966. cameraSNLegal = false;
  967. if (m_cameraManager.GetCurrentCamera() != null)
  968. {
  969. string cameraSn;
  970. string cameraName;
  971. if (m_cameraManager.GetCurrentCamera().ReadRegisterData(out cameraSn))
  972. {
  973. if (FileOperationHelper.CheckCameraSNAndReturnName(Application.StartupPath + "\\array.dll", cameraSn, out cameraName))
  974. {
  975. cameraSNLegal = true;
  976. }
  977. }
  978. m_cameraManager.GetCurrentCamera().StartWaitForFrame();
  979. }
  980. }
  981. /// <summary>
  982. /// 初始化平台运动控制器
  983. /// </summary>
  984. public void InitStageController()
  985. {
  986. var m_Stage = AxisController.GetInstance();
  987. if (SerialPort.GetPortNames().Length > 0 || AxisController.ControllerType.Equals("HDS"))
  988. {
  989. m_Stage.LoadingStageModel = instance.loadingStageModel;
  990. m_Stage.Open();
  991. }
  992. }
  993. }
  994. }