Author Topic: How to create a polygon viewport in autocad's layout?  (Read 2092 times)

0 Members and 1 Guest are viewing this topic.


  • Mosquito
  • Posts: 17
How to create a polygon viewport in autocad's layout?
« on: November 11, 2016, 03:47:52 AM »
I want to create a polygon viewport in a layout using a closed polyline in modelspace,the entities included in the polyline area will be displayed in the viewport.
can anybody give me some information?
Using c# to create the viewport.
Please see the attach file.


  • Bull Frog
  • Posts: 348
Re: How to create a polygon viewport in autocad's layout?
« Reply #1 on: November 11, 2016, 02:08:30 PM »
Polyline viewport is created if Viewport.NonrectClipEntityId property is assigned to a polyline's ObjectId, and set Viewport.NonRectClipOn property to "true". So, simply create a viewportand a polyline, then set the 2 Viewport properties. You are done.

The code shown here assumes that there are already a viewport and a polyline in a layout. The code simply combine the both to be a non-rectangular viewport:

Code: [Select]
public static void RunDocCommand()
    var doc = CadApp.DocumentManager.MdiActiveDocument;
    var ed = doc.Editor;

    var vportId = SelectEntity(
        ed, typeof(Viewport), "\nSelect a regular viewport:");
    var polyId = SelectEntity(
        ed, typeof(Polyline), "\nSelect a polyline inside the viewport:");

    if (vportId.IsNull || polyId.IsNull)

    using (var tran = doc.TransactionManager.StartTransaction())
        Viewport vport = (Viewport)tran.GetObject(vportId, OpenMode.ForWrite);
        vport.NonRectClipEntityId = polyId;
        vport.NonRectClipOn = true;


private static ObjectId SelectEntity(
    Editor ed, Type entType, string selectionMsg)
    var opt = new PromptEntityOptions(selectionMsg);
    opt.SetRejectMessage("\nInvalid selection.");
    opt.AddAllowedClass(entType, true);

    var res = ed.GetEntity(opt);
    if (res.Status == PromptStatus.OK)
        return res.ObjectId;
        return ObjectId.Null;

For rectangular viewport, the NonRectClipEntityId property is set to ObjectId.Null.