Sorry, I left out that you have to create a new LayerTableRecord for each layer. Also, you probably want to set the other properties of the layer besides just the Name, e.g. Color, Linetype, etc. After you get all the properties set, then you can Add the layer. If you are writing this code to add a bunch of layers to a dwg that doesn't match your template. A better method would be to open the template in a side database and just copy the layers to the current dwg.
Here is an example I have for importing page setups from a template using the CopyFrom method.
using (Transaction sourceTrans = sourceDb.TransactionManager.StartTransaction())
{
// import template page setups
// Importing page setups also brings in referenced views.
DBDictionary sourceDict = sourceTrans.GetObject(sourceDb.PlotSettingsDictionaryId, OpenMode.ForRead) as DBDictionary;
foreach (DBDictionaryEntry de in sourceDict)
{
PlotSettings SourcePlotSettings = sourceTrans.GetObject((ObjectId)(de.Value), OpenMode.ForRead) as PlotSettings;
PlotSettings tempPlotSettings = new PlotSettings(SourcePlotSettings.ModelType);
tempPlotSettings.CopyFrom(SourcePlotSettings);
tempPlotSettings.AddToPlotSettingsDictionary(destDb);
destTrans.AddNewlyCreatedDBObject(tempPlotSettings, true);
}
sourceTrans.Commit();
} }
Ed
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to
post your code.