Skip to content

สคริปต์เครื่องมือบรรทัดคำสั่ง

สคริปต์สามารถใช้สร้างเครื่องมือบรรทัดคำสั่งที่ใช้ประโยชน์จากความสามารถของ 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;
}
// create new document (off screen):
var doc = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
var di = new RDocumentInterface(doc);
// import given file:
if (di.importFile(inFile) != RDocumentInterface.IoErrorNoError) {
di.destroy();
print("Cannot import file:", inFile);
return;
}
// add line to drawing (using QCAD Simple API):
startTransaction(di);
addLine(0, 0, 100, 100);
endTransaction();
// export to given file (-o)
if (!di.exportFile(outFile)) {
di.destroy();
print("Cannot export file:", outFile);
return;
}
di.destroy();
}
if (typeof(including) == 'undefined' || including === false) {
main();
}

การรันสคริปต์เครื่องมือบรรทัดคำสั่ง

Section titled “การรันสคริปต์เครื่องมือบรรทัดคำสั่ง”

สมมติว่าสคริปต์นี้ถูกบันทึกไว้ที่ scripts/Tools/ExTool/ExTool.js

จากนั้นสามารถรันได้ดังนี้:

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

หากรันสคริปต์เป็นส่วนหนึ่งของบริการเว็บ อย่าลืมส่งสวิตช์ -no-gui และ -allow-multiple-instances ด้วย เพื่อบอก QCAD ว่าไม่มี GUI พร้อมใช้งานและสามารถรันหลายอินสแตนซ์แบบขนานได้:

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

บนเซิร์ฟเวอร์ โดยปกติคุณจะส่ง -platform offscreen ด้วยเพื่อรันแบบไม่มีจอ (headless) (เช่น ไม่มีเซิร์ฟเวอร์ X11 บน Linux):

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