namespace PARDUS_CY_V7.Rhino7CommndsPardus.Dimensions.AlignedDimension
{
    [Rhino.Commands.CommandStyle(Rhino.Commands.Style.ScriptRunner)]
    internal class AlignedDimensionObj:PardusObject
    {
        public AlignedDimensionObj()
        {
            UpdatPrduousObject();
        }

        public override void UpdatPrduousObject()
        {
            List<string> DimensionGuids = new List<string>();
            List<Dimension> SelectedDimensionsFromRhino = new List<Dimension>();
            var doc = RhinoDoc.ActiveDoc;

            DimensionGuids.Clear();
            SelectedDimensionsFromRhino.Clear();

            // Run the dimension command
            //RhinoApp.WriteLine("Rectangle is not set. Cannot process leader point pair.");
            RhinoApp.RunScript("_DimAligned _Continue=_No", true);
            //RhinoApp.WriteLine("Rectangle is not set. Cannot process leader point pair.");

            // Retrieve the last added object, assuming it is a dimension
            var lastAddedObj = doc.Objects.MostRecentObject();
            if (lastAddedObj != null && lastAddedObj.ObjectType == ObjectType.Annotation)
            {
                var dimObj = lastAddedObj as DimensionObject;
                if (dimObj != null)
                {
                    // Access the dimension style
                    DimensionStyle dimStyle = dimObj.DimensionStyle;
                    // Extract or construct the suffix from dimension style properties
                    string suffix = "1";

                    // Check if the style name matches a pattern; if so, parse it
                    if (!string.IsNullOrEmpty(dimStyle.Name))
                    {
                        // Assume name pattern has the suffix as last part
                        // Regex pattern to match the desired numbers
                        var pattern = @"(?<=1\/)(\d+(\.\d+)?|\d+/\d+)";
                        var matches = Regex.Matches(dimStyle.Name, pattern);
                        foreach (Match match in matches)
                        {
                            string rawValue = match.Value;

                            if (rawValue.Contains('/'))
                            {
                                string[] parts = rawValue.Split('/');
                                if (parts.Length == 2)
                                {
                                    decimal numerator = decimal.Parse(parts[0]);
                                    decimal denominator = decimal.Parse(parts[1]);
                                    decimal result = numerator / denominator;
                                    suffix = $"{result}";
                                }
                            }
                            else
                            {
                                suffix = $"{decimal.Parse(rawValue)}";
                            }
                        }
                    }
                    else
                    {
                        suffix = "1";
                    }

                    // Construct the layer name based on the extracted suffix
                    string layerName = $"PCY_ANNOTATION::DIM_{suffix}";

                    // Find or create the layer
                    int layerIndex = doc.Layers.FindByFullPath(layerName, -1);
                    if (layerIndex == -1)
                    {
                        Layer newLayer = new Layer { Name = layerName };
                        layerIndex = doc.Layers.Add(newLayer);
                    }

                    // Update the dimension's layer
                    var attributes = dimObj.Attributes;
                    attributes.LayerIndex = layerIndex;

                    // Delete the original dimension object
                    doc.Objects.Delete(dimObj.Id, true);

                    // Add the dimension back with updated attributes
                    doc.Objects.Add(dimObj.Geometry, attributes);
                    // Redraw views
                    doc.Views.Redraw();
                }
            }
        }

        public override IAttributePardus attribute { get; }
        public override IGomatryPardus BakeGeomatries { get; }
    }

somtimes before the Rhino set the DimensionStyle we are using it I want after this comand: RhinoApp.RunScript("_DimAligned _Continue=_No", true); wait for half a second before going to the next line