Issue trying to format a shape form a Custom Stencil

Started by WzBn, April 13, 2015, 02:11:11 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

WzBn

Hi,
Please let me to advise that I'm newbie with Visio creation programmatically.
I want to create a diagram using a custom Stencil that I was created previously. This master stencil has shapes with an image and a text inside.
I can get the master, I can put in the diagram, I can edit the text (and Text Format example color),  but I can´t  for example change the  fill collor of the Shape. Surely I making a silly error, but I can´t  solve it.
If I use a basic stencil, I can handle to change the fill color, and stuff like that.
I don´t know if The custom stencial must be created i a special way or if shapes inside need an special parameter to be updated.
Any Help Will be really appreciated.
Below image example
http://postimg.org/image/hzqpmp5bf/

Paul Herber

The details are in the shape itself. Can you post a Visio diagram containing the shape?
Electronic and Electrical engineering, business and software stencils for Visio -

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

WzBn

Hi, Thanks for answer, Attached are the diagram (with custom stencil and whitout, I added the custom stencil too), in code.

       
'(VB.Net)
             vGenMaster = vDVStencil.Masters("Wait WF to finish")                   ' Sample Custom stencil
             vGenMaster = vStencil.Masters("Rectangle")                                   ' Sample Basic stencil

' Getting Size

' nd (json object containing info for shape from an external dabases)


          Dim iWdh As Double = CDbl(nd("WDH")) / twips
          Dim iHgh As Double = CDbl(nd("HGH")) / twips
          Dim iTop As Double = CDbl(nd("TOP")) / twips
          Dim iLft As Double = CDbl(nd("LFT")) / twips

' Dropping Shape

          vGenShape = vPage.Drop(vGenMaster, iLft, iTop)
          vGenShape.Name = nd("IDX")
          vGenShape.Data1 = JsonConvert.SerializeObject(nd)
          vGenShape.Data2 = nd("ID")

'Shape sizing

          vGenShape.CellsSRC(Visio.VisSectionIndices.visSectionObject,
  Visio.VisRowIndices.visRowXFormIn, Visio.VisCellIndices.visXFormWidth).ResultIU = iWdh
          vGenShape.CellsSRC(Visio.VisSectionIndices.visSectionObject,
  Visio.VisRowIndices.visRowXFormIn, Visio.VisCellIndices.visXFormHeight).ResultIU = iHgh

          If WMax < (iLft + iWdh) Then WMax = iLft + iWdh
          If HMax < (iTop + iHgh) Then HMax = iTop + iHgh

          Dim shapeText As Microsoft.Office.Interop.Visio.Characters
          Dim shapeTextFont As Microsoft.Office.Interop.Visio.Font
          Dim shapeColorFont As Microsoft.Office.Interop.Visio.Color


          shapeText = vGenShape.Characters

          ' Start a new run by setting the begin value to the end value,
          ' so that text may be appended to the existing text.
          shapeText.Begin = shapeText.End

          ' Line 1:
          shapeText.Text = nd("TXT")

          ' Set the character style of the text to bold.

          Dim iTxtStyle As Short

          If nd("FBD") = "1" Then iTxtStyle += Microsoft.Office.Interop.Visio.VisCellVals.visBold
          If nd("FIT") = "1" Then iTxtStyle += Microsoft.Office.Interop.Visio.VisCellVals.visItalic
          If nd("FUD") = "1" Then iTxtStyle += Microsoft.Office.Interop.Visio.VisCellVals.visUnderLine

          If iTxtStyle > 0 Then shapeText.CharProps(CShort(Microsoft.Office.Interop.Visio.VisCellIndices.visCharacterStyle)) = iTxtStyle

          Dim sFNN As String
          Dim iFSZ As Integer

          shapeText.CharProps(CShort(Microsoft.Office.Interop.Visio.VisCellIndices.visCharacterSize)) = 18

          shapeTextFont = shapeText.Document.Fonts.Item("Calibri")

          shapeText.CharProps(CShort(Microsoft.Office.Interop.Visio.VisCellIndices.visCharacterFont)) = shapeTextFont.Index
          shapeText.CharProps(CShort(Microsoft.Office.Interop.Visio.VisCellIndices.visCharacterColor)) = Visio.VisDefaultColors.visDarkGreen

' Shape Formatting

          vGenShape.CellsSRC(Visio.VisSectionIndices.visSectionObject, Visio.VisRowIndices.visRowFill, Visio.VisCellIndices.visFillBkgnd).FormulaU = "RGB(255,128,0)"
          vGenShape.CellsSRC(Visio.VisSectionIndices.visSectionObject, Visio.VisRowIndices.visRowFill, Visio.VisCellIndices.visFillForegnd).FormulaU = "RGB(255,128,128)"
          vGenShape.CellsSRC(Visio.VisSectionIndices.visSectionObject, Visio.VisRowIndices.visRowCharacter, Visio.VisCellIndices.visCharacterColor).FormulaU = "RGB(255,0,0)"

          vGenShape.CellsSRC(Visio.VisSectionIndices.visSectionObject, Visio.VisRowIndices.visRowLine, Visio.VisCellIndices.visLineColor).FormulaU = "RGB(0,128,0)"

          vPage.AutoSizeDrawing()


Paul Herber

Your custom shape is a grouped shape and it's the sub-shape that takes the colour, not the top level shape.
Electronic and Electrical engineering, business and software stencils for Visio -

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

WzBn

Ouch, Thanks a lot, So what it is your recommendation??
create a new stencil un-grouping the shapes?
Re-ordering the shapes in the stencil (something like bring to front??)
or it is a mechanism to access (and formatting) the sub-shape , using the current stencil?


And Really, really thanks !!

Paul Herber

Electronic and Electrical engineering, business and software stencils for Visio -

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

WzBn

Thanks a lot:

I'm not clear if need to re-define/re-create the Stencil, I've added the following


For Each shpSub In vGenShape.Shapes
            Try
'Try one =  using FormulaForce  Because two of three cells  raise error " cell is guarded"

              shpSub.Cells("FillBkgnd").FormulaForce = String.Format("=RGB({0},{1},{2})", GFC.R, GFC.G, GFC.B).ToString
              shpSub.Cells("LineColor").FormulaForce = String.Format("=RGB({0},{1},{2})", DRC.R, DRC.G, DRC.B).ToString
              shpSub.Cells("FillForegnd").FormulaForce = String.Format("=RGB({0},{1},{2})", FRC.R, FRC.G, FRC.B).ToString

' Result Nothing change

'Try two=  using FormulaForce  Because two of three cells  raise error " cell is guarded"

              shpSub.CellsSRC(Visio.VisSectionIndices.visSectionObject, Visio.VisRowIndices.visRowFill, Visio.VisCellIndices.visFillBkgnd).FormulaForce = String.Format("RGB({0},{1},{2})", GFC.R, GFC.G, GFC.B).ToString
              shpSub.CellsSRC(Visio.VisSectionIndices.visSectionObject, Visio.VisRowIndices.visRowFill, Visio.VisCellIndices.visFillForegnd).FormulaForce = String.Format("RGB({0},{1},{2})", FRC.R, FRC.G, FRC.B).ToString
' Result Nothing change

'Try three=  using FormulaForce  Because two of three cells  raise error " cell is guarded"

              shpSub.Cells("LineColor").FormulaForce = 3
              shpSub.Cells("FillBkgnd").FormulaForce = 3
              shpSub.Cells("FillForegnd").FormulaForce = 3
' Result Nothing change
            Catch ex As Exception
              Console.WriteLine(ex.ToString)
            End Try

          Next shpSub


Attached is the Diagram generated.. Thanks again.. I'm very frustrated.

wapperdude

#7
Which version of Visio?

In V2007, the following gives me a compile error:
QuoteshpSub.Cells("FillForegnd").FormulaForce = String.Format("=RGB({0},{1},{2})", FRC.R, FRC.G, FRC.B).ToString

VBA doesn't like "String.Format" rejecting that syntax as compile error.  The entire right half of your code line strikes me as wrong, but, I'd have to dig for awhile to make a recommendation.  Unfortunately, it's late where I am. 

Perhaps someone else on the other side of the world will pick this up.

Wapperdude

Ed.:  OK, I see it must be V2010 or newer.  Some of us will need prior version in order to assist...vsd not vsdx. 
Visio 2019 Pro

WzBn

it is Visio 2013, and Code is in VB.Net. Below I'm re-writting the code in order to make it more readable,

For Each shpSub In vGenShape.Shapes
' using some sample colours
'Try one =  using FormulaForce  Because two of three cells  raise error " cell is guarded"

              shpSub.Cells("FillBkgnd").FormulaForce = "=RGB(0,255,0)"
              shpSub.Cells("LineColor").FormulaForce = "=RGB(0,0,255)"
              shpSub.Cells("FillForegnd").FormulaForce = "=RGB(255,0,0)"

' Result Nothing change

'Try two=  using FormulaForce  Because two of three cells  raise error " cell is guarded"

              shpSub.CellsSRC(Visio.VisSectionIndices.visSectionObject, Visio.VisRowIndices.visRowFill, Visio.VisCellIndices.visFillBkgnd).FormulaForce ="RGB(0,255,0)"
              shpSub.CellsSRC(Visio.VisSectionIndices.visSectionObject, Visio.VisRowIndices.visRowFill, Visio.VisCellIndices.visFillForegnd).FormulaForce = "RGB(0,0,255)"
' Result Nothing change

'Try three=  using FormulaForce  Because two of three cells  raise error " cell is guarded"

              shpSub.Cells("LineColor").FormulaForce = 3
              shpSub.Cells("FillBkgnd").FormulaForce = 3
              shpSub.Cells("FillForegnd").FormulaForce = 3
' Result Nothing change

          Next shpSub


Attachs has visio diagram in vsd and an image of the original "diagram" that I want to reproduce in  visio (As an example...)

Thanksa again ( for all your support)

wapperdude

Ah, VB.net.  I don't speak it unfortunately.

Your vsd file helps tremendously though.  The problem is that the shape you want to color is nested a couple of stacked grouped shapes down.

You can see this if you open the Drawing Explorer Window (DEW), expand the Foreground Pages tree structure.  Consider shape 2, which is a top level group shape.  If you select it in the DEW, it will show up selected in the drawing window.  Keep expanding the tree structure in the DEW.  Briefly, Shape2>Alternatiing>Sheet.3> ... .  Turns out, Sheet.3 is the shape you want to fill.  So, in your code, you need to push down one more level, i.e., another nested For loop.  Then you'll want something like  For each subSubShp in Alternating.shapes.....  Hopefully, all of the shapes have a consistent structure to them, or your code will be more involved.

HTH
Wapperdude
Visio 2019 Pro

WzBn

WOW..... I did not realize it... But I like to be aware that Visio has all this type of tools. Well It is time to reorganize my ideas and see what it is more convenient, re-create the stencils for a better "shapes organization" or drilling down in the structure inside the code looking for a specific Shape.

AnyWay Thanks a lot for for assistance and support. Surely more doubts are going to come up.