Why does my Python component run without error but gives null output?

Why does my Python component run without error but gives null output?

here my code:

import Rhino.Geometry as rg
import Grasshopper.Kernel as gh
import Grasshopper.Kernel.Data as ghdata
        
class CircleIntersection(gh.GH_Component):
    """Checks for intersections between circles and draws lines."""
        
    def __init__(self):
        super(CircleIntersection, self).__init__("Circle Intersection", "CircleInt", "Checks for intersections between circles and draws lines.", "Custom", "Analysis")
        
    def RegisterInputParams(self):
        self.AddParameter(ghparams.GH_PointParam, "Points", "P", "A list of points representing circle centers", ghdata.GH_ParamAccess.item)
        self.AddParameter(ghparams.GH_NumberParam, "Radius", "R", "The radius of the circles", ghdata.GH_ParamAccess.item)
        
    def RegisterOutputParams(self, pManager):
        self.AddParameter(ghparams.GH_LineParam, "Lines", "L", "Lines connecting intersecting circles", ghdata.GH_ParamAccess.list)
        self.AddParameter(ghparams.GH_BoolParam, "Intersections", "I", "Boolean indicating if circles intersect", ghdata.GH_ParamAccess.list)
        
    def SolveInstance(self, doc, DA):
        points = self.GetInput(0).CastTo<list[rg.Point3d]>()
        radius = self.GetInput(1).CastTo<float>()
        
        print("Points:", points)
        print("Radius:", radius)
        
        if not points or radius <= 0:
            print("Invalid input: Points must be non-empty and radius must be positive.")
            return
        
        circles = [rg.Circle(pt, radius) for pt in points]
        print("Circles:", circles)
        
        lines = []
        intersections = []
        
        for i in range(len(circles)):
            for j in range(i + 1, len(circles)):
                intersection_result = rg.Intersection.CurveCurve(circles[i], circles[j])
                print("Intersection Result for Circles {} and {}: {}".format(i, j, intersection_result))
        
                if intersection_result.Count > 0:
                    line = rg.Line(circles[i].Center, circles[j].Center)
                    lines.append(line)
                    intersections.append(True)
                else:
                    intersections.append(False)
        
        print("Lines:", lines)
        print("Intersections:", intersections)
        
        DA.SetDataList(0, lines)
        DA.SetDataList(1, intersections)
        

and screenshot

test.3dm (25.4 KB)
test.gh (16.6 KB)

What is the purpose of this code!?
maybe this?!


"""Grasshopper Script"""
from Rhino.Geometry import Point3d, Circle, Line
from Rhino.Geometry.Intersect import Intersection

def run_script(pt, radius):
    c = []  # List to store Circle objects
    l = []  # List to store Line objects

    # Create circles with each point and radius combination
    for p in pt:
        for r in radius:
            c.append(Circle(p, r))
    
    # Find intersections between each pair of circles
    for i in range(len(c)):
        for j in range(i + 1, len(c)):
            # Attempt to find intersection points
            rc, p1, p2 = Intersection.CircleCircle(c[i], c[j])

            if rc:  # If intersection is successful
                l.append(Line(p1, p2))
    
    # Outputs
    return l, c
a=run_script(pt, radius)[0]
b=run_script(pt, radius)[1]
# Example usage:
# pt = [Point3d(x, y, z), ...]
# radius = [r1, r2, ...]
# lines, circles = run_script(pt, radius)
# Now 'lines' contains the intersection lines, and 'circles' contains the generated circles


text circ intersect.gh (10.3 KB)

or maybe this?


text circ intersect-VGer2.gh (10.6 KB)
Python

from System import Array
from System.Runtime.CompilerServices import StrongBox
from Rhino.Geometry import Point3d, Circle, Line
from Rhino.Geometry.Intersect import Intersection

def run_script(pt, radius):
    circles = []  # List to store Circle objects
    lines = []    # List to store Line objects

    # Create circles with each point and appropriate radius
    for i, point in enumerate(pt):
        r = radius[min(i, len(radius) - 1)]  # Use the current radius or the last one if out of range
        circles.append(Circle(point, r))
    
    # Find intersections and draw lines between circle centers
    for i in range(len(circles)):
        for j in range(i + 1, len(circles)):
            p1 = StrongBox[Point3d]()
            p2 = StrongBox[Point3d]()

            # Find intersections between curves
            curve1 = circles[i].ToNurbsCurve()
            curve2 = circles[j].ToNurbsCurve()
            intersections = Intersection.CurveCurve(curve1, curve2, 0.00001, 0.000001)

            if intersections.Count > 0:
                if Intersection.CircleCircle(circles[i], circles[j], p1, p2):
                    # Create a line only if the intersection is valid
                    if p1.Value != Point3d.Unset and p2.Value != Point3d.Unset:
                        inter_line = Line(p1.Value, p2.Value)
                        if inter_line.Length > 0:
                            center_line = Line(circles[i].Center, circles[j].Center)
                            lines.append(center_line)

    # Return the line and circle objects
    return lines, circles

a=run_script(pt, radius)[0]
b=run_script(pt, radius)[1]

and C#

private void RunScript(
	List<Point3d> pt,
	List<double> radius,
	ref object a,
	ref object Circle)
    {
        var c = new List<Circle>();
        var l = new List<Line>();
int k=0;
        // Create circles from points and radii
        for (int i = 0; i < pt.Count; i++)
        { k=Math.Min(i,radius.Count-1);
            c.Add(new Circle(pt[i], radius[k]));
        }

        // Find intersection points and draw lines
        for (int i = 0; i < c.Count; i++)
        {
            for (int j = i + 1; j < c.Count; j++)
            {
                Point3d p1 = Point3d.Unset;
                Point3d p2 = Point3d.Unset;

               Rhino.Geometry.Intersect.CurveIntersections s=Rhino.Geometry.Intersect.Intersection.CurveCurve(c[i].ToNurbsCurve(),c[j].ToNurbsCurve(),0.00001,0.000001);
                var intersectionResult = Rhino.Geometry.Intersect.Intersection.CircleCircle(c[i], c[j], out p1, out p2);

             	if (s.Count!=0){
                    var intersectionLine = new Line(p1, p2);
                    //l.Add(intersectionLine);

                    var d = intersectionLine.Length;

                    // Add a line between the circle centers if the intersection line is not zero-length
                    if (d > 0)
                    {
                        var pc1 = c[i].Center;
                        var pc2 = c[j].Center;
                        var centerLine = new Line(pc1, pc2);
                        l.Add(centerLine);
                    }
                }
            }
        }

        // Output the lines and circles
        a = l;
        Circle = c;
    }
1 Like

I was happy with your answer. I tried your code and it works for some points, but not for some of mine.
I sent the screenshot along with the file.

For example A to B
C to D
E to F
G to H
H to I
and …

Thank You


test2.gh (17.4 KB)

hi @kalantarisaeede
that error fix in this Code:
testV2-Fix.gh (17.0 KB)


C#

using System;
using System.Collections.Generic;
using Rhino.Geometry;

public class Script_Instance : GH_ScriptInstance
{
  private void RunScript(
	List<Point3d> points,
	List<double> radius,
	ref object A,
	ref object B)
  {
    var circles = new List<Circle>();
    var lines = new List<Line>();
int k=0; int w=0;

	for (int i = 0; i <  points.Count; i++)
	{k=Math.Min(i,radius.Count-1);
		var circle = new Circle(points[i], radius[k]);
		circles.Add(circle);
	}

    // Check for intersections and draw lines between circle centers
    for (int i = 0; i < circles.Count; i++)
    {
      for (int j = i + 1; j < circles.Count; j++)
      {
        Circle circleA = circles[i];
        Circle circleB = circles[j];

        if (Intersects(circleA, circleB))
        {
          Line line = new Line(circleA.Center, circleB.Center);
          lines.Add(line);
        }
      }
    }

    A = circles;
    B = lines;
  }

  // Function to check if two circles intersect
  private bool Intersects(Circle c1, Circle c2)
  {
    // Calculate distance between centers
    double distance = c1.Center.DistanceTo(c2.Center);

    // Check if the circles intersect
    return distance <= (c1.Radius + c2.Radius) && distance >= Math.Abs(c1.Radius - c2.Radius);
  }
}

Python

import Rhino.Geometry as rg

def check_intersection(c1, c2):
    # Calculate distance between centers
    distance = c1.Center.DistanceTo(c2.Center)
    
    # Check if the circles intersect
    return distance <= (c1.Radius + c2.Radius) and distance >= abs(c1.Radius - c2.Radius)

def create_circles_and_lines(points, radii):
    circles = []
    lines = []
    
    # Create circles from points with the corresponding radius
    for i in range(len(points)):
        k = min(i, len(radii) - 1)  # Ensure radius indexing is within range
        circle = rg.Circle(points[i], radii[k])
        circles.append(circle)
    
    # Check for intersections and draw lines between circle centers
    for i in range(len(circles)):
        for j in range(i + 1, len(circles)):
            circleA = circles[i]
            circleB = circles[j]
            
            if check_intersection(circleA, circleB):
                line = rg.Line(circleA.Center, circleB.Center)
                lines.append(line)
    
    return circles, lines

# Inputs in Grasshopper
circles, lines = create_circles_and_lines(points, radius)

# Outputs to Grasshopper
A = circles
B = lines
1 Like

hi @arcarc60
great
Thank you for correcting my code.