function [flagclicks, flagproperties, flagimage]=writeShapeFileDBS(filename, DBS, plotHandle)

% Export 3 files:

% (1) click file
% flagclicks=true is succefullly saved
flagclicks=lncWriteClicks([filename,'.txt'], DBS);

% (2) property file (default values used - the CIVAN GUI will update accordingly once saved to the CBS card 
% flagproperties=true is succefullly saved
flagproperties=lncWriteProperties([filename,'.properties'], DBS);

% (3) image file in "jpg" format of the shape
% flagimage=true is succefullly saved
flagimage=lncWriteImage([filename,'.jpg'], plotHandle);

%---------------
function flag=lncWriteClicks(filename, DBS)

flag=true;

% open file
idf=fopen(filename,'w');

if idf==-1
    flag=false;
    return
end

if DBS.Shape.MatrixResolution==1
    MatrixResolution=32;
elseif DBS.Shape.MatrixResolution==2
    MatrixResolution=64;
end

fprintf(idf,'sizeMatrix:%g\n', MatrixResolution); 
for i=1:DBS.Shape.ShapeNumberPoints
    fprintf(idf,'%g,%g\n', DBS.Shape.Clicks(i,1), DBS.Shape.Clicks(i,2)); 
end

% close file
fclose(idf);

%------------
function flag=lncWriteProperties(filename, DBS)

flag=true;

% open file
idf=fopen(filename,'w');

if idf==-1
    flag=false;
    return
end

% if DBS.Shape.MatrixResolution==1
%     MatrixResolution=32;
% elseif DBS.Shape.MatrixResolution==2
%     MatrixResolution=64;
% end

% [a, b]=lncMapPixel2Image(MatrixResolution);

%x
xType=128*ones(1,256);
% for i=1:DBS.Shape.ShapeNumberPoints
%     xType(i)=a*DBS.Shape.Clicks(i,1) + b;
% end

%y
yType=128*ones(1,256);
% for i=1:DBS.Shape.ShapeNumberPoints
%     yType(i)=a*DBS.Shape.Clicks(i,2) + b;
% end

%z
zType=128*ones(1,256);

%shapetype
shapeType=zeros(1,256);

% X_type
fprintf(idf,'X_Type=');
for i=1:256
    if i<256
        fprintf(idf,'%g,', xType(i)); 
    else
        fprintf(idf,'%g\n', xType(i)); 
    end
end

% Y_type
fprintf(idf,'Y_Type=');
for i=1:256
    if i<256
        fprintf(idf,'%g,', yType(i)); 
    else
        fprintf(idf,'%g\n', yType(i)); 
    end
end

% Z_type
fprintf(idf,'Z_Type=');
for i=1:256
    if i<256
        fprintf(idf,'%g,', zType(i)); 
    else
        fprintf(idf,'%g\n', zType(i)); 
    end
end

% shape_type
fprintf(idf,'Shape_Type=');
for i=1:256
    if i<256
        fprintf(idf,'%g,', shapeType(i)); 
    else
        fprintf(idf,'%g\n', shapeType(i)); 
    end
end

fprintf(idf,'numpoints=%g\n', DBS.Shape.ShapeNumberPoints-1);

% close file
fclose(idf);


function flag=lncWriteImage(filename, plotHandle)

flag=true;

exportgraphics(plotHandle,filename)


% NOT USED
function [a, b]=lncMapPixel2Image(matrixResolution)

a=256/matrixResolution;
b=256-a*matrixResolution/2;




