#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#

from __future__ import division
import math
import pcbnew

import HelpfulFootprintWizardPlugin
import PadArray as PA


class SMDElCapWizard(HelpfulFootprintWizardPlugin.HelpfulFootprintWizardPlugin):

    def GetName(self):
        return "CAPELSMD"

    def GetDescription(self):
        return "SMD Electrolytic Capacitor Footprint Wizard v1.2"

    def GenerateParameterList(self):
        self.AddParam("Pads", "size", self.uMM, 10)
        self.AddParam("Pads", "height", self.uMM, 10)
        self.AddParam("Pads", "pad clearance", self.uMM, 4)
        self.AddParam("Pads", "pad width", self.uMM, 1.5)
        self.AddParam("Pads", "pad length", self.uMM, 5)
        #self.AddParam("Pads", "marking width", self.uMM, 2)

    def CheckParameters(self):
        return
     
    def GetReferencePrefix(self):
        return "C"

    def GetValue(self):
        size = self.parameters["Pads"]["size"] / 100000
        height = self.parameters["Pads"]["height"] / 100000
        size, sfrag = divmod(size, 10)
        height, hfrag = divmod(height, 10)
        if sfrag==0:
            t_size = "%d" % size
        else:
            t_size = "%d.%d" % (size, sfrag)
        if hfrag==0:
            t_height = "%d" % height
        else:
            t_height = "%d.%d" % (height, hfrag)
        return "c_elec_%sx%s" % (t_size, t_height) 

    def BuildThisFootprint(self):
        pads = self.parameters["Pads"]
        size = pads["size"]
        height = pads["height"]
        pad_length = pads["pad length"]
        pad_width = pads["pad width"]
        pad_offset = pads["pad clearance"]
        #m_width = pads["marking width"]
        m_width = size / 5
        first_pad = PA.PadMaker(self.module).SMDPad(pad_width, pad_length, pcbnew.PAD_RECT)
        normal_pad = PA.PadMaker(self.module).SMDPad(pad_width, pad_length, pcbnew.PAD_OVAL)        
        pin1Pos = pcbnew.wxPoint(0, 0)
        array = PA.PadLineArray(normal_pad, 2, -(pad_length + pad_offset), False, pin1Pos)
        array.SetFirstPadInArray(1)
        array.SetFirstPadType(first_pad)
        array.AddPadsToModule(self.draw) 
        r = size / 2 - 350000
        #self.draw.Circle(0,0,r)
        m_clearance = pcbnew.FromMM(0.5)
        arc_starty = (pad_width / 2) + m_clearance
        angle_intercept = math.acos(arc_starty / r)
        sx = -(math.sin(angle_intercept) * r)                
        self.draw.Arc(0, 0, sx, -arc_starty, (2 * angle_intercept) * 572.9578)
        self.draw.Arc(0, 0, -sx, arc_starty, (2 * angle_intercept) * 572.9578)
        #m_offs = self.draw.GetWidth() / 1.1 # marking line offset
        m_offs = self.draw.dc["lineThickness"] / 1.1
        m_num = int(round((m_width-(r+sx)) / m_offs, 0))        
        for i in range(1,m_num):
            offs = (r+sx) + i * m_offs
            angle_intercept = math.acos((r-offs)/r)
            y = -(math.sin(angle_intercept) * r - m_offs / 2)
            self.draw.VLine(-r+offs,y,abs(y)-arc_starty) 
            self.draw.VLine(-r+offs,arc_starty,abs(y)-arc_starty)      
        self.draw.HLine(sx,arc_starty,(m_num - 1) * m_offs) 
        self.draw.HLine(sx,-arc_starty,(m_num - 1) * m_offs)
        notch = size / 6        
        self.draw.Polyline([
            (- size / 2, - arc_starty),
            (- size / 2, - size / 2),
            (size / 2 - notch, - size / 2),
            (size / 2, - (size / 2 - notch)),
            (size / 2, - arc_starty)
        ])  
        self.draw.Polyline([
            (- size / 2, arc_starty),
            (- size / 2, size / 2),
            (size / 2 - notch, size / 2),
            (size / 2, size / 2 - notch),
            (size / 2, arc_starty)
        ])             
        text_size = pcbnew.FromMM(1.2) 
        text_offset = text_size + size / 2
        self.draw.Value(0, text_offset, text_size)
        self.draw.Reference(0, -text_offset, text_size)

SMDElCapWizard().register()
