Skip to content

Commit

Permalink
Successfully exported lines
Browse files Browse the repository at this point in the history
into the three.js editor.  refactored some stuff.  Working on issues #2,
#6, #7.
  • Loading branch information
bhowes-tt committed Nov 4, 2014
1 parent 70e3e0a commit 5a88bf5
Show file tree
Hide file tree
Showing 9 changed files with 604 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@

namespace GHva3c
{
public class va3c_Scene : GH_Component
{
public class va3c_Scene_ARCHIVE_20141104 : GH_Component
{
public override GH_Exposure Exposure
{
get
{
return GH_Exposure.hidden;
}
}

/// <summary>
/// Initializes a new instance of the va3c_Scene class.
/// </summary>
public va3c_Scene()
public va3c_Scene_ARCHIVE_20141104()
: base("va3c_Scene", "va3c_Scene","va3c_Scene","va3c", "va3c")
{
}
Expand Down Expand Up @@ -260,31 +268,4 @@ private bool isJSONfile(string fileExtension)
}
}
}

public class va3cGeometryCatcher
{
public string uuid;
public string type;
public object data;
}

public class va3cMaterialCatcher
{
public string uuid;
public string type;
public string color;
public string ambient;
public string emissive;
public string specular;
public double shininess;
public double opacity;
public bool transparent;
public bool wireframe;
public int side;
}

public class va3cAttributesCatcher
{
public object userData;
}
}
5 changes: 4 additions & 1 deletion GHva3c/GHva3c/GHva3c.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="va3c_Line.cs" />
<Compile Include="va3c_LineBasicMaterial.cs" />
<Compile Include="va3c_MeshColoredFaces.cs" />
<Compile Include="va3c_Mesh.cs" />
<Compile Include="va3c_Material.cs" />
<Compile Include="va3c_MeshColoredVertices.cs" />
<Compile Include="va3c_Scene.cs">
<Compile Include="ComponentArchive\va3c_Scene_ARCHIVE_20141104.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="va3c_SceneCompiler.cs" />
<Compile Include="_Utilities.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
78 changes: 78 additions & 0 deletions GHva3c/GHva3c/_Utilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Rhino.Geometry;

namespace GHva3c
{
/// <summary>
/// Contains utility functions to be called from GH component classes
/// </summary>
public class _Utilities
{
/// <summary>
/// Returns a string representation of a hex color given a GH_Colour object
/// </summary>
/// <param name="ghColor">the grasshopper color to convert</param>
/// <returns>a hex color string</returns>
public static string hexColor(GH_Colour ghColor)
{
string hexStr = "0x" + ghColor.Value.R.ToString("X2") +
ghColor.Value.G.ToString("X2") +
ghColor.Value.B.ToString("X2");

return hexStr;
}
}


//below are a number of Catcher classes which are used to deserialize JSON objects
//mostly called from the va3c_CompileScene component


public class va3cGeometryCatcher
{
public string uuid;
public string type;
public object data;
}

public class va3cMaterialCatcher
{
public string uuid;
public string type;
public string color;
public string ambient;
public string emissive;
public string specular;
public double shininess;
public double opacity;
public bool transparent;
public bool wireframe;
public int side;
}

public class va3cAttributesCatcher
{
public object userData;
}

public class va3cLineCatcher
{
public string uuid;
public string type;
public object data;
}

public class va3cLineBasicMaterialCatcher
{
public string uuid;
public string type;
public string color;
public double linewidth;
public double opacity;
}
}
55 changes: 54 additions & 1 deletion GHva3c/GHva3c/va3c_Line.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
using System;
using System.Dynamic;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Timers;

using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Rhino.Geometry;
using GHva3c.Properties;

using Newtonsoft.Json;

namespace GHva3c
{
Expand Down Expand Up @@ -40,7 +48,52 @@ protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager
/// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
protected override void SolveInstance(IGH_DataAccess DA)
{
DA.SetData(0, "BUILD ME!");
//local variables
GH_Line line = null;

//catch inputs and populate local variables
if (!DA.GetData(0, ref line))
{
return;
}

//create JSON from line
string outJSON = lineJSON(line.Value);

//output results
DA.SetData(0, outJSON);

}

private string lineJSON(Line line)
{
//create a dynamic object to populate
dynamic jason = new ExpandoObject();

//top level properties
jason.uuid = Guid.NewGuid();
jason.type = "Geometry";
jason.data = new ExpandoObject();

//populate data object properties
jason.data.vertices = new object[6];
jason.data.vertices[0] = line.FromX * -1.0;
jason.data.vertices[1] = line.FromZ;
jason.data.vertices[2] = line.FromY;
jason.data.vertices[3] = line.ToX * -1.0;
jason.data.vertices[4] = line.ToZ;
jason.data.vertices[5] = line.ToY;
jason.data.normals = new object[0];
jason.data.uvs = new object[0];
jason.data.faces = new object[0];
jason.data.scale = 1;
jason.data.visible = true;
jason.data.castShadow = true;
jason.data.receiveShadow = false;


//return
return JsonConvert.SerializeObject(jason);
}

/// <summary>
Expand Down
107 changes: 107 additions & 0 deletions GHva3c/GHva3c/va3c_LineBasicMaterial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Dynamic;

using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Rhino.Geometry;

using Newtonsoft.Json;
using GHva3c.Properties;

namespace GHva3c
{
public class va3c_LineBasicMaterial : GH_Component
{
/// <summary>
/// Initializes a new instance of the va3c_LineBasicMaterial class.
/// </summary>
public va3c_LineBasicMaterial()
: base("va3c_LineBasicMaterial", "va3c_LineBasicMaterial",
"Creates a THREE.js Basic Line Material to use with line geometries",
"va3c", "materials")
{
}

/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddColourParameter("Color", "C", "Material Color", GH_ParamAccess.item);
pManager.AddNumberParameter("LineWeight", "LW", "The thickness, in pixels, of the line material", GH_ParamAccess.item, 1.0);
pManager[1].Optional = true;
}

/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddTextParameter("Material", "Mat", "Line Material", GH_ParamAccess.item);
}

/// <summary>
/// This is the method that actually does the work.
/// </summary>
/// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
protected override void SolveInstance(IGH_DataAccess DA)
{
//loacl varaibles
GH_Colour inColor = null;
GH_Number inNumber = new GH_Number(1.0);

//get user data
if (!DA.GetData(0, ref inColor))
{
return;
}
DA.GetData(1, ref inNumber);

//spin up a JSON material from the inputs
string outJSON = ConstructMaterial(inColor, inNumber);

//output
DA.SetData(0, outJSON);


}

private string ConstructMaterial(GH_Colour inColor, GH_Number inNumber)
{
//json object to populate
dynamic jason = new ExpandoObject();

//JSON properties
jason.uuid = Guid.NewGuid();
jason.type = "LineBasicMaterial";
jason.color = _Utilities.hexColor(inColor);
jason.linewidth = inNumber.Value;
jason.opacity = 1;


return JsonConvert.SerializeObject(jason);
}

/// <summary>
/// Provides an Icon for the component.
/// </summary>
protected override System.Drawing.Bitmap Icon
{
get
{
//You can add image files to your project resources and access them like this:
// return Resources.IconForThisComponent;
return null;
}
}

/// <summary>
/// Gets the unique ID for this component. Do not change this ID after release.
/// </summary>
public override Guid ComponentGuid
{
get { return new Guid("{dc90f883-fc2f-4f0b-955b-b276fda72c70}"); }
}
}
}
Loading

0 comments on commit 5a88bf5

Please sign in to comment.