.. _simple-load-vtf-tutorial:

#################################################################
UnstructGrid: Load Model from File and Set Up Model Specification 
#################################################################

.. image:: ../images/tut_loadvtf.png
    :height: 300
    :align: center

This tutorial shows how to create a data source by loading a VTFx file and setup a model 
specification.

This data source contains multiple states, scalar results, vector results and a displacement
result. After opening the file, the available metadata can be queried through the data source
directory. 

In this example you will set up the model specification to show the following:

-   Set the last state id as current
-   Set the first scalar result as fringes on the model
-   Set the first vector result
-   Set the first displacement result

.. note::
    This tutorial expect the application to have a correctly configured :class:`cee::vis::View`
    in place. See demo applications on how to set up a :class:`cee::vis::View` in your application.

**Create the model and data source**

Create a model and a data source. 

In this tutorial you will be reading a data source from a VTFx file so you need an instance
of the VTFx file interface data source, :class:`cee::ug::DataSourceVTFx`. 

Give the data source a unique id. 

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/SimpleLoadVTFx.cpp
    :language: cpp
    :lines: 47-48

**Open file**

Open the test file (contact.vtfx) included in the tutorial data files. The :func:`open() <cee::ug::DataSourceVTFx::open>`
function will return true if the file was opened successfully.

Set the data source in the model.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/SimpleLoadVTFx.cpp
    :language: cpp
    :lines: 51-57,59

**Setup model specification**

After opening the file, the metadata will be available through the data source's directory through
:func:`cee::ug::DataSource::directory()`.

The data source directory contains the following metadata for the data source:

-   State info
-   Scalar results info
-   Vector results info
-   Displacement results info
-   Parts info
-   Transformation result info

The :class:`cee::ug::ModelSpec` object is the model specification. Each :class:`cee::ug::UnstructGridModel` will own
a model specification object. 

The model specification describes the following settings for the model:

-   An array of state ids. Must always be at least one state set in the model specification.
-   A fringes result
-   An array of vector results
-   A displacement result
-   Use transformation result true/false

Use the information retrieved from the data source directory to set up the model specification.

**Set state**

Get metadata for all states and set the last state as current in the model specification.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/SimpleLoadVTFx.cpp
    :language: cpp
    :lines: 62-67

**Set result as fringes**

Get metadata for all available scalar results and set the first scalar to show as fringes in 
the model specification.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/SimpleLoadVTFx.cpp
    :language: cpp
    :lines: 70-75

**Set result as vector**

Get metadata for all available vector results and set the first vector in the model specification.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/SimpleLoadVTFx.cpp
    :language: cpp
    :lines: 78-83

**Set displacement result**

Get metadata for all available displacement results and set the first displacement in the 
model specification.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/SimpleLoadVTFx.cpp
    :language: cpp
    :lines: 86-91

**Set result visibility**

Toggle on result visibility for all parts for the selected result types. For easy access 
to the settings for all available parts in a model, use the :class:`cee::ug::PartSettingsIterator`.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/SimpleLoadVTFx.cpp
    :language: cpp
    :lines: 94-102

**Set up the created model**

The model is ready to use and can be added to the view. Exactly where the view exists
depends on the platform and solution. These examples uses Qt and the view is set up 
in a :class:`cee::qt::ViewerWidget`.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/SimpleLoadVTFx.cpp
    :language: cpp
    :lines: 105,107,109

**See the complete source code here:**

:ref:`simple-load-vtf-example`
