#!/usr/bin/perl -w # filename: makeiv.pl # author: Marvin Simkin # date: 2002-10-21 # purpose: make VRML 1 ".iv" files from instructions # syntax: makeiv.pl < pic.draw > pic.iv # pic.draw drawing instructions # pic.iv VRML 1.0 drawing # NOTE: # This originally only drew lines but was expanded. # The goal of the data format is to make it possible to describe an object # in a one-line instruction (e.g. draw a line from point A to point B) # and to set defaults that apply to all subsequent objects (e.g. color red). # You can have one "word" per line, or multiple "words" separated by ; # A word describing an object has a colon, such as "line:X1,Y1,Z1,X2,Y2,Z2". # This immediately causes the object to be created with existing colors etc. # Or a word can set a value with an equal sign, such as "color=R G B". # When a word changes a value it applies to all subsequent instructions. # Input data format # color=1 0 0 # linewidth=3 # line:X1,Y1,Z1,X2,Y2,Z2 # line:X1,Y1,Z1,X2,Y2,Z2;line:X1,Y1,Z1,X2,Y2,Z2 # color=0 0 1 # line:X1,Y1,Z1,X2,Y2,Z2 # triangle:X1,Y1,Z1,X2,Y2,Z2,X3,Y3,Z3 # line:X1,Y1,Z1,X2,Y2,Z2 # linewidth=1;line:X1,Y1,Z1,X2,Y2,Z2 # color=0.2 0.2 0.2;line:X1,Y1,Z1,X2,Y2,Z2 # fontname=Times-Roman;fontsize=12;fontjust=CENTER # text:X1,Y1,Z1,whatever you want to say here # ENHANCEMENT: # Allow lines with more than two points. # This would be MUCH more efficient. # e.g. line:X1,Y1,Z1,X2,Y2,Z2,X3,Y3,Z3... # # Modify other programs to output longer lines. # INITIALIZE: my $LineWidth; $LineWidth = 3; my $Color; $Color = '0.2 0.2 0.2'; my $FontName; $FontName = 'Times-Roman'; my $FontSize; $FontSize = 12; my $FontJust; $FontJust = 'CENTER'; print "#Inventor V2.0 ascii\n"; print "\n"; print "# generated by $0\n"; print "\n"; print "\n"; # SUBROUTINES: use strict; sub PlotLine { my $Word; ($Word) = @_; # this "word" had better be X1,Y1,Z1,X2,Y2,Z2 my $X1; my $Y1; my $Z1; my $X2; my $Y2; my $Z2; ($X1, $Y1, $Z1, $X2, $Y2, $Z2) = split (/,/, $Word); # NOTE about BaseColor # specify colors for lines in order plotted # NOTE about IndexedLineSet # think of this as pen instructions # drop pen at point 1, drawto 2, 3... # to pick up pen use -1 # coordIndex [ 0, 1 ] # # THE FOLLOWING VRML 1 INSTRUCTIONS WERE DROPPED # SEEM TO HAVE NO PRACTICAL EFFECT ON WHAT I'M DOING # LightModel { # model BASE_COLOR # } # MaterialBinding { # value PER_PART # } # LightModel { # model PHONG # } # MaterialBinding { # value OVERALL # } print qq| Separator { DrawStyle { lineWidth $LineWidth } Coordinate3 { point [ $X1 $Y1 $Z1, $X2 $Y2 $Z2 ] } BaseColor { rgb [ $Color ] } IndexedLineSet { coordIndex [ 0, 1 ] } } |; # end of print qq| } # end of sub PlotLine sub PlotTriangle { my $Word; ($Word) = @_; # this "word" had better be X1,Y1,Z1,X2,Y2,Z2,X3,Y3,Z3 my $X1; my $Y1; my $Z1; my $X2; my $Y2; my $Z2; my $X3; my $Y3; my $Z3; ($X1, $Y1, $Z1, $X2, $Y2, $Z2, $X3, $Y3, $Z3) = split (/,/, $Word); print qq| Separator { Coordinate3 { point [ $X1 $Y1 $Z1, $X2 $Y2 $Z2, $X3 $Y3 $Z3 ] } BaseColor { rgb [ $Color ] } IndexedFaceSet { coordIndex [ 0, 1, 2 ] } } |; # end of print qq| } # end of sub PlotLine sub PlotText { my $Word; ($Word) = @_; # Expected format: X1,Y1,Z1,whatever you want to say here # Due to the syntax the text cannot contain ; , " or newline my $X1; my $Y1; my $Z1; my $Text; ($X1, $Y1, $Z1, $Text) = split (/,/, $Word); print qq| Separator { Font { name "$FontName" size $FontSize } Transform { translation $X1 $Y1 $Z1 rotation 0 0 1 0 } Text3 { string "$Text" justification $FontJust } } |; # end of print qq| } # end of sub PlotText # ARGUMENTS: # MAINLINE: my $Line; while ($Line = <>) { chomp $Line; # a "word" is anything separated by semicolons my @Word; @Word = split (/;/, $Line); my $Word; foreach $Word (@Word) { # print STDERR "Processing '$Word'\n"; # some words may set variables if ($Word =~ /=/) { # parse VAR=VALUE my $Var; my $Value; ($Var, $Value) = split (/=/, $Word, 2); if ($Var eq 'color') { $Color = $Value; } elsif ($Var eq 'linewidth') { $LineWidth = $Value; } elsif ($Var eq 'fontname') { $FontName = $Value; } elsif ($Var eq 'fontsize') { $FontSize = $Value; } elsif ($Var eq 'fontjust') { $FontJust = $Value; ## } elsif ($Var eq 'x') { ## $X = $Value; } else { print STDERR "Unrecognized variable assignment: '$Word'\n"; } # other words may specify objects } elsif ($Word =~ /:/) { # parse VAR=VALUE my $Var; my $Value; ($Var, $Value) = split (/:/, $Word, 2); if ($Var eq 'line') { &PlotLine ($Value); } elsif ($Var eq 'triangle') { &PlotTriangle ($Value); } elsif ($Var eq 'text') { &PlotText ($Value); } else { print STDERR "Unrecognized object name: '$Word'\n"; } } else { print STDERR "Unrecognized instruction: '$Word'\n"; } } }