📜 ⬆️ ⬇️

Using the KOMPAS-3D API → Lesson 1 → Basics

Designers and engineers are accustomed to working in KOMPAS, manually building drawings, 3D models and assemblies. However, the COMPASS system provides a rich set of functions to automate their work. If desired, the entire process of building a complex drawing, 3D-model or assembly can be reduced to pressing a single button. The truth for this will have to work for us, programmers.

Within one article it is impossible to describe all the capabilities of KOMPAS for automation. Even a book is not enough for this. Fortunately for us, KOMPAS includes documentation, header files for various programming languages, and a number of examples.


')
Sergey Alexandrovich Norseev, Software Engineer, JSC Signal Research Institute, Kovrov, tells. Author of the book “Development of applications for KOMPAS in Delphi”


Documentation and examples



The documentation is framed as a single CHM file. It is located in the KOMPAS directory SDK . On my home computer, for example, the full path to it looks like this: "C: \ Program Files \ ASCON \ KOMPAS-3D v17 \ SDK \ SDK.chm" . The figure below shows the contents of this file.



This file contains a complete description of all interfaces, their properties and methods. In this article we will give only a brief overview of the interaction with KOMPAS through COM interfaces version 5.

In addition to the documentation, a large number of programming examples for this system in different programming languages ​​are included in the package of KOMPAS. They are located in the same directory as the documentation. When you open the “SDK” directory, you will see several archives whose names correspond to programming languages: “Basic” , “C #” , “ ++” and “Pascal” . They are examples. We will use the C ++ language, unpack the corresponding archive to any place convenient to you. C ++ Builder is used as the medium.



Header files



Before we begin, we need the header files. They are located in the “SDK \ Include \” directory. We need the following files.

"Ks_TLB.h" and "Ks_TLB.cpp" . They contain declarations of all major interfaces. These are the main files.

"KsConstants.h" and "ldefin2D.h" . These are optional files that define various constants.
After these files are connected to our project, we can start working.



Connection to COMPASS



The KOMPAS system itself in the “API of version 5 interfaces” is described by the KompasObject interface. The corresponding COM object is specified by the string "KOMPAS.Application.5" . Below is an example of connecting to KOMPAS.

KompasObjectPtr kompas; //  kompas.CreateInstance(L"KOMPAS.Application.5"); //   kompas->Visible = true; //   kompas.Unbind(); 


The “KompasObjectPtr” data type sets the pointer to the KompasObject interface.

As a result of this program, the main window of the KOMPAS program will appear on the screen. It will not open a single document.



To close the COMPASS program, use the Quit () method of the KompasObject interface. One of the most important methods of the KompasObject interface is the GetParamStruct method. It returns a pointer to the parameter interface of an object of a particular type. The requested object type is passed to the method as the value of a single parameter. In total, according to the KOMPAS documentation, through this method you can get 129 parameter interfaces for objects of various types.

Drawing and fragment



Creating a drawing and a fragment takes place in two stages. At the first stage, the parameters of the created document are prepared. On the second - the document itself is created. This two-step approach is used to create almost all objects in the KOMPAS system.

Document parameters are described by the ksDocumentParam interface. To get a pointer to it, use the KompasObject interface's GetParamStruct method with the ko_DocumentParam parameter. The main property of this interface is the type property, which indicates the type of document described by this interface.

The drawing and fragment are described by the ksDocument2D interface. You can get a pointer to it using the KompasObject Document2D method. The following is an example of a program creating a new drawing.

 KompasObjectPtr kompas; //  kompas.CreateInstance(L"KOMPAS.Application.5"); //   DocumentParamPtr DocumentParam; DocumentParam=(DocumentParamPtr)kompas->GetParamStruct(ko_DocumentParam); DocumentParam->Init(); DocumentParam->type = lt_DocSheetStandart; //:  //  Document2DPtr Document2D; Document2D = (Document2DPtr)kompas->Document2D(); Document2D->ksCreateDocument(DocumentParam); //   kompas->Visible = true; kompas.Unbind(); 


As a result of this program, the main window of the KOMPAS program will appear on the screen with a new drawing. This drawing will have neither a frame nor a main inscription, since we have not configured them.



To create a fragment in the type field of the ksDocumentParam interface, you must specify the value lt_DocFragment . In all other respects, the process of creating a new fragment is similar to the process of creating a drawing.

Specification



Creating a specification is similar to creating a drawing and a fragment. Here, too, the ksDocumentParam interface is used . But there are a number of differences. First, the specification is described by the ksSpcDocument interface. A pointer to this interface returns the KompasObject interface's SpcDocument () method . Secondly, in the case of creating a specification, you must specify the full path to the style library. If it is not specified, the system will not create a specification. The style library is the file “graphic.lyt” . It is located in the Sys \ KOMPAS directory. The following is an example of a program that creates a specification.

 //  KompasObjectPtr kompas; kompas.CreateInstance(L"KOMPAS.Application.5"); //   DocumentParamPtr DocumentParam; DocumentParam=(DocumentParamPtr)kompas->GetParamStruct(ko_DocumentParam); DocumentParam->Init(); DocumentParam->type = lt_DocSpc; //:  //      BSTR str; str = kompas->ksSystemPath(sptSYSTEM_FILES); SysReAllocString(&str, SysAllocString(L"\graphic.lyt")); //     SheetParPtr SheetPar; SheetPar = (SheetParPtr)DocumentParam->GetLayoutParam(); SheetPar->Init(); SheetPar->layoutName = str; //  SpcDocumentPtr SpcDocument; SpcDocument = (SpcDocumentPtr)kompas->SpcDocument(); SpcDocument->ksCreateDocument(DocumentParam); //   kompas->Visible = true; kompas.Unbind(); 


I make three important points to the above example.
  1. To get the path to the Sys directory, we use the KompasObject interface's ksSystemPath method . This method returns the path to the specified KOMPAS directory.
  2. COMPASS, however, like all COM objects, operates on BSTR strings. These lines, just like the wchar_t * strings, are composed of Unicode characters, but have a more complex structure. An attempt to send a regular Unicode string ( wchar_t * ) to KOMPAS will fail.
  3. The path to the style library is specified in the ksSheetPar interface. This interface sets the design options for the drawing and / or specification.


As a result of this program, the KOMPAS window with the specification created in it will appear on the screen.



Detail and assembly



The part and assembly in KOMPAS are described by the ksDocument3D interface. More specifically, this interface describes a file containing a part or assembly. To obtain a pointer to the ksDocument3D interface , use the Document3D () method of the KompasObject interface. The following is an example of creating a part.

 //  KompasObjectPtr kompas; kompas.CreateInstance(L"KOMPAS.Application.5"); //  Document3DPtr Document3D; Document3D = (Document3DPtr)kompas->Document3D(); Document3D->Create(false, true); //   kompas->Visible = true; kompas.Unbind(); 


The Create method of the ksDocument3D interface has two input parameters. They are listed below.
  1. Sign of the document editing mode ( TRUE - invisible mode; FALSE - visible).
  2. The type of file to be created ( TRUE - part; FALSE - assembly).


This shows that to create an assembly in the above example, you just need to change the value of the second parameter in the Create method.



Conclusion

In this article, I showed a small part of the tip of the iceberg called “interaction with the COMPASS system through COM interfaces”. I just showed how to connect to KOMPASU and how to create documents of all basic types (drawing, fragment, specification, detail and assembly). These documents are created without content. How to fill them will be discussed in the following articles. Follow the news blog.

The second part of.

Sergey Norseev, author of the book “Development of applications for KOMPAS in Delphi”.

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


All Articles