////////////////////////////////////////////////////////////////////////////////
//
//     
//
////////////////////////////////////////////////////////////////////////////////
unit Step1_API7_2D_1;

interface
uses
  Sysutils, LDefin2D, ksAuto, Libtool, Windows;

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

  procedure  LIBRARYENTRY( command: WORD  ); Pascal;
  function   LIBRARYID   : Cardinal;         Pascal;
  function   LoadStr( ID : Integer ) : string;                     //    
  function   LibMessage( str : string; flags : integer ) : Integer;//   
  function   LibMessageByID( strId, flags : Integer ) : Integer;   //   
  procedure  GetNewKompasAPI;
  procedure  NewMarkOnLeader();
  procedure  NewMarkOnLine();
  procedure  NewMultiTextLeader();
  procedure  NewBrace;
  procedure  AddStrAxis;
  procedure  NewUnitnumber;
  procedure  NewDraft;
  procedure  NewUnitMarking;
  procedure  NewCutUnitMarking;

implementation

uses
  LtDefine,
  ksApi7,
  ksConstTLB,
  forms;


var
  newKompasAPI : IApplication;                                //  Application 7



//------------------------------------------------------------------------------
// LibraryId
//---
function LIBRARYID: UINT; pascal;
begin
  Result := 100;  // IDR_LIB
end;
//------------------------------------------------------------------------------
// LibraryEntry
//---
procedure LIBRARYENTRY( command: WORD ); pascal;
begin
  Application.Handle := GetHWindow;
  GetNewKompasAPI;
  if ( newKompasAPI <> nil ) then
  begin
    case command of
      1 : NewMarkOnLeader();  //    -                                     //  1
      2 : NewMarkOnLine();    //  . 
      3 : NewMultiTextLeader(); // 
      4 : NewBrace;             // 
      5 : AddStrAxis;           //  
      6 : NewUnitnumber;        //   
      7 : NewDraft;        // 
      8 : NewUnitMarking;    // 
      9 : NewCutUnitMarking;    //   
  end;
  newKompasAPI := nil;
end;
end;

//-------------------------------------------------------------------------------
//       (COM)
// ---
function GetViews : IViews;

var
   pKompasDocument2D : IKompasDocument2D;
   pViewsAndLayersManager : IViewsAndLayersManager;
begin
  Result := nil;
  //     
  pKompasDocument2D := newKompasAPI.ActiveDocument As IKompasDocument2D;
  if ( pKompasDocument2D <> nil) then
  begin
    //       
    pViewsAndLayersManager := pKompasDocument2D.ViewsAndLayersManager;

    if ( pViewsAndLayersManager <> nil ) then
    begin
      Result := pViewsAndLayersManager.Views;
    end;
  end;
end;

//-------------------------------------------------------------------------------
// _1_    -
// ---
procedure NewMarkOnLeader;

var
  ppViews : IViews;
  ppView  : IView;
  ppBuildingContainer : IBuildingContainer;
  ppMarks : IMarks;
  pMarkOnL : IMarkOnLeader;
  pTextAfter : IText;
  strAfter : WideString;

begin
  //    
  ppViews := GetViews();
    if( ppViews <> nil ) then
    begin
      //    
      // ActiveView-     
      ppView := ppViews.ActiveView;
      //    As  
      //     
      ppBuildingContainer := ppView As IBuildingContainer;

        if (  ppBuildingContainer <> nil ) then
        begin
          //    
          ppMarks := ppBuildingContainer.Marks;
          if( ppMarks <> nil ) then
          begin
            //   
            pMarkOnL := ppMarks.Add( ksDrMarkOnLeader	) As IMarkOnLeader;
            if ( pMarkOnL <> nil )then
              begin
                //  
                pMarkOnL.X := 250;
                pMarkOnL.Y := 150;
                pMarkOnL.ArrowType := ksLeaderArrow;
                pMarkOnL.ShelfDirection := ksLSUp;
                pMarkOnL.AddBranchByPoint( 1, FALSE, 100, 100);
                // 
                pMarkOnL.Update();
                //  
                pTextAfter := pMarkOnL.TextAfter;
                strAfter := ' ';
                pTextAfter.Str :=  strAfter;
                // 
                pMarkOnL.Update();

              end
          end
        end
    end;
    ksMessage('  - - ');
end;
//-------------------------------------------------------------------------------
// _2_  / 
// ---
procedure NewMarkOnLine;
var
  ppViews : IViews;
  ppView  : IView;
  ppBuildingContainer : IBuildingContainer;
  pDrawingContainer : IDrawingContainer;
  ppMarks : IMarks;
  pMarkOnLine : IMarkOnLine;
  pTextAfter : IText;
  strAfter : WideString;
  pLineSegments : ILineSegments;
  pLineSegment : ILineSegment;
begin
  ppViews := GetViews();
  if( ppViews <> nil ) then
  begin
    //    
    // ActiveView-     
    ppView := ppViews.ActiveView;
      //  -    
      //     
          pDrawingContainer := ppView As IDrawingContainer;
           if (  pDrawingContainer <> nil ) then
           begin
            //    
            pLineSegments := pDrawingContainer.LineSegments;
             if( pLineSegments <> nil)  then
             begin
                //   
                pLineSegment := pLineSegments.Add;
                  if ( pLineSegment <> nil )then
                  begin

                    pLineSegment.X1   := 100 ;
                    pLineSegment.Y1   := 100 ;
                    pLineSegment.X2   := 150 ;
                    pLineSegment.Y2   := 110 ;
                    //   
                    pLineSegment.Style  := 1 ;
                    // 
                    pLineSegment.Update();
                  end
             end
           end;
        //     
        ppBuildingContainer := ppView As IBuildingContainer;
          if (  ppBuildingContainer <> nil ) then
          begin
            //    
            ppMarks := ppBuildingContainer.Marks;
              if( ppMarks <> nil)  then
              begin
                //   
                pMarkOnLine := ppMarks.Add( ksDrMarkOnLine	) As IMarkOnLine;
                  if ( pMarkOnLine <> nil )then
                  begin
                    //  
                    pTextAfter := pMarkOnLine.TextAfter;
                    strAfter := ' ';
                    pTextAfter.Str :=  strAfter;
                    //    
                    pMarkOnLine.Line := pLineSegment;
                    //   
                    pMarkOnLine.Position := ksMTextOnLine;
                    // 
                    pMarkOnLine.Update();
                  end
              end
          end;
    ksMessage('/ ');
  end;
end;

//-------------------------------------------------------------------------------
// 3       ()
// ---
procedure NewMultiTextLeader;
var
  ppViews : IViews;
  ppView  : IView;
  ppBuildingContainer : IBuildingContainer;
  pMTLs : IMultiTextLeaders;
  pMTL : IMultiTextLeader;
  pText : IText;
  strAfter : WideString;
begin
  ppViews := GetViews();
  if( ppViews <> nil ) then
  begin
    //    
    // ActiveView-     
    ppView := ppViews.ActiveView;
    if( ppView <> nil ) then
    begin
      //     
      ppBuildingContainer := ppView As IBuildingContainer;
      if( ppBuildingContainer <> nil ) then
      begin
        //         ()
        pMTLs := ppBuildingContainer.MultiTextLeaders;
        if(pMTLs <> nil) then
        begin
          //   
          pMTL := pMTLs.Add;
          if( pMTL <> nil) then
          begin
            //  
            pMTL.Form := TRUE;
            pMTL.ArrowType := ksLeaderArrow;
            pMTL.ShelfX := 250;
            pMTL.ShelfY := 250;
            pMTL.TextDirection := TRUE;
            pMTL.ShelfDirection := ksLSRight;
            // 
            pMTL.AddBranchByPoint( 1, 100, 100);
            pMTL.AddBranchByPoint( -1, 120, 100);
            pMTL.AddBranchByPoint( -1, 130, 100);
            //  
              pText := pMTL.Text;
              strAfter := ' ';
              pText.Str :=  strAfter;
             // 
            pMTL.Update();
          end
        end
      end;
    end;
  end;
  ksMessage('    ');
end;

//-------------------------------------------------------------------------------
// 4   
// ---
procedure NewBrace;
var
  ppViews : IViews;
  ppView  : IView;
  ppBuildingContainer : IBuildingContainer;
  pBraces : IBraces;
  pBrace : IBrace;
  pText : IText;
  strText : WideString;
begin
  ppViews := GetViews();
  if( ppViews <> nil ) then
  begin
    //    
    // ActiveView-     
    ppView := ppViews.ActiveView;
    if( ppView <> nil ) then
    begin
      //     
      ppBuildingContainer := ppView As IBuildingContainer;
      if( ppBuildingContainer <> nil ) then
      begin
        //    
        pBraces := ppBuildingContainer.Braces;
        if(pBraces <> nil) then
        begin
          //   
          pBrace := pBraces.Add;
          if( pBrace <> nil) then
          begin
            //  
            pBrace.X1 := 100;
            pBrace.Y1 := 100;
            pBrace.Alignment := 0;
            pBrace.Angle := 145;
            pBrace.Direction := FALSE;
            pBrace.Length := 100;
            pBrace.Radius := 8;
            pBrace.ShelfDirection := ksLSRight;
            pBrace.Style := 7;

            //  
              pText := pBrace.Text;
              strText := ' ';
              pText.Str :=  strText;
             // 
            pBrace.Update();
          end
        end
      end;
    end;
  end;
  ksMessage(' ');
end;


//-------------------------------------------------------------------------------
//     .
//
// ---
procedure AddStrAxis();
var
  pViews : IViews;
  pView  : IView;
  pBuildContainer : IBuildingContainer;
  pAxes : IBuildingAxes;
  pAxis : IStraightAxis;
  pText : IText;
  pText1 : IText;
  pText2 : IText;
  pText3 : IText;
  pText4 : IText;
  pText1l : IText;
  pText2r : IText;
  pText01r : Itext;
  strText : WideString;
  strText1 : WideString;
  strText2 : WideString;
  strText3 : WideString;
  strText4 : WideString;
  strText1l : WideString;
  strText2r : WideString;
  strText01r : WideString;
  pNodes : IMarkNodes;
  pNodes1l : IMarkNodes;
  pNodes0l : IMarkNodes;
  pNodes0r : IMarkNodes;
  pNodes2R : IMarkNodes;
  pNode0 : IMarkNode;
  pNode1 : IMarkNode;
  pNode2 : IMarkNode;
  pNode3 : IMarkNode;
  pNode4 : IMarkNode;
  pNode0l1 : IMarkNode;
  pNode0lr : IMarkNode;
  pNode1l1 : IMarkNode;
  pNode2r1 : IMarkNode;
  pJut1 : IAxisJut;
  pJut2: IAxisJut;
begin
  //   
  pviews := GetViews();
  if ( pviews <> nil ) then
  begin
    //    
    pView := pViews.ActiveView;
    //     
    pBuildContainer := pView As IBuildingContainer;
      if ( pBuildContainer <> nil )then
      begin
        //    
        pAxes := pBuildContainer.BuildingAxes;
        if (pAxes <> nil ) then
        begin
        //    
          pAxis := pAxes.Add ( ksDrStraightAxis	) As IStraightAxis;
          if ( pAxis <> nil ) then
          begin
            //  
            pAxis.Angle := 60;
            pAxis.Length := 70;
            pAxis.DoubleMark := TRUE;
            pAxis.MarkSize := 30;
            pAxis.X1 := 100;
            pAxis.Y1 := 100;
              //  
              //     1
              pJut1 := pAxis.Jut[TRUE];
                if ( pJut1 <> nil ) then
                begin
                pJut1.MarkOn := TRUE;
                pJut1.BreakDirection := TRUE;
                pJut1.BreakOffset := 25;
                pJut1.Length := 30;
                pJut1.MarkOffset := 24;

                end;
              //     2
              pJut2 := pAxis.Jut[FALSE];
                if ( pJut2 <> nil ) then
                begin
                pJut2.MarkOn := TRUE;
                pJut2.BreakDirection := TRUE;
                pJut2.BreakOffset := 25;
                pJut2.Length := 30;
                pJut2.MarkOffset := 24;

                end;

            //  
              pText := pAxis.Text;
              strText := ' ';
              pText.Str :=  strText;

            pAxis.Update();
             //    
              pNodes := pAxis.MarkNodes;
              if (pNodes <> nil ) then
              begin
              //    
                pNode0 := pNodes.Item [0];
                if (pNode0 <> nil ) then
                begin
                  //       
                  pNodes0l := pNode0.MarkNodes[true];
                         if (pNodes0l <> nil ) then
                         begin
                         //    
                         pNode0l1 := pNodes0l.Add ( ksMarkRefCircle, -1 );
                          if ( pNode0l1 <> nil ) then
                          begin
                          pNode0l1.DoubleMark := TRUE;
                          pNode0l1.RefLength := 40;
                          end
                         end;
                   pNodes0r := pNode0.MarkNodes[false];
                         if (pNodes0r <> nil ) then
                         begin
                         //    
                         pNode0lr := pNodes0r.Add ( ksMarkText, -1 );
                          if ( pNode0lr <> nil ) then
                          begin
                          //  
                          pText01r := pNode0lr.Text;
                          strText01r := '    ';
                          pText01r.Str :=  strText01r;
                          end
                         end
                end;

              //    
                pNode1 := pNodes.Add ( ksMarkCircle, -1 );
                if ( pNode1 <> nil) then
                begin
                //  
                pText1 := pNode1.Text;
                strText1 := '  ';
                pText1.Str :=  strText1;
                pAxis.Update();

                  pNode2 := pNodes.Add ( ksMarkCircle, -1 );
                  if ( pNode2 <> nil) then
                  begin
                  pText2 := pNode2.Text;
                  strText2 := ' ';
                  pText2.Str :=  strText2;

                    pNode3 := pNodes.Add ( ksMarkCircle, -1 );
                    if ( pNode3 <> nil ) then
                    begin
                    //  
                    pText3 := pNode3.Text;
                    strText3 := ' ';
                    pText3.Str :=  strText3;

                      pNode4 := pNodes.Add ( ksMarkRefCircle, -1 );
                      if ( pNode3 <> nil ) then
                      begin
                      //  
                      pText4 := pNode4.Text;
                      strText4 := ' ';
                      pText4.Str :=  strText4;

                        if ( pNode1 <> nil) then
                      //      
                        begin
                         pNodes1l := pNode1.MarkNodes[true];
                         if (pNodes1l <> nil ) then
                         begin
                         //    
                         pNode1l1 := pNodes1l.Add ( ksMarkRefCircle, -1 );
                          if ( pNode1l1 <> nil ) then
                          begin
                          pNode1l1.DoubleMark := TRUE;
                          pNode1l1.RefLength := 50;
                          //   
                          pText1l := pNode1l1.Text;
                          strText1l := ' ';
                          pText1l.Str := strText1l;
                            //   
                            if ( pNode2 <> nil) then
                            begin
                            pNodes2r := pNode2.MarkNodes[false];
                              if ( pNodes2r <> nil ) then
                              begin
                              //    
                              pNode2r1 := pNodes2r.Add ( ksMarkText, -1);
                              //   
                              pText2r := pNode2r1.Text;
                              strText2r := ' ';
                              pText2r.Str := strText2r;

                              end
                            end
                          end
                         end
                        end;
                      pAxis.Update();
                      end
                    end
                  end
                end
              end
          end
        end
      end
    end;
      ksMessage ( '  ' );
end;
//-------------------------------------------------------------------------------
// __     
// ---
procedure NewUnitnumber;
var
  ppViews : IViews;
  ppView  : IView;
  ppBuildingContainer : IBuildingContainer;
  pUNumbers : IUnitNumbers;
  pUNumber : IUnitNumber;
  pTextUp : IText;
  pTextDown : IText;
  strTxtUp : WideString;
  strTxtDown : WideString;
begin
  //    
  ppViews := GetViews();
    if( ppViews <> nil ) then
    begin
      //    
      // ActiveView-     
      ppView := ppViews.ActiveView;
      //    As  
      //     
      ppBuildingContainer := ppView As IBuildingContainer;

        if (  ppBuildingContainer <> nil ) then
        begin
          //     
          pUNumbers := ppBuildingContainer.UnitNumbers;
          if( pUNumbers <> nil ) then
          begin
            //    
            pUNumber := pUNumbers.Add() As IUnitNumber;
            if ( pUNumber <> nil )then
              begin
                //   
                pUNumber.X := 150;
                pUNumber.Y := 100;

                //   
                pTextUp := pUNumber.TextUp;
                strTxtUp := '  ';
                pTextUp.Str :=  strTxtUp;

                //   
                pTextDown := pUNumber.TextDown;
                strTxtDown := '  ';
                pTextDown.Str :=  strTxtdown;
                // 
                pUNumber.Update();
              end
          end
        end
    end;
    ksMessage('   ');
end;


//-------------------------------------------------------------------------------
// __     
// ---
procedure NewUnitMarking;
var
  ppViews : IViews;
  ppView  : IView;
  ppBuildingContainer : IBuildingContainer;
  pUMarkings : IUnitMarkings;
  pUMarking : IUnitMarking;
  pTextUp : IText;
  pTextDown : IText;
  strTxtUp : WideString;
  strTxtDown : WideString;
begin
  //    
  ppViews := GetViews();
    if( ppViews <> nil ) then
    begin
      //    
      // ActiveView-     
      ppView := ppViews.ActiveView;
      //    As  
      //     
      ppBuildingContainer := ppView As IBuildingContainer;

        if (  ppBuildingContainer <> nil ) then
        begin
          //     
          pUMarkings := ppBuildingContainer.UnitMarkings;
          if( pUMarkings <> nil ) then
          begin
            //    
            pUMarking := pUMarkings.Add() As IUnitMarking;
            if ( pUMarking <> nil )then
              begin
                //   
                pUMarking.Xc := 150;
                pUMarking.Yc := 300;
                pUMarking.Form := ksUFormCRectangle;
                pUMarking.Height := 25;
                pUMarking.Width := 30;
                pUMarking.ShelfX := 180;
                pUMarking.ShelfY := 340;
                pUMarking.ShelfDirection := ksLSRight;
                //   
                pTextUp := pUMarking.TextUp;
                strTxtUp := '   ';
                pTextUp.Str :=  strTxtUp;

                //   
                pTextDown := pUMarking.TextDown;
                strTxtDown := '   ';
                pTextDown.Str :=  strTxtdown;
                // 
                pUMarking.Update();
              end
          end
        end
    end;
    ksMessage('   ');
end;


//-------------------------------------------------------------------------------
// __       
// ---
procedure NewCutUnitMarking;
var
  ppViews : IViews;
  ppView  : IView;
  ppBuildingContainer : IBuildingContainer;
  pCUMarkings : ICutUnitMarkings;
  pCUMarking : ICutUnitMarking;
  pTextUp : IText;
  pTextDown : IText;
  strTxtUp : WideString;
  strTxtDown : WideString;
begin
  //    
  ppViews := GetViews();
    if( ppViews <> nil ) then
    begin
      //    
      // ActiveView-     
      ppView := ppViews.ActiveView;
       //     
      ppBuildingContainer := ppView As IBuildingContainer;
        if (  ppBuildingContainer <> nil ) then
        begin
          //     
          pCUMarkings := ppBuildingContainer.CutUnitMarkings;
          if( pCUMarkings <> nil ) then
          begin
            //    
            pCUMarking := pCUMarkings.Add() As ICutUnitMarking;
            if ( pCUMarking <> nil )then
              begin
                //   
                pCUMarking.Angle := 45;
                pCUMarking.ShelfDirection := ksLSRight;
                pCUMarking.ShelfX := 160;
                pCUMarking.ShelfY := 160;

                pCUMarking.AddStroke( 100, 65, 25);
                pCUMarking.AddStroke( 120, 95, 25);
                pCUMarking.AddStroke( 145, 135, 15);


                //   
                pTextUp := pCUMarking.TextUp;
                strTxtUp := '   ';
                pTextUp.Str :=  strTxtUp;

                //   
                pTextDown := pCUMarking.TextDown;
                strTxtDown := '   ';
                pTextDown.Str :=  strTxtdown;
                // 
                pCUMarking.Update();
              end
          end
        end
    end;
    ksMessage('     ');
end;


//-------------------------------------------------------------------------------
//    
// ---
procedure NewDraft;
var
  pDocs : IDocuments;
  pDoc  : IKompasDocument;
  lSheets : ILayoutSheets;

  num : Integer;
  buf : String;
begin
  if( newKompasAPI <> nil ) then
  begin
  pDocs := newKompasAPI.Documents;
    if ( pDocs <> nil) then
    begin
    pDoc := pDocs.Add( ksDocumentSpecification, TRUE );
    lSheets := pDoc.LayoutSheets;
      if (lSheets <> nil) then
      begin
      num := lSheets.count;
      buf := Format ('count=%d', [num]);
      ksMessage(PChar(buf));
      end
      else
      ksMessage ('');
    end
  end
end;


//-------------------------------------------------------------------------------
//     API
// ---
procedure GetNewKompasAPI;
var
  disp : IDispatch;
begin
	if newKompasAPI = nil then
  begin
    disp := IDispatch(CreateKompasApplication);
    newKompasAPI := disp As IApplication;
  end;
end;

//----------------------------------------------------------------------------------------------
//    
//---
function LoadStr( ID : Integer ) : string;
var
  buf : array [0..255] of char;
begin
  ksConvertLangStrEx( hInstance, ID, buf, 255 );
  Result := buf;
end;

//------------------------------------------------------------------------------
//  
// ---
function LibMessage( str : string; flags : integer ) : Integer;
var
 enabse : Integer;
begin
  enabse := IsEnableTaskAccess();  //  
  if enabse <> 0 then             //     
    EnableTaskAccess(0);          //  

    //                                      
  Result := Application.MessageBox( PChar(str), PChar(LoadStr(LIBRARYID)), flags );

  if enabse <> 0 then                         //      
    EnableTaskAccess(1);              //    


end;

//------------------------------------------------------------------------------
//  
// ---
function LibMessageByID( strId, flags : Integer ) : Integer;
begin
  Result := LibMessage( LoadStr(strId), flags );
end;

end.


