Skip to content

Commit

Permalink
Display geometry parameter added for baking with custom materials tha…
Browse files Browse the repository at this point in the history
…t include textures.
  • Loading branch information
visose committed Jul 18, 2018
1 parent abf39be commit a24c3cb
Show file tree
Hide file tree
Showing 33 changed files with 789 additions and 168 deletions.
57 changes: 42 additions & 15 deletions Extensions/Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,34 @@
<Reference Include="Costura, Version=3.1.0.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>..\packages\Costura.Fody.3.1.0\lib\net46\Costura.dll</HintPath>
</Reference>
<Reference Include="Eto, Version=2.4.6752.17745, Culture=neutral, PublicKeyToken=552281e97c755530, processorArchitecture=MSIL">
<HintPath>..\packages\RhinoCommon.6.6.18177.16151\lib\net45\Eto.dll</HintPath>
</Reference>
<Reference Include="geometry3Sharp, Version=1.0.144.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\geometry3Sharp.1.0.144\lib\net45\geometry3Sharp.dll</HintPath>
</Reference>
<Reference Include="GH_IO">
<HintPath>..\..\..\..\..\..\Program Files\Rhino 6\Plug-ins\Grasshopper\GH_IO.dll</HintPath>
<Private>False</Private>
<Reference Include="GH_IO, Version=6.6.18177.16150, Culture=neutral, PublicKeyToken=6a29997d2e6b4f97, processorArchitecture=MSIL">
<HintPath>..\packages\Grasshopper.6.6.18177.16151\lib\net45\GH_IO.dll</HintPath>
</Reference>
<Reference Include="Grasshopper">
<HintPath>..\..\..\..\..\..\Program Files\Rhino 6\Plug-ins\Grasshopper\Grasshopper.dll</HintPath>
<Private>False</Private>
<Reference Include="Grasshopper, Version=6.6.18177.16150, Culture=neutral, PublicKeyToken=dda4f5ec2cd80803, processorArchitecture=MSIL">
<HintPath>..\packages\Grasshopper.6.6.18177.16151\lib\net45\Grasshopper.dll</HintPath>
</Reference>
<Reference Include="MoreLinq, Version=3.0.0.0, Culture=neutral, PublicKeyToken=384d532d7e88985d, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\morelinq.3.0.0\lib\net451\MoreLinq.dll</HintPath>
</Reference>
<Reference Include="RhinoCommon">
<HintPath>..\..\..\..\..\..\Program Files\Rhino 6\System\RhinoCommon.dll</HintPath>
<Private>False</Private>
<Reference Include="Rhino.UI, Version=6.6.18177.16150, Culture=neutral, PublicKeyToken=552281e97c755530, processorArchitecture=MSIL">
<HintPath>..\packages\RhinoCommon.6.6.18177.16151\lib\net45\Rhino.UI.dll</HintPath>
</Reference>
<Reference Include="RhinoCommon, Version=6.6.18177.16150, Culture=neutral, PublicKeyToken=552281e97c755530, processorArchitecture=MSIL">
<HintPath>..\packages\RhinoCommon.6.6.18177.16151\lib\net45\RhinoCommon.dll</HintPath>
</Reference>
<Reference Include="Robots">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\AppData\Roaming\Grasshopper\Libraries\Robots.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="RobotsGH, Version=1.0.0.1, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="RobotsGH, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\..\..\AppData\Roaming\Grasshopper\Libraries\Robots.gha</HintPath>
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
Expand Down Expand Up @@ -96,6 +100,7 @@
<Compile Include="Model\Discrete\VoxelTiles.cs" />
<Compile Include="Model\Document\Cache.cs" />
<Compile Include="Model\Document\Debug.cs" />
<Compile Include="Model\Document\DisplayGeometry.cs" />
<Compile Include="Model\Document\IO.cs" />
<Compile Include="Model\Geometry\BallPivot.cs" />
<Compile Include="Model\Geometry\PolygonFill.cs" />
Expand All @@ -113,25 +118,29 @@
<Compile Include="Model\Spatial\BucketSearch.cs" />
<Compile Include="Model\Spatial\Octree.cs" />
<Compile Include="Model\Spatial\Primitives.cs" />
<Compile Include="Model\Toolpaths\Milling\MillingAttributes.cs" />
<Compile Include="Model\Toolpaths\Milling\MillingToolpath.cs" />
<Compile Include="Model\Toolpaths\SpatialExtrusion\SpatialAttributes.cs" />
<Compile Include="Model\Toolpaths\SpatialExtrusion\SpatialExtrusion.cs" />
<Compile Include="Model\Toolpaths\CSVConverter.cs" />
<Compile Include="Model\Toolpaths\SpatialExtrusion\Vertex.cs" />
<Compile Include="Model\Util.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="View\Discrete\VoxelTiles.cs" />
<Compile Include="View\Discrete\CurveSnap.cs" />
<Compile Include="View\Document\DisplayStyle.cs" />
<Compile Include="View\Document\DisplayGeometry.cs" />
<Compile Include="View\Document\Upload.cs" />
<Compile Include="View\Geometry\DifferentialGrowth.cs" />
<Compile Include="View\Discrete\UnityExport.cs" />
<Compile Include="View\Document\Export.cs" />
<Compile Include="View\Goo.cs" />
<Compile Include="View\Toolpaths\MillingToolpath.cs" />
<Compile Include="View\Toolpaths\MillingAttributes.cs" />
<Compile Include="View\Toolpaths\LayeredToolpath.cs" />
<Compile Include="View\Geometry\Remesher.cs" />
<Compile Include="View\Geometry\StraightSkeleton.cs" />
Expand All @@ -145,16 +154,15 @@
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Extensions.ruleset" />
<None Include="packages.config" />
<None Include="Resources\Icons\PaintBrush01.png" />
<None Include="Resources\Robots.gha" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\Icons\PaintBrush02.png" />
Expand Down Expand Up @@ -212,6 +220,21 @@
<ItemGroup>
<None Include="Resources\Icons\EyeParam.png" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\Icons\LayerConfig.png" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\Icons\LayersSubtract.png" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\Icons\LayersAdd.png" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\Icons\LayersClean.png" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\Icons\LayersConfigParam.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down Expand Up @@ -243,6 +266,10 @@ Erase "$(TargetPath)"</PostBuildEvent>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Fody.3.1.3\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.3.1.3\build\Fody.targets'))" />
<Error Condition="!Exists('..\packages\Costura.Fody.3.1.0\build\Costura.Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Costura.Fody.3.1.0\build\Costura.Fody.targets'))" />
<Error Condition="!Exists('..\packages\RhinoCommon.6.6.18177.16151\build\net45\RhinoCommon.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\RhinoCommon.6.6.18177.16151\build\net45\RhinoCommon.targets'))" />
<Error Condition="!Exists('..\packages\Grasshopper.6.6.18177.16151\build\net45\Grasshopper.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Grasshopper.6.6.18177.16151\build\net45\Grasshopper.targets'))" />
</Target>
<Import Project="..\packages\Costura.Fody.3.1.0\build\Costura.Fody.targets" Condition="Exists('..\packages\Costura.Fody.3.1.0\build\Costura.Fody.targets')" />
<Import Project="..\packages\RhinoCommon.6.6.18177.16151\build\net45\RhinoCommon.targets" Condition="Exists('..\packages\RhinoCommon.6.6.18177.16151\build\net45\RhinoCommon.targets')" />
<Import Project="..\packages\Grasshopper.6.6.18177.16151\build\net45\Grasshopper.targets" Condition="Exists('..\packages\Grasshopper.6.6.18177.16151\build\net45\Grasshopper.targets')" />
</Project>
101 changes: 101 additions & 0 deletions Extensions/Model/Document/DisplayGeometry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using Rhino.Geometry;
using Rhino.DocObjects;
using System.Net;
using System.Threading.Tasks;
using Rhino.UI;
using Rhino.Display;
using Rhino;

namespace Extensions.Model.Document
{

public class DisplayGeometry
{
public GeometryBase Geometry { get; set; }
public DisplayMaterial Material { get; set; }
public string Layer { get; set; }

public DisplayGeometry(GeometryBase geometry, DisplayMaterial material, string layer = "")
{
Geometry = geometry;
Material = material;
Layer = layer;
}

public Guid Bake(RhinoDoc doc, ObjectAttributes att = null, bool flipYZ = false)
{
if (att == null) att = doc.CreateDefaultAttributes();

if (!string.IsNullOrEmpty(Layer))
{
var layer = doc.Layers.FindName(Layer, RhinoMath.UnsetIntIndex);

if (layer == null)
{
att.LayerIndex = doc.Layers.Add(new Layer() { Name = Layer });
}
else
{
att.LayerIndex = layer.Index;
}
}

var geometry = Geometry;

if (Geometry is Mesh)
{
if (flipYZ)
geometry = (Geometry as Mesh).FlipYZ();

if (Material != null)
{
att.ColorSource = ObjectColorSource.ColorFromMaterial;
att.MaterialSource = ObjectMaterialSource.MaterialFromObject;

double transparency = Material.Transparency;
if (flipYZ) transparency = 1 - transparency;

var material = new Material
{
DiffuseColor = Material.Diffuse,
EmissionColor = Material.Emission,
Transparency = transparency
};

var texture = Material.GetBitmapTexture(true);

if (texture != null)
material.SetBitmapTexture(texture);

var matIndex = doc.Materials.Add(material);
att.MaterialIndex = matIndex;
}
}
else if (Material != null)
{
att.ColorSource = ObjectColorSource.ColorFromObject;
att.ObjectColor = Material.Diffuse;
}

return doc.Objects.Add(geometry, att);
}

public DisplayGeometry Duplicate()
{
return new DisplayGeometry(Geometry.Duplicate(), new DisplayMaterial(Material), Layer);
}

public DisplayGeometry Transform(Transform xform)
{
var copy = Duplicate();
copy.Geometry.Transform(xform);
return copy;
}

public override string ToString() => $"Display Geometry ({Geometry.ObjectType}, {Material?.Diffuse.Name ?? "Default"}, {Layer ?? "Active"})";
}
}
71 changes: 30 additions & 41 deletions Extensions/Model/Document/IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,48 @@
using System.Net;
using System.Threading.Tasks;
using Rhino.UI;
using Rhino.Display;
using Rhino;

namespace Extensions.Model.Document
{
public static class IO
{
public static string Export(List<DisplayStyle> geometries, int exportType, string folder, string fileName)
public enum ExportType { HTML, FBX };

public static string Export(List<DisplayGeometry> geometries, ExportType exportType, string folder, string fileName)
{
var doc = Rhino.RhinoDoc.ActiveDoc;
var doc = RhinoDoc.ActiveDoc;
var guids = new List<Guid>(geometries.Count);

bool flipYZ = exportType == ExportType.FBX;

foreach (var geometry in geometries)
{
int layerIndex;
guids.Add(geometry.Bake(doc, null, flipYZ));
}

var layer = doc.Layers.FindName(geometry.Layer, Rhino.RhinoMath.UnsetIntIndex);
doc.Objects.UnselectAll(false);
doc.Objects.Select(guids, true);

if (layer == null)
{
layerIndex = doc.Layers.Add(new Layer() { Name = geometry.Layer });
}
else
{
layerIndex = layer.Index;
}

var att = new ObjectAttributes
{
ColorSource = ObjectColorSource.ColorFromObject,
ObjectColor = geometry.Color,
LayerIndex = layerIndex
};
string filePath = Path.Combine(folder, fileName);

guids.Add(doc.Objects.Add(geometry.Geometry, att));
switch (exportType)
{
case ExportType.HTML:
{
RhinoApp.RunScript($"-_Export \"{filePath}.html\" ui=yes launch=yes _Enter", false);
break;
}
case ExportType.FBX:
{
RhinoApp.RunScript($"-_Export \"{filePath}.fbx\" _Enter _Enter", false);
break;
}
default:
break;
}

string filePath = Path.Combine(folder, $"{fileName}.html");
Rhino.RhinoApp.RunScript($"-_SaveAs \"{filePath}\" ui=yes launch=yes _Enter", false);
doc.Objects.Delete(guids, true);

return filePath;
Expand All @@ -55,7 +60,7 @@ public static void FtpUpload(string localFilePath, string url, string user, stri
{
if (_uploadTask != null && !_uploadTask.IsCompleted)
{
Rhino.RhinoApp.WriteLine("Please wait until the last upload has completed.");
RhinoApp.WriteLine("Please wait until the last upload has completed.");
return;
}

Expand Down Expand Up @@ -87,30 +92,14 @@ public static void FtpUpload(string localFilePath, string url, string user, stri
}
}

Rhino.RhinoApp.InvokeOnUiThread(new Action(() => StatusBar.HideProgressMeter()));
RhinoApp.InvokeOnUiThread(new Action(() => StatusBar.HideProgressMeter()));

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
Action text = () => Rhino.RhinoApp.WriteLine($"Web upload of file '{fileName}.html' complete, status: {response.StatusDescription}");
Rhino.RhinoApp.InvokeOnUiThread(text);
RhinoApp.InvokeOnUiThread(text);
}
});
}
}

public class DisplayStyle
{
public GeometryBase Geometry { get; set; }
public Color Color { get; set; }
public string Layer { get; set; }

public DisplayStyle(GeometryBase geometry, Color color, string layer = "")
{
Geometry = geometry;
Color = color;
Layer = layer;
}

public override string ToString() => $"Display Style ({Geometry.ObjectType}, {Color.Name}, {Layer})";
}
}
10 changes: 8 additions & 2 deletions Extensions/Model/Rendering/RenderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public static class RenderExtensions
{
public static Mesh BitmapFromVertexColors(Mesh mesh, string file)
{
if (!Directory.Exists(Path.GetDirectoryName(file))) throw new FileNotFoundException($" File \"{file}\" not found.", file);
var path = Path.GetDirectoryName(file);
if (!Directory.Exists(path)) throw new DirectoryNotFoundException($" Directory \"{path}\" not found.");

mesh.Unweld(0, false);
mesh.TextureCoordinates.Clear();
Expand Down Expand Up @@ -54,12 +55,17 @@ public static Mesh BitmapFromVertexColors(Mesh mesh, string file)
}

bitmap.Save(file, System.Drawing.Imaging.ImageFormat.Png);

mesh.VertexColors.Clear();

return mesh;
}

public static IEnumerable<Mesh> BitmapFromSolidColoredMeshes(IEnumerable<Mesh> meshes, string file)
{
if (!Directory.Exists(Path.GetDirectoryName(file))) throw new FileNotFoundException($" File \"{file}\" not found.", file);
var path = Path.GetDirectoryName(file);
if (!Directory.Exists(path)) throw new DirectoryNotFoundException($" Directory \"{path}\" not found.");

int count = meshes.Count();
int size = (int)Math.Ceiling(Math.Sqrt(count));
float fSize = (float)size * 2;
Expand Down
2 changes: 1 addition & 1 deletion Extensions/Model/Toolpaths/CSVConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Rhino.Geometry;
using Robots;

namespace Extensions.Toolpaths
namespace Extensions.Model.Toolpaths
{
// pX,pY,pZ,nX,nY,nZ,F'

Expand Down
Loading

0 comments on commit a24c3cb

Please sign in to comment.