function port = pfcblock_updateGroundInport(blk,toblockname,AvailableBlocks,type_in_mask,type_in_block,index,name,port,toport)
% Utility function refreshes optional block inports in the "Path following
% control system Block".
%
% 
% Inputs:
%   blk:                the masked subsystem where the inport is added
%   toblockname:        which block the inport connects to
%   AvailableBlocks:    all the blocks inside masked subsystem
%   type_in_mask:       all optional inports demands in the mask dialog
%   type_in_block:      all optional inports in the masked subsystem
%   index:              index in the list of all optional inports
%   name:               name of the inport
%   port:               port number in the masked subsystem
%   toport:             port number of the "toblock" that it connects to

%   Author: Meng Xia, Rong Chen
%   Copyright 2017-2018 The MathWorks, Inc.

%%
if type_in_mask(index)
    if ~type_in_block(index)
        add_inport(blk,name,port,[toblockname '/' num2str(toport)]);
    end
    port = port + 1;
else
    if type_in_block(index)
        if ismember(name,AvailableBlocks)
            delete_inport(blk,name,[toblockname '/' num2str(toport)]);
        end
    end
end

    


function add_inport(blk,name,port,toport)
inportblk = [blk '/' name];
groundblk = [blk '/' name ' ground']; 
delete_line(blk,[name ' ground/1'],toport);
position = get_param(groundblk,'position');
delete_block(groundblk);
add_block('built-in/Inport',inportblk);
add_line(blk,[name '/1'],toport);
set_param(inportblk,'position',position,'port',num2str(port));

function delete_inport(blk,name,toport)
inportblk = [blk '/' name];
groundblk = [blk '/' name ' ground']; 
delete_line(blk,[name '/1'],toport);
position = get_param(inportblk,'position');
delete_block(inportblk);
add_block('built-in/Constant',groundblk);
add_line(blk,[name ' ground/1'],toport);
set_param(groundblk,'position',position);
set_param(groundblk,'OutDataTypeStr','DataType');




