0

I have problem with deleting steps from scenario in Add-in for Enterprise Architect

I want delete empty steps of scenario from element, but its not working, this "deleted" steps exists in this scenario. Where is mistake in my code?

short esCnt = element.Scenarios.Count;
                for (short esIdx = (short)(esCnt - 1); esIdx >= 0; --esIdx)
                {
                    EA.IDualScenario es = element.Scenarios.GetAt(esIdx);
                    short essCnt = es.Steps.Count;
                    for (short essIdx = (short)(essCnt - 1); essIdx >= 0; --essIdx)
                    {
                        EA.IDualScenarioStep ess = es.Steps.GetAt(essIdx);
                        if (ess.Name.Trim().Length == 0 &&
                            ess.Uses.Trim().Length == 0 &&
                            ess.Results.Trim().Length == 0)
                        {
                            //1. section 
                            es.Steps.Delete(essIdx);
                            ess.Update();
                        }
                    }
                    //2. section 
                    es.Update();
                }

Do you have any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Thank you for the quick response, I tried to changed my code as You suggest, but I still need help. These empty rows are not deleted, there are only moved on the end of the steps collection. I tried delate, update and refresh in different sections but still was uncorrect. Could You help me? – user3274759 Jun 03 '14 at 11:47
  • I corrected code in question, it's work but...This "deleted" steps still exists in this scenario. These empty rows are not deleted, there are only moved on the end of the steps collection. I tried used delate(), update() and refresh() in different sections but still was uncorrect. I think it is not correct in API. – user3274759 Jun 03 '14 at 12:24

2 Answers2

0

Looks like off-by-one. The Collection index is zero-based, but the scenario step numbering in the GUI, as reflected in ScenarioStep.Pos, starts from 1. So you might in fact be deleting the wrong steps.

To be on the safe side, you shouldn't use a foreach loop when you're making changes to the collection you're looping over, but a reverse for loop:

int nrScenarios = element.Scenarios.Count;
for (int scenIx = nrScenarios - 1; scenIx >= 0; --scenIx) {
    Scenario scenario = element.Scenarios.GetAt(scenIx);
    int nrSteps = scenario.Steps.Count;
    for (int stepIx = nrSteps - 1; stepIx >= 0; --stepIx) {

In this case, not as important in the outer loop as in the inner, since that's the collection you're manipulating.

Other than that, you shouldn't need to call es.Update() at all, and element.Scenarios.Refresh() should be called outside the inner loop.

Finally, are you sure that Step.Name is actually empty? I'm unable to create steps with empty names ("Action") in the GUI, but you might have been able to do it through the API.

Uffe
  • 10,396
  • 1
  • 33
  • 40
  • I corrected code in question, it's work but...This "deleted" steps still exists in this scenario. These empty rows are not deleted, there are only moved on the end of the steps collection. I tried used delate(), update() and refresh() in different sections but still was uncorrect. I think it is not correct in API – user3274759 Jun 03 '14 at 13:02
0

I think the problem lies in the Update() calls.

The EA.Collection.DeleteAt() will immediately delete the element from the database (but not from the collection in memory). If however you call Update() on the object you just created I think it will recreate it, possibly with a new sequence number; which explains why the deleted steps are now "moved" to the end.

Try removing the Update() call and see if that helps.

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50