using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Diagnostics;
#pragma warning disable CS0618
namespace AcadTest2020
{
public class TestSolid
{
private readonly double diameter = 0.1;
private readonly Vector3d vertDir = new Vector3d(0, 0, 1);
[CommandMethod("RunSolidTest1")]
public void RunTest1()
{
Vector3d insideDirection = new Vector3d
(0.968490925903539,
0.249048843487588,
0);
Point3d circleCenter = new Point3d
(2232732.26355686,
163570.797977758,
134.681813969545);
CreateSolid(insideDirection, Point3d.Origin, Matrix3d.Displacement(new Vector3d(circleCenter.ToArray())));
}
[CommandMethod("RunSolidTest2")]
public void RunTest2()
{
Vector3d insideDirection = new Vector3d
(-0.968490925903539,
-0.249048843487588,
0);
Point3d circleCenter = new Point3d
(2232741.17367338,
163573.089227118,
134.820753838578);
CreateSolid(insideDirection, Point3d.Origin, Matrix3d.Displacement(new Vector3d(circleCenter.ToArray())));
}
void CreateSolid
(Vector3d insideDirection,
Point3d circleCenter, Matrix3d mat)
{
Vector3d axisDir = vertDir.CrossProduct(insideDirection);
Point3d axisPoint = circleCenter - vertDir.MultiplyBy(diameter / 2.0);
using (Circle circle = new Circle(circleCenter, insideDirection, diameter / 2.0))
using (Solid3d rev = RevolveContour(circle, axisPoint, axisDir, Math.PI / 2.0))
{
if (rev != null)
{
ObjectId spaceId
= SymbolUtilityServices.GetBlockModelSpaceId
(HostApplicationServices.WorkingDatabase);
using (BlockTableRecord space
= spaceId.Open(OpenMode.ForWrite) as BlockTableRecord)
{
rev.TransformBy(mat);
space.AppendEntity(rev);
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="contour"></param>
/// <param name="axisPoint"></param>
/// <param name="axisDir"></param>
/// <param name="angleOfRev"></param>
/// <returns></returns>
static Solid3d RevolveContour
(Entity contour, Point3d axisPoint,
Vector3d axisDir, double angleOfRev = 2 * Math.PI)
{
RevolveOptionsBuilder
builder = new RevolveOptionsBuilder();
builder.CloseToAxis = false;
builder.DraftAngle = 0;
builder.TwistAngle = 0;
// Вращаем полусечение
Solid3d solid = new Solid3d();
try
{
solid.CreateRevolvedSolid
(contour,
axisPoint,
axisDir,
angleOfRev,
0.0,
builder.ToRevolveOptions());
return solid;
}
catch (System.Exception ex)
{
solid.Dispose();
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
return null;
}
}
}
}