📜 ⬆️ ⬇️

Using the KOMPAS-3D API → Lesson 9 → Reading the caption cells

We continue the cycle of articles on working with the CAD API KOMPAS-3D, Sergey Norseev, Ph.D. C ++ Builder is used as the medium. We continue to talk about the main inscription. In the two previous articles (you can familiarize yourself with them here and here ) we have examined in detail the various ways of writing into the main title. Here we talk about her reading.



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

KsGetStampColumnText Method


The ksGetStampColumnText method of the ksStamp interface is intended for reading the contents of the main inscription cells. Below is its prototype.

LPDISPATCH ksGetStampColumnText ( long* numb //  ); 

Looking at this prototype, there is a false sense that the method has one input parameter, but it is not. The numb parameter is the return value. In it, the method returns the number of the cell viewed with it. For one call, it allows you to view only one cell.
')
The contents of the scanned cell are returned by the method as a dynamic array ( ksDynamicArray ) of type TEXT_LINE_ARR . That is, its elements are the ksTextLineParam interfaces (dynamic arrays and the ksTextLineParam interface were covered in the last lesson).

There are two ways to set the desired cell number. The first is using the ksColumnNumber method of the ksStamp interface. The second is by repeatedly calling the ksGetStampColumnText method.

After each call, the ksGetStampColumnText method moves to the “next” non-empty cell. All non-empty cells are ordered in the order they are filled. In this order, the ksGetStampColumnText method lists them. In case the method has reached the end of its internal buffer of non-empty cells, it returns NULL .

According to the KOMPAS documentation, the numb parameter can be NULL . However, as shown by my experiments, in this case the ksGetStampColumnText method also returns NULL .

Use the ksGetStampColumnText method in the editing mode of the title block , that is, after calling the ksOpenStamp () method and before calling ksCloseStamp () .

Example. Single cell reading


The following is the source code of a program that demonstrates reading the contents of a single cell in the title block.

 //   KompasObjectPtr kompas; kompas.CreateInstance(L"KOMPAS.Application.5"); //         WideString str; str = ExtractFileDir(Application->ExeName) + L"\\.cdw"; //  Document2DPtr Document2D; Document2D = (Document2DPtr)kompas->Document2D(); Document2D->ksOpenDocument(SysAllocString(str.c_bstr()), false); //     TextItemParamPtr TextItemParam; TextItemParam = (TextItemParamPtr)kompas->GetParamStruct(ko_TextItemParam); TextLineParamPtr TextLineParam; TextLineParam = (TextLineParamPtr)kompas->GetParamStruct(ko_TextLineParam); //    StampPtr Stamp; Stamp = (StampPtr)Document2D->GetStamp(); //     Stamp->ksOpenStamp(); //   Stamp->ksColumnNumber(ksStAuthor); long numb = 0; ksDynamicArrayPtr DynamicArrayLines; DynamicArrayLines = (ksDynamicArrayPtr)Stamp->ksGetStampColumnText(&numb); //   str = WideString(L""); if(DynamicArrayLines) { //  ksTextLineParam for(int iline = 0; iline < DynamicArrayLines->ksGetArrayCount(); ++iline) { //   DynamicArrayLines->ksGetArrayItem(iline, TextLineParam); ksDynamicArrayPtr DynamicArrayItems; DynamicArrayItems = (ksDynamicArrayPtr)TextLineParam->GetTextItemArr(); if(!DynamicArrayItems) continue; //  ksTextItemParam for(int iitem = 0; iitem < DynamicArrayItems; ++iitem) { DynamicArrayItems->ksGetArrayItem(iitem, TextItemParam); str += WideString(TextItemParam->get_s()); } //   ksTextItemParam DynamicArrayItems->ksDeleteArray(); DynamicArrayItems.Unbind(); }//for(int iline = 0; iline < DynamicArray->ksGetArrayCount(); ++iline) //   ksTextLineParam DynamicArrayLines->ksDeleteArray(); DynamicArrayLines.Unbind(); }//if(DynamicArrayLines) //     Stamp->ksCloseStamp(); //  Stamp.Unbind(); TextItemParam.Unbind(); TextLineParam.Unbind(); Document2D.Unbind(); kompas->set_Visible(true); kompas.Unbind(); //   ShowMessage(str); 

This example opens the document, reads the cell of the title block with the last name of the author of the document and displays a string with its contents.

In addition to working with the title block, this example demonstrates the enumeration of the elements of the ksDynamicArray array. The presence of a nested loop is explained by the structure of the array returned by the ksGetStampColumnText method. She was cited in one of the previous articles in our cycle.

To simplify the example, we do not process special characters, and also do not check the types of arrays obtained.

Note: the value of the parameter passed to the ksGetStampColumnText method is zero and differs from the cell number, the contents of which we receive. The number of the desired cell is determined by the ksColumnNumber method.

Calling the ksGetStampColumnText method changes the value of the variable numb . It becomes equal to ksStAuthor , that is, the cell number, the contents of which we read. If you call the ksGetStampColumnText method again , it will read the contents of the “next” cell and write its number in the variable passed to it.

For an empty cell, the ksGetStampColumnText method returns an empty string.

Example. Read all cells


Let's modify the previous example so that it reads the contents of all non-empty cells of the main label.


Sample drawing caption example

 //   KompasObjectPtr kompas; kompas.CreateInstance(L"KOMPAS.Application.5"); //         WideString str; str = ExtractFileDir(Application->ExeName) + L"\\.cdw"; // ,       HANDLE hOutFile; hOutFile = CreateFile("Stamp_Content.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL); //  Document2DPtr Document2D; Document2D = (Document2DPtr)kompas->Document2D(); Document2D->ksOpenDocument(SysAllocString(str.c_bstr()), false); //     TextItemParamPtr TextItemParam; TextItemParam = (TextItemParamPtr)kompas->GetParamStruct(ko_TextItemParam); TextLineParamPtr TextLineParam; TextLineParam = (TextLineParamPtr)kompas->GetParamStruct(ko_TextLineParam); //    StampPtr Stamp; Stamp = (StampPtr)Document2D->GetStamp(); //     Stamp->ksOpenStamp(); //   long numb = 0; ksDynamicArrayPtr DynamicArrayLines; DynamicArrayLines = (ksDynamicArrayPtr)Stamp->ksGetStampColumnText(&numb); //   while(DynamicArrayLines) { str = WideString(IntToStr(numb)) + WideString(L" : "); //  ksTextLineParam for(int iline = 0; iline < DynamicArrayLines->ksGetArrayCount(); ++iline) { //   DynamicArrayLines->ksGetArrayItem(iline, TextLineParam); ksDynamicArrayPtr DynamicArrayItems; DynamicArrayItems = (ksDynamicArrayPtr)TextLineParam->GetTextItemArr(); if(!DynamicArrayItems) continue; //  ksTextItemParam for(int iitem = 0; iitem < DynamicArrayItems; ++iitem) { DynamicArrayItems->ksGetArrayItem(iitem, TextItemParam); str += WideString(TextItemParam->get_s()); } //   ksTextItemParam DynamicArrayItems->ksDeleteArray(); DynamicArrayItems.Unbind(); }//for(int iline = 0; iline < DynamicArray->ksGetArrayCount(); ++iline) //    str += WideString(L"\r\n"); DWORD r; WriteFile(hOutFile, str.c_bstr(), str.Length() * sizeof(wchar_t), &r, NULL); //   ksTextLineParam DynamicArrayLines->ksDeleteArray(); //    DynamicArrayLines=(ksDynamicArrayPtr)Stamp->ksGetStampColumnText(&numb); }//while(DynamicArrayLines) //     Stamp->ksCloseStamp(); //  Stamp.Unbind(); TextItemParam.Unbind(); TextLineParam.Unbind(); Document2D.Unbind(); kompas->set_Visible(true); kompas.Unbind(); //      CloseHandle(hOutFile); 

This example lists all non-empty cells in the title block. Their contents are recorded in the text file “Stamp_Content.txt” (the program creates it in the application directory). Each cell corresponds to a row of the form

<Cell Number>: <Cell Content>

Please note: we do not use the ksColumnNumber method. The cells are listed by the ksGetStampColumnText method. As mentioned earlier, it lists the cells in the order they are filled. So in my test example, the contents of the Stamp_Content.txt file were as follows.

 6 : 1:1 32 : A4 8 : 1 2 :  1 :  110 :  3 :  9 :  

Only filled cells fall into the file, because the ksGetStampColumnText method does not return the contents of empty cells.

Note that the ksGetStampColumnText method only lists cells in the forward direction. If using the ksColumnNumber method we move it to cell number 2 and then start calling it in a loop, it will list the cells with numbers 2 , 1 , 110 , 3 and 9 (for my example), and the cells with numbers 6 , 32 and 8 will not be listed.

When the end of the cells is reached, the ksGetStampColumnText method returns NULL .

Conclusion


In this article we examined the issue of reading the contents of the cells of the main inscription. To perform this operation, you have to work with the array of ksTextLineParam interfaces. It has a rather complicated structure, therefore the most difficult part of the programs is its correct analysis. However, if you remember the structure given in the previous article of the cycle, then working with it will not cause any difficulties.

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/359232/


All Articles