Connect Version 13 C# - tag element question

I created a program that uses tags, I have no problem creating the tags and assigning them to elements.  My question is how to update a tag's value when it already exists in the design file.

The following is a section from my code, I know that I am accessing the tag's value because I added a print statement to show me the tag's current value.

MessageBox1.Text = MessageBox1.Text + "current tag value: " + tagEl.GetTagValue().ToString() + "\n";
//First tried changing the value using the following                            
tagEl.SetTagValue(chStr);

//Then tried changing the value using the following
TextQueryOptions textQueryOptions = new TextQueryOptions
{
    ShouldIncludeEmptyParts = false,
    ShouldRequireFieldSupport = false
};

TextPartIdCollection textParts = tagEl.GetTextPartIds(textQueryOptions);
TextPartId textPart = textParts[0];
TextBlock txtBlock = tagEl.GetTextPart(textPart);
Caret startCaret = txtBlock.CreateStartCaret();
Caret endCaret = txtBlock.CreateEndCaret();
txtBlock.ReplaceText(chStr, startCaret, endCaret);
tagEl.ReplaceTextPart(textPart, txtBlock);

Do I have to clone the tag element, make the changes and then replace the current tag with the clone tag that has the changes?

Thank you for any suggestions!

Donna

Parents
  • Donna,

    I see in this post you said you have no problem creating tags and attaching them to elements. Could you send me a c# code example of how you are doing this. I've created my own in vba in the past but can't seem to figure it out in c#. 

    Thanks

    Dave

  •     /*=================================================================================**//**
        * @bsiclass                                                               Bentley Systems
        /*--------------+---------------+---------------+---------------+---------------+------*/
        public class TagElementInfo
        {
            public bool TagExists { get; set; }
            public DgnTagDefinition TagDef { get; set; }
            public string TagSetName { get; set; }
            public string TagDefName { get; set; }
            public string TagDefPrompt { get; set; }
            public TagType TagDefType { get; set; }
            public object DefaultValue { get; set; }
            public object ActualValue { get; set; }
    
            /*---------------------------------------------------------------------------------**//**
            * Set tag type from string.
            * @bsimethod                                                              Bentley Systems
            /*--------------+---------------+---------------+---------------+---------------+------*/
            public void SetTagType(string type)
            {
                switch (type)
                {
                    case "Double":
                        TagDefType = TagType.Double;
                        break;
                    case "Int32":
                        TagDefType = TagType.Int32;
                        break;
                    default:
                        TagDefType = TagType.Char;
                        break;
                }
            }
    
            /*---------------------------------------------------------------------------------**//**
            * Set tag default value based on tag type.
            * @bsimethod                                                              Bentley Systems
            /*--------------+---------------+---------------+---------------+---------------+------*/
            public void SetTagDefaultValue(string value)
            {
                switch (TagDefType)
                {
                    case TagType.Char:
                        DefaultValue = value;
                        break;
                    case TagType.Int16:
                    case TagType.Int32:
                        DefaultValue = int.Parse(value);
                        break;
                    case TagType.Double:
                        DefaultValue = double.Parse(value);
                        break;
                }
            }

    The code above is from sdk examples.

    using System;
    using System.Collections.Generic;
    using Bentley.DgnPlatformNET;
    using Bentley.DgnPlatformNET.Elements;
    using Bentley.GeometryNET;
    

    The following code is how I incorporated the class.  My program reads and writes to an Excel workbook/spreadsheet to retrieve tag information to be placed into our border cells.

    Go to the examples in the SDK - ManagedAutoTagElementExample is a great example of coding for tags.

    origin = Bentley.GeometryNET.DPoint3d.FromXYZ(0, 0, 0);     //Origin needs to be set to zero for tag that is attaching to an element, otherwise, the tag is offset from element by X and Y
    TagElement tagg = new TagElement(activeModel, null, tagInfo.TagDefName, tagInfo.TagSetName, null, style, false, origin, orientation, copiedEle);
    activeFile.ProcessChanges(DgnSaveReason.UserInitiated);
    //Must set new value for tag, otherwise, the default value will be used.
    ElementPropertiesSetter remapper = new ElementPropertiesSetter();
    tagg.SetTagValue(tagInfo.ActualValue);
    tagg.SetTagValue(tagInfo.ActualValue);
    remapper.SetElementClass(DgnElementClass.Primary);
    remapper.SetColor(CTTagVals.ElColor);
    remapper.SetWeight(CTTagVals.ElWt);
    remapper.SetLevel(CTTagVals.ElLevel);
    remapper.Apply(tagg);
    tagg.AddToModel();

            /*---------------------------------------------------------------------------------**//**
            * Create Tag set with tag definition if user selects to create new tag definition.
            * @bsimethod                                                              Bentley Systems
            /*--------------+---------------+---------------+---------------+---------------+------*/
            public static void CreateTagSetAndTagDefinition(TagElementInfo tagInfo)
            {
                DgnFile activeFile = SheetManager.GetActiveDgnFile();
    
                if (activeFile != null)
                {
                    DgnModel dictionaryModel = activeFile.GetDictionaryModel();
                    DgnTagDefinition[] tagDefs = new DgnTagDefinition[1];
                    tagDefs[0] = new DgnTagDefinition
                    {
                        Name = tagInfo.TagDefName,
                        Prompt = tagInfo.TagDefPrompt,
                        TagDataType = tagInfo.TagDefType,
                        Value = tagInfo.DefaultValue
                    };
    
                    TagSetElement tagSet = new TagSetElement(dictionaryModel, tagDefs, tagInfo.TagSetName, "", true, activeFile, 0);
                    tagSet.AddToModel();
                }
            }
    

    I made some minor modifications to code above.

  • Donna,

    Thanks. I searched all over the sdk examples and could only find c++ examples. I'll try this out. Thanks again for the prompt reply.

    Dave

  • Hi ,

    I searched all over the sdk examples and could only find c++ examples.

    If helpful, when you first open the MicroStation CONNECT Edition Developer Shell (run as admin), you are placed in the Examples folder.  From there you can use one of the SDK convenience macros like: "s" followed by text that you want to search for (under the current folder), or use SdkSearch (searches across the entire installed SDK contents and MicroStation Programming forum; if you enter 'Y') similar to these examples:

    • s <searchterm>
      e.g. s TagElement
    • sdksearch <searchterm>
      e.g. sdksearch TagElement

    HTH,
    Bob



  • Bob and Donna

    Thank you very much for the help..I'll have a few less bruises on my forehead. I'll definitely need to use the search option in the future. I was able to get my tag element created and attached. Although I still cannot figure out how to get the (dgnTextStyle.SetProperty(TextStyleProperty.justification...) to work correctly. I'm attaching the code I have so far. Hope this helps someone else. I think I commented it pretty well. Let me know what you think.

    //create cell element from list of elements (listElements)
                CellHeaderElement cellHeaderElement = new CellHeaderElement(dgnModelActive, "WireDuct", pt3dDuctOriginSelected, dMatrix3DCell, listElements);
                cellHeaderElement.AddToModel();
    
                //create array of tag definitions and assign parameters for each
                DgnTagDefinition[] dgnTagDefinition = new DgnTagDefinition[3];
                dgnTagDefinition[0] = new DgnTagDefinition
                {
                    Name = "Length",
                    Prompt = "Length",
                    TagDataType = TagType.Double
                };
                dgnTagDefinition[1] = new DgnTagDefinition
                {
                    Name = "Type",
                    Prompt = "Type",
                    TagDataType = TagType.Char
                };
                dgnTagDefinition[2] = new DgnTagDefinition
                {
                    Name = "Size",
                    Prompt = "Size",
                    TagDataType = TagType.Char
                };
    
                //create tag set and add to model - needs no reportname and ownerid can be 0(have yet to figure out what that even is)
                TagSetElement tagSetElement = new TagSetElement(dgnModelActive, dgnTagDefinition, "DuctTagSet", "", true, dgnFileActive, 0);
                tagSetElement.AddToModel();
    
                //this 'origin' is actually the offset from the element origin the tag is associated with
                DPoint3d origin = DPoint3d.FromXYZ(0, 0, 0);
    
                //text offset (because I can't figure out justification)
                DVector3d dVector3D = new DVector3d(.187 * 254, -.187 * 254, 0);
                origin = DPoint3d.Add(origin, dVector3D);
    
                //rotate text 90 degrees CW matrix
                DMatrix3d MtrxTag90 = new DMatrix3d(0, 1, 0, -1, 0, 0, 0, 0, 1);
    
                //get active text style from active model
                DgnTextStyle style = DgnTextStyle.GetSettings(dgnFileActive);
                string OleTextStyle = style.Name;
    
                //text size
                style.SetProperty(TextStyleProperty.Height, .375 * 254);
                style.SetProperty(TextStyleProperty.Width, .375 * 254);
    
                //replace to update
                style.Replace(OleTextStyle,dgnFileActive);
    
                //this a value for th tag element
                object Test = 3.5;
    
                //get level id for tagelement
                levelId = Settings.GetLevelIdFromName("MY_DIMS_LAYER");
    
                //set level and color
                elementPropSet.SetLevel(levelId);
                elementPropSet.SetColor(5);
    
                //create tag element, apply properties and insert
                TagElement tagg = new TagElement(dgnModelActive,  null, "Length", "DuctTagSet", Test, style, false, origin, MtrxTag90, cellHeaderElement);
                elementPropSet.Apply(tagg);
                tagg.AddToModel();

    Dave

  • Hi Dave

    Code includes modifying justification:

    Modify and use active textstyle
    
    DgnFile activeFile = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnFile();
    double uorsPerMaster = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;
    uint ElColor = Convert.ToUInt16(Bentley.MstnPlatformNET.Settings.GetActiveColor().Index);
    uint ElWt = Convert.ToUInt16(Settings.LineWeight);
    LevelId ElLevel = Settings.ActiveLevelId;
    uint txtJst = 6;
    
    DgnTextStyle style = DgnTextStyle.GetSettings(activeFile);
    style.SetProperty(TextStyleProperty.Justification, txtJst);
    style.SetProperty(TextStyleProperty.LineSpacing, 1.0);
    
    double FontHt = 7.0 * uorsPerMaster; // initialize
    style.GetProperty(TextStyleProperty.Height, out FontHt);

  • I made some minor modifications to code above.

    I would also recommend these code enhancements to make it even better:

    • TagType enum contains also None and Binary values, which should be included in switch also.
    • TagSetElement is disposable object, so it should be enclosed by using.

    With regards,

      Jan

  • Thanks,

    I'll incorporate these improvements in my code

    Dave

Reply Children
No Data