Color Fill check

Started by DejanV, July 01, 2010, 01:39:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

DejanV

Hey. I am new to Visio and VBA macro coding, so I seek help from enlightened brains :)

I am designing a valve system that changes color to indicate if it is OPEN/CLOSED. After trying & failing, I succeeded in making a durable code with help from an iteration tutorial. As it is now, I check for the text of the valves to decide which color the valve needs to be. However, the perfect situation would be to change the text checking with code that checks the actual color fill for every object iterated. Here's the code for text-based checking:


Sub SimpleValveControl()

Dim shp As Shape
Dim sel As Selection
Dim i As Integer
Dim UndoScopeID2 As Long

UndoScopeID2 = Application.BeginUndoScope("Fill Color")

Set sel = ActiveWindow.Selection

For i = 1 To sel.Count
Set shp = sel(i)
With shp

    If (.Text = "") Then
        .CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "THEMEGUARD(RGB(255,00,00))"
        .CellsSRC(visSectionObject, visRowFill, visFillBkgnd).FormulaU = "THEMEGUARD(SHADE(FillForegnd,LUMDIFF(THEME(""FillColor""),THEME(""FillColor2""))))"
        .Text = "Closed"

    ElseIf (.Text = "Closed") Then
        .CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "THEMEGUARD(RGB(0,255,00))"
        .CellsSRC(visSectionObject, visRowFill, visFillBkgnd).FormulaU = "THEMEGUARD(SHADE(FillForegnd,LUMDIFF(THEME(""FillColor""),THEME(""FillColor2""))))"
        .Text = "Open"
   
    ElseIf (.Text = "Open") Then
        .CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "THEMEGUARD(RGB(255,00,00))"
        .CellsSRC(visSectionObject, visRowFill, visFillBkgnd).FormulaU = "THEMEGUARD(SHADE(FillForegnd,LUMDIFF(THEME(""FillColor""),THEME(""FillColor2""))))"
        .Text = "Closed"
    End If

End With
Next i

Application.EndUndoScope UndoScopeID2, True

End Sub


In the end, all I am looking for is how to change out the (.Text = "....") with an equal code that checks for the color fill of the iterated objects. Can anyone help?

Jumpy

Maybe I dont understand the question, but what is with:


If .CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "THEMEGUARD(RGB(255,00,00))" Then
  .CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "THEMEGUARD(RGB(0,255,00))"
Else
  .CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "THEMEGUARD(RGB(255,00,00))"
End IF


It may be much more easy, to change the FillColor based on formulas in the ShapeSheet, without VBA.
Somewhere in the Shape you can define it's status (open or closed), for example in an ShapeData cell.
Depending on that cell the fillcolor changes, because of ShapeSheet formulas.

Now you can change the status of a valve manually or per VBA. Changing the status will than trigger the changing of the fillcolor.

vojo

right click the shapes to see what they can do in shapesheet for fill colors (among other things)....have fun

Jumpy

Hello vojo,

how do you show or hide the connection points? The action cell only changes it's "checked" value, but in the Subshapes I can't find a line that depends on that. Or did I just not see it?

Jumpy

vojo

check out the "isnaptarget" cell in the group.

JuneTheSecond

How would you like to use user defined cell or shape data?
For example, add user defined cell to the light shape like a "User.isOpen", set value TRUE or FALSE.
And change color after checking the value of user defined cell.


Sub test()
Dim Sel As Visio.Selection
Dim I As Long
Dim shp As Visio.Shape
Dim isOpen As Boolean

Set Sel = ActiveWindow.Selection

For I = 1 To Sel.Count
    Set shp = Sel(I)
    With shp
        isOpen = .CellsSRC(visSectionUser, visRowUser, visUserValue).FormulaU
        If isOpen Then
            .CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "THEMEGUARD(RGB(255,00,00))"
            .CellsSRC(visSectionObject, visRowFill, visFillBkgnd).FormulaU = "THEMEGUARD(SHADE(FillForegnd,LUMDIFF(THEME(""FillColor""),THEME(""FillColor2""))))"
            .CellsSRC(visSectionUser, visRowUser, visUserValue).FormulaU = "FALSE"
   
        Else
            .CellsSRC(visSectionObject, visRowFill, visFillForegnd).FormulaU = "THEMEGUARD(RGB(0,255,00))"
            .CellsSRC(visSectionObject, visRowFill, visFillBkgnd).FormulaU = "THEMEGUARD(SHADE(FillForegnd,LUMDIFF(THEME(""FillColor""),THEME(""FillColor2""))))"
            .CellsSRC(visSectionUser, visRowUser, visUserValue).FormulaU = "TRUE"
        End If
   
    End With
Next I


End Sub
Best Regards,

Junichi Yoda
http://june.minibird.jp/

Jumpy

#6
Thanks vojo,

didn't have any use for that cell, up to now, but that is rather cool.
When I had to hide a Connection Point I moved it out of the way / the page so it seemed gone. (like to x = 10000 mm, y = 100000 mm).
But this solution is more elegant (at least in Groups).

Jumpy

P.S.: Sorry all and especially dejanV for this sidetrack inside a post of a different topic.

DejanV

I am gonna check all your suggestions now, this looks amazing. Thank you all for replying :). Going to post the file when it's done so you can see how I wanted it to be.

DejanV

Finished with a temporary solution, and it seems to work. I used JuneTheSecond's suggestion to change the colors by right clicking on the shape, and define whether a valve is open or closed.

I have one additional question about the stencils in Visio. Let's say I want to save some custom made shapes such as valves and chokes in a stencil, so that I can send that stencil to other developers for use in their own drawings.
While trying to modify the ShapeSheet settings for the respective masters I discovered that when I drag a shape that has a macro made in the stencil, that shape will not be able to run the macro (gives an error code '13'). I suspect this is due to some referencing issues in the macro itself, or the fact that Visio has some other way of implementing macros so that both stencils and drawings can use them?

Jumpy

Each stencil and each drawing have a VBA project.
Inside that project are the VBA Modules, especially the ThisDocument modul.

When a shape starts a macro with RUNMACRO the macro must be stored in the same (project of the) document (either drawing or stencil) as the shape.
Or in that document must be a reference to the other document, where the vba project is stored.
Such a reference is created in the VBA Editor in Extras->References.

That means, that your stencils need the reference to drawing, where the code is sored.
But is this always possible?

-------------

Therefore the better way for you is to store the macro (only) in the stencil (no longer in the drawing) and use the CALLTHIS function instead of RUNMACRO, because the CALLTHIS function accepts as second(?) argument the name of the project (normally the document) where the macro is stored, in your case that will be the stencil. Because CALLTHIS so knows, where the code is stored, you don't have to bother with References.
A second benefit is that the code is shipped together with the stencil to your co-workers and not with the drawing/template.
And last but not least, when you send drawings to others (customers?) they won't see any code in the drawing, because it's stored in the stencil.

-----------

Last comment: There are two or three older threads arround here (from 2008 or 2009) where vojo, al and chris discussed a little about code in drawings vs. stencils. Search for vba,stencil,references - It's interesting.

Yacine

Hi Dejan,

this is not a direct answer to your question, but I had a quite similar project some time ago.

The task was not so much a simulation, but a visualisation of the different states of a processing plant.

As my P&IDs include different fluids and flows colored differently, switching the colors was not much an option, so we opted for different line weights. For more flexibility the values for both states were stored in the page's properties.

As switch we chose the double-click event (faster than any writing, menu,...)

In my attachement I removed all the macros that complement the project. The most important being the capability to tell the connectors to which actor they are connected and write the formulas accordingly in the line weight fields.

By the way, I noticed the way you drew your sketch. Have a look a line patterns for arrows in your pipes and play with text tools.

HTH
Cheers Yacine
Yacine

DejanV

Thanks for input again guys. Seems like there is a lot to learn in Visio...

@Yacine, thank you very very much for sharing the drawing. I have changed mine now so that I change color based on a property value (like jumpy explained early in this post - however I did not understand it at that time). When right-clicking and choosing a value, a valve will now change properly, and it is very easy to add additional conditions such as "vent" or "alarm". The reason for using menu is because I find it troublesome double-clicking 4-5 times in a row to get to the state I want the valve to be in :).

In addition, your implementation of indicating fluids within pipes is extremely nice. Trying to make a similar one for our project now as well. I found a macro that works (but only on connectors) here: http://www.visguy.com/2009/04/22/analyze-connectivity-between-process-flows/

However, I wish to implement the same macro for use on other shapes than connectors, such as valves. From that macro, you can see it actually has some sort of handling for 2D-shapes in the ELSE-part of the first IF-statement. However, when using the macro on a shape, it won't tell which shapes it is connected to? When I change "Set colConnectsCollection = shpConnector.Connects" to following "..... = shpConnector.FromConnects", then it suddenly shows the shapes attached. How can I change this macro in a way that I can track any Connectors glued to it (for the purpose of changing the color of pipes as well)?

Jumpy

If you have the FromConnects Collection, each Item represents a Connector (1D) or shape with control handle (2D) that is glued to your shape.
The connection is from those Connector/ControlHandle to your shape.
Therefore colConnectsCollection.Item(1).FromSheet gives you the Shape that is glued to your shape.
Here is an example (untested, writen here only from memory)


Dim Valve As Visio.Shape
Dim Tube As Visio.Shape
Dim Con As Connect

For Each Con in Valve.FromConnects
If Con.FromSheet.OneD Then               'Tests, if the shape is a connector (in your case a tube)
  Set Tube = Con.FromSheet
  Tube.Cells("LineColor").FormulaU = 5    'Or do sth. else with the tubes/pipes
End If
Next