.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Viewport creation in Paperspace

1 REPLY 1
SOLVED
Reply
Message 1 of 2
swapnil_lokam
181 Views, 1 Reply

Viewport creation in Paperspace

I want to create a single viewport in paperspace of the active document 
I am struggling to write the code for the same 
However I am referring the below code to create the viewport in paperspace but it  creates the multiple viewports in a modelspace and I want to create a single viewport in paperspace
Can anyone please help to guide with the issue 

 

 

[CommandMethod("CreateModelViewport")]
public static void CreateModelViewport()
{
// Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;



// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Viewport table for read
ViewportTable acVportTbl;
acVportTbl = acTrans.GetObject(acCurDb.ViewportTableId,
OpenMode.ForRead) as ViewportTable;

// Check to see if the named view 'TEST_VIEWPORT' exists
if (acVportTbl.Has("TEST_VIEWPORT") == false)
{
// Open the View table for write
acTrans.GetObject(acCurDb.ViewportTableId, OpenMode.ForWrite);

// Add the new viewport to the Viewport table and the transaction
using (ViewportTableRecord acVportTblRecLwr = new ViewportTableRecord())
{
acVportTbl.Add(acVportTblRecLwr);
acTrans.AddNewlyCreatedDBObject(acVportTblRecLwr, true);

// Name the new viewport 'TEST_VIEWPORT' and assign it to be
// the lower half of the drawing window
acVportTblRecLwr.Name = "TEST_VIEWPORT";
acVportTblRecLwr.LowerLeftCorner = new Point2d(0, 0);
acVportTblRecLwr.UpperRightCorner = new Point2d(1, 0.5);

// Add the new viewport to the Viewport table and the transaction
using (ViewportTableRecord acVportTblRecUpr = new ViewportTableRecord())
{
acVportTbl.Add(acVportTblRecUpr);
acTrans.AddNewlyCreatedDBObject(acVportTblRecUpr, true);

// Name the new viewport 'TEST_VIEWPORT' and assign it to be
// the upper half of the drawing window
acVportTblRecUpr.Name = "TEST_VIEWPORT";
acVportTblRecUpr.LowerLeftCorner = new Point2d(0, 0.5);
acVportTblRecUpr.UpperRightCorner = new Point2d(1, 1);

// To assign the new viewports as the active viewports, the
// viewports named '*Active' need to be removed and recreated
// based on 'TEST_VIEWPORT'.

// Step through each object in the symbol table
foreach (ObjectId acObjId in acVportTbl)
{
// Open the object for read
ViewportTableRecord acVportTblRec;
acVportTblRec = acTrans.GetObject(acObjId,
OpenMode.ForRead) as ViewportTableRecord;

// See if it is one of the active viewports, and if so erase it
if (acVportTblRec.Name == "*Active")
{
acTrans.GetObject(acObjId, OpenMode.ForWrite);
acVportTblRec.Erase();
}
}

// Clone the new viewports as the active viewports
foreach (ObjectId acObjId in acVportTbl)
{
// Open the object for read
ViewportTableRecord acVportTblRec;
acVportTblRec = acTrans.GetObject(acObjId,
OpenMode.ForRead) as ViewportTableRecord;

// See if it is one of the active viewports, and if so erase it
if (acVportTblRec.Name == "TEST_VIEWPORT")
{
ViewportTableRecord acVportTblRecClone;
acVportTblRecClone = acVportTblRec.Clone() as ViewportTableRecord;

// Add the new viewport to the Viewport table and the transaction
acVportTbl.Add(acVportTblRecClone);
acVportTblRecClone.Name = "*Active";
acTrans.AddNewlyCreatedDBObject(acVportTblRecClone, true);
}
}

// Update the display with the new tiled viewports arrangement
acDoc.Editor.UpdateTiledViewportsFromDatabase();
}
}

// Commit the changes
acTrans.Commit();
}

// Dispose of the transaction
}
}

 

 



The purpose of this code is 
In the modelspace I have a closed polyline block and in this block there are again several smaller blocks, if I select the closed polyline block, the objects(blocks) present inside the selected closed polyline should get selected, this selected objects should be copy pasted in the viewport which should be present in the paperspace.

I am struggling to select the objects inside the closed polyline 
Hence a code for the above entire process would be an added help
Thanks 

1 REPLY 1
Message 2 of 2
_gile
in reply to: swapnil_lokam

Hi,

 

Here's a basic example for creating a rectangular viewport in Paper Space:

[CommandMethod("FVP", CommandFlags.NoTileMode)]
public static void CreateFloatingViewport()
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;

    var promptPointResult = ed.GetPoint("\nSpecify the first corner: ");
    if (promptPointResult.Status != PromptStatus.OK)
        return;
    var point1 = promptPointResult.Value;

    promptPointResult = ed.GetCorner("\nSpecify the opposite corner: ", point1); 
    if (promptPointResult.Status != PromptStatus.OK)
        return;
    var point2 = promptPointResult.Value;

    using (var tr = db.TransactionManager.StartTransaction())
    {
        var viewport = new Viewport();
        viewport.CenterPoint = new LineSegment3d(point1, point2).MidPoint;
        viewport.Width = Math.Abs(point1.X - point2.X);
        viewport.Height = Math.Abs(point1.Y - point2.Y);

        var paperSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
        paperSpace.AppendEntity(viewport);
        tr.AddNewlyCreatedDBObject(viewport, true);
        viewport.On = true;

        tr.Commit();
    }
}

You should also read the documentation about creating Paper Space Viewports (but you don't have to respect the awful naming convention used in the examples which is not consistent with C# standards).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report