Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visio to image command line conversion

Tags:

windows

visio

At work we make pretty extensive use of Visio drawing as support for documentation. Unfortunately vsd files don't play nicely with our wiki or documentation extraction tools like javadoc, doxygen or naturaldocs. While it is possible to convert Visio files to images manually, it's just a hassle to keep the image current and the image files are bound to get out of date. And let's face it: Having generated files in revision control feels so wrong.

So I'm looking for a command line tool that can convert a vsd file to jpeg, png, gif or any image that can be converted to an image that a browser can display. Preferably it will run under unix, but windows only is also fine. I can handle the rest of the automation chain, cron job, image to image conversion and ssh, scp, multiple files, etc.

And that's why I'm turning to you: I can't find such a tool. I don't think I can even pay for such a tool. Is my Google-fu completely off? Can you help me?

I mean, it has got to be possible. There has to be a way to hook into Visio with COM and get it to save as image. I'm using Visio 2007 by the way.

Thanks in advance.

like image 519
Peter Monsson Avatar asked Jul 17 '09 19:07

Peter Monsson


2 Answers

I slapped something together quickly using VB6, and you can download it at: http://fournier.jonathan.googlepages.com/Vis2Img.exe

You just pass in the input visio file path, then the output file path (visio exports based on file extension) and optionally the page number to export.

Also here is the source code I used, if you want to mess with it or turn it into a VBScript or something, it should work, though you'd need to finish converting it to late-bound code.

hope that helps,

Jon

Dim TheCmd As String
Const visOpenRO = 2
Const visOpenMinimized = 16
Const visOpenHidden = 64
Const visOpenMacrosDisabled = 128
Const visOpenNoWorkspace = 256

Sub Main()
    ' interpret command line arguments - separated by spaces outside of double quotes
    TheCmd = Command
    Dim TheCmds() As String
    If SplitCommandArg(TheCmds) Then
        If UBound(TheCmds) > 1 Then
            Dim PageNum As Long
            If UBound(TheCmds) >= 3 Then
                PageNum = Val(TheCmds(3))
            Else
                PageNum = 1
            End If

            ' if the input or output file doesn't contain a file path, then assume the same
            If InStr(1, TheCmds(1), "\") = 0 Then
                TheCmds(1) = App.Path & "\" & TheCmds(1)
            End If
            If InStr(1, TheCmds(2), "\") = 0 Then
                TheCmds(2) = App.Path & "\" & TheCmds(2)
            End If

            ConvertVisToImg TheCmds(1), TheCmds(2), PageNum
        Else
            ' no good - need an in and out file
        End If
    End If

End Sub

Function ConvertVisToImg(ByVal InVisPath As String, ByVal OutImgPath As String, PageNum As Long) As Boolean
    ConvertVisToImg = True
    On Error GoTo PROC_ERR

    ' create a new visio instance
    Dim VisApp As Visio.Application
    Set VisApp = CreateObject("Visio.Application")

    ' open invispath
    Dim ConvDoc As Visio.Document
    Set ConvDoc = VisApp.Documents.OpenEx(InVisPath, visOpenRO + visOpenMinimized + visOpenHidden + visOpenMacrosDisabled + visOpenNoWorkspace)

    ' export to outimgpath
    If Not ConvDoc.Pages(PageNum) Is Nothing Then
        ConvDoc.Pages(PageNum).Export OutImgPath
    Else
        MsgBox "Invalid export page"
        ConvertVisToImg = False
        GoTo PROC_END
    End If

    ' close it off
PROC_END:
    On Error Resume Next
    VisApp.Quit
    Set VisApp = Nothing
    Exit Function
PROC_ERR:
    MsgBox Err.Description & vbCr & "Num:" & Err.Number
    GoTo PROC_END
End Function

Function SplitCommandArg(ByRef Commands() As String) As Boolean
    SplitCommandArg = True
    'read through command and break it into an array delimited by space characters only when we're not inside double quotes
    Dim InDblQts As Boolean
    Dim CmdToSplit As String
    CmdToSplit = TheCmd 'for debugging command line parser
    'CmdToSplit = Command
    Dim CharIdx As Integer
    ReDim Commands(1 To 1)
    For CharIdx = 1 To Len(CmdToSplit)
        Dim CurrChar As String
        CurrChar = Mid(CmdToSplit, CharIdx, 1)
        If CurrChar = " " And Not InDblQts Then
            'add another element to the commands array if InDblQts is false
            If Commands(UBound(Commands)) <> "" Then ReDim Preserve Commands(LBound(Commands) To UBound(Commands) + 1)
        ElseIf CurrChar = Chr(34) Then
            'set InDblQts = true
            If Not InDblQts Then InDblQts = True Else InDblQts = False
        Else
            Commands(UBound(Commands)) = Commands(UBound(Commands)) & CurrChar
        End If
    Next CharIdx
End Function
like image 95
Jon Fournier Avatar answered Sep 26 '22 05:09

Jon Fournier


F# 2.0 script:

//Description:
// Generates images for all Visio diagrams in folder were run according to pages names
//Tools:
// Visio 2010 32bit is needed to open diagrams (I also installed VisioSDK32bit.exe on my Windows 7 64bit)

#r "C:/Program Files (x86)/Microsoft Visual Studio 10.0/Visual Studio Tools for Office/PIA/Office14/Microsoft.Office.Interop.Visio.dll"

open System
open System.IO

open Microsoft.Office.Interop.Visio

let visOpenRO = 2
let visOpenMinimized = 16
let visOpenHidden = 64
let visOpenMacrosDisabled = 128
let visOpenNoWorkspace = 256

let baseDir = Environment.CurrentDirectory;

let getAllDiagramFiles = Directory.GetFiles(baseDir,"*.vsd")

let drawImage fullPathToDiagramFile = 
    let diagrammingApplication = new  ApplicationClass()
    let flags = Convert.ToInt16(visOpenRO + visOpenMinimized + visOpenHidden + visOpenMacrosDisabled + visOpenNoWorkspace)
    let document = diagrammingApplication.Documents.OpenEx(fullPathToDiagramFile,flags)
    for page in document.Pages do
        let imagePath = Path.Combine(baseDir, page.Name + ".png")
        page.Export (imagePath)
    document.Close()
    diagrammingApplication.Quit()   

let doItAll =     
    Array.iter drawImage getAllDiagramFiles

doItAll
like image 21
Dzmitry Lahoda Avatar answered Sep 26 '22 05:09

Dzmitry Lahoda