Here are the two functions I am using, I have collected everything on the internet to be able to solve my problem, but still to no avail. Please help me, I have been suffering in this problem for about a month.
The getActiveUcs function checks to see if there is a named ucs, if not it creates a named ucs from the unnamed ucs.
The getUCS function applies the first function to the required viewport.
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Public Class Test
<CommandMethod("GetUCS", CommandFlags.NoTileMode)> _
Public Sub GetUCS_PaperSpace()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Dim peo As New PromptEntityOptions("Select a viewport : ")
peo.SetRejectMessage("Select a viewport.")
peo.AddAllowedClass(GetType(Autodesk.AutoCAD.DatabaseServices.Viewport), True)
Dim per As PromptEntityResult = ed.GetEntity(peo)
If per.Status <> PromptStatus.OK Then
Return
End If
Dim vpId As ObjectId = per.ObjectId
Dim acadDoc As AcadDocument = Autodesk.AutoCAD.ApplicationServices.DocumentExtension.GetAcadDocument(doc)
acadDoc.MSpace = True
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim vp As Autodesk.AutoCAD.DatabaseServices.Viewport = TryCast(tr.GetObject(vpId, OpenMode.ForRead), Autodesk.AutoCAD.DatabaseServices.Viewport)
acadDoc.ActivePViewport = vp.AcadObject
Dim ucs As AcadUCS = getActiveUcs()
ed.WriteMessage([String].Format(vbLf & "Origin : {0}", ucs.ToString))
Dim ucsMat As Matrix3d = ed.CurrentUserCoordinateSystem
Dim cs As CoordinateSystem3d = ucsMat.CoordinateSystem3d
ed.WriteMessage([String].Format(vbLf & "Origin : {0}", cs.Origin))
ed.WriteMessage([String].Format(vbLf & "X Vec : {0}", cs.Xaxis))
ed.WriteMessage([String].Format(vbLf & "Y Vec : {0}", cs.Yaxis))
tr.Commit()
End Using
acadDoc.MSpace = False
End Sub
Function getActiveUcs() As AcadUCS
' get the active UCS
' if the UCS is not saved, save it
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ThisDrawing As AcadDocument = Autodesk.AutoCAD.ApplicationServices.DocumentExtension.GetAcadDocument(doc)
Dim ed As Editor = doc.Editor
Dim Origin
Dim xaxis
Dim yaxis
Dim Zero(0 To 2) As Double
Zero(0) = 0 : Zero(1) = 0 : Zero(2) = 0
Dim currentUCSName As String
currentUCSName = ThisDrawing.GetVariable("UCSNAME")
If currentUCSName = "" Then
' Current UCS is not saved so get the data and save it
' A ucs is saved when a user makes and saves one or
' a user clicks on an isoview button
If ThisDrawing.GetVariable("WORLDUCS") = 1 Then
' active UCS is identical to WCS
xaxis = Zero : yaxis = Zero
xaxis(0) = 1 : yaxis(1) = 1
getActiveUcs = ThisDrawing.UserCoordinateSystems.Add(Zero, xaxis, yaxis, "World")
Else
Origin = ThisDrawing.GetVariable("UCSORG")
xaxis = ThisDrawing.GetVariable("UCSXDIR")
yaxis = ThisDrawing.GetVariable("UCSYDIR")
getActiveUcs = ThisDrawing.UserCoordinateSystems.Add(Zero, xaxis, yaxis, "Active")
'Changing the origin later stops the error message
'-2145320930 UCS X axis and Y axis are not perpendicular
getActiveUcs.Origin = Origin
ThisDrawing.ActiveUCS = getActiveUcs
End If
Else
getActiveUcs = ThisDrawing.UserCoordinateSystems.Item(currentUCSName)
End If
End Function
End Class
This code doesnt work for the first time it is applied, but if you do the function again it works!!!!!!
I am not able to figure out the problem, any help is much appreciated.