Registriert seit: 8. Dez 2007
Ort: Graz
4 Beiträge
Delphi 10.4 Sydney
|
AW: Wie ruft man POVRAY aus einem Programm heraus auf ?
2. Dez 2021, 16:24
Tja, ist schon eine Weile her, daß diese Frage gestellt wurde!
Ich starte schon sehr lange das PovRay Rendering Programm ( http://www.povray.org/) von meinem 3d-Programm aus.
Eine etwas umfangreichere Lösung zur gestellten Frage möchte ich allen Interessierten nicht vorenthalten, da man sonst zu diesem Thema eher wenig detaillierte Infos erhält.
Das Testprogramm geht wie folgt vor:
- übergebene Programmparameter checken
- Startkommando aus Registry checken
- ev. eigenes Startkommando verwenden
- PovRay starten und dabei Bild als PNG-Datei speichern
Übrigens, der Unterschied der beiden Exe-Dateien ist beachtlich!
Delphi-Quellcode:
//********************************************************************
// 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.
|