Recent Posts

Pages: [1] 2 3 ... 10
1
Quote
... but when I record a macro, they all are referred to by shape name.

??? 
You used macro recorder to verify the steps, albeit in VBA.  You were successful using the macro recorder to get code for dropping the shapes and placing them on drawing page?  All three shapes?
2
Doing the homework for you ...

You somehow need to make sure that the master you want to drop is available. Either start from a template or load the right stencil. Relying on the drawing might be a worse idea.
Find out which stencil contains the desired "diamond" master.
Open the stencil as you did for the document itself.
stencil = app.Documents.Open(r"... full path of stencil ...")
Then provided the masters are all in that one stencil, you could either define the different masters directly:
rectangle = stencil.Masters.Item("Rectangle")
...
diamond = stencil.Masters.Item("Diamond")
added_new_shape3 = page.Drop(diamond, 5.25, 5.75)



or build up some structure to hold them:
eg a lazy way to collect them all.
masters = {}
for mstr in stencil.Masters:
  masters[mstr.Name] = mstr

added_new_shape3 = page.Drop(masters['Diamond'], 5.25, 5.75)


PS: You might ask, why use variables for accessing the masters? That is actually not really necessary - you still can write the full original object path, app.Documents("stencil name").Masters.Item("Diamond") - but the intermediate definition of variables gives you the opportunity to catch errors (availability of the objects), to count and list the masters, ... and whatever you may discover to be helpful.
4
The deep reaches of the web may contain all sorts of incorrect code. Stay here, you know it makes sense!  8)
Unless you have already used a diamond shape in your document there won't be such a shape in the document masters. You need to load the shape from a suitable stencil.
5
Why can I not create a Diamond by name?  O
Your document contains masters named Rectangle, Circle and Diamond?
Code
# drop shape to the page from doc stencil by name "Master.4" at x,y coord.
added_new_shape1 = page.Drop(doc.Masters("Rectangle"), 6.5, 6.5)
6
Programming & Code / Python & Visio - can create Rectangle and Circle just fine, but...
« Last post by approx on December 10, 2023, 12:17:32 AM »
...not anything else!

Here's the python I am playing around with, a conglomeration of code from the deep reaches of the web.

Code
import win32com.client

app = win32com.client.Dispatch("Visio.Application")
app.Visible = True
#open Visio document and assign it to variable doc
doc = app.Documents.Open(r"c:\python\MyDrawing.vsdx")
page = app.ActivePage

# drop shape to the page from doc stencil by name "Master.4" at x,y coord.
added_new_shape1 = page.Drop(doc.Masters("Rectangle"), 6.5, 6.5)
added_new_shape2 = page.Drop(doc.Masters("Circle"), 3.25, 3.75)
added_new_shape3 = page.Drop(doc.Masters.ItemU("Diamond"), 5.25, 5.75)

# create connection point for added_new_shape1
# by adding row to 7 section (stores an object's connection points), after last exists row, unnamed rows
conPt1 = added_new_shape1.AddRow(7, -2, 153) # .AddRow(visSectionConnectionPts, visRowLast, visTagCnnctPt)
conRow1 = added_new_shape1.Section(7).Row(conPt1) #get the created row
# set coordinates of the connection point (0) - x, (1) - y
conRow1.Cell(0).FormulaU = "Width*1"
conRow1.Cell(1).FormulaU = "Height*0.5"

# create connection point for added_new_shape2
conPt2 = added_new_shape2.AddRow(7, -2, 153)
conRow2 = added_new_shape2.Section(7).Row(conPt2)
conRow2.Cell(0).FormulaU = "Width*0.5"
conRow2.Cell(1).FormulaU = "Height*1"

# create connection point for added_new_shape2
conPt3 = added_new_shape3.AddRow(7, -2, 153)
conRow3 = added_new_shape3.Section(7).Row(conPt3)
conRow3.Cell(0).FormulaU = "Width*0.5"
conRow3.Cell(1).FormulaU = "Height*1"

# drop the connector onto page
myConnector = page.Drop(app.ConnectorToolDataObject, 4, 10)
myConnectorBegin = myConnector.Cells("BeginX") #get start point of the connector
myConnectorEnd = myConnector.Cells("EndX") #get end point of the connector

vsoCellGlueToObject = added_new_shape1.Cells("Connections.X1") # get early created connection point of the first shape
vsoCellGlueToObject2 = added_new_shape2.Cells("Connections.X5") # get early created connection point of the second shape
#X1 = bottom (but top for shape2)
#X2 = right side? (but bottom for shape2)
#X3 = top? (but left side for shape2)
#X4 = left side? (but bottom for shape2)
#X5 = middle? (but right side for shape2)
#X6 = right side?

myConnectorBegin.GlueTo(vsoCellGlueToObject) # connect start point of the connector to shape's connection point
myConnectorEnd.GlueTo(vsoCellGlueToObject2)


# second connector onto page
myConnector2 = page.Drop(app.ConnectorToolDataObject, 4, 10)
myConnectorBegin2 = myConnector2.Cells("BeginX") #get start point of the connector
myConnectorEnd2 = myConnector2.Cells("EndX") #get end point of the connector

vsoCellGlueToObject = added_new_shape1.Cells("Connections.X1") # get early created connection point of the first shape
vsoCellGlueToObject2 = added_new_shape3.Cells("Connections.X1") # get early created connection point of the second shape

myConnectorBegin2.GlueTo(vsoCellGlueToObject) # connect start point of the connector to shape's connection point
myConnectorEnd2.GlueTo(vsoCellGlueToObject2)

#Change text of shape
added_new_shape1.Text = "testing"
added_new_shape2.Text = "EMMA"

#Change color of shape
added_new_shape2.Cells("FillForegnd").FormulaForceU = "RGB(255,153,51)"

Now here's the error I'm getting:

Code
C:\python>py createvisio2.py
Traceback (most recent call last):
  File "C:\python\createvisio2.py", line 12, in <module>
    added_new_shape3 = page.Drop(doc.Masters.ItemU("Diamond"), 5.25, 5.75)
  File "<COMObject <unknown>>", line 2, in ItemU
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'MyDrawing.vsdx - Visio Professional', '\n\nObject name not found.', None, 0, -2032465660), None)

Why can I not create a Diamond by name?  Oh, and I've tried it without ItemU as well, it doesn't work either way!  I haven't found any shapes besides Rectangle and Circle that work to create via python... but when I record a macro, they all are referred to by shape name.

I appreciate any help!
7
User-submitted Stuff / "Layer properties" (user-interface) with AND- function
« Last post by hidden layer on December 09, 2023, 12:05:46 PM »
Hi,
working with Vectorworks (as a 3D-modelling tool) I have the option to allocate objects to Classes AND Layers. Only if both of them are activated the object shows up.
In Visio I can do this as well but they're linked with an OR- function. Everybody knows.

I had a task to highlight some objects with the very same description in different modules. And the modules are separated to different locations... hard to find something.
Here's what I found out to work. It's just an example.
The layers are accessible by the user but they don't contain any of the highlighting-shapes. They are just used as user-interface. And the user interface is not filled with hundreths of items...
The allocation of the highlighting-shapes to the "layers" is done by DATA.

Maybe this has been discovered before but I couldn't find it. So I had a funny afternoon while its raining and thawing here.

Of course it's more bulky than the "find" function but whatever.

Have fun
hl
8
Programming & Code / Re: Some formatting questions for organisational shapes
« Last post by Nikolay on December 07, 2023, 05:27:53 PM »
A basic recommendation for questions like this is to use the macro recorder,
and then modify the generated VBA code manually to fit your needs.

This button:


Have you tried that?
9
Programming & Code / Re: Some formatting questions for organisational shapes
« Last post by Cheg on December 07, 2023, 03:43:52 PM »
Hi!

Thanks a lot, I need to process your answer.
I can import the data alright from excel. The thing is, I have many boxes and I would like to avoid changing them each by hand one after the other...

I'll make an example to send here with what I get from the import.

best,
Cheg
10
Programming & Code / Re: Some formatting questions for organisational shapes
« Last post by hidden layer on December 07, 2023, 01:11:59 PM »
Hello Cheg,
the horizontal bars are the frames of the 4 rectangles tha contains a text field each (insert -> field) whose content comes from Data.
The Group contains 4 data-rows (shapesheet -> properties) and the rectangles "look" into the shapesheet-cells of this group.
the construction is visible by rightclick -> Group -> open Group (not ungroup!)

I didn't say that this is not possible with vba. Of course is. But (similar to Excel) formulas are faster than vba.
How you get the data from Excel - that's a question for someone else.

cheers,
hl
Pages: [1] 2 3 ... 10