[Solved] Combine Multiple Transactions Into One

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
333onlyhalfevil
Full Member
Posts: 92
Joined: Fri Apr 28, 2023 12:39 pm

[Solved] Combine Multiple Transactions Into One

Post by 333onlyhalfevil » Wed Dec 18, 2024 6:41 am

Hello everyone:

Is there any way to combine multiple document transactions into one? I have a script that is (1) creating a block, (2) adding objects to the block, and (3) modifying the block to add in some custom properties. The script works but the problem is that I have to hit the back button 3x in order to undo everything. Is there any way to combine these three transactions into a single transaction so all 3 happen in just one operation?

Thank you very much for your help.
Last edited by 333onlyhalfevil on Sat Jan 11, 2025 9:10 pm, edited 1 time in total.

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

Re: Combine Multiple Transactions Into One

Post by CVH » Wed Dec 18, 2024 7:43 am

Hi,

Yes, with a transaction group but there are counter indications.

- Layers must exists before you can cast entities on them.
- An entity or a Block must have an id before you can re-query it.
- ....

Sometimes there is no way around.
For example: Adding a layer requires 2 steps to undo.
- Undo set current
- Undo creation

2, 3 ... or much more steps to Undo (OO) the last GUI action is not at all strange. :wink:

If it is an option ...
It is probably more interesting to complete all actions on the virtual objects before casting them.
The same advice as in this topic

Code: Select all

    // Start a new transaction group
    doc.startTransactionGroup();

   // First operation
    var op = new RAddObjectsOperation();
    op.setTransactionGroup(doc.getTransactionGroup());
    ...
    op.addObject(...);
    op.addObject(...);
    di.applyOperation(op);

    // Second operation
    op = new RAddObjectsOperation();
    op.setTransactionGroup(doc.getTransactionGroup());
    ...
    op.addObject(...);
    di.applyOperation(op);
Or automatically within a limited section:

Code: Select all

        doc.startTransactionGroup();

        // Add all operations to the current transaction group:
        doc.setAutoTransactionGroup(true);
        ....
        ..
        .
        doc.setAutoTransactionGroup(false);
Regards,
CVH

333onlyhalfevil
Full Member
Posts: 92
Joined: Fri Apr 28, 2023 12:39 pm

Re: Combine Multiple Transactions Into One

Post by 333onlyhalfevil » Wed Jan 08, 2025 2:38 am

This seems to work as expected. Thank you.

333onlyhalfevil
Full Member
Posts: 92
Joined: Fri Apr 28, 2023 12:39 pm

Re: Combine Multiple Transactions Into One

Post by 333onlyhalfevil » Wed Jan 08, 2025 3:55 pm

How would you recommend I force the start of a new transaction group?

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

Re: Combine Multiple Transactions Into One

Post by CVH » Wed Jan 08, 2025 6:56 pm

333onlyhalfevil wrote:
Wed Jan 08, 2025 3:55 pm
How would you recommend I force the start of a new transaction group?
Never really thought about it further ...

Neither the reference nor existing usage makes any mention of:
- That doc.startTransactionGroup(); starts a new group.
- What ends it.

The only refernece to new is this answer by Andrew.

I suspect it is as simply as starting a 'new' transaction group.
But then the former group becomes obsolete in the meaning of out of the picture or finished I think.

Regards,
CVH

333onlyhalfevil
Full Member
Posts: 92
Joined: Fri Apr 28, 2023 12:39 pm

Re: Combine Multiple Transactions Into One

Post by 333onlyhalfevil » Thu Jan 09, 2025 1:08 am

That seems like it might be the case in general. All but one of my scripts are registering as separate transaction groups using doc.startTransactionGroup() at the beginning. The one that is giving problems is a sole RModifyObjectsOperation() that gets grouped together with the previous transaction group even though it has the doc.startTransactionGroup() at the beginning.

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

Re: Combine Multiple Transactions Into One

Post by CVH » Thu Jan 09, 2025 5:09 am

333onlyhalfevil wrote:
Thu Jan 09, 2025 1:08 am
All but one of my scripts ... The one that is giving problems
With "all" and "one" I have too little info to help you further.
Perhaps you could contact me per PM and share the scripts and required resources like example files if that is necessary.

Everything will be treated with utmost confidentiality.
Any copy will be deleted afterwards.
What you attach at a PM (or any topic) can be deleted at your side.

Regards,
CVH

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

Re: Combine Multiple Transactions Into One

Post by CVH » Thu Jan 09, 2025 8:12 am

Tinkering further ...

I expect that the explicit method with op.setTransactionGroup(doc.getTransactionGroup()); uses the current group.

From the reference: Calling startTransactionGroup(); does nothing more than increasing the lastTransactionGroup counter.
And getTransactionGroup() returns that counter value. (A few lines lower)

Then it might be the order of thing, or simply some points and commas ...
Or don't mix the explicit and the auto assignment method.
Also, don't forget to stop the auto assignment method.
Probably mandatory before you start a new group ... With or without auto assignment.

In the light of that:
What does the following do:

Code: Select all

// Ifso, stop auto grouping:
if (doc.getAutoTransactionGroup()) {
    doc.setAutoTransactionGroup (false);
}

// Start a new transaction group
doc.startTransactionGroup();

// Problematic operation
var op = new RModifyObjectsOperation();
op.setTransactionGroup(doc.getTransactionGroup());
...
di.applyOperation(op);

// Back to other script;
return;
I also think that you can't stack it ....
  • Code A with group 1
    Code A calls Code B and there you want it to be group 2
    Back to A, still using group 1
Regards,
CVH

333onlyhalfevil
Full Member
Posts: 92
Joined: Fri Apr 28, 2023 12:39 pm

Re: Combine Multiple Transactions Into One

Post by 333onlyhalfevil » Sat Jan 11, 2025 9:09 pm

The code in the code block doesn't change anything.
CVH wrote:
Thu Jan 09, 2025 8:12 am
I also think that you can't stack it ....
  • Code A with group 1
    Code A calls Code B and there you want it to be group 2
    Back to A, still using group 1
This seems to be the case. I can get it to be a separate transaction by simply adding in a random entity with an RAddObjectsOperation.

Anyways, thank you very much for your help. I'll mark this one as solved.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”