public class Volumetry
{
[CommandMethod("Testing","TestVolume",0)]
public void TestVolume()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
BlockTableRecord current = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
SelectionFilter sf
= new SelectionFilter
(new TypedValue
[] { new TypedValue
((int)DxfCode
.Start,
"3DFACE") }); PromptSelectionOptions pso
= new PromptSelectionOptions
(); pso.MessageForAdding = "\nSelect faces on surface 1";
PromptSelectionResult psr1 = ed.GetSelection(pso, sf);
if (psr1.Status != PromptStatus.OK) return;
pso.MessageForAdding = pso.MessageForAdding.Replace('1', '2');
PromptSelectionResult psr2 = ed.GetSelection(pso, sf);
if (psr2.Status != PromptStatus.OK) return;
HashSet<myFace> surface1;
HashSet<myFace> surface2;
Dictionary<Point3d, myPoint3d> surf1Points;
Dictionary<Point3d, myPoint3d> surf2Points;
Plane pl
= new Plane
(Point3d
.Origin, Vector3d
.ZAxis);//plane to do 2d projections onto GetFacesPoints(psr1, pl, out surface1, out surf1Points);
GetFacesPoints(psr2, pl, out surface2, out surf2Points);
foreach (myFace face in surface1)
{
face.Face2d.ColorIndex = 3;
current.AppendEntity(face.Face2d);
tr.AddNewlyCreatedDBObject(face.Face2d, true);
DBText text
= new DBText
(); text.SetDatabaseDefaults();
text.Position = face.Center;
text.TextString = face.index.ToString();
current.AppendEntity(text);
tr.AddNewlyCreatedDBObject(text, true);
}
foreach (myFace face in surface2)
{
face.Face2d.ColorIndex = 5;
current.AppendEntity(face.Face2d);
tr.AddNewlyCreatedDBObject(face.Face2d, true);
DBText text
= new DBText
(); text.SetDatabaseDefaults();
text.Position = face.Center;
text.TextString = face.index.ToString();
current.AppendEntity(text);
tr.AddNewlyCreatedDBObject(text, true);
}
foreach (KeyValuePair<Point3d, myPoint3d> kvp in surf1Points)
{
Line newLine
= new Line
(Point3d
.Origin, kvp
.Value.point); current.AppendEntity(newLine);
tr.AddNewlyCreatedDBObject(newLine, true);
DBText text
= new DBText
(); text.SetDatabaseDefaults();
string temp = null;
foreach (int num in kvp.Value.ownerFaces)
{
temp += string.Format("{0}, ", num);
}
text.TextString = kvp.Value.pointIndex.ToString() + "Faces: " + temp;
text.Position = kvp.Value.point;
current.AppendEntity(text);
tr.AddNewlyCreatedDBObject(text, true);
}
foreach (KeyValuePair<Point3d, myPoint3d> kvp in surf2Points)
{
Line newLine
= new Line
(Point3d
.Origin, kvp
.Value.point); current.AppendEntity(newLine);
tr.AddNewlyCreatedDBObject(newLine, true);
DBText text
= new DBText
(); text.SetDatabaseDefaults();
string temp = null;
foreach (int num in kvp.Value.ownerFaces)
{
temp += string.Format("{0}, ", num);
}
text.TextString = kvp.Value.pointIndex.ToString() + "Faces: " + temp;
text.Position = kvp.Value.point;
current.AppendEntity(text);
tr.AddNewlyCreatedDBObject(text, true);
}
tr.Commit();
}
}
private void GetFacesPoints(PromptSelectionResult psr, Plane workingPlane, out HashSet<myFace> surface, out Dictionary<Point3d, myPoint3d> surfPoints)
{
surface
= new HashSet
<myFace
>(); surfPoints
= new Dictionary
<Point3d, myPoint3d
>(); for (int faceIndex = 0; faceIndex < psr.Value.Count; faceIndex++)
{
SelectedObject so = psr.Value[faceIndex];
surface
.Add(new myFace
(faceIndex,
(Face
)so
.ObjectId.GetObject(OpenMode
.ForRead), workingPlane
)); }
int pointIndex = 0;
foreach (myFace face in surface)
{
Face thisFace = face.Face;
for (short i = 0; i < 3; i++)
{
Point3d thisVertex = thisFace.GetVertexAt(i);
if (surfPoints.ContainsKey(thisVertex))
{
surfPoints[thisVertex].ownerFaces.Add(face.index);
}
else
{
surfPoints
.Add(thisVertex,
new myPoint3d
(pointIndex
++, thisVertex, face
.index, workingPlane
));
}
}
}
}
private class myPoint3d
{
public myPoint3d(int index, Point3d point, int faceIndex, Plane pl)
{
this.pointIndex = index;
this.point = point;
this.ownerFaces = new HashSet
<int>(); this.ownerFaces.Add(faceIndex);
this.matchedToFace = false;
this.point2d = pl.GetClosestPointTo(point).GetPoint();
}
public int pointIndex { get; set; }
public Point3d point { get; set; }
public Point3d point2d { get; set; }
public HashSet<int> ownerFaces { get; set; }
public bool matchedToFace { get; set; }
}
private class myFace
{
public myFace(int index, Face Face, Plane pl)
{
this.index = index;
this.Face = Face;
this.vch = new bool[3] { false,
false,
false }; Point3d vertex0 = Face.GetVertexAt(0);
Point3d vertex1 = Face.GetVertexAt(1);
Point3d vertex2 = Face.GetVertexAt(2);
this.Center = new Point3d
( (vertex0.X + vertex0.X + vertex0.X) / 3,
(vertex1.Y + vertex1.Y + vertex1.Y) / 3,
(vertex2.Z + vertex2.Z + vertex2.Z) / 3
);
this.Face2d = new Face
(); this.Face2d.SetVertexAt(0, pl.GetClosestPointTo(vertex0).GetPoint());
this.Face2d.SetVertexAt(1, pl.GetClosestPointTo(vertex1).GetPoint());
this.Face2d.SetVertexAt(2, pl.GetClosestPointTo(vertex2).GetPoint());
this.Face2d.SetVertexAt(3, pl.GetClosestPointTo(vertex2).GetPoint());
this.matchedToPoint = false;
}
public int index { get; set; }
public Face Face { get; set; }
public Face Face2d { get; set; }
public Point3d Center { get; set; }
public bool[] vch { get; set; }
public bool matchedToPoint { get; set; }
}
}