////////////////////////////////////////////////////////////////////////////////
//
//     
//
////////////////////////////////////////////////////////////////////////////////
unit Step3_API7_3D_1;

interface
uses
  Sysutils, LDefin2D, ksAuto, Libtool, ks3DCOM_TLB, LDefin3D, LTool3D, 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  Rough3DWork;
  procedure  Base3DWork;
  procedure  Leader3DWork;
  procedure  BrandLeader3DWork;
  procedure  MarkLeader3DWork;
  procedure  Tolerance3DWork;


implementation

uses
  LtDefine,
  ksApi7,
  ksConstTLB,
  forms;

const
  IDS_NODOC       = 200;
  IDS_OBJ         = 201;
  IDS_POINT       = 202;
  IDS_ROUGH       = 203;
  IDS_EDIT        = 204;
  IDS_NOCREATE    = 205;
  IDS_BASE        = 206;
  IDS_BEGINBRANCH = 207;
  IDS_POSOBJ      = 208;
  IDS_BEGINSHELF  = 209;
  IDS_LEADER      = 210;
  IDS_MARK2       = 211;
  IDS_BRAND       = 212;
  IDS_MARK        = 213;
  IDS_ENDPOINT    = 214;
  IDS_TABLE       = 215;
  IDS_TOLERANCE   = 216;

var
  newKompasAPI  : IApplication;                                //  Application 7
  doc           : IKompasDocument3D;
  oType         : integer;

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


//-------------------------------------------------------------------------------
//   
// ---
function GetCurrentDocument : IKompasDocument3D;

var
  kDoc : IKompasDocument;
  doc3D : IKompasDocument3D;
begin
  Result := nil;

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

  Result := doc3D;
end;


//-------------------------------------------------------------------------------
//    3D
// ---
function GetSymbols3DContainer : ISymbols3DContainer;

var
  part7 : IPart7;
begin
  Result := nil;

  if ( doc <> nil ) then
  begin
    //   
    part7 := doc.TopPart;
		
    if ( part7 <> nil ) then
      //    3D
      Result := part7 As ISymbols3DContainer;

  end;
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: Rough3DWork;
       2: Base3DWork;
       3: Leader3DWork;
       4: BrandLeader3DWork;
       5: MarkLeader3DWork;
       6: Tolerance3DWork;
    end;
    newKompasAPI := nil;
  end
  else
  begin
    mes := LoadStr( IDS_NODOC );
    ksMessageW( PWideChar(mes) );
  end;
end;


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


//-----------------------------------------------------------------------------
//  
// ---
function UserFilterProc( e : PIEntity ) : Integer; pascal;
 var
   e1 : IEntity;
begin
  e1 := IEntity(e);
  if ( (e1 <> nil) And ((oType = 0) Or (e1.GetType() = oType)) ) then
    Result := 1
  else
    Result := 0;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure SetRoughPars( rough : IRough3D  );

var
  roughPars : IRoughParams;
  txt1,
  txt2,
  txt3      : IText;
begin
	if ( rough <> nil ) then
	begin
		//    
		roughPars := rough As IRoughParams;

		if ( roughPars <> nil ) then
		begin
			//    
			roughPars.ProcessingByContour := TRUE;
			//   
			roughPars.LeaderLength := 20;
			//    
			roughPars.LeaderAngle := 45;
			
			//     
			txt1 := roughPars.RoughParamText;

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

			//      
			txt2 := roughPars.ProcessText;

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

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


//-------------------------------------------------------------------------------
//   3D
// ---
function CreateRough3D( rough : IRough3D ) : boolean;

var
  doc3D   : IDocument3D;
  baseObj : IEntity;
  mObj    : IModelObject;
  mes     : WideString;
  x, y, z : double;
begin
	Result := false;

	if ( rough <> nil ) then
	begin
		//    3D  API7  API5
    doc3D := IUnknown( ksTransferInterface(PIUnknown(doc), ksAPI3DCom, 0{ }) ) As IDocument3D;

	 	if ( doc3D <> nil ) then
		begin
      mes := LoadStr( IDS_OBJ );
			//        
      baseObj := IEntity( doc3D.UserSelectEntity(nil, nil, PWideChar(mes)) );

			if ( baseObj <> nil ) then
			begin
        mes := LoadStr( IDS_POINT );
				//   
				doc3D.UserGetCursor( PWideChar(mes), x, y, z );

				//     API5  API7
        mObj := IUnknown( ksTransferInterface( PIUnknown(baseObj), ksAPI7Dual, 0) ) As IModelObject;

				if ( mObj <> nil ) then
					//   
					rough.SetBasePosition( x, y, z, mObj );

				//   
				rough.BasePlane := o3d_planeXOZ;

				//   
				SetRoughPars( rough );
				//  
				rough.Update;
				Result := true;
        baseObj._Release;
			end;
      doc3D._Release;
	 	end;
	end;
end;


//-------------------------------------------------------------------------------
//       
// ---
procedure SetPosition( rough : IRough3D; face : IModelObject );

var
  faceEnt : IEntity;
  faceDef : IFaceDefinition;
  edgeCol : IEdgeCollection;
  edgeDef : IEdgeDefinition;
  vertex  : IVertexDefinition;
  mObj    : IModelObject;
begin
	if ( (rough <> nil) And (face <> nil) ) then
	begin
		//      API7  API5
    faceEnt := IUnknown( ksTransferInterface(PIUnknown(face), ksAPI3DCom, o3d_entity) ) As IEntity;

		if ( faceEnt <> nil ) then
		begin
			//    
			faceDef := IUnknown( faceEnt.GetDefinition ) As IFaceDefinition;

			if ( faceDef <> nil ) then
			begin
				//   
				edgeCol := IEdgeCollection( faceDef.EdgeCollection );

				if ( edgeCol <> nil ) then
				begin
					//    
					edgeDef := IEdgeDefinition( edgeCol.First );

					if ( edgeDef <> nil ) then
					begin
						//    
						vertex := IVertexDefinition( edgeDef.GetVertex(1{ *}) );

						if ( vertex <> nil ) then
						begin
							//     API5  API7
              mObj := IUnknown( ksTransferInterface( PIUnknown(vertex), ksAPI7Dual, 0) ) As IModelObject;

							if ( mObj <> nil ) then
								//       
								rough.PositionObject := mObj;

              vertex._Release;
						end;
            edgeDef._Release;
					end;
          edgeCol._Release;
				end;
        faceDef._Release;
			end;
      faceEnt._Release;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//    
// ---
function GetNewName( featObj : IFeature7; strID : integer ) : WideString;

var
  newName,
  name    : WideString;
  p       : integer;
begin
	Result := '';

	if ( featObj <> nil ) then
	begin
		//      
		newName :=	LoadStr( strID );
		name := featObj.Name;
    p := Pos( ':', name );

		if ( p > 0 ) then
			newName := newName + Copy( name, p + 1, Length(name) - p );

		Result := newName;
	end;
end;


//-------------------------------------------------------------------------------
//   3D
// ---
procedure EditRough3D( rough : IRough3D  );

var
  roughPars : IRoughParams;
  featObj   : IFeature7;
  baseObj   : IModelObject;
  colorPars : IColorParam7;
begin
	if ( rough <> nil ) then
	begin
		//    
		roughPars := rough As IRoughParams;

		if ( roughPars <> nil ) then
			//     
			roughPars.ProcessingByContour := FALSE;

		//    
		featObj := rough As IFeature7;

		if ( featObj <> nil ) then
		begin
			//     		
			rough.Name := GetNewName( featObj, IDS_ROUGH );
			//   
			baseObj := rough.BaseObject;
			//       
			SetPosition( rough, baseObj );
		end;

		//     
		colorPars := rough As IColorParam7;

		//    
		if ( colorPars <> nil ) then
			colorPars.Color := RGB( 0, 0, 255 );

		//  
		rough.Update;
	end;
end;


//-------------------------------------------------------------------------------
//     3D
// ---
procedure Rough3DWork;

var
  symbCont  : ISymbols3DContainer;
  roughsCol : IRoughs3D;
  newRough,
  rough     : IRough3D;
  feature   : IFeature7;
  name,
  mes       : WideString;
begin
	//    3D
	symbCont := GetSymbols3DContainer;
	
	if ( symbCont <> nil ) then
	begin
		//    3D
		roughsCol := symbCont.Roughs3D;
		
		if ( roughsCol <> nil ) then
		begin
			//     3D
			newRough := roughsCol.Add;
			
			if ( newRough <> nil ) then
			begin
				//    3D
				if ( CreateRough3D(newRough) ) then
				begin
					name := '';
					//     
					feature := newRough As IFeature7;
					
					//   
					if ( feature <> nil ) then
						name := feature.Name;

					mes := LoadStr( IDS_EDIT );
					if ( YesNoW(PWideChar(mes)) = 1 ) then
					begin
						//      
						rough := roughsCol.Rough3D[name];
						//  
						EditRough3D( rough );
					end;
				end
				else
        begin
					//   "  "
          mes := LoadStr( IDS_NOCREATE );
					ksMessageW( PWideChar(mes) );
        end;
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
function CreateBase3D( base : IBase3D ) : boolean;

var
  doc3D   : IDocument3D;
  baseObj : IEntity;
  mObj    : IModelObject;
  x, y, z : double;
  mes     : WideString;
begin
	Result := false;

	if ( base <> nil ) then
	begin
		//    3D  API7  API5
    doc3D := IUnknown( ksTransferInterface(PIUnknown(doc), ksAPI3DCom, 0{ }) ) As IDocument3D;

		if ( doc3D <> nil ) then
		begin
      mes := LoadStr( IDS_OBJ );
			//        
      baseObj := IEntity( doc3D.UserSelectEntity(nil, nil, PWideChar(mes)) );

			if ( baseObj <> nil ) then
			begin
        mes := LoadStr( IDS_POINT );
				//   
        doc3D.UserGetCursor( PWideChar(mes), x, y, z );

				//     API5  API7
        mObj := IUnknown( ksTransferInterface( PIUnknown(baseObj), ksAPI7Dual, 0) ) As IModelObject;

				if ( mObj <> nil ) then
					//   
					base.SetBranchBeginPoint( x, y, z, mObj );

				//   
				base.BasePlane := o3d_planeXOZ;

				//  
				base.Update;
				Result := true;
        baseObj._Release;
			end;
      doc3D._Release;
		end;
	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 EditBase3D( base : IBase3D );

var
  txt       : IText;
  line      : ITextLine;
  featObj   : IFeature7;
  colorPars : IColorParam7;
  x, y, z   : double;
begin
	if ( base <> nil ) then
	begin
		//  
		base.AutoSorted := FALSE;

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

			if ( line <> nil ) then
			begin
				//  
				AddTextItem( line, 'A', ksTItString );
				//     (   
				//    
				AddTextItem( line, '', ksTItSBase );
				//   
				AddTextItem( line, '1', ksTItSLowerIndex );
				//    
				AddTextItem( line, '', ksTItSEnd );
			end;
		end;

		//  -  
		base.DrawType := FALSE;
		//     
		base.GetBranchEndPoint( x, y, z );
		base.SetBranchEndPoint( x + 10, y + 10, z );

		//    
		featObj := base As IFeature7;

		if ( featObj <> nil ) then
			//     
			base.Name := GetNewName( featObj, IDS_BASE );

		//     
		colorPars := base As IColorParam7;
		
		//    
		if ( colorPars <> nil ) then
			colorPars.Color := RGB( 0, 0, 0 );

		base.Update;
	end;
end;


//-------------------------------------------------------------------------------
//     
// ---
procedure Base3DWork;

var
  symbCont  : ISymbols3DContainer;
  basesCol  : IBases3D;
  newBase,
  base      : IBase3D;
  feature   : IFeature7;
  name,
  mes       : WideString;
begin
	//    3D
	symbCont := GetSymbols3DContainer;
	
	if ( symbCont <> nil ) then
	begin
		//     3D
		basesCol := symbCont.Bases3D;
		
		if ( basesCol <> nil ) then
		begin
			//     3D
			newBase := basesCol.Add;
			
			if ( newBase <> nil ) then
			begin
				//    3D
				if ( CreateBase3D(newBase) ) then
				begin
					name := '';
					//     
					feature := newBase As IFeature7;

					//   
					if ( feature <> nil ) then
						name := feature.Name;

          mes := LoadStr( IDS_EDIT );
					if ( YesNoW(PWideChar(mes)) = 1 ) then
					begin
						//      
						base := basesCol.Base3D[name];
						//   
						EditBase3D( base );
					end;
				end
				else
        begin
					//   "  "
          mes := LoadStr( IDS_NOCREATE );
					ksMessageW( PWideChar(mes) );
        end;
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   -
// ---
procedure SetLeader3DTexts( baseLeader : IBaseLeader3D );

var
  leader      : ILeader;
  txtOnSh,
  txtUnderSh,
  txtOnBr,
  txtUnderBr,
  txtAfterSh  : IText;
begin
	leader := baseLeader As ILeader;

	if ( leader <> nil ) then
	begin
		//     
		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';
	end;
end;


//-------------------------------------------------------------------------------
//   -
// ---
procedure CreateBranchsLeader( leader : IBaseLeader3D );

var
  doc3D       : IDocument3D;
  baseObj,
  posObj      : IEntity;
  mObj1,
  mObj2       : IModelObject;
  branchs3D   : IBranchs3D;
  x, y, z,
  x1, y1, z1  : double;
  mes         : WideString;
begin
	if ( leader <> nil ) then
	begin
		//    3D  API7  API5
		doc3D := IUnknown( ksTransferInterface(PIUnknown(doc), ksAPI3DCom, 0{ }) ) As IDocument3D;

		if ( doc3D <> nil ) then
		begin
      mes := LoadStr( IDS_OBJ );
			//     
      baseObj := IEntity( doc3D.UserSelectEntity(nil, nil, PWideChar(mes)) );

      mes := LoadStr( IDS_BEGINBRANCH );
			//    
      //      
      doc3D.UserGetCursor( PWideChar(mes), x, y, z );

			mes := LoadStr( IDS_POSOBJ );
			//  ,   
      posObj := IEntity( doc3D.UserSelectEntity(nil, nil, PWideChar(mes)) );

			if ( (baseObj <> nil) And (posObj <> nil) ) then
			begin
        mes := LoadStr( IDS_BEGINSHELF );
				//  ,   
        //      
        doc3D.UserGetCursor( PWideChar(mes), x1, y1, z1 );
				//     API5  API7
        mObj1 := IUnknown( ksTransferInterface( PIUnknown(baseObj), ksAPI7Dual, 0) ) As IModelObject;
        mObj2 := IUnknown( ksTransferInterface( PIUnknown(posObj), ksAPI7Dual, 0) ) As IModelObject;

				if ( (mObj1 <> nil) And (mObj2 <> nil) ) then
				begin
					//   
					branchs3D := leader As IBranchs3D;

					//  
					if ( branchs3D <> nil ) then
						branchs3D.AddBranchByPoint( x, y, z, mObj1 );

					//  ,   
					leader.PositionObject := mObj2;
					leader.SetPosition( x1, y1, z1 );
				end;
        baseObj._Release;
        posObj._Release;
			end;
      doc3D._Release;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//  -
// ---
function CreateLeader3D( leader : IBaseLeader3D ) : boolean;
begin
	Result := false;

	if ( leader <> nil ) then
	begin
		//   -
		CreateBranchsLeader( leader );
		//   -
		SetLeader3DTexts( leader );
		//  
		leader.Update;
		Result := true;
	end;
end;


//-------------------------------------------------------------------------------
//   -
// ---
procedure EditLeaderPars( baseLeader : IBaseLeader3D );

var
  leader  : ILeader;
  txt     : IText;
  line    : ITextLine;
begin
	if ( baseLeader <> nil ) then
	begin
		leader := baseLeader As ILeader;

		if ( leader <> nil ) then
		begin
			//   -  
			leader.SignType := ksLGlueSign;
			//     
			leader.Arround := TRUE;

			//    -   
			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;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure LeaderAddBranch( leader : IBaseLeader3D );

var
  doc3D     : IDocument3D;
  baseObj   : IEntity;
  mObj      : IModelObject;
  branchs3D : IBranchs3D;
  x, y, z   : double;
  mes       : WideString;
begin
	if ( leader <> nil ) then
	begin
		//    3D  API7  API5
    doc3D := IUnknown( ksTransferInterface(PIUnknown(doc), ksAPI3DCom, 0{ }) ) As IDocument3D;

		if ( doc3D <> nil ) then
		begin
      mes := LoadStr( IDS_OBJ );
			//     
      baseObj := IEntity( doc3D.UserSelectEntity(nil, nil, PWideChar(mes)) );

      mes := LoadStr( IDS_BEGINBRANCH );
      //    
      //      
      doc3D.UserGetCursor( PWideChar(mes), x, y, z );

			if ( baseObj <> nil ) then
			begin
				//     API5  API7
        mObj := IUnknown( ksTransferInterface( PIUnknown(baseObj), ksAPI7Dual, 0) ) As IModelObject;

				if ( mObj <> nil ) then
				begin
					//   
					branchs3D := leader As IBranchs3D;

					//  
					if ( branchs3D <> nil ) then
						branchs3D.AddBranchByPoint( x, y, z, mObj );
				end;
        baseObj._Release;
			end;
      doc3D._Release;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//  -
// ---
procedure EditLeader3D( leader : IBaseLeader3D );

var
  featObj   : IFeature7;
  colorPars : IColorParam7;
begin
	if ( leader <> nil ) then
	begin
		//   
		LeaderAddBranch( leader );
		//   -
		EditLeaderPars( leader );

		//    
		featObj := leader As IFeature7;
		
		if ( featObj <> nil ) then
			//     
			leader.Name := GetNewName( featObj, IDS_LEADER );
		
		//     
		colorPars := leader As IColorParam7;
		
		//    
		if ( colorPars <> nil ) then
			colorPars.Color := RGB( 123, 40, 0 );

		//  
		leader.Update;
	end;
end;


//-------------------------------------------------------------------------------
//    - 3D
// ---
procedure Leader3DWork;

var
  symbCont  : ISymbols3DContainer;
  leadsCol  : ILeaders3D;
  newLeader,
  leader    : IBaseLeader3D;
  feature   : IFeature7;
  name,
  mes       : WideString;
begin
	//    3D
	symbCont := GetSymbols3DContainer;

	if ( symbCont <> nil ) then
	begin
		//   - 3D
		leadsCol := symbCont.Leaders3D;

		if ( leadsCol <> nil ) then
		begin
			//   - 3D
			newLeader := leadsCol.Add( o3d_leader3D );

			if ( newLeader <> nil ) then
			begin
				//  - 3D
				if ( CreateLeader3D(newLeader) ) then
				begin
					name := '';
					//     
					feature := newLeader As IFeature7;

					//   
					if ( feature <> nil ) then
						name := feature.Name;

					mes := LoadStr( IDS_EDIT );
					if ( YesNoW(PWideChar(mes)) = 1 ) then
					begin
						//      
						leader := leadsCol.Leader3D[name];
						//  -
						EditLeader3D( leader );
					end;
				end
				else
        begin
					//   "  "
          mes := LoadStr( IDS_NOCREATE );
					ksMessageW( PWideChar(mes) );
        end;
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//    3D
// ---
function CreateBrandLeader3D( baseLeader : IBaseLeader3D  ) : boolean;

var
  brandLeader : IBrandLeader;
begin
	Result := false;
	
	if ( baseLeader <> nil ) then
	begin
		//  
		CreateBranchsLeader( baseLeader );

		//    
		brandLeader := baseLeader As IBrandLeader;

		if ( brandLeader <> nil ) then
			// 
			brandLeader.Direction := false;
			
		//  
		baseLeader.Update;
		Result := true;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure EditBrandLeader3D( leader : IBaseLeader3D );

var
  brandLeader : IBrandLeader;
  des         : IText;
  featObj     : IFeature7;
  colorPars   : IColorParam7;
  str         : WideString;
begin
	if ( leader <> nil ) then
	begin
		//   
		LeaderAddBranch( leader );

		//    
		brandLeader := leader As IBrandLeader;
		
		if ( brandLeader <> nil ) then
		begin
			//    
			des := brandLeader.Designation;
			
			if ( des <> nil ) then
      begin
				//  
        str := LoadStr( IDS_MARK2 );
				des.Str := str;
      end;
		end;

		//    
		featObj := leader As IFeature7;

		if ( featObj <> nil ) then
			//     
			leader.Name := GetNewName( featObj, IDS_BRAND );
		
		//     
		colorPars := leader As IColorParam7;
		
		//    
		if ( colorPars <> nil ) then
			colorPars.Color := RGB( 128, 0, 128 );

		//  
		leader.Update;
	end;
end;


//-------------------------------------------------------------------------------
//     
// ---
procedure BrandLeader3DWork;

var
  symbCont    : ISymbols3DContainer;
  leadsCol    : ILeaders3D;
  newBrand,
  brandLeader : IBaseLeader3D;
  feature     : IFeature7;
  name,
  mes         : WideString;
begin
	//    3D
	symbCont := GetSymbols3DContainer;

	if ( symbCont <> nil ) then
	begin
		//   - 3D
		leadsCol := symbCont.Leaders3D;

		if ( leadsCol <> nil ) then
		begin
			//     3D
			newBrand := leadsCol.Add( o3d_brandLeader3D );

			if ( newBrand <> nil ) then
			begin
				//    3D
				if ( CreateBrandLeader3D(newBrand) ) then
				begin
					name := '';
					//     
					feature := newBrand As IFeature7;

					//   
					if ( feature <> nil ) then
						name := feature.Name;

					mes := LoadStr( IDS_EDIT );
					if ( YesNoW(PWideChar(mes)) = 1 ) then
					begin
						//      
						brandLeader := leadsCol.Leader3D[name];
						//    3D
						EditBrandLeader3D( brandLeader );
					end;
				end
				else
        begin
					//   "  "
          mes := LoadStr( IDS_NOCREATE );
					ksMessageW( PWideChar(mes) );
        end;
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
function CreateMarkLeader3D( baseLeader : IBaseLeader3D ) : boolean;

var
  markLeader      : IMarkLeader;
  textOnBranch,
  textUnderBranch : IText;
begin
	Result := false;

	if ( baseLeader <> nil ) then
	begin
		//  
		CreateBranchsLeader( baseLeader );

		//    
		markLeader := baseLeader As IMarkLeader;

		if ( markLeader <> nil ) then
		begin
			//     
			textOnBranch := markLeader.TextOnBranch;

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

			//     
			textUnderBranch := markLeader.TextUnderBranch;

			if ( textUnderBranch <> nil ) then
				//  
				textUnderBranch.Str := '3';
		end;
		//  
		baseLeader.Update;
		Result := true;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure EditMarkLeader3D( baseLeader : IBaseLeader3D );

var
  markLeader  : IMarkLeader;
  des         : IText;
  featObj     : IFeature7;
  colorPars   : IColorParam7;
begin
	if ( baseLeader <> nil ) then
	begin
		//   
		LeaderAddBranch( baseLeader );

		//    
		markLeader := baseLeader As IMarkLeader;

		if ( markLeader <> nil ) then
		begin
			//    
			des := markLeader.Designation;

			if ( des <> nil ) then
				//  
				des.Str := '10';
		end;

		//    
		featObj := baseLeader As IFeature7;

		if ( featObj <> nil ) then
			//     
			baseLeader.Name := GetNewName( featObj, IDS_MARK );
		
		//     
		colorPars := baseLeader As IColorParam7;
		
		//    
		if ( colorPars <> nil ) then
			colorPars.Color := RGB( 204, 153, 255 );
		
		//  
		baseLeader.Update;
	end;
end;


//-------------------------------------------------------------------------------
//      3D
// ---
procedure MarkLeader3DWork;
var
  symbCont    : ISymbols3DContainer;
  leadsCol    : ILeaders3D;
  newMark,
  markLeader  : IBaseLeader3D;
  feature     : IFeature7;
  name,
  mes         : WideString;
begin
	//    3D
	symbCont := GetSymbols3DContainer;

	if ( symbCont <> nil ) then
	begin
		//   - 3D
		leadsCol := symbCont.Leaders3D;

		if ( leadsCol <> nil ) then
		begin
			//     3D
			newMark := leadsCol.Add( o3d_markLeader3D );

			if ( newMark <> nil ) then
			begin
				//    3D
				if ( CreateMarkLeader3D(newMark) ) then
				begin
					name := '';
					//     
					feature := newMark As IFeature7;

					//   
					if ( feature <> nil ) then
						name := feature.Name;

					mes := LoadStr( IDS_EDIT );
					if ( YesNoW(PWideChar(mes)) = 1 ) then
					begin
						//      
						markLeader := leadsCol.Leader3D[name];
						//    3D
						EditMarkLeader3D( markLeader );
					end;
				end
				else
        begin
					//   "  "
          mes := LoadStr( IDS_NOCREATE );
					ksMessageW( PWideChar(mes) );
        end;
			end;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//     
// ---
procedure CreateToleranceBranch( tolerance : ITolerance3D );
var
  doc3D       : IDocument3D;
  baseObj,
  posObj      : IEntity;
  mObj1,
  mObj2       : IModelObject;
  branchs3D   : IBranchs3D;
  x, y, z,
  x1, y1, z1  : double;
  mes         : WideString;
begin
	if ( tolerance <> nil ) then
	begin
		//    3D  API7  API5
		doc3D := IUnknown( ksTransferInterface(PIUnknown(doc), ksAPI3DCom, 0{ }) ) As IDocument3D;

		if ( doc3D <> nil ) then
		begin
      mes := LoadStr( IDS_OBJ );
			//     
      baseObj := IEntity( doc3D.UserSelectEntity(nil, nil, PWideChar(mes)) );

      mes := LoadStr( IDS_ENDPOINT );
			//    
      //      
      doc3D.UserGetCursor( PWideChar(mes), x, y, z );

			mes := LoadStr( IDS_POSOBJ );
			//  ,   
      posObj := IEntity( doc3D.UserSelectEntity(nil, nil, PWideChar(mes)) );

			if ( (baseObj <> nil) And (posObj <> nil) ) then
			begin
        mes := LoadStr( IDS_TABLE );
				//    
        doc3D.UserGetCursor( PWideChar(mes), x1, y1, z1 );
				//     API5  API7
        mObj1 := IUnknown( ksTransferInterface( PIUnknown(baseObj), ksAPI7Dual, 0) ) As IModelObject;
        mObj2 := IUnknown( ksTransferInterface( PIUnknown(posObj), ksAPI7Dual, 0) ) As IModelObject;

				if ( (mObj1 <> nil) And (mObj2 <> nil) ) then
				begin
					//   
					branchs3D := tolerance As IBranchs3D;

					//  
					if ( branchs3D <> nil ) then
						branchs3D.AddBranchByPoint( x, y, z, mObj1 );

					//  ,   
					tolerance.PositionObject := mObj2;
					tolerance.SetPosition( x1, y1, z1 );
				end;
        baseObj._Release;
        posObj._Release;
			end;
      doc3D._Release;
		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;


//-------------------------------------------------------------------------------
//   
// ---
function CreateTolerance3D( tolerance : ITolerance3D ) : boolean;

var
  tolPar  : IToleranceParam;
begin
	Result := false;

	if ( tolerance <> nil ) then
	begin
		//  
		CreateToleranceBranch( tolerance );
		//     
		tolPar := tolerance As IToleranceParam;
		
		if ( tolPar <> nil ) then
		begin
			//    
			SetToleranceText( tolPar );
			//      -  
			tolPar.BasePointPos := ksTPBottomCenter;
		end;
		//   1-  - 
		tolerance.Set_ArrowType( 0, false );
		//  
		tolerance.Update;
		Result := true;
	end;
end;


//-------------------------------------------------------------------------------
//     
// ---
procedure ToleranceAddBranch( tolerance : ITolerance3D );
var
  doc3D     : IDocument3D;
  baseObj   : IEntity;
  mObj      : IModelObject;
  branchs3D : IBranchs3D;
  x, y, z   : double;
  mes       : WideString;
begin
	if ( tolerance <> nil ) then
	begin
		//    3D  API7  API5
    doc3D := IUnknown( ksTransferInterface(PIUnknown(doc), ksAPI3DCom, 0{ }) ) As IDocument3D;

		if ( doc3D <> nil ) then
		begin
      mes := LoadStr( IDS_OBJ );
			//     
      baseObj := IEntity( doc3D.UserSelectEntity(nil, nil, PWideChar(mes)) );

      mes := LoadStr( IDS_BEGINBRANCH );
      //    
      //      
      doc3D.UserGetCursor( PWideChar(mes), x, y, z );

			if ( baseObj <> nil ) then
			begin
				//     API5  API7
        mObj := IUnknown( ksTransferInterface( PIUnknown(baseObj), ksAPI7Dual, 0) ) As IModelObject;

				if ( mObj <> nil ) then
				begin
					//   
					branchs3D := tolerance As IBranchs3D;

					//  
					if ( branchs3D <> nil ) then
						branchs3D.AddBranchByPoint( x, y, z, mObj );

          //   
					tolerance.Set_ArrowType( 1, TRUE );
				end;
        baseObj._Release;
			end;
      doc3D._Release;
		end;
	end;
end;


//-------------------------------------------------------------------------------
//   
// ---
procedure EditTolerance3D( tolerance : ITolerance3D );

var
  tolPar    : IToleranceParam;
  featObj   : IFeature7;
  colorPars : IColorParam7;
begin
	if ( tolerance <> nil ) then
	begin
		ToleranceAddBranch( tolerance );

		//    
		tolPar := tolerance As IToleranceParam;

		if ( tolPar <> nil ) then
		begin
			//   
			tolPar.Vertical := TRUE;
			//      -  
			tolPar.BasePointPos := ksTPLeftBottom;
		end;

		//    
		featObj := tolerance As IFeature7;
		
		if ( featObj <> nil ) then
			//     
			tolerance.Name := GetNewName( featObj, IDS_TOLERANCE );

		//     
		colorPars := tolerance As IColorParam7;
		
		//    -
		if ( colorPars <> nil ) then
			colorPars.Color := RGB( 186, 12, 34 );

		//  
		tolerance.Update;
	end;
end;


//-------------------------------------------------------------------------------
//      3D
// ---
procedure Tolerance3DWork;

var
  symbCont    : ISymbols3DContainer;
  tolerCol    : ITolerances3D;
  newTolerance,
  tolerance   : ITolerance3D;
  feature     : IFeature7;
  name,
  mes         : WideString;
begin
	//    3D
	symbCont := GetSymbols3DContainer;

	if ( symbCont <> nil ) then
	begin
		//     3D
		tolerCol := symbCont.Tolerances3D;
		
		if ( tolerCol <> nil ) then
		begin
			//     3D
			newTolerance := tolerCol.Add;

			if ( newTolerance <> nil ) then
			begin
				//    3D
				if ( CreateTolerance3D(newTolerance) ) then
				begin
					name := '';
					//     
					feature := newTolerance As IFeature7;

					//   
					if ( feature <> nil ) then
						name := feature.Name;

					mes := LoadStr( IDS_EDIT );
					if ( YesNoW(PWideChar(mes)) = 1 ) then
					begin
						//      
						tolerance := tolerCol.Tolerance3D[name];
						//   
						EditTolerance3D( tolerance );
					end;
				end
				else
        begin
					//   "  "
          mes := LoadStr( IDS_NOCREATE );
					ksMessageW( PWideChar(mes) );
        end;
			end;
		end;
	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.


