Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 4120

Script Grasshopper and Eto

$
0
0

I’m starting to create scripts with grasshopper and Eto, but I’m having some problems, I need help, I have some problems with Eto and Rhino closes when I try to run the preview

import Rhino
import rhinoscriptsyntax as rs
import Rhino.UI
import Eto.Drawing as drawing
import Eto.Forms as forms
import ghpythonlib.components as ghcomp
import Rhino.Geometry as rg

class CirculosInterface(forms.Form):

    def __init__(self):
        super().__init__()
        self.Title = "Generate Circles"
        self.ClientSize = drawing.Size(300, 200)
        
        self.curva_input = None
        self.preview_output = None
        self.final_output = None

        self.count_label = forms.Label(Text="Number of Points")
        self.count_box = forms.NumericUpDown(Value=10, MinValue=1, MaxValue=100)
        
        self.radius_label = forms.Label(Text="Radius of Circles:")
        self.radius_box = forms.NumericUpDown(Value=1, MinValue=0.1, MaxValue=10)

        self.seed_label = forms.Label(Text="Seeds:")
        self.seed_box = forms.NumericUpDown(Value=1, MinValue=1, MaxValue=100)

        self.preview_button = forms.Button(Text="Preview")
        self.preview_button.Click += self.on_preview_click

        self.generate_button = forms.Button(Text="Generate")
        self.generate_button.Click += self.on_generate_click

        layout = forms.DynamicLayout()
        layout.Spacing = drawing.Size(5, 5)
        layout.AddRow(self.count_label, self.count_box)
        layout.AddRow(self.radius_label, self.radius_box)
        layout.AddRow(self.seed_label, self.seed_box)
        layout.AddRow(self.preview_button, self.generate_button)
        
        self.Content = layout

    def on_preview_click(self, sender, e):
        count = int(self.count_box.Value)
        radius = float(self.radius_box.Value)
        seed = int(self.seed_box.Value)

        if self.curva_input is None:
            self.curva_input = rs.GetObject("Select a curve", rs.filter.curve)

        if self.curva_input:
            curva = rs.coercecurve(self.curva_input)
            points = ghcomp.PopulateGeometry(curva, count, seed)

            if self.preview_output:
                rs.DeleteObjects(self.preview_output)

            preview_circles = []
            for pt in points:
                plane = rg.Plane(pt, rg.Vector3d.ZAxis)
                circle = rg.Circle(plane, radius)
                preview_circles.append(rs.AddCircle(circle))

            self.preview_output = preview_circles

    def on_generate_click(self, sender, e):
        count = int(self.count_box.Value)
        radius = float(self.radius_box.Value)
        seed = int(self.seed_box.Value)

        if self.curva_input:
            curva = rs.coercecurve(self.curva_input)
            points = ghcomp.PopulateGeometry(curva, count, seed)

            goals = []
            for pt in points:
                goal = ghcomp.SphereCollide(pt, radius, 1)
                goals.append(goal)

            curve_goal = ghcomp.CurvePointCollide(points, curva, True, 1)
            goals.append(curve_goal)

            result = ghcomp.KangarooSolver(goals, True, 0.1, 10)
            result_points = result[0]

            if self.final_output:
                rs.DeleteObjects(self.final_output)

            final_circles = []
            for pt in result_points:
                plane = rg.Plane(pt, rg.Vector3d.ZAxis)
                circle = rg.Circle(plane, radius)
                final_circles.append(rs.AddCircle(circle))

            self.final_output = final_circles

form = CirculosInterface()
form.Owner = Rhino.UI.RhinoEtoApp.MainWindow
form.Show()

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 4120

Trending Articles