Python ... again

Started by Yacine, May 01, 2024, 11:25:25 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Yacine

Hi everyone,

I'm back with a topic that might seem a bit off-center for Visio, but hear me out!
Many of us here are engineers or tech enthusiasts, so I wanted to share an experience that might be relevant.

I was recently tasked with mass balance calculations for a large project.  After a decade away from this area, I needed to refresh my skills. Looking for alternatives to Excel, I explored using Python with Jupyter Notebooks.

During my library search, I discovered "handcalcs" and "forallpeople." These fantastic libraries generate human-readable calculation notebooks with unit checking!
https://github.com/connorferster/handcalcs
https://github.com/connorferster/forallpeople

For those interested, there's an excellent YouTube video by the creator of handcalcs that showcases intelligent engineering in the new millenium.
https://www.youtube.com/watch?v=FtzVN3s4yRI&ab_channel=ConnorFerster

One key takeaway from the video is the availability of Jupyter Lab as a desktop application. Here's where the connection to Visio comes in. While Jupyter Lab might be too complicated to install, the desktop version could be a game-changer for "Visioneers" wanting to manipulate Drawings the Python way!
https://github.com/jupyterlab/jupyterlab-desktop

(and the generic source for jupyterlab: https://github.com/jupyterlab/jupyterlab)

Perhaps Jupyter Lab for desktop can bridge the gap and encourage more visioneers to leverage Python's power alongside Visio.

Happy May 1st!

Yacine
Yacine

Robert K S

Can you explain how Python/Jupyter could be used to manipulate Visio drawings?

Yacine

#2
I've written several posts on the subject.
https://visguy.com/vgforum/index.php?topic=9883.0
https://visguy.com/vgforum/index.php?topic=10259.0
https://visguy.com/vgforum/index.php?topic=9774.0
https://visguy.com/vgforum/index.php?topic=9810.0 - specially this one lacks from the fact that the forum doesn't show uploaded images anymore.
https://visguy.com/vgforum/index.php?topic=9216.0
https://visguy.com/vgforum/index.php?topic=8584.0
http://visguy.com/vgforum/index.php?topic=9884.0

In short:
- win32com gives you access to visio's object model, as VBA and C# do.
- Python is way faster in prototyping code than its competitors
- Jupyter Lab with its capability to run small chunks of code makes it an ideal REPL tool for Visio.

I use it mainly for manipulating data of P&IDs and PFDs, but also for fun stuff, like the Piet post, or this one
https://visguy.com/vgforum/index.php?topic=9859.0
where I generate randomly flowers.
Yacine

Paul Herber

Quote from: Yacine on May 01, 2024, 06:55:35 PMthe forum doesn't show uploaded images anymore.

Click the Reply button and you can drag-and-drop images.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Yacine

#4
So I would probably need to edit my old posts?

P.S.: Yes, I tried it and it actually works better than the previous one. Now you can place the images where you want them to be.

P.P.S.: Welcome back, moving completed?
Yacine

Paul Herber

Quote from: Yacine on May 01, 2024, 07:06:55 PMP.P.S.: Welcome back, moving completed?

Haven't been away, just hovering. Not moved yet, maybe a couple of weeks.
P.S. When we move my Location will change.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

wapperdude

Take your location with you.  👀😲🙄😆
Visio 2019 Pro

Thomas Winkel

#7
Hi Yacine,

interesting topic.
My first contact to Python is the Online Shape Library because I decided to use Flask on server side.
There I also use win32com.client to fill the database because I thought it could be easier to access Visio from Python than accessing the database from VBA (and because I wanted to try it out).
For this project I use VSCode.

I think JupyterLab is a great MATLAB replacement for data analysis, control engineering and scientific calculations.
Thus, everywhere plots or descriptive flows are required for presentation.
But at the moment I do not see a benefit for Visio automation here...?

For me the main barrier using Visio with Python is the lack of IntelliSence as it comes with the VBA Editor or Visual Studio:
Auto-complete, function/parameter description, etc..
In VBA you can even edit the code or change the current execution line while debugging!
For me that makes VBA the best prototyping platform (if Visio is the focus).

On the other hand VBA comes to limits as the project grows or for "professional" projects.
It's not good for collaborative work on the same (binary) files.
Also there are no reasonable solutions for things like DataGridView, TreeView, Newtonsoft-JSON, etc. available.

So, in my opinion:
* If it's satisfying go with VBA
* If VBA is not satisfying go with a .NET AddIn
* Everything else to integrate Visio in another environment (depending on your project and skills)

Anyway, I really like Python and am currently spending some time learning how to use it for server-side applications.

Yacine

Well, when I write REPL, I mean that I use the Jupyter Notebook as a line command tool.

Say I want to select the valves that belong to machine x1

vWin.DeselectAll()
for shp in vPg-Shapes:
  if shp.CellExists("prop.machine", False):
    if shp.Cells("prop.machine").ResultStr("") == "x1":
      vWin.Select(shp, visSelect)

Now this is not very impressive.

But it if I were to need that again, I can either repeat running the cell (The notebook is still open and I can navigate it)
Or I can decide to make a function out of the previously entered code, just be adding a def and a function name.

I may as well decide to parameterize this function, so I would replace the hard coded "machine" and "x1" by variables and use the function as generic selection tool for different prop fields and values.

Basically in such scenario I don't start with the idea to write a program, but use simple code lines for repetitive tasks. Only when functions proof their usefulness, I move them to a library for later re-use. "The program writes itself".

It is specially the data structures in Python that makes it so attractive.

Having collected all relevant shapes by a snippet like above, I could then easily define the list of valves belong to x1:
valves_x1 = [shp for shp in shps if prop_is(shp,"machine","x1") and prop_is(shp,"type","valve") ]
That is a one-liner that would require quite a bunch of code in VBA.



Yacine

Thomas Winkel

Ahh... you use Python as a "Visio Terminal". That's a great idea indeed!
I will try that :)

Thomas Winkel

Really cool, but I definitely need auto-complete.
Maybe this could be added by loading some type-library..?
You cannot view this attachment.

Thomas Winkel

#11
Autocomplete:
You cannot view this attachment.

Notes:
* Hit [Tab] after obj.
* Enter characters to filter, hit [Enter] to apply
* Filter is case sensitive
  -> app.ac + [Tab] -> no results
  -> app.Ac + [Tab] -> works
* Objects must be active
  -> I.e. the example in the screenshot only works if you run the cell before using "win"

More info here:
https://stackoverflow.com/questions/26990655/how-to-have-win32com-code-completion-in-ipython

Edit:
Attached a template Notebook.

Yacine

#12
Thomas, I gotta up my game, you're showing me up!

I actually wanted to upload some youtube stuff, but you're way faster.

And this youtube requires so much right speech and editing.

I'll rather upload some commented notebooks. Hoping they'll carry the intention as well.
Yacine

Yacine

#13
Second upload post.

more to follow ....
Yacine

Surrogate

#14
Quote from: Yacine on May 04, 2024, 08:38:39 AMI'll rather upload some commented notebooks.
You can upload the notebooks to GitHub for easy viewing. Users do not need to download notebooks for viewing and do not need to have specific software to view ipynb files.

As an example of one of my published notebooks...