function gm = importGeometry(self, geofilename)
% importGeometry - Creates a geometric model from STL data read from a file.
%    G = importGeometry(PDEM,FILENAME) Constructs a geometry object from STL 
%    data imported from a file. The STL data can represent 3D solid or
%    planar geometry. FILENAME is a character vector or string specifying 
%    the pathname and filename of the STL file including the file extension 
%    ending in 'stl' or 'STL'. If the file is in the current directory or 
%    in a directory on the MATLAB path, the pathname can be omitted. The 
%    geometry object created is assigned to the Geometry property and a handle 
%    to the geometry is returned in G.  
%
%    Example 1:  Import a planar STL geometry and plot.
%      thePde = createpde();
%      gm = importGeometry(thePde,'PlateHolePlanar.stl')
%      pdegplot(gm, 'EdgeLabels','on')
%
%    Example 2:  Import a 3D STL geometry and plot.
%      thePde = createpde();
%      gm = importGeometry(thePde,'Block.stl')
%      pdegplot(gm, 'FaceLabels','on')
%
%    See also pdegplot, pde.DiscreteGeometry, pde.DiscreteGeometry/addVertex

% Copyright 2014-2019 The MathWorks, Inc.

        if nargin > 1
            geofilename = convertStringsToChars(geofilename);
        end
        
        if ~isempty(self.Geometry)                        
           error(message('pde:pdeModel:noAssemSupport')); 
        end
        
        fid = fopen(geofilename, 'r');

        if (fid == -1)
            dirfile = dir(geofilename);
            if ~isempty(dirfile)
                if (length(dirfile) ~= 1)
                    error(message('pde:importGeometry:fileIsDirectory', geofilename));           
                else
                    error(message('pde:importGeometry:fileReadRights', geofilename));
                end
            else  
                error(message('pde:importGeometry:fileDoesNotExist', geofilename));
            end
        else
            % File exists and may be on the MATLAB path.  
            % Get full pathname and filename.
            geofilename = fopen(fid);
            fclose(fid);
        end

        % Verify the file extension
        [~,~,ext] = fileparts(geofilename);
        if ~strcmpi(ext,'.stl')
           error(message('pde:importGeometry:stlFileExtension', geofilename));
        end

        gm = pde.DiscreteGeometry(geofilename);   
        self.Geometry = gm;
    end

% LocalWords:  stl Assem
