Menu

Create a class from an object or multi objects

Help
Mostafa
2018-01-09
2018-02-09
  • Mostafa

    Mostafa - 2018-01-09

    Hi,
    How can I create a class to draw a grid by TGLCylinder or pipe ? I want this grid is an object and it is not created by lines. I want this object to be specified in the scene with a name so that it is known by clicking on any part like the standard objects. Help me please.

     
  • Jerome.D (BeanzMaster)

    Hi, from sratch :

    MyGrid :=Class(TGLCustomSceneObject)
    private
       MyGrid : array[0..0,0..0] of TGLCylinder
       FSizeX, FSizeY, FStepX, FStepY   :  Single
     protected
         procedure InitGrid;
         procedure DrawMyGrid
     public
        procedure RendeObject; override ;  //Check this for args in GLScene.pas
    
    
        Initialization
             RegisterClass(['MyGrid']);  // or registerobject dont remember
    
     

    Last edit: Jerome.D (BeanzMaster) 2018-01-10
  • Mostafa

    Mostafa - 2018-01-18

    Thank you Jerome.D,
    Give me more explanation please ,
    or
    If there is an example here, please give me.

     
  • Jerome.D (BeanzMaster)

    If you want can construct your own object with others glscene objects you must herited your object from TGLCustomSceneObject else your object will no be display.
    forwhat you want to do and a more concrete sample take look at the code in units GLGizmo.pas and for a bit more complicated GLGizmoEX you'll see how each part a drawn from GLScene's object You can also take a look at this https://sourceforge.net/p/glscene/discussion/93606/thread/44c2c3b7/2f3b/attachment/glfreeformex.pas on how to make your own object from TGLFreeForm

     

    Last edit: Jerome.D (BeanzMaster) 2018-01-18
  • Mostafa

    Mostafa - 2018-01-20

    Hi,
    I tried to write the following code:

    type
    TMyGrid = Class(TGLCustomSceneObject)
    private
    TMyGrid : array of TGLCylinder;
    FSizeX, FSizeY, FStepX, FStepY : single ;
    x_pos,y_pos,z_pos : single;
    protected
    procedure SetPosition(X,Y,Z:single);
    procedure SetDimension(X_Size,Y_Size:single);
    procedure SetRowNum(R_Num:Integer);
    procedure SetColNum(C_Num:Integer);
    // procedure InitGrid;
    procedure DrawMyGrid;
    public

    procedure RendeObject; override ;  //Check this for args in GLScene.pas
    

    End;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    {TMyGrid}
    procedure TMyGrid.SetPosition(X,Y,Z:single);
    begin
    x_pos:=X;
    y_pos:=Y;
    z_pos:=z;
    end;

    procedure TMyGrid.SetDimension(X_Size: Single; Y_Size: Single);
    begin
    FSizeX:=X_Size;
    FSizeY:=Y_Size;
    end;

    procedure TMyGrid.SetRowNum(R_Num: Integer);
    begin
    if R_Num<1 then
    R_Num:=2;

    FStepY:=FSizeY/R_Num;

    if FStepY=0 then
    FStepY:=2;
    end;

    procedure TMyGrid.SetColNum(C_Num: Integer);
    begin
    if C_Num<1 then
    C_Num:=2;

    FStepX:=FSizeX/C_Num;

    if FStepX=0 then
    FStepX:=2;
    end;

    procedure Tmygrid.DrawMyGrid;
    begin
    SetLength(MyGrid_X,1);
    MyGrid_X[0].Position.x:=x_pos;
    MyGrid_X[0].Position.y:=y_pos;
    MyGrid_X[0].Position.z:=z_pos;
    MyGrid_X[0].TopRadius:=0.5;
    MyGrid_X[0].Bottomradius:=0.5;

    end;

    Initialization
        RegisterClass(['TMyGrid']);  // or registerobject dont remembe
    

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    But there are some problems about rendering and initialization :
    1-Is my code correct?
    2- I do not know which function to use for rendering. This function exists in the GLscene.pas:
    procedure Render(var ARci: TGLRenderContextInfo);
    or
    procedure Render(baseObject: TGLBaseSceneObject = nil); override;

    Should I apply this function exactly? whitout any change?

    3- and about Initialization too

     

    Last edit: Mostafa 2018-01-20
  • Jerome.D (BeanzMaster)

    Ok first at first time remove RegisterClass(['TMyGrid']); not need, it's just form glscene editor in ide.

    2nd you need create TGLZCylinder whatelse you 'll never show there
    MyGrid_X[index] := TGLCylinder.Create

    and don't forget to free all cylinder in a destructor Destruct
    Take a look more deeply in GLGizmo Unit see how each part is construct and how the object can be rendered. All the answer are in

     

    Last edit: Jerome.D (BeanzMaster) 2018-01-20
  • Mostafa

    Mostafa - 2018-02-08

    thank you Jerome,
    I did a simple job to better understanding, I derived a class from one of the GLscene classes, for example:

    type
    TMyGLPipe = class(TGLPipe)
    end;

    I used this class in my program and I plotted the pipe with TMyGLPipe class easily. I saved the scene. But when I try to reload the file by GLScene.LoadFromFile, I encounter the following error:
    Class TMyGLPipe Not found,

    Can you help me to find out what the solution to this problem is?

     

    Last edit: Mostafa 2018-02-08
  • Jerome.D (BeanzMaster)

    Hi Mostafa, it's normal it's a new object you need override some function. But
    1st : Don't forget to register your new object in source\designtim\GLSceneRegister.Pas
    Add your unit in uses
    find where objects are registered and add

      RegisterSceneObject(TMyGLPipe, 'MyPipe', glsOCAdvancedGeometry, HInstance);
    

    rebuild GLScene Package to take in charge your new object. Normally it will be work

    If you have added your own properties, it will possible you'll must add "Store" keyword after property myvalue:integer read FMyValue write FMyValue Store IsMyValueStored

     

    Last edit: Jerome.D (BeanzMaster) 2018-02-09
  • Jerome.D (BeanzMaster)

    An another thing you must also override procedure assign and call the inherited in.

     

Log in to post a comment.

MongoDB Logo MongoDB