Microstation ConnectSDK I cannot modify existing text in textblock

Hello, I am scanning a file for text and pass the MSElementDescr to my function for modification.  I have tried a number of methods from the examples file.  I can ADD text, but my ability to modify the existing simple text string seems to be lacking something.  Is there an element add needed, or am I using the incorrect process for modifying existing text?  I am fairly new to programming for Microstation, are there any books explaining how to program for Microstation using Visual Studio 2017 in c++?

Public void getText(MSElementDescr *elm) {

ElementHandle eh(elm,false);
EditElementHandle eeh(elm, true, true, 0);

TextBlockPtr textBlock = TextHandlerBase::GetFirstTextPartValue(eh);
CaretPtr searchStart =textBlock->CreateStartCaret();
CaretPtr searchEnd = textBlock->CreateEndCaret();
ITextQueryCP textQueryHandler = eh.GetITextQuery();

if (!textQueryHandler || !textQueryHandler->IsTextElement(eh)) {
return;
}


ITextEditP textEdit = eh.GetITextEdit();
ITextPartIdPtr textPart;
TextBlockPtr textBlock = textEdit->GetTextPart(eh, *textPart);

if (!textBlock.IsNull() && !textBlock->IsEmpty())
{
WString strW = textBlock->ToString();
 CaretPtr startCaret = textBlock->CreateStartCaret();
  CaretPtr endCaret = textBlock->CreateEndCaret();
   WString originalString = textBlock->ToString();


if (0 == originalString.compare(L"TEXT_TO_MATCH"))

{
std::wostringstream oss;
oss << "this is a test";
textBlock->ReplaceText(oss.str().c_str(), *textBlock->CreateStartCaret(), *textBlock->CreateEndCaret());//CHANGES TEXT IN TEXTBLOCK, BUT NOT IN DESIGN FILE
textBlock->PerformLayout();//DESPERATE TEST TO SEE IF IT WOULD UPDATE FILE... BUT NO
textEdit->ReplaceTextPart(eeh, *textPart, *textBlock);

//THESE REMAIN FROM MY FAILED TESTS BELOW
//ElementRefP erp = eeh.GetElementRef();
//MSElementCP msecp = eeh.GetElementCP();


//eeh.ReplaceInModel(erp); //doesn't work
//eeh.ReplaceElement(msecp); doesn't work
//textBlock->AppendText(oss.str().c_str());//doesn't work
//eeh.AddToModel();//will add element to file, keeps old element this works.
}

}

}

  • Hi John,

    please (it seems I have to ask everybody) read and follow this forum best practices!

    • There is recommended subject format [<product> <version> <API>] <Issue> that helps to navigate in the list of posts and also provides basic information about the issue context.
    • Specify what product and version do you use (e.g. there have been 17 different versions of MicroStation CE released so far).
    • Always use Insert > Insert code tool every time you share any code snippet (even one line). To read code formatted as a plain text is confusing.
    but my ability to modify the existing simple text string seems to be lacking something

    Did you search this forum for similar discussions? How to edit text using C++ API was discussed several times.

    EditElementHandle eeh(elm, true, true, 0);

    I am not sure whether 0 is right.

    To use explicit ISessionMgr::GetActiveDgnModelP() is better in my opinion.

    //eeh.ReplaceInModel(erp); //doesn't work

    EditElementHandle::ReplaceInModel() method is (I think) the only correct method how to update element existing in DGN file.

    But your code does not tell too much. What value is returned by the method?

    I am fairly new to programming for Microstation, are there any books explaining how to program for Microstation using Visual Studio 2017 in c++?

    Please respect the rule "one post = one topic" and do not merge more problems into one discussion. But anyway...

    There is no official book for MicroStation developers.

    The best sources are MicroStationAPI help file, combined with examples delivered with MicroStation SDK.

    Plus, Bentley Developer Portal lists a few more sources (e.g. MicroStation Programming blog is not bad).

    Personally, I think one from the best sources is this forum, because typical problems were discussed here already.

    With regards,

      Jan

  • Hello Jan,
    Thanks for the response... .yes, I figured I'd get spanked on the format... I wasn't sure on the tools for posting or formatting,in fact, I hit tab a few times and it bounced me off the page and made me gasp... I was worried I'd lost my input.
    I looked for an "about" under help for the info you've requested but it doesn't exist there.  I'm using Microstation Connect Edition release 14.  I am going to need to convert all our old MDL apps for the new Microstation.
    Sorry about the formatting, and thanks for the info.  I'll do more digging to find out why my ReplaceInModel isn't working.
    Thanks again,
    John Schenk

  • I am scanning a file for text and pass the MSElementDescr to my function

    Here's a MicroStationAPI way to enumerate elements in a model.  The enumeration gives you an immutable ElementHandle.  If you want to modify it, construct an EditElementHandle from it. 

    You may want to test the element first, using ITextQuery.IsTextElement (ElementHandleRC el), as you have done in your code above.  Look in the IncrementText example to see how to implement that test.

    From the MicroStationAPI help about TextBlock: To create a free-standing text element from a TextBlock, use TextHandlerBase::CreateElement.  That creates a new EditElementHandle, which you then add to the DGN model.

     
    Regards, Jon Summers
    LA Solutions

  • Hello Jan, I'm still struggling with no success trying to change a simple text string from aaaaaa to bbbbb.  Is there a working example that will do this somewhere that I can see what I'm missing?  All I can find in the provided examples are number increments and property changes.
    Thanks,
    John

  • Still nothing, 2 weeks of simply trying to modify an existing text string.  Non of the examples demonstrate what I'm trying to do, which is change a string of text in a design file from aaaa to bbbbbbbb.  There has to be something missing, the "eeh.ReplaceInModel(erp)" shown in my code above returns a status of 32768.  But then, none of the other examples show any type of element rewrite back into the file, simply change the textblock using textBlock->ReplaceText(ts.GetWCharCP(), *caretStart, *caretEnd) according to the sample routines should do the trick, but doesn't.

    Help....please!

  • I've created a new version of the ModifyTextTool example.  It has two key-ins: CHANGECASE and CHANGETEXT.

    • CHANGECASE is the existing command
    • CHANGETEXT new text is new and implements text substitution

    Both commands behave the same idiosyncratic way as the original example: the text element isn't replaced, but a new text element is created with the substitution. 

    ModifyTextTool.zip

     
    Regards, Jon Summers
    LA Solutions

  • Hello Jon, 

    "the text element isn't replaced, but a new text element is created with the substitution. "

    I have no problems "adding" the text to my file... my whole problem is to simply alter the existing text string characters.  My text strings contain MSLINKS that need to remain in place, so I want to modify the existing string from stringY to stringX but not delete, or add.

    CaretPtr caretStart = textBlock->CreateStartCaret();

    CaretPtr caretEnd = textBlock->CreateEndCaret();

    textBlock->ReplaceText(changeText.c_str(), *caretStart, *caretEnd)

    changing the variable "changeText shows as altered text in my textBlock, but not in my design file. textBlock->ToString() shows the correct value, but no change in the design file.

    Is is possible to simply change the existing string by not adding or deleting?

    Thanks for your efforts.

    John

  • I've published an article about an example ReplaceTextTool.

    It adds some commands to replace text, rather than copy text as in the SDK example.  In particular, it shows how to extract text from a TextBlock, and how to update the text in a TextBlock.

     
    Regards, Jon Summers
    LA Solutions

    Answer Verified By: John Schenk 

  • Thanks SOOOO much Jon, this is exactly what I needed!!!!!!

  • Please use the 'This Answered My Question' button to mark this thread as answered. 

     
    Regards, Jon Summers
    LA Solutions