MapWindowのPlugin作成 [Chapter 1-1]

MapWindowのPlugin作成 [Chapter 1-1]

[Chapter 1-1]では、MyFirstPluginを使ってPlugin機能を新しく作成してみます。

[CHapter 1] の設定ができていることを前提に進めていきます。
開発環境は、Visual Studio 2010 vb.net、MapWindowはVersion4.8.6です。

Plugin機能の準備

まずは、[CHapter 1] で作成したMyFirstPlugin Templateをコピーして、MyPluginと言う名前に変更しておきます。

MyFirstPlugin.vbをMyPlugin.vbに変更します。
その他、プロジェクトのプロパティのMyFirstPluginとなっているところをすべてMyPluginに変更します。
一度、保存します。

frmInfoの作成

今回のMyPluginの機能は、Map上のLayer情報を表示するフォームを呼び出すものを作成します。

まずは、フォームの作成です。
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image06.jpg
frmInfo.vbのデザイン

LabelとDataGridViewを配置しています。

frmInfoのCodingを抜粋して載せておきます。
frmInfoが新規に作成された時に、引数としてmapWin As MapWindow.Interfaces.IMapWinでMapの情報を受け取っています。

Imports System
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
 
Imports MapWindow.Interfaces
Imports MapWinGIS
 
Public Class frmInfo
 
    'The _mapWin object is used for communicating
    'with the main application
    Private _mapWin As MapWindow.Interfaces.IMapWin
 
    Public Sub New(ByVal mapWin As MapWindow.Interfaces.IMapWin)
        InitializeComponent()
        _mapWin = mapWin
 
        'set DataGridView
        DataGridViewInit()
    End Sub
 
    Private Sub DataGridViewInit()
        'グリッドに表示するデータ
        'グリッドに表示するデータ形式の設定をする。
    End Sub
 
    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
        e.Cancel = True
        Me.Hide()
    End Sub
 
 
    Public Sub UpdateLayerInfo()
        'loop through all map layers and show information
        Dim numLayers As Integer = _mapWin.Layers.NumLayers
 
        'remove Row data
        DataGridViewRowRemove(dgvLayers)
 
        If numLayers > 0 Then
            lblNumLayers.Text = numLayers.ToString()
            'add Row data
            Dim n As Integer = 0
            For Each lay As Layer In _mapWin.Layers
                Dim layName As String = lay.Name.ToString()
                Dim shpCount As Integer
                If lay.LayerType = eLayerType.LineShapefile Or
                    lay.LayerType = eLayerType.PointShapefile Or
                    lay.LayerType = eLayerType.PolygonShapefile Then
                    shpCount = lay.Shapes.NumShapes
                Else
                    shpCount = 0
                End If
                'display information about the source Layer set
                If Not layName Is Nothing Then
                    dgvLayers.Rows.Add()
                    dgvLayers.Item(0, n).Value = layName
                    dgvLayers.Item(1, n).Value = lay.LayerType.ToString()
                    dgvLayers.Item(2, n).Value = lay.Handle
                    dgvLayers.Item(3, n).Value = shpCount
                    n = n + 1
 
                End If
            Next
        Else
            lblNumLayers.Text = "マップ上にレイヤーがありません。"
            Exit Sub
        End If
    End Sub
 
    Private Sub DataGridViewRowRemove(ByRef dgv As DataGridView)
        '行を数えて、全行のデータを削除します。
    End Sub
 
End Class

MyPluginのCode

MyPluginのclassのcodeはMyFirstPluginからの変更点だけを載せておきます。
frmInfoをInstanseします。

    Private frmInfo As frmInfo

今回は、Methodのみを変更してます。

  1. InitializeでMapWindowのメニューにMyPluginを追加して、
  2. TerminateでMapWindowのメニューから削除

します。

#Region "Plugin Start/Stop"
 
    Public Sub Initialize(ByVal MapWin As MapWindow.Interfaces.IMapWin,
                          ByVal ParentHandle As Integer) Implements MapWindow.Interfaces.IPlugin.Initialize
        'This event is fired when the user loads your plug-in either through the plug-in dialog
        'box, or by checkmarking it in the plug-ins menu.  This is where you would add buttons to the
        'tool bar or menu items to the menu.
        '
        'It is also standard to set a global reference to the IMapWin that is passed through here so that
        'you can access it elsewhere in your project to act on MapWindow.
        g_MapWin = MapWin
 
        Dim nil As Object
        nil = Nothing
        With g_MapWin.Menus
            .AddMenu("msgMain", nil, "My Plugin")
            .AddMenu("msgInfoMap", "msgMain", nil, "Map Information")
        End With
 
    End Sub
 
    Public Sub Terminate() Implements MapWindow.Interfaces.IPlugin.Terminate
        'This event is fired when the user unloads your plug-in either through the plug-in dialog
        'box, or by un-checkmarking it in the plug-ins menu.  This is where you would remove any
        'buttons from the tool bar tool bar or menu items from the menu that you may have added.
        'If you don't do this, then you will leave dangling menus and buttons that don't do anything.
        g_MapWin.Menus.Remove("msgInfoMap")
        g_MapWin.Menus.Remove("msgMain")
 
    End Sub

次に、MapWindowのメニューのitemをclickした時のeventを書いておきます。
frmInfoを表示することがitem click eventになります。

    Public Sub ItemClicked(ByVal ItemName As String, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.ItemClicked
        'This event fires when a menu item or toolbar button is clicked.  So if you added a button or menu
        'on the Initialize event, then this is where you would handle it.
 
        Select Case ItemName
            Case "msgInfoMap"
                'Show the form
                If frmInfo Is Nothing Then
                    frmInfo = New frmInfo(g_MapWin)
                End If
                If frmInfo.Visible = False Then
                    frmInfo.Show()
                    frmInfo.UpdateLayerInfo()
                    frmInfo.TopMost = True
                Else
                    frmInfo.UpdateLayerInfo()
                    frmInfo.TopMost = True
                End If
        End Select
    End Sub

MyPluginの実行

MyPluginをDebug実行させると、MapWindowが起動します。
メニューのプラグインから
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image12.jpg
MyPluginを選択すると、
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image07.jpg
のようにメニューにMyPluginが登録されます。
MyPluginメニューの
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image13.jpg
を選択するとマップ情報のフォームが
blog.godo-tys.jp_wp-content_gallery_mapwindow_01_image08.jpg
な感じで表示されます。

今回のまとめ

どうでしょうか? 簡単に作成できましたね。

では、今回のまとめを

  1. PluginメニューにMyPluginを作成しました。
  2. Plugin作成の基本機能を作成しました。

MapWindowのPluginを作成することで、カスタマイズが簡単になります。また、MapWinGISでのCodingとほぼ同じように扱えるので、開発は比較的簡単にできます。

vb.net2010だけでなく、C#でも、visual studio2008でも動作することができます。
Plugin_ex01のsource codeを公開しますので、活用してください。
ただし、あくまでの自分用のサンプルですので、バグなどがある可能性が高いので、原文のままの使用は避けてください。

サンプルコードおよびサンプルデータを使って、お使いのPCの不具合が生じても一切責任は持てませんので、あくまでも自己責任にて使用してください。

次回からは、MapWindowのPluginにもsampleとして公開されているSample MapWindow Plug-in Project: Path Analyzerを作成していきます。
Sample MapWindow Plug-in Project: Path Analyzerはvb2005で作成されていて、MapWinGISのVersion3.0と古いためダウンロードしただけでは動きませんので、これに若干手直しをして使えるようにします。

Comments are closed.

Social Widgets powered by AB-WebLog.com.