Page 1 of 1

Remove last Object from RAddObjectsOperation

Posted: Sat Nov 02, 2024 1:33 am
by ikua
Hello!

In my script i am creating a blockreference. Afterwards i have do to some geometrical checks. If they fail I want to remove it. But as i have not applied the transaction I can not use the delete command. Furthermore i have to do the operation many times and to add with the transaction and then delete with transaction would make the script slow.

So here a the code part:

Code: Select all

var di = this.getDocumentInterface();
var op = new RAddObjectsOperation();

op.addObject(blockRef)
.... to some testing
if (test fails) {
here i would like to "undo" the addOject of the BlockRef
}else{
some other stuff happens here
}
di.applyOperation(op);
I suppose my wording is not the right one, but thats is the level of my understanding until now.

thanks and greets

Re: Remove last Object from RAddObjectsOperation

Posted: Sat Nov 02, 2024 8:47 am
by CVH
Hi,

I think you have two options.

The most clever and straightforward one:
:arrow: Do the test before adding the object.
It is not required to add an object to an operation before one can preform some sort of testing.
Even when added to an operation, it will not yet be a drawing object than can be queried back for a test.
Everything you can do with an RBlockReferenceEntity queried from the document can also be done before the operation.
Apart from that the queried object has a uniquely defined id.

Undo means undo the operation, essentially removing the drawing entity, setting it undone, but not removing the operation.
An operation in the operations stack can still be redone and undone again.

With more objects, for example to compare, collect them in an array:

Code: Select all

    blockRefs = [];
    ...
    // Creation of Block References:
    ....
    // Collecting:
    blockRefs.push(blockRef);
Preform the test(s) afterwards on the items in the array, keeping those that comply
One could set the array item to undefined when failed:

Code: Select all

    blockRefs[i] = undefined;
Also avoiding further testing based on not complying objects, undefined items.

Finally only adding the objects that did comply to all testing, are still defined:

Code: Select all

    for (var i=0; i<blockRefs.length, i++)
        if (!isNull(blockRefs[i]) {
            op.addObject(blockRefs[i]);
        }
    }
    di.applyOperation(op);

Another but dirty approach would be to create a clone and:

Code: Select all

    op.deleteObject(clone);
Dirty, because you will loose track of what will eventually make it as drawing entity.
Perhaps impossible because of a conflict between a new and a not yet defined id.
Still a bit uncharted, perhaps you don't need cloning.


Regards,
CVH

Re: Remove last Object from RAddObjectsOperation

Posted: Mon Nov 04, 2024 3:23 pm
by ikua
you have been absolutely right. I could do the testing without making the transaction. Could not see that before.
Thanks again.