Author Topic: Set current Paperspace viewport - AutoCAD Mechanical  (Read 14251 times)

0 Members and 1 Guest are viewing this topic.

vegbruiser

  • Guest
Set current Paperspace viewport - AutoCAD Mechanical
« on: November 25, 2010, 05:28:35 AM »
Hi all,

I've been working on this again recently and have hit a slight snag whilst debugging.

In AutoCAD 2009, I can successfully use the following:

Code: [Select]
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PBVAcDbViewport@@@Z")]
extern static private int acedSetCurrentVPort(IntPtr AcDbVport);

To set a new paperspace viewport as active.

Yet, in my AutoCAD Mechanical 2011 environment it doesn't work. Does anybody know of a workaround - I've looked at Glenn's post here but I can't see what I'm doing wrong.

Thanks in advance.

Alex (aka vegbruiser)

Glenn R

  • Guest
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #1 on: November 25, 2010, 09:35:22 AM »
Hmmm....(IntPtr AcDbVport)....that looks like it is a tiled modelspace viewport, NOT a floating Paperspace viewport, so that signature looks off to me.

Regardless, you could probably just try setting the system variable CVPORT to the number returned from your paperspace Viewport as long as it's active, which from memory means on.

If that doesn't work, you will have to get the correct function import signature from the AutoCAD 2011 dll's.

vegbruiser

  • Guest
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #2 on: November 25, 2010, 11:04:56 AM »
Thanks Glenn,

I'll have to look for that later.

For now I'm stuck with the following line:

doc.Editor.SwitchToModelSpace(); (copied verbatim from the AutoCAD .NET Developer's Guide page)

I always get "eCannotChangeActiveViewport" despite already having added 5 viewports to the layout I'm working on. Admittedly, I don't need to go into ModelSpace for the other viewports, but if I try this with any of them I get the same message.

Any ideas??

Scratch all that, I was doing it wrong - by attempting to bypass the activation of the last active layout and go to the layout I wanted to add views to directly.

I figure all I need to do now is activate each layout in turn, in order to get "TILEMODE 0" to pick the correct layout to add viewports to?
« Last Edit: November 25, 2010, 11:32:50 AM by AlexF »

Glenn R

  • Guest
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #3 on: November 25, 2010, 11:18:20 AM »
Are the viewports on and active?

Glenn R

  • Guest
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #4 on: November 25, 2010, 11:23:26 AM »
Code?

vegbruiser

  • Guest
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #5 on: November 25, 2010, 11:34:31 AM »
Sorry Glenn,

I edited my post before seeing your reply.

It now runs through (on AutoCAD 2009 at least) without an error.

I'll look at Mechanical again once I've got it working completely on 2K9.

vegbruiser

  • Guest
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #6 on: November 26, 2010, 06:38:16 AM »
Me again,  :-(

I'm getting really cheesed off with this now :pissed: I can't for the life of me figure out how to add viewports to a specific layout.

I thought this morning that I might be able to use this:

Code: [Select]
<CommandMethod("AssignView")> _
    Public Sub AssignView()
        '' Get the current database
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database
        '' Start a transaction
        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            Dim myLayoutDictionary As DBDictionary = acCurDb.LayoutDictionaryId.GetObject(OpenMode.ForRead)
            Dim oid As ObjectId = myLayoutDictionary.Item("Layout XofX")
            Dim myLayout As Layout = DirectCast(acTrans.GetObject(oid, OpenMode.ForWrite), Layout)
            Dim ltman As LayoutManager = LayoutManager.Current
            Application.SetSystemVariable("TILEMODE", 0)
            acDoc.Editor.SwitchToPaperSpace()
            ltman.CurrentLayout = "Layout XofX"
            Dim oids As ObjectIdCollection = myLayout.GetViewports()
            Dim vp As Viewport = DirectCast(acTrans.GetObject(oids(0), OpenMode.ForWrite), Viewport)
            Dim vCenter As Point2d = New Point2d(posX + dimX / 2, posY + dimY / 2)
            Dim locked As Boolean = vp.Locked
            If (locked) Then vp.Locked = False
            vp.ViewCenter = vCenter
            vp.UpdateDisplay()
            vp.Locked = locked
            acTrans.Commit()
        End Using
    End Sub

(from here)

But even though it correctly grabs the first viewport on my newly created layout I am then unable to do this:
Code: [Select]
doc.Editor.SwitchToModelSpace(); without an "eCannotChangeActiveViewport" error.

Does anybody have an example I could borrow that shows how to add a new layout, rename it, then add new viewports to that layout? Failing that, I'll post my LayoutCommands class for you to look at.

Thanks,

Alex.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #7 on: November 26, 2010, 10:52:16 AM »
Try this
Code: [Select]
        [CommandMethod("l&vp")]
        public static void Addvp()
        {
            LayoutManager lm=LayoutManager.Current;
            string sLayout="Brandnew";
            lm.CreateLayout(sLayout);
            ObjectId LayoutId=lm.GetLayoutId(sLayout);
            lm.CurrentLayout = sLayout;
            pViewport(LayoutId, 12, 6);

        } // end Addvp


        private static void pViewport(ObjectId LayoutId, double wd, double ht)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
            ObjectId id=ObjectId.Null;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Layout layout = tr.GetObject(LayoutId, OpenMode.ForRead) as Layout;
                Viewport vp =tr.GetObject(layout.GetViewports()[1],OpenMode.ForWrite) as Viewport;
                vp.CenterPoint = new Point3d(0.5 * wd , 0.5 * ht, 0);
                vp.Width = wd;
                vp.Height = ht;
                vp.On = true;
                ed.SwitchToModelSpace();
                tr.Commit();
            }
        }
GetViewports() - the first one (0) is the paperspace vp, not the one you want

vegbruiser

  • Guest
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #8 on: November 26, 2010, 11:28:17 AM »
Thanks Bryco, trying that code now.

What if I wanted to add multiple layouts with several viewports on each one could I just call the Addvp routine from a loop? - I've been struggling with this very problem all week and keep getting "eNotInPaperSpace" errors.




Bryco

  • Water Moccasin
  • Posts: 1883
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #9 on: November 26, 2010, 08:32:28 PM »
Yes should be no problem, I think vp.on is pretty important.
Remember some don't check ; add a viewport to new layout in options, so you have to check for that

vegbruiser

  • Guest
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #10 on: December 01, 2010, 07:47:12 AM »
Hi Bryco,

I tried that code you supplied and whilst it does make use of the viewport created when a new Layout is added to the drawing, it doesn't allow me to add further viewports to the new Layouts without popping up with an "eNotinPaperSpace" error whenever I try and call ed.SwitchToPaperSpace(); (in order to add another viewport to Paperspace)

Any ideas?

Thanks,

Alex.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #11 on: December 01, 2010, 10:12:38 AM »
That's odd, using vp.On = true; I didn't have that problem

vegbruiser

  • Guest
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #12 on: December 01, 2010, 11:46:53 AM »
Bryco, your code works just fine - the problem I have is when attempting to add viewports to the new layouts i.e. extra to the single viewport that is already created when you make a "new" Layout. I hope to be able to share some code to show my problem shortly. (I am currently in the middle of simplifying my routine so that I only pass a list of expected viewport properties around my program as opposed to several named objects.)

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #13 on: December 02, 2010, 09:49:57 AM »
 
This didn't break on acad 2010
       [CommandMethod("l&vp")]
        public static void Addvp()
        {
            LayoutManager lm=LayoutManager.Current;
            ObjectId LayoutId = lm.GetLayoutId(lm.CurrentLayout);
            int j=12, k=6;
            for (int i = 0; i < 6; i++)
            {
                pViewport(LayoutId, j, k);
                j += 4; k += 4;
            }
           

        } // end Addvp


        private static ObjectId pViewport(ObjectId LayoutId, double wd, double ht)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
            ObjectId id=ObjectId.Null;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Viewport vp =new Viewport();
                vp.CenterPoint = new Point3d(0.5 * wd , 0.5 * ht, 0);
                vp.Width = wd;
                vp.Height = ht;
                vp.SetDatabaseDefaults();
                vp.CenterPoint = new Point3d(0.5 * wd, 0.5 * ht, 0);
                vp.Width = wd;
                vp.Height = ht;
                Layout layout = tr.GetObject(LayoutId, OpenMode.ForRead) as Layout;
                BlockTableRecord space = tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite) as BlockTableRecord;
                space.AppendEntity(vp);
                tr.AddNewlyCreatedDBObject(vp, true);
                vp.On = true;
                ed.SwitchToModelSpace();
                tr.Commit();
            }
            return id;
        }
Code: [Select]

vegbruiser

  • Guest
Re: Set current Paperspace viewport - AutoCAD Mechanical
« Reply #14 on: December 02, 2010, 10:02:18 AM »
Thanks Bryco,

I've actually just succeeded in getting my implementation working correctly.

The problem I think had to do with the order in which new layouts were created and then initialized prior to adding viewports to them.

I can now comfortably create as many layouts as I like with my tool, although I have a few more issues to overcome before I get the same functionality as my original VBA code.

I'll be sure to update my original thread once I'm happy with the outcome.

:D