source: MML/trunk/mml/at/drawlattice.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 11 years ago

Initial import--MML version from SOLEIL@2013

File size: 8.9 KB
Line 
1function drawlattice(Offset, Scaling, hAxes)
2%DRAWLATTICE - Draws the AT lattice to a figure
3%  drawlattice(Offset {0}, Scaling {1}, hAxes {gca})
4%
5%  Programmers Note: The AT index is stored in the Userdata of each symbol.
6%
7%  Written by Greg Portmann
8
9
10global THERING
11
12
13% Minimum icon width in meters
14MinIconWidth = .09;
15
16if nargin < 1
17    Offset = 0;
18end
19Offset = Offset(1);
20if nargin < 2
21    Scaling = 1;
22end
23Scaling = Scaling(1);
24
25if nargin < 3
26    hAxes = gca;
27end
28
29SPositions = findspos(THERING, 1:length(THERING)+1);
30L = SPositions(end);
31plot(hAxes, [0 L], [0 0]+Offset, 'k');
32
33% Remember the hold state then turn hold on
34HoldState = ishold(hAxes);
35hold(hAxes, 'on');
36
37
38ATIndexHCM = family2atindex(gethcmfamily);
39ATIndexVCM = family2atindex(getvcmfamily);
40
41
42% Make default icons for elements of different physical types
43for i = 1:length(THERING)
44    NumberOfFinds = 0;
45   
46    SPos = SPositions(i);
47    if isfield(THERING{i},'BendingAngle') && THERING{i}.BendingAngle
48        % make icons for bending magnets
49        NumberOfFinds = NumberOfFinds + 1;
50        IconHeight = .3;
51        IconColor = [1 1 0];
52        IconWidth = THERING{i}.Length;
53        if IconWidth < MinIconWidth % meters
54            IconWidth = MinIconWidth;
55            SPos = SPos - IconWidth/2 + THERING{i}.Length/2;
56        end
57        vx = [SPos SPos+IconWidth SPos+IconWidth SPos];
58        vy = [IconHeight IconHeight -IconHeight -IconHeight];
59        h = patch(vx, Scaling*vy+Offset, IconColor,'LineStyle','-', 'Parent',hAxes);
60        set(h, 'UserData', i);
61
62        %if IconWidth < .1 % meters
63        %    set(h, 'EdgeColor', IconColor);
64        %end
65
66    elseif isfield(THERING{i},'K') && THERING{i}.K
67        % Quadrupole
68        NumberOfFinds = NumberOfFinds + 1;
69       if THERING{i}.K >= 0
70            % Focusing quadrupole           
71            IconHeight = .8;
72            IconColor = [1 0 0];
73            IconWidth = THERING{i}.Length;
74            if IconWidth < MinIconWidth % meters
75                IconWidth = MinIconWidth;
76                SPos = SPos - IconWidth/2 + THERING{i}.Length/2;
77            end
78            vx = [SPos SPos+IconWidth/2  SPos+IconWidth SPos+IconWidth/2 SPos];
79            vy = [0          IconHeight               0      -IconHeight    0];
80        else
81            % Defocusing quadrupole
82            IconHeight = .7;
83            IconColor = [0 0 1];
84            IconWidth = THERING{i}.Length;
85            if IconWidth < MinIconWidth % meters
86                % Center about starting point
87                IconWidth = MinIconWidth;
88                SPos = SPos - IconWidth/2 + THERING{i}.Length/2;
89            end
90            vx = [SPos+.4*IconWidth    SPos    SPos+IconWidth  SPos+.6*IconWidth  SPos+IconWidth    SPos      SPos+.4*IconWidth];
91            vy = [     0            IconHeight   IconHeight          0              -IconHeight  -IconHeight    0];
92        end
93        h = patch(vx, Scaling*vy+Offset, IconColor,'LineStyle','-', 'Parent',hAxes);
94        set(h, 'UserData', i);
95        %if IconWidth < .1 % meters
96        %    set(h, 'EdgeColor', IconColor);
97        %end
98
99    elseif isfield(THERING{i},'PolynomB') && length(THERING{i}.PolynomB)>2 && (THERING{i}.PolynomB(3) || any(strcmpi(THERING{i}.FamName,{'SF','SFF','SD','SDD'})))
100        % Sextupole
101        NumberOfFinds = NumberOfFinds + 1;
102        if THERING{i}.PolynomB(3)>0 || any(strcmpi(THERING{i}.FamName,{'SF','SFF'}))
103            % Focusing sextupole
104            IconHeight = .6;
105            IconColor = [1 0 1];
106            IconWidth = THERING{i}.Length;
107            if IconWidth < MinIconWidth % meters
108                IconWidth = MinIconWidth;
109                SPos = SPos - IconWidth/2 + THERING{i}.Length/2;
110            end
111            vx = [SPos          SPos+.33*IconWidth  SPos+.66*IconWidth  SPos+IconWidth   SPos+IconWidth   SPos+.66*IconWidth  SPos+.33*IconWidth      SPos          SPos];
112            vy = [IconHeight/3      IconHeight          IconHeight        IconHeight/3    -IconHeight/3      -IconHeight          -IconHeight     -IconHeight/3  IconHeight/3];
113        else
114            % Defocusing sextupole
115            IconHeight = .6;
116            IconColor = [0 1 0];
117            IconWidth = THERING{i}.Length;
118            if IconWidth < MinIconWidth % meters
119                IconWidth = MinIconWidth;
120                SPos = SPos - IconWidth/2 + THERING{i}.Length/2;
121            end
122            vx = [SPos          SPos+.33*IconWidth  SPos+.66*IconWidth  SPos+IconWidth   SPos+IconWidth   SPos+.66*IconWidth  SPos+.33*IconWidth      SPos          SPos];
123            vy = [IconHeight/3      IconHeight          IconHeight        IconHeight/3    -IconHeight/3      -IconHeight          -IconHeight     -IconHeight/3  IconHeight/3];
124        end
125        h = patch(vx, Scaling*vy+Offset, IconColor,'LineStyle','-', 'Parent',hAxes);
126        set(h, 'UserData', i);
127        %if IconWidth < .1 % meters
128        %    set(h, 'EdgeColor', IconColor);
129        %end
130
131    elseif isfield(THERING{i},'Frequency') && isfield(THERING{i},'Voltage')
132        % RF cavity
133        NumberOfFinds = NumberOfFinds + 1;
134        IconColor = [1 0.5 0];
135        h = plot(hAxes, SPos, 0+Offset, 'o', 'MarkerFaceColor', IconColor, 'Color', IconColor, 'MarkerSize', 4);
136        set(h, 'UserData', i);
137
138    elseif strcmpi(THERING{i}.FamName,'BPM')
139        % BPM
140        NumberOfFinds = NumberOfFinds + 1;
141        IconColor = 'k';
142        h = plot(hAxes, SPos, 0+Offset, '.', 'Color', IconColor);
143        %h = plot(hAxes, SPos, 0, 'o', 'MarkerFaceColor', IconColor, 'Color', IconColor, 'MarkerSize', 1.5)
144        set(h, 'UserData', i);
145       
146    elseif strcmpi(THERING{i}.FamName,'TV')
147        % TV screen
148        NumberOfFinds = NumberOfFinds + 1;
149        IconHeight = .7;
150        IconColor = [.5 0 0];  %'k';
151        %h = plot(hAxes, SPos, 0+Offset, 'x', 'Color', IconColor);
152        h = plot(hAxes, SPos, Scaling*IconHeight+Offset, 'Marker','Square', 'MarkerFaceColor', IconColor, 'Color', IconColor, 'MarkerSize', 3.5);
153        set(h, 'UserData', i);
154    end
155   
156    % Since correctors could be a combined function magnet, test separately
157    if any(strcmpi(THERING{i}.FamName,{'COR','XCOR','YCOR','HCOR','VCOR'})) || isfield(THERING{i},'KickAngle')
158        % Corrector
159        NumberOfFinds = NumberOfFinds + 1;
160       
161        if NumberOfFinds > 1
162            IconWidth = 0;
163        else
164            IconWidth = THERING{i}.Length;
165        end
166        IconHeight = 1.0;  % was .8
167        vx = [SPos   SPos];
168
169        % Draw a line above for a HCM and below for a VCM
170        % If it's not in the ML, then draw a line above and below
171        CMFound = 1;
172        if any(i == ATIndexVCM)
173            IconColor = [0 0 0];
174            vy = [-IconHeight 0];
175            if IconWidth < MinIconWidth
176                h = plot(hAxes, vx, Scaling*vy+Offset, 'Color', IconColor);
177            else
178                IconWidth = THERING{i}.Length;
179                vx = [SPos SPos+IconWidth SPos+IconWidth SPos];
180                vy = [0 0 -IconHeight -IconHeight];
181                %vy = [IconHeight IconHeight -IconHeight -IconHeight];
182                h = patch(vx, Scaling*vy+Offset, IconColor, 'LineStyle', '-', 'Parent',hAxes);
183                if IconWidth < MinIconWidth*2 % meters
184                    set(h, 'EdgeColor', IconColor);
185                end
186            end
187            set(h, 'UserData', i);
188            CMFound = 0;
189        end
190
191        if any(i == ATIndexHCM)
192            IconColor = [0 0 0];
193            vy = [0 IconHeight];
194            if IconWidth < MinIconWidth
195                h = plot(hAxes, vx, Scaling*vy+Offset, 'Color', IconColor);
196            else
197                IconWidth = THERING{i}.Length;
198                vx = [SPos SPos+IconWidth SPos+IconWidth SPos];
199                vy = [IconHeight IconHeight 0 0];
200                %vy = [IconHeight IconHeight -IconHeight -IconHeight];
201                h = patch(vx, Scaling*vy+Offset, IconColor, 'LineStyle', '-', 'Parent',hAxes);
202                if IconWidth < MinIconWidth*2 % meters
203                    set(h, 'EdgeColor', IconColor);
204                end
205            end
206            set(h, 'UserData', i);
207            CMFound = 0;
208        end
209       
210        if CMFound
211            IconColor = [0 0 0];
212            vy = [-IconHeight IconHeight];
213            if IconWidth < MinIconWidth
214                h = plot(hAxes, vx, Scaling*vy+Offset, 'Color', IconColor);
215            else
216                IconWidth = THERING{i}.Length;
217                vx = [SPos SPos+IconWidth SPos+IconWidth SPos];
218                vy = [IconHeight IconHeight -IconHeight -IconHeight];
219                h = patch(vx, Scaling*vy+Offset, IconColor, 'LineStyle', '-', 'Parent',hAxes);
220                if IconWidth < MinIconWidth*2 % meters
221                    set(h, 'EdgeColor', IconColor);
222                end
223            end
224            set(h, 'UserData', i);
225            CMFound = 0;
226        end
227
228    end
229end
230
231
232% Leave the hold state as it was at the start
233if ~HoldState
234    hold(hAxes, 'off');
235end
236
237a = axis(hAxes);
238axis(hAxes, [0 L a(3:4)]);
239
Note: See TracBrowser for help on using the repository browser.