Author Topic: Get and change current view ....cont. tryng to move UCS  (Read 4058 times)

0 Members and 1 Guest are viewing this topic.

DBARANAS

  • Guest
Get and change current view ....cont. tryng to move UCS
« on: August 02, 2006, 12:10:48 AM »
I applied what I had learnt from that recent post to be able to adjust the viewport so that solids that are not drawn orthoganal can appear as though they are.

e.g. a solid wall drawn at some strange angle (building is rotated on its site or building has lots of angles) can be looked at in all the standard views and not appear twisted.

This is a bit of a strange feature and I use it to pull buildings apart to generate assembly drawings that are consistant.

Everything is working with the viewports. The last part of this is to untwist the UCS and move it to a desired location (xyz of the origin of a sub assembly.

I had all of this in VBA, the UCS part is tricky, I cluged it together from several posts in Randall Rath's old VBDesign group

old UCS VBA code:
******************************************
        With ThisDrawing
            sPnt = .Utility.polarPoint(pnt, 0, 0)
            xVector = .Utility.polarPoint(sPnt, twistang, 1)
            yVector = .Utility.polarPoint(sPnt, twistang + pi * 0.5, 1)
            Set twistUCS = .UserCoordinateSystems.add(sPnt, xVector, yVector, "ViewTwist")
            .SetVariable "UCSFOLLOW", 1    'turn on ucsfollow
            .ActiveUCS = twistUCS    'set the new twist ucs as active
            .SetVariable "UCSFOLLOW", 0    'turn off ucsfollow
            .SendCommand "ucs n view" & " "
        End With

******************************************

Working .net code************


    <DllImport("acad.exe", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="?acedVportTableRecords2Vports@@YA?AW4ErrorStatus@Acad@@XZ")> _
   Private Function acedVportTableRecords2Vports() As Boolean
    End Function

    <DllImport("acad.exe", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="?acedVports2VportTableRecords@@YA?AW4ErrorStatus@Acad@@XZ")> _
    Private Function acedVports2VportTableRecords() As Boolean
    End Function

    <DllImport("acad.exe", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="?acedSetCurrentView@@YA?AW4ErrorStatus@Acad@@PAVAcDbViewTableRecord@@PAVAcDbViewport@@@Z")> _
    Public Function acedSetCurrentView(ByVal viewTableRec As IntPtr, ByVal viewport As IntPtr) As IntPtr
    End Function

Imports acap = Autodesk.AutoCAD.ApplicationServices
Imports acdb = Autodesk.AutoCAD.DatabaseServices
Imports acge = Autodesk.AutoCAD.Geometry
Imports acgi = Autodesk.AutoCAD.GraphicsInterface
Imports acop = Autodesk.AutoCAD.Interop

    Public Sub setViewDirection(ByVal direction As Integer, _
                                ByVal angle As Double)

        Dim rotAngle As Double
        Dim xVec As Double
        Dim zVec As Double

        rotAngle = angle   'top
        Select Case direction
            Case 0 'top
                zVec = 90
            Case 1 'front
                xVec = 180 + angle : zVec = 0
            Case 2 'NW
                xVec = 45 + angle : zVec = 90
            Case 3 'NE
                xVec = angle - 45 : zVec = 90
            Case 4 'SW
                xVec = 135 + angle : zVec = 90
            Case 5 'SE
                xVec = angle - 135 : zVec = 90
            Case 6 'Back
                xVec = angle : zVec = 0
        End Select

        Dim DB As acdb.Database = acadApp.DocumentManager.MdiActiveDocument.Database
        Dim docLock As acap.DocumentLock = acadApp.DocumentManager.MdiActiveDocument.LockDocument(acap.DocumentLockMode.Write, "CrosshairAlignmentMagLevel", "CrosshairAlignmentMagLevel", True)
        Dim trans As acdb.Transaction = DB.TransactionManager.StartTransaction

        Try
            Dim centerPoint2D As New acge.Point2d(0, 0)'????? I zoom extents at the end so this does not affect much

            acedVports2VportTableRecords()

            Dim vTblRec As acdb.ViewportTableRecord = Nothing
            Dim vTbl As acdb.ViewportTable = trans.GetObject(DB.ViewportTableId, acdb.OpenMode.ForRead)
            For Each OID As acdb.ObjectId In vTbl
                If OID.IsErased = False Then
                    'The first unerased viewport should be the current one!
                    vTblRec = trans.GetObject(OID, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
                    Exit For
                End If
            Next
            vTbl.Dispose()

            If vTblRec Is Nothing Then
                trans.Dispose()
                Return
            End If

            Dim vTblRec1 As New acdb.ViewTableRecord
            vTblRec1.IsPaperspaceView = False
            vTblRec1.Height = vTblRec.Height
            vTblRec1.Width = vTblRec.Width
            vTblRec1.CenterPoint = centerPoint2D
            Select Case direction
                Case 0 'top
                    Dim vector As New Vector3d(0, 0, rad(zVec))
                    vTblRec1.ViewDirection = vector
                    vTblRec1.ViewTwist = rad(rotAngle)
                Case Else
                    Dim vector As New Vector3d(Math.Sin(rad(xVec)), Math.Cos(rad(xVec)), Math.Sin(rad(zVec)))
                    vTblRec1.ViewDirection = vector
            End Select

            acedSetCurrentView(vTblRec1.UnmanagedObject, Nothing)
            vTblRec.Dispose()
            vTblRec1.Dispose()

            trans.Commit()
        Catch ex As Autodesk.AutoCAD.Runtime.Exception
            MsgBox(ex.StackTrace)
        Finally
            trans.Dispose()
        End Try

        docLock.Dispose()

        Dim oApp As acop.AcadApplication
        oApp = CType(acap.Application.AcadApplication, acop.AcadApplication)
        oApp.ZoomExtents()

    End Sub

    Public Function rad(ByVal ang As Double) As Double

        rad = ang * (pi / 180)

    End Function