<commit_before>'''
Created on Apr 3, 2012

@author: Dan
'''
from math import sin,sqrt,atan2,cos,radians,fabs,degrees
from LayerIds import getLayerId, makeViaMask

class Line(object):
    __slots__=('x1','y1','x2','y2',"cX","cY",'width','layer','curve','radius','sAngle','eAngle')
    
    def __init__(self,node,converter,noTranspose=False, offset=None):
        self.getWireInfo(node,converter,noTranspose, offset)
        
    def getWireInfo(self,wire,converter,noTranspose, offset):
        """
        Converts a wire dictionary node from Eagle to a usable form for kicad
        It then returns the dictionary of the wires info
        
        Params:
            wire:    the wire's node from myDict
            noTranspose: if true, coordinates will not be transposed
                            to screens center
        Returns:
            dict:    the wires info in a kicad usable dictionary form
                    Keys = x1,x2,y1,y2,layer,curve,width
            
        """
            
        x1,y1=converter.convertCoordinate(wire.get('x1'),wire.get('y1'),noTranspose)
        x2,y2=converter.convertCoordinate(wire.get('x2'),wire.get('y2'),noTranspose)
        curve=wire.get('curve')
        
        width=converter.convertUnit(wire.get('width'))
        layer=getLayerId(wire.get('layer'))
        
        
        if curve==None:
            cX=(x1+x2)//2
            cY=(y1+y2)//2
            radius = 0
            sAngle = 0
            eAngle = 0
        else:
            cX,cY,curve,radius,sAngle,eAngle=self.getWireArcInfo(wire,converter,noTranspose)
        
        if offset != None:
            dX, dY = converter.convertCoordinate(offset[0], offset[1], noTranspose)
            x1 += dX
            y1 += dY
            x2 += dX
            y2 += dY
            cX += dX
            cY += dY
            
        self.x1=        str(x1)
        self.y1=        str(y1)
        self.x2=        str(x2)
        self.y2=        str(y2)
        self.cX=        str(cX)
        self.cY=        str(cY)
        self.width=     str(width)
        self.layer=     str(layer)
        self.curve=     str(curve)
        self.radius=    str(radius)
        self.sAngle=    str(sAngle)
        self.eAngle=    str(eAngle)

    def getWireArcInfo(self,arc,converter, noTranspose=False,noInvert=False):
        """
        Converts between Eagle's arc definitions, and kicads arc definitions
        Eagle holds the two endpoints and the degrees of the sweep ccw betweeen them
        
        Kicad holds the center point, and an enpoint, and a degree sweep CW from 
        the endpoint

        Params:
            arc:             the node of the arc from myDict
            noTranspose:    If true the coordinates will not be transposed
            noInvert:       If True the coordinates will not be inverted
        
        Returns:
            cX: the center x coord
            cY: the center y coord
            x1: the endpt x coord
            y1: the endpt y coord
            curve: the angle to sweep in 1/10 degrees
            radius: the radius of the arc
        """
        
        curve=float(arc.get('curve'))
        x1,y1= float(arc.get('x1')) , float(arc.get('y1'))
        x2,y2= float(arc.get('x2')) , float(arc.get('y2'))
            
        #length of the chord
        l=sqrt((x1-x2)**2+(y1-y2)**2)
        
        #radius = l / ( 2*sin(curve/2) )
        r = fabs(l/( 2*sin(radians(curve/2.0))))
        
        #find midpoint of chord and then move perpendicularly to the center
           
        #go to midpoint of chord
        mX=(x2+x1)/2
        mY=(y2+y1)/2
        
        #distance between center and chord
        h=sqrt(r**2-(l/2)**2)  
          
        #slope of chord
        dY=(y2-y1)
        dX=(x2-x1)
        
        chordangle=atan2(dY,dX)
        
        #normalise the chord angle
        if chordangle<0:
            chordangle+=radians(360)
        
        #the angle perpendicular to the chord  
        angleSign=curve/fabs(curve)    
        angle=chordangle-radians(90)*angleSign 
          
        xChange=h*cos(angle)
        yChange=h*sin(angle)
        
        if fabs(curve)<180:
            xChange=-xChange
            yChange=-yChange
               
        cX=mX+xChange
        cY=mY+yChange

        if curve<0:
            sAngle=degrees(atan2(y2-cY,x2-cX))
        else:
            sAngle=degrees(atan2(y1-cY,x1-cX))
        eAngle=sAngle+curve
        
        cX,cY  = converter.convertCoordinate(cX,cY,noTranspose,noInvert)
        curve  = int(curve*10)
        r      = converter.convertUnit(r)
        sAngle = int(sAngle*10)%3600
        eAngle = int(eAngle*10)
        
        return cX,cY,curve,r,sAngle,eAngle<commit_msg>Fixed More Issues with Arc conversion in library symbols<commit_after>'''
Created on Apr 3, 2012

@author: Dan
'''
from math import sin,sqrt,atan2,copysign,radians,fabs,degrees
from LayerIds import getLayerId, makeViaMask

class Line(object):
    __slots__=('x1','y1','x2','y2',"cX","cY",'width','layer','curve','radius','sAngle','eAngle')
    
    def __init__(self,node,converter,noTranspose=False, offset=None):
        self.getWireInfo(node,converter,noTranspose, offset)
        
    def getWireInfo(self,wire,converter,noTranspose, offset):
        """
        Converts a wire dictionary node from Eagle to a usable form for kicad
        It then returns the dictionary of the wires info
        
        Params:
            wire:    the wire's node from myDict
            noTranspose: if true, coordinates will not be transposed
                            to screens center
        Returns:
            dict:    the wires info in a kicad usable dictionary form
                    Keys = x1,x2,y1,y2,layer,curve,width
            
        """
            
        x1,y1=converter.convertCoordinate(wire.get('x1'),wire.get('y1'),noTranspose)
        x2,y2=converter.convertCoordinate(wire.get('x2'),wire.get('y2'),noTranspose)
        curve=wire.get('curve')
        
        width=converter.convertUnit(wire.get('width'))
        layer=getLayerId(wire.get('layer'))
        
        
        if curve==None:
            cX=(x1+x2)//2
            cY=(y1+y2)//2
            radius = 0
            sAngle = 0
            eAngle = 0
        else:
            cX,cY,curve,radius,sAngle,eAngle=self.getWireArcInfo(wire,converter,noTranspose)
        
        if offset != None:
            dX, dY = converter.convertCoordinate(offset[0], offset[1], noTranspose)
            x1 += dX
            y1 += dY
            x2 += dX
            y2 += dY
            cX += dX
            cY += dY
            
        self.x1=        str(x1)
        self.y1=        str(y1)
        self.x2=        str(x2)
        self.y2=        str(y2)
        self.cX=        str(cX)
        self.cY=        str(cY)
        self.width=     str(width)
        self.layer=     str(layer)
        self.curve=     str(curve)
        self.radius=    str(radius)
        self.sAngle=    str(sAngle)
        self.eAngle=    str(eAngle)

    def getWireArcInfo(self,arc,converter, noTranspose=False,noInvert=False):
        """
        Converts between Eagle's arc definitions, and kicads arc definitions.
        
        \note Eagle holds the two endpoints and the degrees of the sweep ccw 
              betweeen them.                
        \note PCBNew holds the center point, and an enpoint, and a degree 
              sweep CW from the endpoint
        \note EESchema Holds Endpoints, Center Point, Radius, Start Angle,
              EndAngle

        \param arc          The node of the arc from myDict
        \param converter    The converter to use for unit conversions
        \param noTranspose  If true the coordinates will not be transposed
        \param noInvert     If True the coordinates will not be inverted
        
        
        \return cX     the center x coord
        \return cY     the center y coord
        \return x1     the endpt x coord
        \return y1     the endpt y coord
        \return curve  the angle to sweep in 1/10 degrees
        \return radius the radius of the arc
        \return sAngle the start Angle   
        \return eAngle the end Angle
        
        \bug sAngle, and eAngle aren't always correct
        """
        
        curve=float(arc.get('curve'))
        x1,y1= float(arc.get('x1')) , float(arc.get('y1'))
        x2,y2= float(arc.get('x2')) , float(arc.get('y2'))
              
        #length of the chord
        l=sqrt((x1-x2)**2+(y1-y2)**2)
        #radius = l / ( 2*sin(curve/2) )
        r = l/(2*sin(radians(fabs(curve)/2)))
        #midpoint of chord
        mX, mY = (x2+x1)/2 , (y2+y1)/2
        #distance between center and chord
        h=sqrt(r**2-(l/2)**2)
        
        #calculate center point by moving perpendicular from chord by height
        #make sure we move toward center not away
        sign = copysign(1,curve)*(-1 if fabs(curve)>180 else 1)
        cX = mX + h*(y1-y2)/l*sign #mx +- h*cos(theta)        
        cY = mY + h*(x2-x1)/l*sign #my +- h*sin(theta)
        
        sAngle = round(degrees(atan2(y1-cY,x1-cX)))
        eAngle = round(sAngle+curve+copysign(180,curve) if fabs(curve)>180 else 0)
        
        cX,cY  = converter.convertCoordinate(cX,cY,noTranspose,noInvert)
        curve  = int(curve*10)
        r      = converter.convertUnit(r)
        sAngle*=10
        eAngle*=10
        return cX,cY,curve,r,sAngle,eAngle
{'fraction_non_alphanumeric': 0.10508510413362934, 'fraction_numerical': 0.022412517179405858, 'mean_word_length': 1.7238698531528938, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2173855', 'n_tokens_mistral': 3137, 'n_tokens_neox': 2979, 'n_words': 715}