POV-Ray : Newsgroups : povray.programming : How to start POV-Ray from Delphi ! : How to start POV-Ray from Delphi ! Server Time
6 Oct 2024 03:17:53 EDT (-0400)
  How to start POV-Ray from Delphi !  
From: m steiger
Date: 2 Dec 2021 11:15:00
Message: <web.61a8eeee55e26774b1e9fe83e4f0ed64@news.povray.org>
For all Delphi programmers who want to render their 3d scenery with PovRay -
here is a sample code of how to start PovRay from your 3d application.
Of course, before rendering, you have to export your own PovRay data file first
;-)

Hint: Save following source code in file 'PovRayStarter.dpr'.
-------------------------------------------------------------

//********************************************************************
// Project File:   PovRayStarter.dpr  v1.02   2021-01-03
// A Delphi console example program for starting 'PovRay rendering'.
// PovRayStarter.exe tested with:  Delphi-6 release with      8.704 Bytes
//                                 Delphi-Sydney-CE with  1.162.752 Bytes
// Ref.:
https://www.delphipraxis.net/35458-wie-ruft-man-povray-aus-einem-programm-heraus-auf.html
//********************************************************************
{$D+,L+,Y+,R+,S+,Q+,O-,C+}   // debug
{$D-,L-,Y-,R-,S-,Q-,O-,C-}   // release

{$APPTYPE CONSOLE}

program PovRayStarter;

uses  SysUtils, ShellApi, Windows, Registry;

const
  NL = #13#10;   // NewLine = carriage return + linefeed
  PovRayStartCommand36 = 'C:/Program Files/POV_Ray/POV-Ray 3.6/bin/pvengine.exe
+FP ';
  PovRayStartCommand37 = 'C:/Program Files/POV_Ray/POV-Ray 3.7/bin/pvengine.exe
';
  // set your individual start command here:
  PovRayStartCommand37b=
'E:\Programming_Tools\POV_Ray\POV-Ray_3.7\bin\pvengine32-sse2.exe';

var
  povStartCommand, povStartParams: string;

//*********************************************************
// Gets a string value from the registry from the given root and sub key.
// Converts integer value to a string
// Returns '', if the sub key or value name are unknown.
//---------------------------------------------------------
function GetRegistryString (const rootKey: HKEY;
                            const subKey, name: string): string;
var
  reg: TRegistry;            // registry access object
  valueInfo: TRegDataInfo;   // info about registry value
begin
  Result := '';
  reg := TRegistry.Create;
  try
    reg.RootKey := rootKey;
    if reg.OpenKeyReadOnly (subKey)
    then if reg.GetDataInfo (name, valueInfo)  // check if registry value is
string or integer
    then begin
      case valueInfo.RegData of
        // string value: just return it
        rdString, rdExpandString:   Result := reg.ReadString (name);
        // integer value: convert to string
        rdInteger:   Result := IntToStr (reg.ReadInteger(name));
      end;
    end;
  finally
    reg.CloseKey;
    reg.Free;
  end;
end;
//*********************************************************
// get start command of PovRay Renderer
//---------------------------------------------------------
procedure GetPovRayStartCommand;
var si: integer;
begin
  povStartCommand := paramStr(1);
  if povStartCommand = ''
  then begin
    povStartCommand := GetRegistryString (HKEY_CLASSES_ROOT
                    , '\Applications\pvengine.exe\shell\open\Command', '');
    if povStartCommand <> ''
    then begin
      // cut text after 'pvengine.exe"'
      si := pos('pvengine.exe"', povStartCommand);
      if si > 0
      then delete (povStartCommand, si+13, 999);
    end;
  end;
  if povStartCommand = ''
  then povStartCommand := PovRayStartCommand36;
  if povStartCommand = '' then
    povStartCommand := PovRayStartCommand37;
  writeln ('Start command of PovRay program : '#13#10,'  ', povStartCommand);
end;
//*********************************************************
// execute Windows application
// input:    name: string     application name
//           param: string    application start parameters
// result:   boolean          =true application started
//           errorCode        e.g.  2 = ERROR_FILE_NOT_FOUND
// see:
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Input-Output_Errors
// examples: started := RunApplication ('pvengine.exe', '/RENDER example.pov');
//           started := RunApplication ('ReadMe.txt', '');  // view text file
//           RunApplication ('notepad', 'info.txt');        // view text file
with notePad
//           RunApplication ('Explorer', 'C:\');            // start windows
explorer
//---------------------------------------------------------
function RunApplication (const name, param: string): boolean;
var   errorCode: cardinal;
begin
  writeln (NL,'try starting PovRay with command...', NL, name, param);

  errorCode := ShellExecute (0, 'open', PChar(name), PChar(param), nil,
SW_SHOWNORMAL);

  result := (errorCode = 0) or (errorCode = 42);
  if result
  then writeln ('  successfully started ')
  else writeln ('RunApplication:  ', NL
               ,'  can not start application ''', name+param, '''' + NL
               ,'  error code = ' + IntToStr(errorCode));

  // german info from Delphi-Praxis:
  //   https://www.delphipraxis.net/126563-shellexecute-error.html
end;
//*********************************************************
// start rendering of povray scene
//---------------------------------------------------------
procedure StartPovRayRendering (const povfilename: string);
var
  result: boolean;
begin
  povStartParams := ' /EDIT ' + povfilename
                  + ' /RENDER ' + povfilename
                  + ' +FN';   // PNG output see
http://www.povray.org/documentation/view/3.6.1/219/

  // try to start PovRay 3.6
  result := RunApplication (povStartCommand, povStartParams);
  if not result
  // try to start PovRay 3.7b
  then RunApplication (PovRayStartCommand37b, povStartParams);
end;
//*********************************************************
// create povray test scene file
//---------------------------------------------------------
procedure SavePovRayTestFile (const povfilename: string);
var  pov: text;
begin
  Assign (pov, povfilename);
  ReWrite (pov);
  writeln (pov, '// File: ' + povfilename, NL
              , 'camera {location -3*z}', NL
              , 'light_source {<9,9,-9> 1 parallel}', NL
              , 'sphere {0 1 pigment {rgb<1,1,0.5>}}', NL);
  Close (pov);
end;
//*********************************************************
const
  povFilename = 'Test1.pov';
begin
  writeln (NL, '=== PovRayStarter v1.02 ===', NL);
  SavePovRayTestFile (povFilename);
  GetPovRayStartCommand;
  StartPovRayRendering (povFilename);
  sleep(7000);
end.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.