1
3D entities / Re: Ellipsoid Solid
« on: December 23, 2011, 08:27:05 AM »
By the way: I've changed the command name to "EllipsoidSolid" (or "ElSol", for short) to avoid conflicts.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

. . . .
A confirmation to a Autodesk/Microsoft commitment . . . .
using System;
using System.IO;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Windows;
[CommandMethod("Ellipsoid")]
//Basic method to put an ellipsoid at WCS origin
static public void OpInitEl()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptDoubleOptions pdo = new PromptDoubleOptions("\nEnter Length ");
pdo.AllowArbitraryInput = true;
pdo.AllowNegative = false;
PromptDoubleResult pdr = ed.GetDouble(pdo);
if (pdr.Status != PromptStatus.OK || pdr.Value <= 0) return;
double Length = pdr.Value;
pdo.Message = " Enter Width";
pdr = ed.GetDouble(pdo);
if (pdr.Status != PromptStatus.OK || pdr.Value <= 0) return;
double Width = pdr.Value;
pdo.Message = " Enter Height";
pdr = ed.GetDouble(pdo);
if (pdr.Status != PromptStatus.OK || pdr.Value <= 0) return;
double Height = pdr.Value;
Double[] matarr = new Double[16]{Length/2, 0.0,0.0,0.0,
0.0,Width/2,0.0,0.0,
0.0,0.0,Height/2,0.0,
0.0,0.0,0.0,1.0};
Matrix3d mat = new Matrix3d(matarr);
Point3dCollection Cvs = GenCVs();
//Scale points from sphere to ellipsoid
for (int i = 0; i < 9; i++ )
{
Cvs[i] = Cvs[i].TransformBy(mat);
}
DoubleCollection Dc = GenWghts();
KnotCollection[] Knots = GenKnots();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)(trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite));
try
{
//Generate the ellipsoid Octant
Autodesk.AutoCAD.DatabaseServices.NurbSurface NS = new Autodesk.AutoCAD.DatabaseServices.NurbSurface
(2, 2, true, 3, 3, Cvs, Dc,
Knots[0], Knots[1]);
//Create, mirror and union
Solid3d sol = new Solid3d();
sol.CreateBox(Length, Width, Height);
sol.TransformBy(Matrix3d.Scaling(0.5, new Point3d(Length / 2.0, Width / 2.0, Height / 2.0)));
sol.Slice(NS);
NS.Dispose();//Perhaps not specifically necessary - as NS is transaction resident
Plane Mir = new Plane(new Point3d(), new Vector3d(1.0,0.0,0.0));
Solid3d solMirr = sol.GetTransformedCopy(Matrix3d.Mirroring(Mir)) as Solid3d;
sol.BooleanOperation(BooleanOperationType.BoolUnite, solMirr);
Mir = new Plane(new Point3d(), new Vector3d(0.0, 1.0, 0.0));
solMirr = sol.GetTransformedCopy(Matrix3d.Mirroring(Mir)) as Solid3d;
sol.BooleanOperation(BooleanOperationType.BoolUnite, solMirr);
Mir = new Plane(new Point3d(), new Vector3d(0.0, 0.0, 1.0));
solMirr = sol.GetTransformedCopy(Matrix3d.Mirroring(Mir)) as Solid3d;
sol.BooleanOperation(BooleanOperationType.BoolUnite, solMirr);
sol.SetDatabaseDefaults();
btr.AppendEntity(sol);
trans.AddNewlyCreatedDBObject(sol, true);
trans.Commit();
}
catch
{
trans.Abort();
}
}
}
private static Point3dCollection GenCVs()
{
//Generate Control Vertices for one octant of nurb based sphere
Point3dCollection tempPts = new Point3dCollection();
tempPts.Add(new Point3d(1.0, 0.0, 0.0));
tempPts.Add(new Point3d(1.0, 0.0, 1.0));
tempPts.Add(new Point3d(0.0, 0.0, 1.0));
tempPts.Add(new Point3d(1.0, 1.0, 0.0));
tempPts.Add(new Point3d(1.0, 1.0, 1.0));
tempPts.Add(new Point3d(0.0, 0.0, 1.0));
tempPts.Add(new Point3d(0.0, 1.0, 0.0));
tempPts.Add(new Point3d(0.0, 1.0, 1.0));
tempPts.Add(new Point3d(0.0, 0.0, 1.0));
return tempPts;
}
private static DoubleCollection GenWghts()
{
//Generate weights for rational nurb surface
DoubleCollection temp = new DoubleCollection(9);
Double NearCorner = Math.Sqrt(0.5);
temp.Add(1.0);
temp.Add(NearCorner);
temp.Add(1.0);
temp.Add(NearCorner);
temp.Add(0.5);
temp.Add(NearCorner);
temp.Add(1.0);
temp.Add(NearCorner);
temp.Add(1.0);
return temp;
}
private static KnotCollection[] GenKnots()
{
//Generate knot vectors for PI based paramatization - uniform span from 0 to Pi/2 in U
// - uniform span from 0 to Pi/2 in V
KnotCollection[] tempKnots = new KnotCollection[2];
tempKnots[0] = new KnotCollection();
tempKnots[0].Add(0.0);
tempKnots[0].Add(0.0);
tempKnots[0].Add(0.0);
tempKnots[0].Add(Math.PI / 2);
tempKnots[0].Add(Math.PI / 2);
tempKnots[0].Add(Math.PI / 2);
tempKnots[1] = new KnotCollection();
tempKnots[1].Add(0.0);
tempKnots[1].Add(0.0);
tempKnots[1].Add(0.0);
tempKnots[1].Add(Math.PI / 2);
tempKnots[1].Add(Math.PI / 2);
tempKnots[1].Add(Math.PI / 2);
return tempKnots;
}