Here is the full code with declaration.
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.