! ******************************
! $Revision: 145002 $
! $Date: 2009-11-13 11:20:27 -0500 (Fri, 13 Nov 2009) $
! ******************************
! macro MTto_ans (_envloc)

! *** primary function: transfer MT information to ANSYS and switch on full GUI
! ***
! *** Notice - This file contains ANSYS Confidential information ***

!  input arguments:
!   _envloc    (arg1)    (int,sc)      the environment to take to ANSYS

!  output arguments:
!   _success             (int,sc)      =0 no success, =1 success  

!  this macro transfers the information contained in the internal array parameters
!  to ANSYS FE quantities and cleans up after intself:
!   1. saves the current MTB data to jobnmae.mtdb
!   2. deletes all loads
!   3. transfers load array parameters to the FE model
!   4. deletes all internal parameters
!   5. resets the KEYWORDS
!   6. turns on the side menu, input window, and abbrevation window

/nopr
_SUCCESS=1

!  debug
*if,_DEBUG,gt,0,then
!{
   /gopr
   *vwrite,arg1
   (' === MTto_ans    envloc =',f3.0)
   *if,_DEBUG,lt,3,then
   !{
      /nopr
   !}
   *endif
!}
*endif

_SUCCESS=1

_envloc=arg1

! save the environment name
xname1=_ENVNAM(_envloc,1)
xname2=_ENVNAM(_envloc,2)
xname3=_ENVNAM(_envloc,3)
xname4=_ENVNAM(_envloc,4)

! save the current MTB data to jobname.mtdb
save,,mtdb

! The solution assigns files to the environment name change back
! to the jobname for the run
/assign,rst
/assign,rth

! set ENVDP in case of re-entry to MTB
*do,_i,1,_NUMENV
   _ENVDP(_i)=0
*enddo
_ENVDP(_envloc)=2
_CURENV=_envloc

! check if a model exists!
*if,kpinqr(0,12),gt,0,then ! {

   ! if contact, reset SOLCCONTROL to pick up correct defaults in lsclear
   *if,_ncpairs,gt,0,then
      /solu
      solcontrol,on
      fini
   *endif

   ! delete all existing loads
   /prep7
   lsclear,all

   ! transfer load arrays
   *if,ndinqr(0,12),gt,0,then ! {
      ! transfer to FE model
      mttranbc,2
      ! restore any temperature loads
      *get,_strucxx,active,,keyw,,PR_STRUC
      *get,_modalxx,active,,keyw,,MODAL
      *if,_strucxx,eq,1,then
         *if,_modalxx,eq,0,then
            _antype=1          ! structural
         *else
            _antype=2          ! modal
         *endif
      *else
         _antype=3             ! thermal
      *endif
      *if,_antype,eq,1,then
         *if,_TEMPFIL(_CURENV,1),ne,' ',then
            *do,_i,1,10
               _n%_i%=_TEMPRD(_CURENV,_i)
            *enddo
            *cfopen,temp,inp
            *vwrite,_n1,_n2,_n3,_n4,_n5,_n6,_n7,_n8,_n9,_n10
            ('LDREAD,TEMP,,,,,',10a)
            *cfclos
            /inp,temp,inp
            /del,temp,inp
            *do,_i,5,10
               *del,_n%_i%
            *enddo
         *endif
      *endif
      ! create and transfer contact (if no previous solution)
      *if,_NCPAIRS,gt,0,then
         *if,etyiqr(170,1),eq,0,then
            cm,_curarea,area
            *do,_ncont,1,_NCPAIRS
                cmsel,s,CONT_%_ncont%
                _aid1=arnext(0)
                _aid2=arnext(_aid1)
                cmsel,s,_curarea
                _cttype=_CTPROP(_ncont)
                *if,_cttype,eq,0,then
                   _cttype=_CTPDEF
                *endif
                MTmkcont,_aid1,_aid2,_cttype,_ncont
            *enddo
            *del,_ncont
            *del,_aid1
            *del,_aid2
            cmsel,s,_curarea
            cmdele,_curarea
         *endif
      *endif
      fini
   *else ! } {
      ! transfer to SOLID model that which we can...
      mttranbc,1
   *endif ! }

*endif ! }

! delete all MTB parameters
!*del,,_PRM              ! keep for now (allow usage of MTB load, solve, etc.)
!
FINISH                   ! The following commands need to be run at BEGIN level
                         ! especially the /config commands
! delete components used in MTB picking operations
CMDEL,_CRKEYP
CMDEL,_CRLINE
CMDEL,_CRAREA
CMDEL,_CRVOLU
CMDEL,_MTKSEL
CMDEL,_MTLSEL
CMDEL,_MTASEL
CMDEL,_MTVSEL

! reset the keywords
!KEYW,MECHTOOL,0
KEYW,PR_SGUI,0
!KEYW,PR_SGVOF,0             ! no "verify" prompting!

! reset graphics to default (except view)
!   moved to mainWin.eui (for case when use Utility Menu to leave)

/output,note,txt
*if,kpinqr(0,12),gt,0,then ! {
   ! get the jobname which can be 32 characters long
   ! these cannot be brought into an array because *vwrite will not work
   *get,_file1,active,,jobnam,,start,1
   *get,_file2,active,,jobnam,,start,9
   *get,_file3,active,,jobnam,,start,17
   *get,_file4,active,,jobnam,,start,25
   _filext='.mtdb'
   *do,i,1,4 ! {
      _key%i%=0.0
      _len=strleng(_file%i%)
      *if,_len,le,3,then ! {
         _key5=0.0
      *else ! } {
         _key5=1.0
      *endif ! }
      *if,_len,lt,8,then ! {
         ! append the extension subsequent passes will have an extension of
         ! blank but we need to loop to get the keys correct.
         _file%i%=strfill(_file%i%,_filext,_len+1)
         _filext=strsub(_filext,9-_len,8)
         *if,strleng(_file%i%),eq,0,then ! {
            _key%i%=1.0
         *endif ! }
      *endif ! }
   *enddo ! }

   *if,ndinqr(0,12),gt,0,then !{
      *vwrite,xname1,xname2,xname3,xname4
('The model and loads for the environment ',4a)
      *vwrite
('have been transferred to fully functional ANSYS. Note that some loads have')
      *vwrite
('been applied to the nodes and elements of the model and are not associated')
      *vwrite
('with the solid model.  Any subsequent load applications to solid model')
      *vwrite
('entities may overwrite these loads. Please verify all loads by transferring')
      *vwrite
('any additional solid model loads to the nodes and elements and reviewing')
      *vwrite
('them before solving')
      *vwrite
('(Main Menu > Solution > -Loads- Operate > -Transfer to FE- All Solid Lds).')

      *if,_key2,eq,1.0,then ! {
      *vwrite,_file1
(/'Note, the database file ',a,' has been created. You may use this')
      *elseif,_key3,eq,1.0,then ! } {
      *vwrite,_file1,_file2
(/'Note, the database file ',a,a,' has been created. You may use this')
      *elseif,_key4,eq,1.0,then ! } {
      *vwrite,_file1,_file2,_file3
(/'Note, the database file ',a,a,a,' has been created. You may use this')
      *elseif,_key5,eq,1.0,then ! } {
      *vwrite,_file1,_file2,_file3,_file4,_filext
(/'Note, the database file ',a,a,a,a,a,'has been created. You may use this')
      *else ! } {
      *vwrite,_file1,_file2,_file3,_file4
(/'Note, the database file ',a,a,a,a,' has been created. You may use this')
      *endif ! }

      *vwrite
('file for resuming your Mechanical Toolbar session.')
   *else ! } {

      *vwrite
('The solid model has not been meshed yet. Not all loads that may have been specified')
      *vwrite
('can be taken into ANSYS. If you wish to take all loadings into ANSYS, resume')

      *if,_key2,eq,1.0,then ! {
      *vwrite,_file1
(a,' and mesh the model in the Mechanical Toolbar.')
      *elseif,_key3,eq,1.0,then ! } {
      *vwrite,_file1,_file2
(a,a,' and mesh the model in the Mechanical Toolbar.')
      *elseif,_key4,eq,1.0,then ! } {
      *vwrite,_file1,_file2,_file3
(a,a,a,' and mesh the model in the Mechanical Toolbar.')
      *elseif,_key5,eq,1.0,then ! } {
      *vwrite,_file1,_file2,_file3,_file4,_filext
(a,a,a,a,a,'and mesh the model in the Mechanical Toolbar.')
      *else ! } {
      *vwrite,_file1,_file2,_file3,_file4
(a,a,a,a,' and mesh the model in the Mechanical Toolbar.')
      *endif ! }

   *endif ! }
*else ! } {
   *vwrite
('Note: no model was imported into the Mechanical Toolbar')
*endif !}
/output,term
*uilist,note,txt
/delete,note,txt

*del,xname1
*del,xname2
*del,xname3
*del,xname4
*del,_file
*del,_len
*del,_i
*del,_z1
*del,_antype
*del,_strucxx
*del,_modalxx
*del,_cttype

