📜 ⬆️ ⬇️

We work with KOMPAS 3D from DELPHI

imageimage Greetings,% username%.
Where we just do not throw sudbinushka. What creatures of the human mind alone do not come across. So I had to dig a topic about the API Compass from Ascon.
I did not find enough information on this topic. The zero developer is given the SDK Help and a number of examples, all bundled with the program.
Support on the forum http://forum.ascon.ru/index.php/board,4.0.html - practically none (users answer 80% of the questions to themselves).
But the program is still quite popular (especially honestly hacked versions) , so questions are being asked. To somehow help those who, by the will of fate (evil rock, leadership, or hysterical customer) will face this product, this article.
Here I will lay out the "subsistence minimum", the start, having pushed off from which, you can write not the worst programs, and even make money if you wish, as practice has shown.

Immediately I warn you that before writing under Compass, you must have at least minimal ideas about the system (drawing, model creation, parameterization, work with variables)

I note right away that the information for this article was taken from various sources, I don’t remember many already ... I wrote myself ... all the hints on copy-paste to ignore, except for some pieces of code, because a bicycle is a good thing, but I personally prefer to use ready-made developments.

1. Installation

Nothing new is invented here. Standard install component for Delphi. We follow that the paths were registered.
2. After installation

I recommend to start (see) Demos that come with it.
3. How to run a compass from our program

(general structure, purely for example):
')
Copy Source | Copy HTML uses ActiveX, Db, IniFiles, LDefin2D,LDefin3D, ksConstTLB, {$IFDEF __LIGHT_VERSION__} klTLB; {$ELSE} ksTLB, CheckLst; {$ENDIF} var kompas: KompasObject ; doc2: ksDocument2D ;
  1. Copy Source | Copy HTML uses ActiveX, Db, IniFiles, LDefin2D,LDefin3D, ksConstTLB, {$IFDEF __LIGHT_VERSION__} klTLB; {$ELSE} ksTLB, CheckLst; {$ENDIF} var kompas: KompasObject ; doc2: ksDocument2D ;
  2. Copy Source | Copy HTML uses ActiveX, Db, IniFiles, LDefin2D,LDefin3D, ksConstTLB, {$IFDEF __LIGHT_VERSION__} klTLB; {$ELSE} ksTLB, CheckLst; {$ENDIF} var kompas: KompasObject ; doc2: ksDocument2D ;
  3. Copy Source | Copy HTML uses ActiveX, Db, IniFiles, LDefin2D,LDefin3D, ksConstTLB, {$IFDEF __LIGHT_VERSION__} klTLB; {$ELSE} ksTLB, CheckLst; {$ENDIF} var kompas: KompasObject ; doc2: ksDocument2D ;
  4. Copy Source | Copy HTML uses ActiveX, Db, IniFiles, LDefin2D,LDefin3D, ksConstTLB, {$IFDEF __LIGHT_VERSION__} klTLB; {$ELSE} ksTLB, CheckLst; {$ENDIF} var kompas: KompasObject ; doc2: ksDocument2D ;
  5. Copy Source | Copy HTML uses ActiveX, Db, IniFiles, LDefin2D,LDefin3D, ksConstTLB, {$IFDEF __LIGHT_VERSION__} klTLB; {$ELSE} ksTLB, CheckLst; {$ENDIF} var kompas: KompasObject ; doc2: ksDocument2D ;
  6. Copy Source | Copy HTML uses ActiveX, Db, IniFiles, LDefin2D,LDefin3D, ksConstTLB, {$IFDEF __LIGHT_VERSION__} klTLB; {$ELSE} ksTLB, CheckLst; {$ENDIF} var kompas: KompasObject ; doc2: ksDocument2D ;
  7. Copy Source | Copy HTML uses ActiveX, Db, IniFiles, LDefin2D,LDefin3D, ksConstTLB, {$IFDEF __LIGHT_VERSION__} klTLB; {$ELSE} ksTLB, CheckLst; {$ENDIF} var kompas: KompasObject ; doc2: ksDocument2D ;
  8. Copy Source | Copy HTML uses ActiveX, Db, IniFiles, LDefin2D,LDefin3D, ksConstTLB, {$IFDEF __LIGHT_VERSION__} klTLB; {$ELSE} ksTLB, CheckLst; {$ENDIF} var kompas: KompasObject ; doc2: ksDocument2D ;
  9. Copy Source | Copy HTML uses ActiveX, Db, IniFiles, LDefin2D,LDefin3D, ksConstTLB, {$IFDEF __LIGHT_VERSION__} klTLB; {$ELSE} ksTLB, CheckLst; {$ENDIF} var kompas: KompasObject ; doc2: ksDocument2D ;


Copy Source | Copy HTML
  1. // Create automation object KOMPAS_Graphic
  2. if Kompas = nil then begin
  3. {$ IFDEF __LIGHT_VERSION__}
  4. Kompas: = KompasObject ( CreateOleObject ('KompasLT.Application.5' ));
  5. {$ ELSE}
  6. Kompas: = KompasObject ( CreateOleObject ('Kompas.Application.5' ));
  7. {$ ENDIF}
  8. if Kompas <> nil then Kompas. Visible : = true;
  9. end;
  10. doc2: = ksDocument2D ( kompas.Document2D );
  11. if FileIsThere ( FileName ) then // FileName - template name + path
  12. doc2.ksOpenDocument ( FileName, false ) // Open the drawing
  13. else MessageBox ( self.Handle, Pchar (FileName ), 'File not found' , MB_OK);
  14. if doc2 <> nil then begin
  15. // HERE WE WORK WITH A DOCUMENT
  16. doc2.ksSaveDocument ( FileName1 ); // FileName1 - the name of the finished document + path
  17. end;
  18. // When we finish - Kompas: = nil;


Function FileIsThere - torn from the examples help Delphi.

Let me remind you that, as with any third-party applications (written by someone), if possible, you should check all returned results.
It is possible to work with Compass API in two versions: External program and built-in library. Here only the external control of the system will be considered, for the difference is not great (in the sense of the essence of interaction with Compass).

In the SDK demos there are examples of direct drawing in Compass, but I prefer to draw manually, especially since usually the output requires variants of the same product with different sizes, which means we can use templates.

4. And so ... we work with the part

4.1. Template preparation

First, create a template. Let's say it will be a three-stage shaft. While simple (without any frills).
Draw a 3D shaft.
Parameterize it. Variables L1, D1, L2, D2, D3, L3 - Length and Diameters of the steps. Let's make them external variables.
Assign Designation, Part Name and create BOM Object.
Remember to save.

Further, on the basis of this 3d template, we need a drawing, create it.
Ensure that the dimensioned dimensions are correctly linked to the elements in the drawing.
Save already, let's get down to programming.

4.2. Preparation of the form of the program

Let's create a form, we throw on it 6 TEdit elements, let's call them by the names of our external variables edL1 ... edD3.
Kines on the form of 3 buttons. Let's write “Start”, “Exit” and “Apply” on them.
We also need TSaveDialog .
The rest, at the request of the workers: 2 TEdit elements under the Designation and name of the part (you can not do this, but assign it programmatically).

Variables:

Copy Source | Copy HTML
  1. var doc: ksDocument3D;
  2. doc2: ksDocument2D;
  3. Kompas: KompasObject;


The button "Start" assign code:

Copy Source | Copy HTML
  1. begin
  2. // Create automation object KOMPAS_Graphic
  3. if Kompas = nil then
  4. begin
  5. {$ IFDEF __LIGHT_VERSION__}
  6. Kompas: = KompasObject ( CreateOleObject ('KompasLT.Application.5' ));
  7. {$ ELSE}
  8. Kompas: = KompasObject ( CreateOleObject ('Kompas.Application.5' ));
  9. {$ ENDIF}
  10. if Kompas <> nil then
  11. Kompas. Visible : = true;
  12. end;
  13. end;


The button "Exit" assign code:

Copy Source | Copy HTML
  1. begin
  2. if doc2d <> nil then doc2d: = nil;
  3. if doc3d <> nil then doc3d: = nil;
  4. if Kompas <> nil then begin
  5. // force close
  6. Kompas.Quit;
  7. Kompas: = nil;
  8. end ;
  9. end ;


Save the project and run it. The compass opens, the Compass closes, which is good.

4.3. We work with external variables 3d

We process input of values ​​in TEdit. We only need numbers.
And we will start working with the “Apply” button.
We will check for the presence of the bound object compass and the entered parameters. I will not intentionally paint error messages, because we have a clean demonstration, forgive me for the moveton.

I carried out all the processing of the part in a separate procedure, so that if necessary I could work with different products.
procedure TForm2.ReBuildPart (FileName: String);
we make a call from the handler of pressing the button “Apply” ReBuildPart ('Shaft.m3d');
where, as everyone already guessed, 'Shaft.m3d' is the name of the file with our shaft.

Variables to work with:
Copy Source | Copy HTML
  1. Var
  2. part: ksPart; // Store our part here
  3. varCol: ksVariableCollection; // collection of variable details
  4. variable : ksVariable; // Active variable
  5. i, count : integer ; // Counters
  6. Patch: String; // Path to the template file


Let's start by opening the model template file. (I have it saved in the “draw” subdirectory for convenience)
The procedure for processing external variables details:

Copy Source | Copy HTML
  1. Begin
  2. Patch: = ExtractFilePath (Paramstr (0)) + 'draw \' ; // Templates in my "draw" directory
  3. if Kompas <> nil then begin
  4. if FileIsThere (Patch + FileName) then begin
  5. doc3d: = ksDocument3D (Kompas.Document3D);
  6. if doc3d <> nil then
  7. doc3d. Open (Patch + FileName, false );
  8. end else exit ;
  9. end else exit ;
  10. part: = ksPart (doc3d.GetPart (pTop_Part)); // first part in the assembly
  11. // in our case, our item.
  12. if part <> nil then begin
  13. // work with an array of external variables
  14. varCol: = ksVariableCollection (part.VariableCollection ());
  15. if varCol <> nil then
  16. begin
  17. variable : = ksVariable (kompas.GetParamStruct (ko_VariableParam));
  18. if variable = nil then exit ; // Check if there are any?
  19. count : = varCol.GetCount (); // Number of external variables
  20. for i: = 0 to count - 1 do // Run through all variables
  21. begin
  22. variable : = ksVariable (varCol.GetByIndex (i));
  23. // Look at the variable name, if our, then assign the value.
  24. if variable .Name = 'L1' then variable . Value : = StrToFloat (edL1.Text);
  25. if variable .Name = 'L2' then variable . Value : = StrToFloat (edL2.Text);
  26. if variable .Name = 'L3' then variable . Value : = StrToFloat (edL3.Text);
  27. if variable .Name = 'D1' then variable . Value : = StrToFloat (edD1.Text);
  28. if variable .Name = 'D2' then variable . Value : = StrToFloat (edD2.Text);
  29. if variable .Name = 'D3' then variable . Value : = StrToFloat (edD3.Text);
  30. end ;
  31. // Insert the specification here.
  32. part.needRebuild: = true ;
  33. part. Update ;
  34. part.RebuildModel (); // rebuilding the part model
  35. end ;
  36. end ;
  37. doc3d.treeNeedRebuild: = true ;
  38. doc3d.RebuildDocument; // rebuild the document model
  39. // Overwrite the file.
  40. doc3d. Save ;
  41. doc3d. close ; // Optionally ... I don't like it when a lot of things are open ...
  42. // For the cognitive moment, you can comment out.
  43. End ;


Immediately there are questions: "Why so many times to rebuild the model?"
Immediately I will answer: “Sometimes it does not work. That variable "does not hook", then the rest of the values ​​"will not accept". It happens ... Different consumers have different versions, from licensed with occasional pop-up errors, to broken, sometimes ideally working ... "
(This is not a call to abandon the purchase of a licensed version, it is rather a request to local distributors to try to solve problems in a qualitative manner, even if sometimes Krivorukov, but Customers.)

Let's try the generated code in action.

Behind this, for the time being everything ... If there is strength, desire and mood, then we may see in part two

4.4. We work with the specification

4.5. We work with the name of the part

4.6. We work with a drawing

5. Small "workarounds". Or not always bad advice.

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


All Articles