So I started to go down the direction of accessing the VP Color via this method.
The following code is my massively large approach to trying to change the VP Color for a Viewport on specified Layout.
I first start by gathering the layers that I want to remain Red.
I then attempt to get the Viewport in a Layout so I can try to change the VP Layer in that viewport for all the layers with a specified string that is not in my list of gathered Layers. I want to change the VP Colors for those layers to Cyan.
I ran some testing by going into a drawing, creating some DeltaBlocks, and linked them to a cloud. This then created two layers both for the DeltaBlock and the Cloud. I then went into a layout, clicked in the viewport, and changed the VP Layer for the DeltaBlock to Magenta. Going back into ModelSpace and it was still red.
Then I ran my code, and the Color for that deltablock was red, and the suspected "VP Color" I pulled was still also red. I was expecting for it to be magenta since I believe I have accessed the VP Color via the GetViewportOverrides using the Viewport I have pulled from the Layout. So I was expecting for the second output to be Magenta.
private void ViewportLayerHandling()
{
List<string> LayersToKeep = new List<string>();
DB_Sheet Sheet;
LayerTable layerTable;
ObjectId ViewportID;
foreach (DataGridViewRow Row in PlanSheetsAvailableTable.Rows)
{
LayersToKeep.Clear();
// Gather the Layers to Keep
// Go through the Deltas
for (int i = 0; i < DeltaSheets.Length; i++)
{
Sheet = DeltaSheets[i].Item2;
// Get the Deltas only for the RSSheet
if (Sheet.sheetNumber == Row.Cells[SheetNumColumnIndex].Value.ToString())
{
DB_Delta Delta = DeltaSheets[i].Item1;
string Handle = Delta.handle;
string FilePath = $"{Sheet.fullPath}\\{Sheet.fileName}";
Database db = OpenDrawingFile(FilePath); // False = Project is opened in the background
ObjectId DeltaBlock = GetBlockIdByHandle(db, Handle);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Pull its BlockLayer
BlockReference Block = tr.GetObject(DeltaBlock, OpenMode.ForRead) as BlockReference;
layerTable = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
LayerTableRecord BlockLayer = tr.GetObject(Block.LayerId, OpenMode.ForWrite) as LayerTableRecord;
Debug.Print($"Block Name:{Block.Name}");
Debug.Print($"Block Layer Name:{BlockLayer.Name}");
// Add it to the list
LayersToKeep.Add(BlockLayer.Name);
Debug.Print($"Block Layer:{BlockLayer.Color}");
try
{
// Get the Cloud Layer and also add it to the list
ObjectId CloudLayerId = layerTable[$"{BlockLayer.Name}-Cloud"];
LayerTableRecord CloudLayer = tr.GetObject(CloudLayerId, OpenMode.ForWrite) as LayerTableRecord;
Debug.Print($"Cloud Layer Name:{CloudLayer.Name}");
LayersToKeep.Add(CloudLayer.Name);
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
Debug.Print($"LayerNotFound:{ex.Message}");
}
}
}
}
// Go through the Drawing, and for every Delta that is not in the List
foreach (string LayerName in LayersToKeep)
{
Debug.Print($"LayerName in List:{LayerName}");
}
Debug.Print("-------------------------------------------------");
// Get the Document
Sheets.TryGetValue(Row.Cells[SheetNumColumnIndex].Value.ToString(), out Sheet);
// Get the Deltas only for the RSSheet
if (Sheet.sheetNumber == Row.Cells[SheetNumColumnIndex].Value.ToString())
{
// Open the drawing
string FilePath = $"{Sheet.fullPath}\\{Sheet.fileName}";
Database db = OpenDrawingFile(FilePath); // False = Project is opened in the background
ViewportID = db.ViewportTableId;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Open the Layout Dictionary
DBDictionary LayoutList = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
// Go through the Layouts
foreach (DBDictionaryEntry layoutId in LayoutList)
{
// Get the Layout
Layout layout = tr.GetObject(layoutId.Value, OpenMode.ForRead) as Layout;
// When the Layout is correct
if (layout.LayoutName == Row.Cells[SheetNumColumnIndex].Value.ToString())
{
// Get the Layouts's BlockTableRecord
BlockTableRecord LayoutBlockTableRecord = tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord;
// Go through the Sheet BlockTable
foreach (ObjectId ID in LayoutBlockTableRecord)
{
// Try to pull the Viewport from the Layout BlockTable
Viewport vp = tr.GetObject(ID, OpenMode.ForRead) as Viewport;
if (vp != null)
{
// This is believed to be a Viewport in a Sepcified Layout
Debug.Print($"vp.BlockName:{vp.BlockName}");
Debug.Print($"vp.BlockId:{vp.BlockId}");
// Save the ID for the suspected Viewport
ViewportID = vp.BlockId;
}
}
}
}
// Get the LayerTable
layerTable = tr.GetObject(db.LayerTableId, OpenMode.ForRead)as LayerTable;
// Go through the layers
SymbolTableEnumerator layerEnumerator = layerTable.GetEnumerator();
while (layerEnumerator.MoveNext())
{
// Get the layer
LayerTableRecord currentLayer = tr.GetObject(layerEnumerator.Current, OpenMode.ForRead) as LayerTableRecord;
// Check the Layer by the Name
if (currentLayer.Name.Contains("Delta$-") && !LayersToKeep.Contains(currentLayer.Name))
{
// Access the Data on the Layer
Debug.Print($"CurrentLayerName:{currentLayer.Name}");
Debug.Print($"CurrentLayerColor:{currentLayer.Color}");
Debug.Print($"CurrentLayerVPColor:{currentLayer.GetViewportOverrides(ViewportID).Color}");
//currentLayer.GetViewportOverrides(ViewportID).Color = Autodesk.AutoCAD.Colors.Color.FromRgb(0,255,255);
}
}
}
}
}
}
*Moderator edit* Changed code window language to C#.