Customise section with koma script

koma-scriptsection-titlessectioning

I wonder if you could offer some help. I want to customise the section titles in a scrbook class document. Something similar to this:
enter image description here

Section number in a box followed by a centred line Section name and another vertically centred line filling the text width.

So far, I have searched for solutions in this StackExchange but with no success. I have checked the Koma-Script manual and found two possible commands to do something similar: \sectionformat and \sectionlinesformat. But honestly, my very limited knowledge of latex does not allow me to understand when to use one or the other.

Looking at similar solutions found in this forum, I manage to have this:

\documentclass[openright]{scrbook}
\usepackage{geometry}
\geometry{a4paper,twoside, margin=3cm}
\usepackage{blindtext}% only dummy text
\usepackage{xcolor}

\renewcommand*{\sectionformat}{%
    \usekomafont{section}
    \makebox[0pt][r]{\fboxrule=1.5pt\fcolorbox{black}{white!0}{\color{black}\thesection}\rule[.7ex]{10pt}{1.5pt}}
 }

\begin{document}
    \chapter{Introduction}
    \section{Section 1}
    \Blindtext[2]
    \section{Section 2}
    \Blindtext[2]
    \section{Section 3}
    \Blindtext[2]
    \section{Section 4}
    \Blindtext[2]
\end{document}

from which I get:
enter image description here

But I have not found a way to add the horizontal-vertical text height aligned line to fill the text width.

Another option I tried was using:

%%%%% Simple + underline
\renewcommand{\sectionlinesformat}[4]{%
\Ifstr{#1}{section}{%
    \parbox[c]{\linewidth}{%
      \raggedsection{\hskip #2{\color{black}#3}}{#4}\par%
      \kern-.75\ht\strutbox\rule{\linewidth}{1pt}%
    }%
  }{%
    {\hskip #2#3}{#4}}% 
}

See, here \sectionlinesformat is used instead of \sectionformat, but no idea why.
For which I get:

enter image description here

A third option I found was using \makeatletter and \makeatother:

\makeatletter
\renewcommand\@seccntformat[1]{%
  \makebox[0pt][l]{\rule[-\dimexpr\fboxsep+2pt\relax]{\columnwidth}{1.5pt}}%
  \colorbox{blue}{%
    \rule[-2pt]{0pt}{0pt}%
    \color{white}\csname the#1\endcsname
  }%
  \quad
}
\makeatother

And I get:

enter image description here

Any guidance on how can I get the desired result?

Also, It would be nice if we can have the option to have the section number and its box aligned with the text or on the margin.

Best Answer

Something like this?

\documentclass[openright]{scrbook}
\usepackage{geometry}
\geometry{a4paper,twoside, margin=3cm}
\usepackage{blindtext}% only dummy text
\usepackage{xcolor}
\usepackage{xhfill}

\renewcommand*{\sectionformat}{%
    \usekomafont{section}%
    \fboxrule=1pt\fcolorbox{blue}{white!0}{\color{black}\thesection}%
    \color{blue}\rule[.7ex]{10pt}{1pt}\>%
}
\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
    \@hangfrom{\hskip #2#3}{\color{blue}#4}\>%
    \xrfill[.7ex]{1pt}[blue]%
}
\makeatother

\begin{document}
    \chapter{Introduction}
    \section{Section 1}
    \Blindtext[2]
    \section{Section 2}
    \Blindtext[2]
    \section{Section 3}
    \Blindtext[2]
    \section{Section 4}
    \Blindtext[2]
\end{document}