Author Topic: Layout Viewport to Modelspace  (Read 2048 times)

0 Members and 1 Guest are viewing this topic.

surveyor_randy

  • Guest
Layout Viewport to Modelspace
« on: July 14, 2009, 08:44:15 AM »
Hi all!  I am writting a routine that will draw the outline of a selected paperspace viewport in modelspace.  My code is working fine unless the viewport has been rotate or is not on the WCS.  Could someone have a look-see and give me a nudge in the right direction....  Thanks for any assistance!  :-)

Code: [Select]
Using tr As Transaction = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()

                Dim oPrOpts As New PromptSelectionOptions
                Dim oPrRes As PromptSelectionResult

                oPrOpts.SingleOnly = True
                oPrOpts.MessageForAdding = "select a viewport"
                oPrRes = ed.GetSelection(oPrOpts)

                If oPrRes.Status <> PromptStatus.OK Then
                    ed.WriteMessage(vbCrLf & "*** cancelled ***")
                    Exit Sub
                End If

                Dim oSelections As SelectionSet
                oSelections = oPrRes.Value

                Dim aIDS As ObjectId() = oSelections.GetObjectIds()

                Dim id As ObjectId
                For Each id In aIDS
                    Dim Ent As Entity = tr.GetObject(id, OpenMode.ForWrite, True)
                    If TypeOf Ent Is Viewport Then
                        Dim oViewport As Viewport = CType(tr.GetObject(id, OpenMode.ForWrite, True), Viewport)

                        Dim oVPCenter As New Point3d(oViewport.ViewCenter.X, oViewport.ViewCenter.Y, 0)
                        Dim height As Double = oViewport.Height * (1 / oViewport.CustomScale)
                        Dim width As Double = oViewport.Width * (1 / oViewport.CustomScale)


                        Dim ul As Point3d = New Point3d(oVPCenter.X + width, oVPCenter.Y + height / 2, 0)
                        Dim ur As Point3d = New Point3d(ul.X + width, ul.Y, 0)
                        Dim lr As Point3d = New Point3d(ur.X, ur.Y - height, 0)
                        Dim ll As Point3d = New Point3d(oVPCenter.X - width / 2, oVPCenter.Y - height / 2, 0)

                        Dim extents As Extents3d = oViewport.GeometricExtents
                       
                        Dim BorderPoints As New Point3dCollection
                        BorderPoints.Add(ul)
                        BorderPoints.Add(ur)
                        BorderPoints.Add(lr)
                        BorderPoints.Add(ll)
                        BorderPoints.Add(ul)

                        rhAcad.DrawPolyLine(BorderPoints)
                                             
                    End If
                Next id
                tr.Commit()
            End Using

surveyor_randy

  • Guest
Re: Layout Viewport to Modelspace
« Reply #1 on: July 14, 2009, 08:45:16 AM »
I found this on the discussion groups over at Autodesk, but I don't have a clue how to use it...

Code: [Select]
... maybe ...
vp = someviewport
Matrix3d matWCS2DCS;
matWCS2DCS = Matrix3d.PlaneToWorld(vp.ViewDirection);
matWCS2DCS = Matrix3d.Displacement(vp.ViewTarget - Point3d.Origin) * matWCS2DCS;
matWCS2DCS = Matrix3d.Rotation(-vp.TwistAngle, vp.ViewDirection, vp.ViewTarget) * matWCS2DCS;
matWCS2DCS = matWCS2DCS.Inverse();

//tranform the extents to the DCS
mExtents.TransformBy(matWCS2DCS);

surveyor_randy

  • Guest
Re: Layout Viewport to Modelspace
« Reply #2 on: July 20, 2009, 10:29:15 AM »
Okay...  I figured it out, and I'll post the solution here in case someone else needs the reference later.

I transform each point as follows:

Code: [Select]
Public Function TransformPaperspacePointToWCS(ByVal oViewport As Viewport, ByVal oPointToTransform As Point3d) As Point3d
            Dim oRotationMatrix As Matrix3d
            oRotationMatrix = Matrix3d.WorldToPlane(New Plane(oViewport.ViewTarget, oViewport.ViewDirection)) * Matrix3d.Rotation(oViewport.TwistAngle, Vector3d.ZAxis, Point3d.Origin)
            Return oPointToTransform.TransformBy(oRotationMatrix.Inverse())
        End Function

This only works with viewports that have been rotated around the Z access which is typical on our plan sheets.

Next, I need to figure out how to collect the paperspace points from irregular shaped viewports.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Layout Viewport to Modelspace
« Reply #3 on: July 20, 2009, 11:10:22 AM »
If you don't mind using a Lisp, then you can download the file of this post by Alan.
[ http://www.theswamp.org/index.php?topic=9461.msg129670#msg129670 ]

It should work for all viewport types.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

surveyor_randy

  • Guest
Re: Layout Viewport to Modelspace
« Reply #4 on: July 21, 2009, 11:24:36 AM »
Thank you!  That will come in very handy!  Nice job Alan!

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Layout Viewport to Modelspace
« Reply #5 on: July 21, 2009, 11:45:20 AM »
Randy, curious about this; are you folks NOT using the Plan production tools for your sheets?
Or why is there a need to copy viewports into the model space? 
This can be answered offline if you want, so as not to hijack the topic.
Be your Best


Michael Farrell
http://primeservicesglobal.com/

KewlToyZ

  • Guest
Re: Layout Viewport to Modelspace
« Reply #6 on: July 21, 2009, 03:05:38 PM »
I like the tool. Nice job as always CAB !
Usually this is something helpful when we have client files not following standards etc..