POV-Ray : Newsgroups : povray.programming : Who to start PovRay from another program and render a scenery... : Re: How to start PovRay from another program and render a scenery... Server Time
25 Apr 2024 07:52:03 EDT (-0400)
  Re: How to start PovRay from another program and render a scenery...  
From: m steiger
Date: 3 Jan 2011 12:20:06
Message: <4d220546$1@news.povray.org>
I was extracting the needed code to a console application for testing.
Hope this implemention is useful for all Delphi coders and for coding in 
other languages too.

With Delphi create a new console application, save it as 'PovRayStarter' 
and replace the whole code with this one...

//********************************************************************
// PovRayStarter v1.0
// a console program for testing start of PovRay rendering with Delphi
//********************************************************************
{$D+,L+,Y+,R+,S+,Q+,O-,C+}

program PovRayStarter;

{$APPTYPE CONSOLE}

uses  SysUtils, ShellApi, Windows, Registry;

const CRLF = #13#10;      // carriage return + linefeed
       PovRayStartCommand36
         = 'C:/Programme/POV_Ray/POV-Ray 3.6/bin/pvengine.exe ';
       PovRayStartCommand37
         = 'C:/Programme/POV_Ray/POV-Ray 3.7/bin/pvengine-sse2.exe ';

var   povStartCommand, povStartParams: string;

//*********************************************************
// Gets a string value from the registry from the given 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)
     then begin
       // check if registry value is string or integer
       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 := GetRegistryString (HKEY_CLASSES_ROOT
                   , '\Applications\pvengine.exe\shell\open\Command', '');
   if povStartCommand = ''
   then povStartCommand := PovRayStartCommand36
   else begin
     // cut text after 'pvengine.exe '
     si := pos('pvengine.exe ', povStartCommand);
     delete (povStartCommand, si+12, 999);
   end;
end;
//*********************************************************
// execute Windows application
// input:    name: string     application name
//           param: string    application start parameters
// result:   boolean          =true application started
//           errorCode        <= 32 --> error
//                            e.g.  2 = ERROR_FILE_NOT_FOUND
// 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 (CRLF,'starting PovRay with command...', CRLF,'  ', 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:  ', CRLF
     ,'  can not start application ''' + povStartCommand + '''' + CRLF
     ,'  error code = ' + IntToStr(errorCode));
end;
//*********************************************************
// start rendering of povray scene
//---------------------------------------------------------
procedure StartPovRayRendering (const povfilename: string);
var
   result: boolean;
begin
   povStartParams := ' /EDIT ' + povfilename + ' /RENDER ' + povfilename;

   // try to start PovRay 3.6
   result := RunApplication (povStartCommand, povStartParams);
   if not result
   // try to start PovRay 3.7
   then RunApplication (PovRayStartCommand37, 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, CRLF
               ,'camera {location -3*z}', CRLF
               ,'light_source {<9,9,-9> 1 parallel}', CRLF
               ,'sphere {0 1 pigment {rgb<1,1,0.5>}}', CRLF);
   Close (pov);
end;
//*********************************************************
const
   povFilename = 'Test1.pov';
begin
   writeln (CRLF, '=== PovRayStarter v1.0 ===', CRLF);
   SavePovRayTestFile (povFilename);
   GetPovRayStartCommand;
   StartPovRayRendering (povFilename);
   sleep(7000);
end.


Post a reply to this message

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