function [ handlesR ] = Plot_Mechanism( handles )
%% Plot GSP model on axes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Base = getappdata( 0 , 'Base' );
Platform = getappdata( 0 , 'Platform' );
ConMatrix = getappdata( 0 , 'ConMatrix' );
LegLimits = getappdata( 0 , 'LegLimits' );
JointLimits = getappdata( 0 , 'JointLimits' );
Thickness_Base = getappdata( 0 , 'Thickness_Base' );
Thickness_Moving = getappdata( 0 , 'Thickness_Moving' );
if ( isempty(Base) == 1 ) ,     return;  end
if ( isempty(Platform) == 1 ) ,     return;  end
if ( isempty(ConMatrix) == 1 ) ,     return;  end
%if ( isempty(LegLimits) == 1 ) ,     return;  end
%if ( isempty(JointLimits) == 1 ) ,     return;  end
%if ( isempty(Thickness_Base) == 1 ) ,     return;  end
%if ( isempty(Thickness_Moving) == 1 ) ,     return;  end

grid on;rotate3d on;zlabel('Z');ylabel('Y');xlabel('X');
% campos('manual');

Platform.Translate = [ Platform.Px ; Platform.Py  ; Platform.Pz ];
Platform.Rotation = Rotation( Platform.alpha, Platform.beta, Platform.gamma , 'RPY' ) ;
[ Leg, Platform.Pbase, ErrorMsg ]= inverse_kinematic(Base, Platform, ConMatrix, LegLimits, JointLimits );

%%cla;

%%is there any platform on GUI ?? already ploted ??
%%if so delete them.. 
try
    ishandle( handles.platform_handle );
    delete( handles.platform_handle);
    handles.platform_handle  = tabla_cizimi_genel( Platform.Pbase, -Thickness_Moving , [ 255/255, 242/255, 221/255] );
catch exception
    handles.platform_handle  = tabla_cizimi_genel( Platform.Pbase, -Thickness_Moving , [ 255/255, 242/255, 221/255] );
end

try      
    ishandle( handles.base_handle );
    delete( handles.base_handle );
    handles.base_handle  = tabla_cizimi_genel ( Base.BTable , Thickness_Base , [ 222/255, 235/255, 250/255 ] );
catch exception
    handles.base_handle  = tabla_cizimi_genel ( Base.BTable , Thickness_Base , [ 222/255, 235/255, 250/255 ] );
end

try      
    ishandle( handles.Leg.Leg_handle );
    delete( handles.Leg.Leg_handle );
    handles.Leg.Leg_handle = 0;
catch exception
    handles.Leg.Leg_handle = 0;
end

try      
    ishandle( handles.Base.Btext );
    delete( handles.Base.Btext );
    handles.Base.Btext = 0;
catch exception
    handles.Base.Btext = 0;
end

try      
    ishandle( handles.Platform.Ptext );
    delete( handles.Platform.Ptext );
    handles.Platform.Ptext = 0;
catch exception
    handles.Platform.Ptext = 0;
end

try      
    ishandle( handles.Leg.Ltext );
    delete( handles.Leg.Ltext );
    handles.Leg.Ltext = 0;
catch exception
    handles.Leg.Ltext = 0;
end
    
for index = 1:1:6
    if ~isnan(ConMatrix(1,index)) && ~isnan(ConMatrix(2,index))

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
        % Bacaklar?n çizilmesi
        % draw legs as line with the use of conMatrix
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
            handles.Leg.Leg_handle(index) = line(...
            'XData',[Base.BTable(ConMatrix(1,index),1); Platform.Pbase(ConMatrix(2,index),1)],...
            'YData',[Base.BTable(ConMatrix(1,index),2); Platform.Pbase(ConMatrix(2,index),2)],...
            'ZData',[Base.BTable(ConMatrix(1,index),3); Platform.Pbase(ConMatrix(2,index),3)],...
            'Color',[161/221,172/255,86/255],'LineWidth',5,'Marker','o','MarkerFaceColor','g','MarkerSize',15);
       
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         
       %B nokta isimlerinin yazd?r?lmas?
       B = Base.BTable(ConMatrix(1,index),:);
       txt = ['\bf\fontname{Tahoma}\fontsize{12}\color{black}B_' int2str(ConMatrix(1,index))];
       handles.Base.Btext(index)=text( B(1),B(2),B(3),txt );
       
       
       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       %P nokta isimlerinin yazd?r?lmas?
       P = Platform.Pbase(ConMatrix(2,index),:);
       txt = ['\bf\fontname{Tahoma}\fontsize{12}\color{black}P_' int2str(ConMatrix(2,index))];
       handles.Platform.Ptext(index)=text( P(1),P(2),P(3),txt );

       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       %L bacak isimlerinin yazd?r?lmas?

       %önce bacak vektörünün do?rultusundaki birim vektör hesaplan?r
       L_vector = Platform.Pbase(ConMatrix(2,index),:) - Base.BTable(ConMatrix(1,index),:);

       unit_Lvector =  L_vector / norm ( L_vector ) ;
        %bacak birim vektörün yar? uzunlu?undaki noktaya L ismi yazd?r?l?r.
        nokta = unit_Lvector * Leg.L(index)/2 + ...
                Base.BTable(ConMatrix(1,index),:); 

        txt = ['\bf\fontname{Tahoma}\fontsize{12}\color{black}L_' int2str(index)];
        handles.Leg.Ltext(index)=text( nokta(1),nokta(2),nokta(3), txt);
        
        
        nokta = unit_Lvector * Leg.L(index) + ...
                 Base.BTable(ConMatrix(1,index),:);     
    else
        handles.Leg_handle(index) = 0;
    end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
% BA?LANTI NOKTALARININ ?S?MLER? EKRANDA GÖSTER?LS?N M? ?? 
% AN?MASYON SIRASINDA EKRANDA GÖSTER?LMES? ?STENMEYEB?L?R.
% try      
% %     ishandle( handles.checkbox_showNames );
%     if get(handles.checkbox_showNames,'Value')
%         for index = 1:1:6
%             if ~isnan(ConMatrix(1,index)) && ~isnan(ConMatrix(2,index))             
% 
%            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%            %P nokta isimlerinin yazd?r?lmas?
%            P = Platform.PBase(ConMatrix(2,index),:);
%            set(Platform.Ptext(index), 'Position', [P(1)-5,P(2)+5,P(3)]);
% 
%            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%            %L bacak isimlerinin yazd?r?lmas?
% 
%            %önce bacak vektörünün do?rultusundaki birim vektör hesaplan?r
%            L_vector = Platform.PBase(ConMatrix(2,index),:) - Base.BTable(ConMatrix(1,index),:);
% 
%            unit_Lvector(index,:) =  L_vector / norm ( L_vector ) ;
%             %bacak birim vektörün yar? uzunlu?undaki noktaya L ismi yazd?r?l?r.
%             nokta = unit_Lvector(index,:) * handles.Leg.L(index)/2 + ...
%                     Base.BTable(ConMatrix(1,index),:); 
% 
%             txt = ['\bf\fontname{Tahoma}\fontsize{12}\color{black}L_' int2str(index)];
%             set( handles.Leg.Ltext(index) ,'Position', [nokta(1),nokta(2)+5,nokta(3)]);
% 
%             end%if
%         end %for
%     end     %if handles.checkbox_showNames
% catch exception
% end 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    



%%[ sizeNaN , LegNumber ] = LegNumbers( ConMatrix );

% 
% maxBase = max( Base.BTable ); minBase = min( Base.BTable );
% maxPlatform = max( Platform.PBase ); minPlatform = min( Platform.PBase );
% 
% maxP = max( [ Base.BTable ; Platform.PBase ] );
% minP = min( [ Base.BTable ; Platform.PBase ] );
% 
% 
% % campos( [minBase(1,1) maxBase(1,2) maxPlatform(1,3)*1.5 ] ) ;
% 
% haxesAnalysis = gca;
% default_axis = axis ;
% 
% XLim = get(haxesAnalysis, 'XLim');
% YLim = get(haxesAnalysis, 'YLim');
% ZLim = get(haxesAnalysis, 'ZLim');
% 
% if ( minP(1) < XLim(1) ) XLim(1) = minP(1); end
% if ( maxP(1) > XLim(2) ) XLim(2) = maxP(1)*1.7; end
% 
% if ( minP(2) < YLim(1) ) YLim(1) = minP(2); end
% if ( maxP(2) > YLim(2) ) YLim(2) = maxP(2)*1.7; end
% 
% %%if ( minP(3) < ZLim(1) ) ZLim(1) = minP(3); end
% if ( minP(3) < ZLim(1) ) ZLim(1) = 0; end
% if ( maxP(3) > ZLim(2) ) ZLim(2) = maxP(3)*1.7; end
% 
% 
% set(handles.axesAnalysis, 'XLim' , XLim );
% set(handles.axesAnalysis, 'YLim' , YLim );
% set(handles.axesAnalysis, 'ZLim' , ZLim );
% 
% guidata(gcf, handles);
handlesR = handles;


%%guidata(hObject, handles);


end

