Visio Guy

Visio Discussions => Programming & Code => Topic started by: Miki on May 20, 2015, 07:23:44 PM

Title: Error in getting shape's coordinates using vba
Post by: Miki on May 20, 2015, 07:23:44 PM
Hello,
I have a macro (I found it on a forum by Russell Christopher) that gets XY coordinates of a shape path for all the shape and creates a *.csv file for the output.
It works perfectly when the origin is set at default (lower left corner of the page). But when I change the origin to some other location, it outputs the coordinates such that the origin is still at the default location. Is there a way to change this?

Here is the code:
Sub PlotPoints()
    Dim xPS As Visio.Shape
    Dim xPath As Visio.Path
    Dim dPoints() As Double
       
    'Set the output file to be the same as the source file, but .CSV
    xFileName = Left(ActiveDocument.FullName, Len(ActiveDocument.FullName) - 4) & "csv"
   
    'Set the separator character
    xSep = "|"
   
    'If file already exists, delete it
    If Dir(xFileName) <> "" Then
        Kill xFileName
    End If
   
    'Open the output file and write the header line
    xFile = FreeFile
    Open xFileName For Append As #xFile
    Print #xFile, "ShapeNo" & xSep & "PathNo" & xSep & "PointNo" & xSep & "X" & xSep & "Y"
   
    'Get all the shapes on the page
    ActiveWindow.SelectAll
    Set xShapes = ActiveWindow.Selection


    'Cycle through the shapes
    For Each xPS In xShapes
       
        'Shapes can have multiple paths
        For j = 1 To xPS.Paths.Count
            Set xPath = xPS.Paths(j)
           
            'Enumerate the points in each path with 0.5 sensitivity for curves
            xPath.Points 0.5, dPoints
            i = 0
            Do While i < UBound(dPoints)
                x = Int((dPoints(i)) / 12)
                y = Int((dPoints(i + 1)) / 12)
                i = i + 2
               
                'Write the record for each point
                Print #xFile, xPS.Index; xSep; j; xSep; i; xSep; x; xSep; y
            Loop
        Next j
    Next
   
    'Close the file and exit
    Close #xFile
End Sub


Any help is appreciated.

- Miki
Title: Re: Error in getting shape's coordinates using vba
Post by: Paul Herber on May 20, 2015, 11:24:37 PM
The ruler origin coordinates are stored in the page shapesheet cells XRulerOrigin/YRulerOrigin.
Title: Re: Error in getting shape's coordinates using vba
Post by: Miki on May 21, 2015, 01:18:54 PM
Paul,
Yes, I looked at XRulerOrigin and YRulerOrigin in the page's shapesheet and they show where my origin is. However, when I run the macro, the macro does not recognized those origin coordinates, it outputs the XY coordinates of the shapes based on default origin location at the lower left corner of the page.
I don't understand why it is doing that.
Any comments?

- Miki
Title: Re: Error in getting shape's coordinates using vba
Post by: Paul Herber on May 21, 2015, 01:28:37 PM
You have to subtract the origin coordinates from the shape coordinates.
Title: Re: Error in getting shape's coordinates using vba
Post by: Miki on May 21, 2015, 01:45:09 PM
I think thats the last resort. Thanks Paul!

But why doesn't the code pick up the new coordinates for origin. The X & Y values in Size & Position window are correct, but not when the code gets the coordinates.
I this a bug in Visio?

-Miki
Title: Re: Error in getting shape's coordinates using vba
Post by: Paul Herber on May 21, 2015, 02:41:13 PM
Quite normal. The origin scales are only there as a visual aid. Imagine the horific consequences of every shape effectively having its location updated whenever the origin was changed. No.