News:

BB code in posts seems to be working again!
I haven't turned on every single tag, so please let me know if there are any that are used/needed but not activated.

Main Menu

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.

Thomas Winkel

Hi Yacine, sorry I didn't find the time testing your notebooks yet. And now we're on our honeymoon for the next three weeks... 🥰
But this is a very exciting topic. Not only for Visio, but also for any other software with a COM interface.
I'll be back in a few weeks 8)

Yacine

#16
Now we're talking. How could we use Visio in our Honeymoon.
Wrong answers only, please. ;)

P.S.: sure it's yours, but we're anxiously following along and holding our thumbs for you!
P.P.S.: Python would be a plus!
Yacine

Yacine

#17
PPPS - COM, absolutely! I use it on simple text files, Excel of course, but also Word.
The thing that did not work at all were Access Databases. Some 32/64 bit issue, that I absolutely could not solve.


Bottom line: All the editings that are too big to do by hand and too seldom to write a right program for, are wonderfully handled by Jupyter Notebooks.

Enjoy you Honeymoon, and greetings from me and the whole Visguy core team to your wife. (I am sure no one will contradict).
Yacine

Surrogate

Quote from: Yacine on May 14, 2024, 09:18:51 PMEnjoy you Honeymoon, and greetings from me and the whole Visguy core team to your wife.
Absolutely! Thomas my congratulations to you!!!
Quote from: Yacine on May 14, 2024, 09:18:51 PMPPPS - COM, absolutely!
I've made timid attempts to use Python in AutoCAD.

Thomas Winkel

Thanks, friends ;D

I tested VSCode with Jupyter extensions.
And I prefer this over JupyterLab Desktop because it feels much more responsible.
Also the IntelliSence feature is more comfortable: No [Tab] required, not case sensitive

Yacine, have you thought about your own package on pypi.org?
* Dependencies for pywin32 and jupyter
* Initialization for IntelliSence
* Modules for Visio, Excel, etc. for easy startup


Btw.
What's wrong with the forum? I cannot access the main page.

wapperdude

Yep.  Main page has issue. Requires Visio Guy fix.
Visio 2019 Pro

Yacine

Quote from: Thomas Winkel on June 19, 2024, 04:42:57 PMThanks, friends ;D

Now we deserve at least one wedding picture.  ;)

Quote from: Thomas Winkel on June 19, 2024, 04:42:57 PMI tested VSCode with Jupyter extensions.
And I prefer this over JupyterLab Desktop because it feels much more responsible.
Also the IntelliSence feature is more comfortable: No [Tab] required, not case sensitive

Yacine, have you thought about your own package on pypi.org?
* Dependencies for pywin32 and jupyter
* Initialization for IntelliSence
* Modules for Visio, Excel, etc. for easy startup
I got familiar with Jupyter, but that's  a matter of preference. I understand that the autocompletion is a big deal. I don't use it that much myself.

Pypi? Well, maybe yes. At the end of the day I don't use all the functions I wrote - simply because I don't remember them. The only really useful thing is the declaration of the constants, but I believe there is a smarter way to automate this.
The only thing beside these constants is the definition of the main objects: vApp, vDoc, vPg, vWin. From there on you can do everything as you would in VBA.

But true, having the helper function as import would be better than what copying it to the current folder. (I'm too lazy).

Quote from: Thomas Winkel on June 19, 2024, 04:42:57 PMBtw.
What's wrong with the forum? I cannot access the main page.

Yep, we've been discussing this issue in PMs. I'll add you to the discussion next time.
Yacine

Paul Herber

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

Moving now complete. Been in 3 weeks. Love it.  8)  8)  8)
Electronic and Electrical engineering, business and software stencils for Visio -

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

Yacine

#23
@Thomas, es ist vollbracht!

pip install visio_library

The name is probably not so well chosen, because "library" is a very big word for the 2 functions it provides.
But ... the minimalistic feature scope is on purpose. (I tried indeed to add GUI dialogs and all sorts of fancy stuff, just to be remembered by myself how little of these features I actually used to use. )
The module is meant for people knowing Visio's object model and it does only 2 things:
Retrieve all the loaded documents in all Visio instances (that was a big hurdle) - loaded_docs()
This prints out the document names with their index and it returns this list as list of document objects.

vInit(index, globals_dict=globals()) instantiates the 4 basic variables for working with Visio - vApp, vDoc, vPg and vWin (the names are self explanatory).
Index is the index of the document as loaded_docs displays.

If for any reason, you need to work with more than one document at once, you can use the suffix parameter to instantiate different the variables with a suffix.
vInit(1, globals_dict = globals(), suffix = "2") will create vApp2, vDoc2, Vpg2 and vWin2.


You can leave the index empty, then a new doc is created. And with the optional parameter "Template", you can specify the template to use.

You can also write the fullname of your visio file as parameter, this will open this file for you.

Extra feature, when you instantiate the doc, the variable "c" for constants is instantiate as well. It holds all the Visio constants.
Usage shp.AddNamedRow(c.visSectionUser, "myUserCell", c.visTagDefault)
Or simple test: print(c.visSectionProp)

That is it - only a tool to get a handle on Visio's base objects.

Now, happy coding.

from visio_library import loaded_docs, vInit

loaded_docs()
# to display your open Visio files

vInit(0, globals_dict = globals())
# connects to the first document as displayed above.

shps = [shp for shp in vPg.Shapes]
for shp in shps:
  print(shp.ID)
  shp.Cells("PinX").Formula = int(shp.Cells("PinX").ResultInt("mm")*10)/10 + "mm"
  shp.Cells("PinY").Formula = int(shp.Cells("PinY").ResultInt("mm")*10)/10 + "mm"

Thank you Thomas, for having pushed me. ;)

PS: Next step would probably be to set up a GitHub project and a manual.
Yacine

Yacine

#24
Now that the base is set. I want to elaborate on helper functions.
The base library is intended for people who know Visio's object model and will be very fast when operating with the provided handles on the base objects.

The helper functions however will be a set functions, that are beyond the usual and fast manipulation of the Visio objects. Things you don't do every day and forget about how to do it.

Here is a list of thoughts regarding the helper functions:

Creating rows in the user and prop section, with all necessary tests.
Not a big deal in itself, but handling all the ifsectionexsists, ifcellexists, adding the label to prop rows (important but often forgotten), and subtle stuff like the sort order, the visibility and the like.
This part would also include checking for values depending on the prop row type, which is not alway obvious.
Also handling different unit types in Visio (UI - universal units, inches and millimeters)

batch processes for fast processing - visio has these batch operations where you pass an array with shape IDs, cell address and so on. You can read an write to such arrays. Very fast, but also very sensitive to variable section structures (prop, user, geometriy and so on)

transform shapesheet cells in properties - thats a neat one. Instead of writing shape.Cells("Width").ResultUI and .Formula you just address the cell by shape.Width as getter and setter. More difficult than you would expect, but I worked a solution out for it.

SetGetStates - that is neat and IMPORTANT. You have basically a rather complex drawing and set highlights of different kinds and different texts in shapes depending on scenarios. You begin be saving the original state, then for each scenario you modify certain properties of shapes (color, line weight, ... what ever), store the modifications in a file (Excel, JSON or other), then you generate report documents by switching from state to state, exporting to bitmap. This is actually a very big deal, because these modifications don't depend on a certain state of the document. The states adjust themselves to what ever the drawing evolves into. Absolutely not to neglect when handling big documents and defining such scenarios. Drawing over existing documents is a dead end.

SmartShapeManager - also a neat one. You basically compare a smartshape to a virgin shape and document the differences. This documentation can then be used as definition of the smartshape and can be used to build such a shape up from the definition. The definition being a Excel or JSON file can also be edited separately to make adjustments. Important and definiteley a missing in the base functionality of Visio.

LayerManager
The layer managment in Visio is poor when compared to Autocad and the like.

PythonVBA translater - Python being so fast and good, it is my primary tool for fast prototyping. When it comes to distributing code, VBA is better because I can embed it in templates and stencils. Converting Python to VBA is however not trivial to myself. A converter would be nice.

I would be very interested on getting your opinion on the points and suggestions what I may or should add (or remove).
What are features, that you wish were in the standard features set of Visio?
Yacine

Thomas Winkel

Hi Yacine, that is good news 8)

But I was not able to get it running on my installation:
* Create a blank drawing and add some shapes
* Tried your demo code above -> Error
* Save the drawing and tried again -> Different Error

Then I tried to debug, but this pythoncom stuff is too complicated for me.
At least I found out that visio_clsid differs for my installation (Visio 2019 Professional).
See all outputs as well as the ID comparison attached.

I think the name visio_library is too Visio specific and too serious.
Imagine you will integrate other tools in future.
For example generate Visio documents, pages, shapes based on Excel tables.
Or create an email with the current Visio document attached as PDF.
You need something more fancy. Maybe a funny acronym or a cute pet name ;D

Stay tuned 👍


P.S. wedding picture as PM ;D

Yacine

#26
Changed name to visiopy.
pip install visiopy

from visiopy import loaded_docs, vInit

loaded_docs()
# if the list is not empty use one of the displayed indices

vInit(index=0,globals_dict=globals())

-->
Instantiated the variables vApp, vDoc, vPg and vWin for the document xxxx, as well as the variable c for the Visio constants
(<win32com.gen_py.Microsoft Visio 16.0 Type Library.IVApplication instance at 0x1586341931984>, <win32com.gen_py.Microsoft Visio 16.0 Type Library.IVDocument instance at 0x1586341931664>,
<win32com.gen_py.Microsoft Visio 16.0 Type Library.IVPage instance at 0x1586341931920>,
<win32com.gen_py.Microsoft Visio 16.0 Type Library.IVWindow instance at 0x1586341981328>)

print(vDoc.Name)
Yacine

Visio Guy

We've got the forum's home page working again, but had to disable bulletin board code to do this. Until we figure out what's really wrong, you bold, youtube, ,color, italic, url codes won't work properly.
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Yacine

#28
Danke für die Hilfe Chris.
Yacine

Thomas Winkel

Now it works for saved documents:
0: C:\Temp\Drawing1.vsdx
Instantiated the variables vApp, vDoc, vPg and vWin for the document Drawing1.vsdx,
as well as the variable c for the Visio constants
(<COMObject <unknown>>,
 <COMObject <unknown>>,
 <COMObject <unknown>>,
 <COMObject <unknown>>)

But for unsaved documents there is still an error.
Here the ID compare:
       name = '!{00021A20-0000-0000-C000-000000000046}'
visio_clsid = '{{00021A98-0000-0000-C000-000000000046}}'

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 572 (show)
Files included: 32 - 1207KB. (show)
Memory used: 1367KB.
Tokens: post-login.
Cache hits: 15: 0.00289s for 26,728 bytes (show)
Cache misses: 5: (show)
Queries used: 17.

[Show Queries]