Also, Thonny einbetten hat nicht funktioniert(meine fehlende Python/Python-umgebungskenntnisse)
Aber ich kann jetzt trotzdem meine Scripter ausführen/debuggen.
Ich habe folgendes gemacht (doch IPC):
In Hauptprogramm :
TPythonEngine, TPythonModule und TIdTCPServer Komponenten genommen.
Code: Alles auswählen
//PythonModule.OnInitialization:
procedure TdmPython.pmCADSys4Initialization(Sender: TObject);
begin
with Sender as TPythonModule do
begin
AddDelphiMethod('CAD_AddLine', CAD_AddLine, 'CAD_AddLine');
end;
end;
Die Methode ist natürlich implementiert und kann ganz normal auf Zeichnun zugreifen.
So schaut die Methode aus:
Code: Alles auswählen
function TdmPython.CAD_AddLine(pself, args : PPyObject ): PPyObject; cdecl;
var x0, y0, x1, y1: TRealType; colori: integer; layer: integer; TmpLine2D: TLine2D;
begin
if PythonEngine1.PyArg_ParseTuple( args, 'ffffii:AddLine', @x0, @y0, @x1, @y1, @colori, @layer) <> 0 then
begin
TmpLine2D := TLine2D.Create(10, Point2D(x0, y0), Point2D(x1, y1));
TmpLine2D.PenColor := colori;
TmpLine2D.Layer := layer;
fCADCmp2D.AddObject(TmpLine2D.ID, TmpLine2D);
end;
Result := PythonEngine1.ReturnNone;
end;
In Hauptprogramm :
TIdTCPServer.OnExecute Ereignis:
(Diese Code ist von Delphi Treff, Indy Tutorials. Danke)
Code: Alles auswählen
// 'CADSys4' ist PythonModule1.ModuleName.
procedure TdmPython.IdTCPServer1Execute(AContext: TIdContext);
var cmd: String;
begin
try
cmd := AContext.Connection.IOHandler.ReadLn;
cmd := 'CADSys4.' + cmd;
PythonEngine1.ExecString(UTF8Encode(cmd));
finally
//AContext.Connection.Disconnect;
end;
end;
Und eine ganz einfache dll (CADSys4AppIntrf.dll) erstellt. Eine TIdTCPClient Komponente genommen.
So schaut die Bibliothek aus:
Code: Alles auswählen
library CADSys4AppIntrf;
{$mode delphi}{$H+}
uses
Classes, SysUtils, Interfaces, Dialogs,
IdTCPClient, indylaz;
type
TrealType = single;
var IdTCPClient1: TIdTCPClient;
procedure CAD_AddLine(x0, y0, x1, y1: single; colori: integer; layer: integer); stdcall;
var cmd: string;
begin
cmd := 'CAD_AddLine('
+ FloatToStr(x0) + ', '
+ FloatToStr(y0) + ', '
+ FloatToStr(x1) + ', '
+ FloatToStr(y1) + ', '
+ IntToStr(colori) + ', '
+ IntToStr(layer) +
')';
IdTCPClient1.IOHandler.WriteLn(cmd);
end;
exports
CAD_AddLine;
initialization
DefaultFormatSettings.DecimalSeparator := '.';
try
IdTCPClient1 := TIdTCPClient.Create(nil);
IdTCPClient1.Host := '127.0.0.1';
IdTCPClient1.Port := 40000;
IdTCPClient1.Connect;
except
raise Exception.Create('can not connect to server!');
end;
finalization
IdTCPClient1.Disconnect;
IdTCPClient1.Free;
end.
Und folgende Script erstellt:
Code: Alles auswählen
#Dieser Teil, kann man in einer Python-Datei schreiben und importieren.
#######################################################################
a = os.environ['path']
a = os.getcwd() + ";" + a
os.environ['path'] = a
intrf = ct.windll.CADSys4AppIntrf
CAD_AddLine = intrf.CAD_AddLine
CAD_AddLine.restype = ct.c_int
CAD_AddLine.argtypes = [ct.c_float, ct.c_float, ct.c_float, ct.c_float, ct.c_int, ct.c_int]
############################################################################################
for i in range(0, 1800):
x = i/100;
y = math.sin(x);
CAD_AddLine(x, y, x+2, y+2, 100, 0)
Und Thonny starte ich im Hauptprogramm so:
Code: Alles auswählen
procedure TfrmMainLinux.acToolsPythonIDEExecute(Sender: TObject);
var AProcess: TProcess; pgm: string;
begin
try
AProcess := TProcess.Create(self);
{$IFDEF windows}
pgm := applicationh.GetAppPath + 'libs\Python311-amd64\Scripts\thonny.exe';
AProcess.Executable:= pgm;
{$ENDIF}
AProcess.Execute;
finally
AProcess.Free;
end;
end;
Das ist alles.
Diese Lösung ist besser als Thonny einzubettten weil ich jetzt jeder IDE/Debugger verwenden kann,
welche eine DLL laden kann.
Diese Script habe ich mit Thonny und PyScripter ausgeführt. Funktioniert problemlos.
Wenn man IP-Adresse und Port in eine Ini-Datei schreibt, wird das Ganze sogar netzerktransparent.
(

ich weiss, in dem Fall, macht es keinen Sinn).
Und Ergebnis schaut so aus:
http://www.hackcad.com/maindownloads/python.mp4
Ich freue mich auf eure Kommentare und Verbesserungsvorschläge.
Danke,
Maurog.