using System;
using Rhino;
using Rhino.DocObjects;
using Rhino.Geometry;
[System.Runtime.InteropServices.Guid("62dd8eec-5cce-42c7-9d80-8b01fc169b81")]
public class AnalysisModeOnCommand : Rhino.Commands.Command
{
public override string EnglishName { get { return "cs_analysismode_on"; } }
protected override Rhino.Commands.Result RunCommand(RhinoDoc doc, Rhino.Commands.RunMode mode)
{
var zmode = Rhino.Display.VisualAnalysisMode.Register(typeof(ZAnalysisMode));
const ObjectType filter = Rhino.DocObjects.ObjectType.Surface | Rhino.DocObjects.ObjectType.PolysrfFilter | Rhino.DocObjects.ObjectType.Mesh;
Rhino.DocObjects.ObjRef[] objs;
var rc = Rhino.Input.RhinoGet.GetMultipleObjects("Select objects for Z analysis", false, filter, out objs);
if (rc != Rhino.Commands.Result.Success)
return rc;
int count = 0;
for (int i = 0; i < objs.Length; i++)
{
var obj = objs[i].Object();
if (obj.InVisualAnalysisMode(zmode))
continue;
if (obj.EnableVisualAnalysisMode(zmode, true))
count++;
}
doc.Views.Redraw();
RhinoApp.WriteLine("{0} objects were put into Z-Analysis mode.", count);
return Rhino.Commands.Result.Success;
}
}
[System.Runtime.InteropServices.Guid("0A8CE87D-A8CB-4A41-9DE2-5B3957436AEE")]
public class AnalysisModeOffCommand : Rhino.Commands.Command
{
public override string EnglishName { get { return "cs_analysismode_off"; } }
protected override Rhino.Commands.Result RunCommand(RhinoDoc doc, Rhino.Commands.RunMode mode)
{
var zmode = Rhino.Display.VisualAnalysisMode.Find(typeof(ZAnalysisMode));
if (zmode != null)
{
foreach (Rhino.DocObjects.RhinoObject obj in doc.Objects)
{
obj.EnableVisualAnalysisMode(zmode, false);
}
doc.Views.Redraw();
}
RhinoApp.WriteLine("Z-Analysis is off.");
return Rhino.Commands.Result.Success;
}
}
public class ZAnalysisMode : Rhino.Display.VisualAnalysisMode
{
Interval m_z_range = new Interval(-10,10);
Interval m_hue_range = new Interval(0,4*Math.PI / 3);
private const bool m_show_isocurves = true;
public override string Name { get { return "Z-Analysis"; } }
public override Rhino.Display.VisualAnalysisMode.AnalysisStyle Style { get { return AnalysisStyle.FalseColor; } }
public override bool ObjectSupportsAnalysisMode(Rhino.DocObjects.RhinoObject obj)
{
if (obj is Rhino.DocObjects.MeshObject || obj is Rhino.DocObjects.BrepObject)
return true;
return false;
}
protected override void UpdateVertexColors(Rhino.DocObjects.RhinoObject obj, Mesh[] meshes)
{
Rhino.Render.MappingTag mt = GetMappingTag(obj.RuntimeSerialNumber);
for (int mi = 0; mi < meshes.Length; mi++)
{
var mesh = meshes[mi];
if( mesh.VertexColors.Tag.Id != this.Id )
{
System.Drawing.Color[] colors = new System.Drawing.Color[mesh.Vertices.Count];
for (int i = 0; i < mesh.Vertices.Count; i++)
{
double z = mesh.Vertices[i].Z;
colors[i] = FalseColor(z);
}
mesh.VertexColors.SetColors(colors);
mesh.VertexColors.Tag = mt;
}
}
}
public override bool ShowIsoCurves
{
get
{
return m_show_isocurves;
}
}
Rhino.Render.MappingTag GetMappingTag(uint serialNumber)
{
Rhino.Render.MappingTag mt = new Rhino.Render.MappingTag();
mt.Id = this.Id;
mt.MeshTransform = Transform.Identity;
uint crc = RhinoMath.CRC32(serialNumber, m_z_range.T0);
crc = RhinoMath.CRC32(crc, m_z_range.T1);
crc = RhinoMath.CRC32(crc, m_hue_range.T0);
crc = RhinoMath.CRC32(crc, m_hue_range.T1);
mt.MappingCRC = crc;
return mt;
}
System.Drawing.Color FalseColor(double z)
{
double s = m_z_range.NormalizedParameterAt(z);
s = Rhino.RhinoMath.Clamp(s, 0, 1);
return System.Drawing.Color.FromArgb((int)(s * 255), 0, 0);
}
}
Imports Rhino.DocObjects
Imports Rhino
Imports Rhino.Geometry
<System.Runtime.InteropServices.Guid("62dd8eec-5cce-42c7-9d80-8b01fc169b81")> _
Public Class AnalysisModeOnCommand
Inherits Rhino.Commands.Command
Public Overrides ReadOnly Property EnglishName() As String
Get
Return "cs_analysismode_on"
End Get
End Property
Protected Overrides Function RunCommand(doc As RhinoDoc, mode As Rhino.Commands.RunMode) As Rhino.Commands.Result
Dim zmode = Rhino.Display.VisualAnalysisMode.Register(GetType(ZAnalysisMode))
Const filter As ObjectType = Rhino.DocObjects.ObjectType.Surface Or Rhino.DocObjects.ObjectType.PolysrfFilter Or Rhino.DocObjects.ObjectType.Mesh
Dim objs As Rhino.DocObjects.ObjRef() = Nothing
Dim rc = Rhino.Input.RhinoGet.GetMultipleObjects("Select objects for Z analysis", False, filter, objs)
If rc <> Rhino.Commands.Result.Success Then
Return rc
End If
Dim count As Integer = 0
For i As Integer = 0 To objs.Length - 1
Dim obj = objs(i).[Object]()
If obj.InVisualAnalysisMode(zmode) Then
Continue For
End If
If obj.EnableVisualAnalysisMode(zmode, True) Then
count += 1
End If
Next
doc.Views.Redraw()
RhinoApp.WriteLine("{0} objects were put into Z-Analysis mode.", count)
Return Rhino.Commands.Result.Success
End Function
End Class
<System.Runtime.InteropServices.Guid("0A8CE87D-A8CB-4A41-9DE2-5B3957436AEE")> _
Public Class AnalysisModeOffCommand
Inherits Rhino.Commands.Command
Public Overrides ReadOnly Property EnglishName() As String
Get
Return "cs_analysismode_off"
End Get
End Property
Protected Overrides Function RunCommand(doc As RhinoDoc, mode As Rhino.Commands.RunMode) As Rhino.Commands.Result
Dim zmode = Rhino.Display.VisualAnalysisMode.Find(GetType(ZAnalysisMode))
If zmode IsNot Nothing Then
For Each obj As Rhino.DocObjects.RhinoObject In doc.Objects
obj.EnableVisualAnalysisMode(zmode, False)
Next
doc.Views.Redraw()
End If
RhinoApp.WriteLine("Z-Analysis is off.")
Return Rhino.Commands.Result.Success
End Function
End Class
Public Class ZAnalysisMode
Inherits Rhino.Display.VisualAnalysisMode
Private m_z_range As New Interval(-10, 10)
Private m_hue_range As New Interval(0, 4 * Math.PI / 3)
Private Const m_show_isocurves As Boolean = True
Public Overrides ReadOnly Property Name() As String
Get
Return "Z-Analysis"
End Get
End Property
Public Overrides ReadOnly Property Style() As Rhino.Display.VisualAnalysisMode.AnalysisStyle
Get
Return AnalysisStyle.FalseColor
End Get
End Property
Public Overrides Function ObjectSupportsAnalysisMode(obj As Rhino.DocObjects.RhinoObject) As Boolean
If TypeOf obj Is Rhino.DocObjects.MeshObject OrElse TypeOf obj Is Rhino.DocObjects.BrepObject Then
Return True
End If
Return False
End Function
Protected Overrides Sub UpdateVertexColors(obj As Rhino.DocObjects.RhinoObject, meshes As Mesh())
Dim mt As Rhino.Render.MappingTag = GetMappingTag(obj.RuntimeSerialNumber)
For mi As Integer = 0 To meshes.Length - 1
Dim mesh = meshes(mi)
If mesh.VertexColors.Tag.Id <> Me.Id Then
Dim colors As System.Drawing.Color() = New System.Drawing.Color(mesh.Vertices.Count - 1) {}
For i As Integer = 0 To mesh.Vertices.Count - 1
Dim z As Double = mesh.Vertices(i).Z
colors(i) = FalseColor(z)
Next
mesh.VertexColors.SetColors(colors)
mesh.VertexColors.Tag = mt
End If
Next
End Sub
Public Overrides ReadOnly Property ShowIsoCurves() As Boolean
Get
Return m_show_isocurves
End Get
End Property
Private Function GetMappingTag(serialNumber As UInteger) As Rhino.Render.MappingTag
Dim mt As New Rhino.Render.MappingTag()
mt.Id = Me.Id
mt.MeshTransform = Transform.Identity
Dim crc As UInteger = RhinoMath.CRC32(serialNumber, m_z_range.T0)
crc = RhinoMath.CRC32(crc, m_z_range.T1)
crc = RhinoMath.CRC32(crc, m_hue_range.T0)
crc = RhinoMath.CRC32(crc, m_hue_range.T1)
mt.MappingCRC = crc
Return mt
End Function
Private Function FalseColor(z As Double) As System.Drawing.Color
Dim s As Double = m_z_range.NormalizedParameterAt(z)
s = Rhino.RhinoMath.Clamp(s, 0, 1)
Return System.Drawing.Color.FromArgb(CInt(Math.Truncate(s * 255)), 0, 0)
End Function
End Class