Author Topic: Set UCS in a viewport or paperspace  (Read 7375 times)

0 Members and 1 Guest are viewing this topic.

Brent Burgess

  • Guest
Set UCS in a viewport or paperspace
« on: April 05, 2010, 11:24:12 PM »
I have some code that will set a ucs in Modelspace, but when I try to use it through a viewport, or paperspace, it errors on me "Unable to cast object of type 'Autodesk.AutoCAD.DatabaseServices.Viewport' to type 'Autodesk.AutoCAD.DatabaseServices.ViewportTableRecord'"

How do I go about setting a ucs in pspace or vport?

Code Below

        Using dlock As DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument
            Try

                Using tr As Transaction = db.TransactionManager.StartTransaction
                    Using ucst As UcsTable = tr.GetObject(db.UcsTableId, OpenMode.ForRead)

                        If Not ucst.Has(myUCSName) Then
                            ucstr = New UcsTableRecord
                            ucstr.Name = myUCSName

                            ucst.UpgradeOpen()
                            ucst.Add(ucstr)
                            tm.AddNewlyCreatedDBObject(ucstr, True)

                        Else
                            ucstr = tr.GetObject(ucst(myUCSName), OpenMode.ForWrite)

                        End If

                        ucstr.Origin = myOrigin
                        ucstr.XAxis = XAxis
                        ucstr.YAxis = YAxis
                        If UpdateViewport Then
                            Debug.Print(doc.Editor.ActiveViewportId.ToString)
                            Dim vport As ViewportTableRecord = tr.GetObject(doc.Editor.ActiveViewportId, OpenMode.ForWrite)
                            vport.SetUcs(ucstr.ObjectId)
                            doc.Editor.UpdateTiledViewportsFromDatabase()
                        End If

                        tr.Commit()
                    End Using
                End Using
            Catch ex As System.Exception
                Debug.Print(ex.Message & ControlChars.CrLf & ex.StackTrace, MsgBoxStyle.Critical, "Exception")
                MsgBox(ex.Message & ControlChars.CrLf & ex.StackTrace, MsgBoxStyle.Critical, "Exception")
            End Try
        End Using

Thanks in advance

Brent

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Set UCS in a viewport or paperspace
« Reply #1 on: April 06, 2010, 06:44:02 AM »

How about giving us the full Method you're using including parameters.

It may help you solve this yourself if you identify the statement that the is causing the error.

your badly named variable vport here is misleading ;
Dim vport As ViewportTableRecord = ..... 

.. I can't identify where you're using a Viewport Class.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Set UCS in a viewport or paperspace
« Reply #2 on: April 06, 2010, 10:05:01 AM »
doc.Editor.ActiveViewportId is a viewport not a ViewportTableRecord

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Set UCS in a viewport or paperspace
« Reply #3 on: April 06, 2010, 06:31:32 PM »
Can someone who uses VB.net please confirm that a cast is NOT needed please.

From the sample code for VB ( on which the original post seems to be based) ... NO Casting
Code: [Select]
     '' Open the active viewport      
      Dim acVportTblRec As ViewportTableRecord      
      acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, _                                        
       OpenMode.ForWrite)      
      
      '' Display the UCS Icon at the origin of the current viewport      
      acVportTblRec.IconAtOrigin = True      
      acVportTblRec.IconEnabled = True
            
      '' Set the UCS current      
      acVportTblRec.SetUcs(acUCSTblRec.ObjectId)      
      acDoc.Editor.UpdateTiledViewportsFromDatabase()      
      
      '' Display the name of the current UCS      
      Dim acUCSTblRecActive As UcsTableRecord      
      acUCSTblRecActive = acTrans.GetObject(acVportTblRec.UcsName, _                                            
                                                    OpenMode.ForRead)      
      
      Application.ShowAlertDialog("The current UCS is: " & _                                  
                                              acUCSTblRecActive.Name)  


from the Sample Code for C# ...  cast as ViewportTableRecord  and cast as UcsTableRecord

Code: [Select]
     // Open the active viewport
      ViewportTableRecord acVportTblRec;
      acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId,
                                              OpenMode.ForWrite)
                                              as ViewportTableRecord;
                                                          
      // Display the UCS Icon at the origin of the current viewport      
      acVportTblRec.IconAtOrigin = true;      
      acVportTblRec.IconEnabled = true;
            
      // Set the UCS current      
      acVportTblRec.SetUcs(acUCSTblRec.ObjectId);      
      acDoc.Editor.UpdateTiledViewportsFromDatabase();
            
      // Display the name of the current UCS      
      UcsTableRecord acUCSTblRecActive;      
      acUCSTblRecActive = acTrans.GetObject(acVportTblRec.UcsName,                                            
                                               OpenMode.ForRead)
                                               as UcsTableRecord;      
      
      Application.ShowAlertDialog("The current UCS is: " +
                                        acUCSTblRecActive.Name);


Why is VB hiding the necessity to cast the type ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Brent Burgess

  • Guest
Re: Set UCS in a viewport or paperspace
« Reply #4 on: April 06, 2010, 09:27:43 PM »
Here is the full code with declaration.

Code: [Select]
Public Sub SetUCS(ByVal UpdateViewport As Boolean)
        Dim doc As Document = ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = AcApp.DocumentManager.MdiActiveDocument.Editor
        Dim mat As Matrix3d = ed.CurrentUserCoordinateSystem

        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim tm As DatabaseServices.TransactionManager = db.TransactionManager

        Dim myUCSName As String = myProject.UCS.Description(lvwIndex)
        Dim myOrigin As New Point3d(myProject.UCS.OriginX(lvwIndex), myProject.UCS.OriginY(lvwIndex), myProject.UCS.OriginZ(lvwIndex))
        Dim UCSOriginPoint As New Vector3d(myOrigin.X, myOrigin.Y, myOrigin.Z)
        Dim DblRotation As Double = myProject.UCS.Rotation(lvwIndex) * Math.PI / 180
        Dim XAxis As New Vector3d(1 * Math.Cos(DblRotation), 1 * Math.Sin(DblRotation), 0)
        Dim YAxis As New Vector3d(1 * Math.Sin(-1 * DblRotation), 1 * Math.Cos(DblRotation), 0)
        Dim ucstr As UcsTableRecord

        Using dlock As DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument
            Try

                Using tr As Transaction = db.TransactionManager.StartTransaction
                    Using ucst As UcsTable = tr.GetObject(db.UcsTableId, OpenMode.ForRead)

                        If Not ucst.Has(myUCSName) Then
                            ucstr = New UcsTableRecord
                            ucstr.Name = myUCSName

                            ucst.UpgradeOpen()
                            ucst.Add(ucstr)
                            tm.AddNewlyCreatedDBObject(ucstr, True)

                        Else
                            ucstr = tr.GetObject(ucst(myUCSName), OpenMode.ForWrite)

                        End If

                        ucstr.Origin = myOrigin
                        ucstr.XAxis = XAxis
                        ucstr.YAxis = YAxis
                        If UpdateViewport Then
                            Debug.Print(doc.Editor.ActiveViewportId.ToString)
                            Dim vport As ViewportTableRecord = tr.GetObject(doc.Editor.ActiveViewportId, OpenMode.ForWrite)
                            vport.SetUcs(ucstr.ObjectId)
                            doc.Editor.UpdateTiledViewportsFromDatabase()
                        End If

                        tr.Commit()
                    End Using
                End Using
            Catch ex As System.Exception
                MsgBox(ex.Message & ControlChars.CrLf & ex.StackTrace, MsgBoxStyle.Critical, "Exception")
            End Try
        End Using
    End Sub

I have only started experimenting with UCS, so my input may be limited - Your assumption is correct though, I did base the code on the sample code from AutoCAD .NET Developers Guide.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Set UCS in a viewport or paperspace
« Reply #5 on: April 06, 2010, 10:40:05 PM »

I don't have Visual Studio available at the moment so can't test.

Can you test it in debug mode with the 'Locals' window open Step through the code and see the value and type of your variables.

Does it error on the  statement
Dim vport As ViewportTableRecord = tr.GetObject(doc.Editor.ActiveViewportId, OpenMode.ForWrite) ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Brent Burgess

  • Guest
Re: Set UCS in a viewport or paperspace
« Reply #6 on: April 07, 2010, 12:33:05 AM »
Yeah - that is where it errors - I can see now the comment you made about the ViewportTableRecord and ViewportTableID

Ken Alexander

  • Newt
  • Posts: 61
Re: Set UCS in a viewport or paperspace
« Reply #7 on: April 09, 2010, 11:19:17 AM »
Quote
Why is VB hiding the necessity to cast the type ??

Option Strict must be off.
Ken Alexander

PATIPTON

  • Guest
Re: Set UCS in a viewport or paperspace
« Reply #8 on: September 14, 2011, 04:36:03 PM »
Was there an answer to this question? Just trying to set to WCS then back to UCS.

I'm getting the same errors with this code:

Sub World()
        Dim lDrawing As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim Db As Database = lDrawing.Database
        Dim ed As Editor = lDrawing.Editor

        'Set to WORLD Coordinate System and back.
        Using LockDoc As DocumentLock = lDrawing.LockDocument
            Using acTrans As Transaction = Db.TransactionManager.StartTransaction()
                Dim acVpT As ViewportTable
                acVPT = acTrans.GetObject(Db.ViewportTableId, OpenMode.ForWrite)

                Dim acVpTRec As ViewportTableRecord

                'I have tried the following lines. All give the same error.
                acVpTRec = acTrans.GetObject(ed.ActiveViewportId, OpenMode.ForWrite)
                'acVpTRec = acTrans.GetObject(ed.CurrentViewportObjectId, OpenMode.ForWrite)
                'acVpTRec = CType(acTrans.GetObject(ed.ActiveViewportId, OpenMode.ForWrite), ViewportTableRecord)
                'acVpTRec = CType(acTrans.GetObject(ed.CurrentViewportObjectId, OpenMode.ForWrite), ViewportTableRecord)
                'acVpTRec = CType(acTrans.GetObject(ed.ActiveViewportId, OpenMode.ForWrite), Autodesk.AutoCAD.DatabaseServices.ViewportTableRecord)


                'grab current User Coordinate System.
                Dim VpID As Autodesk.AutoCAD.DatabaseServices.ObjectId
                VpID = ed.ActiveViewportId

                'Set to World
                acVpTRec.SetUcsToWorld()
                'return to User
                acVpTRec.SetUcs(VpID)
            End Using
        End Using
    End Sub

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Set UCS in a viewport or paperspace
« Reply #9 on: September 14, 2011, 04:56:33 PM »
Welcome to the Swamp!!
 
 
Quote

Dim acVpT As ViewportTable

Try changing acVpT type from ViewportTable to ViewPort

****************************EDIT******************

Meant

Quote
Dim acVpTRec As ViewportTableRecord

Try changing acVpTRec type from ViewportTableRecord to ViewPort
« Last Edit: September 14, 2011, 05:01:39 PM by Jeff H »

PATIPTON

  • Guest
Re: Set UCS in a viewport or paperspace
« Reply #10 on: September 14, 2011, 05:04:41 PM »
Changed to Dim acVpT as Viewport.

This produces an error: "Unable to cast object of type 'Autodesk.AutoCAD.DatabaseServices.ViewportTable' to type 'Autodesk.AutoCAD.DatabaseServices.Viewport'. "

at this line:
acVpT = acTrans.GetObject(Db.ViewportTableId, OpenMode.ForWrite)

Which is similar to my original problem at line:
acVpTRec = acTrans.GetObject(ed.ActiveViewportId, OpenMode.ForWrite)



Jeff H

  • Needs a day job
  • Posts: 6150
Re: Set UCS in a viewport or paperspace
« Reply #11 on: September 14, 2011, 05:05:29 PM »
Sorry copied and pasted wrong line
See edit below

PATIPTON

  • Guest
Re: Set UCS in a viewport or paperspace
« Reply #12 on: September 14, 2011, 05:08:41 PM »
ok -  stepped through by changing the ViewportTableRecord to Viewport.

Thanks!!

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Set UCS in a viewport or paperspace
« Reply #13 on: September 14, 2011, 05:21:35 PM »
No problem,
 and I also missed this the first time looking it over
 
doc.Editor.ActiveViewportId is a viewport not a ViewportTableRecord

PATIPTON

  • Guest
Re: Set UCS in a viewport or paperspace
« Reply #14 on: September 15, 2011, 10:18:05 AM »
"doc.Editor.ActiveViewportId is a viewport not a ViewportTableRecord"

For what it's worth,  I was trying to follow an example in the AutoCAD 2011 help which produces the same error.
http://docs.autodesk.com/ACD/2011/ENU/filesMDG/WS1a9193826455f5ff2566ffd511ff6f8c7ca-42f6.htm

Dim acVportTblRec As ViewportTableRecord      acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, OpenMode.ForWrite)