Grasshopper

algorithmic modeling for Rhino

Hello All,

I am practicing through some basic Rhinocommon VB.Net routines and was wondering if anyone else has encountered the IEnumerator, IEnumberable Members from the System.Collections
. After some research they seem to have quite a bit of potential for iterating through lists of data.
In the listed example below I am attempting to create 2 points, 2 lines and loft a brep through the lines.  One of the curve parameters is a System.Collections.Generic.IEnumberable(of Curve)

However I am not quite sure how to define it.  Any input would be greatly appreciated.

 

Many Thanks.

 

 
Dim ptL As New List(Of Point3d)
Dim lnL As New List(Of Line)

Dim pt1 As New Point3d(15, 0, 0)
ptL.Add(pt1)
Dim ln1 As New Line(pt1, vector3d.XAxis, x)
lnL.Add(ln1)

Dim pt2 As New Point3d(15, 10, 0)
ptL.Add(pt2)
Dim ln2 As New Line(pt2, vector3d.ZAxis, x)
lnL.add(ln2)


Dim srf As New Brep
srf = Rhino.Geometry.Brep.CreateFromLoft(lnL, ptL(0), ptL(1), Rhino.Geometry.LoftType.Normal, False)

a = srf
B = ln1
c = ln2

Views: 2055

Attachments:

Replies to This Discussion

Hi Madu,

a lot of classes implement IEnumerable(Of T). You could create a List(Of T), or an Array of type T or a Collection or ... or.... or.... The very reason for using IEnumerable is to maximize the possible number ways in which you can supply the data.

--
David Rutten
david@mcneel.com
Seattle, WA
Hi David thank you for the reply,understood.

I have incorporated it in, and am getting a error on the line below claiming
"value of type 1-dim array of Rhino.Geomtry.Brep cannot be converted to Rhino.Geometry.Brep"
Tried a few ways of writing it, seems its due to the way the Brep.CreatefromLoft function is reading the IEnumerable list.

Dim lnL As New list(Of Line)
Dim lnE As IEnumerable(Of Line)
Dim ln1 As New Line(pt1, vector3d.XAxis, x)
Dim ln2 As New Line(pt2, vector3d.ZAxis, x)
lnL.Add(ln1)
lnL.add(ln2)

lnE = lnL

Dim srf As Brep
(Error)srf = Brep.CreateFromLoft(lnE, point3d.Unset, point3d.Unset, Lofttype.Normal, False)
srfL.Add(srf)
Dim lines As New List(Of Curve)
lines.Add(New LineCurve(New Line(pt1, vector3d.XAxis, x)))
lines.Add(New LineCurve(New Line(pt2, vector3d.ZAxis, x)))
Dim srf As Brep() = Brep.CreateFromLoft(lines, Point3d.Unset, Point3d.Unset, Lofttype.Normal, False)
If (srf IsNot Nothing) Then surfaces.AddRange(srf)


--
David Rutten
david@mcneel.com
Seattle, WA
To elaborate, CreateFromLoft requires an IEnumerable(Of Curve). List(Of Curve) implements the IEnumerable(Of Curve) interface, so you don't have to assign it specifically to an IEnumerable(Of Curve) variable before you can pass it to Loft.

Also, Line does not derive from Curve, so you cannot add a Line to a List(Of Curve)*. You need to create LineCurves instead.

CreateFromLoft does not return a single Brep (although often it does). There are cases where multiple Breps are the outcome of a Loft and because of this the CreateFromLoft function must always return an array of breps, even though this array often only contains a single item.

--
David Rutten
david@mcneel.com
Seattle, WA


* Think of this as Curve being "Animal", LineCurve being "pig" and Line being "bacon". You cannot add bacon to a list of animals, because bacon is not an animal. A pig however is an animal, so you can add as many pigs to a list of animals as you want. In addition to pigs you can also add chickens (ArcCurve), dogs (PolylineCurves), horses (NurbsCurves) and cows (PolyCurve) to that very same list.

Line is a light-weight type which does not carry around all the baggage required by the Curve class. Just like Arc, Circle and Polyline it does not derive from Curve.
Ahh got it because the Line type is a value type (i.e Integer, Boolean).
Hence assigning it as LineCurve(Line).
The animal analogy is also quite helpful in understanding these class/member heiarchies.
Brilliant! Thanks David!

Let me ask one question related to IEnumerable(Of T).

I attach my script as follows. It is to find projected points to a complex mesh. I used IEnumerable(Of point3d) with the following script. But I can not use the found points for further script because the points can not be converted to a list of point3d which is 'ptms' at the following script. I want to make a list of point3d with the found points.

 

Please help for the problem.

 

Thanks for your help in advance.

 

Jihoon.

  

Private Sub RunScript(ByVal pts As List(Of Point3d), ByVal terrain As List(Of Mesh), ByRef A As Object) 
    Dim nlast As Integer = pts.count - 1
    Dim vecz As New Vector3d
    Dim meshes As IEnumerable(Of Mesh) = terrain
    Dim points As IEnumerable(Of Point3d) = pts
    Dim ptie As IEnumerable(Of Point3d)
    Dim ptms As New List (Of Point3d)

    vecz.X = 0
    vecz.Y = 0
    vecz.Z = 1

    For i As Integer = 0 To nlast
      ptie = Intersect.intersection.ProjectPointsToMeshes(meshes, points, vecz, 0.001)
    Next

    ptms=ptie

    A = ptms

 End Sub

Typing this from the top of my head:

 

Private Sub RunScript(ByVal pts As List(Of Point3d), _

                               ByVal terrain As List(Of Mesh), _

                               ByRef A As Object) 

  Dim vecz As New Vector3d(0,0,1)

  Dim ptms As New List (Of Point3d)

 

  For i As Integer = 0 To pts.count - 1

    Dim pp As Point3d() = Intersect.intersection.ProjectPointsToMeshes( _

                                      terrain, pts, vecz, 0.001)

    If (pp IsNot Nothing) Then ptms.AddRange(pp)
  Next

 

  A = ptms

End Sub

 

Though I do wonder what good that loop is doing. It's not like you're changing anything inside the loop, it always runs the same data. So I think your ptms probably contains the same data over and over again. I'd just remove the loop altogether and place a single call to ProjectPointsToMeshes()

 

To elaborate on my code, List(Of Whatever) implements the IEnumerable(Of Whatever) interface. So when a function requests an input of type IEnumerable(Of Whatever) and you have a List(Of Whatever), you can just plug it in directly.

 

ProjectPointsToMeshes() returns an array of Point3d structures. Now, Point3d() also implements IEnumerable(Of Point3d), so you can safely assign it to your ptie variable. But you cannot assign ptie (IEnumerable(Of Point3d)) to ptms (List (Of Point3d)) because that would require a conversion in the wrong direction. You see Lists are far more specific than IEnumerables. And although every Mitsubishi Colt is a car, not every car is a Mitsubishi Colt.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thank you so much for your detailed explanation.

 

I will try it again.

 

Jihoon

So, I rediscovered this thread in an attempt to join a couple of curves.

 

In order to do so, as you very well know, I need an IEnumerable.

 

I thought this would work...

 

Dim crvList as New List (Of Curve)

crvList.Add(tCrv)

crvList.Add(bCrv)

 

Dim jCurve as Curve = Rhino.Geometry.Curve.JoinCurves(crvList)

 

But, when I do this, I get some sort of 1-dimensional array error message.  I of course cannot tell you exactly what it says because my German is not that good.

 

I got the part about Lists implementing the interface of IEnumerable, and it made sense until I tried to this.

 

Can someone please explain this to me!?!

 

JoinCurves returns an array of curves, so you need to declare jCurve as such:

Dim jCurve As Curve() = Rhino.Geometry.Curve.JoinCurves(crvList)

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

thanks!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service