Drawings SDK Developer Guide > Working with .dwg Files > Working with Entities > Working with Specific Entitites > Working with Hatches > Working with Gradient Properties of Hatches
Working with Gradient Properties of Hatches

A gradient is an area filled with a color differential from a start color to an end color. The OdDbHatch object has the isGradient(), setGradient(), setGradientColors(), getGradientColors(), gradientName(), gradientType(), setGradientAngle(), gradientAngle(), setGradientShift(), and gradientShift() methods to work with gradients.

Hatch object type

To find out whether or not the hatch is a gradient object, use the getHatchObjectType() method, which was described in Working with General Properties of Hatches, or use the isGradient() method, which returns "true" if a hatch entity is a color gradient or "false" if it is not. For example:


if (pHatch->isGradient() )
  odPrintConsoleString(L"\npHatch entity is a color gradient");
else odPrintConsoleString(L"\npHatch entity is a classic hatch");

Setting the gradient

To set a gradient to a hatch, use the setGradient() method, which requires two parameters: gradient type which is a value from the OdDbHatch::GradientPatternType enumeration (see below), and a gradient name as an OdString value.


enum GradientPatternType {
  kPreDefinedGradient = 0,
  kUserDefinedGradient = 1
};

If the gradient type is kPreDefinedGradient, the gradient name should have one of the next values: SPHERICAL, HEMISPHERICAL, CURVED, LINEAR, CYLINDER, INVSPHERICAL, INVHEMISPHERICAL, INVCURVED, INVLINEAR, or INVCYLINDER.

For example:


pHatch->setGradient(OdDbHatch::GradientPatternType::kPreDefinedGradient, OD_T("SPHERICAL"));

Gradient type

To get the gradient type, use the gradientType() method, which returns the gradient type as a value from the OdDbHatch::GradientPatternType enumeration. For example:


OdString str;
OdInt8 nHatchType = pHatch->gradientType();
switch(nHatchType)
{
  case 0:
    str = "predefined";
    break;
  case 1:
    str = "user defined";
    break;
}
odPrintConsoleString(L"\nGradient is %s", str.c_str());

Gradient name

To get the gradient name, use the gradientName() method, which returns the gradient name as an OdString value. For example:


OdString gradName = pHatch->gradientName();
odPrintConsoleString(L"\nName of gradient is %s", gradName.c_str());

Gradient colors

Gradient colors specify the start color and end color of the gradient.

To get the gradient colors, use the getGradientColors() method, which requires two parameters: output array of colors defining the gradient as an OdCmColorArray object and an output array of interpolation values for the gradient as an OdGeDoubleArray object. For example:


OdCmColorArray colAr;
OdDoubleArray interValAr;
pHatch->getGradientColors(colAr, interValAr);

To set the colors for a gradient, use the setGradientColors() method, which requires three parameters: integer value for the count (must be two for the current implementation), array of OdCmColor objects as an array of gradient colors, and an array of double values as an array of interpolation values for the gradient. For example:


OdCmColor col1;
col1.setRGB(0, 255, 150);
OdCmColor col2;
col2.setRGB(255, 150, 0);
OdCmColor colors[2] = {col1, col2};
double gradientValues[2] = {0.0, 1.0};
pHatch->setGradientColors(2, colors, gradientValues);

Gradient angle

The gradient angle specifies the angle in radians as a double value in the range -2PI to +2PI on which the gradient area rotates around the origin of the hatch entity. A positive value rotates the gradient counterclockwise. A negative value rotates the gradient clockwise. If an absolute angle value is greater than 2PI, it is converted to 2PI.

To get the gradient angle, use the gradientAngle() method, which returns the gradient angle as a double value. For example:


double dAngle = pHatch->gradientAngle();
odPrintConsoleString(L"\nGradient angle is %f radians", dAngle);

To set the gradient angle, use the setGradientAngle() method, which requires one double parameter as an input gradient angle. For example:


pHatch->setGradientAngle(20);

Gradient shifting

You can shift the center of the gradient and get the value of the shift.

To get the gradient shift value, use the gradientShift() method, which returns the gradient shift as a double value. For example:


double dShift = pHatch->gradientShift();
odPrintConsoleString(L"\nGradient shift is %f", dShift);

To set the gradient shift, use the setGradientShift() method, which requires one double parameter as an input gradient shift. For example:


pHatch->setGradientShift(0.5);

See Also

Overview of Hatches

Working with Loops of Hatches

Working with General Properties of Hatches

Working with Pattern Properties of Hatches

Working with User-Defined Patterns of Hatches

Example of Working with Hatches

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