Page 1 of 1

URecords Entity Record declaration syntax

PostPosted: Mon Feb 23, 2009 12:30 pm
by Jsosnowski
In reviewing the Urecords.pas file I found a basic Delphi language question. The declaration for an "Entity" is a pcked record with a series of standard fields follwowed by a case statement that identifies the differences in the specific entity types. (ie Lines have 2 points, circles have point radius and other data, etc.) The Case statement reads:
Code: Select all CASE aSInt OF
         entlin: (linpt1,
            linpt2: point);
         entln3: (ln3pt1,
            ln3pt2: point);
         ....
End;


aSint (an integer) is a declared type, not a variable, so what is the variable that determines what type of entity is in the record? The first field is enttype, but it is a byte, not an aSint.

Does anyone have any ideas, thoughts, knowledge, snide remarks?

PostPosted: Sun Mar 08, 2009 4:53 pm
by ERT
Hello,

aSint only is a declared type: this is no determines anything,
it's a needed structure formula

This is a variable record.

Only you determines that read the data from the record.

PostPosted: Tue Mar 10, 2009 10:30 am
by Jsosnowski
Here is what I learned:

When presented with a case statement in a record declaration, Delphi will allocate memory to encompass the largest requirements defined in the Case statement using the same memory area. The data for a line, circle polygon etc. all have the same offset (start point) in the memory structure for the record. The asint reference is not relevant except as a place holder for writing the record structure using the case statement. In use, the code that uses the data knows how to read the specifics. In the case of entities, the enttype field tells datacad what format should be used to read the data from the case statement. If the entity is a circle, it is read from the datablock as data for a circle. If you identified the data as a polygon, instead, but handed it the same data record, it would try to read the same memory area as if it contained a polygon (it would get meaningless information, but would still try).

Thanks ERT. Variable Records were an element of Delphi I had not been aware of in my reading. (I guess I fell asleep in that chapter.)

PostPosted: Thu Mar 12, 2009 12:39 am
by ERT
Hello

The RECORD structure is a simple structure.
Only data, have not knowledge. It is a simple data.

My favorite structure:
Code: Select all   
   TInteger = packed record case Byte of
       0: (L: Integer);
       1: (LW: LongWord);
       2: (WL, WH: Word);
       3: (W: array[0..1] of Word);
      13: (Wx: array[1..2] of Word);
       4: (SL, SH: SmallInt);
       5: (S: array[0..1] of SmallInt);
      15: (Sx: array[1..2] of SmallInt);
       6: (B0, B1, B2, B3: Byte);
      16: (Bx1, Bx2, Bx3, Bx4: Byte);
       7: (B: array[0..3] of Byte);
      17: (Bx: array[1..4] of Byte);
       8: (C0, C1, C2, C3: Char);
      18: (Cx1, Cx2, Cx3, Cx4: Char);
       9: (C: array[0..3] of Char);
      19: (Cx: array[1..4] of Char);
   end ;


All line read the same four bytes, but on different mode.

The other simple structures:
Code: Select all    TString: packed record case byte of
        1: (Str: ShortString);
        2: (Len: Byte;
             Data: array[1..255] of Byte);
    end ;

    TData: packed record case byte of
        1: (SS: ShortString);   //use all 256 bytes
        2: (I: Integer);        //use first 4 bytes
        3: (E: Extended);       //use first 10 bytes
        4: (W: Word);           //use first 2 bytes
        5: (C: Char);           //use first byte
        6: (AS: AnsiString);    //use first 4 bytes
    end ;

and a simple, one bye length structure: this is a simple converter
Code: Select all    TByte: packed record case byte of
        1: (B: Byte); 
        2: (I: ShortInt);       
        3: (O: Boolean);     
        4: (C: Char);           
    end ;

    var A: TByte;
        X: Char;

    begin
        A.B := 65;
        X := A.C;         //same X := Char(65); or X := Chr(65);
    end .                 //     type convert   or   special function



This is a game with bytes.

or a game with words:
Code: Select all    TWord: packed record case byte of
        1: (W: Word); 
        2: (B: array[1..2] of Byte);       
        3: (O: WordBool);     
        4: (S: SmallInt);           
    end ;


IMPORTANT: these games are only working correctly with packed record

From Delphi Help:
By default, the values in a structured type are aligned on word or double-word boundaries for faster access.
When you declare a structured type, you can include the reserved word packed to implement compressed data storage.
Using packed slows data access.


and look for "Variant parts in records" in Delphi Help.
You can found there excact description.
(Beacuse my English known is few.)