public class Detail2DGH : BaseGhComponent
{
    private PaftalamaDetayAlmaFrm frm = new PaftalamaDetayAlmaFrm();
    private List<Curve> rectangle = new List<Curve>();
    private readonly List<Curve> previewCurvesList = new List<Curve>();
    private bool showGeometry = true;
    private bool bake = false;
    private bool closeForm = false;
    private string blockName;
    private Guid guid;
    private Make2dResult hiddenAndVisibleCurves;
    private List<GeometryBase> cuttedBlockGeo = new List<GeometryBase>();
    List<Curve> insideCurves = new List<Curve>();
    List<Curve> overlappingCurves = new List<Curve>();
    public Detail2DGH()
        : base("2DDETAY", "P", "DETAY ALMA", "PARDUS", "3D'DEN 2D'YE")
    {
        InitializeEventHandlers();
    }

    protected override string CodeName => "DETAY_ALMA";
    protected override GH_Exposure? ValidExposure => GH_Exposure.primary;
    protected override Bitmap Icon => Resources.DETAY_ALMA;

    private void InitializeEventHandlers()
    {
        frm.btnBlock.Click += AddBlock;
        frm.btnRec.Click += RecSel;
        frm.txtName.TextChanged += ControlValueChanged;
        frm.cmbScaleText.TextChanged += ControlValueChanged;
        frm.cmbScaleDraw.TextChanged += ControlValueChanged;
        frm.cmbLayer.TextChanged += ControlValueChanged;
        frm.chbHidden.CheckedChanged += ControlValueChanged;
        frm.radTurkey.CheckedChanged += ControlValueChanged;
        frm.radEnglish.CheckedChanged += ControlValueChanged;
        frm.radRussian.CheckedChanged += ControlValueChanged;
        frm.btnBake.Click += Bake;
        frm.Closed += Frm_Closed;
    }

    private void ControlValueChanged(object sender, EventArgs e)
    {
        ExpireSolution(true);
    }

    private void RecSel(object sender, EventArgs e)
    {
        rectangle.Clear();
        rectangle = EIGetRihnoTl.GetPolylineCurve("Select region.", false, false, false, false, out guid);
        ExpireSolution(true);
    }

    private void Frm_Closed(object sender, EventArgs e)
    {
        if (closeForm) bake = true;
        showGeometry = false;
        ExpireSolution(true);
    }

    private void Bake(object sender, EventArgs e)
    {
        closeForm = true;
        frm.Close();
    }

    private void AddBlock(object sender, EventArgs e)
    {
        blockName = EIGetRihnoTl.GetNameBlock("Block Seç", false, true)[0];
        ExpireSolution(true);
    }

    protected override void RegisterInputParams(GH_InputParamManager pManager) { }

    protected override void RegisterOutputParams(GH_OutputParamManager pManager)
    {
        pManager.AddGenericParameter("C", "c", "c", GH_ParamAccess.list);
        pManager.AddGenericParameter("C", "c", "c", GH_ParamAccess.list);
    }

    /// <summary>
    /// Authenticated Solve Instance method where the main functionality is executed.
    /// </summary>
    protected override void AuthenticatedSolveInstance(IGH_DataAccess DA)
    {
        frm.Show();
        previewCurvesList.Clear();
        var rectangleGeometries = new List<GeometryBase>();
        var make2dGeometries = new List<GeometryBase>();
        var doc = RhinoDoc.ActiveDoc;
        var currentDocPlane = doc.Views.ActiveView.ActiveViewport.ConstructionPlane();

        if (rectangle.Count > 0)
        {
            List<Curve> ttt = new List<Curve>();
            bool is2D;
            List<GeometryWithAttributes> blockGeoWithAttributes = GetBlockGeometries(doc, blockName, out var boundingBoxGeo1, out List<Curve> ggg, out is2D);

            ttt.AddRange(ggg);

            ProcessBlockCurves(ttt, rectangle[0],out List<Curve>_in,out List<Curve>_out);
            // Section 9: Bake Geometries
            if (bake)
            {
                //BakeGeometries(doc, rectangleGeometries, make2dGeometries, _Hiddenss, _Visibles, _Verticals);
            }

            // Show Geometry
            EIShowGeomatryTl.ShowGeomatry(previewCurvesList, showGeometry);
            DA.SetDataList(0, ttt);
            DA.SetDataList(1, rectangle);
            //doc.Objects.Delete(guid, true);
        }
    }
        private List<GeometryWithAttributes> GetBlockGeometries(RhinoDoc doc, string blockName, out BoundingBox boundingBoxGeo, out List<Curve> outputCurvves, out bool is2D)
        {
            var blockGeometriesWithAttributes = new List<GeometryWithAttributes>();
            List<GeometryBase> BlockObjs = new List<GeometryBase>();
            List<Curve> Outputss = new List<Curve>();
            var definition = doc.InstanceDefinitions.Find(blockName, true);
            
            if (definition != null)
            {
                // Retrieve objects from the block definition
                var geometryObjects = definition.GetObjects();
                if (geometryObjects != null)
                {
                    foreach (var obj in geometryObjects)
                    {
                        BlockObjs.Add(obj.Geometry);
                        blockGeometriesWithAttributes.Add(new GeometryWithAttributes
                        {
                            Geometry = obj.Geometry,
                            Attributes = obj.Attributes
                        });
                    }
                }
            }

            // Compute the bounding box of these geometries
            boundingBoxGeo = BoundigboxGeometry(BlockObjs);


            var bbox = BoundingBox.Empty;
            foreach (var obj in BlockObjs)
            {
                if (obj is Curve ddd)
                {
                    var fff = (Curve)ddd;
                    Curve.ProjectToPlane(fff, Plane.WorldXY);
                    var objBbox = fff.GetBoundingBox(true);
                    bbox.Union(objBbox);
                    Outputss.Add(fff);
                }
            }
            var box = new Box(bbox);

            is2D = Math.Abs(box.Z.Max - box.Z.Min) < 1;

            outputCurvves = Outputss;
            // Check if the geometry is 2D
            //is2D = Math.Abs(boundingBoxGeo.Max.Z - boundingBoxGeo.Min.Z) < 1;


            return blockGeometriesWithAttributes;
        }
        private void ProcessBlockCurves(List<Curve> blockCurves, Curve region, out List<Curve>r1, out List<Curve>r2)
        {
            List<Curve> In = new List<Curve>();
            List<Curve> Out = new List<Curve>();
            List<Curve> AllSSegments = new List<Curve>();
            foreach (var curve in blockCurves)
            {
                AllSSegments.AddRange(blockCurves[0].DuplicateSegments());
            }

            foreach (var curve in AllSSegments)
            {

                // Project the curve onto the region's plane if necessary
                Curve projectedCurve = curve.DuplicateCurve();
                projectedCurve = Curve.ProjectToPlane(projectedCurve, Plane.WorldXY);

                // Check if the curve is completely inside the region
                if (IsCurveInsideRegion(projectedCurve, region))
                {
                    insideCurves.Add(projectedCurve);
                }
                else
                {
                    List<double> passs = new List<double>();
                    // Check for intersection with the region
                    var intersectionEvents = Rhino.Geometry.Intersect.Intersection.CurveCurve(projectedCurve, region, 0.001, 0.001);
                    var InterectPoints = CurvveCurveIntersection(projectedCurve, region,out passs);
                    if (InterectPoints.Count > 0)
                    {
                        // Shatter the curve at the intersection points
                        var segments = projectedCurve.DuplicateSegments();
                        var shatteredCurves = projectedCurve.Split(passs);

                        // Check each segment to see if it's inside the region
                        foreach (var segment in shatteredCurves)
                        {
                            // Use the midpoint of the segment to test if it's inside
                            var midpoint = segment.PointAtNormalizedLength(0.5);
                            if (region.Contains(midpoint) == PointContainment.Inside)
                            {
                                In.Add(segment);
                            }
                            else
                            {
                                Out.Add(segment);
                            }
                        }


                    }
                    else
                    {
                        Out.Add(projectedCurve);
                    }

                }
            }

            r1 = In;
            r2 = Out;
        }

        private List<Point3d> CurvveCurveIntersection(Curve A, Curve B,out List<double>prams)
        {
            var doc = RhinoDoc.ActiveDoc;
            //Component written by @bava-kumaravel
            //Solves intersections between two curves A and B and returns the
            //points of intersection P and the parameters on the curves tA and tB

            //Define lists to hold the intersection points and parameters
            List<Point3d> points = new List<Point3d>();
            List<double> paramA = new List<double>();
            List<double> paramB = new List<double>();

            //Define tolerance as the Model Space Absolute Tolerance
            double tol = doc.ModelAbsoluteTolerance;

            //Perform intersection
            Rhino.Geometry.Intersect.CurveIntersections intersections =
              Rhino.Geometry.Intersect.Intersection.CurveCurve(A, B, tol, tol);

            if (intersections != null)
            {
                //If there are intersections, go through the list of intersection events
                for (int i = 0; i < intersections.Count; i++)
                {
                    Rhino.Geometry.Intersect.IntersectionEvent inter = intersections[i];
                    //Check if the intersection event is an intersection or an overlap
                    if (inter.IsOverlap)
                    {
                        //If it is an overlap, there will be two points and parameters as
                        //outputs, one at the start of the overlap and one at the end
                        Point3d startingPt = inter.PointA;
                        Point3d endingPt = inter.PointA2;
                        //Add the two points to the points list
                        points.Add(startingPt);
                        points.Add(endingPt);
                        //Get the overlapping interval in Curve A and B
                        Interval overlapA = inter.OverlapA;
                        Interval overlapB = inter.OverlapB;
                        //Get the starting and ending parameters from the overlapping intervals
                        double startingParamA = overlapA.Min;
                        double endingParamA = overlapA.Max;
                        double startingParamB = overlapB.Min;
                        double endingParamB = overlapB.Max;
                        //Add the parameters to the respective lists
                        paramA.Add(startingParamA);
                        paramA.Add(endingParamA);
                        paramB.Add(startingParamB);
                        paramB.Add(endingParamB);
                    }
                    else if (inter.IsPoint)
                    {
                        //If it is an intersection, there will be one point and one parameter
                        //for each curve
                        Point3d point = inter.PointA;
                        //Add intersection point to the points list
                        points.Add(point);
                        //Get the parameters at curve A and B
                        double parameterA = inter.ParameterA;
                        double parameterB = inter.ParameterB;
                        //Add the parameters to their respective lists
                        paramA.Add(parameterA);
                        paramB.Add(parameterB);
                    }
                }
            }

            prams = paramA;
            return points;
        }
        private bool IsCurveInsideRegion(Curve curve, Curve region)
        {
            // Use a point on the curve to test if it's inside the region
            var testPoint = curve.PointAtNormalizedLength(0.5);
            var testPoint2 = curve.PointAtNormalizedLength(0.2);
            var testPoint3 = curve.PointAtNormalizedLength(0.8);

            var tt1 = region.Contains(testPoint, Plane.WorldXY, 0.001) == PointContainment.Inside;
            var tt12 = region.Contains(testPoint2, Plane.WorldXY, 0.001) == PointContainment.Inside;
            var tt13 = region.Contains(testPoint3, Plane.WorldXY, 0.001) == PointContainment.Inside;
            return (tt1 && tt12 && tt13);
        }


this has a problem which is the bloc curves are always at the origine but the real block is not there