General Mechanical

General Mechanical

How to write an external function that can use ANSYS Mechanical language?

    • Ingmar van Dijk
      Subscriber

      Hi all,

      Experienced engineer but notive programmer here. I code directly into the python console in ansys mechanical. I'd like to make some functions I can always import and call to clean up my code.

      However, a standard function doesn't know what to do with ANSYS commands. 

      Can somebody help me write this example:

      The external function is something like:

      def lights():
          ExtAPI.DataModel.Project.Model.Ambient=0.4

      And I call it like this:

      #Import sys for system and imp for importlib. In Python3 it is importlib instead imp.
      import sys
      import imp

      #Set your path where your python files are, for example my test folder for random ansys stuff
      path = r"R:\OF_EN\SUS\_DOC\Tools\FEA\700-FEM-ANSYS-Python\Mech-Functions"

      #Check if path exists. Without check it will always add the path and create a bunch of bullshit
      if path not in sys.path:
          sys.path.append(path)

      #Import your library you want
      import TestFunction as tester
      #import SetLightning as lights

      #IMPORTANT: reload your library. If you dont, you dont see changes in your code
      imp.reload(tester)
      #imp.reload(lights)

      tester.lights()

      This of course doesn't work as the function is trying to perform a function that doesn't do anything at that external location.

      Help on how to fix this is much apprechated!

      Thanks in advance,

      Ingmar

Viewing 0 reply threads
  • You must be logged in to reply to this topic.