Retrieving the individual SelectionRect of a set of objects

Moderator: Kathy_9

Post Reply
ilgk48
Posts: 60
Joined: Sun Apr 28, 2019 12:26 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PaintShop PRO 2019 32bit

Retrieving the individual SelectionRect of a set of objects

Post by ilgk48 »

Good evening,
please, see this figure:
Immagine 2021-02-18 163459.png
The red lines are Path objects in a Vector layer.
I wish to loop through of them for retrieving the respective SelectionRect.
I'm able to select them all, and loop through using the result of the command ReturnVectorObjectProperties.
But how to select the first one and then use the GetNextObject command in sequence?
Otherwise, without using GetNextObject, 'm trying to inspect the path (nodes) of each object in the hope of calculating the SelectionRect using the first and the last node.
I see that a path has the format:

u'New Path'
[ ( 0 , ( 847.5 , 263.5 ) ) , ( 1 , ( 2136.5 , 897.5 ) ) ]


That is what I expect: every object should have only two nodes: 0 and 1)

But also this format is present:

u'New Path'
[ ( 0 , ( 252.5 , 2202.5 ) ) ,
( 2 , ( 252.5 , 2202.5 ) , ( 1935.5 , 2646.5 ) , ( 1930.5 , 2641.5 ) ) ]


And I do not expect it! ... Crash :-)

Thank for your hints.
:-)
LeviFiction
Advisor
Posts: 5983
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8, 9, X, XI, X2, X8,X9, 2018, 2019
Location: USA

Re: Retrieving the individual SelectionRect of a set of objects

Post by LeviFiction »

0 and 1 are not indexes they are commands. PSP more or less follows the SVG standard 0 = MoveTo (used to move to a starting point without drawing), 1 = LineTo (move from current position to new position). 2 = CurveTo (Cubic), 3= Close Path.

If you're seeing a 2 it's because it was accidentally drawn with the curve tool and dragged out ever so slightly. The first two (x,y) points are the control handles for the curve. On the positive side, in both cases the last coordinate tuple is the ending point. So grabbing the -1 value should always get the end point.

Code: Select all

   # Pseudo-code I haven't tested this but it works in my head.

    StartPoint = PathData[0][-1]
    EndPoint = PathData[1][-1]  #assumes two points exist, but doesn't care what kind of movement is used.  Could use just two unconnected nodes so long as they were part of the same path object.
    Rect = (StartPoint, EndPoint[0]-StartPoint[0], EndPoint[1] - StartPoint[1])
You could also just use the rectangle pre-set tool and use the Rect data directly rather than working it out. It should be just as fast to drag out a rectangle as it is to select two points for a line.

The easiest option for getting all objects is to just manually select them all in the layers palette (select the bottom one, SHIFT + click on the top one to select them all) then use ReturnVectorObjectProperties and the ['ListOfObjects'] property will have all of the objects you selected.

GetNextObject is supposed to get the first object of a vector layer if no object is selected, and then go through the top level objects until the layer no longer has any children to go through. It does not go into vector object groups though. So avoid using those. And it won't auto-jump into the next vector layer. If a vector object is already selected it'll just grab the next one in line. So, if you want to guarantee it'll start at the first object and then move up the line after that, just select the top level layer first. There are several methods, I like to select the bottom most layer and then select the layer I was just on, but I can also do it by checking the layer type and whether a vectorobject is already selected.

Here are two options I can think of right now. Not much but simple examples. Naturally there are probably far better solutions.

method 1

Code: Select all

    currentLayer = App.Do(Environment, 'ReturnLayerProperties')
    if currentLayer['LayerType'] != App.Constants.Layertype.Vector:
        return #not a vector layer, cannot proceed
    App.Do(Environment, 'SelectLayer', {'Path':(-9999,-9999,[0],False)}) #Select bottom layer
    App.Do(Environment, 'Selectlayer', {'Path':currentLayer['Path']}) # re-select layer
    rects = []
    while App.Do(Environment, 'GetNextObject')['HasObject']:
        obj = App.Do(Environment, 'ReturnVectorObjectProperties')['ListOfObjects'][0] #object data
        if obj['ObjectType'] == 'Path':
            pathData = obj['ObjectData']['Path']
            if len(pathData) > 1:
                Startpoint = pathData[0][-1]
                Endpoint = pathData[1][-1]
                rects.append( (StartPoint, EndPoint[0]-StartPoint[0], EndPoint[1] - StartPoint[1]))  #add rectangle to list
method 2

Code: Select all

    currentLayer = App.Do(Environment, 'ReturnLayerProperties')
    if currentLayer['LayerType'] != App.Constants.Layertype.Vector:
        return #not a vector layer, cannot proceed
    currentObject = App.Do(Environment, 'ReturnVectorProperties')['ListOfObjects'][0]
    if currentObject['ObjectType'] != 'Object': # We have an object selected, move out one level.  If it is an object, we have a layer, proceed as normal
        App.Do(Environment, 'SelectLayer', {'Path': (-1, 0, [], False)})
    rects = []
    while App.Do(Environment, 'GetNextObject')['HasObject']:
        obj = App.Do(Environment, 'ReturnVectorObjectProperties')['ListOfObjects'][0] #object data
        if obj['ObjectType'] == 'Path':
            pathData = obj['ObjectData']['Path']
            if len(pathData) > 1:
                Startpoint = pathData[0][-1]
                Endpoint = pathData[1][-1]
                rects.append( (StartPoint, EndPoint[0]-StartPoint[0], EndPoint[1] - StartPoint[1]))  #add rectangle to list
ilgk48
Posts: 60
Joined: Sun Apr 28, 2019 12:26 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
Corel programs: PaintShop PRO 2019 32bit

Re: Retrieving the individual SelectionRect of a set of objects

Post by ilgk48 »

Ah!!!!
Thank you LeviFiction !!!
What to say?
You are my personal manual :-) :-) :-)

quote;
0 and 1 are not indexes they are commands. PSP more or less follows the SVG standard 0 = MoveTo (used to move to a starting point without drawing), 1 = LineTo (move from current position to new position). 2 = CurveTo (Cubic), 3= Close Path.
perfect!

quote:
If you're seeing a 2 it's because it was accidentally drawn with the curve tool and dragged out ever so slightly. The first two (x,y) points are the control handles for the curve.

Just like that! Just like that!
Thank you again.
Post Reply