Getting handle inside start or end Transaction()?

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
bob
Newbie Member
Posts: 5
Joined: Tue Jun 14, 2022 5:14 pm

Getting handle inside start or end Transaction()?

Post by bob » Wed Jun 15, 2022 7:32 pm

This works and prints out
entities:
riser: 59
line: 60

However if I surround addLine() with start/endTransaction() the line handle is -1. Why is that? Can I not surround these draw functions with that? Should I? How to get handles if you do that?

Bob

Code: Select all

	entities = {};
	closed = true;
	relative = false;
	var obj = addPolyline(points, closed, relative);
	print('polyline handle = ' + obj.getHandle());
	entities['riser'] = obj.getHandle();

	// startTransaction(di);
	obj = addLine(0,0, 50,50);
	// endTransaction()
	print('line handle = ' + obj.getHandle());
	entities['line'] = obj.getHandle();

	print('\nentities:');
	// print(entities);
	for (var key in entities) {
		print(key + ':   ' + entities[key]);
	}

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

Re: Getting handle inside start or end Transaction()?

Post by CVH » Thu Jun 16, 2022 4:30 am

Hi,

Newly added entities or objects do not yet have a valid ID if they were added within a transaction.
(We will look that up in a minute)
They are only created in group at the end of the transaction.
bob wrote:
Wed Jun 15, 2022 7:32 pm
Can I not surround these draw functions with that?
Yes you could, but only useful with a group of actions.
Every action is followed by a screen update and much more than that :wink:
With a grouped transaction just one update instead of one each action.
bob wrote:
Wed Jun 15, 2022 7:32 pm
How to get handles if you do that?
You need to re-query the entity(ies)/object(s).

The simplified API of QCAD is intended to do basic things in an easy way.
I think you may want to get deeper into scripting under QCAD :wink:

A reference of the open source: https://qcad.org/doc/qcad/3.0/developer/annotated.html
Scripts can be found on GitHub: https://github.com/qcad/qcad

About ID=-1 ... simple.js : https://github.com/qcad/qcad/blob/087f4 ... _create.js
- function addPolyline(points, closed, relative) creates a polyline shape called pl and returns addShape(pl).
- function addShape(shape, color, linetype, lineweight) creates a polyline entity for the document and returns addEntity(entity).
In the above we have the first remark on IDs within transactions :| (Also: https://qcad.org/doc/qcad/3.0/developer ... ba88080a01)
- function addEntity(entity) returns addObject(entity) and that will add the object and return a reference to it.

Further mind that the ID and the handle is not the same but you can get the handle by the entity ID at runtime.
There are numerous examples of scripts in the GitHub repository :wink:

Regards,
CVH

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

Re: Getting handle inside start or end Transaction()?

Post by andrew » Thu Jun 16, 2022 10:06 am

To get the handle of the objects added by a transaction, you'd have to re-query the object after adding it or inspect the transaction, for example:

Code: Select all

...
transaction = endTransaction();

var ids = transaction.getAffectedObjects();
for (var i=0; i<ids.length; ++i) {
    var id = ids[i];
    var entity = document.queryEntity(id);
    ...
}

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”