Handling of Coordinates (How to Create a Plug-In)

www.CAD6.com

During the development of plug-ins, you will often have to handle with coordinates of different types.

 

Basically, all objects created and maintained by the serving application will use the so-called "internal coordinates" (see Geometrical Representation). These coordinates are always stated in millimeters relative to the current page's center. They are independent from any user settings, i.e. they are not influenced by scales, units, or the current origin. Internal coordinates determine the size and position of the objects when they are printed. A square with a side length of 10 mm in internal coordinates will be printed in the same size (unless the user explicitly scales the output). Due to their invariable definition, internal coordinates are easy to handle and to calculate with. But they are not suitable for interaction with the user, as the user is used to work with origins, scales and different units. So plug-ins will usually have to work both in internal coordinates and in "user coordinates", depending on the user's settings of scale, unit, and origin. The circumstance that the scale, the unit, and the origin are always store as a compound group within coordinate systems simplifies the work with them. The serving application helps you by calculating two matrices for each coordinate system that allow a fast and easy conversion from internal coordinates to user coordinates and vice versa.

 

Let's assume your plug-in allows the user to enter a rectangle by stating the coordinates of its corner-points. This entry will be done in user coordinates, so you will have to calculate the resulting internal coordinates in order to create the rectangle. After you have read the four values (x1, y1, x2, y2), you can convert them to internal coordinates using the following code. Please note that the resulting rectangle may be a rotated or distorted rectangle, so all four corner-points have to calculated separately!

 

C++ Source Code

// Variables required for the calculation.

MKI_SYSTEMDEF  cCurrentSystem;

MKI_POINT      cPoint1,

               cPoint2,

               cPoint3,

               cPoint4;

 

// Initialize the variables.

MKI_SystemGetCurrentDef( &cCurrentSystem );

 

// Convert the coordinates.

cPoint1.x = cCurrentSystem.SystemUnitToMM.x( x1, y1 );

cPoint1.y = cCurrentSystem.SystemUnitToMM.y( x1, y1 );

 

cPoint2.x = cCurrentSystem.SystemUnitToMM.x( x1, y2 );

cPoint2.y = cCurrentSystem.SystemUnitToMM.y( x1, y2 );

 

cPoint3.x = cCurrentSystem.SystemUnitToMM.x( x2, y2 );

cPoint3.y = cCurrentSystem.SystemUnitToMM.y( x2, y2 );

 

cPoint4.x = cCurrentSystem.SystemUnitToMM.x( x2, y1 );

cPoint4.y = cCurrentSystem.SystemUnitToMM.y( x2, y1 );

 

The calculated points can now be used to create a polygon:

 

C++ Source Code

// Variables required to create a polygon.

bool  fResult;

 

// Create a polygon.

MKI_ObjectOpen( MKI_OBJ_SURFACE );

MKI_ObjectAddPoint( MKI_DB_POINT_START, cPoint1 );

MKI_ObjectAddPoint( MKI_DB_POINT_END,   cPoint2 );

MKI_ObjectAddPoint( MKI_DB_POINT_END,   cPoint3 );

MKI_ObjectAddPoint( MKI_DB_POINT_END,   cPoint4 );

 

// Insert the polygon into the current drawing.

fResult = ( MKI_ObjectFastInsert() != nullptr );

 

For further information about the internal data block structure of the object MKI_OBJ_SURFACE, see Object 13 "Surface".

 

A similar method can be used for a conversion in the opposite direction. If, e.g., a plug-in offers an information command that allows the user to identify an object whose coordinates will then be displayed, it will have to convert internal coordinates into user coordinates. A point with the coordinates (x1,y1) can be converted using the following code:

 

C++ Source Code

// Variables required for the calculation.

MKI_SYSTEMDEF  cCurrentSystem;

MKI_POINT      cPoint;

 

// Initialize the variables.

MKI_SystemGetCurrentDef( &cCurrentSystem );

 

// Convert the coordinates.

cPoint.x = cCurrentSystem.SystemMMToUnit.x( x1, y1 );

cPoint.y = cCurrentSystem.SystemMMToUnit.y( x1, y1 );

 

Please note that the only difference is in the matrix used (SystemMMtoUnit instead of SystemUnitToMM). When converting vectors rather than points, you can simply use another macro for this calculation, the rest remains the same. A vector with the values (dx,dy) can be converted using the following code:

 

C++ Source Code

// Variables required for the calculation.

MKI_SYSTEMDEF  cCurrentSystem;

MKI_POINT      cVector;

 

// Initialize the variables.

MKI_SystemGetCurrentDef( &cCurrentSystem );

 

// Convert the vector.

cVector.x = cCurrentSystem.SystemMMToUnit.dx( dx, dy );

cVector.y = cCurrentSystem.SystemMMToUnit.dy( dx, dy );

 

Here, the only difference the usage of MKI_MATRIX::dx and MKI_MATRIX::dy instead of MKI_MATRIX::x and MKI_MATRIX::y.

 

Using the methods described above, you should be able to perform any required coordinate transformation. For more information on how to determine transformation matrices for frequently used modifications, see Matrices and Vectors.

 

CAD6interface 2025.0 - Copyright 2025 Malz++Kassner® GmbH