Author Topic: Do you forsee any problems with using Python to automate diagram creations ?  (Read 7510 times)

0 Members and 1 Guest are viewing this topic.

PF4VisioGuy

  • Full Member
  • ***
  • Posts: 100
This is related to a previous post and to my efforts to put in a diagram nodes (graph) and their position that was already calculated with Graphviz
Neato is the algo used for this and the result is a .dot file that contain node attributes and their position in the picture (the neato file contains the size of the paper (WxH) and DPI info)

My plan is to use Python and a Windows Com library to create the docs
Two questions:
-do you foresee any problems with this approach?
-what book or tutorials should I read in order to familiariaze myself with the COM model for VIsio?

You might wonder why not VBA. it is because I know Python and I use it to collect the connectivity information and to produce PDFs, DOT files and so on so I want to keep everything together

Nikolay

  • Hero Member
  • *****
  • Posts: 1144
    • UnmanagedVisio
Well, it is technically possible to do with python everything you could do with VBA, it will just take much more time.
Also, your solution will run much slower than VBA solution.
But if you do not care about time and money, you can go with python.

Oh, wait. The questions.

Quote
-do you foresee any problems with this approach?

- Most probably you will not have auto-complete or parameter information from the python IDE
- You cannot use Visio macro recorder since it generates VBA
- Most code samples are in VBA or C#, so it's hard to find proper examples
- Since python is running out-of-the-process (python.exe) the code will run times slower than VBA
- You may face some troubles if you want to use some non-automation-compatible Visio methods (there are such). Example: SaveAsWeb is not automation-compatible.
https://stackoverflow.com/questions/49944199/save-visio-document-as-html (requires you to generate a proxy using "import comtypes" instead of "import win32com.client")

Quote
-what book or tutorials should I read in order to familiariaze myself with the COM model for VIsio?

- The free beginner's book from the Micrsofot is good ("Developing Visio Solutions")
Other than that, look at this topic: http://visguy.com/vgforum/index.php?topic=791.0
« Last Edit: August 21, 2018, 01:01:30 PM by Nikolay »

Yacine

  • Hero Member
  • *****
  • Posts: 3005
I'm actually playing with the same idea - using jupyter notebooks to remote control visio.
I've seen many tutorials demonstrating awesome stuff with excel. It's a very trivial conclusion to try to do the same with visio.

As of my current opinion about these notebooks,
- they bring a convenient way to solve everyday problems step by step,
- they can be saved as to replicate the steps for later usage,
- their functionality can be improved over time, as they are used,
- python is far superior to VBA in matters of OOP, easy and clean coding, ...
- there are so many libraries for so many problems that it will be difficult not to find the right one for yours


These Jupyter notebooks have the potential to de-throne excel as tool of choice for regular engineers ... and for visio users.
My ideas for this project would be to begin implementing a comfortable set of "visio.utils": getAllShapes, getCellValue, set..., selectShapes(where, criteria, ...), and so on.
Speed is absolutely not an issue. Most of the problems I encoubter in everyday practice are related to finding the right solution for a problem. Very few relate to speed issues. When the latter occurs, I can optimize the solution ...  and there are so many ways to do it.

Having an interactive tool would really be a plus for visio.

The forum regulars probably know my shapes manipulations tools and my efforts in creating a generic solution for graphs à la 3D.js. "Jupyter-Visio" could be a more intuitive way to do the same.


I encourage everyone in the forum to investigate Jupyter, Anaconda and JupyterLab.
« Last Edit: August 21, 2018, 01:33:47 PM by Yacine »
Yacine

PF4VisioGuy

  • Full Member
  • ***
  • Posts: 100
Totally agree with the speed non-issue, especially if it is for small
I do need to read about these Jupyter notebooks, they are becoming quite popular and I have no clue what they are and how they can be used

Thank you to both of you for the feedback

Yacine...can you please point me to your activity in regards to adding D3.js like functionality to Visio?
I must say that it is on my to do list to learn D3.js, I am fascinated by its beauty and simplicity and elegancy

PF4VisioGuy

  • Full Member
  • ***
  • Posts: 100
Here is some old code I have come across while looking for examples

https://technofrustration.blogspot.com/2007/10/using-python-to-automate-visio.html

Yacine

  • Hero Member
  • *****
  • Posts: 3005
Yacine...can you please point me to your activity in regards to adding D3.js like functionality to Visio?

http://visguy.com/vgforum/index.php?topic=8018.0
Yacine

Nikolay

  • Hero Member
  • *****
  • Posts: 1144
    • UnmanagedVisio
Wouldn't it be more reasonable to go with C#.. (you don't want VBA, I got it)?

Check out the articles regarding LINQPAD from the John Goldsmith:
http://visualsignals.typepad.co.uk/vislog/2015/12/getting-started-with-c-in-linqpad-with-visio.html
http://visualsignals.typepad.co.uk/vislog/2018/04/using-utildif-in-linqpad-with-visio.html

You will have at least autocomplete (intellisense) and the constants ;)

Yacine

  • Hero Member
  • *****
  • Posts: 3005
Nice hint indead.
Yacine

PF4VisioGuy

  • Full Member
  • ***
  • Posts: 100
@NIkolay -sorry but C# is not an option for me, I already have quite a good amount of code in Python so the Win23com route is mandatory for me
and the whole thing should be fairly simple: add shapes to the diagram at the XY coordinates precalculated by Neato (graphviz) and then connect these shapes with connectors as per the DOT file

Nikolay

  • Hero Member
  • *****
  • Posts: 1144
    • UnmanagedVisio
Okay, I see. "You are your own worst enemy" :))

Anyways. The python code should be more or less equivalent to the vbscript code; also the number of python examples is above zero.

Creating a new diagram, and placing shapes should not be difficult, you just need to rescale the coordinates properly.

Placing connectors may be (much) more complicated. They are described as SPLINEs in the GraphViz output, as far as I remember (are they not?).
Converting those SPLINEs to Visio NURBs may be a lot of fun. We had a similar topic on SVG ARC (not curve!) conversion, you could check that one out.
http://visguy.com/vgforum/index.php?topic=7408.15 In that case, only Junichi-san could do it. I hope it is not that bad but it's always better to be prepared for the worst :)

Also, in case you want to convert connectors,  you probably want to use fixed connectors (not-auto-routed).

If your diagrams are simple, you can try shape.AutoConnect, it will auto-generate Visio connector but ditch (ignore) all the layout for the corresponding connector done by graphviz.

I'm also wondering, what Microsoft guys could say regarding feeding the Data Visualizer from a ".dot" file? Too nerdy?
« Last Edit: August 22, 2018, 02:39:02 PM by Nikolay »

vojo

  • Hero Member
  • *****
  • Posts: 1678
several simple python <=> visio examples...just google "visio python"   and select verbatim.
from what I saw, the trick to pipe the right win32 library into python instance and do a few python lines in your code
to establish  pythin <=> vsio bridge.

you also could go down the path of writing a python program to create an xml file for your drawing then let visio read it in
and go from there.  Problem more lifting to do this

PF4VisioGuy

  • Full Member
  • ***
  • Posts: 100
@Nikolay; you do not need to convert the splines, just get their attributes and ignore the coordinates
So if shape1 has X1Y1 coordinates and shape2 has X2Y2 I am thinking to connect these two with a connector for which I set the attributes as per the dot file (if I want a fancy graph)

@vojo yes probably if I want to keep it simple I could use the XML approach... have to see which one is easier

Nikolay

  • Hero Member
  • *****
  • Posts: 1144
    • UnmanagedVisio
@Nikolay; you do not need to convert the splines, just get their attributes and ignore the coordinates

This is the "shape.AutoConnect" approach I mentioned... Sure you can give it a try!
My concern is that sometimes Visio may be not that good at guessing the routes, so a relatively complex diagram can turn into a "crow's nest".

PF4VisioGuy

  • Full Member
  • ***
  • Posts: 100
I am ok with having to rearange the shapes after I put them on the page and connect them with sticky connectors.
Neato (Graphwiz) is not always successful in finding the best layout...

Have you guys came across of any VBA example that does this...: creates the doc, adds two shapes and connect them as above mentioned ?

Nikolay

  • Hero Member
  • *****
  • Posts: 1144
    • UnmanagedVisio
This question seems to do that, in python:
https://stackoverflow.com/questions/43119001/how-to-add-a-link-to-a-visio-document-via-python

Example for the AutoConnect (easier to use than glueTo but less functional) is right in the documentation
https://docs.microsoft.com/en-us/office/vba/api/visio.shape.autoconnect

BTW, Visio has "layout shapes" command that provides some decent layout functionality. And the data visualizer, that can create diagrams from data using graph layout algorithm. It is not neato, but for common case - flowcharts or flowcharts with restrictions (lanes) - it is even better. Have you tried these?
« Last Edit: August 23, 2018, 02:16:45 AM by Nikolay »