public void Test()
{
Document doc = ACAD_APP.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = HostApplicationServices.WorkingDatabase;
using (DocumentLock docLoc = doc.LockDocument())
{
try
{
PromptEntityOptions peo
= new PromptEntityOptions
("Select 3D solid."); peo.SetRejectMessage("\nInvalid selection...");
peo
.AddAllowedClass(typeof(Solid3d
),
true);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
return;
}
else
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Solid3d solid = trans.GetObject(per.ObjectId, OpenMode.ForRead) as Solid3d;
ObjectId
[] ids
= new ObjectId
[] { per
.ObjectId };
FullSubentityPath path
= new FullSubentityPath
(ids,
new SubentityId
(SubentityType
.Null, IntPtr
.Zero));
using (Brep brep
= new Brep
(path
)) {
//get biggest face by making list of areas, and then sorting
var faceAreaList
= new List
<double>(); foreach (Autodesk.AutoCAD.BoundaryRepresentation.Face face in brep.Faces)
{
faceAreaList.Add(face.GetArea());
}
faceAreaList.Sort();
faceAreaList.Reverse();
foreach (Autodesk.AutoCAD.BoundaryRepresentation.Face face in brep.Faces)
{
if (face.GetArea() == faceAreaList[0])
{
//get point in middle of biggest face, and normal vector
Autodesk.AutoCAD.Geometry.Surface surf = face.Surface;
Interval[] box = surf.GetEnvelope();
double p1 = box[0].LowerBound + box[0].Length / 2.0;
double p2 = box[1].LowerBound + box[1].Length / 2.0;
Point2d ptParams
= new Point2d
(p1, p2
); PointOnSurface pos
= new PointOnSurface
(surf, ptParams
); Vector3d normal = pos.GetNormal(ptParams);
Point3d pt = pos.GetPoint(ptParams);
Matrix3d resultantMatrix = Matrix3d.Identity;
//rotation around z axis
Plane xy
= new Plane
(Point3d
.Origin, Vector3d
.ZAxis);
Matrix3d zAxisTrans = Matrix3d.Rotation(Vector3d.ZAxis.AngleOnPlane(xy) - normal.AngleOnPlane(xy),Vector3d.ZAxis,pt);
normal = normal.TransformBy(zAxisTrans);
resultantMatrix = resultantMatrix.PreMultiplyBy(zAxisTrans);
//rotation around x axis
Plane yz
= new Plane
(Point3d
.Origin, Vector3d
.ZAxis);
Matrix3d xAxisTrans = Matrix3d.Rotation(Vector3d.ZAxis.AngleOnPlane(yz) - normal.AngleOnPlane(yz), Vector3d.ZAxis, pt);
normal = normal.TransformBy(xAxisTrans);
resultantMatrix = resultantMatrix.PreMultiplyBy(xAxisTrans);
//rotation around y axis
Plane xz
= new Plane
(Point3d
.Origin, Vector3d
.ZAxis);
Matrix3d yAxisTrans = Matrix3d.Rotation(Vector3d.ZAxis.AngleOnPlane(xz) - normal.AngleOnPlane(xz), Vector3d.ZAxis, pt);
normal = normal.TransformBy(yAxisTrans);
resultantMatrix = resultantMatrix.PreMultiplyBy(yAxisTrans);
solidEnt.TransformBy(resultantMatrix);
}
}
}
trans.Commit();
}
}
}
catch (System.Exception ex)
{
Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowAlertDialog(ex.Message);
}
}
}