Up until this point we have focused on separate pieces of the DCAL language. We have seen small samples of code but have not really put the whole of our knowledge to the test. In this lesson we will write an entire macro from start to finish.
The macro we will write will change layer names. The layers will be searched for a certain sequence of characters and will replace these characters. For example, lets say that we use a layer naming system that is based on a numerical prefix system. We begin all the layer names associated with the cellar/foundation levels of our project with '00.' Layers associated with the first floor are started with '01.' With such a system it may be a nice thing if we could change all the 03 layers to read 06. Assuming we have more than one or two layers in the 03 range, this could get tedious. This is where our macro will come in.
The complete code for this macro is available via the DDN Open Source Exchange.
Step One - Thinking it through
The first step in any programming task is thinking through what the desired objective is and how we can arrive at this objective. It pays to be very clear with the both the objective and the steps we will take to arrive there. So lets make some notes
Objective: To allow search and replace of strings within layer
(You should recall from Lesson 3 'Types' what a string is).
That sounds pretty clear. Now how can we go about doing this? At this point we should write down the steps needed to accomplish the objective. It is not a good idea to use coding terms at this point. Try to be more general and write the steps in plain English (or your favorite local language)
Show Function Keys
Get string to search for
Get string to replace with
Get first layer
Check name of first layer to see if it contains our search string
If the string is found replace it with the replacement string
Get next layer and repeat the process until all layers have been checked
That pretty much sums up how we will do it. Notice that we haven't used DCAL or programming terms to elaborate on the tasks. We will use this outline as the basis for the macro.