Rotation issues

Hier diskutieren die Betatester von PhotoLine untereinander und mit den Entwicklern
User avatar
russellcottrell
Mitglied
Posts: 175
Joined: Sat 26 Jul 2014 10:13

Rotation issues

Post by russellcottrell » Sun 09 Dec 2018 21:39

Hello; I am having trouble after rotating a layer. I am using:

matrix = pl.GetRotationMatrix(90, [0, 0]);
nLayer.MatrixToPage = nLayer.MatrixToPage.Concatenate(matrix);

After rotation, the origin and dimensions of the layer are based on the layer before it was rotated, so resetting the origin puts it in the wrong place, and width and height seem to be reversed. I tried running Fix Layer as an action; it resets the origin, but not the dimensions; and attempting to scale the layer after fixing it crashes PhotoLine. Is there a way to rotate a layer so the rotated layer has the new origin and dimensions?

Martin Huber
Entwickler
Entwickler
Posts: 3673
Joined: Tue 19 Nov 2002 15:49

Re: Rotation issues

Post by Martin Huber » Mon 10 Dec 2018 11:57

russellcottrell wrote:
Sun 09 Dec 2018 21:39
Hello; I am having trouble after rotating a layer. I am using:

matrix = pl.GetRotationMatrix(90, [0, 0]);
nLayer.MatrixToPage = nLayer.MatrixToPage.Concatenate(matrix);

After rotation, the origin and dimensions of the layer are based on the layer before it was rotated, so resetting the origin puts it in the wrong place, and width and height seem to be reversed.
Yes, this just transforms the layer and is similar to entering a rotation angle in the Layer Attributes.
russellcottrell wrote:
Sun 09 Dec 2018 21:39
I tried running Fix Layer as an action; it resets the origin, but not the dimensions;
Fixing it using the UI works fine.
russellcottrell wrote:
Sun 09 Dec 2018 21:39
and attempting to scale the layer after fixing it crashes PhotoLine.
Can you send us a sample script?
russellcottrell wrote:
Sun 09 Dec 2018 21:39
Is there a way to rotate a layer so the rotated layer has the new origin and dimensions?
Your way should work, but I will add a "Rotate" operation.

Martin

User avatar
russellcottrell
Mitglied
Posts: 175
Joined: Sat 26 Jul 2014 10:13

Re: Rotation issues

Post by russellcottrell » Mon 10 Dec 2018 15:41

Code: Select all

var pl = new ActiveXObject("PhotoLine.Application");
var doc = pl.ActiveDocument;

var nLayer = new ActiveXObject("PhotoLine.Image");
nLayer.InitPicture(1, [50, 100], [1, 0, 0]);
nLayer.Origin = [0, 0];
doc.RootLayer.Insert(nLayer, -1);
doc.ActiveLayer = nLayer;

var matrix = pl.GetRotationMatrix(90, [0, 0]);
nLayer.MatrixToPage = nLayer.MatrixToPage.Concatenate(matrix);

var fixLayer = "UGhvdG8gTGluZSBBY3Rpb25zAAAAAQAAAAABAP+bAAAAD0ZpeExheWVyQWN0aW9uAAIBAAAAAQAA" +
  "AAQAAAAB/////w==";
nLayer.DoOperation("Action", "Data", fixLayer);

nLayer.Origin = [10, 10]; // <== this happens BEFORE fix layer!

//nLayer.DoOperation("Scale", "Mode", 3, "ValueX", 50, "Interpolation", 1); // <== crashes PhotoLine

var nLayerSize = VBArray(nLayer.Size).toArray();
var objShell = new ActiveXObject("WScript.Shell");
objShell.Popup(nLayerSize[0]); // <== this is 50 even though it says 100 in the layer panel

Martin Huber
Entwickler
Entwickler
Posts: 3673
Joined: Tue 19 Nov 2002 15:49

Re: Rotation issues

Post by Martin Huber » Mon 10 Dec 2018 17:12

russellcottrell wrote:
Mon 10 Dec 2018 15:41

Code: Select all

(...)
var fixLayer = "UGhvdG8gTGluZSBBY3Rpb25zAAAAAQAAAAABAP+bAAAAD0ZpeExheWVyQWN0aW9uAAIBAAAAAQAA" +
  "AAQAAAAB/////w==";
nLayer.DoOperation("Action", "Data", fixLayer);
(...)
That's the problem. The action will create a fixed layer and replace nLayer by that. After that, nLayer is no longer part of doc.
Adding

Code: Select all

nLayer = doc.ActiveLayer;
will fix that.

I will modify DoOperation() so that it returns a newly created layer if there is one, or the original layer if not.

This way, you will be able to write:

Code: Select all

nLayer = nLayer.DoOperation("Action", "Data", fixLayer);
Setting nLayer as the active layer at the beginning will then be superfluous, too.

Martin

User avatar
russellcottrell
Mitglied
Posts: 175
Joined: Sat 26 Jul 2014 10:13

Re: Rotation issues

Post by russellcottrell » Mon 10 Dec 2018 20:56

Thank you very much for the support.
I would have to say that at this point, the impermanence of layer objects is the biggest scripting hurdle. I was thinking of making a jagged array representing all the layers, children, etc. and keeping track of named layer objects by assigning them a series of numbers like 2,1,2 for their path through the tree; but keeping up with it during layer creation, merges, etc. would be quite daunting. I don’t know if there is some other way of hanging on to layer objects like this.

Martin Huber
Entwickler
Entwickler
Posts: 3673
Joined: Tue 19 Nov 2002 15:49

Re: Rotation issues

Post by Martin Huber » Wed 12 Dec 2018 11:03

russellcottrell wrote:
Mon 10 Dec 2018 20:56
I would have to say that at this point, the impermanence of layer objects is the biggest scripting hurdle.
I understand that this is a problem, but there is no way around. In its core COM is strictly typed, so if a command changes the type of a layer, the object has to change, too.
russellcottrell wrote:
Mon 10 Dec 2018 20:56
I was thinking of making a jagged array representing all the layers, children, etc. and keeping track of named layer objects by assigning them a series of numbers like 2,1,2 for their path through the tree; but keeping up with it during layer creation, merges, etc. would be quite daunting. I don’t know if there is some other way of hanging on to layer objects like this.
I don't fully understand what you want to achieve, but a jagged array is hard to update after inserting/deleting a layer.
It's probably easier to just keep an array of object references and update that after converting a layer. Deleted layers are easily identified by checking their page. NULL means deleted.

Martin

User avatar
russellcottrell
Mitglied
Posts: 175
Joined: Sat 26 Jul 2014 10:13

Re: Rotation issues

Post by russellcottrell » Wed 12 Dec 2018 20:34

Saves and restores layer objects before and after operations such as resize that remove them from the tree:

Code: Select all

var pl = new ActiveXObject("PhotoLine.Application");
var doc = pl.ActiveDocument;
var docSize = VBArray(doc.Size).toArray();

var layerObjects = [];

var aLayer = doc.ActiveLayer; // select a layer with a parent
layerObjects.push(["aLayer", null]); // need to save the layer by its variable name

aLayer.Parent.Name = "Hello";

var bgLayer = doc.RootLayer.First;
layerObjects.push(["bgLayer", null]);

saveLayerObjects(); // need to do these before and after any operation that removes layer objects from the tree
doc.DoOperation("Resize", "Size", [docSize[0] + 100, docSize[1] + 100], "Horizontal", 2, "Vertical", 2);
restoreLayerObjects();

doc.ActiveLayer = bgLayer;

var objShell = new ActiveXObject("WScript.Shell");
objShell.Popup(aLayer.Parent.Name);



function saveLayerObjects() {
for (var i=0; i<layerObjects.length; i++) {

  var layer = eval(layerObjects[i][0]);

  if (layer.Page) { // layer still exists
    var pArray = [];
    while (layer != doc.RootLayer) {
      pArray.unshift(layer.Position); // builds the position of the layer in the tree
      layer = layer.Parent;
    }
    layerObjects[i][1] = pArray;
  }

  else
    layerObjects.splice(i, 1); // have to remove it if it does not exist

}
}



function restoreLayerObjects() {
for (var i=0; i<layerObjects.length; i++) {

  var layer = doc.RootLayer;
  var pArray = layerObjects[i][1];

  for (var j=0; j<pArray.length; j++) {
    layer = layer.Item(pArray[j]); // walk through the tree to get the layer
    if (!layer) // just in case
      break;
  }

  if (layer)
    eval(layerObjects[i][0] + " = layer"); // reassign the original layer object
  else
    layerObjects.splice(i, 1);

}
}
[Edited]
Last edited by russellcottrell on Fri 14 Dec 2018 18:16, edited 2 times in total.

Martin Huber
Entwickler
Entwickler
Posts: 3673
Joined: Tue 19 Nov 2002 15:49

Re: Rotation issues

Post by Martin Huber » Thu 13 Dec 2018 16:57

In saveLayerObjects(), the "layer still exists"-condition is "layer.Page", because you could delete the parent (or any other ancestor) of a layer and thus implicitly delete the layer itself. In that situation it still has a parent, but no page.
And restoreLayerObjects() should have a check whether layer is becoming NULL in the for loop.

I don't fully understand why you need that, though. There is no such code in PhotoLine.

Martin

User avatar
russellcottrell
Mitglied
Posts: 175
Joined: Sat 26 Jul 2014 10:13

Re: Rotation issues

Post by russellcottrell » Thu 13 Dec 2018 22:16

That is a good tip about layer.Page. I did not check for null in restoreLayerObjects() because it is intended to be run after saveLayerObjects(), and everything should still be there. The only thing intervening should be resize, rotate, fix, etc. that removes all layers from the tree.
I don't fully understand why you need that, though. There is no such code in PhotoLine.
I don't understand what you are referring to . . . .