Set Paperspace Viewport Scale and CenterPoint

Is there a way to use Dynamo to Set The Scale of a Viewport in Paperspace and also set the Centerpoint of that viewport in relation to model space?

I see in the Civil 3D Toolkit package that there are nodes to view a multitude of information about viewports (Scaled, Locked, Viewport.PointFromPaperToModelSpace). However I don’t see nodes in that package or others for setting this criteria. It would be nice to be able to set your viewport scale and center it over the point in model space that you want, then lock the viewport.

Hi
Looking at this it may help you

Looks like arkance has some cool nodes for getting viewport data and I think there is one for at least setting locked. But I think you may need to dive into API to edit the properties.

1 Like

There is no node to create a viewport yet, it is on the wishlist.

1 Like

Can something like this be done with python, or is it best to wait until some nodes are released? I don’t know C, so that won’t be an option for me, and I am just intermediate level using python with dynamo. Set the viewport centerpoint in modelspace, and set the scale of that viewport.

Thanks

You can do it with Python and in this case the AutoCAD API

Sounds pretty cool. Are you able to get me started with something on this? I am familiar with the C3D Developer’s guide. Not sure about the AutoCAD API Reference Manual?

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-343a.htm,topicNumber=d0e49632

2 Likes

Hi

Looking at this

The way that I would prefer this type of program work for my application would be:

  1. Create a viewport in paperspace Layout1 tab. This could be done by user inputting the boundary for the viewport, or it could be done by user inputting a length, width, and center point for the viewport. I don’t have a strong preference either way.
  2. Get a point in model space that represents the center of what you want to zoom to in paperspace.
  3. Set Viewport View Center to the point in model space
  4. Set the Viewport scale based on user input.
  5. Lock the Viewport

The piece of this that I am not sure where to input is the case where your view frame is rotated slightly. You would need to set either the viewport twist angle or possibly the viewport view direction (by feeding a vector). Any thoughts on how best to handle a scenario like that?

I am not strong enough at coding to try and put something like this together, but would be happy to help or provide input however I can. I have attached a sample DWG with a couple of “Views&CenterPoints” in model space and a viewport in Paperspace. It is set up to be 40 scale.

Part Catalog Test Drawing Sheet.dwg (636.5 KB)

HI
TRY

import clr
# Add Assemblies for AutoCAD and Civil 3D APIs
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')
clr.AddReference('AeccPressurePipesMgd')
clr.AddReference('acdbmgdbrep')
clr.AddReference('System.Windows.Forms')
clr.AddReference('Civil3DNodes')

# Create an alias to the Autodesk.AutoCAD.ApplicationServices.Application class
import Autodesk
import Autodesk.AutoCAD.ApplicationServices.Application as acapp

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

# Import references for Civil 3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

import System
from System import Array 
from System import Collections

adoc = acapp.DocumentManager.MdiActiveDocument
ed = adoc.Editor
civdoc = CivilApplication.ActiveDocument
AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application
#import AcadApplication

#XX= IN[1]
#YY= IN[2]

objects = IN[0]
name = IN[1]
lys  = IN[2]
vh  = IN[3]
vw  = IN[4]

def move_labels(objects,name,lys,vh,vw):
	if not hasattr(objects, "__iter__"):
		objects = [objects]
	#if not hasattr(yy, "__iter__"):
		#yy = [yy]
	if not hasattr(lys, "__iter__"):
		lys = [lys]
	error_report = None
	res = []
	object_ids_list = []
	try:
		with adoc.LockDocument():		
		    with adoc.Database as db:		
		        with db.TransactionManager.StartTransaction() as t:		    
		            bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
		            btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
		            
		            ii = 0
		            for i in  lys:
		                if LayoutManager.Current.LayoutExists(lys[ii]) == False:
		                   LayoutManager.Current.CopyLayout(name, lys[ii])
		                lm = LayoutManager.Current.CurrentLayout =lys[ii]
		                lmm = LayoutManager.Current
		                objly = t.GetObject(lmm.GetLayoutId(lmm.CurrentLayout), OpenMode.ForWrite)
		                vids = objly.GetViewports()
		                vport = t.GetObject(vids[0], OpenMode.ForWrite)
		                vport.Locked = False
		                ed.SwitchToModelSpace()
		                view = ViewTableRecord()
		                view.CenterPoint =Point2d(objects[ii].X ,objects[ii].Y )
		                view.Height = vh
		                view.Width = vw
		                ed.SetCurrentView(view)
		                #vport.TwistAngle = 1.5
		                ed.SwitchToPaperSpace()
		                vport.Locked = True
		                res.append(view)
		                
		                ii +=1
		            
		            
		            
		            
		            
		            t.Commit()
	except:
		import traceback
		error_report = traceback.format_exc()
	if error_report is None:
		return res
	else:
		return error_report


OUT = move_labels(objects,name,lys,vh,vw)

Q10

Part Catalog Test Drawing Sheet(1).dwg (636.6 KB)

3 Likes

Q101

#region Assembly Acdbmgd, Version=24.1.0.0, Culture=neutral, PublicKeyToken=null
// G:\API-P\ENG  ESSAM\Debug 2022\acdbmgd.dll
#endregion

using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections;
using System.ComponentModel;

namespace Autodesk.AutoCAD.DatabaseServices
{
    [TypeDescriptionProvider("Autodesk.AutoCAD.ComponentModel.TypeDescriptionProvider`1[[Autodesk.AutoCAD.DatabaseServices.Viewport, acdbmgd]], acdbmgd")]
    [Wrapper("AcDbViewport")]
    public class Viewport : Entity
    {
        public Viewport();
        protected internal Viewport(IntPtr unmanagedPointer, bool autoDelete);

        public bool SnapIsometric { get; set; }
        [Category("Misc")]
        [UnitType(UnitType.AngleNotTransformed)]
        public double SnapAngle { get; set; }
        public Point2d SnapBasePoint { get; set; }
        public Vector2d SnapIncrement { get; set; }
        public int SnapIsoPair { get; set; }
        public bool GridOn { get; set; }
        public Vector2d GridIncrement { get; set; }
        public bool HiddenLinesRemoved { get; set; }
        public bool GridBoundToLimits { get; set; }
        public bool GridAdaptive { get; set; }
        public bool GridSubdivisionRestricted { get; set; }
        public bool GridFollow { get; set; }
        public short GridMajor { get; set; }
        public bool DefaultLightingOn { get; set; }
        public DefaultLightingType DefaultLightingType { get; set; }
        [Category("3D Visualization")]
        [UnitType(UnitType.Unitless)]
        public double Brightness { get; set; }
        [Category("Misc")]
        [UnitType(UnitType.Unitless)]
        public double Contrast { get; set; }
        public bool SnapOn { get; set; }
        public int CircleSides { get; set; }
        public bool FastZoomOn { get; set; }
        public bool UcsIconAtOrigin { get; set; }
        [Category("Geometry")]
        public Point3d CenterPoint { get; set; }
        public int Number { get; }
        public bool On { get; set; }
        public Point3d ViewTarget { get; set; }
        public Vector3d ViewDirection { get; set; }
        [Category("Geometry")]
        [UnitType(UnitType.Distance)]
        public double ViewHeight { get; set; }
        public Point2d ViewCenter { get; set; }
        [Category("Misc")]
        [UnitType(UnitType.AngleNotTransformed)]
        public double TwistAngle { get; set; }
        [Category("3D Visualization")]
        [UnitType(UnitType.Distance)]
        public double LensLength { get; set; }
        public bool FrontClipOn { get; set; }
        public bool BackClipOn { get; set; }
        public bool FrontClipAtEyeOn { get; set; }
        [Category("Misc")]
        [UnitType(UnitType.Distance)]
        public double FrontClipDistance { get; set; }
        [Category("Misc")]
        [UnitType(UnitType.Distance)]
        public double BackClipDistance { get; set; }
        [Category("3D Visualization")]
        public bool PerspectiveOn { get; set; }
        public bool UcsFollowModeOn { get; set; }
        public bool UcsIconVisible { get; set; }
        public Color AmbientLightColor { get; set; }
        public ObjectId SunId { get; }
        public ObjectId VisualStyleId { get; set; }
        public bool Locked { get; set; }
        public System.Drawing.Bitmap Thumbnail { get; set; }
        public AnnotationScale AnnotationScale { get; set; }
        [Category("Geometry")]
        [UnitType(UnitType.Distance)]
        public double Width { get; set; }
        public bool LinkedToSheetView { get; }
        public bool PlotAsRaster { get; }
        public bool PlotWireframe { get; }
        public ObjectId ShadePlotId { get; }
        public ObjectId Background { get; set; }
        public ShadePlotType ShadePlot { get; set; }
        public bool UcsPerViewport { get; set; }
        public OrthographicView ViewOrthographic { get; }
        public ToneOperatorParameters ToneOperatorParameters { get; set; }
        [Category("Geometry")]
        [UnitType(UnitType.Distance)]
        public double Elevation { get; set; }
        public CoordinateSystem3d Ucs { get; set; }
        public bool Transparent { get; set; }
        [Category("Misc")]
        [UnitType(UnitType.Unitless)]
        public double CustomScale { get; set; }
        public StandardScaleType StandardScale { get; set; }
        public string PlotStyleSheet { get; set; }
        [Category("Geometry")]
        [UnitType(UnitType.Distance)]
        public double Height { get; set; }
        public bool NonRectClipOn { get; set; }
        public ObjectId NonRectClipEntityId { get; set; }
        public OrthographicView UcsOrthographic { get; }
        public ObjectId UcsName { get; }
        public string EffectivePlotStyleSheet { get; }

        public void FreezeLayersInViewport(IEnumerator layerIds);
        public ObjectIdCollection GetFrozenLayers();
        [Category("Misc")]
        public ObjectId GetPreviousBackground(DrawableType type);
        public void GetUcs(ref Point3d origin, ref Vector3d x, ref Vector3d y);
        public bool IsLayerFrozenInViewport(ObjectId layerId);
        public void SetPreviousBackground(ObjectId value, DrawableType type);
        public void SetShadePlot(ShadePlotType type, ObjectId shadePlotId);
        public ObjectId SetSun([CallerMustClose] DBObject sun);
        public void SetUcs(ObjectId userCoordinateSystemId);
        public void SetUcs(OrthographicView view);
        public void SetUcs(Point3d origin, Vector3d x, Vector3d y);
        public void SetUcsToWorld();
        public void SetViewDirection(OrthographicView view);
        public void ThawAllLayersInViewport();
        public void ThawLayersInViewport(IEnumerator layerIds);
        public void UpdateDisplay();
    }
}

Changing zoom in paperspace viewport.TXT (50.5 KB)
Q1011

This is pretty awesome stuff! I only have a couple of additional comments/questions.

  1. I have not been able to get the angle of the Viewport (if it is twisted) to adjust using the code you provided. I have tried three different locations for placing the vport.TwistAngle command and removed the # so it runs for each try (See image below) but haven’t been able to get that to twist the view.

image

  1. What would change in the code if you just wanted to update only the parameters of the Viewport in Layout 1. What you have set up to copy Layout1 as many times as needed is good. I am just wondering that the code would look like if you wanted to change the View Center, View Height, View Width, View Twist for the viewport already present in Layout1.

This is a great little program! Thank you for all of your time and effort!

I do not know how to use all features
But you must try it after this line

vport.Locked = False

try like this

vport.Locked = False
vport.ViewHeight =10
vport.Width = 10
vport.TwistAngle = 2.301

You should ask what you want for this topic
here

https://forums.autodesk.com/t5/net/bd-p/152

find the answer

Like this

Moving it to the location that you mentioned still does not return a viewport that is twisted. I still get 0° for the viewport twist.

image
image

Try

vport.UpgradeOpen()
vport.TwistAngle = 2.301

Using vport.UpgradeOpen() did not rotate the viewport either.

image

I am really sorry
I don’t know why it’s not working

Can someone help you with it? to thank

1 Like