Using the VB.NET example code on exchange.autodesk.com for Defining a User Coordinate System, I pretty much copy and pasted the code to suit my needs. While the example shows how to create a UCS origin, X Axis and Y Axis from scratch, I am attempting to simply cast the values from the system variables UCSORG, UCSXDIR and UCSYDIR. These system variables are read only, and when looking into the types, I believe they are all 3D Point type values.
I'm experiencing errors when trying to run the following code, and it's making me wonder whether what I'm attempting to do is possible. The JIT Debugger is pointing out a System.InvalidCastException. Is there a problem with setting a Point3D value (from the system variable) to a Vector3D type (which is required for defining a new ucs)? Does anybody know of a way around this?
Public Sub saveCurrentUcs()
'get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
'start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'get the current ucs system variables
Dim curOrigin As Point3d = Application.GetSystemVariable("UCSORG")
Dim curXdir As Vector3d = Application.GetSystemVariable("UCSXDIR")
Dim curYdir As Vector3d = Application.GetSystemVariable("UCSYDIR")
'open the ucs table for read
Dim acUcsTbl As UcsTable = acTrans.GetObject(acCurDb.UcsTableId, OpenMode.ForRead)
Dim acUcsTblRec As UcsTableRecord
'check to see if the proposed ucs already exists
If acUcsTbl.Has("DESIGN") = False Then
'define the new record
acUcsTblRec = New UcsTableRecord()
acUcsTblRec.Name = "DESIGN"
'open the table for write
acUcsTbl.UpgradeOpen()
'add the new record
acUcsTbl.Add(acUcsTblRec)
acTrans.AddNewlyCreatedDBObject(acUcsTblRec, True)
Else
acUcsTblRec = acTrans.GetObject(acUcsTbl("DESIGN"), OpenMode.ForWrite)
End If
'save the current ucs variables to the design ucs
acUcsTblRec.Origin = curOrigin
acUcsTblRec.XAxis = curXdir
acUcsTblRec.YAxis = curYdir
'open the active viewport
Dim acVportTblRec As ViewportTableRecord
acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, OpenMode.ForWrite)
'set the design ucs as 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)
acDoc.Editor.WriteMessage(vbLf & "Current UCS set to: " & acUcsTblRecActive.Name)
'commit and dispose of the transaction
acTrans.Commit()
End Using
End Sub
Thanks in advance,
Drew
Solved! Go to Solution.
Solved by architectOfIdeas. Go to Solution.
Ha... yeah...
To answer my own question; I wasn't grabbing the system variables properly!
Find and replace:
Dim curOrigin As Point3d = Application.GetSystemVariable("UCSORG")
Dim curXdir As Vector3d = Application.GetSystemVariable("UCSXDIR")
Dim curYdir As Vector3d = Application.GetSystemVariable("UCSYDIR")
With
Dim curOrigin As Point3d = acCurDb.Ucsorg
Dim curXdir As Vector3d = acCurDb.Ucsxdir
Dim curYdir As Vector3d = acCurDb.Ucsydir
If only I could give Kudos to myself... 😐