Outils pour utilisateurs

Outils du site


fr:documentation:script_example

Ceci est une ancienne révision du document !


Exemple de script

Cette page donne des conseils et des exemples de script.
Vous pouvez également regarder le code des boîte à outils standard à partir du programme.
Pour plus de détails sur une fonction spécifique, voir la page de référence des script.

Généralités

Nous examinons d'abord en détail le code du bouton Goto de la boîte à outils standard pour l'observation.
Cela couvre de nombreuses bases de programmation.

Le code complet du script ressemble à ceci:

// Slew telescope
  var ra,de: double;
      a,b,r: string;
      c: Tstringlist;
  begin
  memo_1.clear;
  if MenuTelescopeConnect.checked then begin
    if not StrToAR(Edit_1.text,ra) then begin memo_1.lines.add(rserror+' RA!');exit;end;
    if not StrToDE(Edit_2.text,de) then begin memo_1.lines.add(rserror+' DE!');exit;end;
    a:=floattostr(ra);
    b:=floattostr(de);
    GetSL('STRL1',c);
    c.clear;
    c.add('SLEW');
    c.add(a);
    c.add(b);
    r:=cmd('',c);
    c.clear;
    memo_1.lines.add(r);
  end
  else memo_1.lines.add(rsTelescopeNot);
  end.

Regardons chaque partie en détail:

// Slew telescope

C'est un commentaire, vous pouvez utiliser // {..} (*..*) pour marquer les commentaires.


  var ra,de: double;
      a,b,r: string;
      c: Tstringlist;

Définit les variables que nous utiliserons plus loin dans le script.
Les types de variable important sont: integer, double, string.
Le type Tstringlist est utilisé pour envoyer des commandes à Cartes du Ciel.


  begin

Le début de notre programme.


memo_1.clear;

Efface la zone de texte que nous utilisons pour afficher les messages. Cela garantit que la zone de texte n'est pas remplie par les messages précédents.


  if MenuTelescopeConnect.checked then begin

Nous testons la propriété Checked de l'élément de menu MenuTelescopeConnect. Ceci indique que nous avons connecté le télescope.
Si le résultat est vrai, nous exécutons le bloc de code commençant par “begin” jusqu'à “end” correspondant.


    if not StrToAR(Edit_1.text,ra) then begin memo_1.lines.add(rserror+' RA!');exit;end;

Nous essayons de convertir RA du format HMS de la zone de texte Edit_1 dans une valeur numérique. Si la conversion échoue (parce que nous tapons n'importe quoi dans la zone de texte) nous affichons un message d'erreur et nous quittons.


   a:=floattostr(ra);

Conversion de RA à nouveau en chaîne de caractères mais avec le format décimal requis par la commande.


GetSL('STRL1',c);
c.clear;

Demande un objet TStringList identifié par STRL1. Nous effaçons toutes les données qui peuvent rester dans l'objet.


    c.add('SLEW');
    c.add(a);
    c.add(b);

Ajoutez la commande et les paramètres requis (dans ce cas, RA et DEC) à la liste de chaînes.


   r:=cmd('',c);

Exécute la commande et stocke le résultat dans la variable r.


memo_1.lines.add(r);

Affiche le résultat de la commande dans la zone de texte.


else memo_1.lines.add(rsTelescopeNot);

Dans le cas où le test MenuTelescopeConnect.checked est faux, nous exécutons cette ligne.
Il affiche dans la zone de texte une traduction dans la langue locale de 'Télescope non connecté'.


 end.

La fin du programme.

Call an external library

You can define a function in an external library for use within your script as another local function.

This example implement a simple chronometer by using the GetTickCount function of the Windows API.
There is two button Start and Stop and two text box. A global integer variable is used to store the start time.

Script for the Start button:

 function GetTickCount: Longint; external 'GetTickCount@kernel32.dll stdcall';
 var
   tick: Longint;
 begin  
   tick:=GetTickCount;
   setI('Int1',tick);
   edit_1.text:='Started';
 end.

Script for the Stop button:

function GetTickCount: Longint; external 'GetTickCount@kernel32.dll stdcall';
var
  t: double;
  t1,tick: Longint;
begin
  tick:=GetTickCount;
  getI('Int1',t1);
  t:=double(tick-t1)/1000;
  edit_1.text:='';
  edit_2.text:=formatdatetime('HH:MM:SS.ZZZ',t/24/3600);
end.

You can call any library function this way but beware this is system dependent, the kernel32.dll library is not available on Mac or Linux.

Another limitation is that many library function expect a pointer to a parameter structure. As the script language use byte code internally (as Java) it cannot use a pointer to give the parameters. A solution is to write a C library wrapper that export the function with a flat parameter list.

Using ASCOM directly

This describe how to use an ASCOM device directly from your script without any use of the Skychart internal ASCOM telescope.

This can be use to access another class of device, the example here connect to a dome, or to access additional properties for your telescope.
In the later case you must be careful that your script work as a concurrent to Skychart main program for the device access.

Use the ASCOM chooser

The following code assigned to a button allow to select the ASCOM Dome driver we want to use. The driver name is saved in the text field Edit_1.

 var
   V: variant;
   w,s: widestring;
 begin
   V := CreateOleObject('ASCOM.Utilities.Chooser');
   w:='Dome';
   V.DeviceType:=w;
   s:=edit_1.text;
   s:=V.Choose(s);
   edit_1.text:=s;
   V:=Unassigned;
 end.

Replace w:='Dome'; by Telescope, Focuser, Rotator, Camera, Filter to select another driver class.

Connect to the ASCOM driver

The following code is for the “Connect” button. It connect to the ASCOM Dome driver we select previously. We use the global variable Dome1 to store the ASCOM object.

var
  D: variant;
  s: widestring;
begin
  s:=edit_1.text;
  getV('Dome1',D);
  if VarIsEmpty(D) then
    D := CreateOleObject(s);
  D.connected:=true;
  setV('Dome1',D);
end.

Use the ASCOM driver

Now we want to add a button to open the dome shutter. This is just an example, at this point any ASCOM property can be use.
The first test protect again a program crash if we try to use an initialized variant.
The second test protect again an ASCOM error if the dome is not connected.

var
  D: variant;
begin
  getV('Dome1',D);
  if (not VarIsEmpty(D)) then
    if D.Connected then
       D.OpenShutter;
end.

Disconnect the ASCOM driver

Add a button to disconnect the driver and release the resources.

var
  D: variant;
begin
  getV('Dome1',D);
  if (not VarIsEmpty(D)) then
    if D.Connected then
       D.connected:=false;
  D:=Unassigned;
  setV('Dome1',D);
end.

Open a document

The following code open the Skychart documentation page in the default web browser.
You can use any document type with this function, the document open with the default application the same way as if you double click the document in the file explorer.

begin
  OpenFile('doc\wiki_doc\en\documentation\start.html');
end.

Run a command

There is two different way to run an external command or program, depending if you want to wait for a result or not.

Wait for a result

The following command run the DIR command in the current directory. The result is stored in a stringlist and later show in a text memo. It contain the list of files in the directory.

var r:TstringList;
begin
  GetSL('STRL1',r);
  r.clear;
  RunOutput('dir',r);
  Memo_1.lines.assign(r);
end.

No wait

If the command can run for an undetermined time or do not produce an output you need to use the following form.
This example run the Variable star observer program and exit immediately.

begin
  Run('varobs');
end.
fr/documentation/script_example.1488291614.txt.gz · Dernière modification : 2017/02/28 15:20 de pch