[Solved]Permanent Changes to Entity

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
Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

[Solved]Permanent Changes to Entity

Post by Ghost_of_Magellan » Thu May 05, 2016 10:50 am

Greetings.

Following the advice on the topic Listing Properties, I made the following piecec of code:
for (i = 0; i < entityIds.length; i++) {
			entity = doc.queryEntity(entityIds);
			Entity_ID = entity.getId();
			Entity_ID = Entity_ID.toString();
			
			for (j = 0; j < entityIds.length; j++)	{
					table_ID = widgets["Obj_table"].item(j, 0);
					table_ID = table_ID.text();
					//Quando encontrar, só altera draworder. (Não é uma questão de 'Se' porque já foi feito refresh)
					if (Entity_ID == table_ID){
						entity.setDrawOrder(j);
						break;	//Breaks out of loop
					}
				}
			//Adiciona parametros à entidade
			vl_ModEntities.addObject(entity, false);	//False = force new parametres
			var draw = entity.getDrawOrder();
			draw = draw.toString();
			appWin.handleUserMessage(draw);
		}


The code succeeds in altering the draw order according to the order of a list i manipulated, however the changfes are not permanent. If I close, and reopen the file, the program will automatically reorder draworder and even change the entity ID of the entities, leaving its drawing order in the original order.

Here is what I did:
I created two lines, shaping a corner, then copy pasted it two times. as before, the order went to hell, but after a refresh (that used the code above to sort it out) and a reordering of the table to an order or my choosing, it was all OK.

I saved, Reopened it and the draworder had been reworked as well as the entity Ids. The printscreens in anex show the process:
QCadDraw1.jpg
1st Refresh
QCadDraw1.jpg (87.27 KiB) Viewed 7754 times
1st Refresh
QCadDraw2.jpg
Ordered table and refresh
QCadDraw2.jpg (87.22 KiB) Viewed 7754 times
Ordered table and Refresh. (Note the ID of the first entity selected)
QCadDraw3.jpg
Close, and reopen
QCadDraw3.jpg (86.89 KiB) Viewed 7754 times
Close and Reorder (and note the Id of the same entity as before)

Is there a way to make these changes permanent?

Thank you.
Last edited by Ghost_of_Magellan on Mon May 09, 2016 10:26 am, edited 1 time in total.

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

Re: Permanent Changes to Entity

Post by andrew » Thu May 05, 2016 6:48 pm

Object IDs are not persistent between saves. I.e. an entity with ID 100 can have a different ID after saving / reloading.

If you need a way to identify entities or other objects between saves, you can use object handles instead (getHandle()).

The drawing orders should be persistent (although the exact values aren't). The drawing order is actually nothing else than the order in which entities are stored in the file. Let't say you have an entity with drawing order 1000 and another one with drawing order 2000, after a save and reload, the drawing orders will be 0 and 1 for the orders in which the entities were stored in the file. This might sound strange, but the DXF / DWG formats simply support no other way to define the drawing order.

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Permanent Changes to Entity

Post by Ghost_of_Magellan » Fri May 06, 2016 1:19 pm

If you need a way to identify entities or other objects between saves, you can use object handles instead (getHandle()).
No, I don't believe I do need it, I just mentioned it because it might have been relevant for the discussion.
The drawing orders should be persistent
If they should, then I'm doing something wrong.

I made a simple 3 entity design on QCad and Repeated the procedure I did before.

On this code:
doc = EAction.getDocument();
entityIds = doc.queryAllVisibleEntities();
entityIds = doc.getStorage().orderBackToFront(entityIds);
vl_ModEntities = new RModifyObjectsOperation();

for (i = 0; i < entityIds.length; i++) {
			entity = doc.queryEntity(entityIds);
			Entity_ID = entity.getId();
			Entity_ID = Entity_ID.toString();
			
			for (j = 0; j < entityIds.length; j++)	{
					table_ID = widgets["Obj_table"].item(j, 0);
					table_ID = table_ID.text();
					
					if (Entity_ID == table_ID){
						entity.setDrawOrder(j);
						break;	//Breaks out of loop
					}
				}
			vl_ModEntities.addObject(entity, false);	//False = force new parametres
			var draw = entity.getDrawOrder();
			draw = draw.toString();
			appWin.handleUserMessage(draw);
		}

This piece of code happens whenever I refresh.

Procedure:
Step One: The first time I refresh I have the following draworder for each entity: 0, 1, 2.
Makes sense.
Step Two: Then I invert the order of the entities on my table and refresh again: 2, 1, 0.
In theory this is what I want, however, the changes are not saved when I close the document and reopen it.
Step Three: With a test button I created with the code below, I got the following order: 0, 1, 2
The entity basically told me and my new ordering to go hang ourselves.

widgets["cmdExit"].clicked.connect(function () {
	for (i = 0; i < entityIds.length; i++) {
		entity = doc.queryEntity(entityIds);
		var draw = entity.getDrawOrder();
		draw = draw.toString();
		appWin.handleUserMessage(draw);
	}
});


Either I am making some sort of mistake on the first code that is printing the order wrong or I have made some blunder on the addObject part that isn't doing what it should.
Any ideas?

Thank you.

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

Re: Permanent Changes to Entity

Post by andrew » Fri May 06, 2016 1:50 pm

You can easily verify if entity draw orders are persistent in QCAD as follows:
- Draw some entities
- Change the drawing order using the property editor
- Save and Load again or use File > Reload

If this does not work for you, that would be a bug (although it seems to work fine here).

If the test above does work for you, please verify the drawing order with the property editor after setting it using your code.
The drawing order shown in the property editor is the drawing order used when saving.

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Permanent Changes to Entity

Post by Ghost_of_Magellan » Fri May 06, 2016 6:37 pm

Writting on Draw order of the Property Editor worked.

However, though I can introduce an order, I cannot see it.
Picture below.
QCadEdit.jpg
QCadEdit.jpg (13.59 KiB) Viewed 7723 times
In this case, what is the function of the handle? Is it relevant to this matter?

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Permanent Changes to Entity

Post by Ghost_of_Magellan » Fri May 06, 2016 6:58 pm

I might be biting more than I can chew, but since I'm not sure you'll be available on weekends, I went poking about the code for the property editor and found what I think is the portion that receives what's on the label of Property editor and saves it onto whatever variable/database will serve as a save template.
function PropertyWatcher(propertyEditor, sender, propertyType) {
    this.propertyEditor = propertyEditor;
    this.sender = sender;
    this.propertyType = propertyType;
}

var drawOrderEdit = this.widget.findChild("DrawOrder");
    drawOrderEdit.editingFinished.connect(
                new PropertyWatcher(this, drawOrderEdit, REntity.PropertyDrawOrder),
                'propertyChanged');

Just tell me if I'm not on the right track.

Thank you.

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

Re: Permanent Changes to Entity

Post by andrew » Fri May 06, 2016 7:22 pm

The property editor is far more complex than what you are trying to do.
Using setDrawOrder was fine. If you are looking for a working example, have a look at Modify > Bring to Front:

https://github.com/qcad/qcad/blob/maste ... ToFront.js

The three steps are:
- query the entity from the document
- set the draw order (setDrawOrder)
- add the modified entity to an operation
- apply the operation

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

Re: Permanent Changes to Entity

Post by andrew » Fri May 06, 2016 7:26 pm

Ghost_of_Magellan wrote:However, though I can introduce an order, I cannot see it.
Picture below.
QCadEdit.jpg
That does not look right. Every entity has a draw order which should be displayed when the entity is selected. Same for linetype scale. What version of QCAD are you using? Can you try this with a clean QCAD installation (unmodified)?
In this case, what is the function of the handle? Is it relevant to this matter?
The handle of the selected entity is displayed for your information. If you are working with handles, the handle might come in handy (no pun intended).

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Permanent Changes to Entity

Post by Ghost_of_Magellan » Mon May 09, 2016 9:56 am

The version I had was 3.12.4 for win32

I downloaded the latest version, 3.14.3 but, though the draw order now is showing on the property editor (thank you for that), the code still does not appear to be permanent.

Again, after the refresh is done and indicates the order I want, another query and print and the property editor indicates that the order has not been changed.
doc = EAction.getDocument();
docInterface = EAction.getDocumentInterface();
vl_ModEntities = new RModifyObjectsOperation();
entityIds = doc.queryAllVisibleEntities();
entityIds = doc.getStorage().orderBackToFront(entityIds);

for (i = 0; i < entityIds.length; i++) {
			entity = doc.queryEntity(entityIds);
			Entity_ID = entity.getId();
			Entity_ID = Entity_ID.toString();
			
for (j = 0; j < entityIds.length; j++){
					table_ID = widgets["Obj_table"].item(j, 0);
					table_ID = table_ID.text();
					
					if (Entity_ID == table_ID){
						entity.setDrawOrder(j);
						vl_ModEntities.addObject(entity, false);	//False = force new parametres
						break;	//Breaks out of loop
					}
				}
			//Indicates on the fly what change has been done to entity
			var draw = entity.getDrawOrder();
			draw = draw.toString();
			appWin.handleUserMessage(draw);
}
docInterface.applyOperation(vl_ModEntities);

//Indicates what order entities are on demand.
function funCmdTeste(x) {
	var appWin = EAction.getMainWindow();
	if (x==1){
		appWin.handleUserMessage("Fun successful");
	}
	else{
		appWin.handleUserMessage("Fun not successful");
	}
}


Results are the same.
1st refresh (unmodified table): 0,1,2
2nd refresh (modified modified table): 2,1,0
press button to check order: 0,1,2 (when it should be 0,1,2)

I am stumped. I have absolutely no idea why this isn't working.

Thank you.

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Permanent Changes to Entity

Post by Ghost_of_Magellan » Mon May 09, 2016 10:26 am

For some reason, It seems it's working now.

I did made changes, minor ones, but more out of desperation than intelect.
At this point I'm not keen on complaining, though.

Thank you, andrew.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”