.NET

Reply
Member
rjhinton
Posts: 5
Registered: ‎01-29-2010
Message 1 of 4 (800 Views)

Viewport Layer Control?

800 Views, 3 Replies
01-29-2010 11:04 AM
I would like create a new layer and freeze it in all existing layout viewports. I've searched high and low for a vb.net example and have not had any success. If any of you good people could shed some light on how viewport layers are controlled I would be very thankful.
*Expert Elite*
_gile
Posts: 2,078
Registered: ‎04-29-2006
Message 2 of 4 (800 Views)

Re: Viewport Layer Control?

01-29-2010 11:38 AM in reply to: rjhinton
Hi,

The Viewport class provides a FreezeLayersInViewport method...
Gilles Chanteau
*Expert Elite*
_gile
Posts: 2,078
Registered: ‎04-29-2006
Message 3 of 4 (800 Views)

Re: Viewport Layer Control?

01-29-2010 02:00 PM in reply to: rjhinton
Here's a little C# sample (if needed, google for convert C# VB")

{code}using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

namespace ClassLibrary1
{
public class Class1
{
[CommandMethod("TEST")]
public static void Test()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Create a new layer named "Test" color 30
LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
if (!lt.Has("Test"))
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 30);
ltr.Name = "Test";
ObjectId layerId = lt.Add(ltr);
tr.AddNewlyCreatedDBObject(ltr, true);
}

// Freeze "Test" layer in all viewports
ObjectId[] ids = new ObjectId[1] { layerId };
DBDictionary layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
foreach (DBDictionaryEntry entry in layoutDict)
{
if (entry.Key != "Model")
{
Layout lay = (Layout)tr.GetObject(entry.Value, OpenMode.ForRead);
foreach (ObjectId vpId in lay.GetViewports())
{
Viewport vp = (Viewport)tr.GetObject(vpId, OpenMode.ForWrite);
vp.FreezeLayersInViewport(ids.GetEnumerator());
}
}
}

tr.Commit();
}
}
}
}
{code}
Gilles Chanteau
Member
rjhinton
Posts: 5
Registered: ‎01-29-2010
Message 4 of 4 (800 Views)

Re: Viewport Layer Control?

02-01-2010 09:47 AM in reply to: rjhinton
Very useful. Your code has gotten me along way to completing my task. I'm having an issue running my version of your code from a paletteset. I've created a Class Vport with a New() subroutine that creates the layers and a FreezeLayersInAllLayouts() subroutine that freezes the newly created layers in all viewport but the current. A button on the paletteset creates a New Vport class object then freezes the layer then draw some entities in the current viewport. If I step through FreezeLayersInAllLayouts() in the debugger all works fine. If I run FreezeLayersInAllLayouts() without debugging the routine has no effect on the layer state in all the other viewports. See the following code for my adaptation of your code.

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Colors
Imports Autodesk.AutoCAD.Geometry

Public Class Vport
Private CVport As Viewport
Private CVportName As String
Private LayerIds(2) As ObjectId

Public Sub New()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Using DocLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument()
Using tr As Transaction = db.TransactionManager.StartTransaction()
CVport = DirectCast(tr.GetObject(ed.CurrentViewportObjectId, OpenMode.ForRead), Viewport)
Dim lt As LayerTable = DirectCast(tr.GetObject(db.LayerTableId, OpenMode.ForWrite), LayerTable)
Dim layerid As ObjectId
If Not lt.Has("PH-" + CVport.Handle.ToString()) Then
Dim ltr As LayerTableRecord = New LayerTableRecord()
ltr.Name = "PH-" + CVport.Handle.ToString()
ltr.Color = Color.FromColorIndex(ColorMethod.ByColor, 8)
ltr.ViewportVisibilityDefault = True
layerid = lt.Add(ltr)
LayerIds(0) = layerid
tr.AddNewlyCreatedDBObject(ltr, True)
End If
If Not lt.Has("PT-" + CVport.Handle.ToString()) Then
Dim ltr As LayerTableRecord = New LayerTableRecord()
ltr.Name = "PT-" + CVport.Handle.ToString()
ltr.Color = Color.FromColorIndex(ColorMethod.ByColor, 6)
ltr.ViewportVisibilityDefault = True
layerid = lt.Add(ltr)
LayerIds(1) = layerid
tr.AddNewlyCreatedDBObject(ltr, True)
End If
If Not lt.Has("PV-" + CVport.Handle.ToString()) Then
Dim ltr As LayerTableRecord = New LayerTableRecord()
ltr.Name = "PV-" + CVport.Handle.ToString()
ltr.Color = Color.FromColorIndex(ColorMethod.ByColor, 3)
ltr.ViewportVisibilityDefault = True
layerid = lt.Add(ltr)
LayerIds(2) = layerid
tr.AddNewlyCreatedDBObject(ltr, True)
End If
tr.Commit()
End Using
End Using
End Sub

Public Sub FreezeLayersInAllLayouts()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Using DocLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument()
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim layoutdict As DBDictionary = DirectCast(tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead, False, True), DBDictionary)
For Each entry As DBDictionaryEntry In layoutdict
If entry.Key <> "Model" Then
Dim lay As Layout = DirectCast(tr.GetObject(entry.Value, OpenMode.ForRead, False, True), Layout)
For Each vpid As ObjectId In lay.GetViewports()
Dim vp As Viewport = DirectCast(tr.GetObject(vpid, OpenMode.ForWrite, False, True), Viewport)
If (vp.Handle <> CVport.Handle) Then
vp.FreezeLayersInViewport(LayerIds.GetEnumerator())
End If
Next
End If
Next
tr.Commit()
End Using
End Using
End Sub

Public Sub DrawTimbers(ByVal SS As SelectionSet)
...
End Sub

End Class

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community