Grasshopper

algorithmic modeling for Rhino

Does anyone know how to write some code to navigate, center and zoom in on a selected component? I have code that allows me to select the component but I cant see methods that will allow me to then navigate and zoom in to the selected component.

 

The code below allows me to select the component on the canvas

 

Dim ghCanvas As Grasshopper.GUI.Canvas.GH_Canvas

ghCanvas = Grasshopper.GH_InstanceServer.ActiveCanvas

Dim ghObjData As New Grasshopper.Kernel.GH_RelevantObjectData(ghObject.Attributes.Bounds.Location)

ghObjData.CreateObjectData(ghObject)

Dim ghSelectionState As Grasshopper.Kernel.GH_SelectionState

ghSelectionState = ghDocument.Select(ghObjData)

                

Views: 2054

Replies to This Discussion

Come on, anyone?

Have a look at Grasshopper.GUI.Canvas.GH_NamedView class. It has several constructors, one of which is bound to suit you. When you're done, call SetToViewport().

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thanks for the reply.

 

I think i sorted it, seems to work fine now, this is what i did:

 

Dim sysPoint As System.Drawing.Point

sysPoint.X = CInt(CInt(ghObject.Attributes.Bounds.Right - ghObject.Attributes.Bounds.Left) / 2) + CInt(ghObject.Attributes.Bounds.Left)

sysPoint.Y = CInt(CInt(ghObject.Attributes.Bounds.Top - ghObject.Attributes.Bounds.Bottom) / 2) + CInt(ghObject.Attributes.Bounds.Bottom) ghCanvas.Viewport.MidPoint = sysPoint ghCanvas.Refresh()

 

Is there a better way?

 

 

Hi david, im trying to avoid using events. Im still ahving trouble rescaling to the corrcet zoom factor.

 

I have selected my component on the canvas and i have set the viewport to teh center of the component.

however, i just cannot find the correct scale factor to use, see the following:

 
  ghCanvas.Viewport.Zoom(True) = (ghCanvas.Viewport.VisibleRegion.Width / ghDocument.BoundingBox(True).Width) 

 

also tried

 
  Dim sysRectangleExtent As System.Drawing.RectangleF = ghDocument.BoundingBox(False) 
ghCanvas.Viewport.Zoom(True) = sysRectangleExtent.Width / ghDocument.BoundingBox(True).Width 

I dont really understand the maths behind the value for Zoom,

 

thanks

 

Steve

Hi Steve,

zoom is the normalised value of the percentage you see in the GUI. Basically, it's the scaling factor applied to the display. So a zoom of 1.0 is pixel-per-pixel perfect, a zoom factor of 0.5 (50%) will make everything look half as big, a zoom factor of 3.0 (300%) will make everything look 3 times bigger.

The code I use in the Variable Parameter tip looks like this:

Dim box As RectangleF = Attributes.Bounds
Dim pt As New PointF(box.X + 0.5F * box.Width, _

                               box.Top + 0.5F * box.Height)
Dim view As New GH_NamedView("zui_zoom", pt, _

                                                10.0F, GH_NamedViewType.center)

view.SetToViewport(canvas, 1200)

I use a fixed zoom factor of 10.0 but you'll need to compute a variable zoom level each time. Basically, the zoom level you want it the factor you need to multiply the Attributes.Bounds with in order to make them as big as the viewport.ScreenPort So if the ScreenPort is 950 pixels wide and 720 pixels high and the Bounds of the attributes are 55 pixels wide and 75 pixels high, the zoom factor to make the width and height fit exactly would be:

Dim zoomW As Single = Convert.ToSingle(950/55)

Dim zoomH As Single = Convert.ToSingle(720/75)

Dim zoom As Single = Math.Min(zoomW, zoomH)

Where zoom is the lowest of the two. I hope my math isn't wrong, I didn't test this and only just woke up... But that's the basic idea.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thank you David, I think this will help tremendously

 

Steve

David, awesome, your code worked, heres my final code.....

 

Dim sysMidPoint As System.Drawing.Point Dim sysTargetPoint As System.Drawing.Point sysTargetPoint.X = CInt(ghDocument.BoundingBox(True).Location.X) sysTargetPoint.Y = CInt(ghDocument.BoundingBox(True).Location.Y) sysMidPoint.X = CInt(sysTargetPoint.X + (ghDocument.BoundingBox(True).Width / 2)) sysMidPoint.Y = CInt(sysTargetPoint.Y + (ghDocument.BoundingBox(True).Height / 2)) Dim sZoomW As Single = Convert.ToSingle(ghCanvas.Viewport.ScreenPort.Width / ghDocument.BoundingBox(True).Width) Dim sZoomH As Single = Convert.ToSingle(ghCanvas.Viewport.ScreenPort.Height / ghDocument.BoundingBox(True).Height) Dim sZoom As Single = Math.Min(sZoomW, sZoomH) Dim ghNamedView As New Grasshopper.GUI.Canvas.GH_NamedView ghNamedView.Point = sysMidPoint ghNamedView.Zoom = sZoom ghNamedView.Type = Grasshopper.GUI.Canvas.GH_NamedViewType.center ghNamedView.SetToViewport(ghCanvas, 1200) ghCanvas.Refresh()

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service