How to change active block via script.

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
mrhickman53
Junior Member
Posts: 17
Joined: Thu Sep 07, 2023 1:17 am

How to change active block via script.

Post by mrhickman53 » Fri Sep 08, 2023 10:38 pm

using the script shell, I can find the BlockId for the block highlighted in blue using Block.getActiveBlockId(). Using the term "Active" for the block to be insterted vs. "Current" for the block it will be inserted into, I need to change the active block, either by name or Id from a script. What function would perform this?

I can change the current block with documentInterface().setCurrentBlock(blockname).
qcad pro 3.28.1.0 on ubuntu 22.04

mrhickman53
Junior Member
Posts: 17
Joined: Thu Sep 07, 2023 1:17 am

Re: How to change active block via script.

Post by mrhickman53 » Sat Sep 09, 2023 1:19 am

I have not solved my problem yet, but it seems that RBlockReferenceData() has the structure and methods to accomplish what I am setting out to do. I finally started understanding InsertBlock.js in order to discover this.

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

Re: How to change active block via script.

Post by CVH » Sat Sep 09, 2023 6:13 am

Hi,

Indeed, you can use a RBlockReferenceData()
var blockRefData = new RBlockReferenceData();
The block it refers to can be set by blockRefData.setReferencedBlockId(blockId);
Or by block name blockRefData.setReferencedBlockName(....);

From this data object you need to construct an RBlockReferenceEntity and cast that to the drawing.

The same can be done when you have a RBlockReferenceEntity() queried from the drawing.
blockRefEntity.setReferencedBlockId(blockId);
blockRefEntity.setReferencedBlockName(....);

The change is not immediate, you need to cast this RBlockReferenceEntity back to the drawing.

InsertBlock.js includes BlockInsert.js
BlockInsert.js includes Block.js
Block.js includes BlockList.js
Block.getActiveBlockId() will retrieve the highlighted block in the block list:
https://github.com/qcad/qcad/blob/maste ... ck.js#L110

The current active block (the one you are editing) can be retrieved by doc.getCurrentBlockId()

There is a danger in setting the current block, Block.editBlock() will verify if you are not editing a block in-place among other things:
https://github.com/qcad/qcad/blob/maste ... ck.js#L159

Regards,
CVH

mrhickman53
Junior Member
Posts: 17
Joined: Thu Sep 07, 2023 1:17 am

Re: How to change active block via script.

Post by mrhickman53 » Sat Sep 23, 2023 10:34 pm

Thank you. I've gotten my block insertions working. You have been helpful.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”