Redraw in every iteration VB.Net

Dear all,
In a custom Grasshopper component, how can you enable redraw in every iteration?
For example I have this AntColonyOptimizer solving a TSP and I would like to see how the Graph changes in every iteration.

For a start, I tried to just put the DA.setData into a for loop inside the Grasshopper component item :

Protected Overrides Sub SolveInstance(DA As IGH_DataAccess)

    Dim pointList As New List(Of Point3d)
    If (Not DA.GetDataList(0, pointList)) Then Return

    Dim cities(pointList.Count - 1)() As Double
    For i As Integer = 0 To pointList.Count - 1
        cities(i) = New Double(2) {}
        cities(i)(0) = pointList.Item(i).X
        cities(i)(1) = pointList.Item(i).Y
        cities(i)(2) = pointList.Item(i).Z
    Next


    For u As Integer = 0 To 100
        Dim tsp As New ACO_TSP()
        tsp.Main(cities)

        Dim pts As New List(Of Point3d)
        For i As Integer = 0 To pointList.Count - 1
            pts.Add(pointList.Item(tsp.bestTrailOut(i)))
        Next
        pts.Add(pointList.Item(tsp.bestTrailOut(0)))
        Dim pline As New Polyline(pts)

        DA.SetData(0, pline)

        For i As Integer = 0 To pointList.Count - 1
            cities(i) = New Double(2) {}
            cities(i)(0) = pts.Item(i).X
            cities(i)(1) = pts.Item(i).Y
            cities(i)(2) = pts.Item(i).Z
        Next
    Next

End Sub 

But it still only shows the final polyline… ?
Is there something like an EnableRedraw as in RhinoScripting?
Would such a command also work, if it is in another class-item (in this case, if I’m trying to draw the polyline within my ACO_TSP class) and not in the Grasshopper-Component class item?

Any help is very much appreciated!
Christoph

@stevebaer, what is the best way these days to force an immediate redraw of Rhino viewports?

doc.Views.Redraw() possibly followed by RhinoApp.Wait()

1 Like

Thanks. @Christoph1, you’ll still run into problems doing it this way. You are inside a tight loop in that piece of code and GH is waiting for you to exit before it will complete the solution. Until the solution completes GH will not be able to participate in any redraws as all preview geometry has been nuked at the start of the solution.

You’re going to have to do the drawing yourself, and you’ll have to accept that GH will be left in an awkward state if you start pumping messages in the middle of a solution.

The best approach would be to not run your loop inside the GH solution but in a separate context, and only inject your finished data back into your component outputs after you’re done.

I’m sorry it’s this complicated but the solution mechanics just weren’t designed for stuff to happen in the middle.

1 Like

I’m not sure I understood your question, but…
For example, for the case of the timer function.

You do not do this:

SolveInstance ()
Dim counter as new integer
For i as int32 = 0 to 100
counter += 1
DA.setData (counter)
End sub

Because you will always take the final result.
You need to do this:

Dim counter as new integer
SolveInstance ()
counter + = 1
DA.setData (counter)
end sub

and rerun the component. As the component has already been instantiated when you place it on canvas, the counter variable is not reset in each iteration.

Samething like this for your case, take the variables out of solveinstance and use some method to recompute the component, may be using the timer component or ExpireSolution() method or something.

This may go in the direction that David said, but I’m not sure.

Thanks all!
both @stevebaer and @DavidRutten tips worked. But with David’s way, where I am redrawing outside SolveInstance, I am using rhinocommon geometry, and not grasshopper.
Would it be possible to also redraw grasshopper geometry outside that GH SolveInstance?

@Dani_Abalde , how would I rerun the SolveInstance() component automatically, other than manually clicking on the GH component and say “recompute”?