Page 1 of 1

Delete all consolidated elements

Posted: Sat Oct 24, 2009 6:43 am
by chesteronrada
Hi...
need help with my TI...
i have this code to delete all consolidated elements(elements with type 'C') from my Product Dimension.

#######################
DimName = 'ProductDim';
eCnt = DIMSIZ(DimName);
x=1;
WHILE(x<=eCnt);
ElName = DIMNM(DimName,x);
IF(DTYPE(DimName,ElName)@='C');
DimensionElementDelete(DimName, ElName);
ENDIF;
x=x+1;
END;
#######################

but it does not work on a one time run. i have to run this a couple of times to make it work T_T

Re: Delete all consolidated elements

Posted: Sat Oct 24, 2009 7:08 am
by Alan Kirk
chesteronrada wrote:Hi...
need help with my TI...
i have this code to delete all consolidated elements(elements with type 'C') from my Product Dimension.

#######################
DimName = 'ProductDim';
eCnt = DIMSIZ(DimName);
x=1;
WHILE(x<=eCnt);
ElName = DIMNM(DimName,x);
IF(DTYPE(DimName,ElName)@='C');
DimensionElementDelete(DimName, ElName);
ENDIF;
x=x+1;
END;
#######################

but it does not work on a one time run. i have to run this a couple of times to make it work T_T
It's because you're counting forward, so every time you delete a consolidation it changes the DimNm index position of the next element.

Suppose that you have a dimension which has the following elements at index positions 1 to 5:
1/ N element 1
2/ C element 1
3/ C Element 2
4/ N Element 2
5/ C element 3

What will happen is that when x=2, you'll delete C element 1. However in doing so you cause C Element 2 to move up to index position 2. But because you've increased x by 1 in the loop, the next element it will look at is the new element 3, which will be N Element 2. Effectively C Element 2 is never checked, which is why it's not deleted in the first pass.

The solution?

Instead of counting forward, count backward from the end of the dim. The deletions will therefore never change the index order of the elements that the loop has yet to look through.

Code: Select all

DimName = 'ProductDim';

eCnt = DIMSIZ(DimName);

x = eCnt;

WHILE( x >= 1 );

   ElName = DIMNM( DimName,x);

   IF( DTYPE( DimName,ElName) @= 'C');
      DimensionElementDelete( DimName, ElName);
   ENDIF;

   x = x - 1;

END;


Re: Delete all consolidated elements

Posted: Sat Oct 24, 2009 7:22 am
by chesteronrada
noted... :shock:
i am sure still is a newbie :D
thanks a lot!