xlogI125’s blog

パソコン作業を効率化したい

PowerShell: フォームにドラッグ&ドロップされたファイルのパスを取得

マウスボタンの接触不良が原因でショートカット(*.lnk)アイコンへのドラッグ&ドロップ操作を失敗すると大変なことになるので、フォームへのドラッグ&ドロップを考える。

# PowerShell 5.1, Windows 11 (2024年2月頃)

$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue

Set-StrictMode -Version Latest

Add-Type -AssemblyName System.Windows.Forms

Add-Type -Language CSharp -TypeDefinition @'
using System.Runtime.InteropServices;
namespace Win32API {
  public class Shlwapi {
    [DllImport("Shlwapi.dll", EntryPoint = "StrCmpLogicalW", CharSet = CharSet.Unicode)]
    public static extern int StrCmpLogicalW(
      [MarshalAs(UnmanagedType.LPWStr)] string psz1, 
      [MarshalAs(UnmanagedType.LPWStr)] string psz2
    );
  }
}
'@

# フォームにドラッグ&ドロップされたファイルのパスを取得
$paths = Invoke-Command -ScriptBlock {
  $refPaths = [ref]$null

  $form = [Windows.Forms.Form]::new()
  $form.AllowDrop = $true
  $form.Text = "ドラッグ&ドロップ"
  $form.Width = 300
  $form.Height = 200

  # ドラッグ
  $form.add_DragEnter([System.Windows.Forms.DragEventHandler]{
    param([object]$sender, [Windows.Forms.DragEventArgs]$e)

    try {
      # ドラッグされた項目のパスを取得
      $paths = $e.Data.GetFileDropList()

      if ($paths.Count -eq 0) {
        return
      }

      # ファイル以外を含む場合はドロップ不可とする
      foreach ($path in $paths) {
        if ( -not [System.IO.File]::Exists($path) ) {
          return
        }
      }

      $e.Effect = [Windows.Forms.DragDropEffects]::Copy

      # ドラッグされた項目のパスを確認
      $paths.ForEach({ [PSCustomObject]@{ Event = "DragEnter"; Value = $_ } }) | 
      Format-Table -AutoSize -Wrap | Out-String -Width 80 | Write-Verbose
    }
    catch {
      Write-Error -ErrorRecord $_ -ErrorAction Continue
    }
  })

  # ドロップ
  $form.add_DragDrop([System.Windows.Forms.DragEventHandler]{
    param([object]$sender, [Windows.Forms.DragEventArgs]$e)

    try {
      # ドロップされたファイルのパスを取得
      $refPaths.Value = [string[]]$e.Data.GetFileDropList()

      # パスをソート
      [System.Array]::Sort($refPaths.Value, [System.Comparison[string]]{
        param ([string]$x, [string]$y)
        return [Win32API.Shlwapi]::StrCmpLogicalW($x, $y)
      })

      # ソート後のパスを確認
      $refPaths.Value.ForEach({ [PSCustomObject]@{ Event = "DragDrop"; Value=$_ } }) | 
      Format-Table -AutoSize -Wrap | Out-String -Width 80 | Write-Verbose
    }
    catch {
      Write-Error -ErrorRecord $_ -ErrorAction Continue
    }
  })

  $null = $form.ShowDialog()
  $form.Dispose()

  # unary array expression
  return (, $refPaths.Value)
}

$paths

メモ: ラプラス変換

正しい内容は教科書などを参照してください

ラプラス変換

t<0 のとき y(t)=0 とする


\Large
\begin{align*}
Y(s)
&= \mathcal{L}\left[ y(t) \right] \\
&= \int_0^\infty y(t) \mathrm{e}^{-st} \mathrm{d}t
\end{align*}

変数 s の関数として見る

\Large
\begin{align*}
Y(s-a)
&= \int_0^\infty y(t) \mathrm{e}^{-\left(s - a\right)t} \mathrm{d}t \\
&= \int_0^\infty \left( y(t) \mathrm{e}^{at}     \right) \mathrm{e}^{-st} \mathrm{d}t \\
&= \mathcal{L}\left[ \mathrm{e}^{at} y(t) \right]
\end{align*}

微分された関数のラプラス変換


\Large
\begin{align*}
\mathcal{L}\left[ \frac{\mathrm{d}y(t)}{\mathrm{d}t} \right]
&= s \mathcal{L}\left[ y(t) \right] - y(0) \\
&= s Y(s) - y(0)
\end{align*}

y(t)=1 のとき

\Large
\begin{gather*}
\mathcal{L}\left[ \frac{\mathrm{d}1}{\mathrm{d}t} \right] = s \mathcal{L}\left[ 1 \right] - 1 \\
\mathcal{L}\left[ 1 \right] = \frac{1}{s}
\end{gather*}

Y(s-a) = \mathcal{L}\left[ \mathrm{e}^{at} y(t) \right] より

\Large
\begin{gather*}
\frac{1}{s - a} = \mathcal{L}\left[ \mathrm{e}^{at} 1 \right] \\
\mathcal{L}\left[ \mathrm{e}^{at} \right] = \frac{1}{s - a}
\end{gather*}

機械的a = \mathrm{j}\omega とする

\Large
\begin{gather*}
\mathcal{L}\left[ \mathrm{e}^{\mathrm{j} \omega t} \right]
= \frac{1}{s - \mathrm{j}\omega}
= \frac{s}{s^2 + \omega^2} + \mathrm{j}\frac{\omega}{s^2 + \omega^2}
\end{gather*}

Y(s-a) = \mathcal{L}\left[ \mathrm{e}^{at} y(t) \right] より

\Large
\begin{gather*}
\frac{s-a}{\left(s-a\right)^2 + \omega^2} + \mathrm{j}\frac{\omega}{\left(s-a\right)^2 + \omega^2}
= \mathcal{L}\left[ \mathrm{e}^{at} \mathrm{e}^{\mathrm{j} \omega t} \right]
\end{gather*}

導出過程は気にしない

\Large
\begin{gather*}
\mathcal{L}\left[ \mathrm{e}^{at} \cos \omega t \right] = \frac{s-a}{\left(s-a\right)^2 + \omega^2} \\
\mathcal{L}\left[ \mathrm{e}^{at} \sin \omega t \right] = \frac{\omega}{\left(s-a\right)^2 + \omega^2}
\end{gather*}

y(t)=t^n のとき

\Large
\begin{gather*}
\mathcal{L}\left[ \frac{\mathrm{d}t^n}{\mathrm{d}t} \right] = s \mathcal{L}\left[ t^n \right] - 0 \\
\mathcal{L}\left[ t^n \right] = \frac{n}{s} \mathcal{L}\left[ t^{n-1} \right]
\end{gather*}

n=1 のとき

\Large
\begin{gather*}
\mathcal{L}\left[ t \right]
= \frac{1}{s} \mathcal{L}\left[ 1 \right]
= \frac{1}{s^2}
\end{gather*}

n=2 のとき

\Large
\begin{gather*}
\mathcal{L}\left[ t^2 \right]
= \frac{2}{s} \mathcal{L}\left[ t \right]
= \frac{2}{s^3}
\end{gather*}

積分された関数のラプラス変換

t<0 のとき f(t)=0, g(t)=0 とするので \tau > t のとき g(t-\tau) = 0

\Large
\begin{align*}
\int_0^t f(\tau)g(t-\tau)\mathrm{d}\tau
&= \int_0^\infty f(\tau)g(t-\tau)\mathrm{d}\tau - \int_t^\infty f(\tau)g(t-\tau)\mathrm{d}\tau \\
&= \int_0^\infty f(\tau)g(t-\tau)\mathrm{d}\tau - 0 \\
&= \int_0^\infty f(\tau)g(t-\tau)\mathrm{d}\tau
\end{align*}


\Large
\begin{align*}
\int_{-\tau_0}^\infty f(\tau)g(t-\tau)\mathrm{d}\tau
&= \int_{-\tau_0}^0 f(\tau)g(t-\tau)\mathrm{d}\tau + \int_0^\infty f(\tau)g(t-\tau)\mathrm{d}\tau \\
&= 0 + \int_0^\infty f(\tau)g(t-\tau)\mathrm{d}\tau \\
&= \int_0^\infty f(\tau)g(t-\tau)\mathrm{d}\tau
\end{align*}


\Large
\begin{align*}
\mathcal{L}\left[ \int_0^t f(\tau)g(t - \tau)\mathrm{d}\tau \right]
&= \int_0^\infty \left( \int_0^t      f(\tau)g(t-\tau)\mathrm{d}\tau \right) \mathrm{e}^{-st} \mathrm{d}t \\
&= \int_0^\infty \left( \int_0^x      f(y)g(x-y)\mathrm{d}y \right) \mathrm{e}^{-sx} \mathrm{d}x \\
&= \int_0^\infty \left( \int_0^\infty f(y)g(x-y)\mathrm{d}y \right) \mathrm{e}^{-sx} \mathrm{d}x \\
&= \int_0^\infty \left( \int_0^\infty f(y)g(x-y) \mathrm{e}^{-sx} \mathrm{d}y \right) \mathrm{d}x \\
&= \int_0^\infty \left( \int_0^\infty f(y) \mathrm{e}^{-sy} g(x-y) \mathrm{e}^{-s\left(x-y\right)} \mathrm{d}y \right) \mathrm{d}x \\
&= \int_0^\infty \left( \int_0^\infty f(y) \mathrm{e}^{-sy} g(x-y) \mathrm{e}^{-s\left(x-y\right)} \mathrm{d}x \right) \mathrm{d}y \\
&= \int_0^\infty f(y) \mathrm{e}^{-sy} \left( \int_0^\infty g(x-y) \mathrm{e}^{-s\left(x-y\right)} \mathrm{d}x \right) \mathrm{d}y \\
&= \int_0^\infty f(y) \mathrm{e}^{-sy} \left( \int_0^\infty g(\tau) \mathrm{e}^{-s\tau} \mathrm{d}\tau \right) \mathrm{d}y \\
&= \left( \int_0^\infty f(y) \mathrm{e}^{-sy} \mathrm{d}y \right) \left( \int_0^\infty g(\tau) \mathrm{e}^{-s\tau} \mathrm{d}\tau \right) \\
&= \left( \int_0^\infty f(t) \mathrm{e}^{-st} \mathrm{d}t \right) \left( \int_0^\infty g(t) \mathrm{e}^{-st} \mathrm{d}t \right) \\
&= \mathcal{L}\left[ f(t) \right] \mathcal{L}\left[ g(t) \right] \\
&= F(s)G(s)
\end{align*}

g(t)=1 のとき

\Large
\begin{gather*}
\mathcal{L}\left[ \int_0^t f(\tau) \mathrm{d}\tau \right] = F(s) \frac{1}{s}
\end{gather*}


\Large
\begin{align*}
\mathcal{L}\left[ \int_0^t y(t) \mathrm{d}t \right]
&= \frac{1}{s} \mathcal{L}\left[ y(t) \right] \\
&= \frac{1}{s} Y(s)
\end{align*}

終値


\Large
\begin{gather*}
\left\{
\begin{aligned}
&\lim_{s \to 0} \int_0^\infty \left( \frac{\mathrm{d}y(t)}{\mathrm{d}t} \right) \mathrm{e}^{-st} \mathrm{d}t &{}
&= \lim_{s \to 0} \mathcal{L}\left[ \frac{\mathrm{d}y(t)}{\mathrm{d}t} \right] &{}
&= \lim_{s \to 0} \left( s \mathcal{L}\left[ y(t) \right] - y(0) \right) \\
%
&\lim_{s \to 0} \int_0^\infty \left( \frac{\mathrm{d}y(t)}{\mathrm{d}t} \right) \mathrm{e}^{-st} \mathrm{d}t &{}
&= \int_0^\infty \left( \frac{\mathrm{d}y(t)}{\mathrm{d}t} \right) \mathrm{d}t &{}
&= y(\infty) - y(0)
\end{aligned}
\right.
\end{gather*}


\Large
\begin{gather*}
y(\infty) = \lim_{s \to 0} s \mathcal{L}\left[ y(t) \right] = \lim_{s \to 0} s Y(s) \\
\end{gather*}

初期値


\Large
\begin{gather*}
\left\{
\begin{aligned}
&\lim_{s \to \infty} \int_0^\infty \left( \frac{\mathrm{d}y(t)}{\mathrm{d}t} \right) \mathrm{e}^{-st} \mathrm{d}t &{}
&= \lim_{s \to \infty} \mathcal{L}\left[ \frac{\mathrm{d}y(t)}{\mathrm{d}t} \right] &{}
&= \lim_{s \to \infty} \left( s \mathcal{L}\left[ y(t) \right] - y(0) \right) \\
%
&\lim_{s \to \infty} \int_0^\infty \left( \frac{\mathrm{d}y(t)}{\mathrm{d}t} \right) \mathrm{e}^{-st} \mathrm{d}t &{}
&= \int_0^\infty 0 \mathrm{d}t &{}
&= 0
\end{aligned}
\right.
\end{gather*}


\Large
\begin{gather*}
y(0) = \lim_{s \to \infty} s \mathcal{L}\left[ y(t) \right] = \lim_{s \to \infty} s Y(s) \\
\end{gather*}

電流I0

正確な内容は電気回路論や交流理論などの教科書を参照してください

集中定数回路で単一周波数の定常状態のみを考慮

定常状態の基本波のみを考慮する

\Large
\begin{align*}
i(x,t)
&= f(x)g(t) \\
&= kg(t) \\
&= \frac{a_0}{2} + \sum_{n=1}^\infty \left( a_n \cos\left(n \omega_{\mathrm{e}1} t \right) + b_n \sin\left(n \omega_{\mathrm{e}1} t \right) \right) \\
&= I_0 + \sum_{n=1}^\infty \sqrt{2} I_n \sin \left( n \omega_{\mathrm{e}1} t + \varphi_{\mathrm{e}n} \right) \\
&= \sqrt{2} I_1 \sin \left( \omega_{\mathrm{e}1} t + \varphi_{\mathrm{e}1} \right)
\end{align*}

電気回路の計算式での取り扱いが便利になるよう重ね合わせる

\Large
\begin{align*}
I(t)
&= \left( A + \mathrm{j} B \right) i(t) + \left( C + \mathrm{j} D \right) \frac{\mathrm{d}}{\mathrm{d}t}i(t) \\
&= \sqrt{2} I_1 \left(\cos \left( \omega_{\mathrm{e}1} t + \varphi_{\mathrm{e}1} \right) + \mathrm{j}\sin \left( \omega_{\mathrm{e}1} t + \varphi_{\mathrm{e}1} \right) \right) \\
&= \sqrt{2} I_1 \mathrm{e}^{\mathrm{j}\varphi_{\mathrm{e}1}} \mathrm{e}^{\mathrm{j} \omega_{\mathrm{e}1} t}
\end{align*}

フェーザ表示

三相回路における電流をフェーザ表示で考えることにする

\Large
\begin{align*}
\dot{I}_{\mathrm{a}}
&= I_{\mathrm{a}} \left( \cos \varphi_\mathrm{a} + \mathrm{j} \sin \varphi_\mathrm{a} \right) \\
&= I_{\mathrm{a}} \mathrm{e}^{\mathrm{j} \varphi_\mathrm{a} }
\end{align*}

ベクトルオペレータ

\Large
\begin{equation*}
a = \mathrm{e}^{\left(2\pi/3\right)\mathrm{j}}
\end{equation*}

三相3線式における各相の電流が変数I0, I1, I2の重ね合わせで表現できるとする

\Large
\begin{equation*}
\left\{
\begin{aligned}
\dot{I}_{\mathrm{a}}
&= &{} &\dot{I}_{\mathrm{0}} &{} &+ &{}     &\dot{I}_{\mathrm{1}} &{} &+ &{}  &\dot{I}_{\mathrm{2}} \\
%
\dot{I}_{\mathrm{b}}
&= &{} &\dot{I}_{\mathrm{0}} &{} &+ &a^{-1} &\dot{I}_{\mathrm{1}} &{} &+ &a   &\dot{I}_{\mathrm{2}} \\
%
\dot{I}_{\mathrm{c}}
&= &{} &\dot{I}_{\mathrm{0}} &{} &+ &a^{-2} &\dot{I}_{\mathrm{1}} &{} &+ &a^2 &\dot{I}_{\mathrm{2}}
\end{aligned}
\right.
\end{equation*}

指数部分を書き直す

\Large
\begin{equation*}
\left\{
\begin{aligned}
\dot{I}_{\mathrm{a}}
&= &{} &\dot{I}_{\mathrm{0}} &{} &+ &{}     &\dot{I}_{\mathrm{1}} &{} &+ &{} &\dot{I}_{\mathrm{2}} \\
%
\dot{I}_{\mathrm{b}}
&= &{} &\dot{I}_{\mathrm{0}} &{} &+ &a^{2} &\dot{I}_{\mathrm{1}} &{} &+ &a   &\dot{I}_{\mathrm{2}} \\
%
\dot{I}_{\mathrm{c}}
&= &{} &\dot{I}_{\mathrm{0}} &{} &+ &a     &\dot{I}_{\mathrm{1}} &{} &+ &a^2 &\dot{I}_{\mathrm{2}}
\end{aligned}
\right.
\end{equation*}

教科書などで登場する行列表記

\Large
\begin{equation*}
\begin{pmatrix}
\dot{I}_{\mathrm{a}} \\
\dot{I}_{\mathrm{b}} \\
\dot{I}_{\mathrm{c}}
\end{pmatrix}
=
\begin{pmatrix}
1 & 1   & 1 \\
1 & a^2 & a \\
1 & a   & a^2
\end{pmatrix}
\begin{pmatrix}
\dot{I}_{\mathrm{0}} \\
\dot{I}_{\mathrm{1}} \\
\dot{I}_{\mathrm{2}}
\end{pmatrix}
\end{equation*}

変数I0, I1, I2

\Large
\begin{equation*}
\begin{pmatrix}
\dot{I}_{\mathrm{0}} \\
\dot{I}_{\mathrm{1}} \\
\dot{I}_{\mathrm{2}}
\end{pmatrix}
=
\cfrac{1}{3}
\begin{pmatrix}
1 & 1   & 1   \\
1 & a   & a^2 \\
1 & a^2 & a
\end{pmatrix}
\begin{pmatrix}
\dot{I}_{\mathrm{a}} \\
\dot{I}_{\mathrm{b}} \\
\dot{I}_{\mathrm{c}}
\end{pmatrix}
\end{equation*}

変数I0

\Large
\begin{align*}
I_{\mathrm{0}}
&= \frac{1}{3} \lvert \dot{I}_{\mathrm{a}} + \dot{I}_{\mathrm{b}} + \dot{I}_{\mathrm{c}} \rvert \\
&= \frac{1}{3} \lvert \dot{i}_{\mathrm{a}} + \dot{i}_{\mathrm{b}} + \dot{i}_{\mathrm{c}} \rvert I_{3\phi\mathrm{BASE}} \\
&= i_{\mathrm{0}} \frac{S_{3\phi\mathrm{BASE}}}{3 E_{3\phi\mathrm{BASE}}}
\end{align*}

変圧器(Y結線)の中性点に流れ込む電流Ig

\Large
\begin{align*}
I_{\mathrm{g}}
&= \lvert \dot{I}_{\mathrm{a}} + \dot{I}_{\mathrm{b}} + \dot{I}_{\mathrm{c}} \rvert \\
&= 3 I_0
\end{align*}