Skip to content

Kịch bản công cụ dòng lệnh

Kịch bản có thể được sử dụng để tạo các công cụ dòng lệnh tận dụng chức năng của QCAD. Ví dụ, một kịch bản như vậy có thể tạo bản vẽ dựa trên các tham số, chuyển đổi bản vẽ hoặc phân tích bản vẽ.

Cấu trúc cơ bản của một kịch bản công cụ dòng lệnh có thể trông giống như ví dụ dưới đây. Đây cũng là một kịch bản ví dụ hoạt động đầy đủ, có thể được sử dụng như một công cụ dòng lệnh để thêm một đường thẳng từ 0,0 đến 100,100 vào một bản vẽ nhất định và xuất bản vẽ đó dưới một tên tệp mới:

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;
}
// tạo tài liệu mới (ngoài màn hình):
var doc = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
var di = new RDocumentInterface(doc);
// nhập tệp đã cho:
if (di.importFile(inFile) != RDocumentInterface.IoErrorNoError) {
di.destroy();
print("Cannot import file:", inFile);
return;
}
// thêm đường thẳng vào bản vẽ (sử dụng QCAD Simple API):
startTransaction(di);
addLine(0, 0, 100, 100);
endTransaction();
// xuất ra tệp đã cho (-o)
if (!di.exportFile(outFile)) {
di.destroy();
print("Cannot export file:", outFile);
return;
}
di.destroy();
}
if (typeof(including) == 'undefined' || including === false) {
main();
}

Chạy kịch bản công cụ dòng lệnh

Section titled “Chạy kịch bản công cụ dòng lệnh”

Giả sử kịch bản này được lưu tại scripts/Tools/ExTool/ExTool.js.

Sau đó, có thể chạy nó như sau:

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

Nếu kịch bản được chạy như một phần của dịch vụ web, hãy đảm bảo cũng truyền các tùy chọn -no-gui và -allow-multiple-instances để báo cho QCAD biết rằng không có GUI khả dụng và có thể chạy song song nhiều phiên bản:

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

Trên máy chủ, thông thường bạn cũng sẽ truyền -platform offscreen để chạy không giao diện (tức là không cần máy chủ X11 trên Linux):

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