Hi,
After lots of tests I've found where is the bug in the API and how I (you) can reproduce it.
I join a DWG file which contains:
3 layers named : Calque1 color Red and Calque2 color Green and FMULT (contaning viewports)
2 layouts names : TEST1 and TEST2
2 layer states named : TEST1, TEST2
1 rectangle in Calque1 and 1 rectangle in Calque2.
The goal is to restore layerstate TEST1 in layout TEST1 in viewport in layer FMULT and layerstate TEST2 in layout TEST2 in viewport in layer FMULT with this code (some comments are in french, if it's a problem I can translate):
[CommandMethod("PgxTest2")]
public void PgxTest2()
{
Document _acCurDoc = AcAp.DocumentManager.MdiActiveDocument;
Database _acCurDb = _acCurDoc.Database;
Editor ed = _acCurDoc.Editor;
Dictionary<ObjectId, string> _dictLyrState = new Dictionary<ObjectId, string>();
// Start a transaction
using (Transaction tr = _acCurDb.TransactionManager.StartTransaction())
{
// Lit les infos des présentations
DBDictionary acDbDict = tr.GetObject(_acCurDb.LayoutDictionaryId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead) as DBDictionary;
foreach (DBDictionaryEntry acDbDictEnt in acDbDict)
{
Layout layout = tr.GetObject(acDbDictEnt.Value, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead) as Layout;
if (layout.LayoutName.ToUpper() == "MODEL") continue;
// Sélectionne la viewport
TypedValue[] tvArray = new TypedValue[] {
new TypedValue((int)DxfCode.Start, "VIEWPORT"),
new TypedValue((int)DxfCode.LayerName, "FMULT"),
new TypedValue((int)DxfCode.LayoutName, layout.LayoutName)
};
SelectionFilter ssFilter = new SelectionFilter(tvArray);
PromptSelectionResult psr = _acCurDoc.Editor.SelectAll(ssFilter);
// If selection actually contains nothing or more than 1 viewport
if ((psr.Status != PromptStatus.OK)
|| psr.Value.Count != 1)
continue;
_dictLyrState.Add(psr.Value[0].ObjectId, layout.LayoutName);
}
// Dispose of the transaction
}
foreach (ObjectId id in _dictLyrState.Keys)
{
// Restore les états de calque
LayerStateManager acLyrStMan = _acCurDb.LayerStateManager;
string sLyrStName = _dictLyrState[id];
if (acLyrStMan.HasLayerState(sLyrStName) == true)
{
acLyrStMan.RestoreLayerState(sLyrStName, id, 4,
LayerStateMasks.Color | LayerStateMasks.LineType | LayerStateMasks.CurrentViewport);
}
}
} So to reproduce the bug :
1st test
Open the DWG (layout TEST1 is current) launch the code => It's OK
2nd test
Close and re-open the DWG, switch to Model tab launch the code => It's bad because red square diseappear, it's the bug : when Model tab is current and you restore a Layerstate which contains no replacement in color or linetype but only Frozen in current viewport, the last layerstate restored is ALSO restored in the Model tab.
3rd test
To verify, close and re-open the DWG, switch to Model tab, go to LayerstateManager select TEST1 button Modify change color of Calque1 to Red (instead the same color by re-specify it), do the same think in layerstate TEST2. launch the code => It's OK
So the bug appears when you are in Model tab and when minimum one layerstate restored contains only Frozen in CurrentViewport modification.
I've reproduced this bug in MAP 3D 2008, 2010, 2012 and 2015. I haven't tested in vanilla AutoCAD but i think it's the same behaviour.
Olivier