Using a Script to Navigate Around the GUI

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
333onlyhalfevil
Junior Member
Posts: 24
Joined: Fri Apr 28, 2023 12:39 pm

Using a Script to Navigate Around the GUI

Post by 333onlyhalfevil » Sat Jun 17, 2023 4:07 pm

Hello everyone:

How do I make my script tell where in the GUI the user currently is (i.e., are they viewing the model space? or a layout? or a block?) and/or how do I have a script force the user around the GUI (for example, if they click something in a layout the script would automatically move them to the model space)?

Thanks in advance for your time/replies.

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

Re: Using a Script to Navigate Around the GUI

Post by CVH » Sat Jun 17, 2023 7:30 pm

333onlyhalfevil wrote:
Sat Jun 17, 2023 4:07 pm
i.e., are they viewing the model space? or a layout? or a block?
The Block List will tell you that.
If that is not showing then menu View .. Block List (GB) or by its shortcut: Type 'G' and then 'B'.
333onlyhalfevil wrote:
Sat Jun 17, 2023 4:07 pm
for example, if they click something in a layout the script would automatically move them to the model space
I don't see any reason for that. One can have Viewports that refer to Model_Space on a Layout but any other entity will do.
Remark that Model_Space is basically not more than a Block and Layouts are that too.
You probably need 'Listeners' for that:
https://github.com/qcad/qcad/tree/maste ... erExamples

Regards,
CVH

333onlyhalfevil
Junior Member
Posts: 24
Joined: Fri Apr 28, 2023 12:39 pm

Re: Using a Script to Navigate Around the GUI

Post by 333onlyhalfevil » Wed Jun 21, 2023 5:34 pm

Thank you for the reply.

For the first part, I meant how do I figure that out programmatically? If I'm programming a coordinateEvent() function, how do I get the script to tell whether the user clicked inside of the modelspace block or inside of a layout block?

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

Re: Using a Script to Navigate Around the GUI

Post by CVH » Wed Jun 21, 2023 5:50 pm

333onlyhalfevil wrote:
Wed Jun 21, 2023 5:34 pm
how do I get the script to tell whether the user clicked inside of the modelspace block or inside of a layout block?

Code: Select all

var document = this.getDocument();
var currentBlockId = document.getCurrentBlockId();
Or

Code: Select all

var currentBlockName = document.getCurrentBlockName();
See: https://qcad.org/doc/qcad/3.0/developer ... 9c1d88d5c4

Regards,
CVH

333onlyhalfevil
Junior Member
Posts: 24
Joined: Fri Apr 28, 2023 12:39 pm

Re: Using a Script to Navigate Around the GUI

Post by 333onlyhalfevil » Sun Jun 25, 2023 4:54 pm

Thank you.

I was close. I was trying to use the getActiveBlockId function from Block.js. You can see it on line 113 in the source code of Block.js (https://github.com/qcad/qcad/blob/maste ... k/Block.js). Curiously, this method isn't in the ECMAscript Block class reference section of the developer documentation here (https://qcad.org/doc/qcad/3.0/developer ... block.html).

My next question seems to be with getting the block listener to work. Referring to this thread (https://qcad.org/rsforum/viewtopic.php? ... 8a03f2c20b) and this example code (https://github.com/qcad/qcad/blob/maste ... ner.js#L49); they are using an RTransactionListenerAdapter class within the code but I can't find this class within the developer documentation. Where can I find information regarding the Adapter classes and what constructors/functions they contain?

Am I even trying to do this right? If I do a doc.setCurrentBlock(modelSpace) line, it doesn't seem to do anything. At this time, I'm assuming that it does something behind the scenes and I need to use the RBlockListener to update the program to show the change. I was trying to do a this.updateBlocks(di) with di being the this.getDocumentInterface() line with no luck. What do you think?

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

Re: Using a Script to Navigate Around the GUI

Post by CVH » Sun Jun 25, 2023 6:24 pm

.getCurrentBlockId() or .getCurrentBlockName() are functions of the RDocument class: https://qcad.org/doc/qcad/3.0/developer ... ument.html
333onlyhalfevil wrote:
Sun Jun 25, 2023 4:54 pm
Curiously, this method isn't in the ECMAscript Block class reference section of the developer documentation here (https://qcad.org/doc/qcad/3.0/developer ... block.html).
getActiveBlockId is a function of the base class for QCAD block tools.
It is not listed in https://qcad.org/doc/qcad/3.0/developer ... block.html as do all functions that don't have the prototype mentioned.

And then there is also the RBlock class, what is all about the actual blocks: https://qcad.org/doc/qcad/3.0/developer ... block.html
333onlyhalfevil wrote:
Sun Jun 25, 2023 4:54 pm
My next question seems to be with getting the block listener to work.
Please post one question per topic, see above forum rules in red.
That makes the forum searchable. :wink:

With doc.setCurrentBlock(modelSpace) you set the current block of the reference in doc to the content of the variable called "modelSpace".
There is no transaction involved to return that to the RDocument.

I think you are better off with the paved path of 'Return to Main Drawing (EM)': https://github.com/qcad/qcad/blob/maste ... Drawing.js
or
'Edit Block (BE)': https://github.com/qcad/qcad/blob/maste ... itBlock.js
They check for the Block List and block/unblock the signals of the Block list.
Eventually they will call this.editBlock (Block.prototype.editBlock) and thus Block.editBlock(di, blockName): https://github.com/qcad/qcad/blob/maste ... k/Block.js

Straightforward with Model_Space as that is a default block in each document.
For other block you need to check if the document has the intended block.

Still I wonder why you want to navigate to the blocks by custom script ... Simply double click on a Block Reference will switch to that block in edit mode or type the key-in shortcut EM to return to Model_Space ... :?

Regards,
CVH

333onlyhalfevil
Junior Member
Posts: 24
Joined: Fri Apr 28, 2023 12:39 pm

Re: Using a Script to Navigate Around the GUI

Post by 333onlyhalfevil » Mon Jun 26, 2023 2:55 pm

Thank you for the help.

I seem to have it working as expected now.

To satisfy your curiosity, I'm trying to program a custom search function that basically searches through the drawing to find common mistakes and bring the user to them, similar to how ctrl+F does it with text in word docs. When you click the 'next' button (presumably inside a widget which I haven't started trying to figure out how to do yet), the program would bring you to wherever in the drawing the mistake is. If the user is currently viewing a layout and the next one is in the model space, when they click next, the program would have to move them to the model space.

To sum up (for someone who might find themselves here in the future), I got it working using a line of code out of the link from this part of CVH's previous reply:
I think you are better off with the paved path of 'Return to Main Drawing (EM)': https://github.com/qcad/qcad/blob/maste ... Drawing.js
In particular, I used the commented out line of code: this.getDocumentInterface().setCurrentBlock(RBlock.modelSpaceName);

Another thing to note is that the RBlock class has a public attribute called modelSpaceName so you don't need to get the model space block's id into a variable before trying to use that variable to move the user to the model space. You can just use modelSpaceName.

Anyways, thanks again for the help. I really appreciate it. If I come across another issue that I need help with, I'll be sure to post it in a new thread. :wink:

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

Re: Using a Script to Navigate Around the GUI

Post by CVH » Mon Jun 26, 2023 9:50 pm

333onlyhalfevil wrote:
Mon Jun 26, 2023 2:55 pm
In particular, I used the commented out line of code: this.getDocumentInterface().setCurrentBlock(RBlock.modelSpaceName);
That was commented out by a commit on Mar 30, 2013: "remember zoom and offset when switching blocks".
The paved method also checks if the user is not by any chance editing a block "In-Place".

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”