Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:sdksamples:objectlayer [2015/09/14] (current)
Line 1: Line 1:
 +====== How To: Determine an Object'​s Layer Name ======
 +====== C++, .NET ======
 +> **Summary:​** //​Demonstrates how to determine a selected object'​s layer name using the Rhino SDK.//
 +
 +The following sample code demonstrates how to determine a selected object'​s layer name using the Rhino SDK.
 +
 +===== C++ =====
 +<code c++>
 +CRhinoCommand::​result CCommandTest::​RunCommand( const CRhinoCommandContext&​ context )
 +{
 +  CRhinoGetObject go;
 +  go.SetCommandPrompt( L"​Select object"​ );
 +  go.GetObjects( 1, 1 );
 +  if( go.CommandResult() != CRhinoCommand::​success )
 +    return go.CommandResult();​
 +
 +  const CRhinoObjRef&​ objref = go.Object(0);​
 +  const CRhinoObject* object = objref.Object();​
 +  if( !object )
 +    return CRhinoCommand::​failure;​
 +
 +  const CRhinoObjectAttributes&​ attributes = object->​Attributes();​
 +  int layer_index = attributes.m_layer_index;​
 +
 +  const CRhinoLayerTable&​ layer_table = context.m_doc.m_layer_table;​
 +  const CRhinoLayer&​ layer = layer_table[layer_index];​
 +  ON_wString layer_name = layer.LayerName();​
 +  RhinoApp().Print( L"The selected object'​s layer is \"​%s\"​\n",​ layer_name );
 +
 +  return CRhinoCommand::​success;​
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
 +  As IRhinoCommand.result
 +
 +  Dim go As New MRhinoGetObject
 +  go.SetCommandPrompt("​Select object"​)
 +  go.GetObjects(1,​ 1)
 +  If (go.CommandResult() <> IRhinoCommand.result.success) Then
 +    Return go.CommandResult()
 +  End If
 +
 +  Dim objref As IRhinoObjRef = go.Object(0)
 +  Dim obj As IRhinoObject = objref.Object()
 +  If (obj Is Nothing) Then Return IRhinoCommand.result.failure
 +
 +  Dim attributes As IRhinoObjectAttributes = obj.Attributes()
 +  Dim layer_index As Integer = attributes.m_layer_index
 +
 +  Dim layer_table As IRhinoLayerTable = context.m_doc.m_layer_table
 +  Dim layer As IRhinoLayer = layer_table(layer_index)
 +  Dim layer_name As String = layer.LayerName()
 +  Dim msg As String = _
 +      String.Format("​The selected object'​s layer is '​{0}'"​ + vbCrLf, layer_name)
 +  RhUtil.RhinoApp.Print(msg)
 +  Return IRhinoCommand.result.success
 +End Function
 +</​code>​
 +===== C# =====
 +<code c#>
 +public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
 +{
 +  MRhinoGetObject go = new MRhinoGetObject();​
 +  go.SetCommandPrompt("​Select object"​);​
 +  go.GetObjects(1,​ 1);
 +  if(go.CommandResult() != IRhinoCommand.result.success)
 +    return go.CommandResult();​
 +
 +  IRhinoObjRef objref = go.Object(0);​
 +  IRhinoObject obj = objref.Object();​
 +  if(obj == null)
 +    return IRhinoCommand.result.failure;​
 +
 +  IRhinoObjectAttributes attributes = obj.Attributes();​
 +  int layer_index = attributes.m_layer_index;​
 +
 +  IRhinoLayerTable layer_table = context.m_doc.m_layer_table;​
 +  IRhinoLayer layer = layer_table[layer_index];​
 +  string layer_name = layer.LayerName();​
 +  string msg = string.Format("​The selected object'​s layer is '​{0}'​\n",​ layer_name);​
 +  RhUtil.RhinoApp().Print(msg);​
 +  return IRhinoCommand.result.success;​
 +}
 +
 +</​code>​
 +
 +
 +{{tag>​Developer cplusplus dotnet}}
  
developer/sdksamples/objectlayer.txt ยท Last modified: 2015/09/14 (external edit)