function [gcoord,gcoord_index,quad4,tria3,Cbar,Off_beam,Pbarl,coordR,shell,shell_index,Pbarl_index,bc,mat,mat_index]=readbdf_new(bdffile)


% gcoord = zeros(1000,4);
coordR = zeros(10,10);

fid = fopen(bdffile,'r');

tria3=[];
quad4=[];
Cbar=[];
Pbarl={};
Pbarl_index=[];
Off_beam=[];
shell=[];
shell_index=[];
mat=[];
mat_index=[];
% i = 1;
% in = 1;
ic = 1;
igr=1;
ict=1;
icq=1;
ish=1;
ibc=1;
ict=1;
ipb=1;
ibe=1;
imt=1;
% it=1;
while feof(fid) == 0
    tline = fgetl(fid);
    entype = strtok(tline);
    
    switch entype

        case 'MAT1'
            mat_temp=str2num(tline(9:end));
            mat(imt,1:length(mat_temp))=mat_temp(1:end);
            mat_index(mat(imt,1),1)=imt;
            imt=imt+1;
        
        case 'PSHELL'
            shell_temp=str2num(tline(9:33));
            shell(ish,1:length(shell_temp))=shell_temp(1:end);
            shell_index(shell(ish,1),1)=ish;
            ish=ish+1;
            
        case 'PCOMP'
            if contains(tline,'SYM')
                factor=2;
            else
                factor=1;
            end
            shell(ish,:) = [str2num(tline(9:16)) 0 0 0 0 0];
            tline = fgetl(fid);
            while ~isempty(str2num(tline(9:33)))
                if length(tline)>=41
                shell_temp=[str2num(tline(9:33)) str2num(tline(41:64))];
                shell(ish,2)= shell_temp(1);
                shell(ish,3)= shell(ish,3)+shell_temp(2)+shell_temp(5);
                else
                shell_temp=str2num(tline(9:33));
                shell(ish,3)= shell(ish,3)+shell_temp(2);
                end
                tline = fgetl(fid);
            end
            shell(ish,3)=shell(ish,3)*factor;
            shell_index(shell(ish,1),1)=ish;
            ish=ish+1;
            
        case 'CQUAD4'
              temp1 = str2num(tline(9:end)); %#ok<*ST2NM>
              if length(temp1)==6 
                  quad4(icq,:)=[temp1 -temp1(1) 0];
              end
              if length(temp1)==7
                  if length(tline)>64 & isempty(str2num(tline(57:64)))
                  quad4(icq,:)=[temp1(1:6) -temp1(1) temp1(7)];
                  else
                  quad4(icq,:)=[temp1(1:7) 0];    
                  end
              end
              if length(temp1)==8
              quad4(icq,:) = temp1;
              end
              icq=icq+1;  
            
        case 'CTRIA3'
            temp2 = str2num(tline(9:end)); %#ok<*ST2NM>
            if length(temp2)==5 
                  tria3(ict,:)=[temp2 -temp2(1) 0];
            end 
            if length(temp2)==6
                  if isempty(str2num(tline(49:56)))
                  tria3(ict,:)=[temp2(1:5) -temp2(1) temp2(6)];
                  else
                  tria3(ict,:)=[temp2(1:6) 0];    
                  end
            end
            if length(temp2)==7
              tria3(ict,:) = temp2;
            end
            ict=ict+1;
            
            case 'CBAR'
              Cbar(ibe,:) = [str2num(tline(9:16)) str2num(tline(17:24)) str2num(tline(25:32)) str2num(tline(33:40)) str2num(tline(41:48)) str2num(tline(49:56)) str2num(tline(57:end))];
              position=ftell(fid);
              tline = fgetl(fid);
              if tline(1:4)~='CBAR' & ~isempty(str2num(tline(end-1)))
                 Off_beam(ibe,:)=[str2num(tline(25:32)) str2num(tline(33:40)) str2num(tline(41:48)) str2num(tline(49:56)) str2num(tline(57:64)) str2num(tline(65:end))];             
              else
                 Off_beam(ibe,:)=zeros(1,6);
              end
              fseek(fid,position,'bof');
              ibe=ibe+1;
            case 'CBEAM'
              Cbar(ibe,:) = [str2num(tline(9:16)) str2num(tline(17:24)) str2num(tline(25:32)) str2num(tline(33:40)) str2num(tline(41:48)) str2num(tline(49:56)) str2num(tline(57:end))];
              position=ftell(fid);
              tline = fgetl(fid);
              Off_beam(ibe,:)=zeros(1,6);
              if tline(1:5)~='CBEAM' & ~isempty(str2num(tline(end-1)))
                Off_beam(ibe,:)=[str2num(tline(25:32)) str2num(tline(33:40)) str2num(tline(41:48)) str2num(tline(49:56)) str2num(tline(57:64)) str2num(tline(65:end))];
              else
                 Off_beam(ibe,:)=zeros(1,6);
              end
              fseek(fid,position,'bof');
              ibe=ibe+1;  
              
        case 'PBARL' 
             index=str2num(tline(9:16));
             Pbarl{ipb,1} = [index str2num(tline(9:16)) str2num(tline(17:24))];
             Pbarl{ipb,2}=strtrim(tline(33:end));
             tline = fgetl(fid);
             Pbarl{ipb,3} = str2num(tline);
             position=ftell(fid);
             tline = fgetl(fid);
             if ~isempty(str2num(tline))
                while ~isempty(str2num(tline))
                    Pbarl{ipb,3} = [Pbarl{ipb,3} str2num(tline)];
                    tline = fgetl(fid);
                end
             end
             fseek(fid,position,'bof');
             Pbarl_index(Pbarl{ipb}(1),1)=ipb;
             ipb=ipb+1;
        case 'PBEAML'
             index=str2num(tline(9:16));
             Pbarl{ipb,1} = [index str2num(tline(9:16)) str2num(tline(17:24))];
             Pbarl{ipb,2}=strtrim(tline(33:end));
             tline = fgetl(fid);
             Pbarl{ipb,3} = str2num(tline);
             position=ftell(fid);
             tline = fgetl(fid);
             if ~isempty(str2num(tline))
                while ~isempty(str2num(tline))
                    Pbarl{ipb,3} = [Pbarl{ipb,3} str2num(tline)];
                    tline = fgetl(fid);
                end
             end
             fseek(fid,position,'bof');
             Pbarl_index(Pbarl{ipb}(1),1)=ipb;
             ipb=ipb+1;
             
        case 'GRID'
            exp1=strfind((tline(25:32)),'-');
            exp2=strfind((tline(33:40)),'-');
            exp3=strfind((tline(41:end)),'-');
            exp1=exp1(exp1>1);
            exp2=exp2(exp2>1);
            exp3=exp3(exp3>1);
            if exp1>1
                tline=[tline(1:22+exp1) 'E' tline(23+exp1+1:end)];
            end 
            if exp2>1
                tline=[tline(1:30+exp2) 'E' tline(31+exp2+1:end)];
            end
             if exp3>1

                tline=[tline(1:38+exp3) 'E' tline(39+exp3+1:end)];
            end
            temp3 = [str2num(tline(9:16)) str2num(tline(25:32)) str2num(tline(33:40)) str2num(tline(41:end))];
            gcoord(igr,1:length(temp3)) = temp3;
            gcoord_index(gcoord(igr,1),1)=igr;
            igr=igr+1;
            
        case 'GRID*'
            one=tline(9:24);
            two=tline(40:56);
            three=tline(57:end);
            m_pos=find(two=='-');
            if ~isempty(m_pos)
                for sear=1:length(m_pos)
                    if m_pos(sear)>1 & two(m_pos(sear)-1)~=' '
                        two=[two(1:m_pos(sear)-1) 'E' two(m_pos(sear):end)];
                        m_pos=find(two=='-');
                    end
                end
            end
            m_pos=find(three=='-');
            if ~isempty(m_pos)
                for sear=1:length(m_pos)
                    if m_pos(sear)>1 & three(m_pos(sear)-1)~=' '
                        three=[three(1:m_pos(sear)-1) 'E' three(m_pos(sear):end)];
                        m_pos=find(three=='-');
                    end
                end
            end
            temp3 = [str2num(one) str2num(two) str2num(three)];
            tline = fgetl(fid);
            four=tline(9:end);
            m_pos=find(four=='-');
            if ~isempty(m_pos)
                for sear=1:length(m_pos)
                    
                    if m_pos(sear)>1 & four(m_pos(sear)-1)~=' '
                        four=[four(1:m_pos(sear)-1) 'E' four(m_pos(sear):end)];
                        m_pos=find(four=='-');
                    end
                end
            end
            temp3 = [temp3 str2num(four)];
            gcoord(igr,1:length(temp3)) = temp3;
            gcoord_index(gcoord(igr,1),1)=igr;
            igr=igr+1;
            
        case 'CORD2R'
            coordR(ic,1:7)=[str2num(tline(9:16)) str2num(tline(25:32)) str2num(tline(33:40)) str2num(tline(41:48)) str2num(tline(49:56)) str2num(tline(57:64)) str2num(tline(65:end))];
            tline = fgetl(fid);
            if tline(1)=='*'
                coordR(ic,8:10)=[str2num(tline(9:24)) str2num(tline(25:40)) str2num(tline(41:end))]; 
            else
                coordR(ic,8:10)=[str2num(tline(9:16)) str2num(tline(17:24)) str2num(tline(25:end))]; 
            end
            ic=ic+1;
            
        case 'SPC1'
            bc{ibc}=str2num(tline(17:end));
            if isempty(bc{ibc})
                bc{ibc}=str2num(tline(17:24));
                bc{ibc}(end+1:end+length(str2num(tline(25:32)):str2num(tline(41:end))))=str2num(tline(25:32)):str2num(tline(41:end));
            end
            position=ftell(fid);
            test=fgetl(fid);
            while ~isempty(str2num(test))
            bc{ibc}(end+1:end+length(str2num(test)))= str2num(test);
            test=fgetl(fid);
            end
    
            fseek(fid,position,'bof');    
           
            ibc=ibc+1;
            
    end
   
end

fclose(fid);

quad4=sortrows(quad4);
tria3=sortrows(tria3);
% gcoord(in:end,:) = [];
coordR(ic:end,:) = [];

end
