9

在工作中,我们非常广泛地使用 Visio 绘图作为文档支持。不幸的是,vsd 文件不能很好地与我们的 wiki 或文档提取工具(如 javadoc、doxygen 或 naturaldocs)配合使用。虽然可以手动将 Visio 文件转换为图像,但保持图像最新只是一件麻烦事,而且图像文件必然会过时。让我们面对现实吧:在修订控制中生成文件感觉很不对劲。

所以我正在寻找一个命令行工具,可以将 vsd 文件转换为 jpeg、png、gif 或任何可以转换为浏览器可以显示的图像的图像。最好它会在 unix 下运行,但只在 windows 下也可以。我可以处理自动化链的其余部分、cron 作业、图像到图像的转换以及 ssh、scp、多个文件等。

这就是我求助于你的原因:我找不到这样的工具。我认为我什至买不起这样的工具。我的 Google-fu 完全关闭了吗?你能帮助我吗?

我的意思是,它必须是可能的。必须有一种方法可以通过 COM 连接到 Visio 并将其保存为图像。顺便说一句,我正在使用 Visio 2007。

提前致谢。

4

4 回答 4

7

我使用 VB6 快速拼凑了一些东西,您可以从以下网址下载:http: //fournier.jonathan.googlepages.com/Vis2Img.exe

您只需传入输入 visio 文件路径,然后是输出文件路径(基于文件扩展名的 visio 导出)和可选的要导出的页码。

这里也是我使用的源代码,如果你想弄乱它或者把它变成一个 VBScript 什么的,它应该可以工作,尽管你需要完成将它转换为后期绑定代码。

希望有帮助,

乔恩

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
于 2009-07-20T16:03:57.240 回答
4

F# 2.0 脚本:

//描述:
// 为文件夹中根据页面名称运行的所有 Visio 图表生成图像
//工具:
// 需要 Visio 2010 32bit 才能打开图表(我还在 Windows 7 64bit 上安装了 VisioSDK32bit.exe)

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

开放系统
打开 System.IO

打开 Microsoft.Office.Interop.Visio

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

让 baseDir = Environment.CurrentDirectory;

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

让 drawImage fullPathToDiagramFile =
    让 diagrammingApplication = new ApplicationClass()
    让标志 = Convert.ToInt16(visOpenRO + visOpenMinimized + visOpenHidden + visOpenMacrosDisabled + visOpenNoWorkspace)
    让文档 = diagrammingApplication.Documents.OpenEx(fullPathToDiagramFile,flags)
    对于 document.Pages 中的页面
        让 imagePath = Path.Combine(baseDir, page.Name + ".png")
        page.Export (imagePath)
    文档.关闭()
    图表应用程序.Quit()   

让做这一切=     
    Array.iter drawImage getAllDiagramFiles

全力以赴
于 2011-05-25T11:06:15.860 回答
-1

您可以尝试“Visio to image”转换器

http://soft.postpdm.com/visio2image.html

使用 MS Visio 2007 和 2010 测试

于 2016-01-11T14:59:25.153 回答
-2

There has to be a way to hook into Visio with COM and get it to save as image.

Why not try writing something yourself, then, if you know how to use COM stuff? After all, if you can't find anything already made to do it, and you know you can figure out how to do it yourself, why not write something to do it yourself?

EDIT: Elaborating a bit on what I stated in my comment: writing a script of some sort does seem to be your best option in this situation, and Python, at least, would be quite useful for that, using the comtypes library found here: http://starship.python.net/crew/theller/comtypes/ Of course, as I said, if you prefer to use a different scripting language, then you could try using that; the thing is, I've only really used COM with VBA and Python at this point (As an aside, Microsoft tends to refer to "Automation" these days rather than specifically referencing COM, I believe.) The nice thing about Python is that it's an interpreted language, and thus you just need a version of the interpreter for the different OSes you're using, with versions for Windows, OSX, Linux, Unix, etc. On the other hand, I doubt you can use COM on non-Windows systems without some sort of hack, so you may very well have to parse the data in the source files directly (and even though Visio's default formats appear to use some form of XML, it's probably one of those proprietary formats Microsoft seems to love).

If you haven't used Python before, the Python documentation has a good tutorial to get people started: http://docs.python.org/3.1/tutorial/index.html

And, of course, you'll want the Python interpreter itself: http://python.org/download/releases/3.1/ (Note that you may have to manually add the Python directory to the PATH environment variable after installation.)

When you write the script, you could probably have the syntax for running the script be something like "python visioexport.py <source/original file[ with path]>[ <new file[ with path]>]" (assuming the script file is in your Python directory), with the new file defaulting to a file of the same name and in the same folder/directory as the original (albeit with a different extension; in fact, if you wish, you could set it up to export to multiple formats, with the format defaulting to that of whatever default extension you choose and being specified by an alternate extension of you specify one in the file name. As well, you could likely set it up so that if you only have the new file name after the source file, no path specified, it'll save with that new file name to the source file's directory. And, of course, if you don't specify a path for the source file, just a file name, you could set it up to get the file from the current directory).

On the topic of file formats: it seems to me that converting to SVG might be the best thing to do, as it would be more space-efficient and would better reflect the original images' status as vectored images. On the other hand, the conversion from a Visio format to SVG is not perfect (or, at least, it wasn't in Visio 2003; I can't find a source of info similar to this one for Visio 2007), and as seen here, you may have to modify the resultant XML file (though that could be done using the script, after the file is exported, via parts of the Python standard library). If you don't mind the additional file size of bitmaps, and you'd rather not have to include additional code to fix resultant SVG files, then you probably should just go with a bitmap format such as PNG.

于 2009-07-17T20:34:50.513 回答