Running a script on all open drawings

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
cjh
Active Member
Posts: 31
Joined: Tue Jul 12, 2016 4:51 pm

Running a script on all open drawings

Post by cjh » Sat Aug 06, 2016 12:43 am

How do I call a script and run it to completion for all of the drawings open at one time?
Here's what I have, but it only runs the close script for the rightmost window.
I know "CloseAll.js" does this, but I would like to be able to run any script on all open drawings.
TestAction.prototype.beginEvent = function() {
    BCS.prototype.beginEvent.call(this);
    var appWin = EAction.getMainWindow();
    var mdiArea = EAction.getMdiArea();
	var windows = mdiArea.subWindowList();
	for (var i=0; i<windows.length; i++) {
		var window = windows;
		var file = window.getDocument().getFileName();
		window.showMaximized();
		mdiArea.setActiveSubWindow(window);
		var action = RGuiAction.getByScriptFile("C:/Program Files/QCAD/scripts/File/CloseFile/CloseFile.js");
		if (!isNull(action)) {
			action.slotTrigger();
          // only runs on the rightmost drawing subwindow
		}
}

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

Re: Running a script on all open drawings

Post by andrew » Mon Aug 08, 2016 2:24 pm

Please make sure that your action runs in the global script context, not in the script context of a single document. When setting up your action (i.e. in TestAction.init or TestActionInit.js):
action.setRequiresDocument(false);

cjh
Active Member
Posts: 31
Joined: Tue Jul 12, 2016 4:51 pm

Re: Running a script on all open drawings

Post by cjh » Mon Aug 08, 2016 2:41 pm

Right - I had action.setRequiresDocument(false), but the script specified in RGuiAction.getByScriptFile() would still only run on the final window.
Adding QCoreApplication.sendPostedEvents(appWin) inside the for loop forces the execution of the action on the proper sub window.
I've reverted back to action.setRequiresDocument(true) because the script shouldn't be run without at least one document open.

Changing scriptFile allows you to run a different script on each open drawing.
include("../BCS.js");

// Constructor calls base class constructor:
function TestAction(guiAction) {
    BCS.call(this, guiAction);
}

TestAction.prototype = new BCS();


TestAction.prototype.beginEvent = function() {
    BCS.prototype.beginEvent.call(this);

	var appWin = EAction.getMainWindow();
	var mdiArea = EAction.getMdiArea();
	var windows = mdiArea.subWindowList();
	for (var i=0; i<windows.length; i++) {
		var window = windows;
		var file = window.getDocument().getFileName();
		window.showMaximized();
		mdiArea.setActiveSubWindow(window);
		//~ var scriptFile = "C:/Program Files/QCAD/scripts/BCS/DrawOneLine/DrawOneLine.js";
		var scriptFile = "C:/Program Files/QCAD/scripts/File/CloseFile/CloseFile.js"
		var action = RGuiAction.getByScriptFile(scriptFile);
		if (!isNull(action)) {
			action.slotTrigger();
			QCoreApplication.sendPostedEvents(appWin);
			EAction.handleUserMessage("closing: " + file);
		}
	}
}

TestAction.init = function(basePath) {
    var action = new RGuiAction("&TestAction", RMainWindowQt.getMainWindow());
    action.setRequiresDocument(true);
    action.setScriptFile(basePath + "/TestAction.js");
    action.setDefaultCommands(["TestAction"]);
    action.setGroupSortOrder(80100);
    action.setSortOrder(200);
    action.setWidgetNames(["BCS"]);
};

Post Reply

Return to “QCAD 'How Do I' Questions”