Site Tools


Print Instance Definition Tree

Developer: C++, .NET
Summary: Demonstrates how to print the names of all instance definitions, including objects and sub-instances, in a tree-style format.
NOTICE: The Rhino.NET SDK is deprecated in Rhino 5. This example adapted for the new RhinoCommon SDK is here

Question

How can I print the names of all instance definitions, including their objects and sub-instances, in a tree-style format?

Answer

See the following example code:

C++

CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
{
  const CRhinoInstanceDefinitionTable& idef_table = context.m_doc.m_instance_definition_table;
  int idef_count = idef_table.InstanceDefinitionCount();
  if (idef_count == 0)
  {
    RhinoApp().Print("No instance definitions found.\n");
    return CRhinoCommand::nothing;
  }
 
  ON_wString writer;
  ON_TextLog dump( writer );
  dump.SetIndentSize( 4 );
 
  for (int i = 0; i < idef_count; i++)
    DumpInstanceDefinition( idef_table[i], dump, true );
 
  RhinoApp().Print( L"%s\n", writer );
 
  return CRhinoCommand::success;
}
 
void CCommandTest::DumpInstanceDefinition( const CRhinoInstanceDefinition* idef, ON_TextLog& dump, bool bRoot )
{
  if( idef && ! idef->IsDeleted() )
  {
    ON_wString node;
    if( bRoot )
      node = L"\u2500";
    else
      node = L"\u2514";
    dump.Print(L"%s Instance definition %d = %s\n", node, idef->Index(), idef->Name() );
 
    const int idef_object_count = idef->ObjectCount();
    if( idef_object_count )
    {
      dump.PushIndent();
      for( int i = 0; i < idef->ObjectCount(); i++ )
      {
        const CRhinoObject* obj = idef->Object( i );
        if( obj )
        {
          const CRhinoInstanceObject* iref = CRhinoInstanceObject::Cast( obj );
          if( iref )
            DumpInstanceDefinition( iref->InstanceDefinition(), dump, false ); // Recursive...
          else
            dump.Print(L"\u2514 Object %d = %s\n", i, obj->ShortDescription(false) );
        }
      }
      dump.PopIndent();
    }
  }
}

C#

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  MRhinoInstanceDefinitionTable idef_table = context.m_doc.m_instance_definition_table;
  int idef_count = idef_table.InstanceDefinitionCount();
  if (idef_count == 0)
  {
    RhUtil.RhinoApp().Print("No instance definitions found.\n");
    return IRhinoCommand.result.nothing;
  }
 
  StringWriter writer = new StringWriter();
  OnTextLog dump = new OnTextLog(writer);
  dump.SetIndentSize(4);
 
  for (int i = 0; i < idef_count; i++)
    DumpInstanceDefinition(idef_table[i], ref dump, true);
 
  RhUtil.RhinoApp().Print(String.Format("{0}\n", writer.ToString()));
 
  return IRhinoCommand.result.success;
}
 
private void DumpInstanceDefinition(IRhinoInstanceDefinition idef, ref OnTextLog dump, bool bRoot)
{
  if (idef != null && idef.IsDeleted() == false)
  {
    string node;
    if (bRoot)
      node = "\u2500";
    else
      node = "\u2514";
    dump.Print(String.Format("{0} Instance definition {1} = {2}\n", node, idef.Index(), idef.Name()));
 
    int idef_object_count = idef.ObjectCount();
    if (idef_object_count > 0)
    {
      dump.PushIndent();
      for (int i = 0; i < idef_object_count; i++)
      {
        IRhinoObject obj = idef.Object(i);
        if (obj != null)
        {
          IRhinoInstanceObject iref = MRhinoInstanceObject.ConstCast(obj);
          if (iref != null)
            DumpInstanceDefinition(iref.InstanceDefinition(), ref dump, false); // Recursive...
          else
            dump.Print(String.Format("\u2514 Object {0} = {1}\n", i, obj.ShortDescription(false)));
        }
      }
      dump.PopIndent();
    }
  }
}

VB.NET

Public Overrides Function RunCommand(ByVal context As RMA.Rhino.IRhinoCommandContext) As RMA.Rhino.IRhinoCommand.result
 
  Dim idef_table As MRhinoInstanceDefinitionTable = context.m_doc.m_instance_definition_table
  Dim idef_count As Integer = idef_table.InstanceDefinitionCount()
  If (idef_count = 0) Then
    RhinoApp().Print("No instance definitions found." + vbCrLf)
    Return IRhinoCommand.result.nothing
  End If
 
  Dim writer As New System.IO.StringWriter()
  Dim dump As New OnTextLog(writer)
  dump.SetIndentSize(4)
 
  For i As Integer = 0 To idef_count - 1
    DumpInstanceDefinition(idef_table(i), dump, True)
  Next
 
  RhUtil.RhinoApp().Print(writer.ToString() + vbCrLf)
 
  Return IRhinoCommand.result.success
 
End Function
 
Private Sub DumpInstanceDefinition(ByVal idef As IRhinoInstanceDefinition, ByRef dump As OnTextLog, ByVal bRoot As Boolean)
 
  If (idef IsNot Nothing AndAlso idef.IsDeleted() = False) Then
 
    Dim node As String
    If (bRoot = True) Then
      node = ChrW(&H2500)
    Else
      node = ChrW(&H2514)
    End If
    dump.Print(String.Format("{0} Instance definition {1} = {2}" + vbCrLf, node, idef.Index(), idef.Name()))
 
    Dim idef_object_count As Integer = idef.ObjectCount()
    If (idef_object_count > 0) Then
      dump.PushIndent()
      For i As Integer = 0 To idef_object_count - 1
        Dim obj As IRhinoObject = idef.Object(i)
        If (obj IsNot Nothing) Then
          Dim iref As IRhinoInstanceObject = MRhinoInstanceObject.ConstCast(obj)
          If (iref IsNot Nothing) Then
            DumpInstanceDefinition(iref.InstanceDefinition(), dump, False) ' Recursive...
          Else
            dump.Print(String.Format("{0} Object {1} = {2}" + vbCrLf, ChrW(&H2514), i, obj.ShortDescription(False)))
          End If
        End If
      Next
      dump.PopIndent()
    End If
 
  End If
 
End Sub


developer/sdksamples/printinstancedefinitiontree.txt ยท Last modified: 2020/08/14 (external edit)