This post has grown arms and legs!
It seems that it is not possible to assign a plot style name from a new inserted plot style table when no objects are using the plot style name.
i.e.
Open a new drawing
- Run the add plot style table function:
public void AddPlotStyleTable(string plotstyletableName)
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
LayoutManager lm = LayoutManager.Current;
try {
ed.WriteMessage(Strings.ChrW(10) + "Starting transaction...");
using (Transaction tr = db.TransactionManager.StartTransaction) {
ed.WriteMessage(Strings.ChrW(10) + "Setting the psv to the current layout: " + lm.CurrentLayout);
PlotSettingsValidator psv = PlotSettingsValidator.Current;
Layout ly = (Layout)tr.GetObject(lm.GetLayoutId(lm.CurrentLayout), OpenMode.ForWrite);
ed.WriteMessage(Strings.ChrW(10) + "Adding the plot style table");
psv.SetCurrentStyleSheet(ly, plotstyletableName);
tr.Commit();
}
}
catch (Exception ex) {
Interaction.MsgBox(ex.ToString);
}
}
Adding a plot style table that exists in your support paths
- Add the posted solution by Daniel
public void doit()
{
AddPlotStyleTable("test.stb");
AddPlotStyle(CreateLayer("Test Layer", 1, LineWeight.LineWeight000), "Black");
}
The function fails eKeyNotFound
The plot style has yet to be assigned to an object, and is not in use (I guess)
Now try this..
- New AutoCAD session
- Add a line to model space
- Change the objects properties to use the Black plot style name in the test.stb
- Execute the program
Works now (or for me anyway)
We could try to add a line by code and assign the plot style name from the plot style table, but again will be facing the problem where the plot style name is not in use and therefore cannot be assigned.
A workaround could be to insert a block (one that has the plot style table attached and the plot style names in use) before the layer is created, then afterwards, delete and purge.
But this, in my opinion is not 'managed' .net development; the creation of a layer requires a block to be stored somewhere in the support paths?