Skip to content

Commit

Permalink
Live session
Browse files Browse the repository at this point in the history
  • Loading branch information
adamenagy committed Oct 18, 2021
1 parent 4aa466d commit 28f09b4
Show file tree
Hide file tree
Showing 11 changed files with 464 additions and 192 deletions.
204 changes: 139 additions & 65 deletions UpdateIPTParam/SampleAutomation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Autodesk.Forge.DesignAutomation.Inventor.Utils;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;

namespace UpdateIPTParam
{
Expand All @@ -19,10 +21,28 @@ public class SampleAutomation
private InventorServer m_server;
public SampleAutomation(InventorServer app) { m_server = app; }

public const string shelvesIamFile = "shelves.iam";
public const string paramsJsonFile = "params.json";
public const string outputPngFile = "output.png";
public const string outputZipFile = "output.zip";
public const string outputJsonFile = "output.json";

public void Run(Document doc)
{
var task = RunAsync(doc);
task.Wait(60000); // 1 minute
if (task.Exception != null)
{
throw task.Exception;
}
}

public async Task RunAsync(Document doc)
{
try
{
LogTrace("v3");

string curDir = System.IO.Directory.GetCurrentDirectory();
LogTrace("Current dir = " + curDir);

Expand All @@ -31,50 +51,136 @@ public void Run(Document doc)

var docDir = System.IO.Path.Combine(dllDdir, "Shelving");

var asm = m_server.Documents.Open(System.IO.Path.Combine(docDir, "shelves.iam"), false) as AssemblyDocument;
var asm = m_server.Documents.Open(System.IO.Path.Combine(docDir, shelvesIamFile), false) as AssemblyDocument;
LogTrace("Assembly path = " + asm.FullFileName);

string paramsPath = System.IO.Path.Combine(curDir, "params.json");
string paramsPath = System.IO.Path.Combine(curDir, paramsJsonFile);
LogTrace("Params path = " + paramsPath);

string data = System.IO.File.ReadAllText(paramsPath);
LogTrace("After reading params.json");
LogTrace("After reading " + paramsJsonFile);
//this errors out :-s >> LogTrace($"Params content = {data}");

JObject jParamsRoot = JObject.Parse(data);
string text = jParamsRoot.ToString(Formatting.None);
Trace.Write(text);

GenerateShelving(asm.ComponentDefinition, jParamsRoot["params"] as JObject);
Trace.WriteLine(text);

//LogTrace("Updating...");
//asm.Update2(true);

var output = jParamsRoot["output"].Value<string>();
switch (output)
{
case "outputPng":
SendPicture(asm.ComponentDefinition, jParamsRoot["screenshot"] as JObject);
break;

case "outputJson":
SendPositions(asm.ComponentDefinition);
break;

default:
var directUpload = false;
do {
directUpload = jParamsRoot["directUpload"].Value<bool>();
var outputPngUrl = jParamsRoot.ContainsKey("outputPngUrl") ? jParamsRoot["outputPngUrl"].Value<string>() : null;
var outputJsonUrl = jParamsRoot.ContainsKey("outputJsonUrl") ? jParamsRoot["outputJsonUrl"].Value<string>() : null;
var outputZipUrl = jParamsRoot.ContainsKey("outputZipUrl") ? jParamsRoot["outputZipUrl"].Value<string>() : null;

Transaction t = m_server.TransactionManager.StartTransaction(asm as _Document, "MyTransaction");

GenerateShelving(asm.ComponentDefinition, jParamsRoot["params"] as JObject);

if (outputPngUrl != null)
{
SavePicture(asm.ComponentDefinition, jParamsRoot["screenshot"] as JObject);
if (directUpload)
{
var outputPngCallback = jParamsRoot["outputPngCallback"].Value<string>();
await UploadFile(outputPngUrl, outputPngFile);
_ = UploadData(outputPngCallback, "{ }");
}
}

if (outputJsonUrl != null)
{
string positionData = SavePositions(asm.ComponentDefinition);
if (directUpload)
{
//_ = UploadFile(outputJsonUrl, outputJsonFile);
_ = UploadData(outputJsonUrl, positionData);
}
}

if (outputZipUrl != null)
{
// We don't have the right to save files in the AppBundle's folder,
// so we'll save it to the working folder
LogTrace("Saving...");
var asmPath = System.IO.Path.Combine(curDir, "shelves.iam");
var asmPath = System.IO.Path.Combine(curDir, shelvesIamFile);
asm.SaveAs(asmPath, true);

LogTrace("Zipping up files...");
string zipPath = System.IO.Path.Combine(curDir, "output.zip");
string zipPath = System.IO.Path.Combine(curDir, outputZipFile);
ZipModelFiles(asm, asmPath, zipPath);
break;
}

if (directUpload)
{
_ = UploadFile(outputZipUrl, outputZipFile);
}
}

t.Abort();

if (directUpload)
{
jParamsRoot = await GetData(jParamsRoot["dataCallback"].Value<string>());
}
} while (directUpload);
}
catch (Exception e) { LogTrace("RunAsync. Processing failed: {0}", e.ToString()); }
}

public async Task UploadFile(string url, string fileName)
{
LogTrace("[UploadFile]");
LogTrace(url + " / " + fileName);
string curDir = System.IO.Directory.GetCurrentDirectory();
string filePath = System.IO.Path.Combine(curDir, fileName);
using (var client = new HttpClient())
using (var fileStream = new StreamContent(System.IO.File.Open(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)))
{
LogTrace("[UploadFile.PutAsync]");
var response = await client.PutAsync(url, fileStream);
LogTrace("[/UploadFile.PutAsync]");
LogTrace("[/UploadFile]");
}
}

public async Task UploadData(string url, string data)
{
try
{
LogTrace("[UploadData]");
LogTrace(url);
using (var client = new HttpClient())
{

//LogTrace(jsonContent.Headers.ContentType.ToString());
//jsonContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
var content = new StringContent(data);
content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
//client.DefaultRequestHeaders.Add("Content-Type", "application/json");
var response = await client.PutAsync(url, content);// jsonContent);
LogTrace("[/UploadData]");
}
}
catch (Exception e) { LogTrace("UploadData. Processing failed: {0}", e.ToString()); }
}

public async Task<JObject> GetData(string url)
{
LogTrace("[GetData]");
LogTrace(url);
using (var client = new HttpClient())
{
var response = await client.GetAsync(url);
LogTrace("[/GetData]");

var data = await response.Content.ReadAsStringAsync();

JObject j = JObject.Parse(data);

return j;
}
catch (Exception e) { LogTrace("Processing failed: {0}", e.ToString()); }
}

public void GenerateShelving(AssemblyComponentDefinition acd, JObject jParams)
Expand All @@ -88,7 +194,7 @@ public void GenerateShelving(AssemblyComponentDefinition acd, JObject jParams)
acd.Parameters["Columns"].Expression = numberOfColumns;
acd.Parameters["ShelfWidth"].Expression = shelfWidth;
// Kick off the model update
acd.Parameters["iTrigger0"].Expression = $"{(acd.Parameters["iTrigger0"].Value + 1).ToString()}";
acd.Parameters["iTrigger0"].Expression = $"{((double)acd.Parameters["iTrigger0"].Value + 1).ToString()}";
}

static IList<string> Split(string str, int chunkSize)
Expand All @@ -104,9 +210,9 @@ static IList<string> Split(string str, int chunkSize)
return list;
}

public void SendPicture(AssemblyComponentDefinition acd, JObject jParams)
public void SavePicture(AssemblyComponentDefinition acd, JObject jParams)
{
Trace.WriteLine("SendPicture, jParams = " + jParams.ToString(Formatting.None));
Trace.WriteLine("SavePicture, jParams = " + jParams.ToString(Formatting.None));
var width = jParams["width"].Value<int>();
var height = jParams["height"].Value<int>();

Expand All @@ -117,18 +223,18 @@ public void SendPicture(AssemblyComponentDefinition acd, JObject jParams)
cam.ViewOrientationType = ViewOrientationTypeEnum.kIsoTopRightViewOrientation;
cam.Fit();
cam.ApplyWithoutTransition();
cam.SaveAsBitmap("output.png", width, height, Type.Missing, Type.Missing);
cam.SaveAsBitmap(outputPngFile, width, height, Type.Missing, Type.Missing);
}

public void SendPositions(AssemblyComponentDefinition acd)
public string SavePositions(AssemblyComponentDefinition acd)
{
Trace.WriteLine("SendPositions");
Trace.WriteLine("SavePositions");
JObject jRoot = new JObject();

JArray jComponents = new JArray();
foreach (ComponentOccurrence occ in acd.Occurrences)
{
PartDocument doc = occ.Definition.Document;
PartDocument doc = occ.Definition.Document as PartDocument;
string fileName = System.IO.Path.GetFileName(doc.FullFileName);

JObject jComponent = new JObject();
Expand All @@ -152,7 +258,9 @@ public void SendPositions(AssemblyComponentDefinition acd)
string data = jRoot.ToString(Formatting.None);
Trace.WriteLine($"data = {data}");

System.IO.File.WriteAllText("output.json", data);
System.IO.File.WriteAllText(outputJsonFile, data);

return data;
}

public void ZipModelFiles(AssemblyDocument asm, string asmPath, string zipPath)
Expand All @@ -170,40 +278,6 @@ public void ZipModelFiles(AssemblyDocument asm, string asmPath, string zipPath)
}
}

private static bool GetOnDemandFile(string name, string suffix, string headers, string responseFile, string content)
{
// writing a string (formatted according to ACESAPI format) to trace
// invokes the onDemand call to get the desired optional input file
LogTrace("!ACESAPI:acesHttpOperation({0},{1},{2},{3},{4})",
name ?? "", suffix ?? "", headers ?? "", content ?? "", responseFile ?? "");

// waiting for a control character indicating
// that the download has successfully finished
int idx = 0;
while (true)
{
char ch = Convert.ToChar(Console.Read());
// error
if (ch == '\x3')
{
return false;
}
// success
else if (ch == '\n')
{
return true;
}

// to many unexpected characters already read from console,
// treating as other error / timeout
if (idx >= 16)
{
return false;
}
idx++;
}
}

/// <summary>
/// This will appear on the Design Automation output
/// </summary>
Expand Down
27 changes: 23 additions & 4 deletions UpdateIPTParam/UpdateIPTParam.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>UpdateIPTParam</RootNamespace>
<AssemblyName>UpdateIPTParam</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
Expand All @@ -34,11 +34,30 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="autodesk.inventor.interop, Version=23.0.0.0, Culture=neutral, PublicKeyToken=d84147f8b4276564, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<EmbedInteropTypes>True</EmbedInteropTypes>
<EmbedInteropTypes>False</EmbedInteropTypes>
<HintPath>$(PackagePath)\autodesk\autodesk.inventor.interop.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -64,10 +83,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.2</Version>
<Version>13.0.1</Version>
</PackageReference>
<PackageReference Include="Autodesk.Forge.DesignAutomation.Inventor.Utils">
<Version>1.0.0-beta3</Version>
<Version>2.0.0</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
6 changes: 6 additions & 0 deletions UpdateIPTParam/UpdateIPTParam.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>
15 changes: 0 additions & 15 deletions designautomation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,12 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AFBF1E9B-9949-4DFC-8D5E-9EF8D5347AC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFBF1E9B-9949-4DFC-8D5E-9EF8D5347AC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFBF1E9B-9949-4DFC-8D5E-9EF8D5347AC1}.Debug|x64.ActiveCfg = Debug|Any CPU
{AFBF1E9B-9949-4DFC-8D5E-9EF8D5347AC1}.Debug|x64.Build.0 = Debug|Any CPU
{AFBF1E9B-9949-4DFC-8D5E-9EF8D5347AC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFBF1E9B-9949-4DFC-8D5E-9EF8D5347AC1}.Release|Any CPU.Build.0 = Release|Any CPU
{AFBF1E9B-9949-4DFC-8D5E-9EF8D5347AC1}.Release|x64.ActiveCfg = Release|Any CPU
{AFBF1E9B-9949-4DFC-8D5E-9EF8D5347AC1}.Release|x64.Build.0 = Release|Any CPU
{687A75D9-9B7D-42B7-9BE7-64893FE58F1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{687A75D9-9B7D-42B7-9BE7-64893FE58F1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{687A75D9-9B7D-42B7-9BE7-64893FE58F1B}.Debug|x64.ActiveCfg = Debug|Any CPU
{687A75D9-9B7D-42B7-9BE7-64893FE58F1B}.Debug|x64.Build.0 = Debug|Any CPU
{687A75D9-9B7D-42B7-9BE7-64893FE58F1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{687A75D9-9B7D-42B7-9BE7-64893FE58F1B}.Release|Any CPU.Build.0 = Release|Any CPU
{687A75D9-9B7D-42B7-9BE7-64893FE58F1B}.Release|x64.ActiveCfg = Release|Any CPU
{687A75D9-9B7D-42B7-9BE7-64893FE58F1B}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading

0 comments on commit 28f09b4

Please sign in to comment.