unit step101;

interface
  procedure LIBRARYENTRY( command: WORD ); Pascal;
  function  LIBRARYID: Cardinal; Pascal;
  function  LIBRARYNAME: PChar; pascal;
implementation
uses
  Windows,
  SysUtils,
  LDefin2D,
  ksConstTLB,
  ksAuto,
{$IFDEF __LIGHT_VERSION__}
  klTLB;
{$ELSE}
  ksTLB;
{$ENDIF}

var
  kompas: KompasObject;
  docActive: ksDocument2D;
  doc: ksDocument2D;
  spc: ksSpecification;

const SPC_NAME = 5;     //

//-----------------------------------------------------------------------------------------------
//       ""
//-----------------------------------------------------------------------------------------------
function EditSpcObjDet( geom: Reference ) : Reference;
var
  spcObj: Reference;
  par: ksUserParam;
  item: ksLtVariant;
  arr: ksDynamicArray;
begin
  spcObj := 0;
  //    ,      
  if docActive.ksEditMacroMode() <> 0 then
  begin
    //    
    spcObj := spc.ksGetSpcObjForGeom( 'graphic.lyt' , //  
                                   1,         //     
                                   0,         //  
                                   1, 1 );
    //   
    if spc.ksSpcObjectEdit( spcObj ) = 0 then
      spcObj := 0;
  end;

  //  ,      
  if ( spcObj <> 0 )  or ( spc.ksSpcObjectCreate( 'graphic.lyt', //  
                                   1,         //     
                                   20,  0,    //   
                                   0,0 ) <> 0 ) then    // 
  begin
    par := ksUserParam( kompas.GetParamStruct(ko_UserParam) );
 	  item := ksLtVariant( kompas.GetParamStruct(ko_LtVariant) );
		arr := ksDynamicArray( kompas.GetDynamicArray(LTVARIANT_ARR) );
		if ( par = nil ) or ( item = nil ) or ( arr = nil ) then
    begin
			Result := 0; exit;
    end;
		par.Init();
		par.SetUserArray( arr );
  	item.Init();
		item.strVal := '';
		arr.ksAddArrayItem( -1, item );
    //
    spc.ksSpcChangeValue( 5, 1, par, STRING_ATTR_TYPE  );

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

    spcObj := spc.ksSpcObjectEnd( );
    //          
    //      Cursor  Placement
    if ( spcObj <> 0 ) and ( spc.ksEditWindowSpcObject( spcObj ) <> 0 ) then
    begin
      Result := spcObj;
      exit;
    end;
  end;
  Result := 0;
end;

//----------------------------------------------------------------------------------------------
//    
//   ,    
//     Cursor  Placement
//----------------------------------------------------------------------------------------------
procedure DrawPosLeader( spcObj: Reference );
var
  info: ksRequestInfo;
  flag: boolean;
  posLeater: Reference;
  x1, y1: Double;
  j1: Integer;
begin
	info := ksRequestInfo( kompas.GetParamStruct(ko_RequestInfo) );
	info.Init();
  flag := true;
  posLeater := 0;
  while flag do
  begin
    info.commandsString := '! ! ';
    info.prompt := '   ';
    j1 := docActive.ksCursor( info, x1, y1, nil );
    case j1 of
      1 :
      begin //   
        posLeater := docActive.ksCreateViewObject( POSLEADER_OBJ );
        flag := false;
      end;
      2 : // 
      begin
        info.prompt := '  ';
        if docActive.ksCursor( info, x1, y1, nil ) <> 0 then
        begin
          posLeater := docActive.ksFindObj( x1, y1, 100 );      //   -   x,y
          if ( posLeater <> 0 ) and ( docActive.ksGetObjParam( posLeater, nil, 0) = POSLEADER_OBJ ) then
            flag := false
          else
          begin
            posLeater := 0;
            flag := true;
          end;
        end else
          flag := false;
      end;
      -1 :
      begin
        posLeater := docActive.ksFindObj( x1, y1, 100 );      //   -   x,y
        if ( posLeater <> 0 ) and ( docActive.ksGetObjParam( posLeater, nil, 0) = POSLEADER_OBJ ) then
          flag := false
        else begin
          kompas.ksError( '!     !' );
          posLeater := 0;
          flag := true;
        end;
      end;
    end;
  end;
  //  ,     
  if posLeater <> 0 then
  begin
    //     
    if spc.ksSpcObjectEdit( spcObj ) <> 0 then
    begin
      //  
      spc.ksSpcIncludeReference( posLeater, 1 );
      //  
      spc.ksSpcObjectEnd();
    end;
  end;
end;

//------------------------------------------------------------------------------
//    
//---
procedure TypeAttrBolt;
var
  attr: ksAttributeObject;
  typeAttr: ksAttributeTypeParam;
  col: ksColumnInfoParam;
  arr: ksDynamicArray;
  nameFile: string;
  numbType: Double;
begin
	attr := ksAttributeObject( kompas.GetAttributeObject() );
	typeAttr := ksAttributeTypeParam( kompas.GetParamStruct(ko_AttributeType) );
	col := ksColumnInfoParam( kompas.GetParamStruct(ko_ColumnInfoParam) );
	if ( typeAttr <> nil ) and ( col <> nil ) and ( attr <> nil ) then
  begin
		typeAttr.Init();
		col.Init();
		typeAttr.header := '111';    // o- 
		typeAttr.rowsCount := 1;									 // -   
		typeAttr.flagVisible := true;             // ,     
		typeAttr.password := '';         // ,      -     
		typeAttr.key1 := 10;
		typeAttr.key2 := 20;
		typeAttr.key3 := 30;
		typeAttr.key4 := 0;
		arr := ksDynamicArray( typeAttr.GetColumns() );
		if arr <> nil then
    begin
    	//   1 " ."
			col.header := ' .'; // o- 
			col.type_ := STRING_ATTR_TYPE;       //     - .
			col.key := 1;                       //  ,        
			col.def := '';         //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

      //   2 ""
			col.header := '';// o- 
			col.type_ := UINT_ATTR_TYPE;         //     - .
			col.key := 3;                       //  ,        
			col.def := '1';            //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

      //  3 ""
			col.header := '';    // o- 
			col.type_ := STRING_ATTR_TYPE;       //     - .
			col.key := 0;                       //  ,        
			col.def := 'M';            //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

      //  4 ""
			col.header := '';   // o- 
			col.type_ := UINT_ATTR_TYPE;         //     - .
			col.key := 3;                       //  ,        
			col.def := '12';           //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

      //  5 ""
			col.header := '';          // o- 
			col.type_ := STRING_ATTR_TYPE;       //     - .
			col.key := 0;                       //  ,        
			col.def := 'x';            //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

      //  6 ""
			col.header := '';       // o- 
			col.type_ := FLOAT_ATTR_TYPE;        //     - .
			col.key := 3;                       //  ,        
			col.def := '1.25';         //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

      //  7 " "
			col.header := ' ';// o- 
			col.type_ := STRING_ATTR_TYPE;       //     - .
			col.key := 3;                       //  ,        
			col.def := '-6g';          //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

      //  8 ""
			col.header := '';          // o- 
			col.type_ := STRING_ATTR_TYPE;       //     - .
			col.key := 0;                       //  ,        
			col.def := 'x';            //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

			//  9 ""
			col.header := '';     // o- 
			col.type_ := UINT_ATTR_TYPE;         //     - .
			col.key := 3;                       //  ,        
			col.def := '60';           //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

			//  10 ". "
			col.header := '. ';// o- 
			col.type_ := STRING_ATTR_TYPE;       //     - .
			col.key := 0;                       //  ,        
			col.def := '58';           //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

			//  11 ""
			col.header := '';  // o- 
			col.type_ := STRING_ATTR_TYPE;       //     - .
			col.key := 0;                       //  ,        
			col.def := '.35';         //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

			//  12 ""
			col.header := '';  // o- 
			col.type_ := STRING_ATTR_TYPE;       //     - .
			col.key := 0;                       //  ,        
			col.def := '.16';          //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

			//  13 ""
			col.header := '';      // o- 
			col.type_ := STRING_ATTR_TYPE;       //     - .
			col.key := 0;                       //  ,        
			col.def := '';         //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

			//  14 ""
			col.header := '';     // o- 
			col.type_ := UINT_ATTR_TYPE;         //     - .
			col.key := 2;                       //  ,        
			col.def := '7808';         //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

			//  15 ""
			col.header := '';          // o- 
			col.type_ := STRING_ATTR_TYPE;       //     - .
			col.key := 0;                       //  ,        
			col.def := '-';            //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );

			//  16 ""
			col.header := '';       // o- 
			col.type_ := STRING_ATTR_TYPE;       //     - .
			col.key := 0;                       //  ,        
			col.def := '70';           //   
			col.flagEnum := false;              //   ,    
			arr.ksAddArrayItem( -1, col );
    end;
		//  
		nameFile := kompas.ksChoiceFile( '*.lat', '', false );
		if nameFile <> '' then
    begin
			//  
			numbType := attr.ksCreateAttrType( typeAttr,       //    
			  																	nameFile ); //    
			if numbType > 1 then
				kompas.ksMessage( Format( 'numbType = %f', [ numbType ] ) )
			else
				kompas.ksMessageBoxResult(); //     
		end;
		arr.ksDeleteArray(); //   
	end;
end;

//------------------------------------------------------------------------------
//   
//---
procedure CreateDet;
var
  gr, spcObj: Reference;
begin
  //     1
  gr := docActive.ksNewGroup(0);
    docActive.ksLineSeg ( 20, 30, 70, 30, 2 );
    docActive.ksLineSeg ( 70, 30, 70, 80, 2 );
    docActive.ksLineSeg ( 70, 80, 20, 80, 2 );
    docActive.ksLineSeg ( 20, 80, 20, 30, 2 );
  docActive.ksEndGroup();
  //   
  spc := ksSpecification(docActive.GetSpecification);
  if spc = nil then  exit;

  spcObj := EditSpcObjDet( gr );
  if spcObj <> 0 then
    DrawPosLeader( spcObj );
end;

//------------------------------------------------------------------------------
//  
//---
procedure CreateStandart;
var
  spcObj: Reference;
  par: ksUserParam;
  item: ksLtVariant;
  arr: ksDynamicArray;
  uBuf, resultat: Integer;
begin
  spc := ksSpecification(docActive.GetSpecification);
  if spc = nil then  exit;

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

  resultat := spc.ksSpcObjectCreate( 'graphic.lyt' , //  
                                   1,         //     
                                   25,  0,    //       
                                   313277777065.0 , 0 );
  if resultat <> 0 then //       spc.lat
  begin
    par := ksUserParam( kompas.GetParamStruct(ko_UserParam) );
 	  item := ksLtVariant( kompas.GetParamStruct(ko_LtVariant) );
		arr := ksDynamicArray( kompas.GetDynamicArray(LTVARIANT_ARR) );
		if ( par = nil ) or ( item = nil ) or ( arr = nil ) then
			exit;
		par.Init();
		par.SetUserArray( arr );
  	item.Init();
		item.strVal := '222';
		arr.ksAddArrayItem( -1, item );

    spc.ksSpcChangeValue( SPC_NAME , 1, par, STRING_ATTR_TYPE  );
    //
    uBuf := 2;
    spc.ksSpcVisible( SPC_NAME, 2, 1 );
    arr.ksClearArray();
    item.Init();
    item.intVal := uBuf;
    arr.ksAddArrayItem( -1, item );
    spc.ksSpcChangeValue( SPC_NAME , 2, par, UINT_ATTR_TYPE  );

    // 
  	arr.ksClearArray();
   	item.Init();
	 	item.doubleVal := 40;
	  arr.ksAddArrayItem( -1, item );
    spc.ksSpcChangeValue( SPC_NAME , 4, par, DOUBLE_ATTR_TYPE  );

    //  
    spc.ksSpcVisible( SPC_NAME, 5, 1 );
    spc.ksSpcVisible( SPC_NAME, 6, 1 );   //
    arr.ksClearArray();
    item.Init();
    item.floatVal := 1;
    arr.ksAddArrayItem( -1, item );
    spc.ksSpcChangeValue( SPC_NAME , 6, par, FLOAT_ATTR_TYPE  );

    //   
    spc.ksSpcVisible( SPC_NAME, 7, 0 );

    // 
 	  arr.ksClearArray();
	  item.Init();
	  item.intVal := 55;
	  arr.ksAddArrayItem( -1, item );
	  spc.ksSpcChangeValue( SPC_NAME , 9, par, UINT_ATTR_TYPE );

    //   
    spc.ksSpcVisible( SPC_NAME, 10, 0 );

    //  
    spc.ksSpcVisible( SPC_NAME, 11, 0 );

    //  
    spc.ksSpcVisible( SPC_NAME, 12, 0 );

    // 
 	  arr.ksClearArray();
	  item.Init();
	  item.floatVal := 7808;
	  arr.ksAddArrayItem( -1, item );
	  spc.ksSpcChangeValue( SPC_NAME , 14, par, UINT_ATTR_TYPE );

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

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

//------------------------------------------------------------------------------
//k   
//---
procedure DecomposeSpc;
var
  spec: ksSpcDocument;
  pDoc, group: Reference;
  pageCount: Integer;
  spcGabarit: ksRectParam;
  docPar: ksDocumentParam;
  i, column: Integer;
  mathBop: ksMathPointParam;
  mathTop: ksMathPointParam;
  x, y, row: Double;
begin
  doc  := ksDocument2D(kompas.Document2D);
	spec := ksSpcDocument( kompas.SpcActiveDocument() );
  if ( doc <> nil ) and ( spec <> nil ) then
  begin
		//   
		pDoc := spec.reference;
		if pDoc = 0 then	exit;

    //   
    pageCount := spec.ksGetSpcDocumentPagesCount();
    //    
	  spcGabarit := ksRectParam( kompas.GetParamStruct(ko_RectParam) );
		if spcGabarit = nil then exit;

    doc.ksGetObjGabaritRect( pDoc, spcGabarit );

    // 
	  docPar := ksDocumentParam( kompas.GetParamStruct(ko_DocumentParam) );
		if docPar = nil then exit;

		docPar.Init();
    docPar.regime := 0;
    docPar.type_ := 3;
    doc.ksCreateDocument ( docPar );
    for i := 0 to pageCount - 1 do
    begin
      //   i-  
      group := doc.ksDecomposeObj( pDoc,        //  
                                              0,  //  0- ,,,:1-,,; 2-, , 
                                              0.4, // 
                                              i+1);  // 0 -      1-   
      if group <> 0 then
      begin
        column := i mod 3;
				mathBop := ksMathPointParam( spcGabarit.GetpBot() );
				mathTop := ksMathPointParam( spcGabarit.GetpTop() );
				if ( mathBop = nil ) and ( mathTop = nil ) then
					exit;
        x := ( mathTop.X - mathBop.X + 5)*column;
        row := i div 3;
        y := ( mathTop.Y - mathBop.Y + 5)*row;
        // 
        doc.ksMoveObj( group, x, -y );
        //  
        doc.ksStoreTmpGroup( group );  // 
        doc.ksClearGroup( group, true );
        doc.ksDeleteObj( group );
      end;
    end;
    kompas.ksMessage( Format( ' %d  ', [ pageCount ] ) );
  end else
    kompas.ksError( '   ' );
end;

//------------------------------------------------------------------------------
// 
//---
procedure ShowSpc;
var
  spec: ksSpcDocument;
  iter: ksIterator;
  obj, columnType, ispoln, blok: LongInt;
  count, i, colNumb: Integer;
  spcColPar: ksSpcColumnParam;
  buf: string;
begin
  spec := ksSpcDocument( kompas.SpcActiveDocument() );
	if ( spec <> nil ) and ( spec.Reference <> 0 ) then
  begin
    spc := ksSpecification(spec.GetSpecification);
    if spc = nil then  exit;

		iter := ksIterator( kompas.GetIterator() );
		iter.ksCreateSpcIterator( '', 0, 0 );
		if iter.Reference <> 0 then
    begin
			obj := iter.ksMoveIterator( 'F' );
      while spec.ksExistObj(obj) <> 0 do
      begin
        //      
        count := spc.ksGetSpcTableColumn( '', 0, 0 );
        kompas.ksMessage( Format( '-  = %d', [ count ] ) );

        //    
        for i := 1 to count do
        begin
          //     ,    
          spcColPar := ksSpcColumnParam( kompas.GetParamStruct(ko_SpcColumnParam) );
          if spc.ksGetSpcColumnType( obj,          // 
                                i,      //  ,   1
                                spcColPar ) <> 0 then
          begin
            // 
            columnType := spcColPar.columnType;
            ispoln := spcColPar.ispoln;
            blok := spcColPar.block;
            buf := spc.ksGetSpcObjectColumnText( obj, columnType, ispoln, blok );
            kompas.ksMessage( buf );
            //  ,       
            colNumb := spc.ksGetSpcColumnNumb(  obj,         // 
              spcColPar.columnType, spcColPar.ispoln, spcColPar.block );
            kompas.ksMessage( Format( 'i = %d colNumb =%d', [ i, colNumb ] ) );
          end;
        end;
        obj := iter.ksMoveIterator( 'N' );
      end;
//      } while ( obj = iter.ksMoveIterator( CString("N") ) );
    end;
  end	else
    kompas.ksError( '   ' );
end;


//------------------------------------------------------------------------------
// LibraryName
//---
function LIBRARYNAME: PChar; pascal;
begin
  Result := '   ';
end;

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

//------------------------------------------------------------------------------
// LibraryEntry
//---
procedure LIBRARYENTRY( command: WORD ); pascal;
begin
  kompas := KompasObject(CreateKompasObject);

  if kompas <> nil then
  begin
//    doc := ksDocument2D(kompas.Document2D);
//    spc := ksSpcDocument(doc.GetSpecification);

//    if (doc = nil ) or ( spc = nil ) then  exit;

    if ( command = 2 ) or ( command = 3 ) then
    begin
      docActive := ksDocument2D(kompas.ActiveDocument2D);
      if ( docActive <> nil ) then
      begin
        case command of
          2  : CreateDet;      //   
          3  : CreateStandart; //  
        end;
      end else
        kompas.ksError( '   ' );
    end
    else begin
      case command of
        1  : TypeAttrBolt;   //    
        4  : DecomposeSpc;   //k   
        5  : ShowSpc;        // 
      end;
    end;

    doc := nil;
    kompas := nil;
    docActive := nil;
  end;
end;

end.
