-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaterialAreas.cs
83 lines (71 loc) · 2.72 KB
/
MaterialAreas.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.IO;
using System.Collections.Generic;
using Rhino;
using Rhino.Commands;
using Rhino.DocObjects.Tables;
using Rhino.Geometry;
using Rhino.Input;
using Rhino.Input.Custom;
using Rhino.DocObjects;
namespace Scripted_Utilities
{
public class MaterialAreas : Command
{
public MaterialAreas()
{
// Rhino only creates one instance of each command class defined in a
// plug-in, so it is safe to store a refence in a static property.
Instance = this;
}
///<summary>The only instance of this command.</summary>
public static MaterialAreas Instance
{
get; private set;
}
///<returns>The command name as it appears on the Rhino command line.</returns>
public override string EnglishName
{
get { return "MaterialAreas"; }
}
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
RhinoApp.WriteLine("The {0} command will run now", EnglishName);
MaterialTable materialTable = doc.Materials;
List<double> totalAreas = new List<double>();
double cumulativeArea = 0;
foreach(Material m in materialTable)
{
doc.Objects.UnselectAll();
string command = string.Format("_-SelMaterialName {0}", m.Name);
RhinoApp.RunScript(command, true);
IEnumerable<RhinoObject> selectedObjects = doc.Objects.GetSelectedObjects(false, false);
double totalArea = 0;
foreach(RhinoObject obj in selectedObjects)
{
if(obj.ObjectType == ObjectType.Brep)
{
Brep brepGeom = (Brep) obj.Geometry;
double area = AreaMassProperties.Compute(brepGeom).Area;
totalArea += area;
}
}
cumulativeArea += totalArea;
totalAreas.Add(totalArea);
}
List<double> areaPercent = new List<double>();
totalAreas.ForEach(area => areaPercent.Add(area / cumulativeArea));
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "materialAreas.txt");
using (StreamWriter sw = File.CreateText(path))
{
for(int i=0; i<areaPercent.Count; i++)
{
string line = string.Format("Material: {0}, Area: {1}", materialTable[i], areaPercent[i]);
}
}
doc.Views.Redraw();
RhinoApp.WriteLine("The curves were rebuilt");
return Result.Success;
}
}
}