📜 ⬆️ ⬇️

Using the KOMPAS-3D API → Lesson 12 → Composite Lines

In the previous lesson, we looked at the display of simple strings. This talk about the formation of composite lines, including deviations and fractions. We will form such strings using the ksText method, which we studied earlier. Strictly speaking, this method is not intended to output complex strings. Nevertheless, in today's lesson we will get acquainted with a number of flags that play a key role in the formation of composite lines.




Content of the cycle of lessons “Working with the API KOMPAS-3D”


  1. The basics
  2. Drawing design
  3. Correct connection to COMPAS
  4. Title block
  5. Graphic primitives
  6. Save the document in various formats
  7. Introduction to the settings
  8. More sophisticated writing methods
  9. Reading the caption cells
  10. Special characters including string
  11. Simple text lettering
  12. Compound strings

Although the described methods for constructing deviations and fractions are undocumented, they, as shown by my experiments, regularly work on modern versions of KOMPAS (tested on 15, 16 and 17). But this behavior can be changed in future versions.
')
The “correct” methods for constructing compound strings will be described in the following lessons.

Italic, bold and underlined


Italic, bold, and underlined is controlled by the flags shown in the table below. They are declared in the ldefin2d.h header file.



In the ksText method , the effect of each of these flags is limited to calling this method. Below is an example of a program that demonstrates the output of lines with different outlines.

Document2D->ksText(100, 100, 0, 0, 0, 0 , SysAllocString(L" ")); Document2D->ksText(100, 90, 0, 0, 0, ITALIC_OFF , SysAllocString(L"  ")); Document2D->ksText(100, 80, 0, 0, 0, BOLD_ON , SysAllocString(L" ")); Document2D->ksText(100, 70, 0, 0, 0, UNDERLINE_ON, SysAllocString(L" ")); 

For simplicity, this example omits the code responsible for creating and formatting the document (this topic was covered in previous lessons), as well as for freeing resources (including after calling the SysAllocString function).

Note: by default, KOMPAS displays text in italics. Therefore, we use the ITALIC_OFF flag to cancel italic. The figure below shows the lines displayed by this program.



Simultaneous use of paired flags (for example, BOLD_ON and BOLD_OFF ) does not affect the display of the string. It is displayed as it would be displayed if none of these flags were set.

Upper and lower deviations


The upper and lower deviations are set using the flags in the table below.



Below is an example of a program that demonstrates the output of a line containing upper and lower deviations.

 double x = 100.0; double y = 100.0; BSTR str = SysAllocString(L"  "); long itext = Document2D->ksText(x, y, 0, 0, 0, 0, str); SysFreeString(str); x += Document2D->ksGetTextLengthFromReference(itext) + 2.0; str = SysAllocString(L" "); itext = Document2D->ksText(x, y, 0, 0, 0, UPPER_DEVIAT, str); SysFreeString(str); double dx1 = Document2D->ksGetTextLengthFromReference(itext); str = SysAllocString(L" "); itext = Document2D->ksText(x, y, 0, 0, 0, LOWER_DEVIAT, str); SysFreeString(str); double dx2 = Document2D->ksGetTextLengthFromReference(itext); x += max(dx1, dx2); str = SysAllocString(L"  "); Document2D->ksText(x, y, 0, 0, 0, 0, str); SysFreeString(str); 

In this example, each time we recalculate the coordinates of the anchor points of the output lines in order to know where to output them. To do this, use the ksGetTextLengthFromReference method described in the previous lesson. Note: only the horizontal coordinate is recalculated, the vertical one does not change. KOMPAS itself determines the desired vertical offset of the rows. The figure below shows the compound line formed by this program.



Fraction


Fraction drawing is set by the flags listed in the table below.



Below is an example of a program that demonstrates the output of a fraction in a text string.

 double x = 100.0; double y = 100.0; BSTR str = SysAllocString(L"  "); long itext = Document2D->ksText(x, y, 0, 0, 0, 0, str); SysFreeString(str); x += Document2D->ksGetTextLengthFromReference(itext) + 2.0; str = SysAllocString(L""); itext = Document2D->ksText(x, y, 0, 0, 0, NUMERATOR, str); SysFreeString(str); double dx1 = Document2D->ksGetTextLengthFromReference(itext); str = SysAllocString(L""); itext = Document2D->ksText(x, y, 0, 0, 0, DENOMINATOR, str); SysFreeString(str); double dx2 = Document2D->ksGetTextLengthFromReference(itext); x += max(dx1, dx2); str = SysAllocString(L"  "); Document2D->ksText(x, y, 0, 0, 0, 0, str); SysFreeString(str); 

It is easy to see that this example is similar to the previous one. Indeed, the output of deviations and fractions differs only in the flags used. The figure below shows the result of the program.



Conclusion

In this lesson, we have addressed the issue of creating composite strings using the ksText method. The method described here is undocumented, so you should not use it in your applications. In the following lessons we will take a closer look at the documented ways to create compound strings. And there we will need flags that we met today.

To be continued, stay tuned to the blog.

Sergey Norseev, Ph.D., author of the book “Development of applications for COMPAS in Delphi”.

Source: https://habr.com/ru/post/424509/


All Articles