Quantcast
Channel: MicroStation Programming Forum - Recent Threads
Viewing all articles
Browse latest Browse all 4331

CONNECT C# How to update a text string in a Cell?

$
0
0

I am trying to updated a Text Node containing a single Text Element inside a cell in a DGN file.  Everything seems to work ok until I attempt to replace the existing cell with the modified version using the ReplaceInModel method.  At that point the program will pause for a minute or two and finally generate an error and crash MicroStation CONNECT.

Here is the code I an using to update the text element in the cell.

Can anyone see anything wrong with the code?

A fellow programmer was able to used a similar code written in C++ to update the text element and my code does almost the same thing so I don't understand why it doesn't work.

Note: This method is part of a class that has already stored the Element Ids for the main cell and the text elements located inside the cell.

//------------------------------------------------------------------------------
// setStringId [pubic]
//
// Used to set the ID value for the specifed String ID text elment in the Solar
// rack cell.
//
// Input: uiStringNo - Specifies the index value for the String ID.
// sIdValue - Specifies the ID for for the String ID text element.
//
// Return: SolarRack instance or null.
//------------------------------------------------------------------------------
public void setStringId(uint uiStringNo, string sIdValue)
{
   ElementId stringId = new ElementId();

   bool bFound = false;

   uint i = 1;
   foreach (ElementId currId in StringIdList)
   {
      if (i == uiStringNo)
      {
         stringId = currId;
         bFound = true;
         break;
      }

      i++;
   }

   if (bFound == false)
      return;

   DgnModel activeDgnModel = Session.Instance.GetActiveDgnModel();

   CellQuery rackQuery = null; 

   Element oCell = activeDgnModel.FindElementById(rackElementId);
   if (oCell != null)
   {
      rackQuery = CellQuery.GetAsCellQuery(oCell);
      if (rackQuery == null)
         return;
   }

   rackQuery.ExposeChildren(ExposeChildrenReason.Edit);

   bool bUpdated = false;

   foreach (Element oChildElm in rackQuery.GetChildren())
   {
      if (oChildElm.ElementId != stringId)  
         continue;

      using (TextEdit textEdit = TextEdit.GetAsTextEdit(oChildElm))
      {
         if (textEdit != null)
         {
            DPoint3d origin = DPoint3d.Zero;
            DMatrix3d orientation = DMatrix3d.Zero;

            textEdit.GetSnapOrigin(out origin);
            textEdit.GetOrientation(out orientation);

            TextQueryOptions textOptions = new TextQueryOptions
            {
               ShouldIncludeEmptyParts = false,
               ShouldRequireFieldSupport = false
            };

            TextPartIdCollection textParts = textEdit.GetTextPartIds(textOptions);

            TextBlockProperties textBlockProps = null;
            ParagraphProperties paragraphProps = null;
            RunProperties runProps = null;

            using (TextBlock textBlock = textEdit.GetTextPart(textParts[0]))
            {
               if (textBlock != null)
               {
                  textBlockProps = textBlock.GetProperties();
                  paragraphProps = textBlock.GetParagraphPropertiesForAdd();
                  runProps = textBlock.GetRunPropertiesForAdd();
               }
            }

            if (textBlockProps != null && paragraphProps != null && runProps != null)
            {
               using (TextBlock newTextBlock = new TextBlock(textBlockProps, paragraphProps, runProps, activeDgnModel))
               {
                  textBlockProps.Dispose();
                  paragraphProps.Dispose();
                  runProps.Dispose();

                  if (newTextBlock != null)
                  {
                     newTextBlock.AppendText("Test");
                     newTextBlock.SetUserOrigin(origin);
                     newTextBlock.SetOrientation(orientation);

                     if (textEdit.ReplaceTextPart(textParts[0], newTextBlock) == TextReplaceStatus.Success)
                        bUpdated = true;
                  }
               }
            }
         }
      }
   }

   if (bUpdated)
      rackQuery.ReplaceInModel(oCell);
}


Viewing all articles
Browse latest Browse all 4331

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>