Forge SVF Extractor in C# .NET

December 15, 2017

This was initially addressed by Cyrille, later by Philippe, but always in Node.js. The first creates a .ZIP with all the files for offline viewing, including the Viewer JS libraries. The second creates a .ZIP too, but just the model viewables.

For the .NET version I tried to implement the raw feature: enumerate the list of URLs to download the files and how to save them locally. With that in mind, the ExtractSVFAsync(urn, accessToken)  method returns a list of Resource, which is a struct that contains the FileName, RemotePath indicating the path to download the file, and LocalPath indicating where to save them locally. The following code demonstrates its usage. One may decide, later, to create a ZIP or add more features, like the original samples. 

// get the list of resources to download
List<Resource> resourcesToDownload = await ExtractSVFAsync(urn, AccessToken);

IRestClient client = new RestClient("https://developer.api.autodesk.com/");
foreach (ForgeUtils.Derivatives.Resource resource in resourcesToDownload)
{
  // prepare the GET to download the file
  RestRequest request = new RestRequest(resource.RemotePath, Method.GET);
  request.AddHeader("Authorization", "Bearer " + AccessToken);
  request.AddHeader("Accept-Encoding", "gzip, deflate");
  IRestResponse response = await client.ExecuteTaskAsync(request);

  if (response.StatusCode == System.Net.HttpStatusCode.OK)
  {
    // combine with selected local path
    string pathToSave = Path.Combine(folderPath, resource.LocalPath);
    // ensure local dir exists
    Directory.CreateDirectory(Path.GetDirectoryName(pathToSave));
    // save file
    File.WriteAllBytes(pathToSave, response.RawBytes);
  }
  else
  {
    // something went wrong for this file...
  }
}

That's it, quite direct, right?

As the ExtractSVF may need further adjustments, please refer to the Github repo for the latest version. The sample includes other cool features and a better UI for upload & download, check previous blog post.

 

Posts by author

Augusto Goncalves
Developer Advocate, Autodesk

Developer Advocate at Autodesk since 2008, working with both desktop and web/cloud apps using top technologies, like C#, JavaScript, NodeJS and any other that can solve problems and improve workflo