=========================
HOOPS Publish Integration
=========================

HOOPS Publish is the 3D PDF export library from Tech Soft 3D. Publish enables the developer to export a HOOPS Visualize Desktop scene to a 3D PDF file or an HTML file. An introduction to Publish can be found `here <http://www.techsoft3d.com/products/hoops-toolkits/hoops-publish/>`_.

All Publish classes are members of the ``HPS::Publish`` container class.

----------------------------------

Prerequisites
=============

A separate license is required to use HOOPS Publish. The interface described on this page is the way to connect HOOPS Visualize Desktop with HOOPS Publish. Publish is only supported on Windows only (C++ and C#).

Steps for integrating HOOPS Visualize Desktop with HOOPS Publish are delineated below:


Step 1: Install Publish
-----------------------

HOOPS Publish is distributed from the `Tech Soft 3D Developer Zone <http://developer.techsoft3d.com/>`_ - the first step is to download and install it. You do not have to generate a separate key to use the Publish component. The key will be embedded with your HOOPS Visualize Desktop license key. 


Step 2: Set up the Environment
------------------------------

Publish uses the environment path to find the location of the supporting libraries. Add the location of the libraries to the path (note the different locations for 32 and 64-bit DLLs), for example, *C:\\Program Files\\HOOPS_Publish\\bin\\win64*.


Step 3: Include the Libraries In Your Project
---------------------------------------------

The HOOPS Visualize Desktop-Publish interface library must be included as a dependency. C++ users will need to link to *hps_sprk_publish.lib* or *hps_sprk_publish.so*. C# users need to reference the *hps_cs_sprk_publish_vc11* assembly.


Step 4: Include the Header File In Your Source (C++ Only)
---------------------------------------------------------

Your source files that use the HOOPS Visualize Desktop-Publish interface must include *sprk_publish.h*.

At this point, Publish is ready to use in your application.


--------------------------------------------

.. _simple:

Simple Export
=============

When initializing the ``HPS::World`` object in your application, you must also set the Publish resource directory. This directory is part of the Publish installation.

.. tabs::

	.. group-tab:: C++
	
		HPS::World world;
 
		// replace the argument with the location of your resource directory
		world->SetPublishResourceDirectory("C:/Program Files/HOOPS_Publish/bin/resource");
		   
	.. group-tab:: C#

		HPS.World world = new HPS.World();
		 
		// replace the argument with the location of your resource directory
		world.SetPublishResourceDirectory("C:/Program Files/HOOPS_Publish/bin/resource");
		   
To export a HOOPS Visualize Desktop model using HOOPS Publish, the :doc:`view hierarchy <0301_core>` must already be in place with the model residing in the ``HPS::Model`` segment. Once that prerequisite is met, only a few calls are required:

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_simple_export.cpp
		   :start-after: //! [simple_export]
		   :end-before: //! [simple_export]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_simple_export.cs
		   :start-after: //! [simple_export]
		   :end-before: //! [simple_export]

The Publish API is completely synchronous, so the export is complete once the call to ``Export`` returns.

**IMPORTANT:** The filename parameter in ``HPS::Publish::File::Export`` must include a fully-qualified path, or the export will fail.

.. _exporting-cad-model:

Exporting HOOPS Exchange Data to 3D PDF
---------------------------------------

For those developers who are using HOOPS Exchange to load a CAD model, the export process is slightly different. If your model contains B-rep, and you want that data to be exported, you need to export using a ``HPS::CADModel`` instead of a ``HPS::SprocketPath``. Doing so will export both the CAD model and any HOOPS Visualize Desktop data as unified model. If you don't specify a ``HPS::CADModel``, the Exchange geometry will be exported as tessellated triangles, and the B-rep data will be lost.

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_exchange_viz_export.cpp
		   :start-after: //! [export1]
		   :end-before: //! [export1]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_exchange_viz_export.cs
		   :start-after: //! [export1]
		   :end-before: //! [export1]
		   

.. _complex:

Complex Export
==============

To make a more complex PDF document, more work is required. In the following example, a document with two views is created, but the logic could be extended to accommodate other enhancements. Performing this complex export means interacting directly with the Publish API, including creating and configuring the Publish document object hierarchy.

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_complex_export.cpp
		   :start-after: //! [declarations]
		   :end-before: //! [declarations]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_complex_export.cs
		   :start-after: //! [declarations]
		   :end-before: //! [declarations]
		   
.. image:: images/publish_hierarchy.png

*The HOOPS Publish object hierarchy*

To set up each of the views, we'll use a regular HOOPS Visualize Desktop ``HPS::CameraKit``. The views are contained in an ``HPS::Publish::ArtworkKit`` object.

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_complex_export.cpp
		   :start-after: //! [setup_view]
		   :end-before: //! [setup_view]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_complex_export.cs
		   :start-after: //! [setup_view]
		   :end-before: //! [setup_view]
		   

Setting the Export Source
-------------------------

Next, you'll need to set the ``HPS::KeyPath`` (which contains the view hierarchy) as the source of the ``HPS::Publish::AnnotationKit``. The ``HPS::KeyPath`` should point to the segment in the scene graph where the data export will begin. That segment and all of its subsegments will be exported.

**NOTE:** For those developers who are using HOOPS Exchange to load a CAD model, the export process is slightly different. If your model contains B-rep, and you want that data to be exported, you need to export using a ``HPS::CADModel`` instead of a ``HPS::KeyPath``. Doing so will export both the CAD model and any HOOPS Visualize Desktop data as unified model. If you don't specify a ``HPS::CADModel``, the Exchange geometry will be exported as tessellated triangles, and the B-rep data will be lost. This behavior is controlled by setting the *source* of the annotation kit. Possible options for the source are shown below:

* ``CADModel`` - Only the Exchange data is exported. If you added any HOOPS Visualize Desktop data, it will not be part of the export.
* ``KeyPath`` or ``KeyPathArray`` - Everything is exported, except B-rep data from the Exchange model (tessellation is still exported).
* ``CADModel`` and ``KeyPath`` - Everything, including B-rep and HOOPS Visualize Desktop data, is exported.

The ``Export`` function has overloads to handle all of these cases.

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_exchange_viz_export.cpp
		   :start-after: //! [export2]
		   :end-before: //! [export2]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_exchange_viz_export.cs
		   :start-after: //! [export2]
		   :end-before: //! [export2]
		   
		   
Putting Everything Together
---------------------------

When configuring the ``HPS::Publish::PageKit`` object, it is possible to specify what portion of the page that HOOPS Publish will appropriate for the 3D model. This is done by passing an ``HPS::IntRectangle``, specified in points. Annotations are always specified using a rectangle. The origin is the bottom left corner of the page. Annotation kits can also be compressed by setting the compression options before export, as shown in the code snippet below. The compression algorithm is a lossy algorithm.

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_complex_export.cpp
		   :start-after: //! [annotation_kit]
		   :end-before: //! [annotation_kit]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_complex_export.cs
		   :start-after: //! [annotation_kit]
		   :end-before: //! [annotation_kit]
		   
**IMPORTANT:** The filename parameter in ``HPS::Publish::File::Export`` must include a fully-qualified path, or the export will fail.


Exporting With a Template
-------------------------

A PDF template is a special type of PDF file that might contain text fields, graphics, and blank areas that you can fill in with your data in order to compose a complete file. In HOOPS Visualize Desktop, templates are manipulated using the ``HPS::Publish::PageKit`` object.

A few sample templates are included with the HOOPS Publish package. This particular example is using *BOM_A4Table_L.pdf*. Whenever you are working with templates, you need to know beforehand the internal names of the fields that you want to fill in with data (there is no way to programmatically enumerate the field names). The template file in this example was generated using the source file found at *<PUBLISH INSTALL DIR>\\samples\\publish\\publishsource\\BillOfMaterial\\BillOfMaterial.cpp*, and the field names can be found inside that file.

To export with a template, you would follow the steps in the previous snippets, but add the following template export code to your calls to ``HPS::Publish::PageKit``. This code example will put the HOOPS Visualize Desktop model into the "My3DWindow" field:

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_complex_export.cpp
		   :start-after: //! [template_kit]
		   :end-before: //! [template_kit]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_complex_export.cs
		   :start-after: //! [template_kit]
		   :end-before: //! [template_kit]

You may have noticed the "_1" suffix on the field names. The suffix indicates the page number of the PDF that you're exporting to. So, to export to page 2, you would use a suffix of "_2". In this way, you have control over the data that gets inserted at the page level. This example only has one page, but the field suffix is required nonetheless.


.. _export_subtree:

Exporting a Subtree
-------------------

It is possible to export specific subtrees of a scene to a 3D PDF document, instead of having to export the whole model. To do this, you must use one of the ``HPS::AnnotationKit::SetSource`` methods and build a path to the segment you wish to export. ``SetSource`` accepts one of:

* ``HPS::SprocketPath``
* ``HPS::KeyPath``
* ``HPS::KeyPathArray`` (for exporting multiple paths)

This example demonstrates how to export using a ``HPS::KeyPathArray``:

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_export_subtree.cpp
		   :start-after: //! [export_subtree]
		   :end-before: //! [export_subtree]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_export_subtree.cs
		   :start-after: //! [export_subtree]
		   :end-before: //! [export_subtree]
		   

Widgets
=======

Links
-----

Publish allows you to add links in the PDF document on the exported pages. In the context of a PDF document, links are regions of the page which, when clicked, will invoke JavaScript embedded in the document. It's important to note that the link itself is just a rectangular region on the page, which may have an associated color, border, and highlight mode. It does not contain text or images. If you want a text string or an image to appear at the link location, that needs to be inserted separately using the appropriate API functions.

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_complex_export.cpp
		   :start-after: //! [linkkit]
		   :end-before: //! [linkkit]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_complex_export.cs
		   :start-after: //! [linkkit]
		   :end-before: //! [linkkit]
		   
		   
Buttons
-------

Buttons can be added anywhere on the page. Size is specified with a rectangle - all other options are set in the ``HPS::Publish::ButtonKit``.

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_complex_export.cpp
		   :start-after: //! [button_kit]
		   :end-before: //! [button_kit]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_complex_export.cs
		   :start-after: //! [button_kit]
		   :end-before: //! [button_kit]
		   
Button logic is handled with Javascript that you add to the PDF document's ``HPS::Publish::PageKit``.

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_complex_export.cpp
		   :start-after: //! [button_javascript]
		   :end-before: //! [button_javascript]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_complex_export.cs
		   :start-after: //! [button_javascript]
		   :end-before: //! [button_javascript]
		   

.. _modifying_pdfs:

Modifying PDF Files
===================

So far, we have talked about building and exporting PDF files, but HOOPS Publish also allows you to open, inspect, modify, and write a PDF that already exists. To create an in-memory copy of a PDF, use the following code:

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_modify_pdf.cpp
		   :start-after: //! [create_document]
		   :end-before: //! [create_document]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_modify_pdf.cs
		   :start-after: //! [create_document]
		   :end-before: //! [create_document]

The code snippet above loads the file specified by *filename* and returns a ``HPS::Publish::DocumentKey``. The ``HPS::Publish::DocumentKey`` can then be used to query and modify the structure of the PDF. Most of the functions correspond with the functions on ``HPS::Publish::DocumentKit``, however, the *Unset* and *Show* functions are not available due to limitations in the Publish API.

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_modify_pdf.cpp
		   :start-after: //! [modify_document]
		   :end-before: //! [modify_document]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_modify_pdf.cs
		   :start-after: //! [modify_document]
		   :end-before: //! [modify_document]
		   
If you need to make changes to a particular page, rather than make a document-wide setting, use the ``HPS::Publish::PageControl``:

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_modify_pdf.cpp
		   :start-after: //! [page_control]
		   :end-before: //! [page_control]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_modify_pdf.cs
		   :start-after: //! [page_control]
		   :end-before: //! [page_control]

After you are finished making changes to the file, simply re-export the document the same way you would export a newly created file. The only difference is that you would use the ``HPS::Publish::DocumentKey`` instead of a ``HPS::SprocketPath`` or ``HPS::Publish::AnnotationKit``.

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_modify_pdf.cpp
		   :start-after: //! [export_document]
		   :end-before: //! [export_document]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_modify_pdf.cs
		   :start-after: //! [export_document]
		   :end-before: //! [export_document]
		   
Associated memory in HOOPS Publish is freed when the last ``HPS::Publish::DocumentKey`` reference goes out of scope, or the developer calls ``Delete`` on it.


.. _exportable_data:

Exportable Data
===============

Not all data in a HOOPS Visualize Desktop scene is exportable via Publish. For those entities that are supported, Publish has its own internal representation; others are not supported at all. The table below lists entites that are exportable. If an attribute or entity is not mentioned in this list, you can assume it will not be exported.

.. csv-table:
	:header: "HOOPS Visualize Desktop entity", "Notes"
	
	"Color", "Color is exportable in general, however, vertex and edge color interpolation is not exportable. When exporting a point cloud, only vertex color defined at the segment level is exported (vertex colors defined on a shell are ignored)."
	"Curves and lines", "All non-facetted geometry is converted to polylines for export. Line patterns are not exported."
	"Edges", "The implicit edges of a tessellated piece of geometry are exported. However, viewing them requires setting a particular rendering mode in the resulting PDF, and there is no granular control for the visibility of different types of edges like in HOOPS Visualize Desktop. For example, you can set any of the rendering modes with ``Wireframe`` in the name, such as ``Publish::Rendering::Mode::[SolidWireframe, TransparentWireframe, Wireframe]``. Edge patterns are not exportable."
	"Facetted geometry", "All facetted geometry, including NURBS surfaces, is converted to a shell for export. Note that if face visibility is off, the geometry will still be exported but will not be visible by default (the user can toggle this using the tree control from within PDF Reader). Per-face visibility and color are supported for shells. Face patterns are not exportable."
	"PMI data", "PMI data is exportable if the original model was imported with PMI data"
	"Text", "All text is exported as UTF-8 and comes through as markup in the PDF. Font name, size, alignment, bold, italic, overline, strikethrough, underline, transforms, and line spacing are exportable. Some information is lost in the translation from HOOPS Visualize Desktop to PRC. For instance, the text insertion point for PRC is always the lower left, so any alignments are translated into moving the insertion point. PRC doesn't have multiline text strings, so these must be split into multiple lines (though still in a single markup), and things like line spacing are implicitly defined by the position information for multiple text strings in a markup. Additionally, screen-facing text will always be a fixed size."
	"Textures", "Only diffuse and environment textures are supported. Multitexturing is not supported. If you require multitexturing, we recommend you composite the textures into a single texture."
	"Vertices", "Vertices are visible if you set ``HPS::Publish::Rendering::Mode::Vertices`` or ``HPS::Publish::Rendering::Mode::ShadedVertices`` However, there is no rendering mode which allows vertices to be visible with faces and lines."


Other Limitations
-----------------

**PRC:** PRC data is exportable only if it was originally loaded with Exchange. If you use Publish to export a model that was loaded from Exchange, just the PRC data imported from Exchange will get written to the PDF (no scene graph changes will get exported). However, if you export a model that did not come from Exchange, the PRC data will be constructed from the scene graph and put into the PDF.

**CATIA drawings:** CATDrawings cannot be exported to 3D PDF. Attempting to export a CATDrawing to 3D PDF will result in an exception of type ``HPS::IOException``, which will have a result field set to ``HPS::IOResult::UnsupportedFormat``.

You can check whether an imported model is a CATDrawing by asking the ``HPS::CADModel`` for the "Filename" metadata, then checking whether its extension is *CATDrawing* (case insensitive). Alternatively, you can also check whether any of the ``HPS::CADModel`` subcomponents has the type ``ExchangeDrawingMask``.


.. _error_handling:

Error Handling
==============

During the import process, Publish will trigger events when it detects a problem or has information to give you. For example, if Publish couldn't find a referenced subcomponent in an assembly, or if there was some sort of import failure, a warning or error could be triggered. It the responsibility of your application to catch these events and respond appropriately.

The HOOPS Visualize Desktop-Publish interface will capture all events and wrap them into one of three types: ``HPS::InformationEvent``, ``HPS::ErrorEvent``, or ``HPS::WarningEvent``. Each of these types derive from ``HPS::Event``, thus, handling them is identical to handling any other type of HOOPS Visualize Desktop event (event handling is discussed :doc:`here <1001_event_handling>`).

``HPS::WarningEvent`` contains both a code and a message, while ``HPS::InformationEvent`` has only a message.


Exceptions
----------

When you attempt to add any object to a Publish page, HOOPS Visualize Desktop pulls the data out of the kit and passes it to HOOPS Publish. Thus, if you pass an empty or invalid object, HOOPS Visualize Desktop will pass the equivalent structure to Publish. If Publish returns an error, HOOPS Visualize Desktop will throw an ``HPS::IOException``, which includes a string describing the problem as well as the error code generated by Publish.


.. _html_export:

Exporting to HTML
=================

In HOOPS Visualize Desktop, there are two ways to export to HTML, either through :ref:`the HTML sprocket <prog_guide/0902_exporting_files:HTML export>` or through the Publish sprocket. With the Publish sprocket, it's possible to import a CAD model from the Exchange sprocket; this offers a performance benefit because the model data can be read directly from the PRC created in Exchange without incurring the overhead needed for data conversion.

(Please note, the Publish HTML export feature is only available for 64-bit versions of the HOOPS Visualize Desktop API, and a HOOPS Exchange license is necessary to import files with the Exchange sprocket.)

To export your CAD model to HTML, first import it via the Exchange sprocket:

.. tabs::

	.. group-tab:: C++
	
		.. literalinclude:: ../../../internals/tests/docs/source/cpp/00700_publish_html_export_1.cpp
		   :start-after: //! [publish_html_export_exchange]
		   :end-before: //! [publish_html_export_exchange]
		   
	.. group-tab:: C#

		.. literalinclude:: ../../../internals/tests/docs/source/cs/00700_publish_html_export_1.cs
		   :start-after: //! [publish_html_export_exchange]
		   :end-before: //! [publish_html_export_exchange]

Once the import has completed, set a new CADModel object to the CADModel retrieved from the import notifier. Then call ``HPS::Publish::File::ExportHTML()`` with your CADModel object, the output file path, and the HTML template file.

For your convenience, there are two template files in the HOOPS Visualize Desktop package, *HOOPSCommunicatorTemplate.html* and *HOOPSCommunicatorMinimalTemplate.html*. The standard template includes advanced UI features, whereas the minimal template only includes the basic functionality for viewing a model.

For more information on modifying an HTML template file, please see :ref:`Modifying the HTML Template <prog_guide/0902_exporting_files:Modifying the HTML template>`.
