Skip to main content
1-Visitor
July 31, 2015
Question

Does anyone have examples of using arrays in SmartAssembly. I am struggling with the admin guide examples.

  • July 31, 2015
  • 1 reply
  • 1933 views

I do not understand why the DELETE_ARRAY_ELEM won't work....

                SEARCH_DRW_VIEWS CurrentDrawing "*" "*" ViewArray

                DECLARE_VARIABLE INTEGER DeleteIndex 0

                FOR EachView REF ARRAY ViewArray

                                GET_DRW_VIEW_ORIENTATION EachView ViewOrientation

                                PRINT "before_EachView = % ViewOrientation = %" EachView ViewOrientation

                END_FOR

                !@@@ error checking for background and overlay views....

                FOR EachView REF ARRAY ViewArray

                                GET_DRW_VIEW_ORIENTATION EachView ViewOrientation

                                IF ViewOrientation == "UNKNOWN"

                                                DELETE_ARRAY_ELEM ViewArray DeleteIndex

                                END_IF

                                DeleteIndex++

                END_FOR

                FOR EachView REF ARRAY ViewArray

                                GET_DRW_VIEW_ORIENTATION EachView ViewOrientation

                                PRINT "after_EachView = %  ViewOrientation = %" EachView ViewOrientation

                END_FOR


This thread is inactive and closed by the PTC Community Management Team. If you would like to provide a reply and re-open this thread, please notify the moderator and reference the thread. You may also use "Start a topic" button to ask a new question. Please be sure to include what version of the PTC product you are using so another community member knowledgeable about your version may be able to assist.

1 reply

23-Emerald IV
July 31, 2015

I think as soon as you execute DELETE_ARRAY_ELEM, the number of elements will shift and your DeleteIndex will no longer agree with the current array element.

23-Emerald IV
July 31, 2015

Actually, I don't think that's it.  It looks like you cannot use DELETE_ARRAY_ELEM when inside a FOR loop that is processing that same array.  Here is the sample code I used to test this:

DECLARE_ARRAY TEST_ARRAY
ADD_ARRAY_ELEM TEST_ARRAY 0
ADD_ARRAY_ELEM TEST_ARRAY 1
ADD_ARRAY_ELEM TEST_ARRAY 2
ADD_ARRAY_ELEM TEST_ARRAY 3
ADD_ARRAY_ELEM TEST_ARRAY 4

DECLARE_VARIABLE INTEGER DeleteIndex 0

FOR EACH_ELEM REF ARRAY TEST_ARRAY
  PRINT "Loop 1 Value: %, Index Value: %" EACH_ELEM DeleteIndex
  IF EACH_ELEM == "1" OR EACH_ELEM == "3"
   DELETE_ARRAY_ELEM TEST_ARRAY DeleteIndex
  END_IF
  DeleteIndex ++
END_FOR

FOR EACH_ELEM REF ARRAY TEST_ARRAY
  PRINT "Loop 2 Value: %" EACH_ELEM
END_FOR

DELETE_ARRAY_ELEM TEST_ARRAY 3

FOR EACH_ELEM REF ARRAY TEST_ARRAY
  PRINT "Loop 3 Value: %" EACH_ELEM
END_FOR

23-Emerald IV
July 31, 2015

Correction.  It does work fine.  I was checking for a string instead of an integer. (Thanks to Hatim from Sigmaxim.)  The compare line should say:

  IF EACH_ELEM == 1 OR EACH_ELEM == 3

Also, removing a array element instantly changes the array size, so you need to decrement the DeleteIndex variable accordingly:

IF EACH_ELEM == 1 OR EACH_ELEM == 3

    DELETE_ARRAY_ELEM TEST_ARRAY DeleteIndex

    DeleteIndex = DeleteIndex - 1

END_IF

Final code:

BEGIN_ASM_DESCR

  BEGIN_CATCH_ERROR

 

    DECLARE_ARRAY TEST_ARRAY

    ADD_ARRAY_ELEM TEST_ARRAY 0

    ADD_ARRAY_ELEM TEST_ARRAY 1

    ADD_ARRAY_ELEM TEST_ARRAY 2

    ADD_ARRAY_ELEM TEST_ARRAY 3

    ADD_ARRAY_ELEM TEST_ARRAY 4

    DECLARE_VARIABLE INTEGER DeleteIndex 0

   

    FOR EACH_ELEM REF ARRAY TEST_ARRAY

        PRINT "Loop 1 Value: %, Index Value: %" EACH_ELEM DeleteIndex

        IF EACH_ELEM == 1 OR EACH_ELEM == 3

            DELETE_ARRAY_ELEM TEST_ARRAY DeleteIndex

            DeleteIndex = DeleteIndex - 1

        END_IF

        DeleteIndex ++

    END_FOR

    FOR EACH_ELEM REF ARRAY TEST_ARRAY

        PRINT "Loop 2 Value: %" EACH_ELEM

    END_FOR

  

    DELETE_ARRAY_ELEM TEST_ARRAY 1

   

    FOR EACH_ELEM REF ARRAY TEST_ARRAY

        PRINT "Loop 3 Value: %" EACH_ELEM

    END_FOR

   

  END_CATCH_ERROR   

END_ASM_DESCR