PCB Genesis或Incam 右键导入TGZ 实现方法

      使用Genesis导入TGZ方式很多 的,比如有:写个脚本框选TGZ的的方式实现TGZ导入,将TGZ拖入脚本界面实现TGZ导入, 给Engineering Toolkit窗口句柄注册拖拽事件实现TGZ导入, 右键实现TGZ导入等,本篇介绍最后一种右键导入TGZ的方法.

  一.实现效果图

      1.tgz文件右键导入

       

     2.tgz文件夹右键导入

     

 

  二.借助Gateway实现InputTGZ脚本----C#实现代码

        1.C#实现代码部份

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace InputTGZ
{
    static class Program
    {
        static Process process = new Process();
        private static bool isIncam = false;
        static string db_name = "genesis";  //db名为默认值,满足个性化可以改为配置读取

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                try
                {
                    string file_path = args[0];
                    string OpenStepName = System.Configuration.ConfigurationManager.AppSettings["OpenStepName"] ?? "";
                    string StateInfo = getPid();
                    if (!string.IsNullOrEmpty(StateInfo))
                    {
                        MessageBox.Show($"{StateInfo}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    List<string> job_list = getJOBlist();
                    var isDirExists = Directory.Exists(file_path);

                    //导入目录下所有TGZ
                    if (isDirExists)
                    {
                        var tgzFileList = Directory.GetFiles(file_path, "*.tgz");
                        foreach (var item in tgzFileList)
                        {
                            string job_name1 = Path.GetFileNameWithoutExtension(item).ToLower().Trim();
                            if (job_list.Any(tt => tt == job_name1))
                            {
                                COM($"close_job,job={job_name1}");
                                COM($"close_form,job={job_name1}");
                                COM($"close_flow,job={job_name1}");
                                COM($"delete_entity,job=,type=job,name={job_name1}");
                            }
                            COM($"import_job,db= {db_name},path={item},name ={job_name1}");
                        }
                        return;
                    }

                    //单个TGZ导入
                    string job_name = Path.GetFileNameWithoutExtension(file_path).ToLower().Trim();
                    if (job_list.Any(tt => tt == job_name))
                    {
                        var isYes = MessageBox.Show($"TGZ名【{job_name}】已存在,请确认是否覆盖?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (isYes == DialogResult.No)
                            return;
                        COM($"close_job,job={job_name}");
                        COM($"close_form,job={job_name}");
                        COM($"close_flow,job={job_name}");
                        COM($"delete_entity,job=,type=job,name={job_name}");
                    }
                    COM($"import_job,db= {db_name},path={file_path},name ={job_name}");
                    if (!string.IsNullOrEmpty(OpenStepName))
                    {
                        COM($"open_job,job={job_name}");
                        COM($"open_entity,job={job_name},type=step,name={OpenStepName}");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"导入TGZ出错了{ex.Message}", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                }
                finally
                {
                    if (process.StartInfo.FileName.Length > 0)
                    {
                        if (!process.HasExited)
                            process.Kill();
                    }
                }
            }
        }
        /// <summary>
        /// 执行COM指令
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public static string COM(string command)
        {
            process.StandardInput.WriteLine("COM " + command);
            var STATUS = process.StandardOutput.ReadLine();
            process.StandardInput.WriteLine("COMANS");
            return process.StandardOutput.ReadLine();
        }
        /// <summary>
        /// 设置genesis或incam PID
        /// </summary>
        /// <returns></returns>
        public static string getPid()
        {
            int gpid = getPIDonly();
            if (gpid < 1) return "未打开Genesis或incam";
            var gatewayFilePath = "";
            if (!isIncam)
            {
                gatewayFilePath = System.Environment.GetEnvironmentVariable("GENESIS_EDIR").Replace("/", "\\") + @"\misc\gateway.exe";
            }
            else
            {
                gatewayFilePath = @"D:\InCAM\release\bin\gateway.exe";
                if (!File.Exists(gatewayFilePath)) gatewayFilePath = @"C:\InCAM\release\bin\gateway.exe";
            }
            process = new Process();
            process.StartInfo.FileName = gatewayFilePath;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.Arguments = "%" + gpid.ToString();
            process.Start();
            return "";
        }
        /// <summary>
        /// 获取的有JOB列表 
        /// </summary>
        /// <returns></returns>
        public static List<string> getJOBlist()
        {
            List<string> job_list = new List<string>();
            if (!isIncam)
            {
                string path = (System.Environment.GetEnvironmentVariable("GENESIS_DIR").Replace("/", "\\")) + "/share/joblist";
                using (StreamReader sr = new StreamReader(path, Encoding.Default))
                {
                    while (!sr.EndOfStream)
                    {
                        string strline = sr.ReadLine();
                        int start = strline.IndexOf("NAME=");
                        if (start != -1)
                        {
                            job_list.Add(strline.Substring(start + 5));
                        }
                    }
                }
                db_name = "genesis";  //默认   
            }
            else
            {
                string filePath = @"D:\InCAM\server\config\joblist.xml";
                if (!File.Exists(filePath)) filePath = @"C:\InCAM\server\config\joblist.xml";
                string xml = File.ReadAllText(filePath);
                xml = "<RootXml>" + xml + "</RootXml>";
                using (StringReader sr = new StringReader(xml))
                {
                    XmlSerializer xmldes = new XmlSerializer(typeof(RootXml));
                    job_list = (xmldes.Deserialize(sr) as RootXml).JobList.Select(tt => tt.Name).ToList(); ;
                }
                db_name = "db1";//默认
            }
            return job_list;
        }
        /// <summary>
        /// 获取唯一PID  仅用单开 
        /// </summary>
        /// <returns></returns>
        public static int getPIDonly()
        {
            Process[] arrayProcess = Process.GetProcesses();
            foreach (Process p in arrayProcess)
            {
                if (p.ProcessName.ToLower() == "get" || p.ProcessName.ToLower() == "gfx")
                {
                    return p.Id;
                }
                if (p.ProcessName.ToLower() == "incam")
                {
                    isIncam = true;
                    return p.Id;
                }
            }
            return 0;
        }
    }

    [XmlRoot("RootXml")]
    public class RootXml
    {
        [XmlArray("JobList")]
        [XmlArrayItem("job")]
        public List<job> JobList { get; set; } = new List<job>();
    }
    [XmlRootAttribute("job")]
    public class job
    {
        [XmlAttribute("name")]
        public string Name { get; set; }
        [XmlAttribute("dbName")]
        public string dbName { get; set; }
    }
}

   2.app.config 设置打开StepName名

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
  
    <appSettings>
      <!--打开StepName-->
      <add key="OpenStepName" value="cam"/>
    </appSettings>
</configuration>

 

 三.注册右键代码

      1.tgz后缀文件名加入右键【导入tgz】

        /// <summary>
        /// tgz后缀文件名加入右键[导入tgz]
        /// </summary>
        static void tgz_SuffixFile()
        {
            string FileSuffix = ".tgz";
            string FileSuffixName = "tgz";
            string menuName = "导入tgz";
            //加入TGZ后缀
            RegistryKey shell = Registry.ClassesRoot;
            RegistryKey custom = shell.CreateSubKey(FileSuffix);
            custom.SetValue("", FileSuffixName);
            custom.Close();
            shell.Close();
           //TGZ绑定程序            
            var FileSuffix2Root = Registry.ClassesRoot.CreateSubKey(FileSuffixName);
            string OpenFilePath = Application.StartupPath  + @"\InputTGZ.exe";
            string OpenFilePathIco = Application.StartupPath  + @"\InputTGZ.ico";
            RegistryKey shell2 = FileSuffix2Root.CreateSubKey("shell");
            RegistryKey custom2 = shell2.CreateSubKey(menuName);
            custom2.SetValue("", menuName  );
            custom2.SetValue("Icon", OpenFilePathIco);
            RegistryKey cmd = custom2.CreateSubKey("command");
            cmd.SetValue("", OpenFilePath + " %1");
            FileSuffix2Root.Close();
            cmd.Close();
            custom.Close();
            shell2.Close();
        }

      2.文件夹加入右键【导入tgz文件夹】

        /// <summary>
        ///  文件夹加入右键[导入tgz文件夹]
        /// </summary>
        static void tgz_Dir()
        {
            string menuName = "导入tgz文件夹";
            string OpenFilePath = Application.StartupPath + @"\InputTGZ.exe";
            string OpenFilePathIco = Application.StartupPath + @"\InputTGZ.ico";
            RegistryKey shell = Registry.ClassesRoot.OpenSubKey(@"Directory\shell", true);
            RegistryKey custom = shell.CreateSubKey(menuName);
            custom.SetValue("", menuName  );
            custom.SetValue("Icon", OpenFilePathIco);
            RegistryKey cmd = custom.CreateSubKey("command");
            cmd.SetValue("", OpenFilePath + " %1");
            cmd.Close();
            custom.Close();
            shell.Close();
        }

 

posted @ 2019-01-23 21:16  pcbren  阅读(4256)  评论(2编辑  收藏  举报