# Sapientino

Use the code below to model a plastic shell like the one in the picture:

``````int backLayer = model.Layers.Add("Back", Color.DeepPink);

// Creates the back surfaces

const double halfPI = Math.PI / 2;

List<Entity> backList = new List<Entity>();

CompositeCurve lateral1SweepRail = new CompositeCurve(new Line(0, -4, 120, -4), new Arc(120, -20, 0, 16, halfPI, 0),                                    new Line(136, -20, 136, -180), new Arc(120, -180, 0, 16, 0, -halfPI), new Line(120, -196, 0, -196));
CompositeCurve lateral2SweepRail = new CompositeCurve(new Line(0, -12, 120, -12), new Arc(120, -20, 0, 8, halfPI, 0),                                    new Line(128, -20, 128, -180), new Arc(120, -180, 0, 8, 0, -halfPI), new Line(120, -188, 0, -188));

Arc a1 = new Arc(Plane.YZ, new Point2D(-4, -4), 4, taperAngle, halfPI);
Arc a2 = new Arc(Plane.YZ, new Point2D(-12, -4), 4, halfPI, Math.PI - taperAngle);

Line l1 = new Line(0, +5, 0, -196);

Surface s1 = l1.ExtrudeAsSurface(150, 0, 0)[0];

s1.TrimBy(lateral2SweepRail, true);
s1.TrimBy(lateral1SweepRail, false);
s1.Translate(0, 0, 30);

Vector3D v1 = new Vector3D(0, taperAngle, -32);
Line lvert1 = new Line(a1.StartPoint, a1.StartPoint + v1);

Vector3D v2 = new Vector3D(0, -taperAngle, -32);
Line lvert2 = new Line(a2.EndPoint, a2.EndPoint + v2);

CompositeCurve lateral1Section = new CompositeCurve(a1, lvert1);
CompositeCurve lateral2Section = new CompositeCurve(a2, lvert2);

Surface[] lateral1 = lateral1Section.SweepAsSurface(lateral1SweepRail, 0.1);
Surface[] lateral2 = lateral2Section.SweepAsSurface(lateral2SweepRail, 0.1);

foreach (Surface surf in lateral1)
surf.Translate(0, 0, 30);

foreach (Surface surf in lateral2)
surf.Translate(0, 0, 30);

// Creates the composite curve for the Sweep of the base

Point3D center1 = new Point3D(126, -20, 0);

Vector3D v = new Vector3D(0, -160, 0);

Point3D center2 = center1 + v;

Point3D[] center3 = Curve.Intersection(arc1, arc2, 0.1);

Point2D center1_proj = Plane.XY.Project(center1);
Point2D center2_proj = Plane.XY.Project(center2);
Point2D center3_proj = Plane.XY.Project(center3[0]);

Vector2D vt = Vector2D.Subtract(center3_proj, center1_proj);
Vector2D vt3 = Vector2D.Subtract(center3_proj, center2_proj);

double startangle1 = vt.Angle;
double startangle2 = vt3.Angle;

double myDelta = 460;

Point3D center = new Point3D(0, -300 - myDelta, 0);

Vector3D centerDifferenceVect = Vector3D.Subtract(center1, center);

centerDifferenceVect.Normalize();

double bigArcAngle = Vector3D.AngleBetween(centerDifferenceVect, Vector3D.AxisY);

Arc bigArc1 = new Arc(Plane.XY, center, bigArcRadius, halfPI - bigArcAngle, halfPI);

arc1 = new Arc(Plane.XY, center1, radius1, startangle1, halfPI - bigArcAngle);

center = new Point3D(0, -350 - myDelta, 0);

centerDifferenceVect = Vector3D.Subtract(center2, center);

centerDifferenceVect.Normalize();

bigArcAngle = Vector3D.AngleBetween(centerDifferenceVect, Vector3D.AxisX);

Arc bigArc2 = new Arc(Plane.XY, center, bigArcRadius, bigArcAngle, halfPI);

arc2 = new Arc(center2, radius1, -Math.PI + bigArcAngle, startangle2);

Point2D arc1Start_proj = Plane.XY.Project(arc1.StartPoint);
Point2D arc2End_proj = Plane.XY.Project(arc2.EndPoint);

Vector2D diff1 = Vector2D.Subtract(arc1Start_proj, center3_proj);
double startangle = diff1.Angle;

Vector2D diff2 = Vector2D.Subtract(arc2End_proj, center3_proj);
double angle = Vector2D.SignedAngleBetween(diff1, diff2);

Arc arc3 = new Arc(center3[0], radius, startangle, startangle + angle);

CompositeCurve lowerBaseSweepRail = new CompositeCurve(bigArc2, bigArc1, arc1, arc3, arc2);

// Creates the lower surfaces

Arc section = new Arc(Plane.YZ, new Point2D(0, -10), 10, 0, halfPI);
// to get outwards pointing normals
section.Reverse();

Surface[] lowerBase = section.SweepAsSurface(lowerBaseSweepRail, 0.1);

foreach (Surface item in lowerBase)
{
item.Rotate(-Utility.DegToRad(5), Vector3D.AxisX, new Point3D(0, 0, 0));
item.ReverseU();
}

Line ln = new Line(new Point3D(0, 40, 0), new Point3D(0, -250, 0));

Surface baseplane = ln.ExtrudeAsSurface(200, 0, 0)[0];

baseplane.TrimBy(lowerBaseSweepRail, false);

baseplane.Rotate(-Utility.DegToRad(5), Vector3D.AxisX, new Point3D(0, 0, 0));

Surface baseplaneClone = (Surface)baseplane.Clone();

Surface.Trim(lateral2, new Surface[] { baseplane }, 0.01, true, false);
Surface.Trim(lateral1, new Surface[] { baseplaneClone }, 0.01, true, false);

// Creates the front surfaces

List<Entity> frontList = new List<Entity>();

section.TransformBy(new Mirror(Plane.XY));

Surface upperPlane = ln.ExtrudeAsSurface(200, 0, 0)[0];
upperPlane.ReverseU();

upperPlane.TrimBy(lowerBaseSweepRail, false);

LinearPath upperHoleProfile = new LinearPath();

upperHoleProfile.Vertices = new Point3D[]
{
new Point3D(0, -4, 0), new Point3D(136, -4, 0), new Point3D(136, -196, 0),
new Point3D(0, -196, 0)
};

upperPlane.TrimBy(upperHoleProfile, true);

foreach (Entity e in frontList)
{
e.Translate(0, 0, -20);

e.Rotate(-Utility.DegToRad(5), Vector3D.AxisX, new Point3D(0, 0, 0));
}

// Flips the entire model upside-down and adds a mirrored copy of each one to the entities collection

Transformation rotation = new Transformation();

rotation.Rotation(Math.PI, Vector3D.AxisY);

Mirror mirror = new Mirror(Plane.YZ);

int count = backList.Count;

for (int i = 0; i < count; i++)
{
backList[i].TransformBy(rotation);

Entity e = (Entity)backList[i].Clone();

e.TransformBy(mirror);

}

count = frontList.Count;

for (int i = 0; i < count; i++)
{
frontList[i].TransformBy(rotation);

Entity e = (Entity)frontList[i].Clone();

e.TransformBy(mirror);

}

// Adds the entities to the model

foreach (Entity entity in backList)
{
entity.Translate(0, 0, 30);
}

foreach (Entity entity in frontList)
{
entity.Translate(0, 0, 30);
}

// Adds the white screen planar surface

Surface screenSurf = Surface.CreatePlanar(Plane.XY, new Point3D(-136, -196, 0), new Point3D(136, -4, 0));

screenSurf.Translate(0, 0, 15);

screenSurf.Rotate(Utility.DegToRad(5), Vector3D.AxisX, new Point3D(0, 0, 0));

screenSurf.Translate(0, 0, 30);