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?
[SOLVED] Retrive attributes from blocks
Moderator: andrew
Forum rules
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
[SOLVED] Retrive attributes from blocks
Last edited by Pawel on Wed Feb 11, 2015 8:16 pm, edited 3 times in total.
Re: Retrive attributes from blocks
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:
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... }
Re: Retrive attributes from blocks
Now it's clear.
Thank You Andrew
Thank You Andrew
Re: Retrive attributes from blocks
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:
Where is mistake?
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?
Re: Retrive attributes from blocks
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:
Re: Retrive attributes from blocks
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?
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?
Re: Retrive attributes from blocks
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:
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(), ... } } }
Re: [SOLVED] Retrive attributes from blocks
Andrew,
Your post resolve my problem.
Thanks for Your help.
Your post resolve my problem.
Thanks for Your help.