How to programmatically assign an override template to a DTM?

Hi All,

I am attempting to write a simple utility to set the template override for a Terrain model element.
I've figured out how to access the EC attributes that set these properties but seem to be missing something.

If I apply a template override by hand through the properties dialog everything works as expected.

However, if I try and do it programattically it does not seem to apply the template ID. The Override Symbology flag is set successfully, however the template ID remains at -1.
My code is shown below:

	bool ECGetter::SetOverride()
	{
		// Set scope to selected element
		FindInstancesScopePtr scope = FindInstancesScope::CreateScope(m_elem, FindInstancesScopeOption(DgnECHostType::Element));
		// Search through all classes on selected element
		ECQueryPtr query = ECQuery::CreateQuery(ECQUERY_PROCESS_SearchAllClasses);

		// Create EC instance iterator
		DgnECInstanceIterable iterable = Bentley::DgnPlatform::DgnECManager::GetManager().FindInstances(*scope, *query);

		WString classText;
		// Iterate through EC Class Instances
		for (DgnECInstanceIterable::const_iterator it = iterable.begin(); it != iterable.end(); ++it)
		{
			DgnECInstancePtr instance = *it;

			ECN::ECClassCR instanceClass = instance->GetClass();

			// Check if we have a "CifDTMElement" ie OpenRoads Terrain
			if (instanceClass.GetName() == L"CifDTMElement")
			{
				// Switch on override symboloy
				instance->SetValue(L"OverrideSymbology", ECValue(true));

				// Name of template I wish to apply
				WCharCP temp_path = L"Terrain Feature\\Existing\\E-Contours HATCH";
				ElementId tID;
				// See if we can retrieve an id for this template
				if (ETSTATUS_Success != ElementTemplateUtils::GetTemplateIDFromPath(&tID, temp_path, ACTIVEMODEL->GetDgnFileP()))
				{
					// If no template id, see what dgnlib this template exists in
					DgnFileR TemplateDgnLibRef = *MstnElementTemplateMgr::GetDgnlibContainingTemplate(temp_path);

					// Get an ElementTemplateNodePtr for the template we want
					ElementTemplateNodePtr pTempl = ElementTemplateUtils::FindTemplateNodeByPath(temp_path, TemplateDgnLibRef);
					DgnFileR activeDGNRef = *ACTIVEMODEL->GetDgnFileP();

					// Copy the template from the dgnlib to the active file
					pTempl = MstnElementTemplateMgr::CopyTemplateToFile(*pTempl, activeDGNRef);

					// Commit template to active file (I think?)
					pTempl->ClearCachedInstances();
					pTempl->Write();
					pTempl->ReloadXAttributeData();

					// Retrieve the template ID of the newly copied template
					ElementTemplateUtils::GetTemplateIDFromPath(&tID, temp_path, ACTIVEMODEL->GetDgnFileP());
					instance->SetValue(L"WantMajorContours", ECValue(true));
					instance->SetValue(L"WantMinorContours", ECValue(true));
				}
				// Set the "OSTemplate" attribute to the template ID
				instance->SetValue(L"OSTemplate", ECValue(static_cast<long long>(tID)));
			}
		}

		return true;
	}

There is not a lot of documentation around about this, so I thought Id throw this out into the forum to see if anyone has any experience.

Any ideas why the above code may not be working?

Thanks

Liam

Parents Reply Children
No Data