# Path: drawingDimensioning/linearDimension.py
# def linearDimensionSVG_points( x1, y1, x2, y2, x3, y3, x4=None, y4=None, autoPlaceText=False, autoPlaceOffset=2.0,
#                                scale=1.0, textFormat_linear='%(value)3.3f', comma_decimal_place=False,
#                                gap_datum_points = 2, dimension_line_overshoot=1, arrowL1=3, arrowL2=1, arrowW=2, strokeWidth=0.5, lineColor='blue', arrow_scheme='auto',
#                                halfDimension_linear=False, textRenderer= defaultTextRenderer):
#     lines = []
#     p1 = numpy.array([ x1, y1 ])
#     p2 = numpy.array([ x2, y2 ])
#     p3 = numpy.array([ x3, y3 ])
#     if min(x1,x2) <= x3 and x3 <= max(x1,x2):
#         d = numpy.array([ 0, 1])
#         textRotation = 0
#     elif  min(y1,y2) <= y3 and y3 <= max(y1,y2):
#         d = numpy.array([ 1, 0])
#         textRotation = -90
#     elif numpy.linalg.norm(p2 - p1) > 0:
#         d = numpy.dot( [[ 0, -1],[ 1, 0]], directionVector(p1,p2))
#         if abs( sum( numpy.sign(d) * numpy.sign( p3- (p1+p2)/2 ))) == 0:
#             return None
#         textRotation = numpy.arctan( (y2 - y1)/(x2 - x1))  / numpy.pi * 180
#         #if textRotation <
#     else:
#         return None
#     A = p1 + numpy.dot(p3-p1,d)*d
#     B = p2 + numpy.dot(p3-p2,d)*d
#     if not halfDimension_linear:
#         lines.append( dimensionSVG_trimLine( p1, A, gap_datum_points,-dimension_line_overshoot))
#         lines.append( dimensionSVG_trimLine( p2, B, gap_datum_points,-dimension_line_overshoot))
#         lines.append( A.tolist() +  B.tolist() )
#     else:
#         lines.append( dimensionSVG_trimLine( p2, B, gap_datum_points,-dimension_line_overshoot)) #linePerpendic2
#         lines.append( p3.tolist() +  B.tolist() ) #line from pointer to B
#     lineXML = '\n'.join( svgLine( x1, y1, x2, y2, lineColor, strokeWidth ) for  x1, y1, x2, y2 in lines )
#     v = numpy.linalg.norm(A-B)*scale
#     text = dimensionText( v, textFormat_linear, comma=comma_decimal_place)
#     textXML = textPlacement_common_procedure(A, B, text, x4, y4, textRotation, textRenderer, autoPlaceText, autoPlaceOffset)
#     distAB = numpy.linalg.norm(A-B)
#     if distAB > 0 and arrow_scheme != 'off': #then draw arrows
#         if arrow_scheme == 'auto':
#             s = 1 if distAB > 2.5*(arrowL1 + arrowL2) else -1
#         elif arrow_scheme == 'in':
#             s = 1
#         elif arrow_scheme == 'out':
#             s = -1
#         arrowXML = arrowHeadSVG( B, s*directionVector(B,A), arrowL1, arrowL2, arrowW, lineColor )
#         if not halfDimension_linear:
#             arrowXML = arrowXML +  arrowHeadSVG( A, s*directionVector(A,B), arrowL1, arrowL2, arrowW, lineColor )
#     else:
#         arrowXML = ''
#     return '<g> \n  %s \n  %s \n  %s </g>' % ( lineXML, arrowXML, textXML )


import unittest
import xml.etree.ElementTree as XML_Tree
    import xml.dom.minidom as minidom
import numpy
from drawingDimensioning.linearDimension import linearDimensionSVG_points
if __name__ == '__main__':
    print('''run tests via
  FreeCAD_drawing_dimensioning$ python2 test''')
    exit()


def xml_prettify( xml_str ):
    xml = minidom.parseString( xml_str )
    S = xml.toprettyxml(indent='  ')
    return '\n'.join( s for s in S.split('\n') if s.strip() != '' )

        
class Tests(unittest.TestCase):
    
    def test_dy( self ):
        svg_string =  linearDimensionSVG_points( x1 = 0, y1 = 10 , x2 = 30, y2= 25, x3 = 40, y3= 20, x4 = 50, y4 = 30 )
        svg = XML_Tree.fromstring( svg_string )
        #print( xml_prettify( svg_string ) )
        self.assertEqual( svg.find('text').text, '15'  )

    def test_dx( self ):

# Path: drawingDimensioning/linearDimension.py
# def linearDimensionSVG_points( x1, y1, x2, y2, x3, y3, x4=None, y4=None, autoPlaceText=False, autoPlaceOffset=2.0,
#                                scale=1.0, textFormat_linear='%(value)3.3f', comma_decimal_place=False,
#                                gap_datum_points = 2, dimension_line_overshoot=1, arrowL1=3, arrowL2=1, arrowW=2, strokeWidth=0.5, lineColor='blue', arrow_scheme='auto',
#                                halfDimension_linear=False, textRenderer= defaultTextRenderer):
#     lines = []
#     p1 = numpy.array([ x1, y1 ])
#     p2 = numpy.array([ x2, y2 ])
#     p3 = numpy.array([ x3, y3 ])
#     if min(x1,x2) <= x3 and x3 <= max(x1,x2):
#         d = numpy.array([ 0, 1])
#         textRotation = 0
#     elif  min(y1,y2) <= y3 and y3 <= max(y1,y2):
#         d = numpy.array([ 1, 0])
#         textRotation = -90
#     elif numpy.linalg.norm(p2 - p1) > 0:
#         d = numpy.dot( [[ 0, -1],[ 1, 0]], directionVector(p1,p2))
#         if abs( sum( numpy.sign(d) * numpy.sign( p3- (p1+p2)/2 ))) == 0:
#             return None
#         textRotation = numpy.arctan( (y2 - y1)/(x2 - x1))  / numpy.pi * 180
#         #if textRotation <
#     else:
#         return None
#     A = p1 + numpy.dot(p3-p1,d)*d
#     B = p2 + numpy.dot(p3-p2,d)*d
#     if not halfDimension_linear:
#         lines.append( dimensionSVG_trimLine( p1, A, gap_datum_points,-dimension_line_overshoot))
#         lines.append( dimensionSVG_trimLine( p2, B, gap_datum_points,-dimension_line_overshoot))
#         lines.append( A.tolist() +  B.tolist() )
#     else:
#         lines.append( dimensionSVG_trimLine( p2, B, gap_datum_points,-dimension_line_overshoot)) #linePerpendic2
#         lines.append( p3.tolist() +  B.tolist() ) #line from pointer to B
#     lineXML = '\n'.join( svgLine( x1, y1, x2, y2, lineColor, strokeWidth ) for  x1, y1, x2, y2 in lines )
#     v = numpy.linalg.norm(A-B)*scale
#     text = dimensionText( v, textFormat_linear, comma=comma_decimal_place)
#     textXML = textPlacement_common_procedure(A, B, text, x4, y4, textRotation, textRenderer, autoPlaceText, autoPlaceOffset)
#     distAB = numpy.linalg.norm(A-B)
#     if distAB > 0 and arrow_scheme != 'off': #then draw arrows
#         if arrow_scheme == 'auto':
#             s = 1 if distAB > 2.5*(arrowL1 + arrowL2) else -1
#         elif arrow_scheme == 'in':
#             s = 1
#         elif arrow_scheme == 'out':
#             s = -1
#         arrowXML = arrowHeadSVG( B, s*directionVector(B,A), arrowL1, arrowL2, arrowW, lineColor )
#         if not halfDimension_linear:
#             arrowXML = arrowXML +  arrowHeadSVG( A, s*directionVector(A,B), arrowL1, arrowL2, arrowW, lineColor )
#     else:
#         arrowXML = ''
#     return '<g> \n  %s \n  %s \n  %s </g>' % ( lineXML, arrowXML, textXML )


# Path: test/test_linear_dimension.py
import unittest
import xml.etree.ElementTree as XML_Tree
    import xml.dom.minidom as minidom
import numpy
from drawingDimensioning.linearDimension import linearDimensionSVG_points

if __name__ == '__main__':
    print('''run tests via
  FreeCAD_drawing_dimensioning$ python2 test''')
    exit()


def xml_prettify( xml_str ):
    xml = minidom.parseString( xml_str )
    S = xml.toprettyxml(indent='  ')
    return '\n'.join( s for s in S.split('\n') if s.strip() != '' )

        
class Tests(unittest.TestCase):
    
    def test_dy( self ):
        svg_string =  linearDimensionSVG_points( x1 = 0, y1 = 10 , x2 = 30, y2= 25, x3 = 40, y3= 20, x4 = 50, y4 = 30 )
        svg = XML_Tree.fromstring( svg_string )
        #print( xml_prettify( svg_string ) )
        self.assertEqual( svg.find('text').text, '15'  )

    def test_dx( self ):

        svg_string =  linearDimensionSVG_points( x1 = 0, y1 = 10 , x2 = 30, y2= 25, x3 = 15, y3= 40, x4 = 50, y4 = 30 )