[SOLVED] correctly add multiple blocks inside script

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
smf
Premier Member
Posts: 177
Joined: Tue Feb 28, 2012 1:05 pm

[SOLVED] correctly add multiple blocks inside script

Post by smf » Sun Dec 14, 2014 7:57 pm

Hi all,

after looking at scripts/Block/InsertBlock/InsertBlock.js I finally got a multi-insertion of blocks working. Now I'm wondering why

Code: Select all

      var nextFreeId = document.getStorage().getMaxObjectId(); //<<<<<<<<< needed?
      //...... something more here
     //and then for each block to insert I do:
      var blockRefId = nextFreeId++;                              //<<<<<<<<< needed?
      var ids = document.queryBlockEntities(blockId);
      for (var j = 0; j < ids.length; j++) {
        var id = ids[j];
        var e = document.queryEntity(id);
        if (!isAttributeDefinitionEntity(e)) {
          continue;
        }
        var att = new RAttributeEntity(document, new RAttributeData(e.getData(), blockRefId, e.getTag()));  //<<<<<<< more parameters here than in code snippet below
        blockRef.applyTransformationTo(att);
        var tag = att.getTag();
        if (!isNull(this.attributes[tag])) {
          att.setText(this.attributes[tag]);
        }
        op.addObject(att);
      }
is working as well as the folling code for each block without any nextFreeId

Code: Select all

      var ids = document.queryBlockEntities(blockId);
      for (var j = 0; j < ids.length; j++) {
        var id = ids[j];
        var e = document.queryEntity(id);
        if (!isAttributeDefinitionEntity(e)) {
          continue;
        }
        var att = new RAttributeEntity(document, new RAttributeData(e.getData())); //<<<<<<<< call has other signature!!!
        blockRef.applyTransformationTo(att);
        var tag = att.getTag();
        if (!isNull(this.attributes[tag])) {
          att.setText(this.attributes[tag]);
        }
        op.addObject(att);
      }
Is the value of document.getStorage().getMaxObjectId() not needed and the code without nextFreeId valid?

Thank you very much,
Stefan
Last edited by smf on Thu Dec 18, 2014 3:17 pm, edited 1 time in total.

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

Re: correctly add multiple blocks inside script

Post by andrew » Mon Dec 15, 2014 2:09 pm

The blockRefId in
new RAttributeData(e.getData(), blockRefId, e.getTag()));
refers to the block reference the attribute belongs to. Attributes have a life on their own though, as individual entities on the same level as the block reference.

To check if an attribute is indeed linked to a block reference, you can select the block reference. This should also select the attributes associated with it.

smf
Premier Member
Posts: 177
Joined: Tue Feb 28, 2012 1:05 pm

Re: correctly add multiple blocks inside script

Post by smf » Mon Dec 15, 2014 2:56 pm

Thank you very much, Andrew. Perhaps I have a misunderstanding of "attribute". I don't have "user attributes", the properties like angle etc. are copied well. But I see that if I "want to do it right", I need the blockId.

How can the blockId be determined if there are several new blocks to be inserted - means: is the nextFreeId++ - approach valid?

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

Re: correctly add multiple blocks inside script

Post by andrew » Thu Dec 18, 2014 2:21 pm

The best approach is probably to add the block references first, then add the attribute entities referring to them.

Note that an attribute in this context is an entity that displays a text and refers to a block reference.

smf
Premier Member
Posts: 177
Joined: Tue Feb 28, 2012 1:05 pm

Re: correctly add multiple blocks inside script

Post by smf » Thu Dec 18, 2014 2:50 pm

Thank you very much. I will then change my code to first insert all BlockReferences and after that create the attributes.

I'm very sorry to be unsagacious, but I still don't get the attribute-thing. :( I have blocks containing lines, hatches and text. The references to them should not contain anything more than "block A at position x,y with angle a". Thinking sometime about your statement "that displays a text and refers to a block reference", I inserted a qDebug in the attribute handling loop which never appears. So I temporary removed all code regarding attributeDefinitionEntities (means: the complete for-loop) - and the blocks appear to be exactly the same and look the way I need them.

The reduced code without Attribute-handling:

Code: Select all

      var pos = new RVector(this.targetPosition.getX(), this.targetPosition.getY());
      var blockReferenceData = new RBlockReferenceData();
      blockReferenceData.setReferencedBlockId(blockId);
      blockReferenceData.setPosition(new RVector(this.targetPosition.getX() + coa[0], this.targetPosition.getY() + coa[1]));
      blockReferenceData.setScaleFactors(new RVector(1.0, 1.0));
      blockReferenceData.setRotation(0.0);
      var blockRef = new RBlockReferenceEntity(document, blockReferenceData);
      op.addObject(blockRef);
       /*removed: document.queryBlockEntities(blockId) and handling of entities that are of type AttributeDefinitionEntity*/
This seem to work and is very short and perspicuous - but is this working by accident?

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

Re: correctly add multiple blocks inside script

Post by andrew » Thu Dec 18, 2014 2:56 pm

You can learn about block attributes at:
http://www.qcad.org/en/blog/136-block-a ... n-qcad-3-3

If you don't use them, you don't need to do anything with the RAttribute* classes indeed.

smf
Premier Member
Posts: 177
Joined: Tue Feb 28, 2012 1:05 pm

Re: [SOLVED] correctly add multiple blocks inside script

Post by smf » Thu Dec 18, 2014 3:22 pm

Thanks again - block attributes are sooooo cool! 8) I didn't know anything about them (using QCad for a long time before 3.3) and don't use them (yet).

Everything solved here, I want to express my sincere gratitude for your helping, always fast and constructive! :)

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

Re: [SOLVED] correctly add multiple blocks inside script

Post by andrew » Thu Dec 18, 2014 3:25 pm

OK, glad it's all sorted :)

Post Reply

Return to “QCAD 'How Do I' Questions”