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

How to set view port “ViewCenter” property through AutoCAD 2008 .Net API

1 REPLY 1
Reply
Message 1 of 2
pearl_vel
579 Views, 1 Reply

How to set view port “ViewCenter” property through AutoCAD 2008 .Net API

I tried to set view port view center using “ViewCenter” property but its not updated in drawing.
I gave small snippets of my code.
ViewportObj.ViewCenter = new Point2d (3000,3000);
Its updated correctly in above ViewCenter but its not updated in property palette of AutoCAD.
Can anybody help me? Thanks in advance.
1 REPLY 1
Message 2 of 2
djonio
in reply to: pearl_vel

This is for 2007 but may apply to you. It is more than you asked for but I think you will be able to noodle-out the stuff that applies ....




static public void SetViewportToExtents(ObjectId obj, object e)
{
AcadApplication app = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
editor.WriteMessage("Setting Viewport\n");
Database db = editor.Document.Database;
DocumentLock DocLock = editor.Document.LockDocument();
Extents3d mExtents;
if (e == null)
{
db.UpdateExt(true);
mExtents.Set(db.Extmin, db.Extmax);
}
else
{
Extents3d ext = (Extents3d)e;
mExtents.Set(ext.MinPoint, ext.MaxPoint);
}


using (Transaction t = db.TransactionManager.StartTransaction())
{
DBObject dbobj = obj.GetObject(OpenMode.ForRead);
Autodesk.AutoCAD.DatabaseServices.Viewport vp = dbobj as Autodesk.AutoCAD.DatabaseServices.Viewport;
if (vp != null)
{
dbobj.UpgradeOpen();

//get the screen aspect ratio to calculate the height and width
double mScrRatio;
mScrRatio = (vp.Width / vp.Height);//width/height

Point3d mMaxExt = db.Extmax;
Point3d mMinExt = db.Extmin;

//prepare Matrix for DCS to WCS transformation
Matrix3d matWCS2DCS;
matWCS2DCS = Matrix3d.PlaneToWorld(vp.ViewDirection);
matWCS2DCS = Matrix3d.Displacement(vp.ViewTarget - Point3d.Origin) * matWCS2DCS;
matWCS2DCS = Matrix3d.Rotation(-vp.TwistAngle, vp.ViewDirection, vp.ViewTarget) * matWCS2DCS;
matWCS2DCS = matWCS2DCS.Inverse();

//tranform the extents to the DCS defined by the viewdir
mExtents.TransformBy(matWCS2DCS);

//width of the extents in current view
double mWidth;
mWidth = (mExtents.MaxPoint.X - mExtents.MinPoint.X);

//height of the extents in current view
double mHeight;
mHeight = (mExtents.MaxPoint.Y - mExtents.MinPoint.Y);

//get the view center point
Point2d mCentPt = new Point2d( ((mExtents.MaxPoint.X + mExtents.MinPoint.X) * 0.5) , ((mExtents.MaxPoint.Y + mExtents.MinPoint.Y)*0.5) );

//check if the width 'fits' in current window,if not then get the new height as per the viewports aspect ratio
if (mWidth > (mHeight * mScrRatio))mHeight = mWidth / mScrRatio;

//set the viewport parameters
//vp.ViewHeight = mHeight; //set the view height
vp.ViewHeight = mHeight * 1.01; //set the view height - adjusted by 1%
//pActVp->setWidth((mHeight * mScrRatio)); this must be done automagically
vp.ViewCenter = mCentPt; //set the view center
}
t.Commit();
}

DocLock.Dispose();
}

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