Grasshopper

algorithmic modeling for Rhino

Hi, 

Could anyone tell me how I can write a piece of code to read the "lines" (in my grasshopper definition) as an input, and give me the "coordinates (x, y, Z) of "Start" and "End" point of each line?

So far  I have the attached code.

It do really appreciate any help

Thanks

Views: 5535

Attachments:

Replies to This Discussion

One way:

Private Sub RunScript(ByVal x As Curve, ByRef A As Object, ByRef B As Object, ByRef C As Object, ByRef D As Object, ByRef E As Object)

Dim x1 As Double = x.GetLength

A = x.PointAt(0)
B = x.PointAt(x1)

C = x.PointAt(0).x
D = x.PointAt(0).y
E = x.PointAt(0).z

End Sub

I didnt look at your files, and I doubt your outputs would be anything like this, but hopefully you can get what you want from this.

I see now that I've looked at your file that you are only dealing with lines (not sure why I assumed crvs earlier).

Anyways Ive attached a file that may help you. I'm not sure what you want to do with the coordinates or how you want them formatted (since you can get them by converting the points to a string - plugging them into a panel or whatever). But this will get you start and end points of multiple lines, outputting all the start points in one list and the end points in another.

It also show an example of how to get a list of x values of all the start points and outputs them as a list. If you need to do this for Y and Z and for End points, you should be able to figure it out with this.

Hope this helps.

brian

Attachments:

Line has From and To properties that allow you to get/set the end-points. Curve has PointAtStart and PointAtEnd properties that allow you to get (but not set) the end-points.

Note that PointAt() uses curve parameters, not lengths. So if you want to use that approach, you need to do:

Dim A As Point3d = curve.PointAt(curve.Domain.Min)

Dim B As Point3d = curve.PointAt(curve.Domain.Max)

Or

Dim A As Point3d = curve.PointAtNormalizedLength(0.0)

Dim B As Point3d = curve.PointAtNormalizedLength(1.0)

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Man I scrolled through the curve members in rhino common and somehow I missed pointAtStart and PointAtEnd.

At first glance the curves i tested looked correct but now that I've zoomed in I see that the end points I was making weren't end points at all! My apologies to M_Nick (and david for having to jump in!)

Dale Lear (our resident Math genius in Seattle) always tells us that it's best to set the domain of a curve from 0.0 to length when you create a new one. I think it's because nurbs sampling algorithms work better when this is the case. This is why you'll often find that Curve.PointAt(0.0) will return a point at the start and Curve.PointAt(Curve.GetLength()) will return a point at the end. However as soon as you modify the shape of a curve you most likely change the length, but often the domain remains the same.

Of course you can always reparameterize curves to give them a new domain, in which case you know for sure what parameters represent the start and end.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thanks Brian and David for your explanation.

I tried "Line.From" and "Line.To" and they work fine to find the start and end points of the line. 

But there is another problem. Through the following code, I get the start points and end points of lines, in the output list there are duplicates of points and I don't know why. For example if I input 3 lines, instead of 6 points as the output, it gives me 17 points. How can I solve this problem?

Protected Overrides Sub RegisterInputParams(ByVal pManager As GH_Component.GH_InputParamManager)

pManager.Register_LineParam("Line", "L", "my lines")

End Sub

-----------------------

Protected Overrides Sub RegisterOutputParams(ByVal pManager As GH_Component.GH_OutputParamManager)
pManager.Register_PointParam("point", "sp", "my points")

End Sub

----------------------

Protected Overrides Sub SolveInstance(ByVal DA As IGH_DataAccess)

Dim Edge As Rhino.Geometry.Line = Rhino.Geometry.Line.Unset
If (Not DA.GetData(0, Edge)) Then Return

StartPointList.Add(Edge.From)

EndPointList.Add(Edge.To)

DA.SetDataList(0, StartPointList)

DA.SetDataList(0, EndPointList)

  End Sub

-------------------------

In this case to pass a list I used DA.SetDatalist and also I tried this :

For i = 0 To StartPointList.Count - 1

DA.SetData(0, StartPointList.Item(i))

Next i

For i = 0 To EndPointList.Count - 1

DA.SetData(0, EndPointList.Item(i))

Next i

in this case, it gives me just End points!!! of course without any duplication of points. 

I really appreciate your help!

Nick

Dim Edge As Rhino.Geometry.Line = Rhino.Geometry.Line.Unset
If (Not DA.GetData(0, Edge)) Then Return

Dim points As New List(Of Point3d)

points.Add(Edge.From)

points.Add(Edge.To)

DA.SetDataList(0, points)

You should only call SetData, SetDataList or SetDataTree once for every iteration of SolveInstance(). Thus, you need to collect all your points into a single list before you assign that to an output parameter.

If you call SetData multiple times then the data that was assigned the previous time will be overwritten.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thanks David, 

But I need to have both lists of Endpoints and Startpoints separately!

Also, I tried your solution but it still gives me duplicate points in output!

Then you need to register two output parameters. But you can use SetData on both.

Dim Edge As Rhino.Geometry.Line = Rhino.Geometry.Line.Unset
If (Not DA.GetData(0, Edge)) Then Return

DA.SetData(0, Edge.From)

DA.SetData(1, Edge.To)

--

David Rutten

david@mcneel.com

Poprad, Slovakia

David, 

My problem solved for the List Input. But when I want to input a Tree I get the cast error! 

What is wrong in my code?

Thanks a lot!!!

-Protected Overrides Sub RegisterInputParams(ByVal pManager As GH_Component.GH_InputParamManager)
pManager.Register_LineParam("Line", "LT", "my lines",
Grasshopper.Kernel.GH_ParamAccess.tree)

-Protected Overrides Sub RegisterOutputParams(ByVal pManager As GH_Component.GH_OutputParamManager)
pManager.Register_PointParam("point", "P", "my points")

-

Protected Overrides Sub SolveInstance(ByVal DA As IGH_DataAccess)


Dim Edge As Rhino.Geometry.Line = Rhino.Geometry.Line.Unset

Dim edgetree As New Data.GH_Structure(Of Grasshopper.Kernel.Types.GH_Line)

If (Not DA.GetDataTree(0, edgetree)) Then Return

For Each Edge In edgetree
Pointlist.Add(Edge.From)
Pointlist.Add(Edge.To)

Next
DA.SetDataList(0, Pointlist)

Attachments:

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service