[Solved] Setting PropertyLayer on Library Item script

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
Taygete
Full Member
Posts: 50
Joined: Wed May 14, 2014 8:53 am

[Solved] Setting PropertyLayer on Library Item script

Post by Taygete » Sun May 25, 2014 11:41 am

Hi,

As a test for later development I have modified the Library Item Example script "MyRectangle.js" so instead of drawing line entities it draws a rectangular polyline. It draws the polyline fine but always into the default layer "0".

As a test I created a new layer called "A" and gave it a colour "Yellow".

In code on my script I set the current layer to "A" then added custom properties to the polyline and also set the PropertyLayer to "A", however it always draws it into layer "0". I can select the polyline layer via the property editor but I wanted to do this in code.

I have also noticed (as a sanity test for myself), if I select either layer "0" or "A" before dropping the MyRectangle item from the library browser, and in code get the current layer it is always the same id (21).

I have pasted the relevant code below:
TypeA.getOperation = function(di, width, height) {
    // the dots:
	var va = new Array(new RVector(0, 0), new RVector(0, height),
			new RVector(width, height), new RVector(width, 0));	

    // the dot connection pairs:
    //
    //    1 +----------------+ 2
    //      |                |
    //      |                |
    //    0 +----------------+ 3
    //
//    var linePairs = [[0,1], [1,2], [2,3], [3,0], [1,3], [0,2]];
//    var linePairs = [[0,1], [1,2], [2,3], [3,0]];
    // connect the dots:

    // Create the polyline shape
    var polyline = new RPolylineData();
    for (var p = 0; p < va.length; p++) {
       polyline.appendVertex(va[p]);
    }

    // Make it closed
    polyline.setClosed(true);

    // id is always the same id whichever layer I select before dropping the script onto the drawing
    var id = di.getDocument().getCurrentLayerId();

    // Select Layer "A"
    di.getDocument().setCurrentLayer("A");

//    id = di.getDocument().getCurrentLayerId();

    // Create a polylineEntity so we can add custom properties to them
    var polylineEntity = new RPolylineEntity(di.getDocument(), polyline)

    // Set some test custom properties
    polylineEntity.setCustomProperty("My Property Test", "Name", "Bob");
    polylineEntity.setCustomProperty("My Property Test", "Length", "100");

    // Place it in layer "A" this returns true
    polylineEntity.setProperty(RPolylineEntity.PropertyLayer, "A");

    // Add the operation and return
    var addOperation = new RAddObjectOperation(polylineEntity);

    return addOperation;
}
I know there are different ways of creating the polyline which seem to work but the above code is the one I ended up using.

I Hope someone can shed some light on this, I am only just starting out with scripting but have years of C++ etc experience.

Thanks,

Andrew.
Last edited by Taygete on Tue May 27, 2014 8:21 am, edited 1 time in total.

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

Re: Setting PropertyLayer on Library Item script

Post by andrew » Mon May 26, 2014 8:36 am

Your script operates on a temporary, empty document which is pasted into the actual drawing after your script has completed its operation.

The reason for this design is to avoid that a script item could accidentally destroy or otherwise unintentionally modify the current drawing.

Your script item can create new layers in the current drawing by adding layers to the script item's operation. The new layers will then be created in the temporary document. The user has the option to overwrite existing layers or keep the existing layers and ignore the attributes of the layers your item creates if a layer already exists.

You could use this feature to create items on the current layer:
- Query the current layer of the current drawing (not the temporary document):
var layer = EAction.getDocument().queryCurrentLayer();
- Add a new layer with the same attributes and name in the temporary document and place your items on that layer.

Taygete
Full Member
Posts: 50
Joined: Wed May 14, 2014 8:53 am

Re: Setting PropertyLayer on Library Item script

Post by Taygete » Mon May 26, 2014 4:45 pm

Hi Andrew,

Thanks for the explanation, however I am still having issues implementing it and must be doing something wrong.

I create a new RAddObjectsOperation

Code: Select all

var addOperation = new RAddObjectsOperation(false);
I now get the current layer from the current drawing using

Code: Select all

var layer = EAction.getDocument().queryCurrentLayer();
I then create a new layer and set the attributes to be the same as layer above.

If I add the layer to the operation at this point like below I get an error message "Transaction failed. Please check for block recursions and locked or invisible layers or blocks."

Code: Select all

addOperation.addObject(tempLayer);
I then set the currentlayer to be the tempLayer just created

Code: Select all

di.getDocument().setCurrentLayer(tempLayer.getName());
I then create the polyline and assign the properties as per the original email.

I then add the polyline to the operation

Code: Select all

addOperation.addObject(polylineEntity);
Then return the operation

Code: Select all

return addOperation;
However, apart from the warning mentioned above, the polyline stayes in layer 0 until I move it in the property editor.

I also tried selecting "Overwrite Layers" in the toolbar when dropping the script onto the drawing but this doesn't seem to make a difference.

As I say I am most likely doing something silly in the script.

Thanks for any pointers and help.

Andrew.

Taygete
Full Member
Posts: 50
Joined: Wed May 14, 2014 8:53 am

Re: Setting PropertyLayer on Library Item script

Post by Taygete » Tue May 27, 2014 8:20 am

Hi Andrew,

Please ignore my last email, I now have it working.

I am not exactly sure why my first attempt wasn't working, maybe it was how I was creating the RLayer.

Anyway I found some code you had used in this post (#8) viewtopic.php?f=30&t=1927&hilit=export which helped me solve the issue.

Thanks,

Andrew.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”