eggmatters has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've looked over the win32::Visio module and it appears to have a lot of great tools for creating Visios but I didn't see any that will actually parse a Visio. I am curious if there is a function or set of functions in that module that will access, namely, text data within a shape. Basically, I want to pull out some text data, compare it with a matching value in a spreadsheet and exchange it with a corresponding value. I use the Win32::OLE for excel successfully but can't seem to find documentation regarding the same for Visio. As of now, I am just parsing the raw xml of the Visio but this has now become untenable due to formatting glitches in Visio.
  • Comment on How do I dicern and pull data elements from a Visio

Replies are listed 'Best First'.
Re: How do I dicern and pull data elements from a Visio
by CountZero (Bishop) on Feb 27, 2008 at 06:30 UTC
    I have never used Visio, so treat this "advice" with some caution, but cannot you use Win32::OLE with Visio?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How do I dicern and pull data elements from a Visio
by CountZero (Bishop) on Feb 27, 2008 at 06:45 UTC
    I had a look at the Visio module (if that is indeed the one you use) and the Visio::Shape package has the following code to set some text in a Visio-file:
    sub set_text { my $self = shift; my $text = shift; my $opts = shift; my $textN = Visio::generic_create_node($self->{shapeNode}, 'Text' ); my $textNode = $self->{xmldoc}->createTextNode($text); $textN->removeChildNodes(); $textN->appendChild($textNode); }
    However, there doesn't seem to be a corresponding get_text method.

    Also as you will see from the above code, it expects to get some options(?) in $opts which however are never used. Strange, definitely alpha code ...

    Update: You will find the official info for the XML-schema used for Visio, here. If everything else fails, you can indeed still use the various XML-tools on cpan to directly access the XML-nodes and attributes and change them.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Interesting. I've looked at the shapes module in CPAN and there certainly doesn't seem to be any 'get' methods for this. I've also found literally no compendium of OLE documentation for this available. The xml schema is well-formed enough for the purposes of Visio but not for parsing. It appears that they engage some strange encoding and it's a mess trying to escape it all. If I am successful, I will append a module to CPAN once all is said and done.
        If anybody is still tracking this thread: Ok, easy enough to answer. It was yet another thread on this site where I found it. Here is some code that got me started:
        use strict; use warnings; use Win32::OLE; $Win32::OLE::Warn = 3; my $path = "c:\\Documents and Settings\\eggmatters\\Desktop\\VisioProj +ect\\"; my $file = "viSamp.vsd"; my $Visio = Win32::OLE->new('Visio.Application', 'Quit'); my $VDocs = $Visio->Documents; my $VDoc = $VDocs->Open("$path$file"); my $VPage = $VDoc->Pages->Item(1); my $VShapes = $VPage->Shapes; my $VShape = $VShapes->Item(1); $VShape->{Text} = "New Name"; print $VShape->{Text}; $VDoc->SaveAs($path."test2.vsd");
        Here, I've assigned the text before dicerning it but that doesn't matter. To 'get' the text, you just need to call $VShape->(Text); and there you have it. For people like me who have to rely on Visio modeling of Web sites and applications, This will definitely be a huge bonus in implementing validation tools and chart editors.
Re: How do I dicern and pull data elements from a Visio
by Anonymous Monk on Feb 27, 2008 at 05:28 UTC
    As of now, I am just parsing the raw xml of the Visio but this has now become untenable due to formatting glitches in Visio.
    If it is no longer producing valid xml, you can use HTML::Parser
Re: How do I dicern and pull data elements from a Visio
by Anonymous Monk on Feb 27, 2008 at 05:27 UTC
    win32::Visio? Its not on CPAN.
      It is called Visio and is still at the alpha stage it seems.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        He said win32::Visio, not Visio