VisioDocument.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190


<#
        .SYNOPSIS
        Opens a visio document

        .DESCRIPTION
        Opens an existing Visio document, a blank Visio Document

        .PARAMETER Path
        The path to an existing document, and empty string (to create a blank document) or the path to a Visio template (vstx, etc.)

        .PARAMETER Visio
        Optional reference to a Visio Application (used if writing to multiple diagrams at the same time?)

        .PARAMETER Update
        Switch indicating that we're updating a diagram, potentially created with VisioBot3000

        .INPUTS
        None. You cannot pipe objects to Add-Extension.

        .OUTPUTS
        None

        .EXAMPLE
        Open-VisioDocument
        --Creates a blank document--

        .EXAMPLE
        Open-VisioDocument .\MySampleVisio.vsdx
        --Opens the named document

        .EXAMPLE
        Open-VisioDocument .\MyVisioTemplate.vstx
        --Creates a Visio template for editing (not a new document based on the template)

#>

Function Open-VisioDocument{
    [CmdletBinding()]
    Param([string]$Path,
        $Visio=$script:Visio,
    [switch]$Update)
    if(!(Test-VisioApplication)){
        New-VisioApplication
        $Visio=$script:Visio
    }
    $documents = $Visio.Documents
    $documents.Add($Path) | out-null
    if($Update){
        $script:updatemode=$True 
    }
}


<#
        .SYNOPSIS
        Creates a new document

        .DESCRIPTION
        Creates a new document

        .PARAMETER Path
        The path you want to save the document to

        .PARAMETER From
        The path to a template file to create the new document from

        .PARAMETER Visio
        Optional reference to a Visio Application (used if writing to multiple diagrams at the same time?)

        .PARAMETER Update
        Switch indicating that we're updating a diagram, potentially created with VisioBot3000

        .INPUTS
        None. You cannot pipe objects to Add-Extension.

        .OUTPUTS
        None

        .EXAMPLE
        New-VisioDocument
        --Creates a blank document--

        .EXAMPLE
        New-VisioDocument .\MySampleVisio.vsdx
        --Opens the named document

        .EXAMPLE
        New-VisioDocument .\MyVisioTemplate.vstx
        --Creates a new document based on a Visio template

#>
 
function New-VisioDocument{
    [CmdletBinding(SupportsShouldProcess=$True)]
    Param([string]$Path,
        [string]$From='',
        $Visio=$script:visio,
        [switch]$Update,
    [switch]$Landscape,[switch]$portrait)
    if($PSCmdlet.ShouldProcess('Creating a new Visio Document','')){
        if(!(Test-VisioApplication)){
            New-VisioApplication
            $Visio=$script:Visio
        }
        if($Update){
            if($From -ne ''){
                Write-Warning 'New-VisioDocument: -From ignored when -Update is present'
            }
            Open-VisioDocument $Path -Update
        } else {
            Open-VisioDocument $From

        }
        
        if($Landscape){
            $Visio.ActiveDocument.DiagramServicesEnabled=8
            $Visio.ActivePage.Shapes['ThePage'].CellsU('PrintPageOrientation')=2
        } elseif ($portrait) {
            $Visio.ActivePage.Shapes['ThePage'].CellsU('PrintPageOrientation')=1
        }
        if($path){
            $Visio.ActiveDocument.SaveAs($Path) | Out-Null
        }
    }
}

<#
        .SYNOPSIS
        Outputs the active Visio document

        .DESCRIPTION
        Outputs the active Visio document

        .PARAMETER Visio
        Optional reference to a Visio Application (used if writing to multiple diagrams at the same time?)

        .INPUTS
        None. You cannot pipe objects to Get-VisioDocument.

        .OUTPUTS
        visio.Document

        .EXAMPLE
        $doc=Get-VisioDocument

#>

Function Get-VisioDocument{
    [CmdletBinding()]
    Param($Visio=$script:Visio)
    if(!(Test-VisioApplication)){
        New-VisioApplication 
    }
    return $Visio.ActiveDocument
}

<#
        .SYNOPSIS
        Saves the diagram and optionally exits Visio

        .DESCRIPTION
        Saves the diagram and optionally exits Visio

        .PARAMETER Close
        Whether to exit Visio or not

        .INPUTS
        None. You cannot pipe objects to Complete-Diagram.

        .OUTPUTS
        None

        .EXAMPLE
        Complete-Diagram
#>

Function Complete-VisioDocument{
    [CmdletBinding()]
    Param([switch]$Close)
    if(Test-VisioApplication){
        $script:updateMode=$false
        $Visio.ActiveDocument.Save() 
        if($Close){
            $Visio.Quit()
        }
        foreach($name in $script:GlobalFunctions){
            remove-item -Path "Function`:$name"
        }
    } else {
        Write-Warning 'Visio application is not loaded'
    }
}