Page 1 of 1
Create a new file from within a script
Posted: Thu Apr 28, 2016 9:45 am
by tukuyomi
Hi all!
I have to write a script that creates 2 drawings on different files.
I tried to
Code: Select all
include("scripts/File/NewFile.js")
but I don't understand how to use it.
I tried various things found on the doc and this forums, but for the life of me, I can't see any "Untitled 2" window
Can anyone please help me on this? Just create a new Window as if you clicked on the New File button and draw a few lines on it from a script?
Code: Select all
include("scripts/File/NewFile.js")
Something.prototype.beginEvent = function() {
//Stuff
var newFile = new NewFile();
//How to get DocumentInterface, a view, etc?
//How to start drawings entities
Thank you for your support
Edit : Typo
Re: Create a new file from within a script
Posted: Thu Apr 28, 2016 10:04 am
by andrew
The easiest way to do exactly the same as
File > New, is to manually trigger
File > New:
var fileNewAction = RGuiAction.getByScriptFile("scripts/File/NewFile/NewFile.js");
if (!isNull(fileNewAction)) {
fileNewAction.slotTrigger();
}
Re: Create a new file from within a script
Posted: Thu Apr 28, 2016 11:39 am
by tukuyomi
Hi Andrew, thanks for the reply!
I managed to get a new file by doing this :
Code: Select all
var _f = new NewFile();
_f.beginEvent();
//Error on next line
var _d = _f.getDocument();
var _di = _f.getDocumentInterface();
Note that the third line produces a warning:
Code: Select all
Warning: RAction::getDocument: Action has no document.
Anything I'm doing wrong? (note that I don't have a clue of what I'm doing here, I go by trials and errors
)
Thanks for your time in helping me.
Re: Create a new file from within a script
Posted: Thu Apr 28, 2016 11:59 am
by andrew
There are actions which run in the context of a document (i.e. require a document), for example all drawing or modification tools.
Other actions run in a global context and do not require a document (such as File > New).
Actions which run in a global context do not reference a document (there might not even be any documents open at all).
You can get the current document from any context using EAction.getDocument() which returns undefined if no documents are open.
Re: Create a new file from within a script
Posted: Thu Apr 28, 2016 3:00 pm
by tukuyomi
Thanks again for the reply.
I managed to get it working somehow :
I created a function in my myFunctions.js script on top on my MyScripts tree:
Code: Select all
function NewDrawing() {
var _f = RGuiAction.getByScriptFile("scripts/File/NewFile/NewFile.js");
if (!isNull(_f)) { _f.slotTrigger(); }
return [EAction.getDocument(), EAction.getDocumentInterface(), EAction.getGraphicsView()];
}
As you can see, it returns an array containing important stuff.
So in my scripts, I use
Code: Select all
BlahBlah.prototype.beginEvent() {
var _a = new NewDrawing(); //returns Array[0=Document, 1=DocumentInterface, 2=GraphicsView]
//Include some drawings
//Call autozoom when drawing is terminated
_a[2].autoZoom();
this.terminate();
}
Surely things can be done better, but It Works For Me™ so I'm happy!
Thanks again
Cheers!
Re: Create a new file from within a script
Posted: Sat Apr 30, 2016 11:34 am
by tukuyomi
I have another small question. Here is my script
include("../MyScripts.js");
function ExSkelton(guiAction) {
MyScripts.call(this, guiAction);
};
ExSkelton.prototype = new MyScripts();
ExSkelton.prototype.beginEvent = function() {
MyScripts.prototype.beginEvent.call(this);
var _f = RGuiAction.getByScriptFile("scripts/File/NewFile/NewFile.js");
if (!isNull(_f)) { _f.slotTrigger(); }
this.terminate();
};
ExSkelton.init = function(basePath) {
var action = new RGuiAction(qsTr("&Skelton"), RMainWindowQt.getMainWindow());
action.setRequiresDocument(true);
action.setScriptFile(basePath + "/ExSkelton.js");
action.setGroupSortOrder(50100);
action.setSortOrder(100);
action.setWidgetNames(["MyScriptsMenu"]);
};
The code works OK, I get a new file created. This is good.
But I would like to set
Code: Select all
(line 20) action.setRequiresDocument(false);
When doing this, my code works as well, but I get some warnings on the terminal:
Qcad 3.12.6
Code: Select all
Warning: RScriptHandlerEcma::eval: script engine exception: "TypeError: Result of expression 'a.finishEvent' [undefined] is not a function."
Qcad 3.14.2
Code: Select all
Warning: RScriptHandlerEcma::eval: script engine exception: "TypeError: Result of expression 'a.destroy' [undefined] is not a function."
Is it all that important?
I only tested my script on Linux so far, but at work, I have Qcad Pro on Windows 10 OS. Will my script work on Windows as well?
Thanks for replying
Re: Create a new file from within a script
Posted: Mon May 02, 2016 8:37 am
by andrew
Actions which do not require a document are run in a global script context. Such actions are also implicitly stateless, meaning that there is no user interaction possible and no events are delivered to such actions. For such actions, only beginEvent is called. Once beginEvent is finished executing, the action is always terminated and destroyed automatically. So calling this.terminate() is not necessary for such an action.
Re: Create a new file from within a script
Posted: Mon May 02, 2016 2:45 pm
by tukuyomi
Thanks for the reply !
I removed this.terminate(); from my code above, but I still get the warnings.
I guess it does not matter, as the scripts runs fine on Windows and Linux.
Thank you again, Cheers !