Importing models

At this point, the application has been initialized, the rendering system has been connected to the window, and our view hierarchy is ready. Now, let’s load a model and get it on the screen. Before we begin, you should be aware that HNP can load two types of models - those with embedded CAD data and those without. In the case of CAD models, the HOOPS Exchange library is used. Those file formats, and the type of data Exchange can read from each of them are enumerated in the Exchange documentation. In this context, a CAD model is one with metadata associated with it (as opposed to just geometry) such as B-rep, filters, configurations, PMI, etc. Supported non-CAD file types are listed here and contain geometry only.

Importing a CAD model is as simple as calling HPS::Exchange::File::Import with a filename and an appropriate HPS::Exchange::ImportOptionsKit. The options kit is where you specify all the various settings related to the file you want to import. For example, if you want to make sure B-rep and tessellation are imported with the model, you would make that setting in this way:

		HPS::Exchange::ImportOptionsKit ioOpts = options;
		ioOpts.SetBRepMode(HPS::Exchange::BRepMode::BRepAndTessellation);

You can even get the file format before the import takes place using the following call. This is important because not all options are supported by all file types, and some options must be set for certain formats. Therefore, the options you set will depend on what kind of file you’re about to load:

		HPS::UTF8 filename(lpszPathName);
		HPS::Exchange::File::Format format = HPS::Exchange::File::GetFormat(filename);

The options kit is then passed to Import:

		notifier = HPS::Exchange::File::Import(filename, ioOpts);

		dlg.DoModal();
		success = dlg.WasImportSuccessful();
		status = notifier.Status();

Notice in this code snippet, we are making use of a HPS::Exchange::ImportNotifier. This object is used to inform us of the status of the import and the eventual result. Other useful information provided by the ImportNotifier are status conditions such as unsupported file versions, file not found, file pending import, and others, including eventual success. In our sandbox import function, we can see exactly that logic happening here when we check for IOResult.Success:

	if (status != HPS::IOResult::Success)
	{
		CAtlString str;
		if (status == HPS::IOResult::Failure)
			str.Format(_T("Error loading file %s:\n\n\t%s"), lpszPathName, std::wstring(message.begin(), message.end()).data());
		else
			str = getErrorString(status, lpszPathName);
		GetCHPSFrame()->MessageBox(str.GetString(), _T("File import error"), MB_ICONERROR | MB_OK);
	}
	else
		_cadModel = notifier.GetCADModel();

In the sandbox, we are using a modal dialog which tracks model progress and waits until the file is completely loaded. However, in your application, that may not be the case. Models are typically loaded asynchronously, therefore, it is important to know when the file is done loading so that we can take action. We can be assured that the model is finished by calling the Wait() function. When the function returns, we know HNP has finished its operation:

notifier.Wait();

A reference to the CAD model itself is provided by the notifier as well, using notifier.GetCADModel(). This object is then attached to the HPS::Model object, which means it is now in the rendering pipeline and will be drawn upon the next update. This code is from CProgressDialog.cpp, which gives a visual indication of model loading progress as well as attaches the model to the HPS::Model object once it’s done loading:

		HPS::View view = mfcView->GetCanvas().GetFrontView();

		// Enable static model for better performance
		_doc->GetModel().GetSegmentKey().GetPerformanceControl().SetStaticModel(HPS::Performance::StaticModel::Attribute);

		// Attach the model created in CHPSDoc
		view.AttachModel(_doc->GetModel());

Importing a non-CAD model is exactly the same process, except instead of using HPS::Exchange::File::Import to load the model, you would use one of the HPS::<namespace>::File::Import variants. Although this type of model contains geometry only, you can still select certain options in a corresponding import options kit, such as HPS::STL::ImportOptionsKit or HPS::Stream::ImportOptionsKit.

Component hierarchy

HOOPS Visualize uses a generic component hierarchy to represent third-party CAD models which feature topological entities. The component hierarchy is a way to organize the scene graph uniformly across disparate file formats. It also provides developers the ability to extend the capabilities of our importers, as well as to create entirely new importers in a way that is compatible with the Visualize scene graph. Our importers produce CADModel objects that abstract the structure of the model into a component hierarchy as shown below:

../../_images/component_hierarchy_generalized.png

Before working with CAD models in your own application, make sure to read and understand the Component hierarchy section of the HOOPS Visualize Programming Guide.