This example creates a circle, and then performs a polar array of the circle. This creates four circles filling 180 degrees around a base point of (4, 4, 0).
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
static Point2d PolarPoints(Point2d pPt, double dAng, double dDist)
{
return new Point2d(pPt.X + dDist * Math.Cos(dAng),
pPt.Y + dDist * Math.Sin(dAng));
}
[CommandMethod("PolarArrayObject")]
public static void PolarArrayObject()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table record for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// Create a circle that is at 2,2 with a radius of 1
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(2, 2, 0);
acCirc.Radius = 1;
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
// Create a 4 object polar array that goes a 180
int nCount = 1;
// Set a value in radians for 60 degrees
double dAng = 1.0472;
// Use (4,4,0) as the base point for the array
Point2d acPt2dArrayBase = new Point2d(4, 4);
while (nCount < 4)
{
Entity acEntClone = acCirc.Clone() as Entity;
Extents3d acExts;
Point2d acPtObjBase;
// Typically the upper-left corner of an object's extents is used
// for the point on the object to be arrayed unless it is
// an object like a circle.
Circle acCircArrObj = acEntClone as Circle;
if (acCircArrObj != null)
{
acPtObjBase = new Point2d(acCircArrObj.Center.X,
acCircArrObj.Center.Y);
}
else
{
acExts = acEntClone.Bounds.GetValueOrDefault();
acPtObjBase = new Point2d(acExts.MinPoint.X,
acExts.MaxPoint.Y);
}
double dDist = acPt2dArrayBase.GetDistanceTo(acPtObjBase);
double dAngFromX = acPt2dArrayBase.GetVectorTo(acPtObjBase).Angle;
Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,
(nCount * dAng) + dAngFromX,
dDist);
Vector2d acVec2d = acPtObjBase.GetVectorTo(acPt2dTo);
Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));
/*
// The following code demonstrates how to rotate each object like
// the ARRAY command does.
acExts = acEntClone.Bounds.GetValueOrDefault();
acPtObjBase = new Point2d(acExts.MinPoint.X,
acExts.MaxPoint.Y);
// Rotate the cloned entity around its upper-left extents point
Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;
CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
acEntClone.TransformBy(Matrix3d.Rotation(nCount * dAng,
curUCS.Zaxis,
new Point3d(acPtObjBase.X,
acPtObjBase.Y, 0)));
*/
acBlkTblRec.AppendEntity(acEntClone);
acTrans.AddNewlyCreatedDBObject(acEntClone, true);
nCount = nCount + 1;
}
}
// Save the new objects to the database
acTrans.Commit();
}
}