English (UK)

Develop Rhinoceros Plug-in PART 4

Come pilotare Rhinoceros dal plug-in
Presento qui i listati dei file .NET del plug-in di base con alcuni comandi funzionanti, presi direttamente dal sito della Mc Neel, in modo da avere qualche esempio su come strutturare l'interfaccia con Rhinoceros.
 
RhinoPlugInBasePlugIn.vb
Public Class RhinoPlugInBasePlugIn Inherits RMA.Rhino.MRhinoUtilityPlugIn
 Public Overrides Function PlugInID() As System.Guid
  Return New System.Guid("{722abf0d-d9d2-4cd7-8637-d9b11148b7ed}")
 End Function

 Public Overrides Function PlugInName() As String
  Return "RhinoPlugInBase"
 End Function

 Public Overrides Function PlugInVersion() As String
  Return "1.0.0.0"
 End Function

 Public Overrides Function OnLoadPlugIn() As Integer
  '''<returns>        
  '''  1 = initialization succeeded, let the plug-in load   
  '''  0 = unable to initialize, don't load plug-in and display an error dialog  
  ''' -1 = unable to initialize, don't load plug-in and do not display an error  
  '''      dialog. Note: OnUnloadPlugIn will not be called       
  '''</returns>    
  Return 1
 End Function

 Public Overrides Sub OnUnloadPlugIn()
 End Sub
End Class
RhinoPlugInBasePlugInAttributes.vb 
Public Class MyPlugIn1Attributes Inherits RMA.Rhino.MRhinoPlugInAttributes
 Public Overrides Function Address() As String
  Return "undefined"
 End Function

 Public Overrides Function Country() As String
  Return "Italy"
 End Function

 Public Overrides Function Email() As String
  Return "Questo indirizzo email è protetto dagli spambots. È necessario abilitare JavaScript per vederlo."
 End Function

 Public Overrides Function Fax() As String
  Return "undefined"
 End Function

 Public Overrides Function Organization() As String
  Return "Tessaro Bruno"
 End Function

 Public Overrides Function Phone() As String
  Return "undefined"
 End Function

 Public Overrides Function UpdateURL() As String
  Return "undefined"
 End Function

 Public Overrides Function Website() As String
  Return "undefined"
 End Function
End Class
AddLayerCommand.vb 
Imports RMA.Rhino Imports RMA.OpenNURBS Imports RMA.Rhino.RhUtil

Public Class AddLayerCommand Inherits RMA.Rhino.MRhinoCommand
 Public Overrides Function CommandUUID() As System.Guid
  Return New Guid("{fe405805-c87d-4445-a48c-6a15a6c70817}")
 End Function

 Public Overrides Function EnglishCommandName() As String
  Return "AddLayerCommand"
 End Function

 Public Overrides Function RunCommand(ByVal context As RMA.Rhino.IRhinoCommandContext) As RMA.Rhino.IRhinoCommand.result
  Dim layer_table As MRhinoLayerTable = context.m_doc.m_layer_table
  Dim unused_name As String = ""
  layer_table.GetUnusedLayerName(unused_name)

  Dim gs As New MRhinoGetString()
  gs.SetCommandPrompt("Name of layer to add")
  gs.SetDefaultString(unused_name)
  gs.AcceptNothing(True)
  gs.GetString()
  If (gs.CommandResult() <> IRhinoCommand.result.success) Then
   Return gs.CommandResult()
  End If

  Dim layer_name As String = gs.String()
  layer_name = layer_name.Trim()
  If (String.IsNullOrEmpty(layer_name)) Then
   RhUtil.RhinoApp().Print("Layer name cannot be blank." + vbCrLf)
   Return IRhinoCommand.result.cancel
  End If

  If (RhUtil.RhinoIsValidName(layer_name) = 0) Then
   RhUtil.RhinoApp().Print(layer_name + " is not a valid layer name." + vbCrLf)
   Return IRhinoCommand.result.cancel
  End If

  Dim layer_index As Integer = layer_table.FindLayer(layer_name)
  If (layer_index >= 0) Then
   Dim msg As String = "A layer with the name " + layer_name + " already exists." + vbCrLf
   RhUtil.RhinoApp().Print(msg)
   Return IRhinoCommand.result.cancel
  End If

  Dim layer As New OnLayer()
  layer.SetLayerName(layer_name)
  layer_index = layer_table.AddLayer(layer)
  If (layer_index < 0) Then
   RhUtil.RhinoApp().Print(String.Format("Unable to add {0} layer." + vbCrLf, layer_name))
   Return IRhinoCommand.result.failure
  End If
  Return IRhinoCommand.result.success
 End Function
End Class 
ExtendSurfaceCommand.vb 
Imports RMA.Rhino Imports RMA.OpenNURBS Imports RMA.Rhino.RhUtil

Public Class ExtendSurfaceCommand Inherits RMA.Rhino.MRhinoCommand
 Public Overrides Function CommandUUID() As System.Guid
  Return New Guid("{1120623a-268f-4986-a9e5-dd287763159c}")
 End Function

 Public Overrides Function EnglishCommandName() As String
  Return "ExtendSurfaceCommand"
 End Function

 Public Overrides Function RunCommand(ByVal context As RMA.Rhino.IRhinoCommandContext) As RMA.Rhino.IRhinoCommand.result
  Dim go As New MRhinoGetObject
  go.SetCommandPrompt("Select edge of surface to extend")
  go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.edge_object)
  go.SetGeometryAttributeFilter(IRhinoGetObject.GEOMETRY_ATTRIBUTE_FILTER.edge_curve)
  go.GetObjects(1, 1)
  If (go.CommandResult() <> IRhinoCommand.result.success) Then
   Return go.CommandResult()
  End If

  Dim objref As IRhinoObjRef = go.Object(0)
  Dim srf As IOnSurface = objref.Surface()
  If (srf Is Nothing) Then

   RhUtil.RhinoApp().Print("Unable to extend polysurfaces." + vbCrLf)
   Return IRhinoCommand.result.nothing
  End If

  Dim brep As IOnBrep = objref.Brep()
  Dim face As IOnBrepFace = objref.Face()
  If (brep Is Nothing Or face Is Nothing) Then Return IRhinoCommand.result.failure
  If (face.m_face_index < 0) Then Return IRhinoCommand.result.failure

  If (Not brep.IsSurface()) Then
   RhUtil.RhinoApp().Print("Unable to extend trimmed surfaces." + vbCrLf)
   Return IRhinoCommand.result.nothing
  End If

  Dim trim As IOnBrepTrim = objref.Trim()
  If (trim Is Nothing) Then Return IRhinoCommand.result.failure

  Dim edge_index As IOnSurface.ISO = trim.m_iso
  Dim dir As Integer = edge_index Mod 2
  If (srf.IsClosed(1 - dir)) Then
   RhUtil.RhinoApp().Print("Unable to extend surface at seam." + vbCrLf)
   Return IRhinoCommand.result.nothing
  End If

  If (edge_index < IOnSurface.ISO.W_iso Or edge_index > IOnSurface.ISO.N_iso) Then
   RhUtil.RhinoApp().Print("Selected edge must be an underlying surface edge." + vbCrLf)
   Return IRhinoCommand.result.nothing
  End If

  Dim myface As OnSurface = srf.DuplicateSurface()
  If (myface Is Nothing) Then
   Return IRhinoCommand.result.failure
  End If

  Dim rc As Boolean = RhUtil.RhinoExtendSurface(myface, edge_index, 5.0, True)
  If (rc) Then
   Dim mybrep As New OnBrep()
   mybrep.Create(myface)
   Dim obj As New MRhinoBrepObject()
   obj.SetBrep(mybrep)
   context.m_doc.ReplaceObject(New MRhinoObjRef(objref.Object()), obj)
   context.m_doc.Redraw()
  End If
  Return IRhinoCommand.result.success
 End Function
End Class 
LanguageCommand.vb 
Imports RMA.Rhino Imports RMA.OpenNURBS Imports RMA.Rhino.RhUtil

Public Class LanguageCommand Inherits RMA.Rhino.MRhinoCommand
 Public Overrides Function CommandUUID() As System.Guid
  Return New Guid("{d8dda157-454c-4e9b-a740-7df8815fe100}")
 End Function

 Public Overrides Function EnglishCommandName() As String
  Return "LanguageCommand"
 End Function

 Public Overrides Function RunCommand(ByVal context As RMA.Rhino.IRhinoCommandContext) As RMA.Rhino.IRhinoCommand.result         Dim settings As MRhinoAppSettings = RhUtil.RhinoApp.AppSettings      
  Dim appearance As IRhinoAppAppearanceSettings = settings.AppearanceSettings()
  Dim id As Integer = CType(appearance.m_language_identifier, Integer)
  Dim culture As New System.Globalization.CultureInfo(id)
  RhUtil.RhinoApp.Print("The current language is " + culture.EnglishName + vbCrLf)
  Return IRhinoCommand.result.success
 End Function
End Class 
Si possono cercare nella guida di riferimento gli altri comandi impiegabili nei plug-in. Il resto è la classica programmazione in Visual Basic .NET. Non sto qui a pubblicare una guida a tale linguaggio, in rete ne esistono già di ottime.