Try this code that completely based on docs example^
static Point2d PolarPoints(Point2d pPt, double dAng, double dDist)
{
return new Point2d(pPt.X + dDist * Math.Cos(dAng),
pPt.Y + dDist * Math.Sin(dAng));
}
// create grid using rectangular array
[CommandMethod("sqv")]
public static void TestFillSquares()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
PromptPointOptions ppo = new PromptPointOptions("\nPick a lower left corner: ");
PromptPointResult res = ed.GetPoint(ppo);
if (res.Status != PromptStatus.OK) return;
Point3d p1 = res.Value;
PromptCornerOptions pko = new PromptCornerOptions("\nPick opposite corner: ", p1);
res = ed.GetCorner(pko);
if (res.Status != PromptStatus.OK) return;
Point3d p2 = res.Value;
//start a transaction
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
// create polyline to make the shape
Polyline poly = new Polyline(4);
poly.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0, 0, 0);
poly.AddVertexAt(1, new Point2d(p2.X, p1.Y), 0, 0, 0);
poly.AddVertexAt(2, new Point2d(p2.X, p2.Y), 0, 0, 0);
poly.AddVertexAt(3, new Point2d(p1.X, p2.Y), 0, 0, 0);
poly.Closed = true;
btr.AppendEntity(poly);
tr.AddNewlyCreatedDBObject(poly, true);
double leg = Math.Abs(p2.X - p1.X);
double wid = Math.Abs(p2.Y - p1.Y);
// Create a rectangular array with 5 rows and 8 columns
int nRows = 5;
int nColumns = 8;
double disrows = leg / nColumns;
double discols = wid / nRows;
// create polyline to make the shape
Polyline npoly = new Polyline(4);
npoly.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0, 0, 0);
npoly.AddVertexAt(1, new Point2d(p1.X + disrows, p1.Y), 0, 0, 0);
npoly.AddVertexAt(2, new Point2d(p1.X + disrows, p1.Y + discols), 0, 0, 0);
npoly.AddVertexAt(3, new Point2d(p1.X, p1.Y + discols), 0, 0, 0);
npoly.Closed = true;
npoly.ColorIndex = 1;
btr.AppendEntity(npoly);
tr.AddNewlyCreatedDBObject(npoly, true);
// Set the row and column offsets along with the base array angle
double dRowOffset = discols;
double dColumnOffset = disrows;
double dArrayAng = 0;
// Get the angle from X for the current UCS
Matrix3d curUCSMatrix = ed.CurrentUserCoordinateSystem;
CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
Vector2d acVec2dAng = new Vector2d(curUCS.Xaxis.X,
curUCS.Xaxis.Y);
// If the UCS is rotated, adjust the array angle accordingly
dArrayAng = dArrayAng + acVec2dAng.Angle;
// Use the upper-left corner of the objects extents for the array base point
Extents3d acExts = npoly.Bounds.GetValueOrDefault();
Point2d acPt2dArrayBase = new Point2d(acExts.MinPoint.X,
acExts.MaxPoint.Y);
// Track the objects created for each column
DBObjectCollection acDBObjCollCols = new DBObjectCollection();
acDBObjCollCols.Add(npoly);
// Create the number of objects for the first column
int nColumnsCount = 1;
while (nColumns > nColumnsCount)
{
Entity acEntClone = npoly.Clone() as Entity;
acDBObjCollCols.Add(acEntClone);
// Caclucate the new point for the copied object (move)
Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,
dArrayAng,
dColumnOffset * nColumnsCount);
Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));
btr.AppendEntity(acEntClone);
tr.AddNewlyCreatedDBObject(acEntClone, true);
nColumnsCount = nColumnsCount + 1;
}
// Set a value in radians for 90 degrees
double dAng = Math.PI / 2;
// Track the objects created for each row and column
DBObjectCollection acDBObjCollLvls = new DBObjectCollection();
foreach (DBObject acObj in acDBObjCollCols)
{
acDBObjCollLvls.Add(acObj);
}
// Create the number of objects for each row
foreach (Entity acEnt in acDBObjCollCols)
{
int nRowsCount = 1;
while (nRows > nRowsCount)
{
Entity acEntClone = acEnt.Clone() as Entity;
acDBObjCollLvls.Add(acEntClone);
// Caclucate the new point for the copied object (move)
Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,
dArrayAng + dAng,
dRowOffset * nRowsCount);
Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));
btr.AppendEntity(acEntClone);
tr.AddNewlyCreatedDBObject(acEntClone, true);
nRowsCount = nRowsCount + 1;
}
}
tr.Commit();
}
}
~'J'~
_____________________________________
C6309D9E0751D165D0934D0621DFF27919