[SOLVED] Retrive attributes from blocks

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
Pawel
Newbie Member
Posts: 7
Joined: Sun Jan 18, 2015 12:51 pm

[SOLVED] Retrive attributes from blocks

Post by Pawel » Sun Jan 25, 2015 6:00 pm

Hi everyone,
I'd like export attributes from blocks to file.
My question is how to get tag and attributes from blocks.
I have:
block = doc.queryBlock(id);
name = block.getName()

Can You help me?
Last edited by Pawel on Wed Feb 11, 2015 8:16 pm, edited 3 times in total.

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

Re: Retrive attributes from blocks

Post by andrew » Mon Jan 26, 2015 10:01 am

Block definitions (RBlock) can contain attribute definition entities (RAttributeDefinitionEntity). The value of block attributes is stored in attribute entities (RAttributeEntity) which are stored at the same level as the block reference entity they belong to.

Finding all block attribute definitions for a block:
var ids = doc.queryBlockEntities(blockId);
for (var i=0; i<ids.length; i++) {
    var id = ids;
    var e = doc.queryEntity(id);
    if (!isAttributeDefinitionEntity(e)) {
        continue;
    }
    // e is a block attribute definition...
}

Pawel
Newbie Member
Posts: 7
Joined: Sun Jan 18, 2015 12:51 pm

Re: Retrive attributes from blocks

Post by Pawel » Mon Jan 26, 2015 9:04 pm

Now it's clear.
Thank You Andrew

Pawel
Newbie Member
Posts: 7
Joined: Sun Jan 18, 2015 12:51 pm

Re: Retrive attributes from blocks

Post by Pawel » Mon Feb 02, 2015 8:59 pm

Hi,
I try get attributes and its tags block by block but it seems I do something wrong.
For example I have:
block1 with attributes: Name=First, Tag1=1, Tag2=1;
block2 with attributes: Name=Second, Tag1=2, Tag2=2;
I'd like:
Name First
Tag1 1
Tag2 1
Name Second
Tag1 2
Tag2 2

But i get:
Tag1 1
Tag1 2
Tag2 1
Tag2 2
Name First
Name Second


My code:
var bloki = doc.queryAllBlocks();
    for (var i=0; i<bloki.length; ++i) {
        var id_bloku = bloki;
        blok = doc.queryBlock(id_bloku);
        if (blok.isNull()) {
            continue;
        }
	var ids = doc.queryBlockEntities(id_bloku);
	for (var j=0; j<ids.length; j++) {
    		var id_atryb = ids[j];
    		var e = doc.queryEntity(id_atryb);
			if(isAttributeEntity(e)){ 
			var test = e.getTag();
			var test1 = e.getPlainText();
			ts.writeString("\n%1\t%2".arg(test).arg(test1));
			}
	}

Where is mistake?

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

Re: Retrive attributes from blocks

Post by andrew » Tue Feb 03, 2015 10:16 am

Please use qDebug() to output information in your loops to see what's going on:
qDebug("id_bloku:",id_bloku);
...
qDebug("blok:",blok.getName());
...
When posting code to the forum, please mark it and choose JavaScript from the combobox to format it in a readable way:
Screen Shot 2015-02-03 at 10.15.31.png
Screen Shot 2015-02-03 at 10.15.31.png (27.03 KiB) Viewed 10652 times

Pawel
Newbie Member
Posts: 7
Joined: Sun Jan 18, 2015 12:51 pm

Re: Retrive attributes from blocks

Post by Pawel » Sun Feb 08, 2015 2:57 pm

Thanks for Your advice Andrew,
I used qDebug and try explain what I got.
First I created one block definition with Tag. Then I pasted three times this block into drawing.
Than I run my script (script should extract tag attributes with values and write them into csv file).
I got all text attributes assigned to block named *Model_Space.
Correct me if I'm wrong but i think that all text attributes during pasting block are assigned to block named *Model_Space.
Is it possible to connect tags and their values with id of block that is pasted?
I tried use queryBlockReferences(id_blockdefinition) but i don't know is it right direction.
Can You help?

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

Re: Retrive attributes from blocks

Post by andrew » Mon Feb 09, 2015 10:39 am

Block attribute definitions are stored in block definitions.

Block attributes are linked to block references (block instances), not block definitions. So you have to list all blocks, all block references and then all attributes:
var doc = this.getDocument();

    // iterate through all block definitions:
    var blockIds = doc.queryAllBlocks();
    for (var i=0; i<blockIds.length; ++i) {
        var blockId = blockIds;

        // iterate through all block references of the current block:
        var blockRefIds = doc.queryBlockReferences(blockId);
        for (var k=0; k<blockRefIds.length; ++k) {
            var blockRefId = blockRefIds[k];

            // iterate through all attributes of the current block reference:
            var attributeIds = doc.queryChildEntities(blockRefId, RS.EntityAttribute);
            for (var c=0; c<attributeIds.length; c++) {
                var attributeId = attributeIds[c];
                var attribute = doc.queryEntityDirect(attributeId);
                if (attribute.isNull()) {
                    continue;
                }

                // do something with attribute.getTag(), attribute.getPlainText(), ...
            }
        }
    }

Pawel
Newbie Member
Posts: 7
Joined: Sun Jan 18, 2015 12:51 pm

Re: [SOLVED] Retrive attributes from blocks

Post by Pawel » Wed Feb 11, 2015 8:18 pm

Andrew,
Your post resolve my problem.
Thanks for Your help.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”