replace a block reference with another block

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
arun
Active Member
Posts: 34
Joined: Tue May 09, 2023 9:04 am

replace a block reference with another block

Post by arun » Mon Jul 24, 2023 9:47 am

Hi,
How do I replace a block reference with another block reference in qcad script.
I am using qcad 3.28.1 on linux.
Thanks,
Arun

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

Re: replace a block reference with another block

Post by CVH » Mon Jul 24, 2023 10:57 pm

arun wrote:
Mon Jul 24, 2023 9:47 am
How do I replace a block reference with another block reference in qcad script.
Query the block reference entity by id:
var doc = this.getDocument();
var blockref = doc.queryEntity(entityId);


Query the block definition by the intended block name:
var block = doc.queryBlockDirect("MyNewBlock");
(Query Direct, no modification required)

Set the referenced block id of your block:
blockref.setReferencedBlockId(block.getId());

Cast the modified block reference back to the drawing:
var di = this.getDocumentInterface();
var op = new RAddObjectOperation(blockref, false, true);

(NOTuseCurrentAttributes, undoAble)
di.applyOperation(op);

Another option is to use an RModifyObjectOperation() instead:
var op = new RModifyObjectOperation(blockref);

It is best that you verify that a block reference entity and a block is returned instead of nothing.
And it might be required to clone the reference first.

Regards,
CVH

arun
Active Member
Posts: 34
Joined: Tue May 09, 2023 9:04 am

Re: replace a block reference with another block

Post by arun » Tue Jul 25, 2023 5:57 am

Thanks CVH :) . Will check this out.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”