using a delphi unit directly in script

Hello all and thanks in advance for your help

Is there a way to use the above BatiprixUnit.pas directly in scripts without adding it in "uses" clause of the calling program? (for example, using the ScripterProIDE demo prog without any change, create a new project for using the BatiprixUnit objects) ?

unit BatiprixUnit;

interface

uses
Pkg.Json.DTO, System.Generics.Collections, REST.Json.Types;

{$M+}

type
TElements = class;
TUnit = class;

TUnit = class
private
FDescription: string;
FLabel: string;
published
property Description: string read FDescription write FDescription;
property &Label: string read FLabel write FLabel;
end;

TElements = class(TJsonDTO)
private
FDescription: string;
FElementType: string;
[JSONName('Elements'), JSONMarshalled(False)]
FElementsArray: TArray;
[GenericListReflect]
FElements: TObjectList;
FId: Integer;
FLabel: string;
FNomenclature: string;
FOrder: Integer;
FQuantity: Double;
FTotalPrice: Double;
FUnit: TUnit;
FUnitPrice: Double;
function GetElements: TObjectList;
protected
function GetAsJson: string; override;
published
property Description: string read FDescription write FDescription;
property ElementType: string read FElementType write FElementType;
property Elements: TObjectList read GetElements;
property Id: Integer read FId write FId;
property &Label: string read FLabel write FLabel;
property Nomenclature: string read FNomenclature write FNomenclature;
property Order: Integer read FOrder write FOrder;
property Quantity: Double read FQuantity write FQuantity;
property TotalPrice: Double read FTotalPrice write FTotalPrice;
property &Unit: TUnit read FUnit;
property UnitPrice: Double read FUnitPrice write FUnitPrice;
public
constructor Create; override;
destructor Destroy; override;
end;

TOuvrages = class(TJsonDTO)
private
FCode: string;
FCostPrice: Double;
FDescription: string;
[JSONName('Elements'), JSONMarshalled(False)]
FElementsArray: TArray;
[GenericListReflect]
FElements: TObjectList;
FFlatCost: Double;
FId: Integer;
FLabel: string;
FLaborFlatCost: Double;
FLaborQuantity: Double;
FLaborUnitCost: Double;
FMillesimeId: Integer;
FNomenclature: string;
FSellingPrice: Double;
FUnit: TUnit;
function GetElements: TObjectList;
protected
function GetAsJson: string; override;
published
property Code: string read FCode write FCode;
property CostPrice: Double read FCostPrice write FCostPrice;
property Description: string read FDescription write FDescription;
property Elements: TObjectList read GetElements;
property FlatCost: Double read FFlatCost write FFlatCost;
property Id: Integer read FId write FId;
property &Label: string read FLabel write FLabel;
property LaborFlatCost: Double read FLaborFlatCost write FLaborFlatCost;
property LaborQuantity: Double read FLaborQuantity write FLaborQuantity;
property LaborUnitCost: Double read FLaborUnitCost write FLaborUnitCost;
property MillesimeId: Integer read FMillesimeId write FMillesimeId;
property Nomenclature: string read FNomenclature write FNomenclature;
property SellingPrice: Double read FSellingPrice write FSellingPrice;
property &Unit: TUnit read FUnit;
public
constructor Create; override;
destructor Destroy; override;
end;

TRoot = class(TJsonDTO)
private
FCustomerProjectId: string;
FOk: Boolean;
[JSONName('ouvrages'), JSONMarshalled(False)]
FOuvragesArray: TArray;
[GenericListReflect]
FOuvrages: TObjectList;
function GetOuvrages: TObjectList;
protected
function GetAsJson: string; override;
published
property CustomerProjectId: string read FCustomerProjectId write FCustomerProjectId;
property Ok: Boolean read FOk write FOk;
property Ouvrages: TObjectList read GetOuvrages;
public
destructor Destroy; override;
end;

implementation

{ TElements }

constructor TElements.Create;
begin
inherited;
FUnit := TUnit.Create;
end;

destructor TElements.Destroy;
begin
FUnit.Free;
GetElements.Free;
inherited;
end;

function TElements.GetElements: TObjectList;
begin
Result := ObjectList(FElements, FElementsArray);
end;

function TElements.GetAsJson: string;
begin
RefreshArray(FElements, FElementsArray);
Result := inherited;
end;

{ TOuvrages }

constructor TOuvrages.Create;
begin
inherited;
FUnit := TUnit.Create;
end;

destructor TOuvrages.Destroy;
begin
FUnit.Free;
GetElements.Free;
inherited;
end;

function TOuvrages.GetElements: TObjectList;
begin
Result := ObjectList(FElements, FElementsArray);
end;

function TOuvrages.GetAsJson: string;
begin
RefreshArray(FElements, FElementsArray);
Result := inherited;
end;

{ TRoot }

destructor TRoot.Destroy;
begin
GetOuvrages.Free;
inherited;
end;

function TRoot.GetOuvrages: TObjectList;
begin
Result := ObjectList(FOuvrages, FOuvragesArray);
end;

function TRoot.GetAsJson: string;
begin
RefreshArray(FOuvrages, FOuvragesArray);
Result := inherited;
end;

end.

Yes, you should register in scripter the classes from that unit you want to use from the script, using one of the many registration methods as described here:

https://doc.tmssoftware.com/biz/scripter/guide/scripter.html#accessing-delphi-objects

Probably the easiest and most straightforward way is to register the classes using RTTI.

For example:

Scripter.DefineClassByRTTI(TElements);
Scripter.DefineClassByRTTI(TOuvrages);

Hello Wagner,

Thanks for your answer.

I think i've'nt been enough clean/clear in my question:
how to register this classes inside a script and not in the delphi (calling) program?

Thanks for your help
Best regards
Eric

What do you mean by "register this classes inside a script"?
A registration is a process to make a Delphi class available to be used from script, so it doesn't make sense to "register" the class inside the script.

Hello,

Thanks for your reply.

This is just what i want to see.
My boss would now understand i don't lie when i said it was not possible. :slight_smile: He wanted someone of TMS to confirm.

Thanks again
Regards
Eric

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.