# How-to: Rotate a CAD model along an axis

**URL:** <https://forum.techsoft3d.com/t/how-to-rotate-a-cad-model-along-an-axis/1452>\
**Category:** HOOPS Exchange\
**Tags:** learn, knowledge-base, how-to, import, export\
**Created:** [November 25, 2021, 5:00am UTC](https://forum.techsoft3d.com/t/how-to-rotate-a-cad-model-along-an-axis/1452 "2021-11-25T05:00:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriel.peragine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/gabriel.peragine/32/1535_2.png) [@gabriel.peragine](https://forum.techsoft3d.com/u/gabriel.peragine)\
**Post date:** [November 25, 2021, 5:00am UTC](https://forum.techsoft3d.com/t/how-to-rotate-a-cad-model-along-an-axis/1452/1 "2021-11-25T05:00:00Z")

</div>

To rotate a whole model along an axis, it is needed to edit its root PO transformation (ie the corresponding [A3DAsmProductOccurrenceData::m\_pLocation](https://docs.techsoft3d.com/exchange/latest/api/structA3DAsmProductOccurrenceData.html#_CPPv4N27A3DAsmProductOccurrenceData11m_pLocationE)) accordingly.  
Below are some examples of code implementation:

```auto
enum Axis { X, Y, Z }; // 3D Axis

// Set-up a X rotation matrix
void SetXRotation(const A3DDouble radianAngle, A3DDouble *p4x4Matrix)
{
	p4x4Matrix[0] = 1.0;	p4x4Matrix[4] = 0.0; p4x4Matrix[8] = 0.0; p4x4Matrix[12] = 0.0;
	p4x4Matrix[1] = 0.0;	p4x4Matrix[5] = cos(radianAngle);	p4x4Matrix[9] = -sin(radianAngle);	p4x4Matrix[13] = 0.0;
	p4x4Matrix[2] = 0.0;	p4x4Matrix[6] = sin(radianAngle);	p4x4Matrix[10] = cos(radianAngle);	p4x4Matrix[14] = 0.0;
	p4x4Matrix[3] = 0.0;	p4x4Matrix[7] = 0.0; p4x4Matrix[11] = 0.0; p4x4Matrix[15] = 0.0;
}

// Set-up a Y rotation matrix
void SetYRotation(const A3DDouble radianAngle, A3DDouble *p4x4Matrix)
{
	p4x4Matrix[0] = cos(radianAngle);	p4x4Matrix[4] = 0.0;	p4x4Matrix[8] = sin(radianAngle);	p4x4Matrix[12] = 0.0;
	p4x4Matrix[1] = 0.0; p4x4Matrix[5] = 1.0;	p4x4Matrix[9] = 0.0; p4x4Matrix[13] = 0.0;
	p4x4Matrix[2] = -sin(radianAngle);	p4x4Matrix[6] = 0.0;	p4x4Matrix[10] = cos(radianAngle);	p4x4Matrix[14] = 0.0;
	p4x4Matrix[3] = 0.0; p4x4Matrix[7] = 0.0;	p4x4Matrix[11] = 0.0; p4x4Matrix[15] = 0.0;
}

// Set-up a Z rotation matrix
void SetZRotation(const A3DDouble radianAngle, A3DDouble *p4x4Matrix)
{
	p4x4Matrix[0] = cos(radianAngle);	p4x4Matrix[4] = -sin(radianAngle);	p4x4Matrix[8] = 0.0;	p4x4Matrix[12] = 0.0;
	p4x4Matrix[1] = sin(radianAngle);	p4x4Matrix[5] = cos(radianAngle);	p4x4Matrix[9] = 0.0;	p4x4Matrix[13] = 0.0;
	p4x4Matrix[2] = 0.0; p4x4Matrix[6] = 0.0; p4x4Matrix[10] = 1.0;	p4x4Matrix[14] = 0.0;
	p4x4Matrix[3] = 0.0; p4x4Matrix[7] = 0.0; p4x4Matrix[11] = 0.0;	p4x4Matrix[15] = 0.0;
}

// Function that apply a rotation to a modelFilea long an axis.
void applyRotationToModelFile(A3DAsmModelFile *pModelFile, A3DDouble rotationDegreeAngle, Axis rotationAxis)
{
	A3DAsmModelFileData modelData;
	A3D_INITIALIZE_DATA(A3DAsmModelFileData, modelData);
	A3DAsmModelFileGet(pModelFile, &modelData);

	A3DAsmProductOccurrenceData rootPoData;
	A3D_INITIALIZE_DATA(A3DAsmProductOccurrenceData, rootPoData);
	A3DAsmProductOccurrenceGet(modelData.m_ppPOccurrences[0], &rootPoData);

	A3DMiscGeneralTransformationData transfoData;
	A3D_INITIALIZE_DATA(A3DMiscGeneralTransformationData, transfoData);

	// We initialize an identity matrix
	transfoData.m_adCoeff[0] = 1.0;	transfoData.m_adCoeff[4] = 0.0;	transfoData.m_adCoeff[8] = 0.0; transfoData.m_adCoeff[12] = 0.0;
	transfoData.m_adCoeff[1] = 0.0;	transfoData.m_adCoeff[5] = 1.0;	transfoData.m_adCoeff[9] = 0.0; transfoData.m_adCoeff[13] = 0.0;
	transfoData.m_adCoeff[2] = 0.0;	transfoData.m_adCoeff[6] = 0.0;	transfoData.m_adCoeff[10] = 1.0;	transfoData.m_adCoeff[14] = 0.0;
	transfoData.m_adCoeff[3] = 0.0;	transfoData.m_adCoeff[7] = 0.0;	transfoData.m_adCoeff[11] = 0.0;	transfoData.m_adCoeff[15] = 0.0;

	// We apply the rotation to the model
	A3DDouble radianAngle = rotationDegreeAngle * M_PI / 180;

	if (rotationAxis == X)
		SetXRotation(radianAngle, transfoData.m_adCoeff);

	if (rotationAxis == Y)
		SetYRotation(radianAngle, transfoData.m_adCoeff);

	if (rotationAxis == Z)
		SetZRotation(radianAngle, transfoData.m_adCoeff);

	A3DMiscGeneralTransformation *pGeneralTransfo = nullptr;
	A3DMiscGeneralTransformationCreate(&transfoData, &pGeneralTransfo);

	rootPoData.m_pLocation = pGeneralTransfo;
        
        // We finally edit the root PO
	A3DAsmProductOccurrenceEdit(&rootPoData, modelData.m_ppPOccurrences[0]);
}

```
