Shell works, function doesn't work

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
sinase
Active Member
Posts: 47
Joined: Sat Jan 30, 2021 11:12 pm

Shell works, function doesn't work

Post by sinase » Tue Mar 02, 2021 6:30 pm

Hello, i have a new question. When i run the follow code with the shell into QCAD, it works:

Code: Select all

//BEGIN
var action = RGuiAction.getByScriptFile("scripts/Edit/Copy/Copy.js");
if (!isNull(action)) {
    action.slotTrigger();
}
var action = RGuiAction.getByScriptFile("scripts/File/NewFile/NewFile.js");
if (!isNull(action)) {
    action.slotTrigger();
}
var document = getDocument();

var op = new RPasteOperation(RDocument.getClipboard());
op.setOffset(new RVector(0,0));
op.setRotation(0.0);
op.setScale(1.0);
op.setFlipHorizontal(false);
op.setFlipVertical(false);
op.setToCurrentLayer(true);
op.setOverwriteBlocks(true);
op.setCopyEmptyBlocks(true);

var di = getDocumentInterface();
di.applyOperation(op);

this.autoZoom();

var dii = new RDocumentInterface(document);
dii.exportFile("/Users/jchueca/Desktop/exporteision.dxf", "DXF 2000");
//END
But when i try to put this code into a function, because i want a button in my interface, doesn't work:

Code: Select all

//BEGIN
include("../DrawExamples.js");

/**
 * \class copyPaste
 * \ingroup 
 * Tras seleccionar una forma, copia y pega en otra pestaña y lo exporta automáticamente a un nuevo .dxf.
 */
function CopyPaste(guiAction) {
    DrawExamples.call(this, guiAction);
}

CopyPaste.prototype = new DrawExamples();

CopyPaste.prototype.beginEvent = function() {
    DrawExamples.prototype.beginEvent.call(this);

	var di = this.getDocumentInterface();
	var document = this.getDocument();
	
    var action = RGuiAction.getByScriptFile("scripts/Edit/Copy/Copy.js");
	if (!isNull(action)) {
		action.slotTrigger();
	}

	var action2 = RGuiAction.getByScriptFile("scripts/File/NewFile/NewFile.js");
	if (!isNull(action2)) {
		action2.slotTrigger();
	}
	
	var op = new RPasteOperation(RDocument.getClipboard());
	op.setOffset(new RVector(0,0));
	op.setRotation(0.0);
	op.setScale(1.0);
	op.setFlipHorizontal(false);
	op.setFlipVertical(false);
	op.setToCurrentLayer(true);
	op.setOverwriteBlocks(true);
	op.setCopyEmptyBlocks(true);
	
	di.applyOperation(op);

	this.autoZoom();
	
	// var document = getDocument();
	// var dii = new RDocumentInterface(document);
	// dii.exportFile("/Users/jchueca/Desktop/exporteision.dxf", "DXF 2000");

};

CopyPaste.init = function(basePath) {
    var action = new RGuiAction(qsTr("CopyPaste"), RMainWindowQt.getMainWindow());
    action.setRequiresDocument(true);
    action.setScriptFile(basePath + "/CopyPaste.js");
    action.setStatusTip(qsTr("CopyPaste"));
    action.setGroupSortOrder(73100);
    action.setSortOrder(500);
    action.setWidgetNames(["DrawExamplesMenu"]);
};
//END
I think the code stops just before to run paste order.

Please, could you help me with this?
Thank you!

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

Re: Shell works, function doesn't work

Post by andrew » Wed Mar 03, 2021 9:27 am

You action is running in the context of the current document. In your action, you are then creating a new document. But since the script is running in the context of the first document, the paste operation also takes place there.

To work on multiple document, your script has to run in the global script context, the one that creates new documents, closes documents, handles persistent widgets, etc.

This can be done by setting:

action.setRequiresDocument(false);

Then, be careful to get the document interface for pasting after creating the new document:

Code: Select all

include("../DrawExamples.js");

/**
 * \class copyPaste
 * \ingroup 
 * Tras seleccionar una forma, copia y pega en otra pestaña y lo exporta automáticamente a un nuevo .dxf.
 */
function CopyPaste(guiAction) {
    DrawExamples.call(this, guiAction);
}

CopyPaste.prototype = new DrawExamples();

CopyPaste.prototype.beginEvent = function() {
    DrawExamples.prototype.beginEvent.call(this);

    var action = RGuiAction.getByScriptFile("scripts/Edit/Copy/Copy.js");
	if (!isNull(action)) {
		action.slotTrigger();
	}

	var action2 = RGuiAction.getByScriptFile("scripts/File/NewFile/NewFile.js");
	if (!isNull(action2)) {
		action2.slotTrigger();
	}

	var di = EAction.getDocumentInterface();
	var document = di.getDocument();
	
	var op = new RPasteOperation(RDocument.getClipboard());
	op.setOffset(new RVector(0,0));
	op.setRotation(0.0);
	op.setScale(1.0);
	op.setFlipHorizontal(false);
	op.setFlipVertical(false);
	op.setToCurrentLayer(true);
	op.setOverwriteBlocks(true);
	op.setCopyEmptyBlocks(true);
	
	di.applyOperation(op);

	di.autoZoom();
	
	// var document = getDocument();
	// var dii = new RDocumentInterface(document);
	// dii.exportFile("/Users/jchueca/Desktop/exporteision.dxf", "DXF 2000");

};

CopyPaste.init = function(basePath) {
    var action = new RGuiAction(qsTr("CopyPaste"), RMainWindowQt.getMainWindow());
    action.setRequiresDocument(false);
    action.setScriptFile(basePath + "/CopyPaste.js");
    action.setStatusTip(qsTr("CopyPaste"));
    action.setGroupSortOrder(73100);
    action.setSortOrder(500);
    action.setWidgetNames(["DrawExamplesMenu"]);
};

sinase
Active Member
Posts: 47
Joined: Sat Jan 30, 2021 11:12 pm

Re: Shell works, function doesn't work

Post by sinase » Wed Mar 03, 2021 10:23 am

Thank you for your answer,

I´m triying with the modification that you said but this code dont work correctly yet. The copied figure is not pasted on my new tab. I cant get the current view that you said.

If you have any idea that could help me, i will be gratted.
Many thanks.

CVH
Premier Member
Posts: 3470
Joined: Wed Sep 27, 2017 4:17 pm

Re: Shell works, function doesn't work

Post by CVH » Wed Mar 03, 2021 12:35 pm

hi,
I copied Andrew's code.
Created a new folder ...\QCAD\scripts\Misc\Examples\DrawExamples\CopyPaste
Saved the code to ...\QCAD\scripts\Misc\Examples\DrawExamples\CopyPaste\CopyPaste.js

On a reboot in debugger mode ...
With a file with some content ...
Selected some content ...
Started Misc .. Script Examples .. Draw .. Copy/Paste

I get a new drawing with the selected content pasted to.
The debugger stays inactive.
All went fine. :P

Meaning that:

Code: Select all

40  di.applyOperation(op);
42  di.autoZoom();
worked out. :!:

Meaning that 'di' is the document interface of the new document.
What also would be the interface to export. :wink:

Regards,
CVH

CVH
Premier Member
Posts: 3470
Joined: Wed Sep 27, 2017 4:17 pm

Re: Shell works, function doesn't work

Post by CVH » Wed Mar 03, 2021 2:44 pm

Hi,

Thanks for the script or at least the general idea ... :wink:
Only changed setToCurrentLayer to false so I keep the layer structure.
And I don't need the export but it works flawless with 'di'.
One could insert a file dialog there.
Ended the beginEvent with 'this.terminate();'

Regards,
CVH

sinase
Active Member
Posts: 47
Joined: Sat Jan 30, 2021 11:12 pm

Re: Shell works, function doesn't work

Post by sinase » Wed Mar 03, 2021 3:43 pm

Hello, when i have been did test with the code, in my qcad didn't work. I have pasted the code directly on the ecma shell script and i have seen this error:

ecma> op.setCopyEmptyBlocks(true);
TypeError: Result of expression 'op.setCopyEmptyBlocks' [undefined] is not a function.

With this line comented, .js code works perfectly.

I dont know if this line is very important but i'm working on my project without that. This project will be run on five computers, we will have adquired 5 licenses for that.

Many thanks for your help.
Kind regards.

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

Re: Shell works, function doesn't work

Post by andrew » Wed Mar 03, 2021 4:01 pm

OK, great. Yes, simply leave setCopyEmptyBlocks away. It's a recent addition.

sinase
Active Member
Posts: 47
Joined: Sat Jan 30, 2021 11:12 pm

Re: Shell works, function doesn't work

Post by sinase » Wed Mar 03, 2021 6:28 pm

Hello sorry, one more question.

I try to read the file content (.txt file) before export my .dxf. I have found a js sample but it doesn't works correctly because qcad gives me this error:

ecma> var fs = require('fs');
ReferenceError: Can't find variable: require

My code at the moment is:

//BEGIN
var action = RGuiAction.getByScriptFile("scripts/Edit/Copy/Copy.js");
if (!isNull(action)) {
action.slotTrigger();
}

var action2 = RGuiAction.getByScriptFile("scripts/File/NewFile/NewFile.js");
if (!isNull(action2)) {
action2.slotTrigger();
}

var di = EAction.getDocumentInterface();
var document = di.getDocument();

var op = new RPasteOperation(RDocument.getClipboard());
op.setOffset(new RVector(0,0));
op.setRotation(0.0);
op.setScale(1.0);
op.setFlipHorizontal(false);
op.setFlipVertical(false);
op.setToCurrentLayer(true);
op.setOverwriteBlocks(true);
//op.setCopyEmptyBlocks(true);

di.applyOperation(op);
di.autoZoom();

var fs = require('fs');
fs.readFile('/Planning/Oferta.txt', 'utf8', function(err, data) {
if (err) {
return console.log(err);
}
di.exportFile("/Users/jchueca/Desktop/" + data + ".dxf", "DXF 2000");
});

// di.exportFile("/Users/jchueca/Desktop/exported.dxf", "DXF 2000");
//END

I need to learn more and more about js coding but maybe you could help directly.
Many thanks.

CVH
Premier Member
Posts: 3470
Joined: Wed Sep 27, 2017 4:17 pm

Re: Shell works, function doesn't work

Post by CVH » Wed Mar 03, 2021 6:57 pm

Hi,
The Forum rules above state 'One question per Topic'
To keep it searchable ... :wink:

I think that the 'require' method is not implemented in the EMCAScript under QCAD.
I can't find a readFile method either ...
We wouldn't know what you are reading from the file or what the intended return would be.

Could you start a new topic and elaborate:
sinase wrote:
Wed Mar 03, 2021 6:28 pm
I have found a js sample
or what the intention is?

Regards,
CVH

sinase
Active Member
Posts: 47
Joined: Sat Jan 30, 2021 11:12 pm

Re: Shell works, function doesn't work

Post by sinase » Thu Mar 04, 2021 8:15 am

Hello, i have written other topic with that question.

Thank you very much for your help.

Post Reply

Return to “QCAD 'How Do I' Questions”