콘텐츠로 이동

명령줄 도구 스크립트

스크립트를 사용하여 QCAD 기능을 활용하는 명령줄 도구를 만들 수 있습니다. 예를 들어 이러한 스크립트는 매개변수를 기반으로 도면을 생성하거나, 도면을 변환하거나, 도면을 분석할 수 있습니다.

명령줄 도구 스크립트의 기본 구조는 예를 들어 아래와 같습니다. 이것은 또한 주어진 도면에 0,0에서 100,100까지 선을 추가하고 해당 도면을 새 파일 이름으로 내보내는 명령줄 도구로 사용할 수 있는 완전히 작동하는 예제 스크립트입니다.

include("scripts/simple.js");
include("scripts/Tools/arguments.js");
function printHelp() {
print("Usage: " + args[1] + " [OPTION]... <drawing file>");
print();
print("This tool does...");
print();
print(" -f, -force Overwrite existing output file");
print(" -h, -help Display this help");
print(" -o, -outfile=FILE Set output file to FILE");
print(" -p, -parameter Some parameter...");
print(" ...");
printGenericUsage();
print();
}
function main() {
if (testArgument(args, "-h", "-help")) {
printHelp();
return;
}
if (args.length < 3) {
print("No input file given. Try -h for help.");
return;
}
var inFile = args[args.length - 1];
if (inFile.indexOf("-") === 0) {
print("No input file. Try -h for help.");
return;
}
var fi = new QFileInfo(inFile);
if (!fi.isAbsolute()) {
inFile = RSettings.getLaunchPath() + QDir.separator + inFile;
fi = new QFileInfo(inFile);
}
var outFile = getArgument(args, "-o", "-outfile");
if (outFile !== undefined) {
outFile = getAbsolutePathForArg(outFile);
}
else {
print("No output file. Try -h for help.");
}
if (new QFileInfo(outFile).exists() && !testArgument(args, "-f", "-force")) {
print("Output file exists already, not overwriting: ", outFile);
print("Use -f to force overwrite");
return;
}
// 새 문서 만들기(화면 밖):
var doc = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
var di = new RDocumentInterface(doc);
// 주어진 파일 가져오기:
if (di.importFile(inFile) != RDocumentInterface.IoErrorNoError) {
di.destroy();
print("Cannot import file:", inFile);
return;
}
// 도면에 선 추가하기(QCAD Simple API 사용):
startTransaction(di);
addLine(0, 0, 100, 100);
endTransaction();
// 주어진 파일로 내보내기(-o)
if (!di.exportFile(outFile)) {
di.destroy();
print("Cannot export file:", outFile);
return;
}
di.destroy();
}
if (typeof(including) == 'undefined' || including === false) {
main();
}

명령줄 도구 스크립트 실행하기

섹션 제목: “명령줄 도구 스크립트 실행하기”

이 스크립트가 scripts/Tools/ExTool/ExTool.js에 저장되어 있다고 가정해 보겠습니다.

그러면 다음과 같이 실행할 수 있습니다.

qcad -autostart scripts/Tools/ExTool/ExTool.js -f -o out.dxf in.dxf

스크립트를 웹 서비스의 일부로 실행하는 경우, GUI를 사용할 수 없으며 여러 인스턴스를 병렬로 실행할 수 있음을 QCAD에 알리기 위해 -no-gui 및 -allow-multiple-instances 스위치도 전달해야 합니다.

qcad -no-gui -allow-multiple-instances -autostart scripts/Tools/ExTool/ExTool.js -f -o out.dxf in.dxf

서버에서는 일반적으로 헤드리스로 실행하기 위해(즉, Linux에서 X11 서버 없이) -platform offscreen도 전달합니다.

qcad -platform offscreen -no-gui -allow-multiple-instances -autostart scripts/Tools/ExTool/ExTool.js -f -o out.dxf in.dxf