轮胎外观检测添加思谋语义分割模型检测工具
C3204
2026-03-30 06c627ec032b3f3876fd7db8a3ff0ff1a6614fa2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;
namespace LB_VisionControls
{
    class ComplieBuilder
    {
 
        public string SrcString;
        public string Command;
        public ComplieType cpType=ComplieType.exe;
        public bool isWindow = false;
        public int RowErrorNumber { get; private set; }
        public bool isHasErrorRows { get; private set; }
        public bool isError { get; private set; }
        public string Creat(string srcString, string Command, ComplieType cpType=ComplieType.exe, bool isStartByWindow=false)
        {
            this.SrcString = srcString;
            this.Command = Command;
            this.cpType = cpType;
            this.isWindow = isStartByWindow;
            this.isHasErrorRows = false;
            this.RowErrorNumber = -1;
            isError = false;
            CreatFile();//
            StringBuilder sbr = new StringBuilder();
            sbr.AppendLine(runStringCSC(MakeCommond()));//编译
            if (File.Exists(CommConfig.ExeFilePath) || File.Exists(CommConfig.DllFilePath))
            {
                sbr = new StringBuilder();
                sbr.AppendLine("-------编译成功--------------------------------");
                if (cpType == ComplieType.exe || cpType == ComplieType.winexe)//测试*.exe文件
                {
                    if (isWindow || cpType == ComplieType.winexe)//判断是否使用CMD
                    {
                        System.Diagnostics.Process.Start(CommConfig.ExeFilePath);
                    }
                    else
                    {
                        sbr.AppendLine("\n" + runStringTemp(this.Command));
                    }
                }
            }
            else
            {
                isError = true;
                sbr.AppendLine("-------编译失败--------------------------------");
 
                try
                {
                    Regex reg = new Regex(@"\((\d+)\,\d+\)", RegexOptions.Multiline);
                    Match m = reg.Match(sbr.ToString());
                    
                    if (m.Success)
                    {
                        isHasErrorRows = true;
                        RowErrorNumber = int.Parse(m.Groups[1].Value);
                    }
                }
                catch (Exception e)
                {
                    isHasErrorRows = false;
                    RowErrorNumber = -1;
                }
            }
 
            return sbr.ToString();
        }
 
        //构建命令
        private string MakeCommond()
        {
            StringBuilder sb = new StringBuilder();
            switch (cpType)
            {
                case ComplieType.exe:
                    sb.Append("/target:exe /out:"+ CommConfig.ExeFilePath + " " + CommConfig.SrcFilePath);//普通exe
                    break;
                case ComplieType.winexe:
                    sb.Append("/target:winexe /out:"+ CommConfig.ExeFilePath + " " + CommConfig.SrcFilePath);//Winexe
                    break;
                case ComplieType.dll:
                    sb.Append("/target:library /out:"+ CommConfig.DllFilePath + " " + CommConfig.SrcFilePath);//dll
                    break;
                default:
                    throw new Exception("无效的输入!");
            }
            return sb.ToString();
        }
        //测试生成文件
        private string runStringTemp(string commond)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = CommConfig.ExeFilePath;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            string[] sp = commond.Split('∫');
            foreach (string i in sp)
            {
                if (string.IsNullOrEmpty(i))
                    continue;
                p.StandardInput.WriteLine(i);
            }
            // p.StandardInput.WriteLine(commond);
            return p.StandardOutput.ReadToEnd();
 
        }
        //编译函数
        private string runStringCSC(string commond)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = CommConfig.CscPath + "\\csc.exe";
            p.StartInfo.Arguments = commond;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine(commond);
            return p.StandardOutput.ReadToEnd();
 
        }
        //生成文件
        private void CreatFile()
        {
            if (Directory.Exists(CommConfig.BinPath))
                Directory.Delete(CommConfig.BinPath, true);
            Directory.CreateDirectory(CommConfig.BinPath);
            Thread.Sleep(500);
            StreamWriter strCode = new StreamWriter(File.OpenWrite(CommConfig.SrcFilePath), System.Text.Encoding.Default);
            strCode.WriteLine(SrcString);
            strCode.Close();
        }
    }
}