Loop All Items (code snippet)

Description

Provides an iterator for items in the pointer system. If called with one or more parameters it will only process those record types. Calling with no parameters processes all record types (fh V6 has 9 types).

Requires: None

Code

AllItems.fh_lua
function allItems(...)
    local iTypeCount = nil
    local iPos = 1
    local p1 = fhNewItemPtr()
    local p2 = fhNewItemPtr()
    local tblRecTypes = {}
 
    if arg['n'] == 0 then
    --  No parameter do all Record Types
        iTypeCount = fhGetRecordTypeCount() -- Get Count of Record types
        for i = 1,iTypeCount do
            tblRecTypes[i] = fhGetRecordTypeTag(i)
        end
    else
        --  Got Parameters Process them instead
        tblRecTypes = arg
        iTypeCount = arg['n']
    end    
    print(tblRecTypes[iPos],iPos)
    p1:MoveToFirstRecord(tblRecTypes[iPos])
    return function()
        repeat
        while p1:IsNotNull() do
            p2:MoveTo(p1)
            p1:MoveNextSpecial()
            if p2:IsNotNull() then
                return p2
            end
        end
        -- Loop through Record Types
        iPos = iPos + 1
        if iPos <= iTypeCount then
            p1:MoveToFirstRecord(tblRecTypes[iPos])
        end
        until iPos > iTypeCount
    end
end

Usage

--  List everyitem in Objects and Family Records
for ptr in allItems('OBJE','FAM') do
    print(fhGetTag(ptr),fhGetDisplayText(ptr))
end
--  List every item in all record types
for ptr in allItems() do
    print(fhGetTag(ptr),fhGetDisplayText(ptr))
end