What is the best way to identify a layer group in Aspose.PSD.FileFormats.Psd.Layers.Layer[]

Hi, I need to get all layer information and process the data in myways. It seems that {Aspose.PSD.FileFormats.Psd.Layers.Layer[]} contains all layer information. And there are usually two items in the array that are related to one layer group. One item contains position and size infos, and the other has the group name. I used the following lines to access the group name. It usually works since the first LayerGroup type item has the information (in Layers[]) of all the layers/layer group inside itself. But when it comes to a PSD which contains many layer groups, this method is not working. I couldn’t find a right formula to identify a layer group, and to know its name, child-layers info etc.

using (PsdImage psd = (PsdImage)Aspose.PSD.Image.Load(psdpath))
{
for(int i = 0; i < psd.Layers.Length; i++)
{
if (psd.Layers[i]is LayerGroup && psd.Layers[i].Name == “</Layer group>”){
int number = (psd.Layers[i] as LayerGroup).Layers.Length;
int j = i + number + 1;
oldName = psd.Layers[j].DisplayName;
}}UXUP_Nest_TabIndicatorPs_vertical☆wanyansong.zip (913.6 KB)

@mcjj23

Can you please consider using property IsVisibleInGroup and see if that works for you or not.

Does this property only indicate true or false inside a group? I need to know every group’s name, size, location, child layers…

@mcjj23

We need to investigate this w.r.t your requirements and will share the feedback with you as soon as we get some possible solution to meet the requirements.

@mcjj23

We suggest you to please try using following approach on your end to serve the purpose.

 var psdPath = @"C:\UXUP_Nest_TabIndicatorPs_vertical☆wanyansong.psd";
            using (PsdImage psd = (PsdImage)Aspose.PSD.Image.Load(psdPath))
            {
                for (int i = 0; i < psd.Layers.Length; i++)
                {
                    var layer = psd.Layers[i];

                    if (layer is LayerGroup) {
                        var sectionResource = GetSectionResource(layer);
                        if (sectionResource.SectionType == LayerSectionType.OpenFolder)
                        {
                            // In the this type of Section Resource you can get group layer name
                            var oldName = layer.DisplayName;
                        }

                        if (sectionResource.SectionType == LayerSectionType.SectionDivider)
                        {
                            // In the this type of Section Resource you can get child layers
                            var childs = ((LayerGroup)layer).Layers;
                        }
                    }
                }
            }

hi can you attach the definition of GetSectionResource? I couldn’t find this function. Thanks!

@mcjj23

Please find the definition below.

        internal static LayerSectionResource GetSectionResource(Layer layer)
        {
                for (int i = 0; i < layer.Resources.Length; i++)
                {
                    LayerSectionResource currentSectionResource = layer.Resources[i] as LayerSectionResource;
                    if (currentSectionResource != null)
                    {
                        return currentSectionResource;                        
                    }
                }

            throw new Exception("LayerSectionResource Not Found. It's not LayerGroup.");
        }


Hi these are the group names I got following your method. And obviously some layer groups haven’t been printed. Also I want to know that how can I associate layergroup with its name, because some LayerGroup only contains name and the others LayerGroup only contains child layers? Thanks!

@mcjj23

I have associated the feedback in our issue tracking system and we will share feedback with you as soon as the issue will be addressed.

@mcjj23

We have further investigated your requirements and will improve our API. At this moment we can only offer to use this code snippet:

        static LayerSectionResource GetSectionResource(Layer layer)
        {
                for (int i = 0; i < layer.Resources.Length; i++)
                {
                    LayerSectionResource currentSectionResource = layer.Resources[i] as LayerSectionResource;
                    if (currentSectionResource != null)
                    {
                        return currentSectionResource;                        
                    }
                }

            throw new Exception("LayerSectionResource Not Found. It's not LayerGroup.");
        }

        // i - is a index of "End" Layer
        static int GetBeginMarkerIndex(int i, Layer[] imageLayers)
        {
            int nestedLevel = 0;
            for (int j = i + 1; j < imageLayers.Length; j++)
            {
                LayerGroup layerGroup = imageLayers[j] as LayerGroup;
                if (layerGroup != null)
                {
                    var layerSectionType =  GetSectionResource(layerGroup).SectionType;
                    if (layerSectionType == LayerSectionType.SectionDivider)
                    {
                        nestedLevel++;
                    }
                    else if (layerSectionType == LayerSectionType.OpenFolder
                             || layerSectionType == LayerSectionType.ClosedFolder)
                    {
                        if (nestedLevel == 0)
                        {
                            return j;
                        }

                        nestedLevel--;
                    }
                }
            }

            return -1;
        }

        // i - is a index of "Begin" Layer 
        static int GetEndMarkerIndex(int i, Layer[] imageLayers)
        {
            int nestedLevel = 0;
            for (int j = i - 1; j >= 0; j--)
            {
                LayerGroup layerGroup = imageLayers[j] as LayerGroup;
                if (layerGroup != null)
                {
                    var layerSectionType = GetSectionResource(layerGroup).SectionType;
                    if (layerSectionType == LayerSectionType.SectionDivider)
                    {
                        if (nestedLevel == 0)
                        {
                            return j;
                        }

                        nestedLevel--;
                    }
                    else if (layerSectionType == LayerSectionType.OpenFolder
                             || layerSectionType == LayerSectionType.ClosedFolder)
                    {
                        nestedLevel++;
                    }
                }
            }

            return -1;
        }