how to test text entity

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
martblek
Active Member
Posts: 29
Joined: Wed Sep 02, 2020 7:58 am

how to test text entity

Post by martblek » Tue Feb 09, 2021 12:36 pm

Hello,
Please how i test entities given by queryAllEntities ().
I need test if entity is RTextEntityPointer.
thanks.

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

Re: how to test text entity

Post by CVH » Tue Feb 09, 2021 1:34 pm

Code: Select all

    var document = this.getDocument();           // Reference to current document
    var ids = document.querySelectedEntities();  // Qset with selected id's
    var entity;                                  // Entity in process (EntityPointer)
    
     Skip on no Selection:
    if (ids.length == 0) {
        //
        // .....
        //
        return;
    }

    // Find Type: Text Entity
    for (i=0; i<ids.length; i++) {    // Cycle through selection
        // Get entity from document by id:
        entity = document.queryEntity(ids[i]);
        
        // Skip when no Entity:
        if (isNull(entity)) {
            continue;
        }
        
        // Filter on Text:
        if (isTextEntity(entity)) {
            //
            // .....
            //
        } // End isText 
    } // Loop selection
    
    // https://qcad.org/doc/qcad/3.0/developer/class_r_document.html#ab0c6b92bc6e4c3796aa17d79a957c678
    //
    // isTextEntity() among others in library.js
    //
    // There is also: doc.queryAllEntities(undone = false, allBlocks = false, RS.EntityText);
    // What returns an id list of all Text entities. 
    
Regards,
CVH

martblek
Active Member
Posts: 29
Joined: Wed Sep 02, 2020 7:58 am

Re: how to test text entity

Post by martblek » Tue Feb 09, 2021 1:49 pm

thanks for answer but

Code: Select all

ecma> a = doc.queryEntity(9145)
RTextEntityPointer(0x92d54e30)
ecma>isTextEntity(9145)
false
9145 is "Text" in attribute editor

a.getData() is RTextData and then i can read values from text.

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

Re: how to test text entity

Post by CVH » Tue Feb 09, 2021 3:02 pm

martblek wrote:
Tue Feb 09, 2021 1:49 pm

Code: Select all

var a = doc.queryEntity(9145);
Loads variable 'a' with an REntityPointer to the entity in the document by current id = 9145.

Then one check if variable 'a' is a text entity:

Code: Select all

isTextEntity(a);
CVH

martblek
Active Member
Posts: 29
Joined: Wed Sep 02, 2020 7:58 am

Re: how to test text entity

Post by martblek » Tue Feb 09, 2021 5:31 pm

thanks.
this works.

martblek
Active Member
Posts: 29
Joined: Wed Sep 02, 2020 7:58 am

Re: how to test text entity

Post by martblek » Thu Feb 11, 2021 9:12 am

Hello,
I have some time again, so I'm writing the script.
Of course, I also look at the manuals, I can't change the searched text or highlight it on the drawing.
Is there a special function that restores the drawing?
Thanks.

Code: Select all

var doc = this.getDocument();
var idx = doc.queryAllEntities();
var entity;
var text;

for(var i=0; i<idx.length; i++)
  {
    entity = doc.queryEntity(idx[i]);
    if(isTextEntity(entity))
      {
        text = entity.getData().getText();
        if(text.contains("GA"))
          {
          EAction.handleUserWarning("found " + text);
          entity.getData().setText("only test");
          }
      }
  }
  

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

Re: how to test text entity

Post by CVH » Thu Feb 11, 2021 10:43 am

Hi, that is more than one question. :wink:

RTextEntity has also the getPlainText() method.
Common is var ids from 'ID selected' or 'more ID's' but it is at your preference.

RTextEntity is a pointer to a clone of the actual entity.
https://qcad.org/doc/qcad/3.0/developer ... 79a957c678

var text is an RTextData.
(I prefer to name that var textData vs 'text' for a string)

You changed whole the RTextData.text to a newer value.

One needs to create a new RTextEntity from this.

Code: Select all

var newTextEntity = new RTextEntity(doc, text);    // With text is an RTextData
And cast it back to the document overwriting the existent entity.

Code: Select all

var di = this.getDocumentInterface();
var op = new RModifyObjectsOperation();
op.addObject(newTextEntity, false, true);    // NOTuseCurrentAttributes, DOforceNew
di.applyOperation(op);
Processed entities are all in selection.
Remember changed text entities in an array with push ...
At the end of your code deselect all and select those listed in the array.

Code: Select all

var alteredList = [];
....
.... // your code
alteredList.push(idx[i]);
....
....
di.deselectAll();
di.deselectEntities(alteredList);
Regards,
CVH

martblek
Active Member
Posts: 29
Joined: Wed Sep 02, 2020 7:58 am

Re: how to test text entity

Post by martblek » Thu Feb 11, 2021 11:29 am

Thanks,
Now i know little more :)
a small plus to my zero knowledge of JS.

Text was modified but overlaped old text. so i add delete operation on old TextData and
now seems to work. Dirty solution i know :( but works :)

if I complete this script, my work will be reduced to a few minutes instead days.
thanks again for the explanation.

Code: Select all

var doc = this.getDocument();
var di = this.getDocumentInterface();
var idx = doc.queryAllEntities();
var entity;
var text;

for(var i=0; i<idx.length; i++)
  {
    entity = doc.queryEntity(idx[i]);
    if(isTextEntity(entity))
      {
        text = entity.getData().getPlainText();
        if(text.contains("GA"))
          {
          entity.setText("Pokus");
          EAction.handleUserWarning("found " + text);
          var newEntity = new RTextEntity(doc, entity.getData());
          var op = new RModifyObjectsOperation();
          op.addObject(newEntity, false, true);
          di.applyOperation(op);
          var de = new RDeleteObjectsOperation();
          de.deleteObject(entity);
          di.applyOperation(de);
          }
      }
  }

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

Re: how to test text entity

Post by CVH » Thu Feb 11, 2021 12:08 pm

Then NOT force new:

Code: Select all

op.addObject(newTextEntity, false, false);    // NOTuseCurrentAttributes, NOTforceNew
My bad (copy-paste issue) :oops:

Now you alter the text directly in the RTextEntity.
No need for the getData or to create a new RTextEntity.
That is, if this is all you need to do ...

RModifyObjectsOperation is plural.
One can set this once outside the loop together with di.applyOperation.
This groups operations, is faster and can be undone in one go.

Code: Select all

var doc = this.getDocument();
var di = this.getDocumentInterface();
var idx = doc.queryAllEntities();
var entity;
var text;
var op = new RModifyObjectsOperation();

for(var i=0; i<idx.length; i++) {
    entity = doc.queryEntity(idx[i]);
    if(isTextEntity(entity)) {
        text = entity.getPlainText();
        if(text.contains("GA")) {
            entity.setText("Pokus");
            EAction.handleUserWarning("found " + text);
            op.addObject(entity, false, false);
        }
    }
}
di.applyOperation(op);
CVH

martblek
Active Member
Posts: 29
Joined: Wed Sep 02, 2020 7:58 am

Re: how to test text entity

Post by martblek » Thu Feb 11, 2021 12:32 pm

Thanks again.
Now I'm going to solve the problem with Japanese fonts :(

Post Reply

Return to “QCAD 'How Do I' Questions”