8 our $ptscale= 72/25.4 / $scale;
13 our $psu_ticksperu= 1;
14 our $psu_ticklen= 5.0;
16 our $psu_sleeperlen= 17;
17 our $psu_sleeperlw= 15;
23 our $lmu_txtboxtxty= $lmu_marktpt * 0.300;
24 our $lmu_txtboxh= $lmu_marktpt * 1.100;
25 our $lmu_txtboxpadx= $lmu_marktpt * 0.335;
26 our $lmu_txtboxoff= $lmu_marklw / 2;
29 our $olu_left= 10 * $scale;
30 our $olu_right= 217 * $scale - $olu_left;
31 our $olu_bottom= 20 * $scale;
32 our $olu_top= 270 * $scale - $olu_bottom;
35 our $olu_textheight= 15;
36 our $olu_textallowperc= $lmu_marktpt * 5.0/11;
42 return 27 unless defined $radius;
43 $radius= abs($radius);
44 return ($radius >= 450 ? 33 :
48 sub allwidth ($) { return allwidth2($_[0]) * 0.5; }
50 our $allwidthmax= allwidth(0);
51 our $allwidthmin= allwidth(undef);
54 # $ctx->{CmdLog}= undef } not in defobj
55 # $ctx->{CmdLog}[]= [ command args ] } in defobj
56 # $ctx->{LocsMade}[]{Id}= $id
57 # $ctx->{LocsMade}[]{Neg}= $id
61 # $ctx->{Trans}{X} # transformation. is ev representing
62 # $ctx->{Trans}{Y} # new origin. (is applied at _input_
63 # $ctx->{Trans}{A} # not at plot-time)
64 # $ctx->{Trans}{R} # but multiply all y coords by this!
65 # $ctx->{Draw} # sequence of one or more chrs from uc $drawers
69 # $objs{$id}{Part} # 1 iff object is a part
73 our @al; # current cmd
78 our $param; # for parametric_curve
83 # Operate on Enhanced Vectors which are a location (coordinates) and a
84 # direction at that location. Representation is a hash with members X
85 # Y and A (angle of the direction in radians, anticlockwise from
86 # East). May be absolute, or interpreted as relative, according to
89 # Each function's first argument is a hashref whose X Y A members will
90 # be created or overwritten; this hashref will be returned (so you can
91 # use it `functionally' by passing {}). The other arguments may be ev
92 # hashrefs, or other info. The results are in general undefined if
93 # one of the arguments is the same hash as the result.
96 # ev_byang(R, ANG,[LEN])
97 # result is evec of specified angle and length (default=1.0)
98 my ($res,$ang,$len)=@_;
99 $len=1.0 unless defined $len;
100 $res->{X}= $len * cos($ang);
101 $res->{Y}= $len * sin($ang);
105 sub ev_compose ($$$) {
106 # ev_compose(SUM_R, A,B);
107 # appends B to A, result is end of new B
108 # (B's X is forwards from end of A, Y is translating left from end of A)
109 # A may have a member R, which if provided then it should be 1.0 or -1.0,
110 # and B's Y and A will be multiplied by R first (ie, we can reflect);
111 my ($sum,$a,$b) = @_;
113 $r= defined $a->{R} ? $a->{R} : 1.0;
114 $sum->{X}= $a->{X} + $b->{X} * cos($a->{A}) - $r * $b->{Y} * sin($a->{A});
115 $sum->{Y}= $a->{Y} + $r * $b->{Y} * cos($a->{A}) + $b->{X} * sin($a->{A});
116 $sum->{A}= $a->{A} + $r * $b->{A};
119 sub ev_decompose ($$$) {
120 # ev_decompose(B_R, A,SUM)
121 # computes B_R s.t. ev_compose({}, A, B_R) gives SUM
124 $r= defined $a->{R} ? $a->{R} : 1.0;
125 $brx= $sum->{X} - $a->{X};
126 $bry= $r * ($sum->{Y} - $a->{Y});
127 $b->{X}= $brx * cos($a->{A}) + $bry * sin($a->{A});
128 $b->{Y}= $bry * cos($a->{A}) - $brx * sin($a->{A});
129 $b->{A}= $r * ($sum->{A} - $a->{A});
132 sub ev_lincomb ($$$$) {
133 # ev_linkcomb(RES,A,B,P)
134 # gives P*A + (1-P)*B
135 my ($r,$a,$b,$p) = @_;
137 map { $r->{$_} = $q * $a->{$_} + $p * $b->{$_} } qw(X Y A);
140 sub ev_bearing ($$) {
142 # returns bearing of B from A
143 # value returned is in [ A->{A}, A->{A} + 2*$pi >
144 # A->{A} and B->{A} are otherwise ignored
147 $r= atan2($b->{Y} - $a->{Y},
150 while ($r < $a->{A}) { $r += 2.0 * $pi; }
155 # returns distance from A to B
156 # A->{A} and B->{A} are ignored
159 $xd= $b->{X} - $a->{X};
160 $yd= $b->{Y} - $a->{Y};
161 return sqrt($xd*$xd + $yd*$yd);
166 $$limr= $now unless defined $$limr && $$limr <= $now;
170 $$limr= $now unless defined $$limr && $$limr >= $now;
174 my ($converter,$defaulter)=@_;
176 return &$defaulter unless @al;
178 $v= &$converter($spec);
179 dv('canf ','$spec',$spec, '$v',$v);
182 sub can ($) { my ($c)=@_; canf($c, sub { die "too few args"; }); }
183 sub cano ($$) { my ($c,$def)=@_; canf($c, sub { return $def }); }
185 sub signum ($) { return ($_[0] > 0) - ($_[0] < 0); }
189 my ($min_x, $max_x, $min_y, $max_y);
191 foreach $loc (values %$objhash) {
192 upd_min(\$min_x, $loc->{X} - abs($allwidthmax * sin($loc->{A})));
193 upd_max(\$max_x, $loc->{X} + abs($allwidthmax * sin($loc->{A})));
194 upd_min(\$min_y, $loc->{Y} - abs($allwidthmax * cos($loc->{A})));
195 upd_max(\$max_y, $loc->{Y} + abs($allwidthmax * cos($loc->{A})));
197 return ($min_x, $max_x, $min_y, $max_y);
200 our %units_len= qw(- mm mm 1 cm 10 m 1000);
201 our %units_ang= qw(- d r 1); $units_ang{'d'}= 2*$pi / 360;
203 sub cva_len ($) { my ($sp)=@_; cva_units($sp,\%units_len); }
204 sub cva_ang ($) { my ($sp)=@_; cva_units($sp,\%units_ang); }
205 sub cva_absang ($) { input_absang(cva_ang($_[0])) }
209 $sp =~ m/^([-0-9eE.]*[0-9.])([A-Za-z]*)$/
210 or die "lexically invalid quantity";
212 $u=$ua->{'-'} unless length $u;
213 defined $ua->{$u} or die "unknown unit $u";
215 print DEBUG "cva_units($sp,)=$r ($n $u $ua->{$u})\n";
220 die "invalid id" unless $sp =~ m/^[a-z][_0-9A-Za-z]*$/;
225 my ($id,$r,$d,$k,$neg,$na,$obj_id,$vflip,$locs);
226 if ($sp =~ s/^(\^?)(\w+)\!//) {
229 die "invalid obj $obj_id in loc" unless exists $objs{$obj_id};
230 $locs= $objs{$obj_id}{Loc};
235 $neg= $sp =~ s/^\-//;
237 die "unknown $id" unless defined $locs->{$id};
240 foreach $k (sort keys %$r) { $d .= " $k=$r->{$k}"; }
241 printf DEBUG "%s\n", $d;
243 $r= { X => $r->{X}, Y => -$r->{Y}, A => -$r->{A} };
247 $na -= 2*$pi if $na >= 2*$pi;
248 $r= { X => $r->{X}, Y => $r->{Y}, A => $na };
255 $neg = $sp =~ s/^\-//;
257 die "duplicate $id" if exists $ctx->{Loc}{$id};
258 exists $ctx->{Loc}{$id}{X};
259 push @{ $ctx->{LocsMade} }, { Id => $id, Neg => $neg };
260 return $ctx->{Loc}{$id};
262 sub cva_cmd ($) { return cva_idstr($_[0]); }
265 return $sp if grep { $_ eq $sp } @$el;
266 die "invalid option (permitted: @$el)";
268 sub cvam_enum { my (@e) = @_; return sub { cva__enum($_[0],\@e); }; }
272 $nl= can(\&cva_idnew);
273 $i->{X}= can(\&cva_len);
274 $i->{Y}= can(\&cva_len);
275 $i->{A}= can(\&cva_ang);
276 ev_compose($nl, $ctx->{Trans}, $i);
279 my ($from,$to,$len,$right,$turn);
280 $from= can(\&cva_idex);
281 $to= can(\&cva_idnew);
282 $len= cano(\&cva_len,0);
283 $right= cano(\&cva_len,0) * $ctx->{Trans}{R};
284 $turn= cano(\&cva_ang, 0) * $ctx->{Trans}{R};
285 my ($u)= ev_compose({}, $from, { X => $len, Y => -$right, A => 0 });
286 ev_compose($to, $u, { X => 0, Y => 0, A => $turn });
291 $pfx . ($pfx =~ m/\}$|\]$/ ? '' : '->');
295 return 'undef' if !defined $v;
296 return $v if $v !~ m/\W/ && $v =~ m/[A-Z]/ && $v =~ m/^[a-z_]/i;
297 return $v if $v =~ m/^[0-9.]+/;
298 $v =~ s/[\\\']/\\$&/g;
302 sub dv1_kind ($$$$$$$) {
303 my ($pfx,$expr,$ref,$ref_exp,$ixfmt,$ixesfn,$ixmapfn) = @_;
305 return 0 if $ref ne $ref_exp;
307 foreach $ix (&$ixesfn) {
309 my ($v)= &$ixmapfn($ix);
310 #print STDERR "dv1_kind($pfx,$expr,$ref,$ref_exp,$ixmapfn) ix=$ix v=$v\n";
311 dv1($pfx,$expr.sprintf($ixfmt,dv__evr($ix)),$v);
314 printf DEBUG "%s%s= $ixfmt\n", $pfx, $expr, ' ';
319 return 0 unless $debug;
320 my ($pfx,$expr,$v) = @_;
323 #print STDERR "dv1 >$pfx|$ref<\n";
325 printf DEBUG "%s%s= %s\n", $pfx,$expr, dv__evr($v);
327 } elsif ($ref eq 'SCALAR') {
328 dv1($pfx, ($expr =~ m/^\$/ ? "\$$expr" : '${'.$expr.'}'), $$v);
331 $expr.='->' unless $expr =~ m/\]$|\}$/;
332 return if dv1_kind($pfx,$expr,$ref,'ARRAY','[%s]',
333 sub { ($[ .. $#$v) },
334 sub { $v->[$_[0]] });
335 return if dv1_kind($pfx,$expr,$ref,'HASH','{%s}',
336 sub { sort keys %$v },
337 sub { $v->{$_[0]} });
338 printf DEBUG "%s%s is %s\n", $pfx, $expr, $ref;
350 sub o ($) { $o .= $_[0]; }
351 sub ol ($) { $ol .= $_[0]; }
353 print $o, $ol, " showpage\n"
360 sub o_path_begin () {
362 $o_path_verb= 'moveto';
364 sub o_path_point ($) {
366 o(" $pt $o_path_verb\n");
367 $o_path_verb= 'lineto';
369 sub o_path_stroke ($) {
371 o(" $width setlinewidth stroke\n");
375 my ($a,$b,$width)=@_;
379 o_path_stroke($width);
382 sub psu_coords ($$$) {
383 my ($ends,$inunit,$across)=@_;
384 # $ends->[0]{X} etc.; $inunit 0 to 1 (but go to 1.5);
385 # $across in mm, +ve to right.
386 my (%ea_zo, $zo, $prop);
387 $ea_zo{X}=$ea_zo{Y}=0;
388 foreach $zo (qw(0 1)) {
389 $prop= $zo ? $inunit : (1.0 - $inunit);
390 $ea_zo{X} += $prop * ($ends->[$zo]{X} - $across * sin($ends->[0]{A}));
391 $ea_zo{Y} += $prop * ($ends->[$zo]{Y} + $across * cos($ends->[0]{A}));
393 # dv("psu_coords ", '$ends',$ends, '$inunit',$inunit, '$across',$across,
394 # '\\%ea_zo', \%ea_zo);
395 return $ea_zo{X}." ".$ea_zo{Y};
398 sub parametric__o_pt ($) {
400 o_path_point("$pt->{X} $pt->{Y}");
403 sub parametric_segment ($$$$$) {
404 my ($p0,$p1,$lenperp,$minradius,$calcfn) = @_;
405 # makes $p (global) go from $p0 to $p1 ($p1>$p0)
406 # $lenperp is the length of one unit p, ie the curve
407 # must have a uniform `density' in parameter space
408 # $calcfn is invoked with $p set and should return a loc
409 # (ie, ref to X =>, Y =>, A =>).
410 my ($pa,$pb,@ends,$side,$ppu,$e,$v,$tick,$draw,$allwidth);
411 return unless $ctx->{Draw} =~ m/[ARSC]/;
412 $ppu= $psu_ulen/$lenperp;
413 $allwidth= allwidth($minradius);
414 my ($railctr)=($psu_gauge + $psu_raillw)*0.5;
415 my ($tickend)=($allwidth - $psu_ticklen);
416 my ($tickpitch)=($psu_ulen / $psu_ticksperu);
417 my ($sleeperctr)=($psu_ulen*0.5);
418 my ($sleeperend)=($psu_sleeperlen*0.5);
419 print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
423 o(" $psu_thinlw setlinewidth\n");
425 for ($param=$p0; $param<$p1; $param += $ppu) {
426 parametric__o_pt(&$calcfn);
429 parametric__o_pt(&$calcfn);
432 return unless $draw =~ m/[ARS]/;
433 for ($pa= $p0; $pa<$p1; $pa=$pb) {
435 $param= $pa; $ends[0]= @ends ? $ends[1] : &$calcfn;
436 $param= $pb; $ends[1]= &$calcfn;
437 #print DEBUG "pa $pa $ends[0]{X} $ends[0]{Y} $ends[0]{A}\n";
438 #print DEBUG "pb $pb $ends[1]{X} $ends[1]{Y} $ends[1]{A}\n";
439 $e= $pb<=$p1 ? 1.0 : ($p1-$pa)/$ppu;
442 o_path_point(psu_coords(\@ends,0,-$allwidth));
443 o_path_point(psu_coords(\@ends,0,$allwidth));
444 o_path_point(psu_coords(\@ends,$e,$allwidth));
445 o_path_point(psu_coords(\@ends,$e,-$allwidth));
446 o(" closepath clip\n");
447 foreach $side qw(-1 1) {
449 o_line(psu_coords(\@ends,0,$side*$railctr),
450 psu_coords(\@ends,1.5,$side*$railctr),
455 o_line(psu_coords(\@ends,$sleeperctr,-$sleeperend),
456 psu_coords(\@ends,$sleeperctr,+$sleeperend),
461 foreach $side qw(-1 1) {
462 o_line(psu_coords(\@ends,0,$side*$allwidth),
463 psu_coords(\@ends,1.5,$side*$allwidth),
465 for ($tick=0; $tick<1.5; $tick+=$tickpitch/$psu_ulen) {
466 o_line(psu_coords(\@ends,$tick,$side*$allwidth),
467 psu_coords(\@ends,$tick,$side*$tickend),
477 my ($to, $ctr,$from, $radius,$delta) = @_;
478 # does parametric_segment to draw an arc centred on $ctr
479 # ($ctr->{A} ignored)
480 # from $from with radius $radius (this must be consistent!)
481 # and directionally-subtending an angle $delta.
482 # sets $to->... to be the other end, and returns $to
484 $to->{A}= $beta= $from->{A} + $delta;
485 $to->{X}= $ctr->{X} - $radius * sin($beta);
486 $to->{Y}= $ctr->{Y} + $radius * cos($beta);
487 return if abs($delta*$radius) < 1E-9;
488 parametric_segment(0.0,1.0, abs($radius*$delta), $radius, sub {
489 my ($beta) = $from->{A} + $delta * $param;
490 return { X => $ctr->{X} - $radius * sin($beta),
491 Y => $ctr->{Y} + $radius * cos($beta),
497 my ($from,$to,$how,$minradius);
498 $from= can(\&cva_idex);
499 $to= can(\&cva_idex);
500 $minradius= can(\&cva_len);
501 my (@paths,@solkinds);
503 my ($sigma,$distfact, $theta,$phi, $a,$b,$c,$d, $m,$r, $radius);
504 my ($cvec,$cfrom,$cto,$midpt, $delta1,$delta2, $path,$reverse);
505 $sigma= ev_bearing($from,$to);
506 $distfact= v_dist($from,$to);
507 $theta= 0.5 * $pi - ($from->{A} - $sigma);
508 $phi= 0.5 * $pi - ($to->{A} + $pi - $sigma);
509 $a= 2 * (1 + cos($theta - $phi));
510 $b= 2 * (cos($theta) - cos($phi));
512 $d= sqrt($b*$b - 4*$a*$c);
513 foreach $m (qw(-1 1)) {
515 $r= -0.5 * (-$b + $m*$d) / $a;
516 $radius= -$r * $distfact;
517 next if abs($radius) < $minradius;
518 $cfrom= ev_compose({}, $from, { X=>0, Y=>-$radius, A=>-0.5*$pi });
519 $cto= ev_compose({}, $to, { X=>0, Y=> $radius, A=> 0.5*$pi });
520 $midpt= ev_lincomb({}, $cfrom, $cto, 0.5);
521 $reverse= signum($r);
526 $delta1= ev_bearing($cfrom, $midpt) - $cfrom->{A};
527 $delta2= ev_bearing($cto, $midpt) - $cto->{A};
533 $path= [{ T=>Arc, F=>$from, C=>$cfrom, R=> $radius, D=>$delta1 },
534 { T=>Arc, F=>$to, C=>$cto, R=>-$radius, D=>$delta2 }];
536 push @solkinds, 'twoarcs';
539 my ($path,$segment,$bestpath,$len,$scores,$bestscores,@bends,$sk);
540 my ($crit,$cs,$i,$cmp);
541 foreach $path (@paths) {
542 $sk= shift @solkinds;
543 o("% possible path $sk $path\n");
546 foreach $segment (@$path) {
547 if ($segment->{T} eq Arc) {
548 o("% Arc C ".loc2dbg($segment->{C}).
549 " R $segment->{R} D ".ang2deg($segment->{D})."\n");
550 $len += abs($segment->{R} * $segment->{D});
551 push @bends, signum($segment->{R} * $segment->{D}); # right +ve
553 die "unknown segment $segment->{T}";
556 o("% length $len\n");
558 foreach $crit (@al, 'short') {
559 if ($crit eq 'long') { $cs= $len; }
560 elsif ($crit eq 'short') { $cs= -$len; }
561 elsif ($crit =~ m/^(begin|end|)(left|right)$/) {
562 if ($1 eq 'begin') { $cs= $bends[0]; }
563 elsif ($1 eq 'end') { $cs= $bends[$#bends]; }
564 else { $cs=0; map { $cs += $_ } @bends; }
565 $cs= -$cs if $2 eq 'left';
566 } elsif ($crit =~ m/^(\!?)(twoarcs|arcline|arcsline)$/) {
567 $cs= ($2 eq $sk) != ($1 eq '!');
571 o("% scores @$scores\n");
572 if (defined $bestpath) {
573 for ($i=0,$cmp=0; !$cmp && $i<@$scores; $i++) {
574 $cmp= $scores->[$i] <=> $bestscores->[$i];
579 $bestscores= $scores;
581 die "no solution" unless defined $bestpath;
582 o("% chose path $bestpath @al\n");
584 foreach $segment (@$bestpath) {
585 if ($segment->{T} eq 'Arc') {
586 arc({}, $segment->{C},$segment->{F},$segment->{R},$segment->{D});
588 die "unknown segment";
594 my ($from,$to,$radius,$len,$upto,$ctr,$beta,$ang,$how,$sign_r);
595 $from= can(\&cva_idex);
596 $to= can(\&cva_idnew);
597 printf DEBUG "from $from->{X} $from->{Y} $from->{A}\n";
598 $how= can(cvam_enum(qw(len upto ang uptoang parallel)));
599 if ($how eq 'len') { $len= can(\&cva_len); }
600 elsif ($how =~ m/ang$/) { $ang= can(\&cva_ang); }
601 elsif ($how eq 'parallel' || $how eq 'upto') { $upto= can(\&cva_idex); }
602 $radius= cano(\&cva_len, 'Inf'); # +ve is right hand bend
603 if ($radius eq 'Inf') {
604 # print DEBUG "extend inf $len\n";
605 if ($how eq 'upto') {
606 $len= ($upto->{X} - $from->{X}) * cos($from->{A})
607 + ($upto->{Y} - $from->{Y}) * sin($from->{A});
608 } elsif ($how eq 'len') {
610 die "len of straight spec by angle";
612 printf DEBUG "len $len\n";
613 $to->{X}= $from->{X} + $len * cos($from->{A});
614 $to->{Y}= $from->{Y} + $len * sin($from->{A});
615 $to->{A}= $from->{A};
616 parametric_segment(0.0, 1.0, abs($len), undef, sub {
617 ev_lincomb({}, $from, $to, $param);
620 my ($sign_r, $sign_ang, $ctr, $beta_interval, $beta, $delta);
621 print DEBUG "radius >$radius<\n";
622 $radius *= $ctx->{Trans}{R};
623 $sign_r= signum($radius);
625 $ctr->{X}= $from->{X} + $radius * sin($from->{A});
626 $ctr->{Y}= $from->{Y} - $radius * cos($from->{A});
627 if ($how eq 'upto') {
628 $beta= atan2(-$sign_r * ($upto->{X} - $ctr->{X}),
629 $sign_r * ($upto->{Y} - $ctr->{Y}));
631 } elsif ($how eq 'parallel') {
634 } elsif ($how eq 'uptoang') {
635 $beta= input_absang($ang);
637 } elsif ($how eq 'len') {
638 $sign_ang= signum($len);
639 $beta= $from->{A} - $sign_r * $len / abs($radius);
642 $sign_ang= signum($ang);
643 $beta= $from->{A} - $sign_r * $ang;
646 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
647 $beta += $sign_ang * $sign_r * 4.0 * $pi;
649 $delta= $beta - $from->{A};
650 last if $sign_ang * $sign_r * $delta <= 0;
651 $beta -= $sign_ang * $sign_r * $beta_interval * $pi;
653 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
654 arc($to, ,$ctr,$from, $radius,$delta);
656 printf DEBUG "to $to->{X} $to->{Y} $to->{A}\n";
661 return "$loc->{X} $loc->{Y} ".ang2deg($loc->{A});
664 return $_[0] * 180 / $pi;
666 sub input_absang ($) {
667 return $_[0] * $ctx->{Trans}{R} + $ctx->{Trans}{A};
669 sub input_abscoords ($$) {
671 ($in->{X}, $in->{Y}) = @_;
673 $out= ev_compose({}, $ctx->{Trans}, $in);
674 return ($out->{X}, $out->{Y});
679 Trans => { X => 0.0, Y => 0.0, A => 0.0, R => 1.0 },
687 sub cmd_defobj { cmd__defobj(0); }
688 sub cmd_defpart { cmd__defobj(1); }
689 sub cmd__defobj ($) {
692 $id= can(\&cva_idstr);
693 die "nested defobj" if $defobj_save;
694 die "repeated defobj" if exists $objs{$id};
696 $defobj_ispart= $ispart;
699 $ctx->{InDefObj}= $id;
705 $id= $ctx->{InDefObj};
706 die "unmatched enddef" unless defined $id;
707 foreach $bit (qw(CmdLog Loc)) {
708 $objs{$id}{$bit}= $ctx->{$bit};
710 $objs{$id}{Part}= $defobj_ispart;
715 sub cmd__runobj ($) {
719 dv("cmd__runobj $obj_id ",'$ctx',$ctx);
720 foreach $c (@{ $objs{$obj_id}{CmdLog} }) {
722 next if $al[0] eq 'enddef';
727 sub cmd_part { cmd__obj(Part); }
728 sub cmd_obj { cmd__obj(1); }
729 sub cmd_objflip { cmd__obj(-1); }
733 my ($obj_id, $ctx_save, $pfx, $actual, $formal_id, $formal, $formcv);
734 my ($part_name, $ctx_inobj, $obj, $id, $newid, $newpt);
736 $part_name= can(\&cva_idstr);
737 $how= (@al && $al[0] =~ s/^\^//) ? -1 : +1;
739 $obj_id= can(\&cva_idstr);
740 if (defined $part_name) {
741 $formal_id= can(\&cva_idstr);
742 $actual= cano(\&cva_idex, undef);
743 if (!defined $actual) {
744 $actual= cva_idex("${part_name}_${formal_id}");
747 $actual= can(\&cva_idex);
748 $formal_id= can(\&cva_idstr);
750 $obj= $objs{$obj_id};
751 dv("cmd__obj ",'$obj',$obj);
752 die "unknown obj $obj_id" unless $obj;
753 $formal= $obj->{Loc}{$formal_id};
754 die "unknown formal $formal_id" unless $formal;
757 $how *= $ctx_save->{Trans}{R};
758 $ctx->{Trans}{R}= $how;
759 $ctx->{Trans}{A}= $actual->{A} - $formal->{A}/$how;
760 $formcv= ev_compose({}, $ctx->{Trans},$formal);
761 $ctx->{Trans}{X}= $actual->{X} - $formcv->{X};
762 $ctx->{Trans}{Y}= $actual->{Y} - $formcv->{Y};
763 if (defined $part_name) {
764 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${part_name}:";
766 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}::";
768 $ctx->{Draw}= $ctx_save->{Draw};
770 $ctx->{Draw} =~ s/[LMN]//g;
771 $ctx->{Draw} =~ s/O/MNO/;
773 $ctx->{Draw} =~ s/[LM]//g;
774 $ctx->{Draw} =~ s/N/MN/;
776 cmd__runobj($obj_id);
777 if (defined $part_name) {
778 $pfx= $part_name.'_';
780 if (@al && $al[0] eq '=') {
783 $pfx= cano(\&cva_idstr,undef);
789 foreach $id (keys %{ $ctx_inobj->{Loc} }) {
790 next if $id eq $formal_id;
792 next if exists $ctx_save->{Loc}{$newid};
793 $newpt= cva_idnew($newid);
794 %$newpt= %{ $ctx_inobj->{Loc}{$id} };
797 if (defined $part_name) {
798 my ($formalr_id, $actualr_id, $formalr, $actualr);
800 die "part results come in pairs\n" unless @al>=2;
801 ($formalr_id, $actualr_id, @al) = @al;
802 if ($actualr_id =~ s/^\-//) {
803 $formalr_id= "-$formalr_id";
804 $formalr_id =~ s/^\-\-//;
807 local ($ctx) = $ctx_inobj;
808 $formalr= cva_idex($formalr_id);
810 $actualr= cva_idnew($actualr_id);
811 %$actualr= %$formalr;
818 dv("cmd__do $ctx @al ",'$ctx',$ctx);
819 $cmd= can(\&cva_cmd);
820 my ($lm,$id,$loc,$io,$ad);
821 $io= defined $ctx->{InDefObj} ? "$ctx->{InDefObj}!" : $ctx->{InRunObj};
822 o("%L cmd $io $cmd @al\n");
823 $ctx->{LocsMade}= [ ];
828 die "too many args" if @al;
829 foreach $lm (@{ $ctx->{LocsMade} }) {
831 $loc= $ctx->{Loc}{$id};
832 $loc->{A} += $pi if $lm->{Neg};
833 $ad= ang2deg($loc->{A});
834 ol("%L point $io$id ".loc2dbg($loc)." ($lm->{Neg})\n");
835 if ($ctx->{Draw} =~ m/[LM]/) {
837 " $loc->{X} $loc->{Y} translate $ad rotate\n");
838 if ($ctx->{Draw} =~ m/M/) {
839 ol(" 0 $allwidthmin newpath moveto\n".
840 " 0 -$allwidthmin lineto\n".
841 " $lmu_marklw setlinewidth stroke\n");
843 if ($ctx->{Draw} =~ m/L/) {
844 ol(" /s ($id) def\n".
846 " /sx5 s stringwidth pop\n".
847 " 0.5 mul $lmu_txtboxpadx add def\n".
848 " -90 rotate 0 $lmu_txtboxoff translate newpath\n".
849 " sx5 neg 0 moveto\n".
850 " sx5 neg $lmu_txtboxh lineto\n".
851 " sx5 $lmu_txtboxh lineto\n".
852 " sx5 0 lineto closepath\n".
853 " gsave 1 setgray fill grestore\n".
854 " $lmu_txtboxlw setlinewidth stroke\n".
855 " sx5 neg $lmu_txtboxpadx add $lmu_txtboxtxty\n".
863 sub cmd_showlibrary {
864 my ($obj_id, $y, $x, $ctx_save, $width, $height);
865 my ($max_x, $min_x, $max_y, $min_y, $nxty, $obj, $loc, $pat, $got, $glob);
867 $x=$olu_left; $y=$olu_bottom; undef $nxty;
869 foreach $obj_id (sort keys %objs) {
871 foreach $glob (@al) {
873 $got= !($pat =~ s/^\!//);
874 die "bad pat" if $pat =~ m/[^0-9a-zA-Z_*?]/;
875 $pat =~ s/\*/\.*/g; $pat =~ s/\?/./g;
876 last if $obj_id =~ m/^$pat$/;
880 $obj= $objs{$obj_id};
881 next unless $obj->{Part};
882 ($min_x, $max_x, $min_y, $max_y) = bbox($obj->{Loc});
886 $width= $max_x - $min_x;
887 $height= $max_y - $min_y;
888 if ($width < $height) {
890 $ctx->{Trans}{X}= $x - $min_x;
891 $ctx->{Trans}{Y}= $y - $min_y + $olu_textheight;
893 ($width,$height)=($height,$width);
894 $ctx->{Trans}{A}= 0.5 * $pi;
895 $ctx->{Trans}{X}= $x + $max_y;
896 $ctx->{Trans}{Y}= $y - $min_x + $olu_textheight;
898 $adj= length($obj_id) * $olu_textallowperc - $width;
901 $ctx->{Trans}{X} += 0.5 * $adj;
902 if ($x + $width > $olu_right && defined $nxty) {
906 } elsif ($y + $height > $olu_top && $y > $olu_bottom) {
908 $x= $olu_left; $y= $olu_bottom;
915 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}//";
916 $ctx->{Draw}= $ctx_save->{Draw};
917 cmd__runobj($obj_id);
919 " /s ($obj_id) def\n".
921 ($x + 0.5*$width)." ".($y - $olu_textheight)." moveto\n".
922 " s stringwidth pop -0.5 mul 0 rmoveto\n".
923 " s show grestore\n");
924 $x += $width + $olu_gap_x;
925 upd_max(\$nxty, $y + $height + $olu_gap_y + $olu_textheight);
937 " /lf /Courier-New findfont $lmu_marktpt scalefont def\n".
938 " 615 0 translate 90 rotate\n".
939 " $ptscale $ptscale scale\n"
944 our $drawers= 'arsclmno';
945 our %chdraw_emap= qw(A ARSc
957 $ctx->{Draw}= 'RLMN';
961 while (@ARGV && $ARGV[0] =~ m/^\-/) {
962 last if $ARGV[0] eq '-';
967 if (s/^D(\d+)//) { $debug= $1; }
968 elsif (s/^D//) { $debug++; }
969 elsif (s/^q//) { $quiet=1; }
970 elsif (s/^([Ee])([a-zA-Z]+)//) {
973 foreach $c (split //, $2) {
975 die "bad -e option $c" unless defined $chdraw_emap{$c};
976 @c= split //, $chdraw_emap{$c};
978 die "bad -E option $c" unless $c =~ m/[$drawers]/i;
982 $ctx->{Draw} =~ s/$cc//ig;
983 $ctx->{Draw} .= $cc if $cc =~ m/[A-Z]/;
987 die "unknown option -$_";
992 open DEBUG, ($debug ? ">&2" : ">/dev/null") or die $!;
996 select(STDOUT); $|=1;
1001 chomp; s/^\s+//; s/\s+$//;
1002 @al= split /\s+/, $_;
1004 print DEBUG "=== @al\n";
1005 last if $al[0] eq 'eof';
1006 push @{ $ctx->{CmdLog} }, [ @al ] if exists $ctx->{CmdLog};
1013 my ($min_x, $max_x, $min_y, $max_y) = bbox($ctx->{Loc});
1015 if (defined $min_x) {
1016 $bboxstr= sprintf("width %.2d (%.2d..%2.d)\n".
1017 "height %.2d (%.2d..%2.d)\n",
1018 $max_x - $min_x, $min_x, $max_x,
1019 $max_y - $min_y, $min_y, $max_y);
1021 $bboxstr= "no locs, no bbox\n";
1023 if (!$quiet) { print STDERR $bboxstr; }
1024 $bboxstr =~ s/^/\%L bbox /mg;
1025 print $bboxstr or die $!;