=head1 NAME

CFX5::Simulation::Configuration::Remeshing::Remesh::MeshMod - Mesh modification module

=head1 SYNOPSIS

Functions for mesh modification step of the remesh process

$Id$

=cut

package CFX5::Simulation::Configuration::Remeshing::Remesh::MeshMod ;

use strict ;

use vars qw($CVSVersion);
$CVSVersion='$Id$ ';

use Exporter ; 
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
@ISA=qw(Exporter);
@EXPORT_OK=qw(MeshMod);
%EXPORT_TAGS = (
  'all' => [qw(MeshMod)],  
);

use CFX5::Output qw(:all error_text);
use CFX5::Output::Utils 'wraptext';
use CFX5::Config qw(%Config) ;
use CFX5::PkgLoader qw(cfx5_require);
use CFX5::PathManip qw(join_file dirname);
use CFX5::Commands qw(do_cmd parse_rv);
use CFX5::Config qw(setconf %Config export_conf prepend_to_libpath);
use CFX5::DefaultConfig qw(ld_path_var);

#
################################################################################
# MeshMod - High level MeshMod routine
################################################################################
#
sub MeshMod {
  my ($self) = @_ ; 
#
#===============================================================================
# Setup
#===============================================================================
#
# Connect to text output file
#
  my $outfile=$self->Get("OutObj");
  unless (defined $outfile) {
    _error "Remeshing: Could not connect to out file" ;
    return 0;
  }
#
#===============================================================================
# Gather required information from the remesh object
#===============================================================================
#
  my $rundir = $self->Get("RunDir") ; 
  if (!defined $rundir) {
    _error "Preprocess: Run directory not available" ;
    return 0; 
  }
  my $cclapi=$self->Get("CCLAPI");
  unless (defined $cclapi) {
    _error "GeometryMod: Could not connect to CCL" ;
    return 0; 
  }
  my $CCLPath = $self->Get("CCLPath");
  unless (defined $CCLPath) {
    _error "GeometryMod: CCLPath not available" ;
    return 0; 
  }
#
#===============================================================================
#  ICEM CFD Replay
#===============================================================================
#   
  if ($self->Get("Option") eq "ICEM CFD Replay") {
#
#----------------------------------------------------------------------- 
# Setup
#----------------------------------------------------------------------- 
#
    my $remesh_rpl = join_file($Config{CFX_DATA_DIR},'Remeshing',
                               'icemcfd_Remesh.rpl') ;

    unless (-e $remesh_rpl) {
      _error "MeshMod: Remesh replay file not found: $remesh_rpl" ;
      return 0; 
    }

    # Look for the icem command (unix first, then windows, then error...)
    my $cmd = $self->Get("Parent")->icem_exe() ; 
    my $cmdLine = $cmd." -batch -script ".$remesh_rpl ;

    # Add Icem lib directory to seach path, ensuring it uses its own libs
    my $ld_path_cfx = $Config{$Config{ld_path_var}};
    prepend_to_libpath(join_file($Config{ICEMINSTALLROOT},'lib'));

    my @text = wraptext(63,$cmdLine) ;
    $outfile->print("\n  Executing remesh command:");
    foreach my $line (@text) {
      $outfile->print("     $line"); 
    }

    $rundir->enter() ;
    my $rv = CFX5::Simulation::Configuration::Remeshing::run_and_redirect_Remeshing(
               $self->Get("RemeshOutFile"),
               $cmd,'-batch','-script',$remesh_rpl );
    unless ($rv->is_success()) {
      _error($rv->errstr("MeshMod: Call to ICEM replay mesh modifications failed."));
      $rundir->leave() ;
      return 0; 
    }
    $rundir->leave() ;

    # Revert search path change  
    $Config{$Config{ld_path_var}}=$ld_path_cfx;
    export_conf($Config{ld_path_var});

  }
#
#===============================================================================
#  User Defined
#===============================================================================
#   
  elsif ($self->Get("Option") eq "User Defined") {
#
#----------------------------------------------------------------------- 
# Setup
#----------------------------------------------------------------------- 
#
    my $option ;
    my $cmdLine ;
    
    $cclapi->getStringValue("$CCLPath/External Command",\$cmdLine) ;
    
    unless (defined $cmdLine) {
      _error "MeshMod: External command not found." ;
      return 0; 
    }
    my @text = wraptext(63,$cmdLine) ;
    $outfile->print("\n  Executing remesh command:");
    foreach my $line (@text) {
      $outfile->print("     $line"); 
    }

    my @cmdArgs = CFX5::Commands::split_cmd($cmdLine); 
    my $cmd = shift(@cmdArgs); 

    $rundir->enter() ;
    my $rv = CFX5::Simulation::Configuration::Remeshing::run_and_redirect_Remeshing(
               $self->Get("RemeshOutFile"),
               $cmd,@cmdArgs);
    unless ($rv->is_success()) {
      _error($rv->errstr("MeshMod: Call to user defined mesh modifications failed."));
      return 0; 
    }
    $rundir->leave() ;
  }
#
#===============================================================================
#  Unhandled Option
#===============================================================================
#   
  else {
    _error "MeshMod: Invalid Remesh Option found" ;
    return 0 ; 
  }
  1 ;
}

1 ;


