-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert.java
66 lines (54 loc) · 2.58 KB
/
Convert.java
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
import java.io.File;
import java.io.IOException;
import java.util.UUID;
public class Convert {
private static final String kicadImgId = "ghcr.io/liangtie/kicad:lite";
private static final String kicadImgHomePath = "/home/kicad";
private static final String[] supportedExtensions = { "PcbDoc", "SchDoc" };
private static final String[] conversionFormats = { "kicad_pcb", "kicad_sch" };
private static final String[] TP_MAP = { "pcb", "sch" };
public static void main(String[] args) {
convert("D:/code/kicad/build/out/ad/PWRMOD-001-RevA.SchDoc");
convert("D:/code/kicad/build/out/ad/PWRMOD-001-RevA.PcbDoc");
}
public static void convert(String oriFilePath) {
File kicadProjectDir = new File(oriFilePath).getParentFile();
String oriFileName = new File(oriFilePath).getName();
String oriExtension = oriFileName.substring(oriFileName.lastIndexOf('.') + 1);
// Check if file extension is supported
int index = -1;
for (int i = 0; i < supportedExtensions.length; i++) {
if (supportedExtensions[i].equals(oriExtension)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("Unsupported file extension: " + oriExtension);
return;
}
String convertedFileName = oriFileName.replace(oriExtension, conversionFormats[index]);
String kicadType = TP_MAP[index];
String mountedPrjPath = kicadImgHomePath + "/" + UUID.randomUUID().toString().replace("-", "/");
String mountedFilePath = mountedPrjPath + "/" + oriFileName.replace("\\", "/");
String dockerOutputFilePath = mountedPrjPath + "/" + convertedFileName.replace("\\", "/");
String[] firstCmd = { "docker", "run", "--rm",
"-v", kicadProjectDir.getAbsolutePath().replace("\\", "/") + ":" + mountedPrjPath,
kicadImgId, "kicad-cli", kicadType,
"convert", "--output", dockerOutputFilePath,
mountedFilePath };
StringBuilder commandStringBuilder = new StringBuilder();
for (String part : firstCmd) {
commandStringBuilder.append(part).append(" ");
}
String commandString = commandStringBuilder.toString().trim();
System.out.println(commandString);
ProcessBuilder processBuilder = new ProcessBuilder(firstCmd);
try {
Process process = processBuilder.start();
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}