Good day!
How to zoom to a specific location in model space by using Dynamo for Civil 3D?
Try this (done with CPython so Civil 3D 2022) :
Code:
import System
from System import *
from Autodesk.AutoCAD.ApplicationServices import *
output = "Error"
FirstCorner = IN[0]
LastCorner = IN[1]
command = "zoom\nW\n"+IN[0]+"\n"+IN[1]+"\n"
OUT = command
adoc = Application.DocumentManager.MdiActiveDocument
adoc.SendStringToExecute(command, True, False, False)
Forum_ZoomTo.dyn (34.9 KB)
all the dynamo nodes to get the point as string could have been included in the python node too, but anyway I went with doing it with dynamo
*edit - also I sent this quickly, started using variables and just realized that I didn’t used them on the command so yeah, no the best python code
Finally, robust way would be using the view class and not sending a command : Help
Thank you very much!
Would you be able to help write a quick dynamo script to help me use zoom window using the view class inside python? I have attached a .dyn and a .DWG.
The .dyn contains two points for the zoom window. The bottom left corner of the window, and the top right corner of the window. Any help you can give you be very much appreciated. Thank you!
JJH_ZOOMWINDOW.dyn (12.2 KB)
Part Catalog Test Drawing Sheet.dwg (651.1 KB)
@david_licona I forgot to mention that I am looking to do a zoom window within the viewport in Layout1, so that the 40 scale box from model space is lined up in the viewport.
Hi @hestingjj I’m sorry but I’m not able to help you on a short term basis Maybe someone else can pick this up ?
@hosneyalaa Do you have any availability to help with the zoomwindow.dyn graph? I think this type of graph could end up being easier to use with the zoom window rather than the center point, width, and height that I was using before. I would prefer not to send to the command line and just use the view class within the python node. Any thoughts?
hi
# Load the Python Standard and DesignScript Libraries
import sys
import clr
# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')
# 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 from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *
# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor
output = "Error"
FirstCorner = IN[0]
LastCorner = IN[1]
with adoc.LockDocument():
with adoc.Database as db:
with db.TransactionManager.StartTransaction() as t:
# Place your code below
#
#
command = "zoom\nW\n"+IN[0]+"\n"+IN[1]+"\n"
OUT = command
adoc = Application.DocumentManager.MdiActiveDocument
adoc.SendStringToExecute(command, True, False, False)
# Commit before end transaction
t.Commit()
pass
# Assign your output to the OUT variable.
OUT = 0
JJH_ZOOMWINDOW022.dyn (24.3 KB)
Thank you for your quick reply. How do I get this python script to
- Go to Layout1 tab
- Switch to Modelspace within the viewport (Activate the Viewport)
- Unlock Viewport
- Perform Zoomwindow using points given
- Lock the Viewport
- Switch back to Paperspace
- Save the file
Hi
Friday vacation
Saturday I try it
HI
There is a problem that I cannot solve
Sometimes it works
Try this mode
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[2]
#lys = IN[2]
vp = IN[0]
vpp = IN[1]
def move_labels( name,vp,vpp ):
#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
lm = LayoutManager.Current.CurrentLayout =name
lmm = LayoutManager.Current
objly = t.GetObject(lmm.GetLayoutId(lmm.CurrentLayout), OpenMode.ForWrite)
vids = objly.GetViewports()
#// First Viewport is Paperspace itself. Only one Viewport will be evaluated.
if vids.Count > 0:
vport = t.GetObject(vids[1], OpenMode.ForWrite)
vport.Locked = False
vport.On = True
vport.Visible = True
ed.SwitchToModelSpace()
command = "zoom\nW\n"+vp+"\n"+vpp+"\n"
adoc.SendStringToExecute(command, True, False, False)
ed.SwitchToPaperSpace()
vport.Locked = True
vport.UpdateDisplay()
commandq = "_QSAVE\n"
adoc.SendStringToExecute(commandq, True, False, False)
t.Commit()
except:
import traceback
error_report = traceback.format_exc()
if error_report is None:
return commandq
else:
return error_report
OUT = move_labels( name,vp,vpp )
@hosneyalaa
I having similar issues to what you had.
If I run the code just through the zoom window command, it runs just fine to that point. Starting with a locked viewport (Image 1) and run the script through the zoom window command result looks good (Image 2).
I noticed that when the script is run in this way nothing populates on the command line as shown in (Image 2). If I then try and turn on the next line of code to either lock the viewport or switch back to paperspace, the script does not work correctly and it performs a zoom window command from paperspace instead of the activated modelspace (Image3).
You can also see from (Image 3) on the command line that it says “Viewport is view-locked”. “Switching to Paperspace”. and it then performs a zoom command. Do my extra observations help you diagnose any more issues that may be occurring? @david_licona any thoughts as we are working with your original code?
Hi
Can you use this
view = ViewTableRecord()
view.CenterPoint =Point2d( from p1&p2)
view.Height = p1.x-p2.x
view.Width = p1.y-p2.y
ed.SetCurrentView(view)
using that method would have to have a view rotation component, which does not work very well. I was hoping to be able to use the zoom window. if it has to be used through the command line, that is okay. I have not been able to figure out why your previous code does the zoom from paperspace instead of within the viewport modelspace as the code states.
Hi
Maybe because you not use python 2.7
The Arkance Systems Node Library has a node for that. But it is always recommended to try it yourself, of course.
Great node. I hadn’t seen that one yet. Is there a way that this node can be used on a Layout tab? For example, the graph would go to a layout tab, activate model space within the viewport, and then perform a zoomwindow to set the viewport. @Anton_Huizinga