import sys
import clr
import os

import System
from System import Reflection


#import chart


import shutil


#-------------------
#  Global Variables
#-------------------
global ExtensionDir


ExtensionDir = ExtAPI.ExtensionManager.CurrentExtension.InstallDir
sys.path.append(ExtensionDir)
ExtAPI.Log.WriteMessage(ExtensionDir)


clr.AddReferenceToFileAndPath(System.IO.Path.Combine(ExtensionDir,'LSDynaSolverExtension.dll'))
clr.AddReferenceToFileAndPath(System.IO.Path.Combine(ExtensionDir,'ACTLSDynaReader.dll'))
clr.AddReferenceToFileAndPath(System.IO.Path.Combine(ExtensionDir,'LSDYNAReader.dll'))
clr.AddReferenceToFileAndPath(System.IO.Path.Combine(ExtensionDir,'MechanicalTools.dll'))


import LSDynaSolverExtension
import MechanicalTools

from LSDynaSolverExtension import InterfaceController
from LSDynaSolverExtension import SolverController
from LSDynaSolverExtension import MenuUtils
from MechanicalTools import EventObserver 

from ACTLSDynaReader  import DynaReaderWrapperClass
from ACTLSDynaReader  import BinoutReaderWrapper
from LSDYNAReader import InterfaceResultReader
from LSDYNAReader import ResultReader

LSDynaSolverExtension.SolverController.ReaderName = "DynaReaderWrapperClass"
#LSDynaSolverExtension.SolverController.ReaderName = "ResultReader"




#-------------------
#  EXTENSION INTERFACE CALLBACKS
#-------------------

def init(context):
    global vals
    ExtAPI.Log.WriteMessage("ANSYS LS-DYNA")
    
    #System.AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve

def initproject(context):
    instance = EventObserver.Instance

def terminateproject(context):
    instance = EventObserver.Instance
    instance.Dispose()

    
def CurrentDomain_AssemblyResolve(sender,args):
    assName = System.Reflection.AssemblyName(args.Name).Name + ".dll"
    extensionPath = ExtAPI.ExtensionManager.CurrentExtension.InstallDir
    fullPath = System.IO.Path.Combine(extensionPath, assName)
    
    if (System.IO.File.Exists(fullPath)):
        try:
            return System.Reflection.Assembly.LoadFile(fullPath);
        except:
            return None
    return None
    
def OnactiveObjectChange(obj,location):
    InterfaceController.OnactiveObjectChange(obj,location,ExtAPI)


def Save(folder):
    SolverController.SaveModels(ExtAPI,folder)

def OnPostStarted(analysis):
    SolverController.OnPostStarted(analysis)
def OnPostFinished(analysis):
    SolverController.OnPostFinished(ExtAPI ,analysis)
def Load(folder):
    global solbystep
    if folder==None:
        return
    fm = System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
    try:
        stream = System.IO.StreamReader(folder+r"\sol.res")
    except:
        return
    try:
        solbystep = fm.Deserialize(stream.BaseStream)
    finally:
        stream.Close()

#-------------------
#  CAN ADD METHODS
#-------------------


def canAddDynamicRelaxation(analysis,name):
    return LSDynaSolverExtension.DynamicRelaxationController.CanAdd(analysis,name)

def canAddExplicitBodies(model,name):
    return LSDynaSolverExtension.ExplicitRigidBodiesController.CanAdd(model,name)

def canAddTimeStepControl(analysis,name):
    return LSDynaSolverExtension.TimeStepController.CanAdd(analysis,name)
    
def CanAdd(analysis,objName):
    return InterfaceController.CanAdd(analysis,objName)     

def canAddEulerianDomain(analysis,name):
    return LSDynaSolverExtension.EulerDomainController.CanAdd(analysis,name)


def canAddEulerianReferenceFrame(analysis,name):
    return LSDynaSolverExtension.EulerLagrangeReferenceFrameController.CanAdd(analysis,name, ExtAPI)
#-------------------
#  ACTIONS METHODS
#-------------------


def AddGeneralPreload(obj):
    obj.CreateChild("General Preload");
    
def AddBoltPreload(obj):
    obj.CreateChild("Bolt Pretension");
        
def AddReferenceFrameSwitch(obj):
    obj.CreateChild("Reference Frame Switch")

def AddEulerLagrange(obj):
    obj.CreateChild("Euler Lagrange Interface")    

def exportToFileBndout(load):
    controller = load.PropertyByName("Value").Controller
    controller.internalexport(load,controller.prop)    


def exportToFileSpcForc(load):
    controller = load.PropertyByName("Orientation").Controller
    controller.internalexport(load,controller.prop)   

def exportToFileRcForc(load):
    controller = load.PropertyByName("Orientation").Controller
    controller.internalexport(load,controller.prop)    

def exportToFileNodout(load):
    controller = load.PropertyByName("Value").Controller
    controller.internalexport(load,controller.prop)    

def exportToFileMatsum(load):
    controller = load.PropertyByName("Value").Controller
    controller.internalexport(load,controller.prop) 

def exportToFileSwForc(load):
    controller = load.PropertyByName("Value").Controller
    controller.internalexport(load,controller.prop) 


def exportCmd(load):
    return LSDynaSolverExtension.CmdEditor.ExportCmd(load)

def importCmd(load):
    return LSDynaSolverExtension.CmdEditor.ImportCmd(load)

def showFailedMeshIndependent(load):
    return LSDynaSolverExtension.SwForcObjectController.showFailedMeshIndependentsSpotwelds(ExtAPI,load)
    
#-------------------
#  MENU
#-------------------

def OnClickWriteInputFile(analysis):
    return MenuUtils.OnClickWriteInputFile(ExtAPI,analysis)

def OnClickAbout(analysis):
    return MenuUtils.OnClickAbout(analysis)

def OnReadResultFile(analysis):
    return MenuUtils.OnReadResultFile(ExtAPI,analysis)

#-------------------
#  SCOPING CALLBACKS
#-------------------


#  Isvalid

def IsOnRigidBodies(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsOnRigidBodies(load,property,ExtAPI)

def IsOnFlexibleBodies(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsOnFlexibleBodies(load,property,ExtAPI)
    
def IsOnRigidBody(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsOnRigidBody(load,property,ExtAPI)   

def IsOnRigidBody2(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsOnRigidBody2(load,property,ExtAPI)
    
def IsOnFlexibleBody(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsOnFlexibleBody(load,property,ExtAPI)

def IsValidNodout(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsValidNodout(load,property,ExtAPI)

def IsValidMatsum(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsValidMatsum(load,property)

def IsValidRcForc(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsValidRcForc(load,property)


def IsValidAdditionalNodes(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsValidAdditionalNodes(load,property,ExtAPI)
    
def IsOnShellBodies(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsOnShellBodies(load,property,ExtAPI)

def IsOnBeamBodies(load,property):
	return LSDynaSolverExtension.Scoping.IsValid.IsOnBeamBodies(load,property,ExtAPI)

def IsOnSingleRigidBody(load,property):
	return LSDynaSolverExtension.Scoping.IsValid.IsOnSingleRigidBody(load,property,ExtAPI)

def IsOnEulerBodies(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsOnEulerBodies(load,property,ExtAPI)
    
def IsOnLagrangeBodies(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsOnLagrangeBodies(load,property,ExtAPI)

def IsOnSingleNode(load,property):
    return LSDynaSolverExtension.Scoping.IsValid.IsOnSingleNode(load,property,ExtAPI)

#  apply

def SetBodyName(load,property):
    return LSDynaSolverExtension.Scoping.OnApply.SetBodyName(load,property)

def SetBodyStiffness(load,property):
    return LSDynaSolverExtension.StiffnessSwitchController.SetBodyStiffness(load,property,ExtAPI)

def ShowAdditionalNodes(load,property):
    return LSDynaSolverExtension.Scoping.OnApply.ShowAdditionalNodes(load,property,ExtAPI)
    
def ShowSlaveBody(load,property):
    return LSDynaSolverExtension.Scoping.OnApply.ShowSlaveBody(load,property,ExtAPI)

def ValidateDeformableSwitch(load,property):
    return LSDynaSolverExtension.DeformableSwitchController.ValidateChilds(load)



    

#def DoReadOnlyTimeStepping(solver,property):
#    return WBLSDynaModel.DoReadOnlyTimeStepping(solver,property)

def OnShowContactProperty(load):
    return  LSDynaSolverExtension.ContactProperty.OnShowContactProperty(load,ExtAPI)
    

def getsolvecommands(load,stream):
    return load.Controller.getsolvecommandsimp(load,stream)
    
def getcommands(load,solverData,stream):
    return load.Controller.getcommands(load,solverData,stream)

    
    



    

