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}= $id
48 # $ctx->{LocsMade}[]{Neg}= $id
52 # $ctx->{Trans}{X} # transformation. is ev representing
53 # $ctx->{Trans}{Y} # new origin. (is applied at _input_
54 # $ctx->{Trans}{A} # not at plot-time)
55 # $ctx->{Trans}{R} # but multiply all y coords by this!
56 # $ctx->{Draw} # sequence of one or more chrs from uc $drawers
60 # $objs{$id}{Part} # 1 iff object is a part
64 our @al; # current cmd
69 our $param; # for parametric_curve
74 # Operate on Enhanced Vectors which are a location (coordinates) and a
75 # direction at that location. Representation is a hash with members X
76 # Y and A (angle of the direction in radians, anticlockwise from
77 # East). May be absolute, or interpreted as relative, according to
80 # Each function's first argument is a hashref whose X Y A members will
81 # be created or overwritten; this hashref will be returned (so you can
82 # use it `functionally' by passing {}). The other arguments may be ev
83 # hashrefs, or other info. The results are in general undefined if
84 # one of the arguments is the same hash as the result.
87 # ev_byang(R, ANG,[LEN])
88 # result is evec of specified angle and length (default=1.0)
89 my ($res,$ang,$len)=@_;
90 $len=1.0 unless defined $len;
91 $res->{X}= $len * cos($ang);
92 $res->{Y}= $len * sin($ang);
96 sub ev_compose ($$$) {
97 # ev_compose(SUM_R, A,B);
98 # appends B to A, result is end of new B
99 # (B's X is forwards from end of A, Y is translating left from end of A)
100 # A may have a member R, which if provided then it should be 1.0 or -1.0,
101 # and B's Y and A will be multiplied by R first (ie, we can reflect);
102 my ($sum,$a,$b) = @_;
104 $r= defined $a->{R} ? $a->{R} : 1.0;
105 $sum->{X}= $a->{X} + $b->{X} * cos($a->{A}) - $r * $b->{Y} * sin($a->{A});
106 $sum->{Y}= $a->{Y} + $r * $b->{Y} * cos($a->{A}) + $b->{X} * sin($a->{A});
107 $sum->{A}= $a->{A} + $r * $b->{A};
110 sub ev_decompose ($$$) {
111 # ev_decompose(B_R, A,SUM)
112 # computes B_R s.t. ev_compose({}, A, B_R) gives SUM
115 $r= defined $a->{R} ? $a->{R} : 1.0;
116 $brx= $sum->{X} - $a->{X};
117 $bry= $r * ($sum->{Y} - $a->{Y});
118 $b->{X}= $brx * cos($a->{A}) + $bry * sin($a->{A});
119 $b->{Y}= $bry * cos($a->{A}) - $brx * sin($a->{A});
120 $b->{A}= $r * ($sum->{A} - $a->{A});
123 sub ev_lincomb ($$$$) {
124 # ev_linkcomb(RES,A,B,P)
125 # gives P*A + (1-P)*B
126 my ($r,$a,$b,$p) = @_;
128 map { $r->{$_} = $q * $a->{$_} + $p * $b->{$_} } qw(X Y A);
131 sub ev_bearing ($$) {
133 # returns bearing of B from A
134 # value returned is in [ A->{A}, A->{A} + 2*$pi >
135 # A->{A} and B->{A} are otherwise ignored
138 $r= atan2($b->{Y} - $a->{Y},
141 while ($r < $a->{A}) { $r += 2.0 * $pi; }
146 # returns distance from A to B
147 # A->{A} and B->{A} are ignored
150 $xd= $b->{X} - $a->{X};
151 $yd= $b->{Y} - $a->{Y};
152 return sqrt($xd*$xd + $yd*$yd);
157 $$limr= $now unless defined $$limr && $$limr <= $now;
161 $$limr= $now unless defined $$limr && $$limr >= $now;
165 my ($converter,$defaulter)=@_;
167 return &$defaulter unless @al;
169 $v= &$converter($spec);
170 dv('canf ','$spec',$spec, '$v',$v);
173 sub can ($) { my ($c)=@_; canf($c, sub { die "too few args"; }); }
174 sub cano ($$) { my ($c,$def)=@_; canf($c, sub { return $def }); }
176 sub signum ($) { return ($_[0] > 0) - ($_[0] < 0); }
180 my ($min_x, $max_x, $min_y, $max_y);
182 foreach $loc (values %$objhash) {
183 upd_min(\$min_x, $loc->{X} - abs($psu_allwidth * sin($loc->{A})));
184 upd_max(\$max_x, $loc->{X} + abs($psu_allwidth * sin($loc->{A})));
185 upd_min(\$min_y, $loc->{Y} - abs($psu_allwidth * cos($loc->{A})));
186 upd_max(\$max_y, $loc->{Y} + abs($psu_allwidth * cos($loc->{A})));
188 return ($min_x, $max_x, $min_y, $max_y);
191 our %units_len= qw(- mm mm 1 cm 10 m 1000);
192 our %units_ang= qw(- d r 1); $units_ang{'d'}= 2*$pi / 360;
194 sub cva_len ($) { my ($sp)=@_; cva_units($sp,\%units_len); }
195 sub cva_ang ($) { my ($sp)=@_; cva_units($sp,\%units_ang); }
196 sub cva_absang ($) { input_absang(cva_ang($_[0])) }
200 $sp =~ m/^([-0-9eE.]*[0-9.])([A-Za-z]*)$/
201 or die "lexically invalid quantity";
203 $u=$ua->{'-'} unless length $u;
204 defined $ua->{$u} or die "unknown unit $u";
206 print DEBUG "cva_units($sp,)=$r ($n $u $ua->{$u})\n";
211 die "invalid id" unless $sp =~ m/^[a-z][_0-9A-Za-z]*$/;
216 my ($id,$r,$d,$k,$neg,$na,$obj_id,$vflip,$locs);
217 if ($sp =~ s/^(\^?)(\w+)\!//) {
220 die "invalid obj $obj_id in loc" unless exists $objs{$obj_id};
221 $locs= $objs{$obj_id}{Loc};
226 $neg= $sp =~ s/^\-//;
228 die "unknown $id" unless defined $locs->{$id};
231 foreach $k (sort keys %$r) { $d .= " $k=$r->{$k}"; }
232 printf DEBUG "%s\n", $d;
234 $r= { X => $r->{X}, Y => -$r->{Y}, A => -$r->{A} };
238 $na -= 2*$pi if $na >= 2*$pi;
239 $r= { X => $r->{X}, Y => $r->{Y}, A => $na };
246 $neg = $sp =~ s/^\-//;
248 die "duplicate $id" if exists $ctx->{Loc}{$id};
249 exists $ctx->{Loc}{$id}{X};
250 push @{ $ctx->{LocsMade} }, { Id => $id, Neg => $neg };
251 return $ctx->{Loc}{$id};
253 sub cva_cmd ($) { return cva_idstr($_[0]); }
256 return $sp if grep { $_ eq $sp } @$el;
257 die "invalid option (permitted: @$el)";
259 sub cvam_enum { my (@e) = @_; return sub { cva__enum($_[0],\@e); }; }
263 $nl= can(\&cva_idnew);
264 $i->{X}= can(\&cva_len);
265 $i->{Y}= can(\&cva_len);
266 $i->{A}= can(\&cva_ang);
267 ev_compose($nl, $ctx->{Trans}, $i);
270 my ($from,$to,$len,$right,$turn);
271 $from= can(\&cva_idex);
272 $to= can(\&cva_idnew);
273 $len= cano(\&cva_len,0);
274 $right= cano(\&cva_len,0) * $ctx->{Trans}{R};
275 $turn= cano(\&cva_ang, 0) * $ctx->{Trans}{R};
276 my ($u)= ev_compose({}, $from, { X => $len, Y => -$right, A => 0 });
277 ev_compose($to, $u, { X => 0, Y => 0, A => $turn });
282 $pfx . ($pfx =~ m/\}$|\]$/ ? '' : '->');
286 return 'undef' if !defined $v;
287 return $v if $v !~ m/\W/ && $v =~ m/[A-Z]/ && $v =~ m/^[a-z_]/i;
288 return $v if $v =~ m/^[0-9.]+/;
289 $v =~ s/[\\\']/\\$&/g;
293 sub dv1_kind ($$$$$$$) {
294 my ($pfx,$expr,$ref,$ref_exp,$ixfmt,$ixesfn,$ixmapfn) = @_;
296 return 0 if $ref ne $ref_exp;
298 foreach $ix (&$ixesfn) {
300 my ($v)= &$ixmapfn($ix);
301 #print STDERR "dv1_kind($pfx,$expr,$ref,$ref_exp,$ixmapfn) ix=$ix v=$v\n";
302 dv1($pfx,$expr.sprintf($ixfmt,dv__evr($ix)),$v);
305 printf DEBUG "%s%s= $ixfmt\n", $pfx, $expr, ' ';
310 return 0 unless $debug;
311 my ($pfx,$expr,$v) = @_;
314 #print STDERR "dv1 >$pfx|$ref<\n";
316 printf DEBUG "%s%s= %s\n", $pfx,$expr, dv__evr($v);
318 } elsif ($ref eq 'SCALAR') {
319 dv1($pfx, ($expr =~ m/^\$/ ? "\$$expr" : '${'.$expr.'}'), $$v);
322 $expr.='->' unless $expr =~ m/\]$|\}$/;
323 return if dv1_kind($pfx,$expr,$ref,'ARRAY','[%s]',
324 sub { ($[ .. $#$v) },
325 sub { $v->[$_[0]] });
326 return if dv1_kind($pfx,$expr,$ref,'HASH','{%s}',
327 sub { sort keys %$v },
328 sub { $v->{$_[0]} });
329 printf DEBUG "%s%s is %s\n", $pfx, $expr, $ref;
341 sub o ($) { $o .= $_[0]; }
342 sub ol ($) { $ol .= $_[0]; }
344 print $o, $ol, " showpage\n"
351 sub o_path_begin () {
353 $o_path_verb= 'moveto';
355 sub o_path_point ($) {
357 o(" $pt $o_path_verb\n");
358 $o_path_verb= 'lineto';
360 sub o_path_stroke ($) {
362 o(" $width setlinewidth stroke\n");
366 my ($a,$b,$width)=@_;
370 o_path_stroke($width);
373 sub psu_coords ($$$) {
374 my ($ends,$inunit,$across)=@_;
375 # $ends->[0]{X} etc.; $inunit 0 to 1 (but go to 1.5);
376 # $across in mm, +ve to right.
377 my (%ea_zo, $zo, $prop);
378 $ea_zo{X}=$ea_zo{Y}=0;
379 foreach $zo (qw(0 1)) {
380 $prop= $zo ? $inunit : (1.0 - $inunit);
381 $ea_zo{X} += $prop * ($ends->[$zo]{X} - $across * sin($ends->[0]{A}));
382 $ea_zo{Y} += $prop * ($ends->[$zo]{Y} + $across * cos($ends->[0]{A}));
384 # dv("psu_coords ", '$ends',$ends, '$inunit',$inunit, '$across',$across,
385 # '\\%ea_zo', \%ea_zo);
386 return $ea_zo{X}." ".$ea_zo{Y};
389 sub parametric__o_pt ($) {
391 o_path_point("$pt->{X} $pt->{Y}");
394 sub parametric_segment ($$$$) {
395 my ($p0,$p1,$lenperp,$calcfn) = @_;
396 # makes $p (global) go from $p0 to $p1 ($p1>$p0)
397 # $lenperp is the length of one unit p, ie the curve
398 # must have a uniform `density' in parameter space
399 # $calcfn is invoked with $p set and should return a loc
400 # (ie, ref to X =>, Y =>, A =>).
401 my ($pa,$pb,@ends,$side,$ppu,$e,$v,$tick,$draw);
402 return unless $ctx->{Draw} =~ m/[ARSC]/;
403 $ppu= $psu_ulen/$lenperp;
404 my ($railctr)=($psu_gauge + $psu_raillw)*0.5;
405 my ($tickend)=($psu_allwidth - $psu_ticklen);
406 my ($tickpitch)=($psu_ulen / $psu_ticksperu);
407 my ($sleeperctr)=($psu_ulen*0.5);
408 my ($sleeperend)=($psu_sleeperlen*0.5);
409 print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
413 o(" $psu_thinlw setlinewidth\n");
415 for ($param=$p0; $param<$p1; $param += $ppu) {
416 parametric__o_pt(&$calcfn);
419 parametric__o_pt(&$calcfn);
422 return unless $draw =~ m/[ARS]/;
423 for ($pa= $p0; $pa<$p1; $pa=$pb) {
425 $param= $pa; $ends[0]= @ends ? $ends[1] : &$calcfn;
426 $param= $pb; $ends[1]= &$calcfn;
427 #print DEBUG "pa $pa $ends[0]{X} $ends[0]{Y} $ends[0]{A}\n";
428 #print DEBUG "pb $pb $ends[1]{X} $ends[1]{Y} $ends[1]{A}\n";
429 $e= $pb<=$p1 ? 1.0 : ($p1-$pa)/$ppu;
432 o_path_point(psu_coords(\@ends,0,-$psu_allwidth));
433 o_path_point(psu_coords(\@ends,0,$psu_allwidth));
434 o_path_point(psu_coords(\@ends,$e,$psu_allwidth));
435 o_path_point(psu_coords(\@ends,$e,-$psu_allwidth));
436 o(" closepath clip\n");
437 foreach $side qw(-1 1) {
439 o_line(psu_coords(\@ends,0,$side*$railctr),
440 psu_coords(\@ends,1.5,$side*$railctr),
444 o_line(psu_coords(\@ends,0,$side*$psu_allwidth),
445 psu_coords(\@ends,1.5,$side*$psu_allwidth),
447 for ($tick=0; $tick<1.5; $tick+=$tickpitch/$psu_ulen) {
448 o_line(psu_coords(\@ends,$tick,$side*$psu_allwidth),
449 psu_coords(\@ends,$tick,$side*$tickend),
455 o_line(psu_coords(\@ends,$sleeperctr,-$sleeperend),
456 psu_coords(\@ends,$sleeperctr,+$sleeperend),
464 my ($to, $ctr,$from, $radius,$delta) = @_;
465 # does parametric_segment to draw an arc centred on $ctr
466 # ($ctr->{A} ignored)
467 # from $from with radius $radius (this must be consistent!)
468 # and directionally-subtending an angle $delta.
469 # sets $to->... to be the other end, and returns $to
471 $to->{A}= $beta= $from->{A} + $delta;
472 $to->{X}= $ctr->{X} - $radius * sin($beta);
473 $to->{Y}= $ctr->{Y} + $radius * cos($beta);
474 return if abs($delta*$radius) < 1E-9;
475 parametric_segment(0.0,1.0, abs($radius*$delta), sub {
476 my ($beta) = $from->{A} + $delta * $param;
477 return { X => $ctr->{X} - $radius * sin($beta),
478 Y => $ctr->{Y} + $radius * cos($beta),
484 my ($from,$to,$how,$minradius);
485 $from= can(\&cva_idex);
486 $to= can(\&cva_idex);
487 $minradius= can(\&cva_len);
488 my (@paths,@solkinds);
490 my ($sigma,$distfact, $theta,$phi, $a,$b,$c,$d, $m,$r, $radius);
491 my ($cvec,$cfrom,$cto,$midpt, $delta1,$delta2, $path,$reverse);
492 $sigma= ev_bearing($from,$to);
493 $distfact= v_dist($from,$to);
494 $theta= 0.5 * $pi - ($from->{A} - $sigma);
495 $phi= 0.5 * $pi - ($to->{A} + $pi - $sigma);
496 $a= 2 * (1 + cos($theta - $phi));
497 $b= 2 * (cos($theta) - cos($phi));
499 $d= sqrt($b*$b - 4*$a*$c);
500 foreach $m (qw(-1 1)) {
502 $r= -0.5 * (-$b + $m*$d) / $a;
503 $radius= -$r * $distfact;
504 next if abs($radius) < $minradius;
505 $cfrom= ev_compose({}, $from, { X=>0, Y=>-$radius, A=>-0.5*$pi });
506 $cto= ev_compose({}, $to, { X=>0, Y=> $radius, A=> 0.5*$pi });
507 $midpt= ev_lincomb({}, $cfrom, $cto, 0.5);
508 $reverse= signum($r);
513 $delta1= ev_bearing($cfrom, $midpt) - $cfrom->{A};
514 $delta2= ev_bearing($cto, $midpt) - $cto->{A};
520 $path= [{ T=>Arc, F=>$from, C=>$cfrom, R=> $radius, D=>$delta1 },
521 { T=>Arc, F=>$to, C=>$cto, R=>-$radius, D=>$delta2 }];
523 push @solkinds, 'twoarcs';
526 my ($path,$segment,$bestpath,$len,$scores,$bestscores,@bends,$sk);
527 my ($crit,$cs,$i,$cmp);
528 foreach $path (@paths) {
529 $sk= shift @solkinds;
530 o("% possible path $sk $path\n");
533 foreach $segment (@$path) {
534 if ($segment->{T} eq Arc) {
535 o("% Arc C ".loc2dbg($segment->{C}).
536 " R $segment->{R} D ".ang2deg($segment->{D})."\n");
537 $len += abs($segment->{R} * $segment->{D});
538 push @bends, signum($segment->{R} * $segment->{D}); # right +ve
540 die "unknown segment $segment->{T}";
543 o("% length $len\n");
545 foreach $crit (@al, 'short') {
546 if ($crit eq 'long') { $cs= $len; }
547 elsif ($crit eq 'short') { $cs= -$len; }
548 elsif ($crit =~ m/^(begin|end|)(left|right)$/) {
549 if ($1 eq 'begin') { $cs= $bends[0]; }
550 elsif ($1 eq 'end') { $cs= $bends[$#bends]; }
551 else { $cs=0; map { $cs += $_ } @bends; }
552 $cs= -$cs if $2 eq 'left';
553 } elsif ($crit =~ m/^(\!?)(twoarcs|arcline|arcsline)$/) {
554 $cs= ($2 eq $sk) != ($1 eq '!');
558 o("% scores @$scores\n");
559 if (defined $bestpath) {
560 for ($i=0,$cmp=0; !$cmp && $i<@$scores; $i++) {
561 $cmp= $scores->[$i] <=> $bestscores->[$i];
566 $bestscores= $scores;
568 die "no solution" unless defined $bestpath;
569 o("% chose path $bestpath @al\n");
571 foreach $segment (@$bestpath) {
572 if ($segment->{T} eq 'Arc') {
573 arc({}, $segment->{C},$segment->{F},$segment->{R},$segment->{D});
575 die "unknown segment";
581 my ($from,$to,$radius,$len,$upto,$ctr,$beta,$ang,$how,$sign_r);
582 $from= can(\&cva_idex);
583 $to= can(\&cva_idnew);
584 printf DEBUG "from $from->{X} $from->{Y} $from->{A}\n";
585 $how= can(cvam_enum(qw(len upto ang uptoang parallel)));
586 if ($how eq 'len') { $len= can(\&cva_len); }
587 elsif ($how =~ m/ang$/) { $ang= can(\&cva_ang); }
588 elsif ($how eq 'parallel' || $how eq 'upto') { $upto= can(\&cva_idex); }
589 $radius= cano(\&cva_len, 'Inf'); # +ve is right hand bend
590 if ($radius eq 'Inf') {
591 # print DEBUG "extend inf $len\n";
592 if ($how eq 'upto') {
593 $len= ($upto->{X} - $from->{X}) * cos($from->{A})
594 + ($upto->{Y} - $from->{Y}) * sin($from->{A});
595 } elsif ($how eq 'len') {
597 die "len of straight spec by angle";
599 printf DEBUG "len $len\n";
600 $to->{X}= $from->{X} + $len * cos($from->{A});
601 $to->{Y}= $from->{Y} + $len * sin($from->{A});
602 $to->{A}= $from->{A};
603 parametric_segment(0.0, 1.0, abs($len), sub {
604 ev_lincomb({}, $from, $to, $param);
607 my ($sign_r, $sign_ang, $ctr, $beta_interval, $beta, $delta);
608 print DEBUG "radius >$radius<\n";
609 $radius *= $ctx->{Trans}{R};
610 $sign_r= signum($radius);
612 $ctr->{X}= $from->{X} + $radius * sin($from->{A});
613 $ctr->{Y}= $from->{Y} - $radius * cos($from->{A});
614 if ($how eq 'upto') {
615 $beta= atan2(-$sign_r * ($upto->{X} - $ctr->{X}),
616 $sign_r * ($upto->{Y} - $ctr->{Y}));
618 } elsif ($how eq 'parallel') {
621 } elsif ($how eq 'uptoang') {
622 $beta= input_absang($ang);
624 } elsif ($how eq 'len') {
625 $sign_ang= signum($len);
626 $beta= $from->{A} - $sign_r * $len / abs($radius);
629 $sign_ang= signum($ang);
630 $beta= $from->{A} - $sign_r * $ang;
633 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
634 $beta += $sign_ang * $sign_r * 4.0 * $pi;
636 $delta= $beta - $from->{A};
637 last if $sign_ang * $sign_r * $delta <= 0;
638 $beta -= $sign_ang * $sign_r * $beta_interval * $pi;
640 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
641 arc($to, ,$ctr,$from, $radius,$delta);
643 printf DEBUG "to $to->{X} $to->{Y} $to->{A}\n";
648 return "$loc->{X} $loc->{Y} ".ang2deg($loc->{A});
651 return $_[0] * 180 / $pi;
653 sub input_absang ($) {
654 return $_[0] * $ctx->{Trans}{R} + $ctx->{Trans}{A};
656 sub input_abscoords ($$) {
658 ($in->{X}, $in->{Y}) = @_;
660 $out= ev_compose({}, $ctx->{Trans}, $in);
661 return ($out->{X}, $out->{Y});
666 Trans => { X => 0.0, Y => 0.0, A => 0.0, R => 1.0 },
674 sub cmd_defobj { cmd__defobj(0); }
675 sub cmd_defpart { cmd__defobj(1); }
676 sub cmd__defobj ($) {
679 $id= can(\&cva_idstr);
680 die "nested defobj" if $defobj_save;
681 die "repeated defobj" if exists $objs{$id};
683 $defobj_ispart= $ispart;
686 $ctx->{InDefObj}= $id;
692 $id= $ctx->{InDefObj};
693 die "unmatched enddef" unless defined $id;
694 foreach $bit (qw(CmdLog Loc)) {
695 $objs{$id}{$bit}= $ctx->{$bit};
697 $objs{$id}{Part}= $defobj_ispart;
702 sub cmd__runobj ($) {
706 dv("cmd__runobj $obj_id ",'$ctx',$ctx);
707 foreach $c (@{ $objs{$obj_id}{CmdLog} }) {
709 next if $al[0] eq 'enddef';
714 sub cmd_part { cmd__obj(Part); }
715 sub cmd_obj { cmd__obj(1); }
716 sub cmd_objflip { cmd__obj(-1); }
720 my ($obj_id, $ctx_save, $pfx, $actual, $formal_id, $formal, $formcv);
721 my ($part_name, $ctx_inobj, $obj, $id, $newid, $newpt);
723 $part_name= can(\&cva_idstr);
724 $how= (@al && $al[0] =~ s/^\^//) ? -1 : +1;
726 $obj_id= can(\&cva_idstr);
727 if (defined $part_name) {
728 $formal_id= can(\&cva_idstr);
729 $actual= cano(\&cva_idex, undef);
730 if (!defined $actual) {
731 $actual= cva_idex("${part_name}_${formal_id}");
734 $actual= can(\&cva_idex);
735 $formal_id= can(\&cva_idstr);
737 $obj= $objs{$obj_id};
738 dv("cmd__obj ",'$obj',$obj);
739 die "unknown obj $obj_id" unless $obj;
740 $formal= $obj->{Loc}{$formal_id};
741 die "unknown formal $formal_id" unless $formal;
744 $how *= $ctx_save->{Trans}{R};
745 $ctx->{Trans}{R}= $how;
746 $ctx->{Trans}{A}= $actual->{A} - $formal->{A}/$how;
747 $formcv= ev_compose({}, $ctx->{Trans},$formal);
748 $ctx->{Trans}{X}= $actual->{X} - $formcv->{X};
749 $ctx->{Trans}{Y}= $actual->{Y} - $formcv->{Y};
750 if (defined $part_name) {
751 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${part_name}:";
753 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}::";
755 $ctx->{Draw}= $ctx_save->{Draw};
757 $ctx->{Draw} =~ s/[LMN]//g;
758 $ctx->{Draw} =~ s/O/MNO/;
760 $ctx->{Draw} =~ s/[LM]//g;
761 $ctx->{Draw} =~ s/N/MN/;
763 cmd__runobj($obj_id);
764 if (defined $part_name) {
765 $pfx= $part_name.'_';
767 if (@al && $al[0] eq '=') {
770 $pfx= cano(\&cva_idstr,undef);
776 foreach $id (keys %{ $ctx_inobj->{Loc} }) {
777 next if $id eq $formal_id;
779 next if exists $ctx_save->{Loc}{$newid};
780 $newpt= cva_idnew($newid);
781 %$newpt= %{ $ctx_inobj->{Loc}{$id} };
784 if (defined $part_name) {
785 my ($formalr_id, $actualr_id, $formalr, $actualr);
787 die "part results come in pairs\n" unless @al>=2;
788 ($formalr_id, $actualr_id, @al) = @al;
789 if ($actualr_id =~ s/^\-//) {
790 $formalr_id= "-$formalr_id";
791 $formalr_id =~ s/^\-\-//;
794 local ($ctx) = $ctx_inobj;
795 $formalr= cva_idex($formalr_id);
797 $actualr= cva_idnew($actualr_id);
798 %$actualr= %$formalr;
805 dv("cmd__do $ctx @al ",'$ctx',$ctx);
806 $cmd= can(\&cva_cmd);
807 my ($lm,$id,$loc,$io,$ad);
808 $io= defined $ctx->{InDefObj} ? "$ctx->{InDefObj}!" : $ctx->{InRunObj};
809 o("%L cmd $io $cmd @al\n");
810 $ctx->{LocsMade}= [ ];
815 die "too many args" if @al;
816 foreach $lm (@{ $ctx->{LocsMade} }) {
818 $loc= $ctx->{Loc}{$id};
819 $loc->{A} += $pi if $lm->{Neg};
820 $ad= ang2deg($loc->{A});
821 ol("%L point $io$id ".loc2dbg($loc)." ($lm->{Neg})\n");
822 if ($ctx->{Draw} =~ m/[LM]/) {
824 " $loc->{X} $loc->{Y} translate $ad rotate\n");
825 if ($ctx->{Draw} =~ m/M/) {
826 ol(" 0 $psu_allwidth newpath moveto\n".
827 " 0 -$psu_allwidth lineto\n".
828 " $lmu_marklw setlinewidth stroke\n");
830 if ($ctx->{Draw} =~ m/L/) {
831 ol(" /s ($id) def\n".
833 " /sx5 s stringwidth pop\n".
834 " 0.5 mul $lmu_txtboxpadx add def\n".
835 " -90 rotate 0 $lmu_txtboxoff translate newpath\n".
836 " sx5 neg 0 moveto\n".
837 " sx5 neg $lmu_txtboxh lineto\n".
838 " sx5 $lmu_txtboxh lineto\n".
839 " sx5 0 lineto closepath\n".
840 " gsave 1 setgray fill grestore\n".
841 " $lmu_txtboxlw setlinewidth stroke\n".
842 " sx5 neg $lmu_txtboxpadx add $lmu_txtboxtxty\n".
850 sub cmd_showlibrary {
851 my ($obj_id, $y, $x, $ctx_save, $width, $height);
852 my ($max_x, $min_x, $max_y, $min_y, $nxty, $obj, $loc, $pat, $got, $glob);
854 $x=$olu_left; $y=$olu_bottom; undef $nxty;
856 foreach $obj_id (sort keys %objs) {
858 foreach $glob (@al) {
860 $got= !($pat =~ s/^\!//);
861 die "bad pat" if $pat =~ m/[^0-9a-zA-Z_*?]/;
862 $pat =~ s/\*/\.*/g; $pat =~ s/\?/./g;
863 last if $obj_id =~ m/^$pat$/;
867 $obj= $objs{$obj_id};
868 next unless $obj->{Part};
869 ($min_x, $max_x, $min_y, $max_y) = bbox($obj->{Loc});
873 $width= $max_x - $min_x;
874 $height= $max_y - $min_y;
875 if ($width < $height) {
877 $ctx->{Trans}{X}= $x - $min_x;
878 $ctx->{Trans}{Y}= $y - $min_y + $olu_textheight;
880 ($width,$height)=($height,$width);
881 $ctx->{Trans}{A}= 0.5 * $pi;
882 $ctx->{Trans}{X}= $x + $max_y;
883 $ctx->{Trans}{Y}= $y - $min_x + $olu_textheight;
885 $adj= length($obj_id) * $olu_textallowperc - $width;
888 $ctx->{Trans}{X} += 0.5 * $adj;
889 if ($x + $width > $olu_right && defined $nxty) {
893 } elsif ($y + $height > $olu_top && $y > $olu_bottom) {
895 $x= $olu_left; $y= $olu_bottom;
902 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}//";
903 $ctx->{Draw}= $ctx_save->{Draw};
904 cmd__runobj($obj_id);
906 " /s ($obj_id) def\n".
908 ($x + 0.5*$width)." ".($y - $olu_textheight)." moveto\n".
909 " s stringwidth pop -0.5 mul 0 rmoveto\n".
910 " s show grestore\n");
911 $x += $width + $olu_gap_x;
912 upd_max(\$nxty, $y + $height + $olu_gap_y + $olu_textheight);
924 " /lf /Courier-New findfont $lmu_marktpt scalefont def\n".
925 " 615 0 translate 90 rotate\n".
926 " $ptscale $ptscale scale\n"
931 our $drawers= 'arsclmno';
932 our %chdraw_emap= qw(A ARSc
944 $ctx->{Draw}= 'RLMN';
948 while (@ARGV && $ARGV[0] =~ m/^\-/) {
949 last if $ARGV[0] eq '-';
954 if (s/^D(\d+)//) { $debug= $1; }
955 elsif (s/^D//) { $debug++; }
956 elsif (s/^q//) { $quiet=1; }
957 elsif (s/^([Ee])([a-zA-Z]+)//) {
960 foreach $c (split //, $2) {
962 die "bad -e option $c" unless defined $chdraw_emap{$c};
963 @c= split //, $chdraw_emap{$c};
965 die "bad -E option $c" unless $c =~ m/[$drawers]/i;
969 $ctx->{Draw} =~ s/$cc//ig;
970 $ctx->{Draw} .= $cc if $cc =~ m/[A-Z]/;
974 die "unknown option -$_";
979 open DEBUG, ($debug ? ">&2" : ">/dev/null") or die $!;
983 select(STDOUT); $|=1;
988 chomp; s/^\s+//; s/\s+$//;
989 @al= split /\s+/, $_;
991 print DEBUG "=== @al\n";
992 last if $al[0] eq 'eof';
993 push @{ $ctx->{CmdLog} }, [ @al ] if exists $ctx->{CmdLog};
1000 my ($min_x, $max_x, $min_y, $max_y) = bbox($ctx->{Loc});
1002 if (defined $min_x) {
1003 $bboxstr= sprintf("width %.2d (%.2d..%2.d)\n".
1004 "height %.2d (%.2d..%2.d)\n",
1005 $max_x - $min_x, $min_x, $max_x,
1006 $max_y - $min_y, $min_y, $max_y);
1008 $bboxstr= "no locs, no bbox\n";
1010 if (!$quiet) { print STDERR $bboxstr; }
1011 $bboxstr =~ s/^/\%L bbox /mg;
1012 print $bboxstr or die $!;