Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
huyc2GQZL
252 Views, 4 Replies

How to Move and Rotate Bodies?

Hi everyone,

I'm working on a project where I need to find the plane with the largest area and use its normal vector as the Z-axis to establish a User Coordinate System (UCS). My next step is to rotate and move the skewed bodies (i.e., the models that are not aligned) into another coordinate system. However, I haven't been able to find any relevant API or examples that could help me achieve this. Could someone please provide some guidance?

Here's the code I have so far:

 

/// <summary>
/// Aligns all bodies based on the plane with the largest area.
/// </summary>
/// <param name="doc">The PartDocument containing the model.</param>
public static void CorrectTheModel(PartDocument doc){
    // Get the plane with the largest area.
    Plane maxFace = GetMaxAreaFace(doc).Geometry as Plane;    ComponentDefinition oCompDef = doc.ComponentDefinition;

    // Get the normal vector of the plane with the largest area.
    double[] normals = new double[3];    maxFace.Evaluator.GetNormal(new double[2], ref normals);
    double[] points = new double[3];    maxFace.Evaluator.GetPointAtParam(new double[2] { 0.5, 0.5 }, ref points);    Application ThisApplication = doc.Parent as Application;
    // Create the Z-axis vector.
    var VectorZ = ThisApplication.TransientGeometry.CreateVector(normals[0], normals[1], normals[2]);    // Create the Y-axis vector.
    var VectorY = ThisApplication.TransientGeometry.CreateVector(0, 1, 0);
    // Calculate the X-axis vector.
    var VectorX = VectorY.CrossProduct(VectorZ);
    // Create the origin point.
    Point Origin = ThisApplication.TransientGeometry.CreatePoint(points[0], points[1], points[2]);
    // Create a transformation matrix for the new coordinate system.
    Matrix newMatrix = ThisApplication.TransientGeometry.CreateMatrix();
   newMatrix.SetCoordinateSystem(Origin, VectorX, VectorY, VectorZ);

    // Define the new UCS.
    var oUCSDef = oCompDef.UserCoordinateSystems.CreateDefinition();
    oUCSDef.Transformation = newMatrix;

    // Add the new UCS to the component definition.
    oCompDef.UserCoordinateSystems.Add(oUCSDef);

    foreach (SurfaceBody body in doc.ComponentDefinition.SurfaceBodies)    {
        // Here is where I need help: how can I perform a coordinate system to coordinate system trans       formation for all the bodies?
    }
}
Michael.Navara
in reply to: huyc2GQZL

I don't understand the purpose of this feature. Can you post some screenshot what do you expect?

But in general. There are limited possibilities of manipulating surface bodies in part. Much easier can be to create new assembly and parts from bodies and place them to this assembly. Or you can create new part where you can create copy of original bodies with given transformation. Here is the sample in VBA  

huyc2GQZL
in reply to: Michael.Navara

Thank you for your response! The NonParametricBaseFeatures function creates independent surfaces after running, but what I'm aiming for is more similar to the native Inventor functionality: moving solid bodies.

huyc2GQZL_0-1724225431064.png

 

Michael.Navara
in reply to: huyc2GQZL

This is represented in API as MoveFeature. There is no problem to translate part in space, but it is hard to define its rotation.

huyc2GQZL
in reply to: Michael.Navara

Yes, I have discovered no other means to wrap a function for the automatic matrix transformation of the entity.

Thanks for reply