{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exporting data using the Engineering Data Service"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Find a list of FEA exporters for a record in a Granta MI database, and export relevant parameter values.\n",
    "\n",
    "This example demonstrates:\n",
    "\n",
    "- Retrieving a list of available exporters\n",
    "- Retrieving a list exporters applicable to a specific record in a Granta MI database\n",
    "- Get the parameters that can be exported for a specific exporter and record combination\n",
    "- Run the FEA exporter on the specific record"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a Granta MI Session\n",
    "\n",
    "Import the GRANTA_MIScriptingToolkit package, and create a connection to a Granta MI server."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import GRANTA_MIScriptingToolkit as gdl\n",
    "\n",
    "session = gdl.GRANTA_MISession('http://localhost/mi_servicelayer', autoLogon=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List the available exporters\n",
    "Retrieve the list of FEA exporters that will export data from the database MI_Training into Ansys Workbench format."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Output of available Exporters for Ansys Workbench\n",
      "458E9A7E-C268-4ED0-9CC1-FF7438521B4F (ANSYS Workbench) - Exports linear, temperature-independent, isotropic data to the Ansys Workbench format\n",
      "CE8DCFA2-B3EE-4545-8D3E-82810FA92AFC (ANSYS Workbench) - Exports linear, temperature-dependent, isotropic data to the Ansys Workbench format\n",
      "4B0B1EA3-8760-43DF-8060-2C79CA471D4C (ANSYS Workbench) - Exports linear, temperature-independent, isotropic with simple failure data to the Ansys Workbench format\n"
     ]
    }
   ],
   "source": [
    "dbKey = \"MI_Training\"\n",
    "request = gdl.GetAvailableExportersRequest(DBKey=dbKey, package=\"ANSYS Workbench\", matchDB=True)\n",
    "\n",
    "print(\"\\nOutput of available Exporters for Ansys Workbench\")\n",
    "response = session.engineeringDataService.GetAvailableExporters(request)\n",
    "\n",
    "for exporter in response.exporters:\n",
    "    print(\"{0} ({1}) - {2}\".format(exporter.name, exporter.package, exporter.description))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get a record by name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 1 record(s)\n"
     ]
    }
   ],
   "source": [
    "req = gdl.RecordNameSearchRequest(\n",
    "    recordName=\"Nickel alloys, Inconel 718, Forging\",\n",
    "    table=gdl.TableReference(DBKey=dbKey, name=\"Design Data\"),\n",
    "    searchShortNames=True\n",
    ")\n",
    "resp = session.searchService.RecordNameSearch(req)\n",
    "print(\"Found {0} record(s)\".format(len(resp.searchResults)))\n",
    "rec = resp.searchResults[0].recordReference\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get exporters for a specific record\n",
    "Use the engineering data service to find valid FEA exporters for Inconel 718."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Output of exporters for Inconel 718\n",
      "71CE1C21-FDEA-4119-B481-81BDC41BD900 (Abaqus 6) - Exports temperature dependent, isotropic data to the Abaqus format.\n",
      "5C560880-4FD3-4E5C-992B-4B6CEF6A055A (Abaqus 6) - Exports temperature independent, isotropic data to the Abaqus 6 format.\n",
      "911AF055-B388-439A-8AF6-EB18480E2D80 (Abaqus 6) - Linear, temperature-independent, isotropic, simple failure\n",
      "3AE2BEA5-B1DB-45D3-A431-48915B8D1317 (Abaqus 6) - Linear, temperature-independent, isotropic, simple failure with thermal expansion\n",
      "722E5C46-3633-4B72-BF93-74E8112C20C3 (Abaqus 6) - Exports temperature dependent, isotropic data to the Abaqus 6 format.\n",
      "B653C213-8BEB-42A7-8512-5F340EEBFAB4 (Abaqus 6) - Exports temperature independent, isotropic data to the Abaqus 6 format.\n",
      "458E9A7E-C268-4ED0-9CC1-FF7438521B4F (ANSYS Workbench) - Exports linear, temperature-independent, isotropic data to the Ansys Workbench format\n",
      "CE8DCFA2-B3EE-4545-8D3E-82810FA92AFC (ANSYS Workbench) - Exports linear, temperature-dependent, isotropic data to the Ansys Workbench format\n",
      "4B0B1EA3-8760-43DF-8060-2C79CA471D4C (ANSYS Workbench) - Exports linear, temperature-independent, isotropic with simple failure data to the Ansys Workbench format\n"
     ]
    }
   ],
   "source": [
    "request = gdl.ExportersForRecordsRequest(records=[rec])\n",
    "resp = session.engineeringDataService.ExportersForRecords(request)\n",
    "print(\"\\nOutput of exporters for Inconel 718\")\n",
    "for  exporter in resp.records[0].exporters:\n",
    "    print(\"{0} ({1}) - {2}\".format(exporter.name, \n",
    "                                   exporter.package, \n",
    "                                   exporter.description))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get a list of parameters that can be exported\n",
    "Get the parameters that can be exported from Inconel 718 using an exporter that supports them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensile Modulus (L-dir) with Temp.\n",
      "\tTemperature\n",
      "\tTime\n",
      "\tOther\n"
     ]
    }
   ],
   "source": [
    "exporter = resp.records[0].exporters[7]\n",
    "req = gdl.GetExporterParametersRequest(records=[rec], exporterKey=exporter.key)\n",
    "expParams = session.engineeringDataService.GetExporterParameters(req)\n",
    "for attrib in expParams.records[0].attributes:\n",
    "    print(attrib.attribute.name)\n",
    "    for param in attrib.parameters:\n",
    "        print(\"\\t\" + param.name)\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform an FEA Export\n",
    "Get all applicable attributes for this record."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "req = gdl.GetRecordAttributesRequest(recordReferences=[rec])\n",
    "attribs = session.browseService.GetRecordAttributes(req)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If exporting a functional data attribute, you also need to define a parameter value to evaluate the attribute at. Here, a parameter is fixed at 1.337."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "myParam = expParams.records[0].attributes[0].parameters[0]\n",
    "pwv = gdl.ParameterReferenceAndValue(parameterValue=gdl.ParameterValue(1.337), \n",
    "                                     parameter=myParam.parameterReference)\n",
    "\n",
    "pv = gdl.UnittedParameterValue(unitSymbol=myParam.unit.unitSymbol, \n",
    "                               parameterWithValues=pwv)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the FEA exporter, and print the output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n",
      "<EngineeringData version=\"14.0.0.367\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n",
      "  <Materials>\r\n",
      "    <MatML_Doc xsi:noNamespaceSchemaLocation=\"http:...\n"
     ]
    }
   ],
   "source": [
    "expReq = gdl.ExportRecordDataRequest(\n",
    "    attributeReferences=[a.attribute.attribute for a in attribs.recordAttributes], \n",
    "    records=[rec], \n",
    "    exporterKey=exporter.key,\n",
    "    parameterValues=[pv]\n",
    ")\n",
    "\n",
    "\n",
    "resp = session.engineeringDataService.ExportRecordData(expReq)\n",
    "\n",
    "print(resp.text[:200] + \"...\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write the output to file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"some_output.xml\", \"wb\") as f:\n",
    "    f.write(resp.text.encode(\"utf-8\"))"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
