#! /bin/sh
######################################
##### Model Transformation Tools #####
######################################
# Bourne shell script: rbg_fig2m
# Raw bond-graph conversion from fig to matlab
# P.J.Gawthrop May 1996
# Modified June 1996 to handle xfig version 3
# Copyright (c) P.J.Gawthrop, 1996.
###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.14 2005/08/05 15:35:17 gawthrop
## Fixed _ in ports
##
## Revision 1.13 2005/03/15 15:03:55 gawthrop
## Now works for Hierarchical systems
##
## Revision 1.12 2005/03/15 12:49:28 gawthrop
## Handle descriptive (inc maths) text as well
##
## Revision 1.11 2005/03/15 12:03:48 gawthrop
## New labg.fig rep - pretty LaTeX figures.
##
## Revision 1.10 2002/04/28 18:55:03 geraint
## Fixed [ 549658 ] awk should be gawk.
## Replaced calls to awk with call to gawk.
##
## Revision 1.9 2002/01/11 03:44:19 geraint
## Breaks compounds objects into individual components. Eliminates "incorrect object code error" in cbg.ps when component is not causally complete.
##
## Revision 1.8 2001/10/15 14:25:44 gawthrop
## Now handles white space at the end of component names
## Converts ports lables [1:5] into [1,2,3,4,5]
##
## Revision 1.7 2001/07/24 22:42:59 geraint
## Fixes problem when lbl.txt does not end with newline.
##
## Revision 1.6 2001/05/24 07:48:17 gawthrop
## Include artwork in the cbg.fig file
##
## Revision 1.5 2001/03/23 14:56:21 gawthrop
## Now puts space after header fields + write _port.fig
##
## Revision 1.4 2000/09/14 08:41:51 peterg
## *** empty log message ***
##
## Revision 1.3 1997/03/19 09:50:24 peterg
## Now creates the sabg (stripped abg) file.
##
# Revision 1.2 1996/08/30 10:17:41 peter
# New path for awk script.
#
## Revision 1.1 1996/08/05 20:13:33 peter
## Initial revision
##
###############################################################
cmp2latex() {
gawk '{
##
## Zap trailing \001
A = substr($0,1,length($0)-4);
## Find before and after:
N=split(A,B,":");
## Split into fields
M=split(B[1],C);
## Dont modify port labels (except for _ -> \_)
if(index(C[M],"[")==1){
for (i=1;i<M;i++){
if (C[i]=="_")
C[i] = "\\\\_";
printf("%s ",C[i]);
}
printf("%s\\001\n",C[M]);
}
else{
## Set special flag
FlagCol = 9
C[FlagCol] = 6;
## Write out modified version
for (i=1;i<M;i++)
printf("%s ",C[i]);
if (N==1)
printf("\\\\B{%s}\\001\n",C[M]);
if (N==2)
if(index(B[2],"[")==1){
gsub(/_/,"\\\\_",B[2]);
printf("\\\\B{%s}:%s\\001\n",C[M],B[2]);
}
else
printf("\\\\BB{%s}{%s}\\001\n",C[M],B[2]);
}
}'
}
art2latex() {
gawk '{
## Is it text?
is_text="4";
if ($1!=is_text)
print $0
else
{
## Zap trailing \001
B = substr($0,1,length($0)-4);
## Split into fields
M=split(B,C);
## Set special flag
FlagCol = 9
C[FlagCol] = 6;
## Write out modified version
N = 14;
for (i=1;i<N;i++)
printf("%s ",C[i]);
for (i=N;i<=M;i++) {
if ( (index(C[i],"_")>0)&&(index(C[i],"$")==0) )
C[i] = sprintf("$%s$", C[i]);
printf(" %s",C[i]);
}
printf("\\001\n");
}
}'
}
# Inform user
echo "Creating $1_rbg.m"
echo "Creating $1_cmp.m"
echo "Creating $1_head.fig"
echo "Creating $1_fig.fig"
echo "Creating $1_cmp.fig"
echo "Creating $1_port.fig"
echo "Creating $1_sabg.fig"
echo "Creating $1_art.fig"
echo "Creating $1_labg.fig"
# Remove some files that I'm going to append to.
rm -f $1_fig.fig
rm -f $1_head.fig
rm -f $1_cmp.fig
rm -f $1_port.fig
rm -f $1_bnd.fig
rm -f $1_art.fig
# Create blank files
touch $1_fig.fig
touch $1_head.fig
touch $1_cmp.fig
touch $1_port.fig
touch $1_bnd.fig
touch $1_art.fig
# Deletes trailing white space in component names
# and break compound objects
sed 's/[ ]*\\001/\\001/' < $1_abg.fig |\
grep -v "^6 [0-9 ]*" | grep -v "^-6$" >$1_abg.mtt1
# Replace [1:5] with [1,2,3,4,5]
gawk '{
if (match($14, /\[1:[0-9]*\]/)){
range = substr($14,2,length($14)-6);
split(range,a,":");
str = "1";
for (i=2;i<=a[2];i++) str = sprintf("%s,%i", str, i);
sub(/1:[0-9]*/, str, $14);
}
print $0
}' < $1_abg.mtt1 > $1_abg.mtt2
# The following horrible sed thing is to replace tab by ----
# this is because I can't get sed to recognise tabs even if FS=" ".
echo "" > $1_abg.mtt3
sed 's/ /---- /'<$1_abg.mtt2 >> $1_abg.mtt3
# This is the main transformation using awk
cat $1_lbl.txt $1_abg.mtt3 | gawk -f $MTTPATH/trans/awk/rbg_fig2m.awk $1 no_alpha_sort=$2
rm -f $1_abg.mtt
#Create the stripped abg file
cat $1_head.fig $1_bnd.fig $1_cmp.fig> $1_sabg.fig
#Create the latexed abg file
##latex_tidy $1_art.fig
art2latex< $1_art.fig >$1_lart.fig
cmp2latex<$1_cmp.fig >$1_lcmp.fig
cat $1_head.fig $1_bnd.fig $1_lcmp.fig | sed 's/---- / /' > $1_labg.fig
strip_comments < $1_lart.fig | sed 's/---- / /' >> $1_labg.fig