function  bcstruct=GetSurfBCs(fighandle)
% BCSTRUCT = GetSurfBCs(FIGHANDLE)
%
% This function accepts the handle FIGHANDLE to the main GUI figure and
% returns the structure BCSTRUCT with the boundary conditions. It draws the 
% surface of the mesh in the main figure axes and numbers the edges, then calls a
% dialogue SurfBCDialogue which cycles through the edges and prompts for the
% boundary conditions.

% get handles from main GUI
handles=guidata(fighandle);

% plot the edges in the axes along with their edge numbers
axes(handles.axes1);
cla reset

edges=handles.mesh.edges;
xcors=handles.mesh.xcoords;
ycors=handles.mesh.ycoords;
numbounds=max(edges(5,:));

colors={'r','b','k','y','m','g','c'};
hold on
xes=cell(1,numbounds);
yes=cell(1,numbounds);
for i=1:size(edges,2)
    edgnum=edges(5,i);
    x=xcors(edges(1:2,i)); y=ycors(edges(1:2,i));
    line(x,y,'Color',colors{mod(edgnum,length(colors))+1});
    xes{edgnum}=[xes{edgnum} , x];
    yes{edgnum}=[yes{edgnum} , y];
end
for i=1:numbounds
    xavg=median(xes{i}); yavg=median(yes{i});
    text(xavg,yavg,num2str(i),'FontSize',14,'Color','r');
end
hold off
xn=min(xcors); xx=max(xcors); yn=min(ycors); yx=max(ycors);
pad=0.1*max(abs([xn xx yn yx]));
xn=xn-pad; xx=xx+pad; yn=yn-pad; yx=yx+pad;
axis([xn xx yn yx]);

% now open a dialogue for defining the boundary conditions for each
% edge
numnp=handles.mesh.size(2);
np=handles.mesh.connect;
for i=1:numbounds
    [code(i), normbc(i), tangbc(i), bcfighand]=SurfBCDialogue(i);
end

% done with the dialogue. close it and apply the boundary conditions
delete(bcfighand);
[bccode, nangle, boundn, boundt] = assignbcs(numnp,xcors,ycors,np,edges,code,normbc,tangbc);

% form output structure
bcstruct = struct('bccodes', bccode,...
    'bcnangles', nangle,...
    'bcboundts', boundt,...
    'bcboundns', boundn,...
    'codes',code,...
    'normbcs',normbc,...
    'tangbcs',tangbc);
