###################
Conversion Examples
###################

Export to StreamCache
=====================

The following example exports an imported CAD file to the StreamCache format for viewing in HOOPS Web Viewer:

.. code-block:: cpp
	
	Communicator::Converter converter;
	converter.Init(your_license_key);
	Communicator::Importer importer;
	Communicator::SC_Import_Options importOptions;
	importer.Init(&converter);
	if (!importer.Load(path_to_your_model_file, importOptions))
	{
		// handle error
	}
		
	Communicator::Exporter *exporter = new Communicator::Exporter();
	exporter->Init(&importer);
	Communicator::SC_Export_Options exportOptions;
	exporter->WriteSC(path_to_your_sc_folder, nullptr, exportOptions);

To generate a single, self-contained SCS file, change the second argument to the filepath for the location of your SCS file:

.. code-block:: cpp

	exporter->WriteSC(nullptr, "C:\\my_path\\my_file.scs", exportOptions);


Export to PNG with custom camera
================================

The following example uses a custom :doc:`Communicator::Camera </api_ref/viewing/classes/Communicator.Camera>` to write a PNG image:

.. code-block:: cpp
	
	Communicator::Converter converter;
	converter.Init(license_key);
	Communicator::Importer importer;
	Communicator::SC_Import_Options importOptions;
	importer.Init(&converter);
	if (!importer.Load(import_path, importOptions))
	{
		// handle error
	}
	//Camera Properties
	Communicator::Camera cam;
	cam.projection = Communicator::Camera::Projection::Perspective;
	cam.field_height = 200.0; //Camera View Height 
	cam.field_width = 355.0; //Camera View Width
	cam.position[0] = 0; cam.position[1] = 0; cam.position[2] = 1000; //0 is x, 1 is y, 2 is z
	cam.up_vector[0] = 0; cam.up_vector[1] = 1; cam.up_vector[2] = 0; //0 is x, 1 is y, 2 is z
	cam.target[0] = 35; cam.target[1] = 75; cam.target[2] = 0;        //0 is x, 1 is y, 2 is z
	Communicator::Exporter *exporter = new Communicator::Exporter();
	exporter->Init(&importer);
	exporter->WritePNG(export_path, 1920, 1080, Communicator::Color(0.0, 0.0, 0.0, 1.0), &cam, Communicator::ViewOrientation::Unspecified, false, false, false);


Export to PNG with pre-defined view
===================================

The following example uses predefined views to write a PNG image. These views are defined in the :doc:`Communicator::ViewOrientation </api_ref/viewing/enum/Communicator.ViewOrientation>` enum.

.. code-block:: cpp

	Communicator::Converter converter;
	converter.Init(license_key);
	Communicator::Importer importer;
	Communicator::SC_Import_Options importOptions;
	importer.Init(&converter);
	if (!importer.Load(path_to_your_model_file, importOptions))
	{
		// handle error
	}
	Communicator::Exporter *exporter = new Communicator::Exporter();
	exporter->Init(&importer);
	Communicator::SC_Export_Options exportOptions;
	exporter->WritePNG(output_file_path, 1920, 1080, Communicator::Color(0.0, 0.0, 0.0, 1.0), nullptr, Communicator::ViewOrientation::TopLeftFront, false, false, false);


Export to shattered parts
=========================

The following example exports a model to shattered parts:

.. code-block:: cpp

	Communicator::Converter converter;
	converter.Init(your_license_key);
	Communicator::Importer importer;
	Communicator::SC_Import_Options importOptions;
	importer.Init(&converter);
	if (!importer.Load(path_to_your_model_file, importOptions))
		return false;
	Communicator::Exporter *exporter = new Communicator::Exporter();
	exporter->Init(&importer);
	// Set up export options
	Communicator::SC_Export_Options exportOptions;
	exportOptions.prepare_shattered_parts = path_to_parts_dir; // creates a subdirectory here using the part name
	exportOptions.prepare_shattered_xml = path_to_xml_file; // writes assembly data to specified xml file
	exporter->WriteSCShattered(exportOptions);
	Communicator::SC_Export_Options exportOptionsMasterBuild;
	exporter->WriteSCMaster(path_to_xml_file, path_to_parts_dir, path_to_your_sc_output_directory, exportOptionsMasterBuild); // Writes to locations specified above

When :ref:`Communicator::SC_Export_Options::prepare_shattered_xml <api_ref/data_import/libconverter/structCommunicator_1_1SC__Export__Options:a898829d0b589a692f4d027c946251eed>` is set to ``true`` in the export options, calling :ref:`Communicator::Exporter::WriteSCShattered() <api_ref/data_import/libconverter/classCommunicator_1_1Exporter:a113e699ecc4a89ae6b78d57f41203107>` will write an XML file containing the full assembly structure referencing the external models. Each of these parts will be exported to its own distinct StreamCache model directory.

After the shattered parts are exported, calling :ref:`Communicator::Exporter::WriteSCMaster() <api_ref/data_import/libconverter/classCommunicator_1_1Exporter:a54bf1b36856547b953418f479a649619>` will generate a master StreamCache directory (or SCS file) that references the parts directory created earlier by :ref:`Communicator::Exporter::WriteSCShattered() <api_ref/data_import/libconverter/classCommunicator_1_1Exporter:a113e699ecc4a89ae6b78d57f41203107>`. When the master StreamCache instance is loaded in the WebViewer, the individual parts will be available in the model tree.


Export to HTML with a template
==============================

The following example exports a file to HTML, using the customizable |HCNOW| HTML template that includes basic UI features:

.. code-block:: cpp

	Communicator::Converter converter;
	converter.Init(your_license_key);
	Communicator::Importer importer;
	Communicator::SC_Import_Options importOptions;
	importOptions.read_attributes = true;
	importer.Init(&converter);
	if (!importer.Load(path_to_your_model_file, importOptions))
	{
		// handle error
	}
	Communicator::Exporter *exporter = new Communicator::Exporter();
	exporter->Init(&importer);
	Communicator::SC_Export_Options exportOptions;
	exporter->WriteHTML(output_file_path, html_template_file_path, exportOptions );

In addition to the full HTML template, a minimal template is also provided in the |HCNOW| package. The minimal template is a self-contained template, whereas the full template includes source links to |HCNOW|'s javascript libraries.

These templates can be found at *authoring/converter/bin/<platform>/HOOPSCommunicatorTemplate.html*

More information about HTML export can be found in the :doc:`Monolithic HTML Export Programming Guide </prog_guide/data_import/cad_conversion/converter_app/html-export>`.


Export to PDF with a template
=============================

**NOTE::** PDF export requires a HOOPS Publish license. 

The following example exports a model to a PDF file with a black background using a PDF template:

.. code-block:: cpp

	Communicator::Converter converter;
	converter.Init(license_key);
	Communicator::Importer importer;
	Communicator::SC_Import_Options importOptions;
	importer.Init(&converter);
	if (!importer.Load(path_to_your_model_file, importOptions))
	{
		// handle error
	}
	Communicator::Exporter *exporter = new Communicator::Exporter();
	exporter->Init(&importer);
	Communicator::SC_Export_Options exportOptions;
	exporter->WritePDF(output_file_path, pdf_template_file_path, "", Color(0,0,0,1), exportOptions );

A PDF template is available in the |HCNOW| package, located at *authoring/converter/bin/<platform>/HOOPSVisualizeWebTemplate.pdf*
