You have to add the pline to get an objectid, but you only need one pline to do all your hatches
[CommandMethod("hatchtest")]
public static void hatchtest()
{
Document doc=acadApp.DocumentManager.MdiActiveDocument;
Editor ed=doc.Editor;
Database db=doc.Database;
ObjectIdCollection hatchIds=new ObjectIdCollection();
double[]p={2,1,3,3,4,5,5,1,4,2,3,1};
double[] p2 = { 12, 11, 13, 13, 14, 15, 15, 11, 14, 12, 13, 11 };
List<double[]>ds=new List<double[]> {p,p2};
Polyline pline=new Polyline();
pline.SetDatabaseDefaults();
pline.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
Hatch hatch = new Hatch();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord space = tr.GetObject
(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
space.AppendEntity(pline);
tr.AddNewlyCreatedDBObject(pline, true);
for (int k = 0; k < ds.Count;k++)
{
for (int i = pline.NumberOfVertices - 1; i > 0; i--)
pline.RemoveVertexAt(i);
pline.SetPointAt(0, new Point2d(ds[k][0], ds[k][1]));
for (int i = 1; i < ds[k].Length / 2; i++)
{
int j = i * 2;
pline.AddVertexAt(i, new Point2d(ds[k][j], ds[k][j + 1]), 0, 0, 0);
}
pline.Closed = true;
hatchIds.Clear();
hatch = new Hatch();
hatch.SetDatabaseDefaults();
hatch.SetHatchPattern(HatchPatternType.PreDefined, "Ansi32");
hatchIds.Add(pline.ObjectId);
space.AppendEntity(hatch);
tr.AddNewlyCreatedDBObject(hatch, true);
hatch.Associative = false;
hatch.AppendLoop(HatchLoopTypes.External, hatchIds);
}
tr.Commit();
}
} // end hatchtest