////////////////////////////////////////////////////////////////////////////////
//
//     
//
////////////////////////////////////////////////////////////////////////////////
unit Step3_API7_2D_1;

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

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

  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  LeaderWork;
  procedure  MarkLeaderWork;
  procedure  ChangeLeaderWork;
  procedure  BrandLeaderWork;
  procedure  RoughWork;
  procedure  BaseWork;
  procedure  CutLineWork;
  procedure  ViewPointerWork;
  procedure  ToleranceWork;
  procedure  ObjectsNavigation;

implementation

uses
  LtDefine,
  ksApi7,
  ksConstTLB,
  forms;

const
  IDS_NODOC       = 200;
  IDS_EDIT        = 201;
  IDS_MARK        = 202;
  IDS_MARK2       = 203;
  IDS_MARK3       = 204;
  IDS_COMMAND1    = 205;
  IDS_COMMAND2    = 206;
  IDS_NOOBJ       = 207;
  IDS_NOPOINT     = 208;
  IDS_LEADER      = 209;
  IDS_POINT       = 210;
  IDS_ARROUND     = 211;
  IDS_ON          = 212;
  IDS_OFF         = 213;
  IDS_BRANCHBEGIN = 214;
  IDS_BEGINSHELF  = 215;
  IDS_ENDSHELF    = 216;
  IDS_PARALLEL    = 217;
  IDS_MARKLEADER  = 218;
  IDS_BRANCHCOUNT = 219;
  IDS_BRANDLEADER = 220;
  IDS_CHANLEADER  = 221;
  IDS_ROUGH       = 222;
  IDS_BASE        = 223;
  IDS_CUTLINE     = 224;
  IDS_VIEWPOINTER = 225;
  IDS_TOLERANCE   = 226;

var
  newKompasAPI : IApplication;                                //  Application 7
  doc : IKompasDocument2D;

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


//-------------------------------------------------------------------------------
//   
// ---
function GetCurrentDocument : IKompasDocument2D;

var
  kDoc : IKompasDocument;
begin
  Result := nil;

  if ( newKompasAPI <> nil ) then
     //   
     kDoc := newKompasAPI.ActiveDocument;
  Result := kDoc As IKompasDocument2D;

end;


//------------------------------------------------------------------------------
// LibraryEntry
//---
procedure LIBRARYENTRY( command: WORD ); pascal;

var
  mes : WideString;
begin
  Application.Handle := GetHWindow;
  GetNewKompasAPI;
  doc := GetCurrentDocument;

  if ( doc <> nil ) then
  begin
    case command of
      1   : LeaderWork;
      2   : MarkLeaderWork;
      3   : ChangeLeaderWork;
      4   : BrandLeaderWork;
      5   : RoughWork;
      6   : BaseWork;
      7   : CutLineWork;
      8   : ViewPointerWork;
      9   : ToleranceWork;
      10  : ObjectsNavigation;
    end;
    newKompasAPI := nil;
  end
  else
  begin
    mes := LoadStr( IDS_NODOC );
    ksMessageW( PWideChar(mes) );
  end;
end;


//-------------------------------------------------------------------------------
//     2D
// ---
function GetSymbols2DContainer : ISymbols2DContainer;

var
  viewsMng  : IViewsAndLayersManager;
  views     : IViews;
  view      : IView;
begin
  Result := nil;

  if ( doc <> nil ) then
  begin
    //        
    viewsMng := doc.ViewsAndLayersManager;

    if ( viewsMng <> nil ) then
    begin
      //   
      views := viewsMng.Views;

      if ( views <> nil ) then
      begin
        //   
        view := views.ActiveView;
        //  
        Result := view As ISymbols2DContainer;
      end;
    end
  end
end;


//-------------------------------------------------------------------------------
//    
// ---
procedure CreateLeader( leader : ILeader );

var
  branchs     : IBranchs;
  baseLeader  : IBaseLeader;
  txtOnSh, txtUnderSh, txtOnBr, txtUnderBr, txtAfterSh  : IText;
begin
	if ( leader <> nil ) then
	begin
		//   - 
		leader.ShelfDirection := ksLSRight;

		//   
		branchs := leader As IBranchs;

		if ( branchs <> nil ) then
		begin
			//      
			branchs.X0 := 100;
			branchs.Y0 := 150;
			//   
			branchs.AddBranchByPoint( -1, 60, 120 );
			branchs.AddBranchByPoint( -1, 65, 105 );
		end;

		//     
		txtOnSh := leader.TextOnShelf;

		if ( txtOnSh <> nil ) then
			//  
			txtOnSh.Str := '1';

		//     
		txtUnderSh := leader.TextUnderShelf;

		if ( txtUnderSh <> nil ) then
			//  
			txtUnderSh.Str := '2';

		//     
		txtOnBr := leader.TextOnBranch;
		
		if ( txtOnBr <> nil ) then
			//  
			txtOnBr.Str := '3';

		//     
		txtUnderBr := leader.TextUnderBranch;

		if ( txtUnderBr <> nil ) then
			//  
			txtUnderBr.Str := '4';

		//     
		txtAfterSh := leader.TextAfterShelf;
		
		if ( txtAfterSh <> nil ) then
			//  
			txtAfterSh.Str := '5';

		baseLeader := leader As IBaseLeader;

		if ( baseLeader <> nil ) then
			//  
			baseLeader.Update();
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure AddTextItem( line : ITextLine; str : WideString; tType : ksTextItemEnum  );

var
  item  : ITextItem;
begin
	if ( line <> nil ) then
	begin
		//    
		item := line.Add();
		
		if ( item <> nil ) then
		begin
			//   
			item.Str := str;
			//  
			item.ItemType := tType;
      //  
			item.Update;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure EditLeader( leader : ILeader  );

var
  txt         : IText;
  branchs     : IBranchs;
  line        : ITextLine;
  baseLeader  : IBaseLeader;
begin
	if ( leader <> nil ) then
	begin
		//   -  
		leader.SignType := ksLGlueSign;
		//     
		leader.Arround := true;

    //   
		branchs := leader As IBranchs;
		
		if ( branchs <> nil ) then
			//   
			branchs.AddBranchByPoint( -1, 140, 120 );

		//    -   
		leader.BranchBegin[2] := false;

		//    
		txt := leader.TextOnShelf;

		if ( txt <> nil ) then
		begin
      txt.Str := '';
			line := txt.Add();

			if ( line <> nil ) then
			begin
        //  
        AddTextItem( line, '1', ksTItString );
				//   
				AddTextItem( line, '1', ksTItNumerator );
				//   
				AddTextItem( line, '1', ksTItDenominator );
				//  
				AddTextItem( line, '1', ksTItFractionEnd );
			end;
		end;

		baseLeader := leader As IBaseLeader;

		if ( baseLeader <> nil ) then
			//  
			baseLeader.Update();
	end;
end;


//-------------------------------------------------------------------------------
//      
// ---
procedure LeaderWork;

var
  symbCont      : ISymbols2DContainer;
  leadersCol    : ILeaders;
  leader, lead  : ILeader;
  bLeader       : IBaseLeader;
  refr          : reference;
  mes           : WideString;
begin
	//    
	symbCont := GetSymbols2DContainer;

	if ( symbCont <> nil ) then
	begin
		//    
		leadersCol := symbCont.Leaders;

		if ( leadersCol <> nil ) then
		begin
			//    
			leader := leadersCol.Add( ksDrLeader ) As ILeader;

			if ( leader <> nil ) then
			begin
				//   
				CreateLeader( leader );

				//   
				bLeader := leader As IBaseLeader;
				refr := 0;

				if ( bLeader <> nil ) then
					refr := bLeader.Reference;

        //   
				LightObj( refr, 1{ } );
        mes := LoadStr( IDS_EDIT );

				if ( YesNoW(PWideChar(mes)) = 1 ) then
				begin
					//      
					lead := leadersCol.Leader[ refr ] As ILeader;
					//   
					EditLeader( lead );
				end;
        LightObj( refr, 0{ } );
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure CreateMarkLeader( markLeader : IMarkLeader );

var
  branchs     : IBranchs;
  mes         : WideString;
  baseLeader  : IBaseLeader;
  des, textOnBranch, textUnderBranch : IText;
begin
	if ( markLeader <> nil ) then
	begin
		//   
		branchs := markLeader As IBranchs;

		if ( branchs <> nil ) then
		begin
			//   
			branchs.X0 := 100;
			branchs.Y0 := 190;
			//   
			branchs.AddBranchByPoint( -1, 60, 120 );
		end;

		//    
		des := markLeader.Designation;
    mes := LoadStr( IDS_MARK );

		if ( des <> nil ) then
			//  
			des.Str := PWideChar( mes );

		//     
		textOnBranch := markLeader.TextOnBranch;

		if ( textOnBranch <> nil ) then
			//  
			textOnBranch.Str := '2';

		//     
		textUnderBranch := markLeader.TextUnderBranch;
		
		if ( textUnderBranch <> nil ) then
			//  
			textUnderBranch.Str := '3';

		//     
		baseLeader := markLeader As IBaseLeader;

		if ( baseLeader <> nil ) then
			//  
			baseLeader.Update;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure EditMarkLeader( markLeader : IMarkLeader );

var
  branchs     : IBranchs;
  baseLeader  : IBaseLeader;
begin
	if ( markLeader <> nil ) then
	begin
		//   
		branchs := markLeader As IBranchs;

		if ( branchs <> nil ) then
			//   
			branchs.AddBranchByPoint( -1, 70, 110 );

		//     
		baseLeader := markLeader As IBaseLeader;

		if ( baseLeader <> nil ) then
		begin
			//  
			baseLeader.ArrowType := ksLeaderArrow;
			//  
			baseLeader.Update();
		end;
	end;
end;


//-------------------------------------------------------------------------------
//     
// ---
procedure MarkLeaderWork;

var
  symbCont          : ISymbols2DContainer;
  leadersCol        : ILeaders;
  mLeader, markLead : IMarkLeader;
  bLeader           : IBaseLeader;
  refr              : reference;
  mes               : WideString;
begin
	//    
	symbCont := GetSymbols2DContainer;
	
	if ( symbCont <> nil ) then
	begin
		//    
		leadersCol := symbCont.Leaders;

		if ( leadersCol <> nil ) then
		begin
			//   
			mLeader := leadersCol.Add( ksDrMarkerLeader ) As IMarkLeader;
			
			if ( mLeader <> nil ) then
			begin
				//   
				CreateMarkLeader( mLeader );
				
				//   
				bLeader := mLeader As IBaseLeader;
				refr := 0;

				if ( bLeader <> nil ) then
					refr := bLeader.Reference;

        //   
				LightObj( refr, 1{ } );
        mes := LoadStr( IDS_EDIT );

				if ( YesNoW(PWideChar(mes)) = 1 ) then
				begin
					//      
					markLead := leadersCol.Leader[refr] As IMarkLeader;
					//   
					EditMarkLeader( markLead );
				end;

        LightObj( refr, 0{ } )
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure CreateChangeLeader( changeLeader : IChangeLeader );

var
  branchs     : IBranchs;
  des         : IText;
  baseLeader  : IBaseLeader;
begin
	if ( changeLeader <> nil ) then
	begin
		//   - 
		changeLeader.SignType := ksCLSSquare;

		//   
		branchs := changeLeader As IBranchs;

		if ( branchs <> nil ) then
		begin
			//  
			branchs.X0 := 70;
			branchs.Y0 := 150;
			//  
			branchs.AddBranchByPoint( -1, 40, 130 );
		end;

		//    
		des := changeLeader.Designation;
		
		if ( des <> nil ) then
			//  
			des.Str := '1';

		//     
		baseLeader := changeLeader As IBaseLeader;

		if ( baseLeader <> nil ) then
			//  
			baseLeader.Update;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure EditChangeLeader( changeLeader : IChangeLeader );

var
  baseLeader  : IBaseLeader;
begin
	if ( changeLeader <> nil ) then
	begin
		//   - 
		changeLeader.SignType := ksCLSCircle;
		//    
		changeLeader.FullLeaderLength := false;
		//  
		changeLeader.LeaderLength := 5;

		//     
		baseLeader := changeLeader As IBaseLeader;
		
		if ( baseLeader <> nil ) then
			//  
			baseLeader.Update;
	end;
end;


//-------------------------------------------------------------------------------
//     
// ---
procedure ChangeLeaderWork;

var
  symbCont              : ISymbols2DContainer;
  leadersCol            : ILeaders;
  chLeader, changeLead  : IChangeLeader;
  bLeader               : IBaseLeader;
  refr                  : reference;
  mes                   : WideString;
begin
	//    
	symbCont := GetSymbols2DContainer;
	
	if ( symbCont <> nil ) then
	begin
		//    
		leadersCol := symbCont.Leaders;;
		
		if ( leadersCol <> nil ) then
		begin
			//   
			chLeader := leadersCol.Add( ksDrChangeLeader ) As IChangeLeader;
			
			if ( chLeader <> nil ) then
			begin
				//   
				CreateChangeLeader( chLeader );
				
				//   
				bLeader := chLeader As IBaseLeader;
				refr := 0;

				if ( bLeader <> nil ) then
					refr := bLeader.Reference;

        //   
				LightObj( refr, 1{ } );
				mes := LoadStr(IDS_EDIT);

        if ( YesNoW(PWideChar(mes)) = 1 ) then
				begin
					//      
					changeLead := leadersCol.Leader[refr] As IChangeLeader;
					//   
					EditChangeLeader( changeLead );
				end;

        LightObj( refr, 0{ } );
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure CreateBrandLeader( brandLeader : IBrandLeader );

var
  des         : IText;
  mes         : WideString;
  branchs     : IBranchs;
  baseLeader  : IBaseLeader;
begin
	if ( brandLeader <> nil ) then
	begin
		// 
		brandLeader.Direction := FALSE;

		//    
		des := brandLeader.Designation;
    mes := LoadStr( IDS_MARK2 );

		if ( des <> nil ) then
			//  
			des.Str := PWideChar( mes );

		//   
		branchs := brandLeader As IBranchs;

		if ( branchs <> nil ) then
		begin
			//  
			branchs.X0 := 100;
			branchs.Y0 := 150;
			//  
			branchs.AddBranchByPoint( -1, 60, 110 );
		end;

		//     
		baseLeader := brandLeader As IBaseLeader;

		if ( baseLeader <> nil ) then
			//  
			baseLeader.Update;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure EditBrandLeader( brandLeader : IBrandLeader );

var
  mes         : WideString;
  baseLeader  : IBaseLeader;
  textOn, textUnder, des : IText;
begin
	if ( brandLeader <> nil ) then
	begin
		// 
		brandLeader.Direction := true;

		//     
		textOn := brandLeader.TextOnBranch;

		if ( textOn <> nil ) then
			//  
			textOn.Str := '2';
		
		//     
		textUnder := brandLeader.TextUnderBranch;

		if ( textUnder <> nil ) then
			//  
			textUnder.Str := '3';

		//    
		des := brandLeader.Designation;
    mes := LoadStr( IDS_MARK3 );

		if ( des <> nil ) then
			//  
			des.Str := PWideChar( mes );

		//     
		baseLeader := brandLeader As IBaseLeader;

		if ( baseLeader <> nil ) then
			//  
			baseLeader.Update;
	end;
end;


//-------------------------------------------------------------------------------
//     
// ---
procedure BrandLeaderWork;

var
  symbCont              : ISymbols2DContainer;
  leadersCol            : ILeaders;
  brLeader, brandLead   : IBrandLeader;
  bLeader               : IBaseLeader;
  refObj                : reference;
  mes                   : WideString;
begin
	//    
	symbCont := GetSymbols2DContainer;
	
	if ( symbCont <> nil ) then
	begin
		//    
		leadersCol := symbCont.Leaders;
		
		if ( leadersCol <> nil ) then
		begin
			//   
			brLeader := leadersCol.Add( ksDrBrandLeader ) As IBrandLeader;
			
			if ( brLeader <> nil ) then
			begin
				//   
				CreateBrandLeader( brLeader );
				
				//   
				bLeader := brLeader As IBaseLeader;
				refObj := 0;

				if ( bLeader <> nil ) then
					refObj := bLeader.Reference;

        //   
				LightObj( refObj, 1{ } );
				mes := LoadStr(IDS_EDIT);

        if ( YesNoW(PWideChar(mes)) = 1 ) then
				begin
					//      
					brandLead := leadersCol.Leader[refObj] As IBrandLeader;
					//   
					EditBrandLeader( brandLead );
				end;

        LightObj( refObj, 0{ } );
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
function CreateRough( rough : IRough ) : boolean;

var
  info      : RequestInfoW;
  sOpt      : SnapOptions;
  x, y      : double;
  pObj      : reference;
  baseObj   : IDrawingObject;
  roughPar  : IRoughParams;
  mes       : WideString;
  txt1, txt2, txt3 : IText;
begin
  Result := false;

	if ( rough <> nil ) then
	begin
		//      
    FillChar( info, SizeOf(info), 0 );
    //   
    mes := LoadStr( IDS_COMMAND1 );
    info.commands := PWideChar( mes );
    info.cursor := PWideChar( OCR_CATCH );

    //  
		ksGetSysOptions( SNAP_OPTIONS, @sOpt, SizeOf(sOpt) );
		sOpt.commonOpt := 0;
		ksSetSysOptions( SNAP_OPTIONS, @sOpt, SizeOf(sOpt) );

		//   
		if ( ksCursorExW(@info, x, y, nil, nil) <> 0 ) then
		begin
			//     
			pObj := FindObj( x, y, ksGetCursorLimit() );
      //  
			LightObj( pObj, 1{ } );
			//      
      baseObj := IUnknown(ksTransferReference( pObj, ksGetCurrentDocument(0{ }))) As IDrawingObject;
			mes := LoadStr( IDS_COMMAND2 );
      info.commands := PWideChar( mes );
      info.cursor := PWideChar( OCR_SELECT );

      //   , 
		  if ( sOpt.commonOpt = 0 ) then
	  	begin
			  sOpt.commonOpt := 1;
			  ksSetSysOptions( SNAP_OPTIONS, @sOpt, SizeOf(sOpt) );
		  end;

			//   
			if ( ksCursorExW(@info, x, y, nil, nil) <> 0 ) then
			begin
				if ( baseObj <> nil ) then
					//  
					rough.BaseObject := baseObj;
				//  
				rough.X0 := x;
				rough.Y0 := y;

				roughPar := rough As IRoughParams;

				if ( roughPar <> nil ) then
				begin
					roughPar.ShelfDirection := ksLSRight;
					rough.Update;
					//    
					roughPar.ProcessingByContour := true;
					//   
					roughPar.LeaderLength := 20;
					//    
					roughPar.LeaderAngle := 45;

					//     
					txt1 := roughPar.RoughParamText;

					if ( txt1 <> nil ) then
						txt1.Str := '1';
					
					//      
					txt2 := roughPar.ProcessText;
					
					if ( txt2 <> nil ) then
						txt2.Str := '2';

					//     
					txt3 := roughPar.TrendText;
					
					if ( txt3 <> nil ) then
						txt3.Str := '3';
				end;

				//  
				rough.Update;
        Result := true;
			end

			else
      begin
        mes := LoadStr( IDS_NOPOINT );
        ksMessageW( PWideChar(mes) );
      end;

      LightObj( pObj, 0{ } );
     end

		else
    begin
      mes := LoadStr( IDS_NOOBJ );
      ksMessageW( PWideChar(mes) );
    end;

    //   , 
    if ( sOpt.commonOpt = 0 ) then
    begin
      sOpt.commonOpt := 1;
      ksSetSysOptions( SNAP_OPTIONS, @sOpt, SizeOf(sOpt) );
    end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure EditRough( rough : IRough );

var
  roughPar : IRoughParams;
begin
	if ( rough <> nil ) then
	begin
		//    
		roughPar := rough As IRoughParams;

		if ( roughPar <> nil ) then
		begin
			//  
			roughPar.ArrowType := ksWithoutArrow;
			//    
			roughPar.ProcessingByContour := false;
		end;
		//  
		rough.Update();
	end;
end;


//-------------------------------------------------------------------------------
//     
// ---
procedure RoughWork;

var
  symbCont              : ISymbols2DContainer;
  roughsCol             : IRoughs;
  newRough, rough       : IRough;
  objRef                : reference;
  mes                   : WideString;
begin
	//    
	symbCont := GetSymbols2DContainer;
	
	if ( symbCont <> nil ) then
	begin
		//    
		roughsCol := symbCont.Roughs;
		
		if ( roughsCol <> nil ) then
		begin
			//   
			newRough := roughsCol.Add();

			if ( newRough <> nil ) then
			begin
				//   
				if ( CreateRough(newRough) )then
				begin
				  //   
				  objRef := newRough.Reference;

          //   
					LightObj( objRef, 1{ } );
				  mes := LoadStr(IDS_EDIT);

          if ( YesNoW(PWideChar(mes)) = 1 ) then
				  begin
					  //      
					  rough := roughsCol.Rough[objRef];
					  //   
					  EditRough( rough );
				  end;

          LightObj( objRef, 0{ } );
        end;
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
function CreateBase( base : IBase ) : boolean;

var
  info      : RequestInfoW;
  sOpt      : SnapOptions;
  x, y      : double;
  pObj      : reference;
  baseObj   : IDrawingObject;
  mes       : WideString;
begin
  Result := false;
	if ( base <> nil ) then
	begin
		//      
    FillChar( info, SizeOf(info), 0 );
    //   
    mes := LoadStr( IDS_COMMAND1 );
    info.commands := PWideChar( mes );
    info.cursor := PWideChar( OCR_CATCH );

    //  
		ksGetSysOptions( SNAP_OPTIONS, @sOpt, SizeOf(sOpt) );
		sOpt.commonOpt := 0;
		ksSetSysOptions( SNAP_OPTIONS, @sOpt, SizeOf(sOpt) );
		
		//   
		if ( ksCursorExW(@info, x, y, nil, nil) <> 0 ) then
		begin
			//     
			pObj := FindObj( x, y, ksGetCursorLimit() );
      //  
			LightObj( pObj, 1{ } );
			//      
      baseObj := IUnknown(ksTransferReference( pObj, ksGetCurrentDocument(0{ }))) As IDrawingObject;
			//   
      mes := LoadStr( IDS_COMMAND2 );
      info.commands := PWideChar( mes );
      info.cursor := PWideChar( OCR_SELECT );

      //   , 
		  if ( sOpt.commonOpt = 0 ) then
	  	begin
			  sOpt.commonOpt := 1;
			  ksSetSysOptions( SNAP_OPTIONS, @sOpt, SizeOf(sOpt) );
		  end;

			//   
			if ( ksCursorExW(@info, x, y, nil, nil) <> 0 ) then
			begin

				if ( baseObj <> nil ) then
					//  
					base.BaseObject := baseObj;

				//   
				base.X0 := x;
				base.Y0 := y;
				//   -   
				base.DrawType := true;
				//  
				base.Update;
        Result := true;
			end

			else
      begin
        mes := LoadStr( IDS_NOPOINT );
        ksMessageW( PWideChar(mes) );
      end;

      LightObj( pObj, 0{ } );
		end

		else
    begin
      mes := LoadStr( IDS_NOOBJ );
      ksMessageW( PWideChar(mes) );
    end;

    //   , 
    if ( sOpt.commonOpt = 0 ) then
    begin
      sOpt.commonOpt := 1;
			ksSetSysOptions( SNAP_OPTIONS, @sOpt, SizeOf(sOpt) );
    end;
  end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure EditBase( base : IBase );

var
  txt : IText;
begin
	if ( base <> nil ) then
	begin
		//   - 
		base.DrawType := false;
		//  
		base.AutoSorted := false;
		//   
		base.BranchX := base.X0 + 10;
		base.BranchY := base.Y0 + 10;

		//     
		txt := base.Text;

		if ( txt <> nil ) then
			//  
			txt.Str := 'B';

		//  
		base.Update;
	end;
end;


//-------------------------------------------------------------------------------
//     
// ---
procedure BaseWork;

var
  symbCont      : ISymbols2DContainer;
  basesCol      : IBases;
  newBase, base : IBase;
  objRef        : reference;
  mes           : WideString;
begin
	//    
	symbCont := GetSymbols2DContainer;

	if ( symbCont <> nil ) then
	begin
		//    
		basesCol := symbCont.Bases;
		
		if ( basesCol <> nil ) then
		begin
			//   
			newBase := basesCol.Add;
			
			if ( newBase <> nil ) then
			begin
				//   
				if ( CreateBase(newBase) ) then
				begin
				  //   
				  objRef := newBase.Reference;

          //   
					LightObj( objRef, 1{ } );
				  mes := LoadStr(IDS_EDIT);

          if ( YesNoW(PWideChar(mes)) = 1 ) then
				  begin
					  //      
					  base:= basesCol.Base[objRef];
					  //   
					  EditBase( base );
				  end;

          LightObj( objRef, 0{ } );
        end;
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//      
// ---
procedure SetTextSmallFont( txt : IText; str : WideString; size : double  );

var
  line  : ITextLine;
  item  : ITextItem;
  font  : ITextFont;
begin
	if ( txt <> nil ) then
	begin
		//    
		line := txt.Add;

		if ( line <> nil ) then
		begin
			//    
			item := line.Add;

			if ( item <> nil ) then
			begin
				//   
				font := item As ITextFont;

				if ( font <> nil ) then
					//   
					font.Height := size;

				//   
				item.Str := str;
				//  
				item.Update();
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   SAFEARRAY  double
// ---
function NewSafeArray( dv : array of double; count : integer ) : OleVariant;

var
  res : OleVariant;
  i   : integer;
begin
  res := VarArrayCreate( [0, count-1], varDouble );

  for i := 0 to count - 1 do
    res[i] := dv[i];

  Result := res;
end;


//-------------------------------------------------------------------------------
//   /
// ---
procedure CreateCutLine( cutLine : ICutLine );

var
  adText : IText;
  points : array [0..3] of double;
begin
	if ( cutLine <> nil ) then
	begin
		//   
		cutLine.X1 := 80;
		cutLine.Y1 := 165;
		//   
		cutLine.X2 := 120;
		cutLine.Y2 := 200;
		//   - 
		cutLine.ArrowPos := true;
		//    -   
		cutLine.AdditionalTextPos := true;

		//     
		points[0] := 80;
		points[1] := 165;
		points[2] := 120;
		points[3] := 200;

		//     
    cutLine.Points := NewSafeArray(points, 4 );

		//    
		adText := cutLine.AdditionalText;
		
		if ( adText <> nil ) then
			SetTextSmallFont( adText, '(1)', 7 );

		//  
		cutLine.Update();
	end;
end;


//-------------------------------------------------------------------------------
//   /
// ---
procedure EditCutLine( cutLine : ICutLine );

var
  points : array [0..5] of double;
begin
	if ( cutLine <> nil ) then
	begin
		//   -    
		cutLine.ArrowPos := false;
		//    -   
		cutLine.AdditionalTextPos := false;
		//  
		cutLine.AutoSorted := false;

		//     
		points[0] := 80;
		points[1] := 165;
		points[2] := 115;
		points[3] := 165;
		points[4] := 120;
		points[5] := 200;

    //     
		cutLine.Points := NewSafeArray(points, 6 );

		//  
		cutLine.Update();
	end;
end;


//-------------------------------------------------------------------------------
//     /
// ---
procedure CutLineWork;

var
  symbCont        : ISymbols2DContainer;
  cutCol          : ICutLines;
  newCut, cutLine : ICutLine;
  objRef          : reference;
  mes             : WideString;
begin
	//    
	symbCont := GetSymbols2DContainer;
	
	if ( symbCont <> nil ) then
	begin
		//    /
		cutCol := symbCont.CutLines;
		
		if ( cutCol <> nil ) then
		begin
			//   /
			newCut := cutCol.Add;
			
			if ( newCut <> nil ) then
			begin
				//   /
				CreateCutLine( newCut );

				//   
				objRef := newCut.Reference;

        //   
        LightObj( objRef, 1{ } );
				mes := LoadStr(IDS_EDIT);

        if ( YesNoW(PWideChar(mes)) = 1 ) then
				begin
					//      
					cutLine := cutCol.CutLine[objRef];
					//   /
					EditCutLine( cutLine );
				end;

        LightObj( objRef, 0{ } );
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//    
// ---
procedure CreateViewPointer( viewPointer : IViewPointer );
begin
	if ( viewPointer <> nil ) then
	begin
		//   
		viewPointer.X1 := 100;
		viewPointer.Y1 := 150;
		//   
		viewPointer.X2 := 120;
		viewPointer.Y2 := 160;
		//  
		viewPointer.Update();
	end;
end;


//-------------------------------------------------------------------------------
//    
// ---
procedure EditViewPointer( viewPointer : IViewPointer );

var
  adText : IText;
begin
	if ( viewPointer <> nil ) then
	begin
		//   
		viewPointer.X2 := 90;
		viewPointer.Y2 := 140;
		//  
		viewPointer.AutoSorted := true;

		//      
		adText := viewPointer.AdditionalText;
		
		if ( adText <> nil ) then
			//  
			SetTextSmallFont( adText, '(1)', 7 );
		
		//  
		viewPointer.Update;
	end;
end;


//-------------------------------------------------------------------------------
//      
// ---
procedure ViewPointerWork;

var
  symbCont                : ISymbols2DContainer;
  viewPointsCol           : IViewPointers;
  newPointer, viewPointer : IViewPointer;
  objRef                  : reference;
  mes                     : WideString;
begin
	//    
	symbCont := GetSymbols2DContainer;
	
	if ( symbCont <> nil ) then
	begin
		//     
		viewPointsCol := symbCont.ViewPointers;

		if ( viewPointsCol <> nil ) then
		begin
			//    
			newPointer := viewPointsCol.Add;
			
			if ( newPointer <> nil ) then
			begin
				//    
				CreateViewPointer( newPointer );

				//   
				objRef := newPointer.Reference;

				//   
        LightObj( objRef, 1{ } );
				mes := LoadStr(IDS_EDIT);

        if ( YesNoW(PWideChar(mes)) = 1 ) then
				begin
					//      
					viewPointer := viewPointsCol.ViewPointer[objRef];
					//    
					EditViewPointer( viewPointer );
				end;

        LightObj( objRef, 0{ } );
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//      
// ---
procedure SetToleranceText( tolPar : IToleranceParam );

var
  tolTable  : ITable;
  txt       : ITextLine;
  cell      : ITableCell;
begin
	if ( tolPar <> nil ) then
	begin
		//       
		tolTable := tolPar.Table;
		
		if ( tolTable <> nil ) then
		begin
			//  3  (1  )
			tolTable.AddColumn( -1, TRUE {} );
			tolTable.AddColumn( -1, TRUE {} );
			tolTable.AddColumn( -1, TRUE {} );
			
			//    1- 
			cell := tolTable.Cell[ 0, 0 ];

			if ( cell <> nil ) then
			begin
				txt:= cell.Text As ITextLine;
				
				if ( txt <> nil ) then
					txt.Str := '@22~';
			end;
			
			//    2- 
			cell := tolTable.Cell[ 0, 1 ];
			
			if ( cell <> nil ) then
			begin
				txt := cell.Text As ITextLine;
				
				if ( txt <> nil ) then
					txt.Str := '@2~';
			end;

			//    3- 
			cell := tolTable.Cell[ 0, 2 ];
			
			if ( cell <> nil ) then
			begin
				txt := cell.Text As ITextLine;

				if ( txt <> nil ) then
					txt.Str := 'B';
			end;

			//    4- 
			cell := tolTable.Cell[ 0, 3 ];

			if ( cell <> nil ) then
			begin
				txt := cell.Text As ITextLine;

				if ( txt <> nil ) then
					txt.Str := '@30~';
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure CreateTolerance( tolerance : ITolerance );

var
  branchs : IBranchs;
  tolPar  : IToleranceParam;
begin
	if ( tolerance <> nil ) then
	begin
		//   
		branchs := tolerance As IBranchs;

		if ( branchs <> nil ) then
		begin
			//   
			branchs.X0 := 100;
			branchs.Y0 := 150;
			//  2 
			branchs.AddBranchByPoint( -1, 100, 120 );
			branchs.AddBranchByPoint( -1, 50, 155 );
		end;

		//     
		tolPar := tolerance As IToleranceParam;

		if ( tolPar <> nil ) then
		begin
			//    
			SetToleranceText( tolPar );
			//      -  
			tolPar.BasePointPos := ksTPBottomCenter;
		end;

		//   1-  - 
		tolerance.Set_ArrowType( 0, FALSE );
		//  1-    -  
		tolerance.Set_BranchPos( 0, ksTPBottomCenter );
		//   2-  - 
		tolerance.Set_ArrowType( 1, TRUE );
		//  2-    -  
		tolerance.Set_BranchPos( 1, ksTPLeftCenter );
		//  
		tolerance.Update();
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure EditTolerance( tolerance : ITolerance );

var
  tolPar    : IToleranceParam;
  tolTable  : ITable;
  txt       : ITextLine;
  branchs   : IBranchs;
  cell      : ITableCell;
begin
	if ( tolerance <> nil ) then
	begin
		//    
		tolPar := tolerance As IToleranceParam;

		if ( tolPar <> nil ) then
		begin
			//       
			tolTable := tolPar.Table;
			
			if ( tolTable <> nil ) then
			begin
				//    2- 
				cell := tolTable.Cell[ 0, 1 ];

				if ( cell <> nil ) then
				begin
					txt := cell.Text As ITextLine;

					if ( txt <> nil ) then
						txt.Str := '@2~15';
				end;
			end;
			//   
			tolPar.Vertical := true;
		end;

		//   
	  branchs := tolerance As IBranchs;

		if ( branchs <> nil ) then
		begin
			//  
			branchs.DeleteBranch( 0 );
			//   
			branchs.AddBranchByPoint( -1, 130, 120 );
		end;

		tolerance.Set_ArrowType( 1, FALSE );
		tolerance.Set_BranchPos( 1, ksTPBottomCenter );
		//  
		tolerance.Update();
	end;
end;


//-------------------------------------------------------------------------------
//     
// ---
procedure ToleranceWork;

var
  symbCont          : ISymbols2DContainer;
  tolerancesCol     : ITolerances;
  newTol, tolerance : ITolerance;
  objRef            : reference;
  mes               : WideString;
begin
	//    
	symbCont := GetSymbols2DContainer;

	if ( symbCont <> nil ) then
	begin
		//    
		tolerancesCol := symbCont.Tolerances;
		
		if ( tolerancesCol <> nil ) then
		begin
			//   
			newTol := tolerancesCol.Add;

			if ( newTol <> nil ) then
			begin
				//   
				CreateTolerance( newTol );
				
				//   
				objRef := newTol.Reference;

				//   
        LightObj( objRef, 1{ } );
				mes := LoadStr(IDS_EDIT);

        if ( YesNoW(PWideChar(mes)) = 1 ) then
				begin
					//      
					tolerance := tolerancesCol.Tolerance[objRef];
					//   
					EditTolerance( tolerance );
				end;

        LightObj( objRef, 0{ } );
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//    
// ---
function GetDrawingContainer : IDrawingContainer;

var
  mng       : IViewsAndLayersManager;
  viewsCol  : IViews;
  view      : IView;
begin
	if ( doc <> nil ) then
	begin
    Result := nil;
		//     
		mng := doc.ViewsAndLayersManager;

		if ( mng <> nil ) then
		begin
			//   
			viewsCol := mng.Views;

			if ( viewsCol <> nil ) then
			begin
				//   
				view := viewsCol.ActiveView;

				if ( view <> nil ) then
          Result := view As IDrawingContainer;
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//       
// ---
procedure GetLeaderPar( leader : ILeader );

var
  x0, y0  : double;
  branchs : IBranchs;
  mes     : WideString;
begin
	if ( leader <> nil ) then
	begin
    x0 := 0;
    y0 := 0;
		//    
		branchs := leader As IBranchs;

		if ( branchs <> nil ) then
		begin
			x0 := branchs.X0;
			y0 := branchs.Y0;
		end;

		//    
		// "  "
    mes := LoadStr( IDS_LEADER );
		// " : X0 = , Y0 = "
    mes := mes + #13#10 + LoadStr( IDS_POINT ) + #13#10 + 'X0 = ' + FormatFloat( '#.##', x0 ) + ', Y0 = ' + FormatFloat( '#.##', y0 );
		// "   : "
    mes := mes + #13#10 + LoadStr( IDS_ARROUND );

		if ( leader.Arround ) then
			// ""
			mes := mes + LoadStr( IDS_ON )
		else
			// ""
			mes := mes + LoadStr( IDS_OFF );

		// " : "
		mes := mes + #13#10 + LoadStr( IDS_BRANCHBEGIN );

		if ( leader.BranchBegin[0] ) then
			// "  "
			mes := mes + LoadStr( IDS_BEGINSHELF )
		else
			// "  "
			mes := mes + LoadStr( IDS_ENDSHELF );

		// "\n  : "
		mes := mes + #13#10 + LoadStr( IDS_PARALLEL );

		if ( leader.ParallelBranch ) then
			// ""
			mes := mes + LoadStr( IDS_ON )
		else
			// ""
			mes := mes + LoadStr( IDS_OFF );
		
		//  
		ksMessageW( PWideChar(mes) );
	end;
end;


//-------------------------------------------------------------------------------
//      
// ---
procedure GetMarkLeaderPar( markLeader : IMarkLeader );

var
  x0, y0  : double;
  count   : integer;
  branchs : IBranchs;
  mes     : WideString;
begin
	if ( markLeader <> nil ) then
	begin
    x0 := 0;
    y0 := 0;
		count := 0;

		branchs := markLeader As IBranchs;
		
		if ( branchs <> nil ) then
		begin
			//    
			x0 := branchs.X0;
			y0 := branchs.Y0;
			//   
			count := branchs.BranchCount;
		end;

		//    
		// " "
		mes := LoadStr( IDS_MARKLEADER );
    // " : X0 = , Y0 = "
    mes := mes + #13#10 + LoadStr( IDS_POINT ) + #13#10 + 'X0 = ' + FormatFloat( '#.##', x0 ) + ',Y0 = ' + FormatFloat( '#.##', y0 );
		// " : "
		mes := mes + #13#10 + LoadStr( IDS_BRANCHCOUNT ) + IntToStr( count );

		//   
		ksMessageW( PWideChar(mes) );
	end;
end;


//-------------------------------------------------------------------------------
//      
// ---
procedure GetBrandLeaderPar( brandLeader : IBrandLeader );

var
  mes : WideString;
begin
	if ( brandLeader <> nil ) then
	begin
		//  
		mes := LoadStr( IDS_BRANDLEADER );
		//  
		ksMessageW( PWideChar(mes) );
	end;
end;


//-------------------------------------------------------------------------------
//      
// ---
procedure GetChangeLeaderPar( changeLeader : IChangeLeader );

var
  mes : WideString;
begin
	if ( changeLeader <> nil ) then
	begin
		//  
		mes := LoadStr( IDS_CHANLEADER );
		//  
		ksMessageW( PWideChar(mes) );
	end;
end;


//-------------------------------------------------------------------------------
//      
// ---
procedure GetRoughPar( rough : IRough );

var
  mes : WideString;
begin
	if ( rough <> nil ) then
	begin
		//  
		mes := LoadStr( IDS_ROUGH );
		//  
		ksMessageW( PWideChar(mes) );
	end;
end;


//-------------------------------------------------------------------------------
//      
// ---
procedure GetBasePar( base : IBase );

var
  mes : WideString;
begin
	if ( base <> nil ) then
	begin
		//  
		mes := LoadStr( IDS_BASE );
		//  
		ksMessageW( PWideChar(mes) );
	end;
end;


//-------------------------------------------------------------------------------
//      /
// ---
procedure GetCutLinePar( cutLine : ICutLine );

var
  mes : WideString;
begin
	if ( cutLine <> nil ) then
	begin
		//  
		mes := LoadStr( IDS_CUTLINE );
		//  
		ksMessageW( PWideChar(mes) );
	end;
end;


//-------------------------------------------------------------------------------
//       
// ---
procedure GetViewPointerPar( viewPointer : IViewPointer );

var
  mes : WideString;
begin
	if ( viewPointer <> nil ) then
	begin
		//  
		mes := LoadStr( IDS_VIEWPOINTER );
		//  
		ksMessageW( PWideChar(mes) );
	end;
end;


//-------------------------------------------------------------------------------
//      
// ---
procedure GetTolerancePar( tolerance : ITolerance );

var
  mes : WideString;
begin
	if ( tolerance <> nil ) then
	begin
		//  
		mes := LoadStr( IDS_TOLERANCE );
		//  
		ksMessageW( PWideChar(mes) );
	end;
end;


//-------------------------------------------------------------------------------
//     
// ---
procedure ObjectsNavigation;

var
  drawCont      : IDrawingContainer;
  arr           : OleVariant;
  pObj          : IDispatch;
  arrCount, j,
  objType       : integer;
  pDrawObj      : IDrawingObject;
  objRef        : reference;
  leader        : ILeader;
  markLeader    : IMarkLeader;
  brandLeader   : IBrandLeader;
  changeLeader  : IChangeLeader;
  rough         : IRough;
  base          : IBase;
  cutLine       : ICutLine;
  viewPointer   : IViewPointer;
  tolerance     : ITolerance;
begin
	//    
	drawCont := GetDrawingContainer;

	if ( drawCont <> nil ) then
	begin
		//  0 -  
		//_variant_t objType = (long)0;
		//   SafeArray 
		arr := drawCont.Objects[ 0 { } ];
		
		//       
		if ( VarType(arr) = (VT_ARRAY Or VT_DISPATCH) ) then
		begin
			//   
			arrCount := ( VarArrayHighBound( arr,  1 ) + 1 );

			for j := 0 to arrCount - 1 do
			begin
				//    
				pObj := arr[j];
				
				if ( pObj <> nil ) then
				begin
					//    
					pDrawObj := pObj As IDrawingObject;
					
					if ( pDrawObj <> nil ) then
					begin
						//   
						objType := pDrawObj.DrawingObjectType;
						//   
						objRef := pDrawObj.Reference;
						//  
						LightObj( objRef, 1{} );

						//          
						case objType of

							//   
							ksDrLeader:
							begin
								leader := pDrawObj As ILeader;
								GetLeaderPar( leader );
							end;

							//  
							ksDrMarkerLeader:
							begin
								markLeader := pDrawObj As IMarkLeader;
								GetMarkLeaderPar( markLeader );
							end;

							//  
							ksDrBrandLeader:
							begin
								brandLeader := pDrawObj As IBrandLeader;
								GetBrandLeaderPar( brandLeader );
							end;

							//  
							ksDrChangeLeader:
							begin
								changeLeader := pDrawObj As IChangeLeader;
								GetChangeLeaderPar( changeLeader );
							end;

							//  
							ksDrRough:
							begin
								rough := pDrawObj As IRough;
								GetRoughPar( rough );
							end;

							//  
							ksDrBase:
							begin
								base := pDrawObj As IBase;
								GetBasePar( base );
							end;

							//  /
							ksDrCut:
							begin
								cutLine := pDrawObj As ICutLine;
								GetCutLinePar( cutLine );
							end;

							//   
							ksDrWPointer:
							begin
								viewPointer := pDrawObj As IViewPointer;
								GetViewPointerPar( viewPointer );
							end;

							//  
							ksDrTolerance:
							begin
								tolerance := pDrawObj As ITolerance;
								GetTolerancePar( tolerance );
							end;
						end;
						//  
						LightObj( objRef, 0{} );
					end;
				end;
			end;
		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.


