Duplicate layer with all sublayer with the right color

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

Duplicate layer with all sublayer with the right color

Post by ikua » Thu Feb 08, 2024 10:19 am

Hello!

The scirpts copies all sublayers from "oldlayer_name" to a new created "newlayer_name". If somebody needs that some day:

Code: Select all

include("scripts/simple.js"); //für addsimpletext
// get main application window:
var appWin = EAction.getMainWindow();
// appWin.handleUserMessage("Neubau mit Farbe hinterlegen")
var doc = this.getDocument();
var ids_lays  = doc.queryAllLayers();
var oldlayer_name = "Bestand"
var newlayer_name = "Entwurf"
addLayer(newlayer_name)

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(oldlayer_name +" ...") != -1 )  { //nur Neubau 
		appWin.handleUserMessage(akt_layer.getColor().name().toString())
		  addLayer(akt_layer.getName().toString().replace(oldlayer_name,newlayer_name),akt_layer.getColor().name().toString());
    }
} 
Last edited by ikua on Fri Feb 09, 2024 10:11 am, edited 2 times in total.

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

Re: Duplicate layer with all sublayer with the right color

Post by CVH » Thu Feb 08, 2024 10:11 pm

Hi,

I suspect the first line should read Include ("scripts/simple.js");
Not for adding simple text but because the script uses 2 addLayer Instructions

How the addLayer in the for loop should work is a mystery.
akt_layer.getColor() returns an RColor object and I can't see the name() method/attribute listed.
https://qcad.org/doc/qcad/3.0/developer ... color.html

I would rather expect a sequence like .toString().toLowerCase()
First ensure it is a string and then set it to lower case ... :wink:

What with custom colors that have no CAD name or relevant SVG name?

Regards,
CVH

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

Re: Duplicate layer with all sublayer with the right color

Post by ikua » Fri Feb 09, 2024 10:11 am

Thank for the hint with the missing i in the first line and the review.

The rest is also a mistery how it works, but it works, but it seems, that the ".name()" converts the RColor to hex, and that can be used to set the color of the new layer. So it also works with custom colors, because it actually never refers to the color name.

Your approach without the name did not work for me, cause i could not use the RColor as input for the addLayer.

I deleted the "toLowerCase()" cause it was of no use.

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

Re: Duplicate layer with all sublayer with the right color

Post by CVH » Fri Feb 09, 2024 1:01 pm

ikua wrote:
Fri Feb 09, 2024 10:11 am
The rest is also a mistery how it works, but it works, but it seems, that the ".name()" converts the RColor to hex, and that can be used to set the color of the new layer. So it also works with custom colors, because it actually never refers to the color name.
Your approach without the name did not work for me, cause i could not use the RColor as input for the addLayer.
Remark that I did not mentioned any kind of an approach beside swapping .toLowerCase() and .toString() ...
... Fairly straightforward, one needs a string based object before one can convert it to lower case. :wink:

Ok, hex it is, I recently discovered that sometimes it is required to look over the hedge with Qt: Mystery solved! :wink:


Another remark is to simply use EAction.handleUserMessage("...");
https://github.com/qcad/qcad/blob/maste ... n.js#L1900

The hierarchic layer separator is in fact " ... " but the absence of the last space won't matter in this case. :wink:
You don't have to convert the result from .getName() to a string because it always returns a QString ... At least an empty one:
https://qcad.org/doc/qcad/3.0/developer ... a0f00ba811

For now you only copy over the child name with the appropriate naming & color ...
... The simple API adds the defaults "Continuous" Linetype and weight 0.00mm
https://github.com/qcad/qcad/blob/maste ... ate.js#L59
You can already see the major difference in the number of RLayer attributes vs 4 for addLayer.
At some point you might want to consider to leave the simple API for what it is.

But the same remarks that Andrew made with your previous script are valid here ... :
- Query the child layer in question, clone it.
(For the moment this is a variable and not an actual layer object of the drawing)
- Alter the naming of the clone to match the new parent.
- Add the cloned layer object to the drawing as a new object.
This new child layer will be a perfect clone of the child in question, all attributes will be a mirror except the parent name.
- Group your actions to one operation and/or multiple operations to one transaction.
This spares you intermediate screen updates and other related things that all take time and one can undo the action in one go.

Finally ...
Your code assumes that oldlayer_name exits and that adding a new parent newlayer_name is always required.
It also assumes that oldlayer_name is a main level parent name, but .indexOf(...) != -1 only diversify on if it occurs, not on where.
Change that to == 0 or use the String.startsWith(...) function:
https://github.com/qcad/qcad/blob/maste ... y.js#L2166

Regards,
CVH

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

Re: Duplicate layer with all sublayer with the right color

Post by ikua » Wed Feb 21, 2024 9:07 am

Hello CVH!

Thanks for the many points to improve the script (and sorry for late reply, a lot to do the last days). For time reasons i will leave it here, as its is. Like i mentioned i am really low level programmer and for me, the aim is, that it works for me, and not for genereal cases. Nevertheless i am still quite unsure, if i should put my improvised scripts here. The are no example for good coding, but in my experience, I am every time quite happy, if i find some snippets of code i can use to start of.
So is this ok for the forum or should i keep them for myself?
greets

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

Re: Duplicate layer with all sublayer with the right color

Post by CVH » Wed Feb 21, 2024 9:25 am

It's okay for me, but please don't mislead users with partially incorrect claims.
ikua wrote:
Thu Feb 08, 2024 10:19 am
The scirpts copies all sublayers from "oldlayer_name" to a new created "newlayer_name".
It is not a one on one copy.
Not all attributes of the existent sub-layer are preserved in the copy.

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”