McNeel Wiki
How To: Insert a Block
edit · print · help · all topics
Main Pages

AccuRender

Bongo

Brazil r/s

Developer

Flamingo

Penguin

Rhino Blogs

Rhino

Rhino Labs

Search

Languages

Česky

Deutsch

English

Español

Français

Italiano

Polish

日本語

한국어

中文(繁體)

 
.
DeveloperC++, .NET
SummaryDemonstrates how to insert a block instance at a user specified location.

C++

  class CGetBlockInsertPoint : public CRhinoGetPoint
  {
  public:
    CGetBlockInsertPoint();
    bool CalculateTransform( CRhinoViewport& vp,
                             const ON_3dPoint& pt,
                             ON_Xform& xform );


    void OnMouseMove( CRhinoViewport& vp, UINT flags,
                      const ON_3dPoint& pt, const CPoint* p );


    void DynamicDraw(HDC hdc, CRhinoViewport& vp, const ON_3dPoint& pt);


  public:
    ON_SimpleArray<const CRhinoObject*> m_block_objects;
    ON_Xform m_xform;
    bool m_draw;
  }; 


  CGetBlockInsertPoint::CGetBlockInsertPoint()
  {
    m_xform.Identity();
    m_draw = false;
  }


  bool CGetBlockInsertPoint::CalculateTransform(
           CRhinoViewport& vp,
           const ON_3dPoint& pt,
           ON_Xform& xform )
  {
    ON_3dVector v = pt - BasePoint();
    xform.Translation( v );
    return true;
  }


  void CGetBlockInsertPoint::OnMouseMove(
           CRhinoViewport& vp,
           UINT flags,
           const ON_3dPoint& pt,
           const CPoint* p 
           )
  {
    m_draw = CalculateTransform( vp, pt, m_xform );
    if( !m_draw )
      m_xform.Identity();
    CRhinoGetPoint::OnMouseMove( vp, flags, pt, p );
  }


  void CGetBlockInsertPoint::DynamicDraw(
           HDC hdc, 
           CRhinoViewport& vp,
           const ON_3dPoint& pt
           )
  {
    ON_Color saved_color = vp.DrawColor();


    int count = m_block_objects.Count();
    if( m_draw && count > 0 )
    {
      ON_Xform saved_model_xform;
      vp.GetModelXform( saved_model_xform );
      vp.SetModelXform( m_xform );


      for( int i = 0; i <count; i++ )
      {
        const CRhinoObject* obj = m_block_objects[i];
        if( obj )
        {
          vp.SetDrawColor( obj->ObjectDrawColor(TRUE) );
          obj->Draw( vp );
          if( vp.InterruptDrawing() )
            break;
        }
      }
      vp.SetModelXform( saved_model_xform );
    }


    vp.SetDrawColor( saved_color );
    CRhinoGetPoint::DynamicDraw( hdc, vp, pt );
  }


  CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
  {
    // Prompt for a block to insert
    CRhinoGetString gs;
    gs.SetCommandPrompt( L"Name of block to insert" );
    gs.GetString();
    if( gs.CommandResult() != CRhinoCommand::success )
      return gs.CommandResult();


    // Validate the return string
    ON_wString block_name = gs.String();
    block_name.TrimLeftAndRight();
    if( block_name.IsEmpty() )
      return CRhinoCommand::cancel;


    // Find the specified block
    CRhinoInstanceDefinitionTable& block_table = 
        context.m_doc.m_instance_definition_table;
    int block_index = block_table.FindInstanceDefinition( block_name );
    if( block_index < 0 )
    {
      ::RhinoApp().Print( L"Unable to insert \"%s\". Block not found.\n", 
                        block_name );
      return CRhinoCommand::cancel;
    }


    // Obtain the block definition
    const CRhinoInstanceDefinition* block = block_table[block_index];
    if( !block || block->IsDeleted() )
      return CRhinoCommand::failure;


    // Prompt the user to pick an insertion point.
    CGetBlockInsertPoint gp;
    gp.SetCommandPrompt( L"Insertion point" );
    gp.SetBasePoint( ON_origin );
    // Add the block definition geometry to our getpoint so we
    // can dynamically draw the stuff...
    block->GetObjects( gp.m_block_objects );
    gp.GetPoint();
    if( gp.CommandResult() != CRhinoCommand::success )
      return gp.CommandResult();


    // Calculate the xform one more time (to be accurate)
    ON_Xform xform;
    gp.CalculateTransform( gp.View()->Viewport(), gp.Point(), xform );


    // Add the block to Rhino
    CRhinoInstanceObject* obj = block_table.AddInstanceObject( block_index, xform );
    if( obj )
    {
      context.m_doc.Redraw();
      return CRhinoCommand::success;
    }


    ::RhinoApp().Print( L"Error creating block reference.\n" );
    return CRhinoCommand::failure;
  } 

VB.NET (Rhino 4)

  Class CGetBlockInsertPoint
    Inherits MRhinoGetPoint


    Public m_block_objects As System.Collections.Generic.List(Of IRhinoObject)
    Public m_xform As OnXform
    Public m_draw As Boolean


    Public Sub New()
      m_block_objects = New System.Collections.Generic.List(Of IRhinoObject)
      m_xform = New OnXform()
      m_xform.Identity()
      m_draw = False
    End Sub


    Public Function CalculateTransform(ByVal pt As IOn3dPoint,_
                                       ByRef xform As OnXform) As Boolean
      Dim v As On3dVector = New On3dPoint(pt) - BasePoint()
      xform.Translation(v)
      Return True
    End Function


    Public Overrides Sub OnMouseMove(ByVal vp As MRhinoViewport,_
                                     ByVal nFlags As UInteger,_
                                     ByVal point As IOn3dPoint,_
                                     ByVal view_wnd_point As System.Drawing.Point)
      m_draw = CalculateTransform(point, m_xform)
      If (Not m_draw) Then m_xform.Identity()
      MyBase.OnMouseMove(vp, nFlags, point, view_wnd_point)
    End Sub


    Public Overrides Sub DynamicDraw(ByVal hdc As System.IntPtr,_
                                     ByVal vp As MRhinoViewport,_
                                     ByVal pt As IOn3dPoint)
      Dim saved_color As OnColor = vp.DrawColor()
      Dim count As Integer = m_block_objects.Count
      If (m_draw And count > 0) Then
        Dim saved_model_xform As New OnXform()
        vp.GetModelXform(saved_model_xform)
        vp.SetModelXform(m_xform)


        For Each obj As IRhinoObject In m_block_objects
          vp.SetDrawColor(CType(obj.ObjectDrawColor(True), UInteger))
          obj.Draw(vp.DisplayPipeline())
          If (vp.InterruptDrawing()) Then Exit For
        Next
        vp.SetModelXform(saved_model_xform)
      End If


      vp.SetDrawColor(CType(saved_color, UInteger))
      MyBase.DynamicDraw(hdc, vp, pt)
    End Sub
  End Class




  Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext)_
    As RMA.Rhino.IRhinoCommand.result
    ' Prompt for a block to insert
    Dim gs As New MRhinoGetString()
    gs.SetCommandPrompt("Name of block to insert")
    gs.GetString()
    If (gs.CommandResult() <> IRhinoCommand.result.success) Then Return gs.CommandResult()


    ' Validate the return string
    Dim block_name As String = gs.String().Trim()
    If (String.IsNullOrEmpty(block_name)) Then Return IRhinoCommand.result.cancel


    ' Find the specified block
    Dim block_table As MRhinoInstanceDefinitionTable =_
       context.m_doc.m_instance_definition_table
    Dim block_index As Integer = block_table.FindInstanceDefinition(block_name)
    If (block_index < 0) Then
      RhUtil.RhinoApp.Print("Unable to insert " + block_name + ". Block not found." + vbCrLf)
      Return IRhinoCommand.result.cancel
    End If


    ' Obtain the block definition
    Dim block As IRhinoInstanceDefinition = block_table(block_index)
    If (block Is Nothing Or block.IsDeleted()) Then Return IRhinoCommand.result.failure


    ' Prompt the user to pick an insertion point.
    Dim gp As New CGetBlockInsertPoint()
    gp.SetCommandPrompt("Insertion point")
    gp.SetBasePoint(OnUtil.On_origin)
    ' Add the block definition geometry to our getpoint so we
    ' can dynamically draw the stuff...


    Dim block_objects As IRhinoObject() = Nothing
    block.GetObjects(block_objects)
    If (block_objects Is Nothing) Then
      Return IRhinoCommand.result.failure
    End If
    gp.m_block_objects.AddRange(block_objects)
    gp.GetPoint()
    If (gp.CommandResult() <> IRhinoCommand.result.success) Then Return gp.CommandResult()


    ' Calculate the xform one more time (to be accurate)
    Dim xform As New OnXform()
    gp.CalculateTransform(gp.View().ActiveViewport(), gp.Point(), xform)


    ' Add the block to Rhino
    Dim obj As MRhinoInstanceObject = block_table.CreateInstanceObject(block_index, xform)
    If (obj IsNot Nothing) Then
      context.m_doc.Redraw()
      Return IRhinoCommand.result.success
    End If


    RhUtil.RhinoApp.Print("Error creating block reference." + vbCrLf)
    Return IRhinoCommand.result.failure
  End Function

C# (Rhino 4)

  public class CGetBlockInsertPoint : MRhinoGetPoint
  {
    public System.Collections.Generic.List<IRhinoObject> m_block_objects;
    public OnXform m_xform;
    public bool m_draw;


    public CGetBlockInsertPoint()
    {
      m_block_objects = new System.Collections.Generic.List<IRhinoObject>();
      m_xform = new OnXform();
      m_xform.Identity();
      m_draw = false;
    }


    public bool CalculateTransform(IOn3dPoint pt, OnXform xform)
    {
      On3dVector v = new On3dPoint(pt) - BasePoint();
      xform.Translation(v);
      return true;
    }


    public override void OnMouseMove(MRhinoViewport vp, uint nFlags,
                                     IOn3dPoint point,
                                     System.Drawing.Point view_wnd_point)
    {
      m_draw = CalculateTransform(point, m_xform);
      if( !m_draw)
        m_xform.Identity();
      base.OnMouseMove(vp, nFlags, point, view_wnd_point);
    }


    public override void  DynamicDraw(System.IntPtr hdc,
                                      MRhinoViewport vp,
                                      IOn3dPoint pt)
    {
      OnColor saved_color = vp.DrawColor();
      int count = m_block_objects.Count;
      if(m_draw && count > 0)
      {
        OnXform saved_model_xform = new OnXform();
        vp.GetModelXform(ref saved_model_xform);
        vp.SetModelXform(m_xform);


        foreach (IRhinoObject obj in m_block_objects)
        {
          vp.SetDrawColor( obj.ObjectDrawColor(true) );
          obj.Draw( vp.DisplayPipeline());
          if( vp.InterruptDrawing() )
            break;
        }
        vp.SetModelXform(saved_model_xform);
      }
      vp.SetDrawColor( saved_color);
             base.DynamicDraw(hdc, vp, pt);
    }
  }






  public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
  {
    //Prompt for a block to insert
    MRhinoGetString gs = new MRhinoGetString();
    gs.SetCommandPrompt("Name of block to insert");
    gs.GetString();
    if( gs.CommandResult() != IRhinoCommand.result.success)
      return gs.CommandResult();


    // Validate the return string
    string block_name = gs.String().Trim();
    if(string.IsNullOrEmpty(block_name))
      return IRhinoCommand.result.cancel;


    // Find the specified block
    MRhinoInstanceDefinitionTable block_table = context.m_doc.m_instance_definition_table;
    int block_index = block_table.FindInstanceDefinition(block_name);
    if(block_index < 0)
    {
      RhUtil.RhinoApp().Print("Unable to insert "+block_name+". Block not found.\n");
      return IRhinoCommand.result.cancel;
    }


    //Obtain the block definition
    IRhinoInstanceDefinition block = block_table[block_index];
    if(block == null || block.IsDeleted())
      return IRhinoCommand.result.failure;


    // Prompt the user to pick an insertion point.
    CGetBlockInsertPoint gp = new CGetBlockInsertPoint();
    gp.SetCommandPrompt("Insertion point");
    gp.SetBasePoint(OnUtil.On_origin);
    // Add the block definition geometry to our getpoint so we
    // can dynamically draw the stuff...
    IRhinoObject[] block_objects = null;
    block.GetObjects(out block_objects);
    if(block_objects == null)
      return IRhinoCommand.result.failure;


    gp.m_block_objects.AddRange(block_objects);
    gp.GetPoint();
    if(gp.CommandResult() != IRhinoCommand.result.success)
      return gp.CommandResult();


    // Calculate the xform one more time (to be accurate)
    OnXform xform = new OnXform();
    gp.CalculateTransform(gp.Point(), xform);


    // Add the block to Rhino
    MRhinoInstanceObject obj = block_table.CreateInstanceObject(block_index, xform);
    if(obj != null)
    {
      context.m_doc.Redraw();
      return IRhinoCommand.result.success;
    }


    RhUtil.RhinoApp().Print("Error creating block reference.\n");
    return IRhinoCommand.result.failure;
  }
rename · changes · history · subscriptions · lost and found · references · file upload