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

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


indata="CROSSGEOM pvcprofile2  general \
0	0	S	0.00225	180	0	8	1 \
0	0	S	0.006	270	0	21	1 \
0	0	CI	280	310	0.015	28	1 \
0	0	CI	310	340	0.015	28	1 \
0	0	CI	340	360	0.035	43	1 \
0	0	CO	90	70	0.08	99	1 \
0	0	S	0.003	240	0	11	1 \
0	0	CI	57	95	0.0615	142	1 \
0	0	CO	30	0	0.017	31	1 \
0	0	CO	340	321	0.05	59	1 \
0	0	CO	219	200	0.05	59	1 \
0	0	CO	180	150	0.017	31	1 \
0	0	CI	85	123	0.0615	142	1 \
0	0	S	0.003	120	0	11	1 \
0	0	CO	110	90	0.08	99	1 \
0	0	CI	180	200	0.035	43	1 \
0	0	CI	200	230	0.015	28	1 \
0	0	CI	230	260	0.015	28	1 \
0	0	S	0.006	90	0	21	1 \
0	0	S	0.002234215	180	0	8	1"



# ##--------------------------------------------------------------"


indata2=indata.split()
nseg=int((len(indata2)-3)/8)
print(nseg)

print(indata2)
plt.figure(1)   
x=[0.0]
y=[0.0]
for seg in range(nseg):
    ip0=3+seg*8
    ccode=indata2[ip0+2]
    if ccode=='S':
        leng=float(indata2[ip0+3])
        ang=float(indata2[ip0+4])
        n=int(indata2[ip0+6])
        print("S ",leng, ang, n)
        dx=leng/n*np.cos(ang/180.0*np.pi)
        dy=leng/n*np.sin(ang/180.0*np.pi)
        dx=-leng/n*np.cos(ang/180.0*np.pi)
        dy=-leng/n*np.sin(ang/180.0*np.pi)
        xloc=[x[-1]]
        yloc=[y[-1]]
        for nn in range(n):
            newx=x[-1]+dx
            newy=y[-1]+dy
            x.append(newx) 
            y.append(newy)
            xloc.append(newx)
            yloc.append(newy)
        plt.plot(xloc,yloc,'x-',label=seg+1)
    if (ccode =='CI' or ccode =='CO'):
        ang1=float(indata2[ip0+3])
        ang2=float(indata2[ip0+4])
        if (ccode =='CI' and ang2 < ang1): ang2=ang2+360
        if (ccode =='CO' and ang2 > ang1): ang2=ang2-360
        rad=float(indata2[ip0+5])
        n=int(indata2[ip0+6])
        print(rad, ang1, ang2,n)
        dang=(ang2-ang1)/n
        x0=rad*np.cos(ang1/180*np.pi)
        y0=rad*np.sin(ang1/180*np.pi)
        x01=x[-1]
        y01=y[-1]
        xloc=[x[-1]]
        yloc=[y[-1]]
        
        for nn in range(n):
            ang=ang1+dang*(nn+1)
            newx=rad*np.cos(ang/180*np.pi)-x0+x01
            newy=rad*np.sin(ang/180*np.pi)-y0+y01
            x.append(newx) 
            y.append(newy)
            xloc.append(newx)
            yloc.append(newy)
        plt.plot(xloc,yloc,'x-',label=seg+1)

    

    
    plt.legend(loc='upper center', bbox_to_anchor=(1.1, 1.1))
    plt.axis('equal')
