unit Step101;
//******************************************************************/
//                (c)                     */
//*                -, 1991, 1996                     */
//*                 -   5.x                      */
//******************************************************************/
//*                                              */
//*                                      */                                                       */
//******************************************************************/

interface
type
  //       spc.lat
BOLT = record
  dr    : single   ;  // 
  p     : single   ;  // 
  s     : single   ;  //   
  h     : single   ;  //  
  D     : single   ;  //    
  d3    : single   ;  //  .  
  h2    : single   ;  //  
  L     : cardinal ;  //  
  l1    : single   ;  //     
  b     : single   ;  //   
  d2    : single   ;  //   
  z4    : single   ;  //   
  k     : smallInt ;  // 1    (   b) ; 2 -   (       )
  f     : word     ;  //      
  klass : smallInt ;  // 
  gost  : word     ;  // 
end;

 procedure  LIBRARYENTRY( comm: WORD  ); Pascal;
 function   LIBRARYID   : Cardinal;      Pascal;
 procedure  TypeAttrBolt;
 procedure  CreateDet;
 procedure  CreateStandart;
 procedure DecomposeSpc;
 procedure  ShowSpc;

 implementation

 uses  Sysutils, LtDefine, LibTool, LDefin2D;

 const
 STR225   = 200;
 STR226   = 201;
 STR227   = 202;
 STR228   = 203;
 MENU_POS_LEADER= 204;
 ISPOLN      = $80;   //  1 >0  2
 PITCH       = $2;    //   >0  
SPC_NAME                =  5;     //

//----------------------------------------------------------------------------------------
//
//---
function   LIBRARYID : Cardinal;  pascal;
begin
  Result:=100;
end;

//----------------------------------------------------------------------------------------
//
//---
procedure  LIBRARYENTRY( comm : Word  );  pascal;
begin
  case ( Comm ) of
    1 : begin //     
        TypeAttrBolt();
        end;
    2 : begin //   
        CreateDet();
        end;
    3 : begin //  
        CreateStandart();
        end;
    4 : begin //   
        DecomposeSpc;
        end;
    5 : begin // 
        ShowSpc();
        end;
  end;
end;
{
       "":

     ,   
  .      ,
       ,      
    .
     apptool  .

   :
   1 12  1.25 -6g x 60.58.35X.16  7808 - 70

      :

                  

1       .                                 1
2             1                        uint      3
3                                             0 (   )
4                12                       uint      4  3 (   )
5                                        0 (   )
6                    1.25                     float     5  3 (   )
7            -6g                          6  3 (   )
8                                        0 (   )
9                  60                       uint      7  3 (   )
10     .     .58                          0 (   )
11              .35                         0 (   )
12              .16                          0 (   )
13                                           0 (   )
14                 7808                     uint      2
15           -                            0 (   )
16                   70                           0 (   )
}

//-------------------------------------------------------------------------
//      
//-------------------------------------------------------------------------
procedure TypeAttrBolt;
const
//    
ST_KEY1                 = 100;    //   
ST_KEY2_GOST            =  5;      //  
ST_KEY3                 =  1;
SPC_NAME                =  5;     //

var
  attrType   : ksAttributeType ;
  parStruct1 : ColumnInfo ;
  name :array [0..255] of char;
//-------------------------------------------------------------------------------
procedure AddColumn( header: PChar; _type, key : Integer; def : PChar );
begin
  StrCopy(parStruct1.header, header);// o- 
  parStruct1._type := _type;    //     - .
  parStruct1.key := key;                    //  ,   
  StrCopy(parStruct1.def,def);         //   
  //    
  AddArrayItem( attrType.columns, -1, Addr(parStruct1), sizeof(parStruct1) );
end;
//-------------------------------------------------------------------------------
begin
//    
  if ( ksChoiceFile( '*.lat',' (*.lat)|*.lat|  (*.*)|*.*|', name, 255, 0) = 0) then
    exit;

//   
  StrCopy(attrType.header,'111');   // o- 
  attrType.rowsCount        := 1;    // -   
  attrType.flagVisible      := 1;    // ,     
  StrCopy(attrType.password,'');     // ,      -     
  attrType.columns          := CreateArray( ATTR_COLUMN_ARR, nil ); //    
  attrType.key1 := ST_KEY1;
  attrType.key2 := ST_KEY2_GOST;
  attrType.key3 := ST_KEY3;
  attrType.key4 := 0;


   //  
   FillChar( parStruct1, sizeof(parStruct1), 0 );
   //   1 " ."
   AddColumn( ' .'{header},STRING_ATTR_TYPE{ _type}, 1{key}, ''{def} );

   //   2 ""
   AddColumn( ''{header}, UINT_ATTR_TYPE{ _type}, 3{key}, '1'{def} );

   //  3 ""
   AddColumn( ''{header}, STRING_ATTR_TYPE{ _type}, 0{key}, ''{def} );

   //  4 ""
   AddColumn( ''{header}, UINT_ATTR_TYPE{ _type}, 3{key}, '12'{def} );

   //  5 ""
   AddColumn( ''{header}, STRING_ATTR_TYPE{ _type}, 0{key}, 'x'{def} );

   //  6 ""
   AddColumn( ''{header}, FLOAT_ATTR_TYPE{ _type}, 3{key}, '1.25'{def} );

   //  7 " "
   AddColumn( ' '{header}, STRING_ATTR_TYPE{ _type}, 3{key}, '-6g'{def} );

   //  8 ""
   AddColumn( ''{header}, STRING_ATTR_TYPE{ _type}, 0{key}, 'x'{def} );

   //  9 ""
   AddColumn( ''{header}, UINT_ATTR_TYPE{ _type}, 3{key}, '60'{def} );

   //  10 ". "
   AddColumn( '. '{header}, STRING_ATTR_TYPE{ _type}, 0{key}, '.58'{def} );

   //  11 ""
   AddColumn( ''{header}, STRING_ATTR_TYPE{ _type}, 0{key}, '.35X'{def} );

   //  12 ""
   AddColumn( ''{header}, STRING_ATTR_TYPE{ _type}, 0{key}, '.16'{def} );

   //  13 ""
   AddColumn( ''{header}, STRING_ATTR_TYPE{ _type}, 0{key}, ''{def} );

    //  14 ""
   AddColumn( ''{header}, UINT_ATTR_TYPE{ _type}, 2{key}, '7808'{def} );

    //  15 ""
   AddColumn( ''{header}, STRING_ATTR_TYPE{ _type}, 0{key}, '-'{def} );

   //  16 ""
   AddColumn( ''{header}, STRING_ATTR_TYPE{ _type}, 0{key}, '70'{def} );

    //     

  ksCreateAttrType( Addr(attrType),   //    
                                           name ); //    

 MessageBoxResult();  //     

 //   
 DeleteArray( attrType.columns );

end;
//-----------------------------------------------------------------------------------------------
//       ""
//-----------------------------------------------------------------------------------------------
function EditSpcObjDet( geom : reference  ): reference ;
var
  spcObj : reference;
  ss : array[0..128] of char;
  j : integer;
begin
  spcObj := 0;
  //    ,      
  if ( EditMacroMode<>0 ) then
  begin
    //    
    spcObj := ksGetSpcObjForGeom( 'graphic.lyt' , //  
                                   1,         //     
                                   0,         //  
                                   1, 1 );
    //   
    if ( ksSpcObjectEdit( spcObj )=0 ) then
      spcObj  := 0;
  end;

 //  ,      
 if ( spcObj<>0) Or  (ksSpcObjectCreate( 'graphic.lyt', //  
                                   1,         //     
                                   20,  0,    //   
                                   0,0 )<>0) then    // 
 begin
   StrCopy(ss, '' );
   //
   ksSpcChangeValue( SPC_NAME{ } , 1, Addr(ss), STRING_ATTR_TYPE  );

   // 
   if ( geom <>0) then
     ksSpcIncludeReference( geom, 1 );

   spcObj := ksSpcObjectEnd( );
   //          
   //      Cursor  Placement
   if ( spcObj<>0 ) then
   begin
     j := ksEditWindowSpcObject(spcObj);
     if ( j=0 )then
       spcObj := 0;
   end;
 end;
 EditSpcObjDet := spcObj;

end;


//----------------------------------------------------------------------------------------------
//    
//   ,    
//     Cursor  Placement
//----------------------------------------------------------------------------------------------
procedure DrawPosLeader( _spcObj : reference );
var
  info      : RequestInfo ;
  flag      : boolean;
  posLeater : reference;
  x1, y1    : double ;
  j1        : integer;
//-------------------------------------------------------------------------------
procedure FindPosLeadir;
begin
  posLeater := FindObj( x1, y1, 100 );      //   -   x,y
  if ( posLeater<>0 ) And (GetObjParam( posLeater, nil, 0, 0) <> POSLEADER_OBJ ) then
  begin
     Error('!     !' );
     posLeater := 0;
     flag := true;
  end else
    flag := false;
end;
//-------------------------------------------------------------------------------
Begin
  flag := false;
  posLeater := 0;


  repeat
    FillChar(info,  sizeof(info), 0 );
    info.commands := PChar(MENU_POS_LEADER);

    info.prompt := '   ';
    j1 := ksCursor( Addr(info), x1, y1, nil );
    case ( j1 ) of
      1:begin //   
        posLeater := ksCreateViewObject( POSLEADER_OBJ );
        flag := false;
        end;
      2:begin // 
        FillChar(info,  sizeof(info), 0 );
        info.commands :='  .';
        if( ksCursor( Addr(info), x1, y1, nil )<>0 ) then
          FindPosLeadir
        else
          flag := false;
        end;
      -1:begin
        FindPosLeadir;
        end;
    end;
  until( flag = false );

  //  ,     
  if ( posLeater<>0  ) then
  begin
    //     
    if ( ksSpcObjectEdit( _spcObj )<>0 ) then
    begin
      //  
      ksSpcIncludeReference( posLeater, 1 );
      //  
      ksSpcObjectEnd;
    end;
  end;
end;
//------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------
procedure  CreateDet;
var
  gr : reference;
  spcObj : reference;
begin
 //     1
 gr :=  NewGroup(0);
      LineSeg ( 20, 30, 70, 30, 2 );
      LineSeg ( 70, 30, 70, 80, 2 );
      LineSeg ( 70, 80, 20, 80, 2 );
      LineSeg ( 20, 80, 20, 30, 2 );
 EndGroup();
 //   
 spcObj := EditSpcObjDet( gr );
 if ( spcObj<>0 ) then
   DrawPosLeader( spcObj  );

end;

//-----------------------------------------------------------------------------------------------
//       " "
//-----------------------------------------------------------------------------------------------
function EditStandartSpcObj (tmp : BOLT;  geom : reference  ): reference ;
var
  spcObj : reference;
  uBuf   : Cardinal;
begin

  spcObj := 0;
  //    ,      
  if ( EditMacroMode<> 0 ) then
  begin
    //    
    spcObj := ksGetSpcObjForGeom( 'graphic.lyt' , //  
                                   1,         //     
                                   0,         //  
                                   1, 1 );
    //   
    if ( ksSpcObjectEdit( spcObj )=0 )then
      spcObj  := 0;
  end;

 if ( spcObj<>0 ) Or  (ksSpcObjectCreate( 'graphic.lyt' , //  
                                   1,         //     
                                   25,  0,    //       
                                   313277777065.0 , 0 ) <> 0) then //       spc.lat
 begin
   //
   if( (tmp.f And ISPOLN)=0 ) then//  
     ksSpcVisible( SPC_NAME, 2, 0 ) // 
   else begin
     uBuf :=  2;
     ksSpcVisible( SPC_NAME, 2, 1 );
     ksSpcChangeValue( SPC_NAME , 2, Addr(uBuf), UINT_ATTR_TYPE  );
   end;

   // 
   ksSpcChangeValue( SPC_NAME , 4, Addr(tmp.dr), FLOAT_ATTR_TYPE  );

   //  
   if( (tmp.f And PITCH)=0 )then//    
   begin
     ksSpcVisible( SPC_NAME, 5, 0 );
     ksSpcVisible( SPC_NAME, 6, 0 );   //
   end
   else begin
     ksSpcVisible( SPC_NAME, 5, 1 );
     ksSpcVisible( SPC_NAME, 6, 1 );   //
     ksSpcChangeValue( SPC_NAME , 6, Addr(tmp.p), FLOAT_ATTR_TYPE );
   end;

   //   
   ksSpcVisible( SPC_NAME, 7, 0 );

   // 
   ksSpcChangeValue( SPC_NAME , 9, Addr(tmp.L), UINT_ATTR_TYPE  );

   //   
   ksSpcVisible( SPC_NAME, 10, 0 );

   //  
   ksSpcVisible( SPC_NAME, 11, 0 );

   //  
   ksSpcVisible( SPC_NAME, 12, 0 );

   // 
   uBuf := Cardinal(tmp.gost);
   ksSpcChangeValue( SPC_NAME , 14, Addr(uBuf), UINT_ATTR_TYPE  );

   // 
   if ( geom<>0 ) then
     ksSpcIncludeReference( geom, 1 );

   spcObj := ksSpcObjectEnd( );
   //          
   //.      Cursor  Placement
   if ( spcObj <>0 ) then
     if ( ksEditWindowSpcObject( spcObj )=0 ) then
       spcObj :=0;
 end;
 EditStandartSpcObj :=spcObj;
end;

//-------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------
procedure  CreateStandart;
var
  tmp : BOLT;
  spcObj :reference;
begin

 tmp.gost := 7809;
 tmp.f:=0;
 tmp.s:=27; tmp.h:=11; tmp.D:=30.1;
 tmp.L:=55; tmp.l1:=49; tmp.b:=46;  tmp.h2:=10;  tmp.klass:=1; {klass=B}
 tmp.d2:=22.5;  tmp.k    := 2;
 tmp.dr := 20;
 //   
 spcObj := EditStandartSpcObj( tmp, 0 );
 if ( spcObj<>0 ) then
   DrawPosLeader( spcObj  );

end;
//-----------------------------------------------------------------------------------------------
//   
//-----------------------------------------------------------------------------------------------
procedure DecomposeSpc;
var
  pDoc : reference;
  pageCount,i,row : integer;
  spcGabarit : RectParam ;
  doc : DocumentParam   ;
  buf : string;
  group : reference;
  column : integer;
  x,y : double;
begin
  //   
  pDoc := ksGetCurrentDocument( 2 );
  if ( pDoc <> 0 ) then
  begin
    //   
    pageCount := ksGetSpcDocumentPagesCount( pDoc );
    //    

    GetObjGabaritRect( pDoc, Addr(spcGabarit) );

    // 
    FillChar( doc,  sizeof(doc), 0 );
    doc.regim :=                0;
    doc._type :=                 3;
    CreateDocument ( Addr(doc) );
    for i := 0 to pageCount-1 do
    begin
      //   i-  
      group :=  DecomposeObj( pDoc,        //  

                              0,  //  0- ,,,;
                                             //                  1-,,; 2-, , 
                              0.4, // 
                              Byte(i+1) );  // 0 -      1-   
      if ( group <> 0 ) then
      begin

        column := i Mod 3;
        x := (spcGabarit.pTop.x-spcGabarit.pBot.x + 5)*column;
        row := i Div 3;
        y := (spcGabarit.pTop.y-spcGabarit.pBot.y + 5)*row;
        // 
        MoveObj( group, x, -y );
        //  
        StoreTmpGroup( group );  // 
        ClearGroup( group );
        DeleteObj( group );
      end;
    end;
    buf := Format( ' %d  ', [pageCount] );
    ksMessage( PChar(buf) );
  end  else
    Error( '   ' );
end;

//-----------------------------------------------------------------------------------------------
//  
//-----------------------------------------------------------------------------------------------
procedure ShowSpc;
var
  iter : reference;
  spcObj : reference;
  count,i : integer;
  par : SpcColumnParam  ;
  buf1 : string;
  buf : array[0..255] of char;
  colNumb : integer;
begin
  //      
  iter := CreateSpcIterator( nil, 0, 0 );
  if ( iter<>0 ) then
  begin
    //    
    spcObj := MoveIterator( iter, 'F' );
    if ( (spcObj<>0) And (ExistObj(spcObj)>0) ) then
    begin
      repeat
        //      
        count := ksGetSpcTableColumn( nil, 0, 0 );
        //    
        for i := 1 to count do
        begin
          //     ,    

          if ( ksGetSpcColumnType( spcObj,          // 
                                  i,      //  ,   1
                                  Addr(par) )>0)then
          begin
            // 
            ksGetSpcObjectColumnText  ( spcObj,       // 
                                        par.columnType, //  SPC_CLM_FORMAT...SPC_CLM_USER
                                        par.ispoln,     //  
                                        par.block,
                                        buf,          //
                                        250 );        //  s
            buf1 := Format ( 'type = %d  =%d block = %d', [par.columnType,par.ispoln,par.block] );
            strcat ( buf, PChar(buf1) );
            ksMessage( buf );
            //  ,       
            colNumb := ksGetSpcColumnNumb(  spcObj,         // 
                                            par.columnType, //  SPC_CLM_FORMAT...SPC_CLM_USER
                                            par.ispoln,     //   
                                            par.block );    // 
            buf1 := Format ( 'i = %d colNumb =%d', [i, colNumb] );
            ksMessage( PChar(buf1) );

          end;
        end;
        //    
        spcObj := MoveIterator( iter, 'N' );
      until( spcObj=0)  Or  (ExistObj(spcObj )=0 );
    end;
    DeleteIterator( iter );
  end;
end;

end.
