• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Mentor
    Dull_Blades
    Posts: 231
    Registered: ‎06-25-2007
    Accepted Solution

    Setting PS Viewport Named View

    256 Views, 5 Replies
    01-05-2012 03:30 PM

    Hi All,

     

    I have code that has the user select a viewport.  I then need to set the selected viewport to an existing view, but I cannot find any exampes of this.

     

    A couple of things that are confusing me...

     

    I have the viewport, but how do I get the ViewPortTable/ViewPortTableRecord from it?

     

    Do I need to set the view in the ViewPortTableRecord od the ViewTableRecord?

     

    No problems to this point...

     

    'Code snip
    If acPrmpt_res.Status = PromptStatus.OK Then
    
                        Dim acVPort As Viewport = Nothing
                        Dim acViewTbl As ViewTable
                        Dim acViewTblrec As ViewTableRecord
                        Dim acVPortTbl As ViewportTable
                        Dim acVPortTblRec As ViewportTableRecord
                        Dim acEnt As Entity = Nothing
    
                        For Each acObjId As ObjectId In acPrmpt_res.Value.GetObjectIds
                            acEnt = acObjId.GetObject(OpenMode.ForRead)
                            If TypeOf acEnt Is Viewport Then
    
                                acVPort = acObjId.GetObject(OpenMode.ForRead)
                                acVPort.Highlight()
    
                                'Set selected viewport named view here...
    
                            End If
                        Next
    'Code snip

     Any help would be greatly appreciated!

    Please use plain text.
    Valued Mentor
    Posts: 306
    Registered: ‎03-31-2005

    Re: Setting PS Viewport Named View

    01-06-2012 08:20 AM in reply to: Dull_Blades

    What I used was set layoutmanager.currentlayout to the name of the layout.  Then I forced the the viewport to be active using setsystemvariable("tilemode",0).  Note that this worked for me because there is only 1 viewport in each of the layouts.  If there was more than 1 viewport I logged an error and fixed it with other methods.  The example in the dev guide uses the cvport and tilemode systemvariables, and then doc.editor.switchtomodelspace.

     

    Once the viewport is active, you can use something like "dim myviewtr as viewtablerecord = doc.editor.getcurrentview.

     

    The dev guide info: 

    http://docs.autodesk.com/ACD/2011/ENU/filesMDG/WS1a9193826455f5ff2566ffd511ff6f8c7ca-3482.htm

     

    It seems to me that there should be a better way.

    Please use plain text.
    Mentor
    Dull_Blades
    Posts: 231
    Registered: ‎06-25-2007

    Re: Setting PS Viewport Named View

    01-06-2012 03:50 PM in reply to: fieldguy

    I am very close now, but still having some issues...

     

    I am able to get the view to change with the following code:

     

    For Each acObjId As ObjectId In acPrmpt_res.Value.GetObjectIds
                            acEnt = acObjId.GetObject(OpenMode.ForRead)
                            If TypeOf acEnt Is Viewport Then
    
                                acVPort = acObjId.GetObject(OpenMode.ForWrite)
    
                                Dim acVPNum As IntPtr = acVPort.Number
                                
                                '' Make selected viewport the current viewport
                                acedSetCurrentVPort(acVPort.UnmanagedObject)
                               
                                '' Activate model space in the viewport
                                acDoc.Editor.SwitchToModelSpace()
    
                                '' Get view table
                                acViewTbl = acTrans.GetObject(acCurDb.ViewTableId, OpenMode.ForRead)
                                acViewTblrec = acTrans.GetObject(acViewTbl(strViewName), OpenMode.ForWrite)
    
                                '' Set current view
                                acEd.SetCurrentView(acViewTblrec)

    Works great with one exception... it's only changing the view in the last viewport that was activated and NOT in the viewport that was selected (acVport).  I thought I was activating (or seting current) the selected viewport with the acedSetCurrentVPort function, but it does not change the viewport.

     

    If I try and pass the acVport.number as the value of acedSetCurrentVPort, I get an error:  "Attempting to read or write protected memory..."

     

    All I have left to figure out is how to change the current viewport to the one that was selected.

     

    Any ideas what I am doing wrong?

    Please use plain text.
    Valued Mentor
    Posts: 330
    Registered: ‎05-11-2006

    Re: Setting PS Viewport Named View

    01-08-2012 06:29 PM in reply to: Dull_Blades

    This process should work for you:

     

    acVPort = acObjId.GetObject(OpenMode.ForWrite)

    ...

     

    acDoc.Editor.SwitchToModelSpace()

    Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CVPORT", acVPort.Number)

     

    'Do something with active viewport here...

     

    'Switch back to paper space...

    acDoc.Editor.SwitchToPaperSpace()

     

     

    Also would be a good idea to check the 'Viewport.On' and 'Viewport.Locked' properties.

    Please use plain text.
    Mentor
    Dull_Blades
    Posts: 231
    Registered: ‎06-25-2007

    Re: Setting PS Viewport Named View

    01-10-2012 10:14 AM in reply to: cadMeUp

    Thanks CadMeUp... that worked great!

     

    Why is it you cannot not set the viewport current prior to switching to modelspace?  I tried it, but no luck.  Just a curiosity as one of the unselected viewports will highlight as activated first (due to the acDoc.Editor.SwitchToModelSpace() function) before the correct viewport is set with .SetSystemVariable("CVPORT", acVPort.Number).  Some users will inevitably get confused by this when seeing a viewport they did not select "highlight".

     

    Thanks again!

    Please use plain text.
    Valued Mentor
    Posts: 330
    Registered: ‎05-11-2006

    Re: Setting PS Viewport Named View

    01-12-2012 07:06 AM in reply to: Dull_Blades

    I know, it seems to work backwards and it is confusing in code. The help doc for the CVPORT system variable should make sense of it though:

     

     

    CVPORT Snippet.JPG

     

     

     

     

    Please use plain text.