Drawings SDK Developer Guide > Working with .dwg Files > Working with Entities > Creating a Custom Entity > Implementing Transformation Functions
Implementing Transformation Functions

The transformBy() function is used to apply a 3D transformation matrix to an entity. getTransformedCopy() is used to create a non-database resident transformed copy of entity. It's default implementation clones the entity and calls transformBy(). In custom class it can be overridden. For example OdDbText::transformBy() requires only uni-scaled matrix. But OdDbText::getTransformedCopy() works with non-uniscaled matrix and modifies text width factor.


virtual OdResult OdDbEntity::transformBy( const OdGeMatrix3d& xform );
virtual OdResult OdDbEntity::getTransformedCopy( const OdGeMatrix3d& xform, OdDbEntityPtr& pCopy ) const;

For example, the smiley entity implements:


OdResult AsdkSmiley::transformBy(const OdGeMatrix3d& xform)
{
   assertWriteEnabled();
   
   // Transform the center point and get the translation vector
   OdGePoint3d oldCenter( center() ),
               newCenter( center() );
   newCenter.transformBy( xform );
   
   OdGeVector3d transVec = newCenter - center();
   
   // Get the equivalent transformation
   OdGeMatrix3d newXform;
   newXform.setToTranslation( transVec );

   // Only translate the face and mouth - do not transform!
   mfacecircle.transformBy( newXform );
   mmoutharc.transformBy( newXform );
   
   // Get the point at a quadrant, transform it
   OdGePoint3d oldXquad = center() + OdGeVector3d( radius(), 0, 0 ),
               newXquad( oldXquad );
   newXquad.transformBy( xform );
   
   // ... then scale the Smiley accordingly
   if( xform.isEqualTo( OdGeMatrix3d::kIdentity ) == false )
     scaleRadius( radius() * newCenter.distanceTo( newXquad ) / oldCenter.distanceTo( oldXquad ));

   return eOk;
}

void AsdkSmiley::scaleRadius(const double r)        // stretch and constrict of smiley
{
   assertWriteEnabled();
   OdGePoint3d center( center() );
   double rad = radius(),
          factor = r / rad;
          
   setEyesApart( factor * eyesApart() );
   setEyesHeight( factor * eyesHeight() );
   setEyeSize( factor * eyeSize() );
   setMouth( mouthLeft().scaleBy( factor, center ),
             mouthBottom().scaleBy( factor, center ),
             mouthRight().scaleBy( factor, center ) );
   setRadius( r );
   recordGraphicsModified();
}

See Also

Creating a Custom Entity

Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.