# -*- coding: utf-8 -*-
"""
Created on Thu May 16 22:52:37 2024

@author: janneg
"""
import numpy as np
import matplotlib.pyplot as plt

# path to a *.bdf file
bdffile='D:\\LocalProjects\\BFLEX_supports\\2024-05-24\\ZETA-ZETAPRARMOR.bdf'
plt.figure(1)

# a target dl for resulting definitin
dltarget=0.3

print('Seg. no --- N now  ---  dl now --- N target')
for line in open(bdffile):
    if (line[0:4]!='G2R:' and line[0:4]!='tria' and line[0:4]!='mesh'):    

        listofargs=line.split()
        tmp=listofargs[0]
        tmp=tmp.replace("bound","",1)
        tmp=tmp.replace("(","",1)
        tmp=tmp.replace(")","",1)
        tmp2=tmp.split(",")        

        boundary_no=tmp2[0]
        start_t=float(tmp2[1])
        stop_t=float(tmp2[2])
        n=int(tmp2[3])

        t=np.linspace(start_t,stop_t,n)
        G2R=np.pi/180
        
        tmp=listofargs[2]
        tmp=tmp.replace(":","",1)
        tmp=tmp.replace("x","",1)
        tmp=tmp.replace("=","",1)
        tmp=tmp.replace(";","",1)
        tmp=tmp.replace("cos","np.cos")
        tmp=tmp.replace("sin","np.sin")
        x=eval(tmp)


        tmp=listofargs[3]
        tmp=tmp.replace(":","",1)
        tmp=tmp.replace("y","",1)
        tmp=tmp.replace("=","",1)
        tmp=tmp.replace(";","",1)
        tmp=tmp.replace("cos","np.cos")
        tmp=tmp.replace("sin","np.sin")
        y=eval(tmp)
        dl=np.sqrt((x[1]-x[0])**2+(y[1]-y[0])**2)

# printing suggested number of points in each segment ot hit the target dl    
        print('%5s  %10d %12f %10d' %(boundary_no,n-1,dl,round((n-1)*dl/dltarget)))
        plt.plot(x,y,'x-',label=boundary_no)

        
    else:
         print(' ')
    plt.legend(loc='upper center', bbox_to_anchor=(1.1, 1.1))
    
