8 our $ptscale= 72/25.4 / $scale;
13 our $psu_ticksperu= 1;
14 our $psu_ticklen= 5.0;
15 our $psu_allwidth= 37.0/2;
17 our $psu_sleeperlen= 17;
18 our $psu_sleeperlw= 15;
24 our $lmu_txtboxtxty= $lmu_marktpt * 0.300;
25 our $lmu_txtboxh= $lmu_marktpt * 1.100;
26 our $lmu_txtboxpadx= $lmu_marktpt * 0.335;
27 our $lmu_txtboxoff= $lmu_marklw / 2;
30 our $olu_left= 10 * $scale;
31 our $olu_right= 217 * $scale - $olu_left;
32 our $olu_bottom= 20 * $scale;
33 our $olu_top= 270 * $scale - $olu_bottom;
36 our $olu_textheight= 15;
37 our $olu_textallowperc= $lmu_marktpt * 5.0/11;
45 # $ctx->{CmdLog}= undef } not in defobj
46 # $ctx->{CmdLog}[]= [ command args ] } in defobj
47 # $ctx->{LocsMade}[]= $id
51 # $ctx->{Trans}{X} # transformation. is ev representing
52 # $ctx->{Trans}{Y} # new origin. (is applied at _input_
53 # $ctx->{Trans}{A} # not at plot-time)
54 # $ctx->{Trans}{R} # but multiply all y coords by this!
55 # $ctx->{Draw} # sequence of one or more chrs from uc $drawers
59 # $objs{$id}{Part} # 1 iff object is a part
63 our @al; # current cmd
68 our $param; # for parametric_curve
73 # Operate on Enhanced Vectors which are a location (coordinates) and a
74 # direction at that location. Representation is a hash with members X
75 # Y and A (angle of the direction in radians, anticlockwise from
76 # East). May be absolute, or interpreted as relative, according to
79 # Each function's first argument is a hashref whose X Y A members will
80 # be created or overwritten; this hashref will be returned (so you can
81 # use it `functionally' by passing {}). The other arguments may be ev
82 # hashrefs, or other info. The results are in general undefined if
83 # one of the arguments is the same hash as the result.
86 # ev_byang(R, ANG,[LEN])
87 # result is evec of specified angle and length (default=1.0)
88 my ($res,$ang,$len)=@_;
89 $len=1.0 unless defined $len;
90 $res->{X}= $len * cos($ang);
91 $res->{Y}= $len * sin($ang);
95 sub ev_compose ($$$) {
96 # ev_compose(SUM_R, A,B);
97 # appends B to A, result is end of new B
98 # (B's X is forwards from end of A, Y is translating left from end of A)
99 # A may have a member R, which if provided then it should be 1.0 or -1.0,
100 # and B's Y and A will be multiplied by R first (ie, we can reflect);
101 my ($sum,$a,$b) = @_;
103 $r= defined $a->{R} ? $a->{R} : 1.0;
104 $sum->{X}= $a->{X} + $b->{X} * cos($a->{A}) - $r * $b->{Y} * sin($a->{A});
105 $sum->{Y}= $a->{Y} + $r * $b->{Y} * cos($a->{A}) + $b->{X} * sin($a->{A});
106 $sum->{A}= $a->{A} + $r * $b->{A};
109 sub ev_decompose ($$$) {
110 # ev_decompose(B_R, A,SUM)
111 # computes B_R s.t. ev_compose({}, A, B_R) gives SUM
114 $r= defined $a->{R} ? $a->{R} : 1.0;
115 $brx= $sum->{X} - $a->{X};
116 $bry= $r * ($sum->{Y} - $a->{Y});
117 $b->{X}= $brx * cos($a->{A}) + $bry * sin($a->{A});
118 $b->{Y}= $bry * cos($a->{A}) - $brx * sin($a->{A});
119 $b->{A}= $r * ($sum->{A} - $a->{A});
122 sub ev_lincomb ($$$$) {
123 # ev_linkcomb(RES,A,B,P)
124 # gives P*A + (1-P)*B
125 my ($r,$a,$b,$p) = @_;
127 map { $r->{$_} = $q * $a->{$_} + $p * $b->{$_} } qw(X Y A);
130 sub ev_bearing ($$) {
132 # returns bearing of B from A
133 # value returned is in [ A->{A}, A->{A} + 2*$pi >
134 # A->{A} and B->{A} are otherwise ignored
137 $r= atan2($b->{Y} - $a->{Y},
140 while ($r < $a->{A}) { $r += 2.0 * $pi; }
145 # returns distance from A to B
146 # A->{A} and B->{A} are ignored
149 $xd= $b->{X} - $a->{X};
150 $yd= $b->{Y} - $a->{Y};
151 return sqrt($xd*$xd + $yd*$yd);
156 $$limr= $now unless defined $$limr && $$limr <= $now;
160 $$limr= $now unless defined $$limr && $$limr >= $now;
164 my ($converter,$defaulter)=@_;
166 return &$defaulter unless @al;
168 $v= &$converter($spec);
169 dv('canf ','$spec',$spec, '$v',$v);
172 sub can ($) { my ($c)=@_; canf($c, sub { die "too few args"; }); }
173 sub cano ($$) { my ($c,$def)=@_; canf($c, sub { return $def }); }
175 sub signum ($) { return ($_[0] > 0) - ($_[0] < 0); }
179 my ($min_x, $max_x, $min_y, $max_y);
181 foreach $loc (values %$objhash) {
182 upd_min(\$min_x, $loc->{X} - abs($psu_allwidth * sin($loc->{A})));
183 upd_max(\$max_x, $loc->{X} + abs($psu_allwidth * sin($loc->{A})));
184 upd_min(\$min_y, $loc->{Y} - abs($psu_allwidth * cos($loc->{A})));
185 upd_max(\$max_y, $loc->{Y} + abs($psu_allwidth * cos($loc->{A})));
187 return ($min_x, $max_x, $min_y, $max_y);
190 our %units_len= qw(- mm mm 1 cm 10 m 1000);
191 our %units_ang= qw(- d r 1); $units_ang{'d'}= 2*$pi / 360;
193 sub cva_len ($) { my ($sp)=@_; cva_units($sp,\%units_len); }
194 sub cva_ang ($) { my ($sp)=@_; cva_units($sp,\%units_ang); }
195 sub cva_absang ($) { input_absang(cva_ang($_[0])) }
199 $sp =~ m/^([-0-9eE.]*[0-9.])([A-Za-z]*)$/
200 or die "lexically invalid quantity";
202 $u=$ua->{'-'} unless length $u;
203 defined $ua->{$u} or die "unknown unit $u";
205 print DEBUG "cva_units($sp,)=$r ($n $u $ua->{$u})\n";
210 die "invalid id" unless $sp =~ m/^[a-z][_0-9A-Za-z]*$/;
215 my ($r,$d,$k,$neg,$na);
216 $neg= $sp =~ s/^\-//;
218 die "unknown $id" unless defined $ctx->{Loc}{$id};
219 $r= $ctx->{Loc}{$id};
221 foreach $k (sort keys %$r) { $d .= " $k=$r->{$k}"; }
222 printf DEBUG "%s\n", $d;
225 $na -= 2*$pi if $na >= 2*$pi;
226 $r= { X => $r->{X}, Y => $r->{Y}, A => $na };
234 die "duplicate $id" if exists $ctx->{Loc}{$id};
235 exists $ctx->{Loc}{$id}{X};
236 push @{ $ctx->{LocsMade} }, $id;
237 return $ctx->{Loc}{$id};
239 sub cva_cmd ($) { return cva_idstr($_[0]); }
242 return $sp if grep { $_ eq $sp } @$el;
243 die "invalid option (permitted: @$el)";
245 sub cvam_enum { my (@e) = @_; return sub { cva__enum($_[0],\@e); }; }
249 $nl= can(\&cva_idnew);
250 $i->{X}= can(\&cva_len);
251 $i->{Y}= can(\&cva_len);
252 $i->{A}= can(\&cva_ang);
253 ev_compose($nl, $ctx->{Trans}, $i);
256 my ($from,$to,$len,$right,$turn);
257 $from= can(\&cva_idex);
258 $to= can(\&cva_idnew);
259 $len= cano(\&cva_len,0);
260 $right= cano(\&cva_len,0) * $ctx->{Trans}{R};
261 $turn= cano(\&cva_ang, 0) * $ctx->{Trans}{R};
262 my ($u)= ev_compose({}, $from, { X => $len, Y => -$right, A => 0 });
263 ev_compose($to, $u, { X => 0, Y => 0, A => $turn });
268 $pfx . ($pfx =~ m/\}$|\]$/ ? '' : '->');
272 return 'undef' if !defined $v;
273 return $v if $v !~ m/\W/ && $v =~ m/[A-Z]/ && $v =~ m/^[a-z_]/i;
274 return $v if $v =~ m/^[0-9.]+/;
275 $v =~ s/[\\\']/\\$&/g;
279 sub dv1_kind ($$$$$$$) {
280 my ($pfx,$expr,$ref,$ref_exp,$ixfmt,$ixesfn,$ixmapfn) = @_;
282 return 0 if $ref ne $ref_exp;
284 foreach $ix (&$ixesfn) {
286 my ($v)= &$ixmapfn($ix);
287 #print STDERR "dv1_kind($pfx,$expr,$ref,$ref_exp,$ixmapfn) ix=$ix v=$v\n";
288 dv1($pfx,$expr.sprintf($ixfmt,dv__evr($ix)),$v);
291 printf DEBUG "%s%s= $ixfmt\n", $pfx, $expr, ' ';
296 return 0 unless $debug;
297 my ($pfx,$expr,$v) = @_;
300 #print STDERR "dv1 >$pfx|$ref<\n";
302 printf DEBUG "%s%s= %s\n", $pfx,$expr, dv__evr($v);
304 } elsif ($ref eq 'SCALAR') {
305 dv1($pfx, ($expr =~ m/^\$/ ? "\$$expr" : '${'.$expr.'}'), $$v);
308 $expr.='->' unless $expr =~ m/\]$|\}$/;
309 return if dv1_kind($pfx,$expr,$ref,'ARRAY','[%s]',
310 sub { ($[ .. $#$v) },
311 sub { $v->[$_[0]] });
312 return if dv1_kind($pfx,$expr,$ref,'HASH','{%s}',
313 sub { sort keys %$v },
314 sub { $v->{$_[0]} });
315 printf DEBUG "%s%s is %s\n", $pfx, $expr, $ref;
327 sub o ($) { $o .= $_[0]; }
328 sub ol ($) { $ol .= $_[0]; }
330 print $o, $ol, " showpage\n"
337 sub o_path_begin () {
339 $o_path_verb= 'moveto';
341 sub o_path_point ($) {
343 o(" $pt $o_path_verb\n");
344 $o_path_verb= 'lineto';
346 sub o_path_stroke ($) {
348 o(" $width setlinewidth stroke\n");
352 my ($a,$b,$width)=@_;
356 o_path_stroke($width);
359 sub psu_coords ($$$) {
360 my ($ends,$inunit,$across)=@_;
361 # $ends->[0]{X} etc.; $inunit 0 to 1 (but go to 1.5);
362 # $across in mm, +ve to right.
363 my (%ea_zo, $zo, $prop);
364 $ea_zo{X}=$ea_zo{Y}=0;
365 foreach $zo (qw(0 1)) {
366 $prop= $zo ? $inunit : (1.0 - $inunit);
367 $ea_zo{X} += $prop * ($ends->[$zo]{X} - $across * sin($ends->[0]{A}));
368 $ea_zo{Y} += $prop * ($ends->[$zo]{Y} + $across * cos($ends->[0]{A}));
370 # dv("psu_coords ", '$ends',$ends, '$inunit',$inunit, '$across',$across,
371 # '\\%ea_zo', \%ea_zo);
372 return $ea_zo{X}." ".$ea_zo{Y};
375 sub parametric__o_pt ($) {
377 o_path_point("$pt->{X} $pt->{Y}");
380 sub parametric_segment ($$$$) {
381 my ($p0,$p1,$lenperp,$calcfn) = @_;
382 # makes $p (global) go from $p0 to $p1 ($p1>$p0)
383 # $lenperp is the length of one unit p, ie the curve
384 # must have a uniform `density' in parameter space
385 # $calcfn is invoked with $p set and should return a loc
386 # (ie, ref to X =>, Y =>, A =>).
387 my ($pa,$pb,@ends,$side,$ppu,$e,$v,$tick,$draw);
388 return unless $ctx->{Draw} =~ m/[ARSC]/;
389 $ppu= $psu_ulen/$lenperp;
390 my ($railctr)=($psu_gauge + $psu_raillw)*0.5;
391 my ($tickend)=($psu_allwidth - $psu_ticklen);
392 my ($tickpitch)=($psu_ulen / $psu_ticksperu);
393 my ($sleeperctr)=($psu_ulen*0.5);
394 my ($sleeperend)=($psu_sleeperlen*0.5);
395 print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
399 o(" $psu_thinlw setlinewidth\n");
401 for ($param=$p0; $param<$p1; $param += $ppu) {
402 parametric__o_pt(&$calcfn);
405 parametric__o_pt(&$calcfn);
408 return unless $draw =~ m/[ARS]/;
409 for ($pa= $p0; $pa<$p1; $pa=$pb) {
411 $param= $pa; $ends[0]= @ends ? $ends[1] : &$calcfn;
412 $param= $pb; $ends[1]= &$calcfn;
413 #print DEBUG "pa $pa $ends[0]{X} $ends[0]{Y} $ends[0]{A}\n";
414 #print DEBUG "pb $pb $ends[1]{X} $ends[1]{Y} $ends[1]{A}\n";
415 $e= $pb<=$p1 ? 1.0 : ($p1-$pa)/$ppu;
418 o_path_point(psu_coords(\@ends,0,-$psu_allwidth));
419 o_path_point(psu_coords(\@ends,0,$psu_allwidth));
420 o_path_point(psu_coords(\@ends,$e,$psu_allwidth));
421 o_path_point(psu_coords(\@ends,$e,-$psu_allwidth));
422 o(" closepath clip\n");
423 foreach $side qw(-1 1) {
425 o_line(psu_coords(\@ends,0,$side*$railctr),
426 psu_coords(\@ends,1.5,$side*$railctr),
430 o_line(psu_coords(\@ends,0,$side*$psu_allwidth),
431 psu_coords(\@ends,1.5,$side*$psu_allwidth),
433 for ($tick=0; $tick<1.5; $tick+=$tickpitch/$psu_ulen) {
434 o_line(psu_coords(\@ends,$tick,$side*$psu_allwidth),
435 psu_coords(\@ends,$tick,$side*$tickend),
441 o_line(psu_coords(\@ends,$sleeperctr,-$sleeperend),
442 psu_coords(\@ends,$sleeperctr,+$sleeperend),
450 my ($to, $ctr,$from, $radius,$delta) = @_;
451 # does parametric_segment to draw an arc centred on $ctr
452 # ($ctr->{A} ignored)
453 # from $from with radius $radius (this must be consistent!)
454 # and directionally-subtending an angle $delta.
455 # sets $to->... to be the other end, and returns $to
457 $to->{A}= $beta= $from->{A} + $delta;
458 $to->{X}= $ctr->{X} - $radius * sin($beta);
459 $to->{Y}= $ctr->{Y} + $radius * cos($beta);
460 return if abs($delta*$radius) < 1E-9;
461 parametric_segment(0.0,1.0, abs($radius*$delta), sub {
462 my ($beta) = $from->{A} + $delta * $param;
463 return { X => $ctr->{X} - $radius * sin($beta),
464 Y => $ctr->{Y} + $radius * cos($beta),
470 my ($from,$to,$how,$minradius);
471 $from= can(\&cva_idex);
472 $to= can(\&cva_idex);
473 $minradius= can(\&cva_len);
474 my (@paths,@solkinds);
476 my ($sigma,$distfact, $theta,$phi, $a,$b,$c,$d, $m,$r, $radius);
477 my ($cvec,$cfrom,$cto,$midpt, $delta1,$delta2, $path,$reverse);
478 $sigma= ev_bearing($from,$to);
479 $distfact= v_dist($from,$to);
480 $theta= 0.5 * $pi - ($from->{A} - $sigma);
481 $phi= 0.5 * $pi - ($to->{A} + $pi - $sigma);
482 $a= 2 * (1 + cos($theta - $phi));
483 $b= 2 * (cos($theta) - cos($phi));
485 $d= sqrt($b*$b - 4*$a*$c);
486 foreach $m (qw(-1 1)) {
488 $r= -0.5 * (-$b + $m*$d) / $a;
489 $radius= -$r * $distfact;
490 next if abs($radius) < $minradius;
491 $cfrom= ev_compose({}, $from, { X=>0, Y=>-$radius, A=>-0.5*$pi });
492 $cto= ev_compose({}, $to, { X=>0, Y=> $radius, A=> 0.5*$pi });
493 $midpt= ev_lincomb({}, $cfrom, $cto, 0.5);
494 $reverse= signum($r);
499 $delta1= ev_bearing($cfrom, $midpt) - $cfrom->{A};
500 $delta2= ev_bearing($cto, $midpt) - $cto->{A};
506 $path= [{ T=>Arc, F=>$from, C=>$cfrom, R=> $radius, D=>$delta1 },
507 { T=>Arc, F=>$to, C=>$cto, R=>-$radius, D=>$delta2 }];
509 push @solkinds, 'twoarcs';
512 my ($path,$segment,$bestpath,$len,$scores,$bestscores,@bends,$sk);
513 my ($crit,$cs,$i,$cmp);
514 foreach $path (@paths) {
515 $sk= shift @solkinds;
516 o("% possible path $sk $path\n");
519 foreach $segment (@$path) {
520 if ($segment->{T} eq Arc) {
521 o("% Arc C ".loc2dbg($segment->{C}).
522 " R $segment->{R} D ".ang2deg($segment->{D})."\n");
523 $len += abs($segment->{R} * $segment->{D});
524 push @bends, signum($segment->{R} * $segment->{D}); # right +ve
526 die "unknown segment $segment->{T}";
529 o("% length $len\n");
531 foreach $crit (@al, 'short') {
532 if ($crit eq 'long') { $cs= $len; }
533 elsif ($crit eq 'short') { $cs= -$len; }
534 elsif ($crit =~ m/^(begin|end|)(left|right)$/) {
535 if ($1 eq 'begin') { $cs= $bends[0]; }
536 elsif ($1 eq 'end') { $cs= $bends[$#bends]; }
537 else { $cs=0; map { $cs += $_ } @bends; }
538 $cs= -$cs if $2 eq 'left';
539 } elsif ($crit =~ m/^(\!?)(twoarcs|arcline|arcsline)$/) {
540 $cs= ($2 eq $sk) != ($1 eq '!');
544 o("% scores @$scores\n");
545 if (defined $bestpath) {
546 for ($i=0,$cmp=0; !$cmp && $i<@$scores; $i++) {
547 $cmp= $scores->[$i] <=> $bestscores->[$i];
552 $bestscores= $scores;
554 die "no solution" unless defined $bestpath;
555 o("% chose path $bestpath @al\n");
557 foreach $segment (@$bestpath) {
558 if ($segment->{T} eq 'Arc') {
559 arc({}, $segment->{C},$segment->{F},$segment->{R},$segment->{D});
561 die "unknown segment";
567 my ($from,$to,$radius,$len,$upto,$ctr,$beta,$ang,$how,$sign_r);
568 $from= can(\&cva_idex);
569 $to= can(\&cva_idnew);
570 printf DEBUG "from $from->{X} $from->{Y} $from->{A}\n";
571 $how= can(cvam_enum(qw(len upto ang uptoang parallel)));
572 if ($how eq 'len') { $len= can(\&cva_len); }
573 elsif ($how =~ m/ang$/) { $ang= can(\&cva_ang); }
574 elsif ($how eq 'parallel' || $how eq 'upto') { $upto= can(\&cva_idex); }
575 $radius= cano(\&cva_len, 'Inf'); # +ve is right hand bend
576 if ($radius eq 'Inf') {
577 # print DEBUG "extend inf $len\n";
578 if ($how eq 'upto') {
579 $len= ($upto->{X} - $from->{X}) * cos($from->{A})
580 + ($upto->{Y} - $from->{Y}) * sin($from->{A});
581 } elsif ($how eq 'len') {
583 die "len of straight spec by angle";
585 printf DEBUG "len $len\n";
586 $to->{X}= $from->{X} + $len * cos($from->{A});
587 $to->{Y}= $from->{Y} + $len * sin($from->{A});
588 $to->{A}= $from->{A};
589 parametric_segment(0.0, 1.0, abs($len), sub {
590 ev_lincomb({}, $from, $to, $param);
593 my ($sign_r, $sign_ang, $ctr, $beta_interval, $beta, $delta);
594 print DEBUG "radius >$radius<\n";
595 $radius *= $ctx->{Trans}{R};
596 $sign_r= signum($radius);
598 $ctr->{X}= $from->{X} + $radius * sin($from->{A});
599 $ctr->{Y}= $from->{Y} - $radius * cos($from->{A});
600 if ($how eq 'upto') {
601 $beta= atan2(-$sign_r * ($upto->{X} - $ctr->{X}),
602 $sign_r * ($upto->{Y} - $ctr->{Y}));
604 } elsif ($how eq 'parallel') {
607 } elsif ($how eq 'uptoang') {
608 $beta= input_absang($ang);
610 } elsif ($how eq 'len') {
611 $sign_ang= signum($len);
612 $beta= $from->{A} - $sign_r * $len / abs($radius);
615 $sign_ang= signum($ang);
616 $beta= $from->{A} - $sign_r * $ang;
619 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
620 $beta += $sign_ang * $sign_r * 4.0 * $pi;
622 $delta= $beta - $from->{A};
623 last if $sign_ang * $sign_r * $delta <= 0;
624 $beta -= $sign_ang * $sign_r * $beta_interval * $pi;
626 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
627 arc($to, ,$ctr,$from, $radius,$delta);
629 printf DEBUG "to $to->{X} $to->{Y} $to->{A}\n";
634 return "$loc->{X} $loc->{Y} ".ang2deg($loc->{A});
637 return $_[0] * 180 / $pi;
639 sub input_absang ($) {
640 return $_[0] * $ctx->{Trans}{R} + $ctx->{Trans}{A};
642 sub input_abscoords ($$) {
644 ($in->{X}, $in->{Y}) = @_;
646 $out= ev_compose({}, $ctx->{Trans}, $in);
647 return ($out->{X}, $out->{Y});
652 Trans => { X => 0.0, Y => 0.0, A => 0.0, R => 1.0 },
660 sub cmd_defobj { cmd__defobj(0); }
661 sub cmd_defpart { cmd__defobj(1); }
662 sub cmd__defobj ($) {
665 $id= can(\&cva_idstr);
666 die "nested defobj" if $defobj_save;
667 die "repeated defobj" if exists $objs{$id};
669 $defobj_ispart= $ispart;
672 $ctx->{InDefObj}= $id;
678 $id= $ctx->{InDefObj};
679 die "unmatched enddef" unless defined $id;
680 foreach $bit (qw(CmdLog Loc)) {
681 $objs{$id}{$bit}= $ctx->{$bit};
683 $objs{$id}{Part}= $defobj_ispart;
688 sub cmd__runobj ($) {
692 dv("cmd__runobj $obj_id ",'$ctx',$ctx);
693 foreach $c (@{ $objs{$obj_id}{CmdLog} }) {
695 next if $al[0] eq 'enddef';
700 sub cmd_part { cmd__obj(Part); }
701 sub cmd_obj { cmd__obj(1); }
702 sub cmd_objflip { cmd__obj(-1); }
706 my ($obj_id, $ctx_save, $pfx, $actual, $formal_id, $formal, $formcv);
707 my ($part_name, $ctx_inobj, $obj, $id, $newid, $newpt);
709 $part_name= can(\&cva_idstr);
710 $how= (@al && $al[0] =~ s/^\-//) ? -1 : +1;
712 $obj_id= can(\&cva_idstr);
713 if (defined $part_name) {
714 $formal_id= can(\&cva_idstr);
715 $actual= can(\&cva_idex);
717 $actual= can(\&cva_idex);
718 $formal_id= can(\&cva_idstr);
720 $obj= $objs{$obj_id};
721 dv("cmd__obj ",'$obj',$obj);
722 die "unknown obj $obj_id" unless $obj;
723 $formal= $obj->{Loc}{$formal_id};
724 die "unknown formal $formal_id" unless $formal;
727 $ctx->{Trans}{R}= $how;
728 $ctx->{Trans}{A}= $actual->{A} - $formal->{A}/$how;
729 $formcv= ev_compose({}, $ctx->{Trans},$formal);
730 $ctx->{Trans}{X}= $actual->{X} - $formcv->{X};
731 $ctx->{Trans}{Y}= $actual->{Y} - $formcv->{Y};
732 if (defined $part_name) {
733 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${part_name}:";
735 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}::";
737 $ctx->{Draw}= $ctx_save->{Draw};
739 $ctx->{Draw} =~ s/[LMN]//g;
740 $ctx->{Draw} =~ s/O/MNO/;
742 $ctx->{Draw} =~ s/[LM]//g;
743 $ctx->{Draw} =~ s/N/MN/;
745 cmd__runobj($obj_id);
746 if (defined $part_name) {
747 $pfx= $part_name.'_';
749 if (@al && $al[0] eq '=') {
752 $pfx= cano(\&cva_idstr,undef);
758 foreach $id (keys %{ $ctx_inobj->{Loc} }) {
759 next if $id eq $formal_id;
761 next if exists $ctx_save->{Loc}{$newid};
762 $newpt= cva_idnew($newid);
763 %$newpt= %{ $ctx_inobj->{Loc}{$id} };
766 if (defined $part_name) {
767 my ($formalr_id, $actualr_id, $formalr, $actualr);
769 die "part results come in pairs\n" unless @al>=2;
770 ($formalr_id, $actualr_id, @al) = @al;
771 if ($actualr_id =~ s/^\-//) {
772 $formalr_id= "-$formalr_id";
773 $formalr_id =~ s/^\-\-//;
775 $formalr= cva_idex($formalr_id);
777 local ($ctx) = $ctx_save;
778 $actualr= cva_idnew($actualr_id);
780 %$actualr= %$formalr;
787 dv("cmd__do $ctx @al ",'$ctx',$ctx);
788 $cmd= can(\&cva_cmd);
789 my ($id,$loc,$io,$ad);
790 $io= defined $ctx->{InDefObj} ? "$ctx->{InDefObj}!" : $ctx->{InRunObj};
791 o("%L cmd $io $cmd @al\n");
792 $ctx->{LocsMade}= [ ];
797 die "too many args" if @al;
798 foreach $id (@{ $ctx->{LocsMade} }) {
799 $loc= $ctx->{Loc}{$id};
800 $ad= ang2deg($loc->{A});
801 ol("%L point $io$id ".loc2dbg($loc)."\n");
802 if ($ctx->{Draw} =~ m/[LM]/) {
804 " $loc->{X} $loc->{Y} translate $ad rotate\n");
805 if ($ctx->{Draw} =~ m/M/) {
806 ol(" 0 $psu_allwidth newpath moveto\n".
807 " 0 -$psu_allwidth lineto\n".
808 " $lmu_marklw setlinewidth stroke\n");
810 if ($ctx->{Draw} =~ m/L/) {
811 ol(" /s ($id) def\n".
813 " /sx5 s stringwidth pop\n".
814 " 0.5 mul $lmu_txtboxpadx add def\n".
815 " -90 rotate 0 $lmu_txtboxoff translate newpath\n".
816 " sx5 neg 0 moveto\n".
817 " sx5 neg $lmu_txtboxh lineto\n".
818 " sx5 $lmu_txtboxh lineto\n".
819 " sx5 0 lineto closepath\n".
820 " gsave 1 setgray fill grestore\n".
821 " $lmu_txtboxlw setlinewidth stroke\n".
822 " sx5 neg $lmu_txtboxpadx add $lmu_txtboxtxty\n".
830 sub cmd_showlibrary {
831 my ($obj_id, $y, $x, $ctx_save, $width, $height);
832 my ($max_x, $min_x, $max_y, $min_y, $nxty, $obj, $loc, $pat, $got, $glob);
834 $x=$olu_left; $y=$olu_bottom; undef $nxty;
836 foreach $obj_id (sort keys %objs) {
838 foreach $glob (@al) {
840 $got= !($pat =~ s/^\!//);
841 die "bad pat" if $pat =~ m/[^0-9a-zA-Z_*?]/;
842 $pat =~ s/\*/\.*/g; $pat =~ s/\?/./g;
843 last if $obj_id =~ m/^$pat$/;
847 $obj= $objs{$obj_id};
848 next unless $obj->{Part};
849 ($min_x, $max_x, $min_y, $max_y) = bbox($obj->{Loc});
853 $width= $max_x - $min_x;
854 $height= $max_y - $min_y;
855 if ($width < $height) {
857 $ctx->{Trans}{X}= $x - $min_x;
858 $ctx->{Trans}{Y}= $y - $min_y + $olu_textheight;
860 ($width,$height)=($height,$width);
861 $ctx->{Trans}{A}= 0.5 * $pi;
862 $ctx->{Trans}{X}= $x + $max_y;
863 $ctx->{Trans}{Y}= $y - $min_x + $olu_textheight;
865 $adj= length($obj_id) * $olu_textallowperc - $width;
868 $ctx->{Trans}{X} += 0.5 * $adj;
869 if ($x + $width > $olu_right && defined $nxty) {
873 } elsif ($y + $height > $olu_top && $y > $olu_bottom) {
875 $x= $olu_left; $y= $olu_bottom;
882 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}//";
883 $ctx->{Draw}= $ctx_save->{Draw};
884 cmd__runobj($obj_id);
886 " /s ($obj_id) def\n".
888 ($x + 0.5*$width)." ".($y - $olu_textheight)." moveto\n".
889 " s stringwidth pop -0.5 mul 0 rmoveto\n".
890 " s show grestore\n");
891 $x += $width + $olu_gap_x;
892 upd_max(\$nxty, $y + $height + $olu_gap_y + $olu_textheight);
904 " /lf /Courier-New findfont $lmu_marktpt scalefont def\n".
905 " 615 0 translate 90 rotate\n".
906 " $ptscale $ptscale scale\n"
911 our $drawers= 'arsclmno';
912 our %chdraw_emap= qw(A ARSc
924 $ctx->{Draw}= 'RLMN';
928 while (@ARGV && $ARGV[0] =~ m/^\-/) {
929 last if $ARGV[0] eq '-';
934 if (s/^D(\d+)//) { $debug= $1; }
935 elsif (s/^D//) { $debug++; }
936 elsif (s/^q//) { $quiet=1; }
937 elsif (s/^([Ee])([a-zA-Z]+)//) {
940 foreach $c (split //, $2) {
942 die "bad -e option $c" unless defined $chdraw_emap{$c};
943 $c= $chdraw_emap{$c};
945 die "bad -E option $c" unless $c =~ m/[$drawers]/i;
947 $ctx->{Draw} =~ s/$c//ig;
948 $ctx->{Draw} .= $c if $c =~ m/[A-Z]/;
951 die "unknown option -$_";
956 open DEBUG, ($debug ? ">&2" : ">/dev/null") or die $!;
960 select(STDOUT); $|=1;
965 chomp; s/^\s+//; s/\s+$//;
966 @al= split /\s+/, $_;
968 print DEBUG "=== @al\n";
969 push @{ $ctx->{CmdLog} }, [ @al ] if exists $ctx->{CmdLog};
976 my ($min_x, $max_x, $min_y, $max_y) = bbox($ctx->{Loc});
978 if (defined $min_x) {
979 $bboxstr= sprintf("width %.2d (%.2d..%2.d)\n".
980 "height %.2d (%.2d..%2.d)\n",
981 $max_x - $min_x, $min_x, $max_x,
982 $max_y - $min_y, $min_y, $max_y);
984 $bboxstr= "no locs, no bbox\n";
986 if (!$quiet) { print STDERR $bboxstr; }
987 $bboxstr =~ s/^/\%L bbox /mg;
988 print $bboxstr or die $!;