Search code examples
c#3dbing-mapswindows-10-universaluwp-maps

How to create a MapModel3D programmatically?


I'm working on a UWP project which uses the Windows MapControl. I want to add 3d models to the map. I know how to do this from 3mf files. But now I would like to make procedural meshes and add these to the map.

I have found documentation on how to create a 3mf file in code: https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/generate-3mf

I followed this documentation to create a Printing3D3MFPackage.

But then I try to create a MapModel3D from this 3mf package, like this:

//package is of type Printing3D3MFPackage
var streamReference = RandomAccessStreamReference.CreateFromStream(package.ModelPart);
var myModel = await MapModel3D.CreateFrom3MFAsync(streamReference, MapModel3DShadingOption.Smooth);

And I get this exception:

System.Runtime.InteropServices.COMException: 'Unspecified error

Failed to create MapModel3D from 3MF stream.'

So I guess these streams aren't compatible.

Does anyone know how to do this?


Solution

  • Try this instead:

    …
    await package.SaveModelToPackageAsync(model);
    var packageStream = await localPackage.SaveAsync();
    var streamReference = RandomAccessStreamReference.CreateFromStream(packageStream );
    var myModel = await MapModel3D.CreateFrom3MFAsync(streamReference, MapModel3DShadingOption.Smooth);