Dah Sra
Dah Sra

Reputation: 4445

Filling a shape of visio with color in c#

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..??

Upvotes: 2

Views: 2052

Answers (2)

JohnGoldsmith
JohnGoldsmith

Reputation: 2698

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;
}

Upvotes: 3

JohnGoldsmith
JohnGoldsmith

Reputation: 2698

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.

Upvotes: 0

Related Questions