.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Viewport Layer Control?

3 REPLIES 3
Reply
Message 1 of 4
rjhinton
2151 Views, 3 Replies

Viewport Layer Control?

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.
3 REPLIES 3
Message 2 of 4
_gile
in reply to: rjhinton

Hi,

The Viewport class provides a FreezeLayersInViewport method...


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 4
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 4
rjhinton
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, 😎
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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost