public static void Zoom(AcGe.Point3d pMin, AcGe.Point3d pMax, AcGe.Point3d pCenter, double dFactor) { int nCurVport = System.Convert.ToInt32(AcApp.GetSystemVariable("CVPORT")); if (db.TileMode == true) { if (pMin.Equals(new AcGe.Point3d()) == true && pMax.Equals(new AcGe.Point3d()) == true) { pMin = db.Extmin; pMax = db.Extmax; } } else { // Check to see if Paper space is current if (nCurVport == 1) { // Get the extents of Paper space if (pMin.Equals(new AcGe.Point3d()) == true && pMax.Equals(new AcGe.Point3d()) == true) { pMin = db.Pextmin; pMax = db.Pextmax; } } else { // Get the extents of Model space if (pMin.Equals(new AcGe.Point3d()) == true && pMax.Equals(new AcGe.Point3d()) == true) { pMin = db.Extmin; pMax = db.Extmax; } } } using (AcDb.Transaction acTrans = db.TransactionManager.StartTransaction()) { using (AcDb.ViewTableRecord acView = ed.GetCurrentView()) { AcDb.Extents3d eExtents; AcGe.Matrix3d matWCS2DCS; matWCS2DCS = AcGe.Matrix3d.PlaneToWorld(acView.ViewDirection); matWCS2DCS = AcGe.Matrix3d.Displacement(acView.Target - AcGe.Point3d.Origin) * matWCS2DCS; matWCS2DCS = AcGe.Matrix3d.Rotation(-acView.ViewTwist, acView.ViewDirection, acView.Target) * matWCS2DCS; if (pCenter.DistanceTo(AcGe.Point3d.Origin) != 0) { pMin = new AcGe.Point3d(pCenter.X - (acView.Width / 2), pCenter.Y - (acView.Height / 2), 0); pMax = new AcGe.Point3d((acView.Width / 2) + pCenter.X, (acView.Height / 2) + pCenter.Y, 0); } using (AcDb.Line acLine = new AcDb.Line(pMin, pMax)) { eExtents = new AcDb.Extents3d(acLine.Bounds.Value.MinPoint, acLine.Bounds.Value.MaxPoint); } double dViewRatio; dViewRatio = (acView.Width / acView.Height); matWCS2DCS = matWCS2DCS.Inverse(); eExtents.TransformBy(matWCS2DCS); double dWidth; double dHeight; AcGe.Point2d pNewCentPt; if (pCenter.DistanceTo(AcGe.Point3d.Origin) != 0) { dWidth = acView.Width; dHeight = acView.Height; if (dFactor == 0) { pCenter = pCenter.TransformBy(matWCS2DCS); } pNewCentPt = new AcGe.Point2d(pCenter.X, pCenter.Y); } else { dWidth = eExtents.MaxPoint.X - eExtents.MinPoint.X; dHeight = eExtents.MaxPoint.Y - eExtents.MinPoint.Y; pNewCentPt = new AcGe.Point2d(((eExtents.MaxPoint.X + eExtents.MinPoint.X) * 0.5), ((eExtents.MaxPoint.Y + eExtents.MinPoint.Y) * 0.5)); } if (dWidth > (dHeight * dViewRatio)) { dHeight = dWidth / dViewRatio; } if (dFactor != 0) { acView.Height = dHeight * dFactor; acView.Width = dWidth * dFactor; } acView.CenterPoint = pNewCentPt; ed.SetCurrentView(acView); } acTrans.Commit(); } }
public static void AttachRasterImage(LandRasterImage landRastr) { AcEd.Editor ed = AcApp.DocumentManager.MdiActiveDocument.Editor; //bool bRasterDefCreated = false; AcDb.Database curDb = AcApp.DocumentManager.MdiActiveDocument.Database; using (AcDb.Transaction tr = curDb.TransactionManager.StartTransaction()) { AcDb.RasterImageDef rasterDef; AcDb.ObjectId imgDefId; AcDb.ObjectId imgDctID = AcDb.RasterImageDef.GetImageDictionary(curDb); if (imgDctID.IsNull) { imgDctID = AcDb.RasterImageDef.CreateImageDictionary(curDb); } AcDb.DBDictionary imgDict = (AcDb.DBDictionary)tr.GetObject(imgDctID, AcDb.OpenMode.ForRead); if (imgDict.Contains(landRastr.ImageName)) { imgDefId = imgDict.GetAt(landRastr.ImageName); rasterDef = (AcDb.RasterImageDef)tr.GetObject(imgDefId, AcDb.OpenMode.ForWrite); if (rasterDef.IsLoaded) { //return; } } else { AcPApp.AcadApplication app = AcApp.AcadApplication as AcPApp.AcadApplication; AcPApp.AcadDocument doc = app.ActiveDocument; AcPDb.AcadModelSpace mSpace = doc.ModelSpace; AcPDb.AcadRasterImage ri = mSpace.AddRaster(landRastr.FileName, landRastr.InsertPoint.ToArray(), 1000, 0); ri.Name = landRastr.ImageName; ri.Transparency = true; ServiceCAD.CreateLayer(landRastr.Layer); ri.Layer = landRastr.Layer; ri.ImageHeight = 1000; ri.ImageWidth = 1000; tr.Commit(); return; } AcDb.BlockTable blkTbl = (AcDb.BlockTable)tr.GetObject(curDb.BlockTableId, AcDb.OpenMode.ForRead); AcDb.BlockTableRecord acBlkTblRec = (AcDb.BlockTableRecord)tr.GetObject(blkTbl[AcDb.BlockTableRecord.ModelSpace], AcDb.OpenMode.ForWrite); using (AcDb.RasterImage rasterImage = new AcDb.RasterImage()) { rasterImage.ImageDefId = imgDefId; AcGe.Vector3d width; AcGe.Vector3d height; AcGe.Matrix3d ucs = ed.CurrentUserCoordinateSystem; double size = 1000; width = new AcGe.Vector3d(size, 0, 0); height = new AcGe.Vector3d(0, size, 0); AcGe.Point3d insPt = landRastr.InsertPoint; AcGe.CoordinateSystem3d coordinateSystem = new AcGe.CoordinateSystem3d(insPt.TransformBy(ucs), width.TransformBy(ucs), height.TransformBy(ucs)); rasterImage.Orientation = coordinateSystem; rasterImage.Rotation = 0; rasterImage.ShowImage = true; acBlkTblRec.AppendEntity(rasterImage); tr.AddNewlyCreatedDBObject(rasterImage, true); rasterImage.AssociateRasterDef(rasterDef); } tr.Commit(); } }