こんにちは。



ご依頼いただきありがとうございます。Sigmaplot 12.0 を使用して、PowerShell スクリプトから図の作成を自動化したいということですね。ご提供いただいた要件に基づいて、具体的なソリューションを提案いたします。



\---



\### **目標**



\- **環境**: Windows 上で PowerShell を使用し、Sigmaplot 12.0 の COM オブジェクトを操作します。

\- **データフロー**: CSV ファイル -> PowerShell スクリプト -> Sigmaplot -> 図 (TIF 形式)

\- **要件**:

  \- ノートブック、セクション、データの作成と名前付け

  \- CSV データのインポートとカラム名の設定

  \- 図の種類ごとのプロット作成

  \- フォント、サイズ、色などのスタイル設定

  \- 図のサイズ (s/m/l) の選択

  \- 色の選択が可能



\---



\### **前提条件**



\- **Sigmaplot 12.0** がインストールされていること

\- **PowerShell** が利用可能であること

\- **Sigmaplot の COM オブジェクト** を操作するためのマニュアルを参照（提供いただいたリンク）



\---



\### **全体の流れ**



1. **PowerShell スクリプトを作成**: Sigmaplot の COM オブジェクトを操作するためのスクリプトを作成します。
2. **CSV データのインポート**: 指定された CSV ファイルを読み込み、Sigmaplot にデータを渡します。
3. **図の作成**: 要件に従い、指定された種類の図を作成します。
4. **スタイル設定**: フォントや色、サイズなどのスタイルを適用します。
5. **図のエクスポート**: 作成した図を TIF 形式で保存します。



\---



\### **1. PowerShell スクリプトの基本構造**



\```powershell

\# Sigmaplot の COM オブジェクトを作成

$Sigmaplot = New-Object -ComObject SigmaPlot.Application



\# Sigmaplot を表示（必要に応じて）

$Sigmaplot.Visible = $true



\# ノートブックの作成と名前付け

$Notebook = $Sigmaplot.Documents.Add()

$Notebook.Name = "NotebookName"  # 任意の名前



\# セクションの作成と名前付け

$Section = $Notebook.Sections.Add()

$Section.Name = "SectionName"  # 任意の名前



\# データの作成と CSV のインポート

$Data = $Section.Data



\# CSV ファイルのパスを指定

$CsvFilePath = "C:\path\to\your\data.csv"



\# データのインポート

$Data.ImportData($CsvFilePath, $true)  # $true はヘッダー行がある場合



\# カラム名の設定（必要に応じて）

$Data.Columns.Item(1).Name = "X"

$Data.Columns.Item(2).Name = "Y"

\```



\---



\### **2. スタイル設定関数の作成**



\```powershell

\# フォント設定の関数

function SetFontStyle($Object, $FontName, $FontSize) {

​    $Object.Font.Name = $FontName

​    $Object.Font.Size = $FontSize

}



\# Tick の設定関数

function SetTickStyle($Axis) {

​    $Axis.MajorTick.Length = 0.8  # 単位は mm

​    $Axis.MajorTick.Width = 0.2

}

\```



\---



\### **3. 図の種類ごとのプロット作成**



\#### **例: シングル スキャッター プロット**



\```powershell

\# グラフの作成

$Graph = $Section.Graphs.Add()

$Graph.Name = "ScatterPlot"



\# プロットの追加

$Plot = $Graph.Plots.Add('Scatter')



\# データの設定

$Plot.XData = $Data.Columns.Item("X")

$Plot.YData = $Data.Columns.Item("Y")



\# スタイルの適用

SetFontStyle $Graph.Title "Arial" 8

SetFontStyle $Graph.Axes.XAxis.Label "Arial" 8

SetFontStyle $Graph.Axes.YAxis.Label "Arial" 8

SetFontStyle $Graph.Axes.XAxis.TickLabels "Arial" 7

SetFontStyle $Graph.Axes.YAxis.TickLabels "Arial" 7



\# Tick のスタイル設定

SetTickStyle $Graph.Axes.XAxis

SetTickStyle $Graph.Axes.YAxis



\# 上側の横軸と右側の縦軸を非表示

$Graph.Axes.TopAxis.Visible = $false

$Graph.Axes.RightAxis.Visible = $false



\# プロットの色を設定

$Color = $Sigmaplot.Colors.Item("Blue")  # 色の選択

$Plot.Symbol.FillColor = $Color

$Plot.Symbol.LineColor = $Color



\# 図のサイズを設定（s/m/l の選択）

function SetGraphSize($Graph, $Size) {

​    switch ($Size) {

​        "s" {

​            $Graph.Width = 80

​            $Graph.Height = 60

​        }

​        "m" {

​            $Graph.Width = 120

​            $Graph.Height = 90

​        }

​        "l" {

​            $Graph.Width = 160

​            $Graph.Height = 120

​        }

​        default {

​            $Graph.Width = 120

​            $Graph.Height = 90

​        }

​    }

}



SetGraphSize $Graph "m"  # サイズを "s", "m", "l" から選択

\```



\---



\### **4. 図のエクスポート**



\```powershell

\# エクスポートの設定

$ExportPath = "C:\path\to\output\figure.tif"

$Graph.ExportAsTIFF($ExportPath)

\```



\---



\### **5. 完全なスクリプトの例**



\```powershell

\# シグマプロットの COM オブジェクトを作成

$Sigmaplot = New-Object -ComObject SigmaPlot.Application

$Sigmaplot.Visible = $true



\# ノートブックとセクションの作成

$Notebook = $Sigmaplot.Documents.Add()

$Notebook.Name = "NotebookName"

$Section = $Notebook.Sections.Add()

$Section.Name = "SectionName"



\# データのインポート

$Data = $Section.Data

$CsvFilePath = "C:\path\to\your\data.csv"

$Data.ImportData($CsvFilePath, $true)

$Data.Columns.Item(1).Name = "X"

$Data.Columns.Item(2).Name = "Y"



\# スタイル設定関数

function SetFontStyle($Object, $FontName, $FontSize) {

​    $Object.Font.Name = $FontName

​    $Object.Font.Size = $FontSize

}



function SetTickStyle($Axis) {

​    $Axis.MajorTick.Length = 0.8

​    $Axis.MajorTick.Width = 0.2

}



function SetGraphSize($Graph, $Size) {

​    switch ($Size) {

​        "s" { $Graph.Width = 80; $Graph.Height = 60 }

​        "m" { $Graph.Width = 120; $Graph.Height = 90 }

​        "l" { $Graph.Width = 160; $Graph.Height = 120 }

​        default { $Graph.Width = 120; $Graph.Height = 90 }

​    }

}



\# グラフの作成と設定

$Graph = $Section.Graphs.Add()

$Graph.Name = "ScatterPlot"

$Plot = $Graph.Plots.Add('Scatter')

$Plot.XData = $Data.Columns.Item("X")

$Plot.YData = $Data.Columns.Item("Y")



\# フォントとスタイルの設定

SetFontStyle $Graph.Title "Arial" 8

SetFontStyle $Graph.Axes.XAxis.Label "Arial" 8

SetFontStyle $Graph.Axes.YAxis.Label "Arial" 8

SetFontStyle $Graph.Axes.XAxis.TickLabels "Arial" 7

SetFontStyle $Graph.Axes.YAxis.TickLabels "Arial" 7



SetTickStyle $Graph.Axes.XAxis

SetTickStyle $Graph.Axes.YAxis



$Graph.Axes.TopAxis.Visible = $false

$Graph.Axes.RightAxis.Visible = $false



\# 色の設定

$ColorName = "Blue"

$Color = $Sigmaplot.Colors.Item($ColorName)

$Plot.Symbol.FillColor = $Color

$Plot.Symbol.LineColor = $Color



\# 図のサイズを設定

SetGraphSize $Graph "m"



\# 図のエクスポート

$ExportPath = "C:\path\to\output\figure.tif"

$Graph.ExportAsTIFF($ExportPath)



\# Sigmaplot を終了

$Sigmaplot.Quit()

\```



\---



\### **6. 図の種類ごとの拡張**



上記の例を基に、他の図の種類も同様に作成できます。



\#### **マルチ スキャッター プロット**



複数のデータセットを繰り返し追加します。



\```powershell

\# 複数のデータセットをプロット

$DataSets = @(

​    @{ X = "X1"; Y = "Y1"; Color = "Blue" },

​    @{ X = "X2"; Y = "Y2"; Color = "Red" },

​    \# 他のデータセット

)



foreach ($DataSet in $DataSets) {

​    $Plot = $Graph.Plots.Add('Scatter')

​    $Plot.XData = $Data.Columns.Item($DataSet.X)

​    $Plot.YData = $Data.Columns.Item($DataSet.Y)

​    $Color = $Sigmaplot.Colors.Item($DataSet.Color)

​    $Plot.Symbol.FillColor = $Color

​    $Plot.Symbol.LineColor = $Color

}

\```



\---



\### **7. 色の選択の実装**



提供いただいた色設定スクリプトを参考に、色を選択できるようにします。



\```powershell

\# 色の取得関数

function GetColor($ColorName) {

​    switch ($ColorName) {

​        "Blue" { return $Sigmaplot.Colors.Item("Blue") }

​        "Red" { return $Sigmaplot.Colors.Item("Red") }

​        \# 他の色を追加

​        default { return $Sigmaplot.Colors.Item("Black") }

​    }

}



\# 使用例

$Color = GetColor "Blue"

$Plot.Symbol.FillColor = $Color

$Plot.Symbol.LineColor = $Color

\```



\---



\### **8. 図のサイズ選択の詳細**



サイズ設定関数を拡張し、具体的な寸法を設定します。



\```powershell

function SetGraphSize($Graph, $Size) {

​    switch ($Size) {

​        "s" {

​            $Graph.Width = 80  # mm 単位

​            $Graph.Height = 60

​        }

​        "m" {

​            $Graph.Width = 120

​            $Graph.Height = 90

​        }

​        "l" {

​            $Graph.Width = 160

​            $Graph.Height = 120

​        }

​        default {

​            $Graph.Width = 120

​            $Graph.Height = 90

​        }

​    }

}

\```



\---



\### **9. コマンドライン引数の導入**



スクリプトを汎用化し、コマンドラインからパラメータを指定できるようにします。



\```powershell

param(

​    [string]$CsvFilePath = "C:\path\to\your\data.csv",

​    [string]$ExportPath = "C:\path\to\output\figure.tif",

​    [string]$PlotType = "Scatter",  # 図の種類

​    [string]$Size = "m",

​    [string]$ColorName = "Blue"

)



\# スクリプト内でパラメータを使用

\```



\---



\### **10. スクリプトの実行方法**



\```bash

\# WSL の Bash から PowerShell スクリプトを実行

powershell.exe -ExecutionPolicy Bypass -File "C:\path\to\script.ps1" -CsvFilePath "C:\path\to\data.csv" -ExportPath "C:\path\to\figure.tif" -PlotType "Line" -Size "l" -ColorName "Red"

\```



\---



\### **まとめ**



\- **柔軟性のあるスクリプト**: 図の種類や色、サイズをパラメータとして指定できるようにしました。

\- **図の種類の拡張**: 各図の種類に応じて、プロットの設定部分を調整します。

\- **スタイルの一貫性**: フォントや色、サイズなどのスタイルを統一しました。

\- **将来の統合性**: コマンドライン引数を使用しているため、Python などからも容易に呼び出せます。



\---



\### **ご注意**



\- **Sigmaplot の COM オブジェクト**: 実際のプロパティやメソッドの名前は、Sigmaplot のドキュメントを参照して正確に指定してください。

\- **エラーハンドリング**: 実運用では、エラーチェックや例外処理を追加して堅牢性を高めてください。

\- **単位の確認**: 長さやサイズの単位（mm など）を確認し、必要に応じて調整してください。



\---



ご不明な点や追加のご要望がございましたら、お気軽にお知らせください。引き続きサポートさせていただきます。