Esempio n. 1
0
        public static Bitmap GetBitmap(Database db, ObjectIdCollection objIdColl)
        {
            using (db)
            {
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        Manager manager = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.GraphicsManager;
                        Device  device  = manager.CreateAutoCADOffScreenDevice();
                        using (device)
                        {
                            device.OnSize(new System.Drawing.Size(100, 100));
                            Autodesk.AutoCAD.GraphicsSystem.Model model = manager.CreateAutoCADModel();
                            using (model)
                            {
                                double dMinX = 0, dMinY = 0, dMinZ = 0;
                                double dMaxX = 0, dMaxY = 0, dMaxZ = 0;

                                bool isUnstarted = true;
                                Autodesk.AutoCAD.GraphicsSystem.View view = new Autodesk.AutoCAD.GraphicsSystem.View();
                                foreach (ObjectId objId in objIdColl)
                                {
                                    // será usado pra definir limites para criar bitmap
                                    Entity ent = trans.GetObject(objId, OpenMode.ForWrite) as Entity;
                                    //Min Point
                                    if (ent.GeometricExtents.MinPoint.X < dMinX || isUnstarted)
                                    {
                                        dMinX = ent.GeometricExtents.MinPoint.X;
                                    }
                                    if (ent.GeometricExtents.MinPoint.Y < dMinY || isUnstarted)
                                    {
                                        dMinY = ent.GeometricExtents.MinPoint.Y;
                                    }
                                    if (ent.GeometricExtents.MinPoint.Z < dMinZ || isUnstarted)
                                    {
                                        dMinZ = ent.GeometricExtents.MinPoint.Z;
                                    }

                                    //Max Point
                                    if (ent.GeometricExtents.MaxPoint.X > dMaxX || isUnstarted)
                                    {
                                        dMaxX = ent.GeometricExtents.MaxPoint.X;
                                    }
                                    if (ent.GeometricExtents.MaxPoint.Y > dMaxY || isUnstarted)
                                    {
                                        dMaxY = ent.GeometricExtents.MaxPoint.Y;
                                    }
                                    if (ent.GeometricExtents.MaxPoint.Z > dMaxZ || isUnstarted)
                                    {
                                        dMaxZ = ent.GeometricExtents.MaxPoint.Z;
                                    }

                                    isUnstarted = false;
                                    view.Add(ent, model);
                                }

                                Point3d p3dLeftLower = new Point3d(dMinX, dMinY, dMinZ);
                                Point3d p3dRightTop  = new Point3d(dMaxX, dMaxY, dMaxZ);
                                device.Add(view);

                                view.ZoomExtents(p3dLeftLower, p3dRightTop);
                                System.Drawing.Rectangle rect = new Rectangle();// view.Viewport;

                                return(view.GetSnapshot(rect));
                            }
                        }
                    }
                    catch (System.Exception ex)
                    {
                        trans.Abort();
                        return(null);
                    }
                }
            }
        }
        public BitmapImage GetBitmap()
        {
            //Suprimido pq tava dando fatal erro quando lia do bloco externo
            //if (_bDbIsInExternalDWG)
            //    db.ReadDwgFile(ArCaRefMgrController.DBPath, FileShare.Read, true, "");
            //else
            try
            {
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                using (Database db = doc.Database)
                {
                    using (Transaction trans = db.TransactionManager.StartTransaction())
                    {
                        try
                        {
                            Manager manager = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.GraphicsManager;
                            Device  device  = manager.CreateAutoCADOffScreenDevice();
                            using (device)
                            {
                                device.OnSize(new System.Drawing.Size(100, 100));
                                Autodesk.AutoCAD.GraphicsSystem.Model model = manager.CreateAutoCADModel();
                                using (model)
                                {
                                    //Suprimido pq tava dando fatal erro quando lia do bloco externo
                                    //if (rbWhite.IsChecked != true)
                                    //    device.BackgroundColor = System.Drawing.Color.Black;
                                    //else
                                    device.BackgroundColor = System.Drawing.Color.White;

                                    double dMinX = 0, dMinY = 0, dMinZ = 0;
                                    double dMaxX = 0, dMaxY = 0, dMaxZ = 0;

                                    Boolean isUnstarted = true;
                                    Autodesk.AutoCAD.GraphicsSystem.View view = new Autodesk.AutoCAD.GraphicsSystem.View();
                                    ObjectIdCollection objIdColl2             = new ObjectIdCollection();

                                    //As pitombas dos objectsId estao trocando de numero quando eu do Read no DWG, to tendo que lere os Ids no Bloco denovo
                                    if (_bDbIsInExternalDWG)
                                    {
                                        using (Transaction tr = db.TransactionManager.StartTransaction())
                                        {
                                            BlockTable       bt  = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                                            BlockTableRecord btr = tr.GetObject(bt[_objBlockModNew.Name], OpenMode.ForRead) as BlockTableRecord;
                                            foreach (ObjectId entId in btr)
                                            {
                                                objIdColl2.Add(entId);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        objIdColl2 = objIdColl;
                                    }

                                    foreach (ObjectId objId in objIdColl2)
                                    {
                                        // será usado pra definir limites para criar bitmap
                                        using (DocumentLock dl = doc.LockDocument())
                                        {
                                            Entity ent = trans.GetObject(objId, OpenMode.ForRead) as Entity;
                                            //Min Point
                                            if (ent.GeometricExtents.MinPoint.X < dMinX || isUnstarted)
                                            {
                                                dMinX = ent.GeometricExtents.MinPoint.X;
                                            }
                                            if (ent.GeometricExtents.MinPoint.Y < dMinY || isUnstarted)
                                            {
                                                dMinY = ent.GeometricExtents.MinPoint.Y;
                                            }
                                            if (ent.GeometricExtents.MinPoint.Z < dMinZ || isUnstarted)
                                            {
                                                dMinZ = ent.GeometricExtents.MinPoint.Z;
                                            }

                                            //Max Point
                                            if (ent.GeometricExtents.MaxPoint.X > dMaxX || isUnstarted)
                                            {
                                                dMaxX = ent.GeometricExtents.MaxPoint.X;
                                            }
                                            if (ent.GeometricExtents.MaxPoint.Y > dMaxY || isUnstarted)
                                            {
                                                dMaxY = ent.GeometricExtents.MaxPoint.Y;
                                            }
                                            if (ent.GeometricExtents.MaxPoint.Z > dMaxZ || isUnstarted)
                                            {
                                                dMaxZ = ent.GeometricExtents.MaxPoint.Z;
                                            }

                                            isUnstarted = false;
                                            view.Add(ent, model);
                                        }
                                    }

                                    Point3d p3dLeftLower = new Point3d(dMinX, dMinY, dMinZ);
                                    Point3d p3dRightTop  = new Point3d(dMaxX, dMaxY, dMaxZ);
                                    device.Add(view);

                                    view.ZoomExtents(p3dLeftLower, p3dRightTop);
                                    System.Drawing.Rectangle rect = new System.Drawing.Rectangle();// view.Viewport;
                                    bmpPreview = view.GetSnapshot(rect);
                                    return(ArCaUtils.GetBitmapImage(bmpPreview));
                                }
                            }
                        }
                        catch (System.Exception ex)
                        {
                            trans.Abort();
                            return(null);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                return(null);
            }
        }