

#1. filter all cells with Fleld and PA/PB in cellname
#2. check sub-cells in "field" cell, if with DARK and PA/PB in cell name add to array
#3. return a arraay with unique cell names
def filterCells()
    layout        = RBA::Application.instance.main_window.current_view.active_cellview().layout     
    cellNameArray = Array.new
    
    layout.each_cell do |eachCell|
        cellName = eachCell.name
        if cellName.upcase.match("FIELD") && (cellName.upcase.match("PA") || cellName.upcase.match("PB")) then
            eachCell.each_inst do |eachInst|
                instCellName = eachInst.cell.name
                if instCellName.upcase.match("DARK") && (cellName.upcase.match("PA") || cellName.upcase.match("PB")) then
                    cellNameArray << instCellName
                end
            end
        end
    end
    return cellNameArray.uniq.sort
end


#1. get cell by name
#2. get shapes in that cell
#3. get holes in those shapes
#4. return the bound box for hole
def cellLayerHoleDBox(cellName, layer, datatype)
    layout        = RBA::Application.instance.main_window.current_view.active_cellview().layout    
    cell          = layout.cell(cellName)
    cellLayer     = layout.layer(layer, datatype)
    cellShapesReg = RBA::Region::new(cell.begin_shapes_rec(cellLayer))
    partAReg      = cellShapesReg.holes
    return RBA::DBox.new(partAReg.bbox * layout.dbu)
end


#1. show cell as new top and show all layer, cells and clear annotations
#2. add annotations
#3. zoom to box 
#4. save image to file
def customScreenShot(cellName, savePath, imageWidth, imageHeight, rulers = [], zoomBox = None, scaleBar = false)
    layoutView    = RBA::Application.instance.main_window.current_view         
    cellView      = layoutView.active_cellview() 
    layout        = cellView.layout
    rulerSetting  = layoutView.get_config("grid-show-ruler")
    cellView.cell = layout.cell(cellName)
    
    layoutView.max_hier
    layoutView.show_all_cells
    layoutView.clear_annotations
    
    layoutView.each_layer do |layerProperties|
        layerProperties.visible = true
    end

    rulers.each do |ruler|
        layoutView.insert_annotation(ruler)
    end     
    layoutView.set_config("grid-show-ruler", scaleBar.to_s)
    layoutView.zoom_box(RBA::DBox::new(*zoomBox))
    screenImage  = layoutView.save_image(savePath, imageWidth, imageHeight)
    layoutView.set_config("grid-show-ruler", rulerSetting.to_s)
end


def normalRuler(line = [])
    ruler               = RBA::Annotation.new()
    ruler.p1            = RBA::DPoint.new(line[0], line[1])
    ruler.p2            = RBA::DPoint.new(line[2], line[3])
    ruler.main_position = 3
    ruler.outline       = 0
    ruler.style         = RBA::Annotation::StyleArrowBoth
    return [ruler]
end



def extendedRuler(line = [], extension = 3500, rulerOffset = 200)
    layoutView  = RBA::Application.instance.main_window.current_view      
    x1, y1      = line[0], line[1]
    x2, y2      = line[2], line[3]
    dx, dy      = (x2 - x1), (y2 - y1)
    dl          = ((dx ** 2) + (dy ** 2)) ** (0.5)
    rulerOffset = (extension.abs > rulerOffset.abs) ? (extension.abs - rulerOffset.abs) * (extension <=> 0) : extension
    x1_e, y1_e  = x1 + extension   * (dy/dl), y1 - extension   * (dx/dl)
    x2_e, y2_e  = x2 + extension   * (dy/dl), y2 - extension   * (dx/dl)
    x1_r, y1_r  = x1 + rulerOffset * (dy/dl), y1 - rulerOffset * (dx/dl)
    x2_r, y2_r  = x2 + rulerOffset * (dy/dl), y2 - rulerOffset * (dx/dl)
    
    ruler       = RBA::Annotation.new()
    bar1        = RBA::Annotation.new()
    bar2        = RBA::Annotation.new()
    ruler.p1    = RBA::DPoint.new(x1_r, y1_r)
    ruler.p2    = RBA::DPoint.new(x2_r, y2_r)
    bar1.p1     = RBA::DPoint.new(x1,   y1  )
    bar1.p2     = RBA::DPoint.new(x1_e, y1_e)
    bar2.p1     = RBA::DPoint.new(x2,   y2  )
    bar2.p2     = RBA::DPoint.new(x2_e, y2_e)
        
    ruler.main_position = 3
    ruler.outline       = 0
    ruler.style         = RBA::Annotation::StyleArrowBoth
    bar1.style          = RBA::Annotation::StyleLine
    bar2.style          = RBA::Annotation::StyleLine
    bar1.fmt            = ""
    bar2.fmt            = ""

    return [ruler, bar1, bar2]
end

#1. loop through cell name list provided by filterCells() function
#2. get layer hole bound box by cellLayerHoleDBox() function
#3. creat annotation line by bound box
#4. take screenshot using customScreenShot() function

imgW, imgH    = 1000, 500
zoomBox       = [-34000.0, 0, 34000.0, -26700.0]
cellNameArray = filterCells()
cellNameArray.each do |cellName|
    hDBox    = cellLayerHoleDBox(cellName, 2, 0)
    savePath = "D:\\#{cellName}.png"
    rulers = []
    
    if cellName.match("PA") then
        rulers = [
            *normalRuler([hDBox.p1.x, hDBox.p1.y, hDBox.p2.x, hDBox.p2.y])
        ]
    end

    if cellName.match("PB") then
        rulers = [
            *extendedRuler([hDBox.p1.x, hDBox.p1.y, hDBox.p2.x, hDBox.p2.y], -6500),
            *extendedRuler([hDBox.p1.x, hDBox.p1.y, hDBox.p1.x, hDBox.p2.y], 36000),
            *extendedRuler([hDBox.p1.x, hDBox.p2.y, hDBox.p2.x, hDBox.p2.y], 20000)
        ]
    end
    customScreenShot(cellName, savePath, imgW, imgH, rulers, zoomBox, false)        
end



