Startup.cs 46 KB

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