script not working

This forum is for 'Work-in-Progress' QCAD user developed script Add Ons and Plug-ins.

Please use this forum to request/submit your script Add-Ons & Plug-in idea's.

Moderators: andrew, Husky, J-J

Post Reply
User avatar
dfriasb
Senior Member
Posts: 119
Joined: Thu Mar 10, 2016 1:08 pm
Location: Calafell, Tarragona, Spain

script not working

Post by dfriasb » Sat May 02, 2020 3:51 pm

Dear all,

I have reviewed this code many times but I can't find what is not working. When I paste the central part in QCAD script console it works great, but not when try it as a command. I guess it is something related with guiAction; looks like action is not completed...
Any help will be very welcomed.

Best regards,

David

This is the code that works correctly on console:

Code: Select all

var di = this.getDocumentInterface();
	var doc = this.getDocument();
	var selectedEntities = doc.querySelectedEntities();
	var op = new RAddObjectsOperation();
	
for (i=0; i<selectedEntities.length; i++)	{
entity = doc.queryEntity(selectedEntities[i]);
if (!isBlockReferenceEntity(entity)) {continue;}
blockName = entity.getReferencedBlockName();
blockRefPosition = entity.getPosition();
blockRefAngle = entity.getRotation()*360/(2*Math.PI);
// impressió ::
printText = addSimpleText(blockName, blockRefPosition, 0.003, blockRefAngle, "Cotes", RS.VAlignMiddle, RS.HAlignCenter, false, false);
op.addObject(printText);}
And this is the complete file:

Code: Select all

// prints all selected block references block names. Text angle displayed is block reference rotation angle ::
//  !!!  something is not working when starting the command on QCAD.

include("../DFa.js");
include("simple.js");
function printblocknameselected(guiAction) {
	DFa.call(this, guiAction);}

printblocknameselected.prototype = new DFa();

printblocknameselected.prototype.beginEvent = function() {
	DFa.prototype.beginEvent.call(this);

// THIS PART IS WORKING IN QCAD SCRIPT CONSOLE :

	var di = this.getDocumentInterface();
	var doc = this.getDocument();
	var selectedEntities = doc.querySelectedEntities();
	var op = new RAddObjectsOperation();
	
for (i=0; i<selectedEntities.length; i++)	{
entity = doc.queryEntity(selectedEntities[i]);
if (!isBlockReferenceEntity(entity)) {continue;}
blockName = entity.getReferencedBlockName();
blockRefPosition = entity.getPosition();
blockRefAngle = entity.getRotation()*360/(2*Math.PI);
// impressió ::
printText = addSimpleText(blockName, blockRefPosition, 0.003, blockRefAngle, "Cotes", RS.VAlignMiddle, RS.HAlignCenter, false, false);
op.addObject(printText);}

// TIL HERE.

di.applyOperation(op);
this.terminate();
};

// Adds menu&command for this action::

printblocknameselected.init = function(basePath) {
    var action = new RGuiAction(qsTr("&printblocknameselected"), RMainWindowQt.getMainWindow());
    action.setRequiresDocument(true);
    action.setScriptFile(basePath + "/printblocknameselected.js");
    action.setDefaultCommands(["bnames"]);
    action.setDefaultShortcut(new QKeySequence("b,n"));
    action.setGroupSortOrder(78100);
    action.setSortOrder(200);
    action.setWidgetNames(["DFaMenu"]);
};
Attachments
printblocknameselected.js
(1.64 KiB) Downloaded 580 times
David Frías Barranco | architect
[email protected] | davidfriasarquitecto.es

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

Re: script not working

Post by andrew » Tue May 05, 2020 9:40 am

This seems to work fine here (adds a small text at selected block reference coordinates). I did have to make a few tweaks (fix include paths, no operation needed in Simple API). Note that I'm deriving from EAction as I don't have DFa.js and I'm adding to the menu Misc > Block. I also increased the text size (easier for testing).

Code: Select all

// prints all selected block references block names. Text angle displayed is block reference rotation angle ::
//  !!!  something is not working when starting the command on QCAD.

include("scripts/EAction.js");
include("scripts/simple.js");

function printblocknameselected(guiAction) {
    EAction.call(this, guiAction);
}

printblocknameselected.prototype = new EAction();

printblocknameselected.prototype.beginEvent = function() {
    EAction.prototype.beginEvent.call(this);

    // THIS PART IS WORKING IN QCAD SCRIPT CONSOLE :

    var di = this.getDocumentInterface();
    var doc = this.getDocument();
    var selectedEntities = doc.querySelectedEntities();

    for (i=0; i<selectedEntities.length; i++)	{
        entity = doc.queryEntity(selectedEntities[i]);
        if (!isBlockReferenceEntity(entity)) {
            continue;
        }
        blockName = entity.getReferencedBlockName();
        blockRefPosition = entity.getPosition();
        blockRefAngle = entity.getRotation()*360/(2*Math.PI);
        // impressió ::
        addSimpleText(blockName, blockRefPosition, 1, blockRefAngle, "Cotes", RS.VAlignMiddle, RS.HAlignCenter, false, false);
    }

    // TIL HERE.

    this.terminate();
};

// Adds menu&command for this action::

printblocknameselected.init = function(basePath) {
    var action = new RGuiAction(qsTr("&printblocknameselected"), RMainWindowQt.getMainWindow());
    action.setRequiresDocument(true);
    action.setScriptFile(basePath + "/printblocknameselected.js");
    action.setDefaultCommands(["bnames"]);
    action.setDefaultShortcut(new QKeySequence("b,n"));
    action.setGroupSortOrder(78100);
    action.setSortOrder(200);
    action.setWidgetNames(["MiscBlockMenu"]);
};

User avatar
dfriasb
Senior Member
Posts: 119
Joined: Thu Mar 10, 2016 1:08 pm
Location: Calafell, Tarragona, Spain

Re: script not working

Post by dfriasb » Tue May 05, 2020 3:00 pm

Thank you Andrew,

Now it's working also for me. I moved to /Misc and changed DFa.js by EAction.js. So I think the bug is in my DFa.js file.

Regards, David.
David Frías Barranco | architect
[email protected] | davidfriasarquitecto.es

Post Reply

Return to “QCAD 'Script Add-On & Plug-in challenge' - Work in Progress”