• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Valued Contributor
    Posts: 51
    Registered: ‎07-07-2004
    Accepted Solution

    Model Space Tiled Viewports

    392 Views, 5 Replies
    09-26-2012 12:16 PM

    When a new document is created I want to set the model space to have tiled viewports. This can be done within autocad using the view/viewports/new viewports menu option. How do I do this using the API?

     

    Any suggestions or pointers to documentation?

     

    Mike B

    Please use plain text.
    Distinguished Contributor
    khoa.ho
    Posts: 131
    Registered: ‎09-15-2011

    Re: Model Space Tiled Viewports

    09-26-2012 04:00 PM in reply to: mbujak

    The link from AutoCAD .NET Developer's Guide helps to create 2 horizontal model viewports. The main point is that model viewport comes from database.ViewportTableId. We need to create a new ViewportTableRecord with defined LowerLeftCorner and UpperRightCorner properties, and then add it to the Viewport Table.

     

    You should assign the name of a new created viewport as "*Active". Otherwise, AutoCAD will not show correctly.

     

    I rewrote the code to be more basic and simpler, to create two vertical tiled viewports:

     

    [CommandMethod("CreateModelViewports")]
    public static void CreateModelViewports()
    {
    	Document doc = Application.DocumentManager.MdiActiveDocument;
    	Database db = doc.Database;
    	using (Transaction trans = db.TransactionManager.StartTransaction())
    	{
    		var viewportTable = (ViewportTable)trans.GetObject(db.ViewportTableId, OpenMode.ForWrite);
    		foreach (ObjectId id in viewportTable)
    		{
    			var viewport = (ViewportTableRecord)trans.GetObject(id, OpenMode.ForRead);
    			// Delete the active viewport
    			if (viewport.Name == "*Active")
    			{
    				viewport.UpgradeOpen();
    				viewport.Erase();
    			}
    		}
    // Create a left viewport var vport = new ViewportTableRecord { Name = "*Active", LowerLeftCorner = new Point2d(0, 0), UpperRightCorner = new Point2d(0.5, 1), }; viewportTable.Add(vport); trans.AddNewlyCreatedDBObject(vport, true);
    // Create a right viewport vport = new ViewportTableRecord { Name = "*Active", LowerLeftCorner = new Point2d(0.5, 0), UpperRightCorner = new Point2d(1, 1), }; viewportTable.Add(vport); trans.AddNewlyCreatedDBObject(vport, true); viewportTable.DowngradeOpen(); // Update the display with the new tiled viewports arrangement doc.Editor.UpdateTiledViewportsFromDatabase(); trans.Commit(); } }

     

    -Khoa

    Please use plain text.
    Valued Contributor
    Posts: 51
    Registered: ‎07-07-2004

    Re: Model Space Tiled Viewports

    09-27-2012 05:28 AM in reply to: khoa.ho

    Yeah, I actually stumbled upon that document before and after I posted. The problem was before, the introduction and the title made me believe that it related to making a viewport active, not actually creating the tiled view. Shame on me for not reading the entire document before posting :smileyembarrassed:. Anyway, after I posted I revisited the document and realized what it was.

     

    There seems to be something bugging me though. If you have to call all the new viewports "*Active", how do you refer to them by name in code? Is there a way to identify each viewport? I guess I could save a collection of their ObjectId's in an object that contains my own name.

     

    Thanks for the reply.

     

    - Mike

    Please use plain text.
    Distinguished Contributor
    khoa.ho
    Posts: 131
    Registered: ‎09-15-2011

    Re: Model Space Tiled Viewports

    09-27-2012 08:23 AM in reply to: mbujak

    Hi Mike,

     

    After reading the Autodesk's document, I saw there is no reason to clone new created viewports and then use those cloned objects as active and left the original new viewports unused on the ViewportTable. So I rewrote the code to be simpler and shorter.

     

    The viewport name in fact is the GROUP NAME of a group of viewports that show together on AutoCAD screen. This name presents the status of viewport, not for individual identification. You can assign new viewports with any name (not just only *Active) and see those names in the Named Viewports tab of Viewports dialog (VPORTS command on AutoCAD).

     

    However, there should be at least a viewport named *Active, and this viewport will be active to accept user input. Without any viewport with the name *Active, AutoCAD will hang up the screen.

     

    To identify each viewport, the obvious way is to store their ObjectIds to a collection to retrieve later. I can't find a property of ViewportTableRecord that indicates its viewport number (CVPORT variable).

     

    -Khoa

    Please use plain text.
    Member
    Posts: 3
    Registered: ‎08-11-2008

    Re: Model Space Tiled Viewports

    02-22-2013 05:13 AM in reply to: mbujak

    I am not able update the display of newly created viewport in AutoCAD 2006. 

    UpdateTiledViewportsFromDatabase() is not available in AutoCAD 2006.

     

    Please suggest.

     

    Asim

    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,177
    Registered: ‎04-09-2008

    Re: Model Space Tiled Viewports

    02-22-2013 12:20 PM in reply to: asim777

    Oh! What a rarity!

    Alternative method - P/Invoke acedVportTableRecords2Vports:

    [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
    EntryPoint = "?acedVportTableRecords2Vports@@YA?AW4ErrorStatus@Acad@@XZ")]
    public static extern int UpdateTiledViewportsFromDatabase();

     

     

     


    Пожалуйста не забывайте про Утвердить в качестве решения!Утвердить в качестве решения и Give Kudos!Баллы
    Please remember to Accept Solution!Accept as Solution and Give Kudos!Kudos

    Please use plain text.