Edit the text Component Editor value via .Net

Edit the text Component Editor value via .Net

soonhui
Advisor Advisor
778 Views
8 Replies
Message 1 of 9

Edit the text Component Editor value via .Net

soonhui
Advisor
Advisor

For the below case, the value in the Text Component Editor is

 

<[Inner Structure Length(Umm|P0|RN|AP|GC|UN|Sn|OF)]> X  <[Inner Structure Width(Umm|P0|RN|AP|GC|UN|Sn|OF)]>

 

text component.png

 

But is there anyway that I can edit that value via .Net code?

 

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes
Accepted solutions (3)
779 Views
8 Replies
Replies (8)
Message 2 of 9

hippe013
Advisor
Advisor
Accepted solution

@soonhui, Yes. 

 

Here is a quick example: 

<CommandMethod("StyleTextComponentEdit")>
Public Sub CmdStyleTextComponentEdit()
   Dim aDoc As Document = Application.DocumentManager.MdiActiveDocument
   Dim ed As Editor = aDoc.Editor
   Dim db As Database = aDoc.Database

   Dim cvlDoc As CivilDocument = CivilApplication.ActiveDocument
   Dim styles As StylesRoot = cvlDoc.Styles
   Dim styleId As ObjectId = styles.LabelStyles.StructureLabelStyles.LabelStyles(0)

   Using tr As Transaction = db.TransactionManager.StartTransaction
      Dim style As LabelStyle = tr.GetObject(styleId, OpenMode.ForWrite)
      For Each id As ObjectId In style.GetComponents(LabelStyleComponentType.Text)
         Dim txtComp As LabelStyleTextComponent = tr.GetObject(id, OpenMode.ForWrite)
         txtComp.Text.Contents.Value = "<[Inner Structure Length(Umm|P0|RN|AP|GC|UN|Sn|OF)]> X  <[Inner Structure Width(Umm|P0|RN|AP|GC|UN|Sn|OF)]>"
         ed.WriteMessage(vbCrLf & $"Modified Component: '{txtComp.Name}' of Style '{style.Name}'")
      Next
      tr.Commit()
   End Using
End Sub
Message 3 of 9

soonhui
Advisor
Advisor

Thank you, @hippe013 , that really works for LabelStyles because we can reach the relevant LabelStyleTextComponent via what your code.

 

However, I found that there is no way to reach the LabelStyleTextComponent via Table Styles, as shown in the screenshot below:

 

tablestyle data.png

 

Somehow I can get the TableStyle via the below code, but there is no related Data Properties that is exposed, see the API list here.

 

 Document aDoc = Application.DocumentManager.MdiActiveDocument;
 Editor ed = aDoc.Editor;
 Database db = aDoc.Database;

 CivilDocument cvlDoc = CivilApplication.ActiveDocument;
 StylesRoot styles = cvlDoc.Styles;

 ObjectId styleId = styles.TableStyles.StructureTableStyles["@StructureTable"];

 using Transaction tr = db.TransactionManager.StartTransaction();
 var style = tr.GetObject(styleId, OpenMode.ForWrite) as Autodesk.Civil.DatabaseServices.Styles.TableStyle;
            

 

Any thoughts?

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes
Message 4 of 9

hippe013
Advisor
Advisor
Accepted solution

@soonhui, drilling down into the components of a table style is quite different, and even more so for a structure table style or an alignment segment table style, according to the API documentation. 

 

I can see in your image that you are looking at modifying a pressure pipe table style; however, my current example code is specifically for a structure table style. 

 

Example code for modifying a structure table style column and its component:

<CommandMethod("StructureTableStyleColumnEdit")>
Public Sub CmdStructureTableStyleColumnEdit()
   Dim aDoc As Document = Application.DocumentManager.MdiActiveDocument
   Dim ed As Editor = aDoc.Editor
   Dim db As Database = aDoc.Database

   Dim cvlDoc As CivilDocument = CivilApplication.ActiveDocument
   Dim styles As StylesRoot = cvlDoc.Styles
   Dim styleId As ObjectId = styles.TableStyles.StructureTableStyles(0)

   Using tr As Transaction = db.TransactionManager.StartTransaction
      Dim style As Autodesk.Civil.DatabaseServices.Styles.TableStyle = tr.GetObject(styleId, OpenMode.ForWrite)
      ed.WriteMessage(vbCrLf & $"Modifying Style: {style.Name}")

      'Get the columnStyle and modify its header
      Dim colmStyle As StructureColumnStyle = style.ColumnStyles(0)
      ed.WriteMessage(vbCrLf & $"Current Column Header: {colmStyle.HeaderString}")
      colmStyle.HeaderStringFormatted = "Structure Description"
      ed.WriteMessage(vbCrLf & $"New Column Header: {colmStyle.HeaderString}")

      'Get the component data
      Dim structColmComp As StructureColumnComponents = colmStyle.GetComponents
      Dim structColmCompData As StructureColumnComponentData = structColmComp.Item(0)
      ed.WriteMessage(vbCrLf & $"Current Component Data (Name / Content): {structColmCompData.Name} | {structColmCompData.Content}")

      'Set the new values of the component
      structColmCompData.Name = "Structure Description"
      structColmCompData.Content = "<[Description(CP)]>"
      colmStyle.SetComponents(structColmComp)

      'Print changes made to the component
      ed.WriteMessage(vbCrLf & $"New Component Data (Name / Content): {structColmCompData.Name} | {structColmCompData.Content}")
      tr.Commit()
   End Using
End Sub

 

The command line results:

Command: STRUCTURETABLESTYLECOLUMNEDIT
Modifying Style: EX STRUCTURE
Current Column Header: Structure Name
New Column Header: Structure Description
Current Component Data (Name / Content): Structure Name | <[Structure Name(CP)]>
New Component Data (Name / Content): Structure Description | <[Description(CP)]>

 

 

 

Message 5 of 9

hippe013
Advisor
Advisor
Accepted solution

Additionally, if you are looking to modify the column contents of a pressure pipe table style see the following example.

 

(I believe it is slightly different getting to the styles root of pressure pipe styles in C#.NET than it is VB.NET, but I am not sure.)

 

Example code for modifying a pressure pipe table style column and its component:

<CommandMethod("PressurePipeTableStyleColumnEdit")>
Public Sub CmdPressurePipeTableStyleColumnEdit()
   Dim aDoc As Document = Application.DocumentManager.MdiActiveDocument
   Dim ed As Editor = aDoc.Editor
   Dim db As Database = aDoc.Database

   Dim cvlDoc As CivilDocument = CivilApplication.ActiveDocument
   Dim tableStylesRoot As TableStylesRoot = cvlDoc.Styles.TableStyles

   'Must have reference to AeccPressurePipesMgd
   Dim styleID As ObjectId = TableStylesRootPressurePipesExtension.GetPressurePipeTableStyles(tableStylesRoot)(0)

   Using tr As Transaction = db.TransactionManager.StartTransaction
      Dim style As Autodesk.Civil.DatabaseServices.Styles.TableStyle = tr.GetObject(styleId, OpenMode.ForWrite)
      ed.WriteMessage(vbCrLf & $"Modifying Style: {style.Name}")

      'Get the columnStyle and modify its header
      Dim colmStyle As ColumnStyle = style.ColumnStyles(0)
      ed.WriteMessage(vbCrLf & $"Current Column Header: {colmStyle.HeaderString}")
      colmStyle.HeaderStringFormatted = "Pressure Pipe Description"
      ed.WriteMessage(vbCrLf & $"New Column Header: {colmStyle.HeaderString}")

      'Get the Current Column Content
      ed.WriteMessage(vbCrLf & $"Current Column Content: {colmStyle.ContentString}")

      'Set the New Column Content
      colmStyle.ContentStringFormatted = "<[Description(CP)]>"

      'Print the new Results
      ed.WriteMessage(vbCrLf & $"New Column Content: {colmStyle.ContentString}")

      tr.Commit()
   End Using
End Sub

 

The command line results:

Modifying Style: Basic
Current Column Header: Pressure Pipe Name
New Column Header: Pressure Pipe Description
Current Column Content: <[Pressure Pipe Name(CP)]>
New Column Content: <[Description(CP)]>
Message 6 of 9

hippe013
Advisor
Advisor

@soonhui do you have a C# example of getting to the PressurePipeStyles? Or do you also use the static methods found in the TableStylesRootPressurePipesExtension class? I had thought that in one of your posts about Pressure Networks that you had a different way of getting to those styles. 

0 Likes
Message 7 of 9

soonhui
Advisor
Advisor

@hippe013 ,

This is how I do it ( ChatGPT gives a perfect translation the first time round:

 

[CommandMethod("PressurePipeTableStyleColumnEdit")]
public static void CmdPressurePipeTableStyleColumnEdit()
{
    Document aDoc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = aDoc.Editor;
    Database db = aDoc.Database;

    CivilDocument cvlDoc = CivilApplication.ActiveDocument;
    TableStylesRoot tableStylesRoot = cvlDoc.Styles.TableStyles;

    // Must have reference to AeccPressurePipesMgd
    ObjectId styleId = TableStylesRootPressurePipesExtension
        .GetPressurePipeTableStyles(tableStylesRoot)[0];

    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        Autodesk.Civil.DatabaseServices.Styles.TableStyle style =
            tr.GetObject(styleId, OpenMode.ForWrite) as Autodesk.Civil.DatabaseServices.Styles.TableStyle;

        ed.WriteMessage($"\nModifying Style: {style.Name}");

        // Get the columnStyle and modify its header
        ColumnStyle colmStyle = style.ColumnStyles[0];
        ed.WriteMessage($"\nCurrent Column Header: {colmStyle.HeaderString}");

        colmStyle.HeaderStringFormatted = "Pressure Pipe Description";
        ed.WriteMessage($"\nNew Column Header: {colmStyle.HeaderString}");

        // Get the current column content
        ed.WriteMessage($"\nCurrent Column Content: {colmStyle.ContentString}");

        // Set the new column content
        colmStyle.ContentStringFormatted = "<[Description(CP)]>";

        // Print the new results
        ed.WriteMessage($"\nNew Column Content: {colmStyle.ContentString}");

        tr.Commit();
    }
}

 

Or just use this direct call:

 

ObjectId styleId = tableStylesRoot.GetPressurePipeTableStyles()[0];

 

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
Message 8 of 9

hippe013
Advisor
Advisor

@soonhui wrote:

Or just use this direct call:

 

ObjectId styleId = tableStylesRoot.GetPressurePipeTableStyles()[0];

 

Yes. That is what I was referring to.

 

It appears that in VB that I can do this:

Dim styleID As ObjectId = TableStylesRootPressurePipesExtension.GetPressurePipeTableStyles(tableStylesRoot)(0)

 

But cannot do this:

Dim styleID As ObjectId = tableStylesRoot.GetPressurePipeTableStyles(0)

At least not that I am aware of. 

 

I just thought it was odd is all. Not that it matters too much as I need to make the switch to C# anyways but figured it was worth noting. 

Message 9 of 9

soonhui
Advisor
Advisor

@hippe013 , now I retest the code again, and I found that even though the all the value in the command line result checks out ( like what you showed), but the actual table is not rendered correctly-- the value table column value still retains the old one, as per here

 

Here's my code.

 

    [CommandMethod(nameof(CmdStructureTableStyleColumnEdit))]
    public void CmdStructureTableStyleColumnEdit()
    {
        Document aDoc = Application.DocumentManager.MdiActiveDocument;
        Editor ed = aDoc.Editor;
        Database db = aDoc.Database;

        CivilDocument cvlDoc = CivilApplication.ActiveDocument;
        StylesRoot styles = cvlDoc.Styles;
        ObjectId styleId = styles.TableStyles.StructureTableStyles["Basic"];

        using Transaction tr = db.TransactionManager.StartTransaction();
        var style = tr.GetObject(styleId, OpenMode.ForWrite) as TableStyle;
        ed.WriteMessage("\n" + $"Modifying Style: {style.Name}");

       
        
        // Get the columnStyle and modify its header
        var colmStyle = (StructureColumnStyle)style.ColumnStyles[0];
        ed.WriteMessage("\n" + $"Current Column Header: {colmStyle.HeaderString}");
        colmStyle.HeaderStringFormatted = "Structure Description";
        ed.WriteMessage("\n" + $"New Column Header: {colmStyle.HeaderString}");

        // Get the component data
        
        StructureColumnComponents structColmComp = colmStyle.GetComponents();
        StructureColumnComponentData structColmCompData = structColmComp[0];
        ed.WriteMessage("\n" + $"Current Component Data (Name / Content): {structColmCompData.Name} | {structColmCompData.Content}");

        // Set the new values of the component
        structColmCompData.Name = "Structure Description";
        structColmCompData.Content = "<[Description(CP)]>";
        colmStyle.SetComponents(structColmComp);

        // Print changes made to the component
        ed.WriteMessage("\n" + $"New Component Data (Name / Content): {structColmCompData.Name} | {structColmCompData.Content}");
        tr.Commit();
    }

 

Note that I use the Basic style for the Structure Table.

 

You can use another drawing but I suspect the result would be the same.

 

To solve the problem, you need to "manually update" the column value in the UI by click on "Edit Table Style", select the related column, then click into it and click OK until you go to Text Component Editor, and then click OK ( no need to change anything). Then click apply. Only then the table rendering will be updated.  

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes