Site Tools


Differences

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

Link to this comparison view

developer:sdksamples:rotatecenter [2015/09/14] (current)
Line 1: Line 1:
 +====== Rotate Objects Around Center ======
 +====== C++ ======
 +> **Summary:​** //​Demonstrates how rotate objects around the center point of their bounding box using the Rhino SDK.//
 +
 +=====Example=====
 +
 +The following sample code demonstrates how rotate objects around the center point of their bounding box using the Rhino SDK.
 +
 +===== C++ =====
 +<code c++>
 +
 +CRhinoCommand::​result CCommandTest::​RunCommand( const CRhinoCommandContext&​ context )
 +{
 +  // Select objects to rotate
 +  CRhinoGetObject go;
 +  go.SetCommandPrompt( L"​Select objects to rotate"​ );
 +  go.GetObjects( 1, 0 );
 +  if( go.CommandResult() != success )
 +    return go.CommandResult();​
 +
 +  // Rotation angle (in degrees)
 +  CRhinoGetNumber gn;
 +  gn.SetCommandPrompt( L"​Rotation angle" );
 +  gn.SetDefaultNumber( m_angle );
 +  gn.GetNumber();​
 +  if( gn.CommandResult() != success )
 +    return gn.CommandResult();​
 +
 +  // Validate input
 +  double angle = gn.Number();​
 +  if( angle == 0 )
 +    return nothing;
 +
 +  m_angle = angle;
 +
 +  // Get the active view's construction plane
 +  ON_Plane plane = RhinoActiveCPlane();​
 +
 +  // Do not split objects that get kinky 
 +  // when they are transformed.
 +  CRhinoKeepKinkySurfaces keep_kinky_srfs;​
 +
 +  int i;
 +  for( i = 0; i < go.ObjectCount();​ i++ )
 +  {
 +    // Get an object reference
 +    const CRhinoObjRef&​ ref = go.Object(i);​
 +
 +    // Get the real object
 +    const CRhinoObject* obj = ref.Object();​
 +    if( !obj )
 +      continue;
 +
 +    // Get the object'​s tight bounding box
 +    ON_BoundingBox bbox;
 +    if( !obj->​GetTightBoundingBox(bbox,​ false, 0) )
 +      continue;
 +
 +    // Create transformation matrix
 +    ON_Xform xform;
 +    xform.Rotation( m_angle * ON_PI / 180.0, plane.zaxis,​ bbox.Center() );
 +
 +    // Transform the object
 +    context.m_doc.TransformObject( obj, xform, true, true, true );
 +  }
 +
 +  context.m_doc.Redraw();​
 +
 +  return success;
 +}
 +
 +</​code>​
 +===== C# =====
 +<code c#>
 +
 +public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
 +{
 +  MRhinoGetObject go = new MRhinoGetObject();​
 +  go.SetCommandPrompt("​Select objects to rotate"​);​
 +  go.GetObjects(1,​ 0);
 +  if (go.CommandResult() != IRhinoCommand.result.success)
 +    return go.CommandResult();​
 +
 +  MRhinoGetNumber gn = new MRhinoGetNumber();​
 +  gn.SetCommandPrompt("​Rotation angle"​);​
 +  gn.GetNumber();​
 +  if (gn.CommandResult() != IRhinoCommand.result.success)
 +    return gn.CommandResult();​
 +
 +  double angle = gn.Number();​
 +  if (angle == 0.0)
 +    return IRhinoCommand.result.nothing;​
 +
 +  OnPlane plane = new OnPlane(RhUtil.RhinoActiveCPlane());​
 +
 +  MRhinoKeepKinkySurfaces keep_kinky_srfs;​
 +
 +  for (int i = 0; i < go.ObjectCount();​ i++)
 +  {
 +    MRhinoObjRef objref = go.Object(i);​
 +
 +    IRhinoObject obj = objref.Object();​
 +    if (obj == null)
 +      continue;
 +
 +    OnBoundingBox bbox = new OnBoundingBox();​
 +    if (!obj.GetTightBoundingBox(ref bbox, false))
 +      continue;
 +
 +    OnXform xform = new OnXform();
 +    xform.Rotation(angle * OnUtil.On_PI / 180.0, plane.zaxis,​ bbox.Center());​
 +
 +    context.m_doc.TransformObject(obj,​ xform, true, true, true);
 +  }
 +
 +  context.m_doc.Redraw();​
 +
 +  return IRhinoCommand.result.success;​
 +}
 +</​code>​
 +
 +
 +{{tag>​Developer cplusplus}}
  
developer/sdksamples/rotatecenter.txt ยท Last modified: 2015/09/14 (external edit)