2

Hi i am creating Shapes of visio2013 using c# .Now i need to fill the shape with some colors using c# i tried the below codes but nothing makes sense :( :(

   Visio.Cell cqellname;
            cqellname = shape.get_CellsSRC(
            (short)Visio.VisSectionIndices.visSectionObject,
            (short)Visio.VisRowIndices.visRowFill,
            (short)Visio.VisCellIndices.visFillBkgnd);
            cqellname.FormulaU = "rgb(255,0,0)";

above code throws an error as Cell is Guarded.

  shape.get_CellsSRC((short)Visio.VisSectionIndices.visSectionObject,
        (short)Visio.VisRowIndices.visRowFill,
   (short)Visio.VisCellIndices.visFillBkgnd).FormulaForceU = "RGB(" + R + "," + G + "," + B + ")";

tried the above, it doesn't throw any exceptions but nothing changed in the shape.

i already tried this solution from stackoverflow and its too not working

I can able to see the value assigned by me in shapesheet FillForeGnd and FillBkGnd , but the shape is not filling with the color which i gave .

enter image description here

Can any one please tell me how to do this..??

Community
  • 1
  • 1
Dah Sra
  • 4,107
  • 3
  • 30
  • 69

2 Answers2

3

As I mentioned in the answer above (which you have marked as not useful) you are targeting the wrong shape.

Now that you've editted your question to include more details it's clear which shape you're dealing with.

The shape you're targeting appears to be the "Collapsed Sub-Process" from the "BPMN Basic Shapes" stencil. This is a group shape and the top level has no geometry, so changing its color, as you're doing in your question, has no visual change. To solve this you need to find sub-shape that is used to show the background. It happens that in this specific master, the sub-shape that contains the fill you need to target has an index one greater than the parent, so you can use this in the code. The shape lacks any other clear features (such as a User cell) that would make for a better candidate so please note that this method is just for this particular shape.

Given that you appear to be doing a fair bit of work with this stencil, my approach would be to create a copy of the stencil and make some modifications to the masters to make this kind of interaction a little easier, but I hope in the meantime that the following code answers your question.

Please mark it as answered if that's the case.

public void OnCheckFillBPMN()
{
    Color fillColor = Color.FromArgb(1, 255, 0, 0);
    CollapsedSubProcessFill(this.Application.ActiveWindow.Selection.PrimaryItem, fillColor);
}

private void CollapsedSubProcessFill(Visio.Shape vShpIn, Color fillColor)
{
    if (vShpIn != null && vShpIn.Master != null && vShpIn.Master.NameU.StartsWith("Collapsed Sub-Process"))
    {
        //The sub-shape that provides the fill colour in 
        //the 'Collapsed Sub-Process' master is the first index
        //after the group shape. 
        //If you want to use a different method to locate the 
        //correct sub-shape then do that here.
        var targetSubShpId = vShpIn.ID + 1;
        var targetShp = TryGetShapeInCollection(vShpIn.Shapes, targetSubShpId);
        if (targetShp != null)
        {
            var targetCell = targetShp.get_CellsSRC(
                (short)Visio.VisSectionIndices.visSectionObject,
                (short)Visio.VisRowIndices.visRowFill,
                (short)Visio.VisCellIndices.visFillForegnd);
            targetCell.FormulaU = "RGB(" + fillColor.R
                                    + ',' + fillColor.G
                                    + ',' + fillColor.B + ')';
        }
    }
}


private Visio.Shape TryGetShapeInCollection(Visio.Shapes vShps, int shpId)
{
    try
    {
        if (vShps != null)
        {
           var targetShp = vShps.ItemFromID[shpId];
           return targetShp; 
        }  
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
        if (ex.ErrorCode == -2032465756) //Invalid sheet identifier
        {
            return null;
        }
    }
    return null;
}
JohnGoldsmith
  • 2,638
  • 14
  • 26
  • Thanks for the reply. I will look into it. :) – Dah Sra Oct 20 '15 at 11:19
  • So in this case if i need to fill a task then that too same procedure..? – Dah Sra Oct 20 '15 at 11:21
  • Thanks it works ..!! But what if its a normal shape as Task etc and not a collapsed sub-process..?? – Dah Sra Oct 20 '15 at 11:51
  • 1
    It looks like Task is based on the same structure, so you could adapt the master name check to include "Task" and "Expanded Sub-Process" – JohnGoldsmith Oct 20 '15 at 12:12
  • what if its not on same structure – Dah Sra Oct 20 '15 at 13:08
  • If it's not the same structure, then you just need to identify where the visual fill is applied (ie, which sub-shape or, potentially, in the group). This is the nature of Visio and coding against shapes that you didn't produce. Tools such as the Drawing Explorer, Event Monitor (part of the SDK) and the macro record can help in uncovering how shapes are built and interact with one another. – JohnGoldsmith Oct 20 '15 at 14:27
  • why is th\is condition required ??? if (vShpIn != null && vShpIn.Master != null && vShpIn.Master.NameU.StartsWith("Collapsed Sub-Process")) { – Dah Sra Oct 21 '15 at 11:09
  • You mean the StartsWith as opposed to just checking the master name? Because a master with the name "Collapsed Sub-Process" already exists in the document stencil of that particular template so when you add a new one from the main stencil a second one is added (as the masters' baseIDs don't match). The second master is named "Collapsed Sub-Process.8", or similar, and hence the the StartsWith check. I'm not sure if there's a rationale behind the existing master in the template or if it's just a bug. – JohnGoldsmith Oct 21 '15 at 13:45
  • http://stackoverflow.com/questions/33343479/how-to-adjust-the-width-of-a-pool-lane-in-visio-shape-using-c-sharp – Dah Sra Oct 26 '15 at 11:49
0

If the FormulaForceU works but you get no visual change then I assume you're not setting the correct cell. Normally you'd set the FillForegnd cell (visFillForegnd) unless there's a different pattern set. Also note that (for Visio 2013+) if FillGradientEnabled is set to true, this will override the solid colors.

One last thing to bear in mind is that the shape you're targeting might have no geometry, or it might have its NoFill cell set to true, and really you should be targeting a child / sub-shape.

In either case you should crack open the ShapeSheet and see what state the shape is in.

JohnGoldsmith
  • 2,638
  • 14
  • 26
  • Whats there in that link which you shared .? i didnt find anything there – Dah Sra Oct 16 '15 at 05:26
  • It's a link to an explaination of what the ShapeSheet is, but now I realize that you are the person with the Event shape problem so I guess you know what it is already. Did you find you're targeting the wrong cell or was it some other problem? – JohnGoldsmith Oct 16 '15 at 14:07
  • Hi, @dahsra, I think if you've not identified the problem yet then it seems a little harsh to mark the answer down. – JohnGoldsmith Oct 20 '15 at 11:18
  • I have to agree with John. Please be respectful of the effort and expertise required to provide free support in this forum. – David Parker Oct 20 '15 at 13:12
  • For me, I had migrated my extension from Visio 2010 to Visio 2016, and colouring had stopped working. I've been trying to fix this for ages, and the FillGradientEnabled flag fixed it for me. – Danny Wilson Apr 19 '18 at 14:51
  • 1
    @DannyWilson - glad that's helped. I think this can happen in the conversion from 2010 (.vsd and similar) file format to 2013+ (.vsdx and similar) where the shape implements a gradient style pattern. The conversion process appears to decide to use the newer gradient stops feature rather than the older pattern based version. – JohnGoldsmith Apr 20 '18 at 12:12