.NET

Reply
Valued Contributor
architectOfIdeas
Posts: 72
Registered: ‎08-10-2007
Message 1 of 2 (281 Views)
Accepted Solution

Saving UCS Using Current System Variables

281 Views, 1 Replies
12-12-2012 11:54 AM

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

Valued Contributor
architectOfIdeas
Posts: 72
Registered: ‎08-10-2007
Message 2 of 2 (267 Views)

Re: Saving UCS Using Current System Variables

12-12-2012 01:48 PM in reply to: architectOfIdeas

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... :smileyindifferent:

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community