Clone different objects to new Layer

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
ikua
Junior Member
Posts: 15
Joined: Tue Apr 25, 2023 4:07 pm

Clone different objects to new Layer

Post by ikua » Tue Feb 06, 2024 6:26 pm

Hello!

I want to highlight some object on a layer, with another layer pushed in the background. I try now to make a clone form all entities and move them to layer one. Unfourtunatly this only works for polylines until now. I am not able to use "castToShap" for RTextEntities or block. Maybe someone has an hint:

Code: Select all

var appWin = EAction.getMainWindow();
	appWin.handleUserMessage("Neubau mit Farbe hinterlegen")
	var doc = this.getDocument();
	var di = this.getDocumentInterface();
	var op = new RAddObjectsOperation();
    var ids  = doc.queryAllEntities();
	var ids_lays  = doc.queryAllLayers();
	var newItm;
	var sh;
	var itm;
	for (var i=0; i<ids_lays.length; i++) {
		var id_lay = ids_lays[i];
		akt_layer = doc.queryLayer(id_lay);
		if ( akt_layer.getName().indexOf("Neubau ...") != -1 )  { //nur Neubau Layer
			
			var layer_entities =doc.queryLayerEntities(id_lay,true)
			doc.setCurrentLayer("0")
			 var copied_entityee = layer_entities.clone()
			for (var k=0; k<layer_entities.length; k++) {
				appWin.handleUserMessage(akt_layer.getName())


				
				var itm = doc.queryEntity(layer_entities[k]).clone();
				 if (isPolylineEntity(itm)) {
					 appWin.handleUserMessage("Polyline")
					 appWin.handleUserMessage(layer_entities[k].toString())
					sh = itm.castToShape();
					newItm = shapeToEntity(doc, sh.clone());
					appWin.handleUserMessage(isEntity(newItm).toString());
					op.addObject(newItm,false);
				 }else{
					 appWin.handleUserMessage("keine Polyline")
					 appWin.handleUserMessage(layer_entities[k].toString())
					 newItm = doc.queryEntity(layer_entities[k]).clone();
					 // newItm =shapeToEntity(newItm);
					 appWin.handleUserMessage(isEntity(newItm).toString())
					 op.addObject(newItm,false,true);
				 }
				// appWin.handleUserMessage(newItm.toString())
				
				di.applyOperation(op);
				op = new RAddObjectsOperation();
				
				
			}
			
		}
		 
	}
	appWin.handleUserMessage("sdf")

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

Re: Clone different objects to new Layer

Post by andrew » Tue Feb 06, 2024 8:55 pm

We don't need to go convert to a shape as indeed not all entities have a shape representation.

You can simply query the entity, clone it and re-add it to a different layer. I'd also do it in one single operation:

Code: Select all

var appWin = EAction.getMainWindow();
appWin.handleUserMessage("Neubau mit Farbe hinterlegen")
var doc = this.getDocument();
var di = this.getDocumentInterface();
var op = new RAddObjectsOperation();
var ids  = doc.queryAllEntities();
var ids_lays  = doc.queryAllLayers();
var sh;
var itm;
for (var i=0; i<ids_lays.length; i++) {
    var id_lay = ids_lays[i];
    akt_layer = doc.queryLayer(id_lay);
    if ( akt_layer.getName().indexOf("Neubau ...") != -1 )  { //nur Neubau Layer       
        var layer_entities =doc.queryLayerEntities(id_lay,true)
        for (var k=0; k<layer_entities.length; k++) {
            appWin.handleUserMessage(akt_layer.getName())
            var itm = doc.queryEntity(layer_entities[k]).clone();
            itm.setLayerName("0");
            op.addObject(itm,false,true);
        }
    }
}
di.applyOperation(op);

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

Re: Clone different objects to new Layer

Post by CVH » Wed Feb 07, 2024 5:46 am

Hi,
I have my reserves about:
ikua wrote:
Tue Feb 06, 2024 6:26 pm
I want to highlight some object on a layer, with another layer pushed in the background. I try now to make a clone form all entities and move them to layer one.
First, Layers are meant to group things that should display alike, print alike.
If you want that more clearer, highlighted, then common practice is to adapt the layer(s) attributes.

Second, there is no layer in the background, no drawing order based on layers.
Don't look at CAD layers as stacked planes to draw on, they are listed in alphanumerical order.

The drawing order is a property defined per entity.
Newly created entities always receive a drawing order one higher as what already exist.
Meaning that they end up in the foreground Instead of the background.

Remark that in both code sections all the intended entities are duplicated on the standard layer '0'.
Including entities intended for Blocks.
Wondering for what var ids = doc.queryAllEntities(); is used.

Regards,
CVH

ikua
Junior Member
Posts: 15
Joined: Tue Apr 25, 2023 4:07 pm

Re: Clone different objects to new Layer

Post by ikua » Wed Feb 07, 2024 8:53 am

How the hack!!!
Thanks @Andrew. Maybe i was in a code blurry. Tried out so many things, nothings seemed to work. Thanks for the hint.

@CVH:
The wohle reason is, i am developing tools to create PID schemes in a comfortable way. We are working often with existing structures and implement new parts. So i have layers with sublayer for the old as for the new part (sulayer such as "water", "gas", "sludge" ...). The layer have different colors (e.g. water blue). Now i wanted to hightlight the new parts but still keep the information (color) of the substance. So i clone all entities to a new layer, give them drawOrder one. This layer has a big linewight so i can different the old from the new easily. See foto.
and on more: var ids = doc.queryAllEntities(); isnt used. It was a copy of fragment copy paste.

Other question:
like I said, i am developing tools for pids (https://en.wikipedia.org/wiki/Piping_an ... on_diagram). You can see with my questions until now, that my knowledge of coding is quite limited. Nevertheless i think my tools are quite handy (automatic Pipe naming in the scheme, get a list of all used components togehter with the layer, hightlighting new parts :-)). I think i will go on working on that. How is the best way to share that tools, and to involve other people? Github??

greets and thanks for your fast replies
Attachments
Screenshot 2024-02-07 084642.jpg
Screenshot 2024-02-07 084642.jpg (46.66 KiB) Viewed 869 times

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

Re: Clone different objects to new Layer

Post by CVH » Wed Feb 07, 2024 9:42 am

ikua wrote:
Wed Feb 07, 2024 8:53 am
Now i wanted to hightlight the new parts but still keep the information (color) of the substance. So i clone all entities to a new layer, give them drawOrder one. This layer has a big linewight so i can different the old from the new easily.
This is clearer, the code snippets did not alter the order ... :wink:
Remark that the drawing order is zero based and negative values can exist in the current session.
A recent post on this: https://www.qcad.org/rsforum/viewtopic. ... 506#p43496

Your code does not work for TTF based text, you might want to consider the usage of CXF based because that is line-art.

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”