Hi All,
I met error "eNotAplicable" when I try to turn off object in viewports each. What happen?
Thanks,
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
@ditran7577 wrote:
Hi All,
I met error "eNotAplicable" when I try to turn off object in viewports each. What happen?
Thanks,
How you try to turn off object in viewports? Please provide your's code.
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"
Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Thanks for help.
Here is the code:
Public Sub OFFVIEWPORT() Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument Dim acCurDb As Database = acDoc.Database Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction() Dim lays As DBDictionary = acTrans.GetObject(acCurDb.LayoutDictionaryId, OpenMode.ForRead) For Each item As DBDictionaryEntry In lays If item.Key <> "Model" Then Dim lay As Layout = TryCast(acTrans.GetObject(item.Value, OpenMode.ForRead, False, True), Layout) For Each vpid As ObjectId In lay.GetViewports() Dim vp As Autodesk.AutoCAD.DatabaseServices.Viewport = TryCast(acTrans.GetObject(vpid, OpenMode.ForWrite, True, True), Autodesk.AutoCAD.DatabaseServices.Viewport) vp.On = False 'Error 'vp.on=true 'is OK Next End If Next acTrans.Commit() End Using End Sub
You may want to try the code like this:
For Each vpid As ObjectId In lay.GetViewports()
Dim vp As Autodesk.AutoCAD.DatabaseServices.Viewport = TryCast
''you cannot turn off viewport the layout itself is shown in it
If vp.Number<>1 Then
vp.On=False
End If
Next
Norman Yuan
Hi,
I changed the code but it did not solve the proplem. An error occurred while processing your guidance. At "Model space" error is: "eNotAplicable" or at "layout" error is "Autocad application has stopped working".
Thanks,
Well, I forgot to mention that if the layout is not active, all viewport's Number property would be -1 and you cannot turn it on or off. So, you need to set each layout as current before turn viewport on the layout on/off. Here is sample code that toggle viewports on one or more layouts on or off:
public class MyCommands { private static bool _vpOn = true; [CommandMethod("ToggleVP")] public static void RunToggleViewport() { _vpOn = !_vpOn; ToggleViewport(_vpOn); } private static void ToggleViewport(bool vpOn) { Document dwg = Application.DocumentManager.MdiActiveDocument; Editor ed = dwg.Editor; Database db = dwg.Database; LayoutManager lManager = LayoutManager.Current; string curLayout = lManager.CurrentLayout; try { using (Transaction tran = db.TransactionManager.StartTransaction()) { DBDictionary layDic = (DBDictionary)tran.GetObject(db.LayoutDictionaryId, OpenMode.ForRead); foreach (DBDictionaryEntry entry in layDic) { if (entry.Key.ToUpper() != "MODEL") { Layout layout = (Layout)tran.GetObject(entry.Value, OpenMode.ForRead); lManager.CurrentLayout = layout.LayoutName; ed.WriteMessage("\nLayout: {0}", layout.LayoutName); ObjectIdCollection vpIds = layout.GetViewports(); foreach (ObjectId id in vpIds) { Viewport vp = (Viewport)tran.GetObject(id, OpenMode.ForRead); ed.WriteMessage("\n =>Viewport number: {0}", vp.Number); if (vp.Number != 1) { vp.UpgradeOpen(); vp.On = vpOn; } } } } tran.Commit(); } } finally { if (lManager.CurrentLayout != curLayout) { lManager.CurrentLayout = curLayout; } } } }
Norman Yuan
Thanks for help,
It's work but the progress is too slow. It's takes some minutes to turn off all viewports of about 10 layouts. I think because Acad regen after switch layout each. Please give me a suggestion.
Thanks,
You may play around with "Layout Regen Options" ("Options" dialog box, tab "System"). Try select different radio button to run the code.
The 3 radion buttons are corresponding to the 3 values of system variable " LAYOUTREGENCTL" (0, 1, and 2). With the best result of your try, you can then set the system variable to the desired value before runnong your code to toggle viewports on layouts on and off, and then set the system variable back at the end of your code.
Norman Yuan
What code you need help? Do you mean how to set system variable "LAYOUTREGNCTL"? Something like this:
//Save current value of the system variable
object oldValue=Application.GetSysVariable("LAYOUTREGENCTL");
//Set the system's variable to 0, 1, or 2, whichever is the best for the following operation
Application.SetSysVariable("LAYOUTREGENCTL", 0);
//Turn on/off viewport on layout, as discussed in previous posts
......
//Afterward, restore the system variable back to its original value
Application.SetSysVariable("LAYOUTREGENCTL", oldValue);
Norman Yuan
Thanks for help. The process is not faster. I don'n know what happen?
Is there any way to turn off viewport each that we do not set the current layout to active layout?
Can't find what you're looking for? Ask the community or share your knowledge.