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

Zoom without using Interop?

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
611 Views, 5 Replies

Zoom without using Interop?

I'm trying to write a .net application without using COM Interop.
The only function I have left using Interop is to zoom in a document.

Does anyone know of a way of doing this natively in .net or ARX?
I don't really want to use SendStringToExecute either to do this (doesn't
seem to work in a DocumentCreated event).

/Fredrik
5 REPLIES 5
Message 2 of 6
RonnieWilkins
in reply to: Anonymous

This should get you started...it centers a particular coordinate in the drawing window or inside of a viewport, emulating a zoomcenter.

I'm planning to create a zoom class that duplicates the Application.Zooms from COM but do not have the time right now. I had this code partially completed from an OEM project I am working on. It is not the cleanest but should be OK.

Please note that there are DLLImport statements missing from the web version of the discussion group. Been a while since I've done html and forgot the syntax to force them to show...any help here???

Imports System.Runtime.InteropServices
Imports AcadApp = Autodesk.AutoCAD.ApplicationServices.Application
Imports AcAp = Autodesk.AutoCAD.ApplicationServices
Imports AcCm = Autodesk.AutoCAD.Colors
Imports AcDb = Autodesk.AutoCAD.DatabaseServices
Imports AcEd = Autodesk.AutoCAD.EditorInput
Imports AcGe = Autodesk.AutoCAD.Geometry
Imports AcGi = Autodesk.AutoCAD.GraphicsInterface
Imports AcLy = Autodesk.AutoCAD.LayerManager
Imports AcPl = Autodesk.AutoCAD.PlottingServices
Imports AcPu = Autodesk.AutoCAD.Publishing
Imports AcRx = Autodesk.AutoCAD.Runtime
Imports AcWi = Autodesk.AutoCAD.Windows

Public Class Zoom
*LessThan*DllImport("acad.exe", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="?acedVportTableRecords2Vports@@YA?AW4ErrorStatus@Acad@@XZ")*GreaterThan* _
Private Shared Function acedVportTableRecords2Vports() As Boolean
End Function

*LessThan*DllImport("acad.exe", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="?acedVports2VportTableRecords@@YA?AW4ErrorStatus@Acad@@XZ")*GreaterThan* _
Private Shared Function acedVports2VportTableRecords() As Boolean
End Function

*LessThan*DllImport("acad.exe", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="?acedSetCurrentView@@YA?AW4ErrorStatus@Acad@@PAVAcDbViewTableRecord@@PAVAcDbViewport@@@Z")*GreaterThan* _
Public Shared Function acedSetCurrentView(ByVal viewTableRec As IntPtr, ByVal viewport As IntPtr) As IntPtr
End Function

*LessThan*AcRx.CommandMethod("ZC")*GreaterThan* _
Public Sub Center()

'The following line would need to be called if running function
'from inside of application context
'Dim DL As AcAp.DocumentLock = AcadApp.DocumentManager.MdiActiveDocument.LockDocument(AcAp.DocumentLockMode.Write, "CrosshairAlignmentMagLevel", "CrosshairAlignmentMagLevel", True)

Dim DB As AcDb.Database = AcadApp.DocumentManager.MdiActiveDocument.Database
Dim myT As AcDb.Transaction = DB.TransactionManager.StartTransaction
Try
Dim Ed As AcEd.Editor = AcadApp.DocumentManager.MdiActiveDocument.Editor
Dim PPO As New AcEd.PromptPointOptions("Select a point: ")
Dim PPR As AcEd.PromptPointResult = Nothing
PPR = Ed.GetPoint(PPO)
If PPR.Status <> Autodesk.AutoCAD.EditorInput.PromptStatus.OK Then
myT.Dispose()
Return
End If
Dim NewCenterPoint3D As AcGe.Point3d = PPR.Value

'Must convert the 3D coordinate to 2D
Dim NewCenterPoint2D As New AcGe.Point2d(NewCenterPoint3D.X, NewCenterPoint3D.Y)

If DB.TileMode = True Then
'In Modelspace
'You must first force the Vport information to the record objects or you will
'be getting stale information.
acedVports2VportTableRecords()

Dim VPTR As AcDb.ViewportTableRecord = Nothing
Dim VPT As AcDb.ViewportTable = myT.GetObject(DB.ViewportTableId, AcDb.OpenMode.ForRead)
For Each OID As AcDb.ObjectId In VPT
If OID.IsErased = False Then
'The first unerased viewport should be the current one!
VPTR = myT.GetObject(OID, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
Exit For
End If
Next
VPT.Dispose()

If VPTR Is Nothing Then
myT.Dispose()
Return
End If

Dim V As New AcDb.ViewTableRecord
V.IsPaperspaceView = False
V.Height = VPTR.Height
V.Width = VPTR.Width
V.CenterPoint = NewCenterPoint2D
acedSetCurrentView(V.UnmanagedObject, Nothing)
VPTR.Dispose()
V.Dispose()
Else
'In paperspace or a floating modelspace viewport
'
'Unlike modelspace PAPERSPACE is an AcDbViewport
'not an AcDbViewportTableRecord
Dim PSVport As AcDb.Viewport
PSVport = myT.GetObject(Ed.CurrentViewportObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
PSVport.ViewCenter = NewCenterPoint2D
End If

myT.Commit()
Catch ex As Exception
AcadApp.ShowAlertDialog(ex.ToString)
Finally
myT.Dispose()
End Try

'Dispose of the lock if it was required
'DL.Dispose()

AcadApp.UpdateScreen()
End Sub

Public Sub Window()

End Sub

Public Sub Extents()

End Sub

Public Sub Entity()

End Sub
End Class

Message was edited by: rwilkins Message was edited by: rwilkins
Ronnie Wilkins, Jr.
Message 3 of 6
RonnieWilkins
in reply to: Anonymous

TextFile Attached for easier readability...
Ronnie Wilkins, Jr.
Message 4 of 6
wang890
in reply to: Anonymous

hey man, have you figure out something to zoom to window?

say i have the lower left and lower right coordinate calulated.

ideally the function can pass the viewport object and make it zoom to that coordinates in model space.
Stantec
Dell Precision 5530, Prism M320PU, C3D 14/17/19
Message 5 of 6
wang890
in reply to: Anonymous

hey man, have you figure out something to zoom to window?

say i have the lower left and lower right coordinate calulated.

ideally the function can pass the viewport object and make it zoom to that coordinates in model space.
Stantec
Dell Precision 5530, Prism M320PU, C3D 14/17/19
Message 6 of 6
xsfhlzh
in reply to: Anonymous

{code} [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl)]
private static extern int acedTrans(
double[] point,
IntPtr fromResbuf,
IntPtr toResbuf,
int displacement,
double[] result
);

public enum UcsCode
{
Wcs = 0,
Ucs,
MDcs,
PDcs
}

public static double[] Trans(double[] point, UcsCode from, UcsCode to)
{
ResultBuffer rbfrom = new ResultBuffer(new TypedValue((int)LispDataType.Int16, from));
ResultBuffer rbto = new ResultBuffer(new TypedValue((int)LispDataType.Int16, to));
double[] res = new double[2];
acedTrans(point, rbfrom.UnmanagedObject, rbto.UnmanagedObject, 0, res);
return res;
}

public static Point2d Wcs2Dcs(Point3d point, bool atPaperSpace)
{
double[] res =
Trans(
point.ToArray(),
UcsCode.Wcs, atPaperSpace ? UcsCode.PDcs : UcsCode.MDcs);

return new Point2d(res);
}


public void ZoomWindow(Point3d minPoint, Point3d maxPoint)
{

Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

ViewTableRecord cvtr = ed.GetCurrentView();
ViewTableRecord vtr = new ViewTableRecord();
vtr.CopyFrom(cvtr);

Point3d[] oldpnts = new Point3d[] { minPoint, maxPoint };
Point3d[] pnts = new Point3d[8];
Point2d[] pnt2ds = new Point2d[8];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
int n = i * 4 + j * 2 + k;
pnts = new Point3d(oldpnts[0], oldpnts[1], oldpnts[2]);
pnt2ds = Wcs2Dcs(pnts, false);

}
}
}

double xmin, xmax, ymin, ymax;
xmin = xmax = pnt2ds[0][0];
ymin = ymax = pnt2ds[0][1];
for (int i = 1; i < 8; i++)
{
xmin = Math.Min(xmin, pnt2ds[0]);
xmax = Math.Max(xmax, pnt2ds[0]);
ymin = Math.Min(ymin, pnt2ds[1]);
ymax = Math.Max(ymax, pnt2ds[1]);
}
vtr.Width = xmax - xmin;
vtr.Height = ymax - ymin;
vtr.CenterPoint = pnt2ds[0] + (pnt2ds[7] - pnt2ds[0]) / 2;

ed.SetCurrentView(vtr);
ed.Regen();
}
{code}

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost