function varargout = Bstruct(varargin)
% BSTRUCT M-file for Bstruct.fig
%      BSTRUCT, by itself, creates a new BSTRUCT or raises the existing
%      singleton.

% Last Modified by GUIDE v2.5 15-Mar-2010 06:05:44

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Bstruct_OpeningFcn, ...
                   'gui_OutputFcn',  @Bstruct_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Bstruct is made visible.
function Bstruct_OpeningFcn(hObject, eventdata, handles, varargin)
% hObject    handle to figure
% handles    structure with handles and user data (see GUIDATA)

% Choose default command line output for Bstruct
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
% My
    MyUICs(1,1) = {'txt1'};   % Of Add atom with coords
    MyUICs(1,2) = {'lstType'};
    MyUICs(1,3) = {'txt2'};
    MyUICs(1,4) = {'edVx'};
    MyUICs(1,5) = {'edVy'};
    MyUICs(1,6) = {'edVz'};
    MyUICs(1,7) = {'btnAddAtom'};
    MyUICs(1,8) = {'btnSeekBond'}; % Of seek-atom bond
    MyUICs(1,9) = {'txt3'};   % Of Add atom rotating vector
    MyUICs(1,10) = {'txt4'};
    MyUICs(1,11) = {'txt5'};
    MyUICs(1,12) = {'lstVect1'};
    MyUICs(1,13) = {'lstVect2'};
    MyUICs(1,14) = {'edA1'};
    MyUICs(1,15) = {'edA2'};
    MyUICs(1,16) = {'edA3'};
    MyUICs(1,17) = {'edA4'};
    MyUICs(1,18) = {'edDistance'};
    MyUICs(1,19) = {'btnTurnAddAtom'};
    MyUICs(1,20) = {'edP1A1'};
    MyUICs(1,21) = {'edP1A2'};
    MyUICs(1,22) = {'edP2A1'};
    MyUICs(1,23) = {'edP2A2'};
    MyUICs(1,24) = {'chClock'};
    MyUICs(1,25) = {'btnChangeAtoms'};
    MyUICs(1,26) = {'btnDeleteAtom'};
    MyUICs(1,27) = {'btnTranslate'};
    MyUICs(1,28) = {'btnTurnGroup'};
    MyUICs(1,29) = {'btnAddGamma'};
    MyUICs(1,30) = {'btnAddHi'};
    MyUICs(1,31) = {'btnAddRo'};
    handles.MyUICNames = MyUICs;
    
    unitVectors = zeros(6,3);
    unitVectors(1,1) = 1;  % ort x
    unitVectors(2,1) = -1;  % ort -x
    unitVectors(3,2) = 1;  % ort y
    unitVectors(4,2) = -1;  % ort -y
    unitVectors(5,3) = 1;  % ort z
    unitVectors(6,3) = -1;  % ort -z
    
    handles.Vectors = unitVectors;
    guidata(hObject, handles);

% end My

% --- Outputs from this function are returned to the command line.
function varargout = Bstruct_OutputFcn(hObject, eventdata, handles) 

% Get default command line output from handles structure
varargout{1} = handles.output;

% CallBacks----------------------------------------------------------
function mnuNewModel_Callback(hObject, eventdata, handles)
global Type InfTable AtomTypes AtomCoords Bonds 
global Alphas Gammas Ros His
Type(1).Symbol = 'C';
Type(1).Mass = 12;
Type(2).Symbol = 'H';
Type(2).Mass = 1.088;
cols = size(Type,2);
A = cell(1, cols);
for i = 1:cols
    A(1,i) = {[int2str(i),' ',Type(i).Symbol,' ', num2str(Type(i).Mass,4)]};
end % i
set(handles.lstType, 'String', A);
set(handles.lstType, 'Value', 1);
guidata(gcbo, handles);

nBlocks = 1;
InfTable(8) = nBlocks;
AtomTypes = [];
AtomCoords = [];
Bonds = [];
Alphas = []; 
Gammas = []; 
Ros = []; 
His = [];
updateInfTable(handles);
set(handles.ednBlocks, 'String', int2str(nBlocks));
set(handles.mnuSaveAs, 'Enable', 'on');
set(handles.mnuNewModel, 'Enable', 'off');
set(handles.mnuOpen, 'Enable', 'off');
set(handles.mnuImportTxt, 'Enable', 'off');
% enable other menus
set(handles.mnuEdit, 'Enable', 'on');
set(handles.mnuAngles, 'Enable', 'on');
set(handles.mnuTransform, 'Enable', 'on');

% --------------------------------------------------------------------
function mnuOpen_Callback(hObject, eventdata, handles)
global Type InfTable AtomTypes AtomCoords Bonds 
global Alphas Gammas Ros His

    [fname, pname] = uigetfile('*.mat','Open','Bstruct.mat');
    if pname == 0, return, end
    
    update_ini(pname);
    
    handles.pathname = pname;
    set(handles.mnuSaveAs, 'Enable', 'off');
    set(handles.mnuSave, 'Enable', 'on');
    set(handles.mnuNewModel, 'Enable', 'off');
    set(handles.mnuOpen, 'Enable', 'on');
    set(handles.mnuImportTxt, 'Enable', 'off');
    set(handles.mnuToCalculi, 'Enable', 'on');
% enable other menus
set(handles.mnuEdit, 'Enable', 'on');
set(handles.mnuAngles, 'Enable', 'on');
set(handles.mnuTransform, 'Enable', 'on');

pname = handles.pathname;
fullname = strcat(pname,'Bstruct.mat');
load(fullname);
[rows cols] = size(Type);
A = cell(1, cols);
for i = 1:cols
    A(1,i) = {[int2str(i),' ',Type(i).Symbol,' ', num2str(Type(i).Mass,4)]};
end % i
set(handles.lstType, 'String', A);
set(handles.lstType, 'Value', 1);
guidata(gcbo, handles);

nBlocks = InfTable(8);
updateInfTable(handles);
set(handles.ednBlocks, 'String', int2str(nBlocks));

% --------------------------------------------------------------------
function mnuToCalculi_Callback(hObject, eventdata, handles)
% To calculi and viewer save txt-files
global Type InfTable AtomTypes AtomCoords Bonds 
global Alphas Gammas Ros His

nAtoms = InfTable(1);
nBonds = InfTable(2);
nAlphas = InfTable(3);
nGammas = InfTable(4);
nRos = InfTable(5);
nHis = InfTable(6);

pname = handles.pathname;
fullname = strcat(pname,'InfTable.txt');

fid = fopen(fullname,'w');
    fprintf(fid,'%12i%12i%12i%12i%12i%12i \r\n',InfTable(1:6));
    fprintf(fid,'%12i%12i \r\n',InfTable(7:8));
fclose(fid);

fullname = strcat(pname,'AtomCoords.txt');
fid = fopen(fullname,'w');
for i = 1:nAtoms
    fprintf(fid,'%11.4f%11.4f%11.4f \r\n',AtomCoords(i,:));
end % i
fclose(fid);

fullname = strcat(pname,'AtomMasses.txt');
fid = fopen(fullname,'w');
for i = 1:nAtoms
    mass = Type(AtomTypes(i)).Mass;
    fprintf(fid,'%11.4f \r\n', mass);
end % i
fclose(fid);

fullname = strcat(pname,'AtomSymbols.txt');
fid = fopen(fullname,'w');
for i = 1:nAtoms
    symbol = Type(AtomTypes(i)).Symbol;
    fprintf(fid,'%s \r\n', symbol);
end % i
fclose(fid);

BondUnitVectors = zeros(nBonds,3);
BondLengthes = zeros(nBonds,1);
for i = 1:nBonds
    Atom1 = Bonds(i,1);
    Atom2 = Bonds(i,2);
    [r,s] = atom_pair_vector(Atom1, Atom2);
    BondUnitVectors(i,:) = r / norm(r);
    BondLengthes(i) = s;
end % i

fullname = strcat(pname,'BondVectors.txt');
fid = fopen(fullname,'w');
for i = 1:nBonds
    fprintf(fid,'%11.4f%11.4f%11.4f \n', BondUnitVectors(i,:));
end % i
fclose(fid);

fullname = strcat(pname,'BondLengthes.txt');
fid = fopen(fullname,'w');
for i = 1:nBonds
    fprintf(fid,'%11.4f \n', BondLengthes(i));
end % i
fclose(fid);

fullname = strcat(pname,'BqStructures.txt');
fid = fopen(fullname,'w');
for i = 1:nBonds
    fprintf(fid,'%4i%4i%11.4f%11.4f%11.4f%11.4f%11.4f%11.4f \r\n', ...
        Bonds(i, :), -BondUnitVectors(i,:), BondUnitVectors(i,:));
end % i
fclose(fid);

if nAlphas > 0
    fullname = strcat(pname,'BaStructures.txt');
    fid = fopen(fullname,'w');
    for ia = 1:nAlphas
        Atom1 = Alphas(ia, 1);
        Atom2 = Alphas(ia, 2);
        Atom3 = Alphas(ia, 3);
        [e1,s1] = atom_pair_vector(Atom2, Atom1);
        s1 = 1.09/s1; 
        [e2,s2] = atom_pair_vector(Atom2, Atom3);
        s2 = 1.09/s2; 
        cs = e1*e2';
        sn = sqrt(1 - cs^2);
        b1 = (e2 - e1*cs)*s1/sn;
        b3 = (e1 - e2*cs)*s2/sn;
        b2 = b1 + b3;
        b1 = -b1;
        b3 = -b3;
        fprintf(fid,'%4i%4i%4i', Atom1, Atom2, Atom3);
        fprintf(fid,'%11.4f%11.4f%11.4f', b1(:));
        fprintf(fid,'%11.4f%11.4f%11.4f', b2(:));
        fprintf(fid,'%11.4f%11.4f%11.4f \n', b3(:));
    end % ia
    fclose(fid);
end % if nAlphas > 0

if nGammas > 0
    fullname = strcat(pname,'BgStructures.txt');
    fid = fopen(fullname,'w');
    for ig = 1:nGammas
        Atom1 = Gammas(ig, 1);
        Atom2 = Gammas(ig, 2);
        Atom3 = Gammas(ig, 3);
        [e1,s1] = atom_pair_vector(Atom2, Atom1);
        s1 = 1.09/s1; 
        [e2,s2] = atom_pair_vector(Atom2, Atom3);
        s2 = 1.09/s2;
        gVectorID = Gammas(ig, 4);
        if gVectorID > 0
            gVector = handles.Vectors(gVectorID,:);
        else
            eaux = atom_pair_vector(Gammas(ig, 5), Gammas(ig, 6));
            normalaux = cross(e1, eaux);
            normalaux = normalaux/norm(normalaux);
            gVector = cross(normalaux, e1);
        end % if gVectorID > 0
        b1 = gVector*s1;
        b3 = gVector*s2;
        b2 = b1 + b3;
        b1 = -b1;
        b3 = -b3;
        fprintf(fid,'%4i%4i%4i', Atom1, Atom2, Atom3);
        fprintf(fid,'%11.4f%11.4f%11.4f', b1(:));
        fprintf(fid,'%11.4f%11.4f%11.4f', b2(:));
        fprintf(fid,'%11.4f%11.4f%11.4f \n', b3(:));
    end % ig
    fclose(fid);
end % if nGammas > 0

if nRos > 0
    fullname = strcat(pname,'BrStructures.txt');
    fid = fopen(fullname,'w');
    for ir = 1:nRos
        Atom1 = Ros(ir, 1);
        Atom2 = Ros(ir, 2);
        Atom3 = Ros(ir, 3);
        Atom4 = Ros(ir, 4);
        [e1, s1] = atom_pair_vector(Atom2, Atom1);
        [e2, s2] = atom_pair_vector(Atom2, Atom3);
        [e4, s4] = atom_pair_vector(Atom2, Atom4);
        s1 = 1.09/s1;
        s2 = 1.09/s2;
        s4 = 1.09/s4;
        eAux = cross(e1,e2);
        s = norm(eAux);
        eAux = eAux/s; % it is normal to the Plane
        cs = e4*eAux';
        sn = sqrt(1 - cs^2);
        Auxr1 = (e4 - eAux*cs)/sn; % it is g-vector
        Auxr2 = (eAux - e4*cs)/sn; % it is f-vector
        temp = cross(e2, Auxr1);
        e3 = cross(e1, Auxr1);
        b1 = s1*temp/s;
        b3 = -s2 * e3/s;
        b4 = s4*Auxr2;
        b2 = -(b4 + b1 + b3);
        fprintf(fid,'%4i%4i%4i%4i', Atom1, Atom2, Atom3, Atom4);
        fprintf(fid,'%11.4f%11.4f%11.4f', b1(:));
        fprintf(fid,'%11.4f%11.4f%11.4f', b2(:));
        fprintf(fid,'%11.4f%11.4f%11.4f', b3(:));
        fprintf(fid,'%11.4f%11.4f%11.4f \n', b4(:));
    end % ir
    fclose(fid);
end % nRos > 0

if nHis > 0
    fullname = strcat(pname,'BhStructures.txt');
    fid = fopen(fullname,'w');
    for ih = 1:nHis
        % Plane 1
        Atom1 = His(ih, 1);
        Atom2 = His(ih, 2);
        Atom3 = His(ih, 3);
        [e1, s1] = atom_pair_vector(Atom2, Atom1);
        [e2, s2] = atom_pair_vector(Atom2, Atom3);
        s1 = 1.09/s1;
        s2 = 1.09/s2;
            % Get eaux as the turn axis
        i = His(ih, 4);
        if i > 0    % axis is a ready unit vector
            eaux = handles.Vectors(i,:);
        else
            eaux = atom_pair_vector(His(ih, 5), His(ih, 6));
        end % if > 0
            % Get normal
        normal = cross(e1, e2);
        normal = normal/norm(normal);
        ete1 = eaux*e1';
        ete2 = eaux*e2';
        e1e2 = e1*e2';
        A = sqrt(ete1^2 + ete2^2 -2*e1e2*ete1*ete2);
        b1 = s1*normal*ete2/A;
        b3 = -s2*normal*ete1/A;
        b2 = -(b1 + b3);
        % Plane 2
        Atom4 = His(ih, 1+6);
        Atom5 = His(ih, 2+6);
        Atom6 = His(ih, 3+6);
        [e1, s1] = atom_pair_vector(Atom5, Atom4);
        [e2, s2] = atom_pair_vector(Atom5, Atom6);
        s1 = 1.09/s1;
        s2 = 1.09/s2;
            % Get eaux as the turn axis
        i = His(ih, 4+6);
        if i > 0    % axis is a ready unit vector
            eaux = handles.Vectors(i,:);
        else
            eaux = atom_pair_vector(His(ih, 5+6), His(ih, 6+6));
        end % if  > 0
            % Get normal
        normal = cross(e1, e2);
        normal = normal/norm(normal);
        ete1 = eaux*e1';
        ete2 = eaux*e2';
        e1e2 = e1*e2';
        A = sqrt(ete1^2 + ete2^2 -2*e1e2*ete1*ete2);
        b4 = s1*normal*ete2/A;
        b6 = -s2*normal*ete1/A;
        b5 = -(b4 + b6);
        fprintf(fid,'%4i%4i%4i%4i%4i%4i', ...
            Atom1, Atom2, Atom3, Atom4, Atom5, Atom6);
        fprintf(fid,'%11.4f%11.4f%11.4f', b1(:));
        fprintf(fid,'%11.4f%11.4f%11.4f', b2(:));
        fprintf(fid,'%11.4f%11.4f%11.4f', b3(:));
        fprintf(fid,'%11.4f%11.4f%11.4f', b4(:));
        fprintf(fid,'%11.4f%11.4f%11.4f', b5(:));
        fprintf(fid,'%11.4f%11.4f%11.4f \n', b6(:));
    end % ih
    fclose(fid);
end % if nHis > 0
update_ini(pname);

% --- Executes during object creation, after setting all properties.
function ednAtoms_CreateFcn(hObject, eventdata, handles)

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function ednBonds_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function ednAlphas_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function ednGammas_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function ednRos_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function ednHis_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function ednVibrCoords_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function ednBlocks_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in btnCloseView.
function btnCloseView_Callback(hObject, eventdata, handles)
delete(handles.Hf);
set(handles.lstQs, 'String', {});
set(handles.lstQs, 'Visible', 'off');
set(handles.btnCloseView, 'Enable', 'off');
set(handles.btnViewBonds, 'Enable', 'on');
set(handles.btnViewA, 'Enable', 'on');
set(handles.btnViewG, 'Enable', 'on');
set(handles.btnViewRo, 'Enable', 'on');
set(handles.btnViewHi, 'Enable', 'on');

% --- Executes on button press in btnViewBonds.
function btnViewBonds_Callback(hObject, eventdata, handles)
global InfTable AtomCoords Bonds 

handles.Hf = figure;
nBonds = InfTable(2);
clear A
A = cell(1, nBonds);
formstr = '%8.4f %8.4f %8.4f ';
for i = 1:nBonds
    A(1,i) = {[int2str(i),' ', ...
        int2str(Bonds(i,1)),' (', ...
        num2str(AtomCoords(Bonds(i,1),:),formstr),') ', ...
        int2str(Bonds(i,2)),' (', ...
        num2str(AtomCoords(Bonds(i,2),:),formstr),')']};
end % i
set(handles.lstQs, 'String', A);
set(handles.lstQs, 'Value', 1);
set(handles.lstQs, 'Visible', 'on');
guidata(gcbo, handles);
set_view_on('q',handles);

% --- Executes on button press in btnViewA.
function btnViewA_Callback(hObject, eventdata, handles)
global InfTable Alphas Gammas Ros His

handles.Hf = figure;
nAlphas = InfTable(3);
clear A
A = cell(1, nAlphas);
for i = 1:nAlphas
    A(1,i) = {[int2str(i),'   ', ...
        int2str(Alphas(i,1)),' ', ...
        int2str(Alphas(i,2)),' ', ...
        int2str(Alphas(i,3))]};
end % i
set(handles.lstQs, 'String', A);
set(handles.lstQs, 'Value', 1);
set(handles.lstQs, 'Visible', 'on');
guidata(gcbo, handles);
set_view_on('a',handles);

% --- Executes on button press in btnViewG.
function btnViewG_Callback(hObject, eventdata, handles)
global InfTable Alphas Gammas Ros His

handles.Hf = figure;
nGammas = InfTable(4);
A = cell(1, nGammas);
for i = 1:nGammas
    A(1,i) = {[int2str(i),'   ', ...
        int2str(Gammas(i,1)),' ', ...
        int2str(Gammas(i,2)),' ', ...
        int2str(Gammas(i,3))]};
end % i
set(handles.lstQs, 'String', A);
set(handles.lstQs, 'Value', 1);
set(handles.lstQs, 'Visible', 'on');
guidata(gcbo, handles);
set_view_on('g',handles);

% --- Executes on button press in btnViewRo.
function btnViewRo_Callback(hObject, eventdata, handles)
global InfTable Alphas Gammas Ros His

handles.Hf = figure;
nRos = InfTable(5);
clear A
A = cell(1, nRos);
for i = 1:nRos
    A(1,i) = {[int2str(i),'   ', ...
        int2str(Ros(i,1)),' ', ...
        int2str(Ros(i,2)),' ', ...
        int2str(Ros(i,3)),'   ', ...
        int2str(Ros(i,4))]};
end % i
set(handles.lstQs, 'String', A);
set(handles.lstQs, 'Value', 1);
set(handles.lstQs, 'Visible', 'on');
guidata(gcbo, handles);
set_view_on('r',handles);

% --- Executes on button press in btnViewHi.
function btnViewHi_Callback(hObject, eventdata, handles)
global InfTable Alphas Gammas Ros His

handles.Hf = figure;
nHis = InfTable(6);
clear A
A = cell(1, nHis);
for i = 1:nHis
    A(1,i) = {[int2str(i),'   ', ...
        int2str(His(i,1)),' ', ...
        int2str(His(i,2)),' ', ...
        int2str(His(i,3)),'   ', ...
        int2str(His(i,7)),' ', ...
        int2str(His(i,8)),' ', ...
        int2str(His(i,9))]};
end % i
set(handles.lstQs, 'String', A);
set(handles.lstQs, 'Value', 1);
set(handles.lstQs, 'Visible', 'on');
guidata(gcbo, handles);
set_view_on('h',handles);

% --- Executes on selection change in lstQs.
function lstQs_Callback(hObject, eventdata, handles)
View_selection(handles)

% --- Executes during object creation, after setting all properties.
function lstQs_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --------------------------------------------------------------------
function mnuImportTxt_Callback(hObject, eventdata, handles)
% Import from *.txt model files
global Type InfTable AtomTypes AtomCoords Bonds
global Alphas Gammas Ros His

[fname, pname] = uigetfile('*.txt','Open','InfTable.txt');
if pname == 0, return, end
fullname = strcat(pname,'InfTable.txt');
handles.pathname = pname;
guidata(gcbo, handles);
fid = fopen(fullname,'r');
InfTable = fscanf(fid,'%i',8);
fclose(fid);
nAtoms = InfTable(1);
nBonds = InfTable(2);
nAlphas = InfTable(3);
nGammas = InfTable(4);
nRos = InfTable(5);
nHis = InfTable(6);
nVibrCoords = InfTable(7);
nBlocks = InfTable(8);

fullname = strcat(pname,'AtomCoords.txt');
load(fullname);
fullname = strcat(pname,'AtomMasses.txt');
load(fullname);

Symbols = '';
fullname = strcat(pname,'AtomSymbols.txt');
fid = fopen(fullname,'r');
while feof(fid) == 0
    line = fgetl(fid);
    Symbols = char(Symbols,line);
end % while
fclose(fid);
AtomTypes = zeros(nAtoms,1);
Type(1).Symbol = Symbols(2);
Type(1).Mass = AtomMasses(1);
AtomTypes(1) = 1;
n = 1;
for i = 2:nAtoms
    coincide = 0;
    for j = 1:n
        if strcmp(Type(j).Symbol, Symbols(i + 1))
            coincide = 1;
            AtomTypes(i) = j;
            break
        end % if
    end % j
    if coincide, continue, end
    n = n + 1;
    Type(n).Symbol = Symbols(i + 1);
    Type(n).Mass = AtomMasses(i);
    AtomTypes(i) = n;
end % i
cols = size(Type,2);
A = cell(1, cols);
for i = 1:cols
    A(1,i) = {[int2str(i),' ',Type(i).Symbol,' ', num2str(Type(i).Mass,4)]};
end % i
set(handles.lstType, 'String', A);
set(handles.lstType, 'Value', 1);
guidata(gcbo, handles);

if nBonds
    fullname = strcat(pname,'BqStructures.txt');
    load(fullname);
    Bonds = BqStructures(1:end,1:2);
    clear BqStructures
end % nBonds

if nAlphas
    fullname = strcat(pname,'BaStructures.txt');
    load(fullname);
    Alphas = BaStructures(1:end,1:3);
    clear BaStructures
end % nAlphas

if nGammas
    fullname = strcat(pname,'BgStructures.txt');
    load(fullname);
    Gammas = BgStructures(1:end,1:3);
    clear BgStructures
    disp('Warning: Correct Gammas manually!')
end % nGammas

if nRos
    fullname = strcat(pname,'BrStructures.txt');
    load(fullname);
    Ros = BrStructures(1:end,1:4);
    clear BrStructures
end % nRos

if nHis
    fullname = strcat(pname,'BhStructures.txt');
    load(fullname);
    RowHi = zeros(1,12);
    for i = 1:nHis
        RowHi(1:3) = BhStructures(i,1:3);
        RowHi(7:9) = BhStructures(i,4:6);
        His = [His; RowHi];
    end % i
    clear BhStructures
    disp('Warning: Correct His manually!')
end % nHis

%InfTable(4:7) = 0;
updateInfTable(handles);
nBlocks = 1;
InfTable(8) = nBlocks;
set(handles.ednBlocks, 'String', int2str(nBlocks));
set(handles.mnuSave, 'Enable', 'on');
set(handles.mnuNewModel, 'Enable', 'off');
set(handles.mnuOpen, 'Enable', 'off');
set(handles.mnuImportTxt, 'Enable', 'off');
set(handles.mnuToCalculi, 'Enable', 'on');
% enable other menus
set(handles.mnuEdit, 'Enable', 'on');
set(handles.mnuAngles, 'Enable', 'on');
set(handles.mnuTransform, 'Enable', 'on');

% --------------------------------------------------------------------
function mnuSave_Callback(hObject, eventdata, handles)
global Type InfTable AtomTypes AtomCoords Bonds 
global Alphas Gammas Ros His

pname = handles.pathname;
fullname = strcat(pname,'Bstruct.mat');
file = fullname;
save(file, 'Type', 'InfTable', 'AtomTypes', 'AtomCoords', 'Bonds', ...
           'Alphas', 'Gammas', 'Ros', 'His');
update_ini(pname);

% --------------------------------------------------------------------
function mnuSaveAs_Callback(hObject, eventdata, handles)
global Type InfTable AtomTypes AtomCoords Bonds 
global Alphas Gammas Ros His

[fname, pname] = uiputfile('*.mat','Save','Bstruct.mat');
if pname == 0, return, end

update_ini(pname);

fullname = strcat(pname,fname);
file = fullname;
save(file, 'Type', 'InfTable', 'AtomTypes', 'AtomCoords', 'Bonds', ...
           'Alphas', 'Gammas', 'Ros', 'His');
handles.pathname = pname;
guidata(gcbo, handles);
set(handles.mnuSaveAs, 'Enable', 'off');
set(handles.mnuSave, 'Enable', 'on');
set(handles.mnuOpen, 'Enable', 'on');
set(handles.mnuToCalculi, 'Enable', 'on');

% --------------------------------------------------------------------
function mnuEdit_Callback(hObject, eventdata, handles)

% --------------------------------------------------------------------
function mnuFile_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edPlanex_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edPlaney_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edPlanez_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edAxisx_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edAxisy_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edAxisz_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edOrder_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --------------------------------------------------------------------
function mnuExit_Callback(hObject, eventdata, handles)
close % This Application

% --------------------------------------------------------------------
function mnuAddAtomCoords_Callback(hObject, eventdata, handles)
% Make visible Add atom with coords UIControls
Pos = [ ...
        [4 16.769 8 1.615]; ...         % txt1
        [12 16.769 18 1.615]; ...       % lstType
        [32 16.769 15 1.615]; ...       % txt2
        [48 16.769 9.8 1.615]; ...      % edVx
        [58.6 16.769 9.8 1.615]; ...    % edVy 
        [69.2 16.769 9.8 1.615]; ...    % edVx
        [80 16.769 12.2 1.771]];        % btnAddAtom
MyUICs = handles.MyUICNames;
n = size(MyUICs,2);
for i = 1:n
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'off');
end % i

for i = 1:7
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'on');
    set(eval(['handles.',UIC]),'Position', Pos(i,:));
end % i
set(handles.txt1,'String', 'Type');
set(handles.txt2,'String', 'Coords x, y, z:');
set(handles.edVx,'String', '0.0');
set(handles.edVy,'String', '0.0');
set(handles.edVz,'String', '0.0');


% --------------------------------------------------------------------
function mnuBondsSeek_Callback(hObject, eventdata, handles)
% Make visible BondsSeek UIControls
Pos = [ ...
        [4.2 16.863 36 1.617]; ...
        [42 16.863 8.2 1.617]; ... 
        [52 16.863 8.2 1.617]; ...
        [64 16.863 20.2 1.617]];
MyUICs = handles.MyUICNames;
n = size(MyUICs,2);
for i = 1:n
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'off');
end % i

MyUICs = MyUICs([1,4,5,8]);
for i = 1:4
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'on');
    set(eval(['handles.',UIC]),'Position', Pos(i,:));
end % i
set(handles.txt1,'String', 'Seek bonds as between atoms:');
set(handles.edVx,'String', '0');
set(handles.edVy,'String', '0');

% --- Executes during object creation, after setting all properties.
function lstType_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edVx_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edVy_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edVz_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in btnAddAtom.
function btnAddAtom_Callback(hObject, eventdata, handles)
% Add atom with coords
global Type InfTable AtomTypes AtomCoords

nAtoms = InfTable(1);
nAtoms = nAtoms + 1;
t = get(handles.lstType,'Value');
AtomTypes = [AtomTypes; t];
AtomCoords = [AtomCoords; [str2double(get(handles.edVx,'String')), ...
                           str2double(get(handles.edVy,'String')), ...
                           str2double(get(handles.edVz,'String'))]];
InfTable(1) = nAtoms;
set(handles.ednAtoms, 'String', int2str(nAtoms));

% --- Executes on button press in btnSeekBond.
function btnSeekBond_Callback(hObject, eventdata, handles)
% Bonds seek
global InfTable AtomCoords Bonds
discr = 0.0001;
Atom1 = str2double(get(handles.edVx,'String'));
Atom2 = str2double(get(handles.edVy,'String'));
r0 = norm(AtomCoords(Atom1,:) - AtomCoords(Atom2,:));
nAtoms = InfTable(1);
nBonds = InfTable(2);
for i = 1:(nAtoms - 1)
    for j = (i+1):nAtoms
        r = norm(AtomCoords(j,:) - AtomCoords(i,:));
        if abs(r - r0) <= discr
            % is there such a bond?
            coincide = 0;
            for k = 1:nBonds
                if (i == Bonds(k,1)) & (j == Bonds(k,2))| ...
                   (j == Bonds(k,1)) & (i == Bonds(k,2))     
                    coincide = 1;
                    break
                end % if
            end  % k
            if coincide == 0
                Bonds = [Bonds; i j];
                nBonds = size(Bonds, 1);
            end %if coincide == 0
        end % if
    end % j
end % i
updateInfTable(handles);

% --------------------------------------------------------------------
function mnuRotateVector_Callback(hObject, eventdata, handles)
Pos = [ ...
        [24 21.483 16.2 1.617]; ...    % txt1 'Vector to turn'
        [4 20.02 18 1.617]; ...     % lstType ''
        [44 21.483 16.2 1.617]; ...    % txt2 'Axis unit vector'
        [64 21.483 24.6 1.617]; ...   % txt3 'Turn Angle grad min sec' 
        [92 21.483 12.2 1.617]; ...    % txt4 'Atom to start'
        [92 16.863 12.2 1.617]; ...    % txt5 'Distance'
        [24 19.866 18 1.617]; ...    % lstVect1 ''
        [44 19.866 18 1.617]; ...    % lstVect2 ''
        [64 19.866 8 1.617]; ...    % edA1 ''
        [72 19.866 8 1.617]; ...    % edA2 ''
        [80 19.866 8 1.617]; ...    % edA3 ''
        [92 19.866 8 1.617]; ...    % edA4 ''
        [92 15.323 8 1.617]; ...    % edDistance ''
        [92 12.089 16.2 1.617]; ...    % btnTurnAddAtom ''
        [24 16.863 8 1.617]; ...    % edP1A1 ''
        [32 16.863 8 1.617]; ...    % edP1A2 ''
        [44 16.863 8 1.617]; ...    % edP2A1 ''
        [52 16.863 8 1.617]; ...    % edP2A2 ''
        [64 18.403 24.2 1.617]];      % chClock
MyUICs = handles.MyUICNames;
n = size(MyUICs,2);
for i = 1:n
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'off');
end % i

MyUICs = MyUICs([1:3,9:24]);
for i = 1:19
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'on');
    set(eval(['handles.',UIC]),'Position', Pos(i,:));
end % i
set(handles.txt1,'String', 'Vector to turn');
set(handles.txt2,'String', 'Axis unit vector');
set(handles.txt3,'String', 'Turn Angle grad min sec');
set(handles.txt4,'String', 'Atom to start');
set(handles.txt5,'String', 'Distance');
set(handles.edA1,'String', '0');
set(handles.edA2,'String', '0');
set(handles.edA3,'String', '0');
set(handles.edA4,'String', '0');
set(handles.edP1A1,'String', '0');
set(handles.edP1A2,'String', '0');
set(handles.edP2A1,'String', '0');
set(handles.edP2A2,'String', '0');
set(handles.edDistance,'String', '0.0');
set(handles.edP1A1,'Visible', 'off');
set(handles.edP1A2,'Visible', 'off');
set(handles.lstVect1,'Value', 1);
set(handles.edP2A1,'Visible', 'off');
set(handles.edP2A2,'Visible', 'off');
set(handles.lstVect2,'Value', 1);

% --- Executes on selection change in lstVect1.
function lstVect1_Callback(hObject, eventdata, handles)
handles.lstVect1Num = get(hObject,'Value');
guidata(gcbo, handles);
if handles.lstVect1Num < 7
    set(handles.edP1A1,'Visible', 'off');
    set(handles.edP1A2,'Visible', 'off');
else 
    set(handles.edP1A1,'Visible', 'on');
    set(handles.edP1A2,'Visible', 'on');
end % if

% --- Executes during object creation, after setting all properties.
function lstVect1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on selection change in lstVect2.
function lstVect2_Callback(hObject, eventdata, handles)
handles.lstVect2Num = get(hObject,'Value');
guidata(gcbo, handles);
if handles.lstVect2Num < 7
    set(handles.edP2A1,'Visible', 'off');
    set(handles.edP2A2,'Visible', 'off');
else 
    set(handles.edP2A1,'Visible', 'on');
    set(handles.edP2A2,'Visible', 'on');
end % if

% --- Executes during object creation, after setting all properties.
function lstVect2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edA1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edA2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edA3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in chClock.
function chClock_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edA4_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edDistance_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in btnTurnAddAtom.
function btnTurnAddAtom_Callback(hObject, eventdata, handles)
% Add atom turning vector
global Type InfTable AtomTypes AtomCoords

% Turn vector  !! must be an unit vector
numVt = get(handles.lstVect1,'Value');
if numVt < 7
    vector_to_turn = handles.Vectors(numVt,:);
else
    Atom1 = str2double(get(handles.edP1A1,'String'));
    Atom2 = str2double(get(handles.edP1A2,'String'));
    vector_to_turn = atom_pair_vector(Atom1, Atom2);
end % if

% Axis vector !! must be an unit vector
numVa = get(handles.lstVect2,'Value');
if numVa < 7
    ax = handles.Vectors(numVa,:);
else
    Atom1 = str2double(get(handles.edP2A1,'String'));
    Atom2 = str2double(get(handles.edP2A2,'String'));
    ax = atom_pair_vector(Atom1, Atom2);
end % if

% Turn angle is unsigned grad, min and sec
angle = [str2double(get(handles.edA1,'String')), ...
         str2double(get(handles.edA2,'String')), ...
         str2double(get(handles.edA3,'String'))];
s = 1;  % Sign of a Turn angle
if get(handles.chClock,'Value'),s = -1;end   
phirad = s*pi*(angle(1) + angle(2)/60 + angle(3)/3600)/180;
M = matrturn(ax, phirad);
vector_new = M*vector_to_turn';
vector_new = vector_new';

% Add atom
nAtoms = InfTable(1) + 1;
t = get(handles.lstType,'Value');
AtomTypes = [AtomTypes; t];
AtomStart = str2double(get(handles.edA4,'String'));
Distanse = str2double(get(handles.edDistance,'String'));
rnew = AtomCoords(AtomStart,:) + Distanse*vector_new;
AtomCoords = [AtomCoords; rnew];

InfTable(1) = nAtoms;
set(handles.ednAtoms, 'String', int2str(nAtoms));

% --- Executes during object creation, after setting all properties.
function edP1A1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edP1A2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edP2A1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edP2A2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --------------------------------------------------------------------
function mnuChangeAtoms_Callback(hObject, eventdata, handles)
% Change atoms type
Pos = [ ...
        [4 18.403 20 1.617]; ...    % txt1 'Atoms to change'
        [60 18.403 18 1.617]; ...     % lstType ''
        [44 18.403 15 1.617]; ...    % txt2 'New Type'
        [24 18.403 12 1.617]; ...    % edA1 ''
        [80 18.403 16 1.617]];      % btnChangeAtoms
MyUICs = handles.MyUICNames;
n = size(MyUICs,2);
for i = 1:n
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'off');
end % i

MyUICs = MyUICs([1:3,14,25]);
for i = 1:5
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'on');
    set(eval(['handles.',UIC]),'Position', Pos(i,:));
end % i
set(handles.txt1,'String', 'Atoms to change');
set(handles.txt2,'String', 'New Type');
set(handles.edA1,'String', ' ');

% --- Executes on button press in btnChangeAtoms.
function btnChangeAtoms_Callback(hObject, eventdata, handles)
global Type InfTable AtomTypes AtomCoords Bonds 

str = get(handles.edA1,'String');
eval(['ind = [',str,'];']);
t = get(handles.lstType,'Value');
for i = ind
    AtomTypes(i) = t;
end % i

% --------------------------------------------------------------------
function mnuDeleteAtom_Callback(hObject, eventdata, handles)
Pos = [ ...
        [4 16.863 16  1.617]; ...    % txt1 'Atom to delete'
        [24 16.863 8 1.617]; ...   % edA1 ''
        [36 16.863 16 1.617]];      % btnDeleteAtom
MyUICs = handles.MyUICNames;
n = size(MyUICs,2);
for i = 1:n
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'off');
end % i

MyUICs = MyUICs([1,14,26]);
for i = 1:3
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'on');
    set(eval(['handles.',UIC]),'Position', Pos(i,:));
end % i
set(handles.txt1,'String', 'Atom to delete');
set(handles.edA1,'String', '0');

% --- Executes on button press in btnDeleteAtom.
function btnDeleteAtom_Callback(hObject, eventdata, handles)

global Type InfTable AtomTypes AtomCoords Bonds Alphas Gammas Ros His

nBonds = InfTable(2);
nAlphas = InfTable(3);
nGammas = InfTable(4);
nRos = InfTable(5);
nHis = InfTable(6);
n = str2double(get(handles.edA1,'String'));
AtomCoords(n,:) = [];
AtomTypes(n,:) = [];

% Bonds to delete
iBonds = [];
for i = 1:nBonds
    k = find(Bonds(i, :) == n);
    if ~isempty(k)
        nBonds = nBonds - 1;
        iBonds = [iBonds, i];
    else
        m = find(Bonds(i, :) > n);
        if ~isempty(m)
            Bonds(i, m) = Bonds(i, m) - 1;
        end % if m
    end % if k
end % i Bonds
Bonds(iBonds,:) = [];

% Other coordinates
if nAlphas > 0
    iAlphas = [];
    for i = 1:nAlphas
        k = find(Alphas(i, :) == n);
        if ~isempty(k)
            nAlphas = nAlphas - 1;
            iAlphas = [iAlphas, i];
        else
            m = find(Alphas(i, :) > n);
            if ~isempty(m)
                Alphas(i, m) = Alphas(i, m) - 1;
            end % if m
        end % if k
    end % i Alphas
    Alphas(iAlphas,:) = [];
end % if nAlphas > 0

if nGammas > 0
    iGammas = [];
    for i = 1:nGammas
        k = find(Gammas(i, :) == n);
        if ~isempty(k)
            nGammas = nGammas - 1;
            iGammas = [iGammas, i];
        else
            m = find(Gammas(i, :) > n);
            if ~isempty(m)
                Gammas(i, m) = Gammas(i, m) - 1;
            end % if m
        end % if k
    end % i Gammas
    Gammas(iGammas,:) = [];
end % if nGammas > 0

if nRos > 0
    iRos = [];
    for i = 1:nRos
        k = find(Ros(i, :) == n);
        if ~isempty(k)
            nRos = nRos - 1;
            iRos = [iRos, i];
        else
            m = find(Ros(i, :) > n);
            if ~isempty(m)
                Ros(i, m) = Ros(i, m) - 1;
            end % if m
        end % if k
    end % i Ros
    Ros(iRos,:) = [];
end % if nRos > 0

if nHis > 0
    iHis = [];
    for i = 1:nHis
        k = find(His(i, :) == n);
        if ~isempty(k)
            nHis = nHis - 1;
            iHis = [iHis, i];
        else
            m = find(His(i, :) > n);
            if ~isempty(m)
                His(i, m) = His(i, m) - 1;
            end % if m
        end % if k
    end % i His
    His(iHis,:) = [];
end % if nHis > 0
updateInfTable(handles);

% --------------------------------------------------------------------
function mnuTransform_Callback(hObject, eventdata, handles)

% --------------------------------------------------------------------
function mnuTranslate_Callback(hObject, eventdata, handles)
% Translate atoms in direction by distance
Pos = [ ...
        [4 18.403 8 1.54]; ...    % txt1 'Atoms'
        [12 18.403 12 1.54]; ...     % edA1 ''
        [24 18.403 24 1.54]; ...    % txt2 'translate in direction of'
        [48 18.403 18 1.54]; ...    % lstVect1 ''
        [68 18.403 4 1.54]; ...    % txt3 'by'
        [72 18.403 12 1.54]; ...    % edVx '0.0'
        [48 16.863 8 1.54]; ...    % edP1A1 '0'
        [56 16.863 8 1.54]; ...    % edP1A2 '0'
        [88 18.403 20 1.54]];      % btnTranslate
MyUICs = handles.MyUICNames;
n = size(MyUICs,2);
for i = 1:n
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'off');
end % i

MyUICs = MyUICs([1,14,3,12,9,4,20,21,27]);
for i = 1:9
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'on');
    set(eval(['handles.',UIC]),'Position', Pos(i,:));
end % i
set(handles.txt1,'String', 'Atoms');
set(handles.txt2,'String', 'translate in direction of');
set(handles.txt3,'String', 'by');
set(handles.edA1,'String', '0');
set(handles.edVx,'String', '0.0');
set(handles.edP1A1,'String', '0');
set(handles.edP1A2,'String', '0');

% --- Executes on button press in btnTranslate.
function btnTranslate_Callback(hObject, eventdata, handles)
% translate atoms
global Type InfTable AtomTypes AtomCoords Bonds 

distance = str2double(get(handles.edVx,'String'));
numVt = get(handles.lstVect1,'Value');
if numVt < 7
    rt = handles.Vectors(numVt,:);
else
    Atom1 = str2double(get(handles.edP1A1,'String'));
    Atom2 = str2double(get(handles.edP1A2,'String'));
    rt = atom_pair_vector(Atom1, Atom2);
end % if

rt = rt*distance; 

str = get(handles.edA1,'String');
eval(['ind = [',str,'];']);
for i = ind        % ind is [1,2,3, ...] 
    AtomCoords(i,:)=AtomCoords(i,:)+rt;
end % i

% --------------------------------------------------------------------
function mnuTurnAtomGroup_Callback(hObject, eventdata, handles)
Pos = [ ...
        [4  18.403 16 1.617]; ...    % txt1 'Group of atoms'
        [32 18.403 12 1.617]; ...    % txt2 'turn around'
        [64 19.943 24 1.617]; ...   % txt3 'Turn Angle grad min sec' 
        [44 18.403 18 1.617]; ...    % lstVect1 ''
        [64 18.403 8 1.617]; ...    % edA1 '0'
        [72 18.403 8 1.617]; ...    % edA2 '0'
        [80 18.403 8 1.617]; ...    % edA3 '0'
        [20 18.403 12 1.617]; ...    % edA4 '0'
        [44 16.863 8 1.617]; ...    % edP1A1 '0'
        [52 16.863 8 1.617]; ...    % edP1A2 '0'
        [64 16.863 16 1.617]; ...    % chClock
        [92 18.403 16 1.617]];       % btnTurnAddAtom ''

MyUICs = handles.MyUICNames;
n = size(MyUICs,2);
for i = 1:n
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'off');
end % i

MyUICs = MyUICs([1,3,9,12,14:17,20,21,24,28]);
for i = 1:12
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'on');
    set(eval(['handles.',UIC]),'Position', Pos(i,:));
end % i
set(handles.txt1,'String', 'Group of atoms');
set(handles.txt2,'String', 'turn around');
set(handles.txt3,'String', 'Turn Angle grad min sec');
set(handles.edA1,'String', '0');
set(handles.edA2,'String', '0');
set(handles.edA3,'String', '0');
set(handles.edA4,'String', '0');
set(handles.edP1A1,'String', '0');
set(handles.edP1A2,'String', '0');
set(handles.edP1A1,'Visible', 'off');
set(handles.edP1A2,'Visible', 'off');
set(handles.lstVect1,'Value', 1);

% --- Executes on button press in btnTurnGroup.
function btnTurnGroup_Callback(hObject, eventdata, handles)
global Type InfTable AtomTypes AtomCoords Bonds 

% Axis vector !! must be an unit vector
numVa = get(handles.lstVect1,'Value');
if numVa < 7
    ax = handles.Vectors(numVa,:);
else
    Atom1 = str2double(get(handles.edP1A1,'String'));
    Atom2 = str2double(get(handles.edP1A2,'String'));
    ax = atom_pair_vector(Atom1, Atom2);
end % if

% Turn angle is unsigned grad, min and sec
angle = [str2double(get(handles.edA1,'String')), ...
         str2double(get(handles.edA2,'String')), ...
         str2double(get(handles.edA3,'String'))];
s = 1;  % Sign of a Turn angle
if get(handles.chClock,'Value');s = -1;end % if 
phirad = s*pi*(angle(1) + angle(2)/60 + angle(3)/3600)/180;
M = matrturn(ax, phirad);

str = get(handles.edA4,'String');
eval(['ind = [',str,'];']);
for i = ind        % ind is [1,2,3, ...] 
    vector_new = M*AtomCoords(i,:)';
    AtomCoords(i,:) = vector_new';
end % i

% --------------------------------------------------------------------
function mnuAngles_Callback(hObject, eventdata, handles)

% --------------------------------------------------------------------
function mnuAutoAlpha_Callback(hObject, eventdata, handles)
global Type InfTable AtomTypes AtomCoords Bonds
global Alphas Gammas Ros His

nAtoms = InfTable(1);
nBonds = InfTable(2);
nAlphas = InfTable(3);
if nAlphas > 0
    Alphas = [];
    nAlphas = 0;
end % if
Atoms102 = zeros(1,3);
for iq = 1:(nBonds-1)
    Atom1 = Bonds(iq,1);
    Atom2 = Bonds(iq,2);
    eiq = atom_pair_vector(Atom1, Atom2);
    for jq = (iq+1):nBonds
        Atom3 = Bonds(jq,1);
        Atom4 = Bonds(jq,2);
        if Atom1 == Atom3 || Atom1 == Atom4 || ...
           Atom2 == Atom3 || Atom2 == Atom4 % It is angle
            ejq = atom_pair_vector(Atom3, Atom4);
            if abs(abs(eiq*ejq')-1) > 0.01 % it is not a linear angle
                nAlphas = nAlphas + 1;
                if Atom1 == Atom3
                   Atoms102 = [Atom2, Atom1, Atom4]; 
                end % if
                if Atom1 == Atom4
                   Atoms102 = [Atom2, Atom4, Atom3]; 
                end % if
                if Atom2 == Atom4
                   Atoms102 = [Atom1, Atom4, Atom3]; 
                end % if
                if Atom2 == Atom3
                   Atoms102 = [Atom1, Atom2, Atom4]; 
                end % if
                Alphas = [Alphas; Atoms102];
            end % if it is not a linear angle 
        end % if It is angle    
    end % jq
end % iq
updateInfTable(handles);
% --------------------------------------------------------------------
function mnuGammas_Callback(hObject, eventdata, handles)
Pos = [ ...
        [4 18.403 24 1.617]; ...    % txt1 'Atoms'
        [36 19.943 8 1.617]; ...    % txt2 'Centr'
        [28 18.403 8 1.617]; ...     % edA1 ''
        [36 18.403 8 1.617]; ...     % edA2 ''
        [44 18.403 8 1.617]; ...     % edA3 ''
        [56 18.403 20 1.617]; ...    % txt3 'Bend direction'
        [76 18.403 18 1.617]; ...    % lstVect1 ''
        [76 16.863 8 1.617]; ...    % edP1A1 '0'
        [84 16.863 8 1.617]; ...    % edP1A2 '0'
        [96 18.403 14 1.617]];      % btnTranslate
MyUICs = handles.MyUICNames;
n = size(MyUICs,2);
for i = 1:n
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'off');
end % i

MyUICs = MyUICs([1,3,14:16,9,12,20,21,29]);
for i = 1:10
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'on');
    set(eval(['handles.',UIC]),'Position', Pos(i,:));
end % i
set(handles.txt1,'String', 'Atoms to form Gamma');
set(handles.txt2,'String', 'Centr');
set(handles.txt3,'String', 'Bend direction');
set(handles.edA1,'String', '0');
set(handles.edA2,'String', '0');
set(handles.edA3,'String', '0');
set(handles.edP1A1,'String', '0');
set(handles.edP1A2,'String', '0');
set(handles.edP1A1,'Visible', 'off');
set(handles.edP1A2,'Visible', 'off');
set(handles.lstVect1,'Value', 1);

% --- Executes on button press in btnAddGamma.
function btnAddGamma_Callback(hObject, eventdata, handles)
global Gammas

RowGamma = zeros(1,6);
numVb = get(handles.lstVect1,'Value');
if  numVb == 7
    Atom1 = str2double(get(handles.edP1A1,'String'));
    Atom2 = str2double(get(handles.edP1A2,'String'));
    RowGamma(5) = Atom1;
    RowGamma(6) = Atom2;
else
    RowGamma(4) = numVb;
end % if
RowGamma(1) = str2double(get(handles.edA1,'String'));
RowGamma(2) = str2double(get(handles.edA2,'String'));
RowGamma(3) = str2double(get(handles.edA3,'String'));
Gammas = [Gammas; RowGamma];
updateInfTable(handles);

% --------------------------------------------------------------------
function mnuRos_Callback(hObject, eventdata, handles)
Pos = [ ...
        [4 16.863 8 1.617]; ...    % txt3 'Atom'
        [20 16.863 48 1.617]; ...    % txt4 'is binded to central atom of  Plane with atoms '
        [76 18.403 8 1.617]; ...    % txt5 'Centr'
        [68 16.863 8 1.617]; ...    % edA1 '0'
        [76 16.863 8 1.617]; ...    % edA2 '0'
        [84 16.863 8 1.617]; ...     % edA3 '0'
        [12 16.863 8 1.617]; ...     % edA4 '0'
        [96 16.863 14 1.617]];      % btnAddRo
MyUICs = handles.MyUICNames;
n = size(MyUICs,2);
for i = 1:n
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'off');
end % i

MyUICs = MyUICs([9:11,14:17,31]);
for i = 1:8
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'on');
    set(eval(['handles.',UIC]),'Position', Pos(i,:));
end % i
set(handles.txt3,'String', 'Atom');
set(handles.txt4,'String', ...
                'is binded to central atom of Plane with atoms');
set(handles.txt5,'String', 'Centre');
set(handles.edA1,'String', '0');
set(handles.edA2,'String', '0');
set(handles.edA3,'String', '0');
set(handles.edA4,'String', '0');

% --- Executes on button press in btnAddRo.
function btnAddRo_Callback(hObject, eventdata, handles)
global InfTable AtomCoords Ros
RowRo = zeros(1,4);
RowRo(1) = str2double(get(handles.edA1,'String'));
RowRo(2) = str2double(get(handles.edA2,'String'));
RowRo(3) = str2double(get(handles.edA3,'String'));
RowRo(4) = str2double(get(handles.edA4,'String'));
Ros = [Ros; RowRo];

updateInfTable(handles);

% --------------------------------------------------------------------
function mnuHis_Callback(hObject, eventdata, handles)
Pos = [ ...
        [52 18.403 8 1.617]; ...    % edVx ' '
        [60 18.403 8 1.617]; ...    % edVy ' '
        [68 18.403 8 1.617]; ...    % edVz ' '
        [4 19.943 48 1.617]; ...    % txt4 'Plane 1 with atoms...      turns around      agaist'
        [52 19.943 40 1.617]; ...    % txt5 'Plane 2 with atoms...    turting  around'
        [28 18.403 18 1.617]; ...    % lstVect1 ''
        [76 18.403 18 1.617]; ...    % lstVect2 ''
        [4 18.403 8 1.617]; ...     % edA1 ''
        [12 18.403 8 1.617]; ...     % edA2 ''
        [20 18.403 8 1.617]; ...     % edA3 ''
        [28 16.863 8 1.617]; ...    % edP1A1 '0'
        [36 16.863 8 1.617]; ...    % edP1A2 '0'
        [76 16.863 8 1.617]; ...    % edP2A1 '0'
        [84 16.863 8 1.617]; ...    % edP2A2 '0'
        [96 18.403 12 1.617]];      % btnAddHi
MyUICs = handles.MyUICNames;
n = size(MyUICs,2);
for i = 1:n
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'off');
end % i

MyUICs = MyUICs([4:6,10:16,20:23,30]);
for i = 1:15
    UIC = MyUICs{i};
    set(eval(['handles.',UIC]),'Visible', 'on');
    set(eval(['handles.',UIC]),'Position', Pos(i,:));
end % i
set(handles.txt4,'String', ...
                'Plane 1 with atoms...      turns around      agaist');
set(handles.txt5,'String', ...
                'Plane 2 with atoms...    turting  around');
set(handles.txt3,'String', 'Bend direction');
set(handles.edA1,'String', '0');
set(handles.edA2,'String', '0');
set(handles.edA3,'String', '0');
set(handles.edVx,'String', '0');
set(handles.edVy,'String', '0');
set(handles.edVz,'String', '0');
set(handles.edP1A1,'String', '0');
set(handles.edP1A2,'String', '0');
set(handles.edP2A1,'String', '0');
set(handles.edP2A2,'String', '0');
set(handles.edP1A1,'Visible', 'off');
set(handles.edP1A2,'Visible', 'off');
set(handles.lstVect1,'Value', 1);
set(handles.edP2A1,'Visible', 'off');
set(handles.edP2A2,'Visible', 'off');
set(handles.lstVect2,'Value', 1);

% --- Executes on button press in btnAddHi.
function btnAddHi_Callback(hObject, eventdata, handles)
global InfTable AtomCoords His

nHis = InfTable(6);
RowHi = zeros(1,12);
nHis = nHis + 1;
% Plane 1
RowHi(1) = str2double(get(handles.edA1,'String'));
RowHi(2) = str2double(get(handles.edA2,'String'));
RowHi(3) = str2double(get(handles.edA3,'String'));
numVh1 = get(handles.lstVect1,'Value');
if  numVh1 == 7
    RowHi(5) = str2double(get(handles.edP1A1,'String'));
    RowHi(6) = str2double(get(handles.edP1A2,'String'));
else
    RowHi(4) = numVh1;
end % if
% Plane 2
RowHi(1+6) = str2double(get(handles.edVx,'String'));
RowHi(2+6) = str2double(get(handles.edVy,'String'));
RowHi(3+6) = str2double(get(handles.edVz,'String'));
numVh2 = get(handles.lstVect2,'Value');
if  numVh2 == 7
    RowHi(5+6) = str2double(get(handles.edP2A1,'String'));
    RowHi(6+6) = str2double(get(handles.edP2A2,'String'));
else
    RowHi(4+6) = numVh2;
end % if
His = [His; RowHi];
updateInfTable(handles);

% --- Executes on button press in rbtnPlane.
function rbtnPlane_Callback(hObject, eventdata, handles)
set(handles.rbtnNone,'Value', 0);
set(handles.rbtnAxis,'Value', 0);
set(handles.rbtnCentrum,'Value', 0);
set(handles.btnApplySymm,'Enable', 'on');


% --- Executes on button press in rbtnAxis.
function rbtnAxis_Callback(hObject, eventdata, handles)
set(handles.rbtnNone,'Value', 0);
set(handles.rbtnPlane,'Value', 0);
set(handles.rbtnCentrum,'Value', 0);
set(handles.btnApplySymm,'Enable', 'on');

% --- Executes on button press in rbtnCentrum.
function rbtnCentrum_Callback(hObject, eventdata, handles)
set(handles.rbtnNone,'Value', 0);
set(handles.rbtnPlane,'Value', 0);
set(handles.rbtnAxis,'Value', 0);
set(handles.btnApplySymm,'Enable', 'on');

% --- Executes on button press in rbtnNone.
function rbtnNone_Callback(hObject, eventdata, handles)
set(handles.rbtnNone,'Value', 1);
set(handles.rbtnAxis,'Value', 0);
set(handles.rbtnPlane,'Value', 0);
set(handles.rbtnCentrum,'Value', 0);
set(handles.btnApplySymm,'Enable', 'off');

% --- Executes on button press in btnApplySymm.
function btnApplySymm_Callback(hObject, eventdata, handles)
global InfTable AtomTypes AtomCoords Bonds 

nAtoms = InfTable(1);
nAtomsNew = nAtoms;
nBonds = InfTable(2);
nBondsNew = nBonds;
nTurns = 1;
AtomsTable = zeros(nAtoms,nTurns);
    
if get(handles.rbtnPlane,'Value')
    Normal = [str2double(get(handles.edPlanex,'String')), ...
          str2double(get(handles.edPlaney,'String')), ...
          str2double(get(handles.edPlanez,'String'))];
    % for atoms
    for i=1:nAtoms
        r = AtomCoords(i,:);
        rnew = r -2*(Normal*r')*Normal;
        % Check coincedence
        coincide = 0;
            for j = 1:nAtomsNew
                if abs(norm(rnew - AtomCoords(j,:))) < 1.0e-3
                    coincide = 1;
                    AtomsTable(i) = j;
                    break
                end % if coincide
            end % j
        % End check
        if coincide == 0
            nAtomsNew = nAtomsNew + 1;
            AtomCoords = [AtomCoords; rnew];
            AtomTypes = [AtomTypes; AtomTypes(i)];
            AtomsTable(i) = nAtomsNew;
        end % if coincide == 0    
    end % i Atoms
end % if Plane

if get(handles.rbtnAxis,'Value')
    sym_ax = [str2double(get(handles.edAxisx,'String')), ...
          str2double(get(handles.edAxisy,'String')), ...
          str2double(get(handles.edAxisz,'String'))];
    ax_order = str2double(get(handles.edOrder,'String'));
    nTurns = ax_order-1;
    M = zeros(3,3,nTurns);
    for iTurn = 1:nTurns
        phirad = iTurn*2*pi/ax_order;
        M(:,:,iTurn) = matrturn(sym_ax, phirad);
    end % iTurn
    % for atoms
    AtomsTable = zeros(nAtoms,nTurns);
    for i=1:nAtoms
        r = AtomCoords(i,:);
        for iTurn = 1:nTurns
            rnew = M(:,:,iTurn)*r';
            rnew = rnew';
            % Check coincedence 
            coincide = 0;
                for j = 1:nAtomsNew
                    if abs(norm(rnew - AtomCoords(j,:))) < 1.0e-3
                        coincide = 1;
                        AtomsTable(i,iTurn) = j;
                        break
                    end % if coincide
                end % j
            % End check
            if coincide == 0
                nAtomsNew = nAtomsNew + 1;
                AtomCoords = [AtomCoords; rnew];
                AtomTypes = [AtomTypes; AtomTypes(i)];
                AtomsTable(i,iTurn) = nAtomsNew;
            end % if coincide == 0
        end % iTurn
    end % i Atoms
end % if Axis

if get(handles.rbtnCentrum,'Value')
    % for atoms
    for i=1:nAtoms
        r = AtomCoords(i,:);
        rnew = -r; 
        % Check coincedence exept
        coincide = 0;
            for j = 1:nAtomsNew
                if abs(norm(rnew - AtomCoords(j,:))) < 1.0e-3
                    coincide = 1;
                    AtomsTable(i) = j;
                    break
                end % if coincide
            end % j
        % End check
        if coincide == 0
            nAtomsNew = nAtomsNew + 1;
            AtomCoords = [AtomCoords; rnew];
            AtomTypes = [AtomTypes; AtomTypes(i)];
            AtomsTable(i) = nAtomsNew;
        end % if coincide == 0    
    end % i Atoms
end % if Centrum

% for bonds
for i=1:nBonds
    for iTurn = 1:nTurns
        Atom1 = AtomsTable(Bonds(i,1),iTurn);
        Atom2 = AtomsTable(Bonds(i,2),iTurn);
        % is there such a bond?
        coincide = 0;
        for j = 1:nBondsNew
            if (Atom1 == Bonds(j,1)) && (Atom2 == Bonds(j,2))|| ...
               (Atom2 == Bonds(j,1)) && (Atom1 == Bonds(j,2))     
                coincide = 1;
                break
            end % if
        end  % j
            if coincide == 0
                nBondsNew = nBondsNew + 1;
                Bonds = [Bonds; [Atom1, Atom2]];
            end % if coincide == 0
    end % iTurn
end % i Bonds

updateInfTable(handles);
set(handles.rbtnNone,'Value', 1);
set(handles.rbtnAxis,'Value', 0);
set(handles.rbtnPlane,'Value', 0);
set(handles.rbtnCentrum,'Value', 0);
set(handles.btnApplySymm,'Enable', 'off');

% End Callbacks ============================================

% My sub-functions ==============================================

function [r,varargout] = atom_pair_vector(Atom1, Atom2)
% construct unit vector from Atom1 to Atom2 using AtomCoords
global AtomCoords

r = AtomCoords(Atom2,:) - AtomCoords(Atom1,:);
s = norm(r);
r = r/s;
if nargout > 0, varargout{1} = s; end % if

function M = matrturn(ax, phirad)
% M is result - 3x3-matrix of rotation
% ax is unit vector of axis of turn
% phirad is signed angle given radians
if abs(norm(ax) - 1) > 1e-3
    error('axis must be of unit size')
end % if
cs = cos(phirad);
sn = sin(phirad);
M = zeros(3);
M(1,1) = ax(1)^2*(1 - cs) + cs;
M(1,2) = ax(1)*ax(2)*(1 - cs) - ax(3)*sn;
M(1,3) = ax(1)*ax(3)*(1 - cs) + ax(2)*sn; 
M(2,1) = ax(2)*ax(1)*(1 - cs) + ax(3)*sn;
M(2,2) = ax(2)^2*(1 - cs) + cs;
M(2,3) = ax(2)*ax(3)*(1 - cs) - ax(1)*sn;
M(3,1) = ax(3)*ax(1)*(1 - cs) - ax(2)*sn;
M(3,2) = ax(3)*ax(2)*(1 - cs) + ax(1)*sn;
M(3,3) = ax(3)^2*(1 - cs) + cs;

function updateInfTable(handles)
global Type InfTable AtomTypes AtomCoords Bonds
global Alphas Gammas Ros His
nAtoms = size(AtomCoords,1); 
InfTable(1) = nAtoms;
set(handles.ednAtoms, 'String', int2str(nAtoms));
nBonds = size(Bonds,1);
InfTable(2) = nBonds;
set(handles.ednBonds, 'String', int2str(nBonds));
nAlphas = size(Alphas,1);
InfTable(3) = nAlphas;
set(handles.ednAlphas, 'String', int2str(nAlphas));
nGammas = size(Gammas,1);
InfTable(4) = nGammas;
set(handles.ednGammas, 'String', int2str(nGammas));
nRos = size(Ros,1);
InfTable(5) = nRos;
set(handles.ednRos, 'String', int2str(nRos));
nHis = size(His,1);
InfTable(6) = nHis;
set(handles.ednHis, 'String', int2str(nHis));
nVibrCoords = sum(InfTable(2:6));
InfTable(7) = nVibrCoords;
set(handles.ednVibrCoords, 'String', int2str(nVibrCoords));
if nBonds > 0 
    set(handles.mnuToCalculi, 'Enable', 'on');
end % if

function View_selection(handles)
global Type InfTable AtomTypes AtomCoords Bonds 
global Alphas Gammas Ros His
figure(handles.Hf);
clf
Coords = zeros(2,3);
nBonds = InfTable(2);
for iBond = 1:nBonds
    a1 = Bonds(iBond,1);
    a2 = Bonds(iBond,2);
    Coords(1,:) = AtomCoords(a1,:);
    Coords(2,:) = AtomCoords(a2,:);
    plot3(Coords(1:2,1),Coords(1:2,2),Coords(1:2,3),'b-o','MarkerSize',12)
    hold on
end % iBond
xlabel('x')
ylabel('y')
zlabel('z')
grid on

nAtoms = InfTable(1);
hold on
for i = 1:nAtoms
    % is isolated?
    isolated = 1;
    for iBond = 1:nBonds
        if (iBond == Bonds(iBond,1))||(iBond == Bonds(iBond,2))
            isolated = 0;
            break
        end % if 
    end % iBond
    if isolated
        Coords(1,:) = AtomCoords(i,:);
     plot3(Coords(1,1),Coords(1,2),Coords(1,3),'bo','MarkerSize',12)
    end % if
    num = [Type(AtomTypes(i)).Symbol,int2str(i)];
    r = AtomCoords(i,:);
    text('String',num, 'Position',r);
end % i Atoms

iQ = get(handles.lstQs, 'Value');
switch handles.TypeQ
    case 'q'
        a1 = Bonds(iQ,1);
        a2 = Bonds(iQ,2);
        n = 2;
        Coords(1,:) = AtomCoords(a1,:);
        Coords(2,:) = AtomCoords(a2,:);
        plot3(Coords(1:n,1),Coords(1:n,2),Coords(1:n,3),'r-o','MarkerSize',12)
    case 'a'
        a1 = Alphas(iQ,1);
        a2 = Alphas(iQ,2);
        a3 = Alphas(iQ,3);
        n = 3;
        Coords(1,:) = AtomCoords(a1,:);
        Coords(2,:) = AtomCoords(a2,:);
        Coords(3,:) = AtomCoords(a3,:);
        plot3(Coords(1:n,1),Coords(1:n,2),Coords(1:n,3),'r-o','MarkerSize',12)
    case 'g'
        a1 = Gammas(iQ,1);
        a2 = Gammas(iQ,2);
        a3 = Gammas(iQ,3);
        n = 3;
        Coords(1,:) = AtomCoords(a1,:);
        Coords(2,:) = AtomCoords(a2,:);
        Coords(3,:) = AtomCoords(a3,:);
        plot3(Coords(1:n,1),Coords(1:n,2),Coords(1:n,3),'r-o','MarkerSize',12)
        if Gammas(iQ,4)
            r = handles.Vectors(Gammas(iQ,4),:);
        else
            e1 = atom_pair_vector(a2, a1);
            eaux = atom_pair_vector(Gammas(iQ,5), Gammas(iQ,6)); 
            normal = cross(e1, eaux);
            normal = normal/norm(normal);
            r = cross(normal, e1);
        end % if
        Coords(1,:) = AtomCoords(a2,:);
        Coords(2,:) = AtomCoords(a2,:) + r;
        plot3(Coords(1:2,1),Coords(1:2,2),Coords(1:2,3),'g-')
    case 'r'
        a1 = Ros(iQ,1);
        a2 = Ros(iQ,2);
        a3 = Ros(iQ,3);
        a4 = Ros(iQ,4);
        n = 3;
        Coords(1,:) = AtomCoords(a1,:);
        Coords(2,:) = AtomCoords(a2,:);
        Coords(3,:) = AtomCoords(a3,:);
        plot3(Coords(1:n,1),Coords(1:n,2),Coords(1:n,3),'r-o','MarkerSize',12)
        Coords(1,:) = AtomCoords(a2,:);
        Coords(2,:) = AtomCoords(a4,:);
        plot3(Coords(1:2,1),Coords(1:2,2),Coords(1:2,3),'r-o','MarkerSize',12)
        Coords(1,:) = AtomCoords(a4,:);
        normal = cross((AtomCoords(a1,:)-AtomCoords(a2,:)), ...
            (AtomCoords(a3,:)-AtomCoords(a2,:)));
        normal = normal/norm(normal);
        Coords(2,:) = AtomCoords(a4,:) + normal;
        plot3(Coords(1:2,1),Coords(1:2,2),Coords(1:2,3),'g-')
    case 'h'
        a1 = His(iQ,1);
        a2 = His(iQ,2);
        a3 = His(iQ,3);
        Coords(1,:) = AtomCoords(a1,:);
        Coords(2,:) = AtomCoords(a2,:);
        Coords(3,:) = AtomCoords(a3,:);
        n = 3;
        plot3(Coords(1:n,1),Coords(1:n,2),Coords(1:n,3),'r-o','MarkerSize',12)
        if His(iQ,4)
           ax = handles.Vectors(His(iQ,4),:);
        else
           ax = atom_pair_vector(His(iQ,5), His(iQ,6)); 
        end % if
        normal = cross(ax, atom_pair_vector(a2, a1));
        if norm(normal) > 0.05
            Coords(1,:) = AtomCoords(a1,:);
            Coords(2,:) = Coords(1,:) + normal;
            plot3(Coords(1:2,1),Coords(1:2,2),Coords(1:2,3),'g-')
        end % if    
        normal = cross(ax, atom_pair_vector(a2, a3));
        if norm(normal) > 0.05
            Coords(1,:) = AtomCoords(a3,:);
            Coords(2,:) = Coords(1,:) + normal;
            plot3(Coords(1:2,1),Coords(1:2,2),Coords(1:2,3),'g-')
        end % if    
        a4 = His(iQ,7);
        a5 = His(iQ,8);
        a6 = His(iQ,9);
        Coords(1,:) = AtomCoords(a4,:);
        Coords(2,:) = AtomCoords(a5,:);
        Coords(3,:) = AtomCoords(a6,:);
        n = 3;
        plot3(Coords(1:n,1),Coords(1:n,2),Coords(1:n,3),'r-o','MarkerSize',12)
        if His(iQ,10)
           ax = handles.Vectors(His(iQ,10),:);
        else
           ax = atom_pair_vector(His(iQ,11), His(iQ,12)); 
        end % if
        normal = cross(ax, atom_pair_vector(a5, a4));
        if norm(normal) > 0.05
            Coords(1,:) = AtomCoords(a4,:);
            Coords(2,:) = Coords(1,:) + normal;
            plot3(Coords(1:2,1),Coords(1:2,2),Coords(1:2,3),'g-')
        end % if    
        normal = cross(ax, atom_pair_vector(a5, a6));
        if norm(normal) > 0.05
            Coords(1,:) = AtomCoords(a6,:);
            Coords(2,:) = Coords(1,:) + normal;
            plot3(Coords(1:2,1),Coords(1:2,2),Coords(1:2,3),'g-')
        end % if    
end % switch

function set_view_on(Q,handles)  %A,
handles.TypeQ = Q;
guidata(gcbo, handles);
set(handles.btnCloseView, 'Enable', 'on');
set(handles.btnViewBonds, 'Enable', 'off');
set(handles.btnViewA, 'Enable', 'off');
set(handles.btnViewG, 'Enable', 'off');
set(handles.btnViewRo, 'Enable', 'off');
set(handles.btnViewHi, 'Enable', 'off');

function update_ini(pname)
diskname = pname(1:2);
filename = strcat(diskname,'\LEVINFINITE\Config\ToCalculate.ini');
s = strcat(diskname,'\LEVINFINITE\MOLECULES\');
ns = length(s);
npn = length(pname);
molname = pname((ns+1):(npn-1));
fid = fopen(filename,'rt+');
fprintf(fid,'%s\n ',molname);
fclose(fid);

% End My functions





