Grasshopper

algorithmic modeling for Rhino

Dose anyone have an example of the rhutil.rhinobooleandifference working in a VB component? I can't figure out the arguments, is their a reference that describes these.
To start, I just want to input two Breps and output the resulting Brep.

Views: 954

Replies to This Discussion

You need an array of Breps to subtract from...InBreps0, an array of breps to do the subtracting...InBreps1, and then the tolerance. The inputs after that are all "out" values which means you need to supply them, but the method will replace whatever is in those variables with the output from the operation. There's a boolean value that you need to provide, although you probably won't need it. Then you will need to supply an empty array of OnBreps which is going to be the resulting breps. And lastly you'll need an empty array of ArrayInt, which probably won't be of much use to you.

I'd check out the example on the dotNET wiki. Look it over and see if you have any further questions

http://en.wiki.mcneel.com/default.aspx/McNeel/SdkBooleanDifference
Thanks Damien, I tried your advice and got something to work. I included only a fraction of the VB code from the dotNET wiki and feel like I may have thrown too many parts away. I specified a tolerance because I couldn't get the
Dim tolerance As Double = context.m_doc.AbsoluteTolerance()
to work- context not defined. How is the document tolerance brought into the VB component?
Jeff



Sub RunScript(ByVal x As List(Of OnBrep), ByVal y As List(Of OnBrep))
'Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
'As IRhinoCommand.result
'Dim go As New MRhinoGetObject()
'go.SetCommandPrompt("Select first set of polysurfaces")
'go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.polysrf_object _
' Or IRhinoGetObject.GEOMETRY_TYPE_FILTER.surface_object)
'go.EnablePreSelect(True)
'go.GetObjects(1, 0)
'If (go.CommandResult() <> IRhinoCommand.result.success) Then
' Return go.CommandResult()
'End If


'Dim InBreps0 As New System.Collections.Generic.List(Of IOnBrep)()
'For i As Integer = 0 To go.ObjectCount() - 1
'Dim brep As IOnBrep = go.Object(i).Brep()
'If (brep IsNot Nothing) Then InBreps0.Add(brep)
' Next

Dim InBreps0 As New List(Of OnBrep)
For i As Integer = 0 To x.Count() - 1
InBreps0.Add(x(i))
Next

Dim InBreps1 As New List(Of OnBrep)
For i As Integer = 0 To y.Count() - 1
InBreps1.Add(y(i))
Next


'go.SetCommandPrompt("Select second set of polysurfaces")
'go.EnablePreSelect(False)
'go.EnableDeselectAllBeforePostSelect(False)
'go.GetObjects(1, 0)
'If (go.CommandResult() <> IRhinoCommand.result.success) Then
' Return go.CommandResult()
'End If


'Dim InBreps1 As New System.Collections.Generic.List(Of IOnBrep)()
'For i As Integer = 0 To go.ObjectCount() - 1
' Dim brep As IOnBrep = go.Object(i).Brep()
'If (brep IsNot Nothing) Then InBreps1.Add(brep)
'Next


Dim OutBreps As OnBrep() = Nothing
Dim InputIndexForOutput As New Arrayint()
Dim something_happened As Boolean = False
'Dim tolerance As Double = context.m_doc.AbsoluteTolerance()
Dim tolerance As Double = 0.1


Dim rc As Boolean = RhUtil.RhinoBooleanDifference( _
InBreps0.ToArray(), _
InBreps1.ToArray(), _
tolerance, _
something_happened, _
OutBreps, _
InputIndexForOutput)

A = OutBreps

'If (Not rc Or Not something_happened) Then
' Return IRhinoCommand.result.nothing
'End If


'For i As Integer = 0 To OutBreps.Length - 1
' Dim brep As OnBrep = OutBreps(i)
' If (brep IsNot Nothing) Then
' context.m_doc.AddBrepObject(brep)
' End If
'context.m_doc.Redraw()
'Return IRhinoCommand.result.success
'End Function
End Sub
I didn't read through your code to carefully, but can help you with getting the Rhino document tolerance, try RhUtil.RhinoApp().ActiveDoc().AbsoluteTolerance()

Hope that helps,

Jon
The context actually comes from running this as a command in Rhino, so following Jon's suggestion on retrieving the tolerance will work instead. Short hand it can just be doc.AbsoluteTolerance

I guess as a general note of using the wiki examples, basically toss all of the stuff that winds up being about getters and retrieving objects from the Rhino document...with GH you just don't need it. As well, don't worry about the code surrounding putting objects back into or interacting with the Rhino document...its just not needed.

Specifically in this case, you don't need to recreate an list/array because presumably you already have a list of objects. So, if you comment out all the crap you don't need and clean that up a little bit you'll have the example attached.
Here's the GH definition...
Attachments:
Thanks Damien, that makes a lot of things clearer to me.
I have two more related questions:
The component I am working on is a series of if-then statements which check for the presence or absence of Brep and performs certain Bool-diff based on this. I have been trying the "If x is nothing then", but it dosen't seem to be working.

Sub RunScript(ByVal x As List(Of OnBrep), ByVal y As List(Of OnBrep))
Dim AOut As OnBrep() = Nothing

Dim Indx As New Arrayint()
Dim Boo As Boolean = False
Dim tolr As Double = doc.AbsoluteTolerance

If x Is Nothing Then
'pass the Brep lists unchanged
'this dosen't work
A = x
B = y

Else
'Perform Bool Diff on x and y

RhUtil.RhinoBooleanDifference(x.ToArray(), y.ToArray(), tolr, boo, AOut, Indx)
A = AOut

End If

End Sub

Having a null in my list of input Breps also throws things off, but the clean component removes them. Is there a simple VB code that will perform what the Clean component does?
Jeff
Jeff,

Not 100% sure on this, but I don't think doing somethiing like If x Is Nothing is an effective way to see if you have any information coming in. It requires a little bit of object oriented thinking in order to understand why. X is a type of object called a list. It can have anything inside of it, but it also can simply be a zero length list (similar to a zero length string). If I declare a list like so

Dim myList as New List (of Object)

is it Nothing? The answer is no, because that list is still a valid object even though it doesn't actually contain any data yet. What I think you'd be better off doing is testing whether there's any sort of information within the list. So you could test what the count is and see if its 0.

As to "cleaning up" your Breps, Lists implement a method called Remove which is an easy way to get things out of the list. So you'd have to iterate through the list, find which items are Nothing, then remove them. Keep in mind that you'd have to stay on top of the changing indexes within the list. Again, not tested, but a loop similar to the following should help...throwing it in a function wouldn't be a bad idea.

Dim i as Integer = 0
Dim lastIndex as Integer
lastIndex = Count-1

Do
If x(i) is Nothing Then
x.Remove(i)
lastIndex =-1
Else
i+=1
End If
Loop While i<=lastIndex
After a little snooping I found that lists do support a RemoveAll method, which could probably help here. I tried somewhat quickly to use it but couldn't get it working correctly as I have no idea what a predicate is and was having trouble declaring in. Here's the MSDN page for the RemoveAll method

http://msdn.microsoft.com/en-us/library/wdka673a(VS.80).aspx

EDIT:
Scratch this...In more complex situations it may be useful to use this method, but for this one I'm not sure. To set up a predicate it appears as though you have to set up a function that you pass that is used to evaluate whether the item in the list is kept or not.
thanks Damien. My current work around is to feed the same Brep list as a non-list into another input and use the is nothing as a check that represents the status of the list (for now if the first item in the list is null I can ignore the whole list).

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service