Hello,
I am trying to change the Text of a cell with the Microstation C++ Api.
The Code I am using is working well on normal Text Elements. But in a Cell the "ReplaceInModel" returns an Error (69645)
void TextTool::textChange(EditElementHandleP eehIn) { int result=0; switch (eehIn->GetElementType()) { case CELL_HEADER_ELM: { for (ChildEditElemIter eehChild(*eehIn); eehChild.IsValid(); eehChild=eehChild.ToNext()) { textChangeTest(&eehChild); } break; } case TEXT_ELM: { TextBlockPtr textBlock = TextHandlerBase::GetFirstTextPartValue(*eehIn); ITextEditP textEdit = eehIn->GetITextEdit(); // Extract the TextBlock from the Text element. if (!textBlock.IsNull() && !textBlock->IsEmpty()) { //Replace Text CaretPtr startCaret = textBlock->CreateStartCaret(); CaretPtr endCaret = textBlock->CreateEndCaret(); textBlock->ReplaceText(L"newText", *textBlock->CreateStartCaret(), *textBlock->CreateEndCaret()); //create new Element and replace in Model EditElementHandle newEeh; ElementHandle templateEh(eehIn->GetElementP(), ACTIVEMODEL); TextHandlerBase::CreateElement(newEeh, &templateEh, *textBlock); //Replace in model. Working on single textelements but not in cells...? result =newEeh.ReplaceInModel(eehIn->GetElementRef()); wprintf(L"Result replaceInModel: %d\n", result); } break; } } }
Can somebody help me?
Regards
Manuel Höger
Manuel Höger said: in a Cell the "ReplaceInModel"
Never rewrite a component of a complex element, such as a cell, directly.
Regards, Jon Summers LA Solutions
Hi,
1. Thanks a lot for the quick reply!
2. How can I replace it in the complex element? I tried with RelaceElement(..) but didn't work as well.
And do I have to make a copy of the cell Elementhandle first?
3. How to rewrite? do i have to use mdlElmdscr_rewrite or is there a more modern way?
Manuel Höger said:How can I replace it in the complex element?
Each member of a ChildEditElemIter is an EditElementHandle. You should be able to use EditElementHandle.ReplaceElement or EditElementHandle.ReplaceElementDescr.
ChildEditElemIter
EditElementHandle
EditElementHandle.ReplaceElement
EditElementHandle.ReplaceElementDescr
Manuel Höger said:Do I have to make a copy of the cell Elementhandle first?
Your ChildEditElemIter is a copy of the original cell.
Manuel Höger said:How to rewrite?
EditElementHandle.ReplaceInModel()
Answer Verified By: Manuel Höger
Ah I thaught childEditElemIter is the first childElement.
I tried your proposal in the following way but it still doesn't work. replace Element with the Textelement succeeds but the replace in Model still errors. Did I misunderstand you in any kind? I'm still confused about this childEditElemIter...
void StampTool::textChangeTest(EditElementHandleP eehIn) { int res=0; switch (eehIn->GetElementType()) { case CELL_HEADER_ELM: { ChildEditElemIter eehChild(*eehIn); for (eehChild; eehChild.IsValid(); eehChild=eehChild.ToNext()) { textChangeTest(&eehChild); } //Replace in Model sends Error res=eehChild.ReplaceInModel(eehIn->GetElementRef()); wprintf(L"Result replaceinModel: %d\n", res); break; } case TEXT_ELM: { TextBlockPtr textBlock = TextHandlerBase::GetFirstTextPartValue(*eehIn); ITextEditP textEdit = eehIn->GetITextEdit(); // Extract the TextBlock from the Text element. if (!textBlock.IsNull() && !textBlock->IsEmpty()) { //Replace Text CaretPtr startCaret = textBlock->CreateStartCaret(); CaretPtr endCaret = textBlock->CreateEndCaret(); wprintf(L"old Text: %s\n", textBlock->ToString()); textBlock->ReplaceText(L"newText", *textBlock->CreateStartCaret(), *textBlock->CreateEndCaret()); wprintf(L"new Text: %s\n", textBlock->ToString()); //create new Element and replace in Model EditElementHandle newEeh; ElementHandle templateEh(eehIn->GetElementP(), ACTIVEMODEL); TextHandlerBase::CreateElement(newEeh, &templateEh, *textBlock); //Replace Element succeeds: res=eehIn->ReplaceElement(newEeh.GetElementCP()); wprintf(L"Result replaceElement: %d\n", res); } break; }
Ok I got it. it's working.
As I understood, the Solution is to replace the incomming ElementReference (contained by the eehIn) with its changed Content?
Working Code:
void StampTool::textChangeTest(EditElementHandleP eehIn) { int res=0; switch (eehIn->GetElementType()) { case CELL_HEADER_ELM: { ChildEditElemIter eehChild(*eehIn); for (eehChild; eehChild.IsValid(); eehChild=eehChild.ToNext()) { textChangeTest(&eehChild); } //Replace in Model sends Error res=eehIn->ReplaceInModel(eehIn->GetElementRef()); wprintf(L"Result replaceinModel: %d\n", res); break; } case TEXT_ELM: { TextBlockPtr textBlock = TextHandlerBase::GetFirstTextPartValue(*eehIn); ITextEditP textEdit = eehIn->GetITextEdit(); // Extract the TextBlock from the Text element. if (!textBlock.IsNull() && !textBlock->IsEmpty()) { //Replace Text CaretPtr startCaret = textBlock->CreateStartCaret(); CaretPtr endCaret = textBlock->CreateEndCaret(); wprintf(L"old Text: %s\n", textBlock->ToString()); textBlock->ReplaceText(L"newText", *textBlock->CreateStartCaret(), *textBlock->CreateEndCaret()); wprintf(L"new Text: %s\n", textBlock->ToString()); //create new Element and replace in Model EditElementHandle newEeh; ElementHandle templateEh(eehIn->GetElementP(), ACTIVEMODEL); TextHandlerBase::CreateElement(newEeh, &templateEh, *textBlock); //Replace Element succeeds: res=eehIn->ReplaceElement(newEeh.GetElementCP()); wprintf(L"Result replaceElement: %d\n", res); } break; } }
Thanks very very much for your Help!!!!
Now I just have do do this with a Cell from a Cell lib. If you have some advice about this, that would be great!
Best Regards
Manuel Höger said: it's working
Thanks for letting us know! Please mark your post as Answered.
Manuel Höger said:Now I just have do do this with a Cell from a Cell lib. If you have some advice about this
Please post a new question for this new topic.