Skip to content

Commit

Permalink
updated Rhino Exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshkedia committed May 19, 2020
1 parent b0436b5 commit d8e3762
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
1 change: 1 addition & 0 deletions rhino-export/ExportAR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<HintPath>C:\Program Files\Rhino 6\System\Rhino.UI.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
<Compile Include="ExportARCommand.cs" />
Expand Down
51 changes: 25 additions & 26 deletions rhino-export/ExportARCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Collections.Specialized;
using System.Text;
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;

namespace ExportAR
{
Expand Down Expand Up @@ -52,37 +54,16 @@ protected override Result RunCommand(RhinoDoc doc, RunMode mode)
Directory.CreateDirectory(dir);
FileUtilties.DeleteFiles(dir);
FileObj.Write(objPath, doc, CreateObjWriteOptions());
FileUtilties.ModifyMtl(mtlPath);

byte[] objFile = File.ReadAllBytes(objPath);
byte[] mtlFile = File.ReadAllBytes(mtlPath);

string encodedObj = FileUtilties.EncodeBase64(objPath);
string encodedMtl = FileUtilties.EncodeBase64(mtlPath);

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;

writer.WriteStartObject();
writer.WritePropertyName("FileName");
writer.WriteValue(docName);
writer.WritePropertyName("FileData");
writer.WriteValue(encodedObj);
writer.WritePropertyName("FileMaterial");
writer.WriteValue(encodedMtl);
writer.WriteEndObject();

}

using (var wb = new WebClient())
{
string url = "http://ec2-13-233-130-134.ap-south-1.compute.amazonaws.com/";
var response = wb.UploadString(url, sw.ToString());
string responseInString = response;
RhinoApp.WriteLine(responseInString);
}


UploadToServer(objFile, mtlFile, docName);

doc.Objects.UnselectAll();

Expand Down Expand Up @@ -121,5 +102,23 @@ private static FileObjWriteOptions CreateObjWriteOptions()

return objWriteOptions;
}

private async Task<string> UploadToServer(byte[] objFile, byte[] mtlFile, String docName)
{
HttpClient httpClient = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(objFile, 0, objFile.Length), "doc.obj", docName + ".obj");
form.Add(new ByteArrayContent(mtlFile, 0, mtlFile.Length), "doc.mtl", docName + ".mtl");

string url = "https://ar.portfo.io/?mode=obj";

HttpResponseMessage response = await httpClient.PostAsync(url, form);

response.EnsureSuccessStatusCode();
httpClient.Dispose();
string sd = response.Content.ReadAsStringAsync().Result;
RhinoApp.WriteLine(sd);
return sd;
}
}
}
29 changes: 28 additions & 1 deletion rhino-export/FileUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO;
using System;
using System.Collections.Generic;
using System.IO;

namespace ExportAR
{
Expand Down Expand Up @@ -41,5 +43,30 @@ public static void WriteToTxt(string fileName, string txt)
}
}

/// <summary>
/// Combines all .mtl files into single material.mtl
/// </summary>
/// <param name="dir">The directory with .obj and .mtl files to parse.</param>
internal static void ModifyMtl(string file)
{
List<string> finalMaterialFile = new List<string>();

string[] mtl = File.ReadAllLines(file);

for (int i = 0; i < mtl.Length; i++)
{
//if (mtl[i].Contains("Ks") || mtl[i].Contains("Tf") || mtl[i].Contains("Ns")) mtl[i] = "";
finalMaterialFile.Add(mtl[i]);
}

File.Delete(file);

finalMaterialFile.RemoveAll(x => x.Length == 0);

File.WriteAllLines(file, finalMaterialFile.ToArray());

}


}
}
Binary file added rhino-export/bin.zip
Binary file not shown.
1 change: 1 addition & 0 deletions rhino-export/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net45" />
<package id="System.Net.Http" version="4.3.4" targetFramework="net45" />
</packages>

0 comments on commit d8e3762

Please sign in to comment.