Can't set the style value

Hello,
I drew a rectangle and added some text.
Now I wanted to make the text bold. But that didn’t work.

I think I know where the problem might be.
@philip.zhou you once wrote to me in response to another question that you cannot change inherit values. How to change an inheritUserPop

But in my case, I only have Inherit values. Mastershape and parentshape are null. The rootshape is available but there are also only inherited values. I couldn’t find any other shapes from which my shape could have inherited the Inherit values.

You can use this code to reproduce the problem. Where’s my mistake to setting the style value?

Diagram diagram = new Diagram();
Aspose.Diagram.Page page = new Aspose.Diagram.Page();
page.Name = "My Page";
diagram.Pages.Add(page);
long shapeId = page.DrawRectangle(0.39, 0.66, 0.79, -0.79);

Aspose.Diagram.Shape shape = page.Shapes.GetShape(shapeId);

var countNormal = shape.Chars.Count; // 0
var countInherit = shape.InheritChars.Count; // 1
var m = shape.Master; // null
var p = shape.ParentShape; // null
var r = shape.RootShape; // Shape
var countRootNormal = r.Chars.Count; // 0
var countRootInherit = r.InheritChars.Count; // 1

shape.Text.Value.Add(new Txt("Hello World"));
shape.InheritChars[0].Style.Value = Aspose.Diagram.StyleValue.Bold;

diagram.Save("c:\\AnalyzeInheritChars.vsdx", SaveFileFormat.Vsdx);

@sgruth

It looks like you’re encountering an issue with setting the style value for text in your diagram shape. The problem arises because you’re trying to modify the inherited character style directly, which is not allowed.

Cause: The inherited character style cannot be modified directly, leading to the issue you’re facing.

Solution: To make the text bold, you should create a new character object instead of modifying the inherited character directly. Here’s how you can do it:

  1. Add a new character to the shape’s Chars collection.
  2. Set the style of this new character to bold.

Here’s an updated version of your code:

Diagram diagram = new Diagram();
Aspose.Diagram.Page page = new Aspose.Diagram.Page();
page.Name = "My Page";
diagram.Pages.Add(page);
long shapeId = page.DrawRectangle(0.39, 0.66, 0.79, -0.79);

Aspose.Diagram.Shape shape = page.Shapes.GetShape(shapeId);

// Clear any existing characters
shape.Chars.Clear();

// Add new text
shape.Text.Value.Add(new Txt("Hello World"));

// Create a new character for the text
Char newChar = new Char();
newChar.Style.Value = Aspose.Diagram.StyleValue.Bold; // Set the style to bold
newChar.Size.Value = 0.22; // Set the size as needed
newChar.Color.Value = "#000000"; // Set the color as needed

// Add the new character to the shape
shape.Chars.Add(newChar);

diagram.Save("c:\\AnalyzeInheritChars.vsdx", SaveFileFormat.Vsdx);

Next Steps: Implement the provided code in your project to create a new character with the desired style. This should resolve the issue you’re facing.

If you have any further questions or need additional assistance, feel free to ask!

Dear AI, yes, your suggestion works.
But before I rebuild my entire source code, I would like to hear @philip.zhou’s opinion on whether this is the only option.

Perhaps there is a fundamental flaw in my approach. Until now, I have assumed that there is always an object somewhere where I can change the properties. That’s why I first look in Shape, then in MasterShape, then ParentShape and finally in RootShape to see if there is an object where I can change a property.

Or do you actually have to proceed as your AI suggests when changing a property? Delete the previous property in the object, then create a new object for the property and finally assign it to the Shape. That seems very cumbersome and unusual to me.

This approach at least contradicts reading properties. We recently had a case where we actually had to read the inherit value because the normal value was unusable. Phenomenon with Shape.Line.LineWeight

Please tell me the correct approach for setting and reading properties of individual Aspose objects.

@sgruth
InheritChars is used to retrieve the properties of the shape itself as well as those inherited from its master and stylesheet. It is not recommended to modify it.
If the shape itself does not have the ‘char’ property, you can create a new one.

        shape.Chars.Add(new Aspose.Diagram.Char());
        shape.Chars[0].IX = 0;
        shape.Chars[0].Color.Value = "#ff0000";
        shape.Chars[0].Font.Value = 4;
        shape.Chars[0].Size.Value = 0.16667;
        shape.Chars[0].Style.Value = StyleValue.Bold;

Thank you for your clarification. That helps me a lot.

@sgruth
You are welcome.
Please feel free to contact us in case you have further comments or questions.
Thanks.