Solved! Go to Solution.
"¿Que?"
using (Transaction myTrans =dbTransactionManager.StartTransaction())
{
LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId,OpenMode.ForRead);
LayerTableRecord layerId=db.clayer;
if (lt.Has("Test"))
{
layerId= (LayerTableRecord)tr.GetObject(lt["Test"],OpenMode.ForRead);
}
else
{
lt.UpgradeOpen();
layerId=new LayerTableRecord();
lt.Add(layerId);
layerId.Name = "Test";
layerId.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
tr.AddNewlyCreatedDBObject(layerId,true);
}
...
aEntity.LayerId = layerID
}
Sorry about the error in the previous sample.
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable tb = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord ms = (BlockTableRecord)tr.GetObject(tb[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
LayerTableRecord testLy = null;
if (lt.Has("Test"))
{
testLy = (LayerTableRecord)tr.GetObject(lt["Test"], OpenMode.ForRead);
}
else
{
lt.UpgradeOpen();
testLy = new LayerTableRecord();
lt.Add(testLy);
testLy.Name = "Test";
tr.AddNewlyCreatedDBObject(testLy, true);
}
//Change CurrentLayer, and thus altering the defaults in "txt.SetDatabaseDefaults()" below
db.Clayer = testLy.ObjectId;
DBText txt = new DBText();
txt.SetDatabaseDefaults();
ms.AppendEntity(txt);
tr.AddNewlyCreatedDBObject(txt, true);
txt.Position = new Point3d(100, 150, 0);
txt.TextString = "Hello World!";
txt.Layer = "Test";
//or
txt.LayerId = testLy.ObjectId;
//or
txt.LayerId = db.Clayer;
tr.Commit();
}