#!/usr/bin/perl
# geo2gibi.pl         SOURCE    LC    01/11/21
#
# [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
# CHAMPANEY Laurent  Universite de Versailles St Quentin le 21 / 11 / 01
#
# Transfert de geometrie GEO (GMSH) vers Gibiane
#
#  Usage :
#    geo2gibi.pl [-D dimension] essai
#  
#  lit essai.geo et le converti en essai.dgibi
#
#
# ======================================================================
#
# ======================================================================
# Analyse des arguments
# ======================================================================
use Getopt::Long;
@knownoptions = ("dimension|D=n",
                 "version|V");
GetOptions (@knownoptions) || exit ;
$dimen1 = 2;
if ($opt_dimension){$dimen1=$opt_dimension};
#
if($#ARGV==-1) { sortie_usage(); exit();}
else {
        ($generic , $generic_ext) = ( $ARGV[0] =~ /([_\/\w]*)\.?(\w*)/ ) ;
}
# ======================================================================
# Valeurs par defaut
# ======================================================================
# ======================================================================
# Fichiers
# ======================================================================
$geo_file ="$generic.geo";
$sor_file = "$generic.dgibi";
#
# ======================================================================
# Traitement
# ======================================================================
open (INFIC, $geo_file) || die "Le fichier $geo_file n existe pas\n";
open (OUFIC, ">$sor_file");
print OUFIC "OPTI DIME $dimen1 ELEM TRI3;\n";
while ($line = <INFIC>) {
	chomp $line;
	$line =~ s/\s//g ;
	$traite = 0;
	if ($line =~ /^Point/) {
		$line =~ /^.*\(\s*(\S+)\s*\).*\{\s*(.*)\}.*/ ;
		$point = $1;
		@tab = split (/,/ , $2);
		if ($dimen1 == 3) {
		print OUFIC "DENS $tab[3]; P$point = ($tab[0]) ($tab[1]) ($tab[2]);\n"; 
		}
		else {
		print OUFIC "DENS $tab[3]; P$point = ($tab[0]) ($tab[1]) ;\n"; 
		}
		$traite = 1;
	}
	if ($line =~ /^Line\(/) {
		$line =~ /^.*\(\s*(\S+)\s*\).*\{\s*(.*)\}.*/ ;
		$ligne = $1;
		@tab = split (/,/ , $2);
		$tab[0] = abs $tab[0];
		$tab[1] = abs $tab[1];
		print OUFIC "L$ligne = D P$tab[0] P$tab[1];\n"; 
		$traite = 1;
	}
	if ($line =~ /^Circle/) {
		$line =~ /^.*\(\s*(\S+)\s*\).*\{(.*)\}.*/ ;
		$ligne = $1;
		@tab = split (/,/ , $2);
		$tab[0] = abs $tab[0];
		$tab[1] = abs $tab[1];
		$tab[2] = abs $tab[2];
		print OUFIC "L$ligne = C P$tab[0] P$tab[1] P$tab[2];\n"; 
		$traite = 1;
	}
	if ($line =~ /^LineLoop/) {
		$line =~ /^.*\(\s*(\S+)\s*\).*\{\s*(.*)\}.*/ ;
		$ligne = $1;
		@tab = split (/,/ , $2);
		$nb_bout = @tab;
		print OUFIC "L$ligne = "; 
		$max = 6;
                for ($j = 0 ; $j < $nb_bout ; $j++) {
			if ($tab[$j] < 0) {
				$tab[$j] = abs @tab[$j];
				printf OUFIC "(INVE L$tab[$j]) ";
				$max = $max - 1;
			}
			else {
				printf OUFIC "L$tab[$j] ";
			}
                        if ($j == ($nb_bout - 1)) {printf OUFIC ";\n"}
                        else {
                                printf OUFIC "ET ";
                                $l = $j + 1 - ($max * int(($j + 1) / $max));
                                if ($l == 0) {printf OUFIC "\n      "; $max = 6;};
                        }
                }
		$traite = 1;
	}
	if ($line =~ /^PlaneSurface/) {
		$line =~ /^.*\(\s*(\S+)\s*\).*\{\s*(.*)\}.*/ ;
		$ligne = $1;
		@tab = split (/,/ , $2);
		$nb_bout = @tab;
		print OUFIC "S$ligne = SURF (";
		$max = 5;
                for ($j = 0 ; $j < $nb_bout ; $j++) {
			if ($j > 0) {
				printf OUFIC "(INVE L$tab[$j]) ";
				$max = $max - 1;
			}
			else {
				printf OUFIC "L$tab[$j] ";
			}
                        if ($j == ($nb_bout - 1)) {printf OUFIC ") PLANE;\n"}
                        else {
                                printf OUFIC "ET ";
                                $l = $j + 1 - ($max * int(($j + 1) / $max));
                                if ($l == 0) {printf OUFIC "\n      "; $max = 5;};
                        }
                }
		$traite = 1;
	}
	if ($line =~ /^Ellipsis/) {
		print OUFIC "* Pas implante\n";
		print OUFIC "* $line\n";
		$traite = 1;
	}
	if ($line =~ /^CatmullRom/) {
		print OUFIC "* Pas implante\n";
		print OUFIC "* $line\n";
		$traite = 1;
	}
	if ($line =~ /^BSpline/) {
		print OUFIC "* Pas implante\n";
		print OUFIC "* $line\n";
		$traite = 1;
	}
	if ($line =~ /^RuledSurface/) {
		print OUFIC "* Pas implante\n";
		print OUFIC "* $line\n";
		$traite = 1;
	}
	if ($line =~ /^ExtrudeSurface/) {
		print OUFIC "* Pas implante\n";
		print OUFIC "* $line\n";
		$traite = 1;
	}
	if ($line =~ /^Volume/) {
		print OUFIC "* Pas implante\n";
		print OUFIC "* $line\n";
		$traite = 1;
	}
	if ($line =~ /^SurfaceLoop/) {
		print OUFIC "* Pas implante\n";
		print OUFIC "* $line\n";
		$traite = 1;
	}
	if ($line =~ /^Delete/) {
		$stop = 0;
		while ($stop == 0) {
			$line = <INFIC>;
			if ($line =~ /^}/) {$stop = 1};
		}
		$traite = 1;
	}
	if ($traite == 0) {
		print OUFIC "$line\n";
	}
	
}
close (INFIC);
#
close OUFIC;
#
# ======================================================================
# On sort
# ======================================================================
print "\n";
print "Conversion $geo_file vers $sor_file effectuee\n";
print "\n";
exit;
#
#
# ===========================================================> End <===
#
# ======================================================================
# Subroutines
# ======================================================================
# =======================================================================
#  Sortie par defaut apres erreur
# =======================================================================
#
sub sortie_usage {
        print "  \n";
        print "geo2gibi.pl : convertion de geometrie gmsh vers gibiane\n";
        print "Laurent CHAMPANEY                     Novembre 2001\n";
        print "  \n";
        print "Usage :\n";
        print "    perl geo2dgibi.pl [-D dimension] essai\n";
        print "  \n";
        print "  lit essai.geo et le converti en essai.dgibi\n";
        print "  \n";
	return;
        };

