Ejemplo n.º 1
0
        /// <summary>
        /// 对实体进行离线绘图,生成Bitmap。
        /// </summary>
        /// <param name="ents">实体集合。</param>
        /// <param name="imgWidth">图片分辨率宽度。</param>
        /// <param name="imgHeight">图片分辨率高度。</param>
        /// <param name="lowerLeft">模型空间截取窗口左下。</param>
        /// <param name="upperRight">模型空间截取窗口右上。</param>
        /// <returns></returns>
        public static Bitmap Snapshort(IEnumerable <Entity> ents,
                                       int imgWidth, int imgHeight, Point3d lowerLeft, Point3d upperRight)
        {
            var doc    = Application.DocumentManager.MdiActiveDocument;
            var graMgr = doc.GraphicsManager;

            // 生成绘图用临时视图。
            using (var view = new View())
            {
                var cvport = (short)Application.GetSystemVariable("CVPORT");
                graMgr.SetViewFromViewport(view, cvport);

                // 设置相机相关属性。
                view.SetView(Point3d.Origin + Vector3d.ZAxis, Point3d.Origin,
                             Vector3d.YAxis, imgWidth, imgHeight);

                var descriptor = new KernelDescriptor();
                descriptor.addRequirement(KernelDescriptor.Drawing3D);

                // 生成绘图设备。
                using (var kernel = Manager.AcquireGraphicsKernel(descriptor))
                {
                    using (var device = graMgr.CreateAutoCADOffScreenDevice(kernel))
                    {
                        device.OnSize(new Size(imgWidth, imgHeight));
                        device.DeviceRenderType = RendererType.Default;
                        device.BackgroundColor  = Color.Black;
                        device.Add(view);
                        device.Update();

                        // 生成临时模型,添加绘图实体。
                        using (var model = graMgr.CreateAutoCADModel(kernel))
                        {
                            foreach (var ent in ents)
                            {
                                view.Add(ent, model);
                            }
                        }

                        // 对视图按指定截取窗口缩放。
                        view.ZoomExtents(lowerLeft, upperRight);

                        return(view.GetSnapshot(
                                   new Rectangle(0, 0, imgWidth - 1, imgHeight - 1)));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        Stream(ArrayList data, Manager mgr)
        {
            data.Add(new Snoop.Data.ClassSeparator(typeof(Manager)));

#if AC2012
            data.Add(new Snoop.Data.ObjectUnknown("Display Size", mgr.DisplaySize));
#else
            data.Add(new Snoop.Data.ObjectUnknown("Display Size", mgr.DeviceIndependentDisplaySize));
#endif

            KernelDescriptor descriptor = new KernelDescriptor();

            descriptor.addRequirement(Autodesk.AutoCAD.UniqueString.Intern("3D Drawing"));

            GraphicsKernel kernal = Manager.AcquireGraphicsKernel(descriptor);

            data.Add(new Snoop.Data.Object("Get DB Model", mgr.GetDBModel(kernal)));
            data.Add(new Snoop.Data.Object("Get GUI Device", mgr.GetGUIDevice(kernal)));
        }
Ejemplo n.º 3
0
        public static System.Drawing.Image GetBlockImageFromDrawing(string blockName)
        {
            System.Drawing.Bitmap blockBmp = null;

            Database db  = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            ObjectId BlockId = ObjectId.Null;

            using (Transaction nativeTrans = db.TransactionManager.StartTransaction())
            {
                using (BlockTable bt = nativeTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable)
                {
                    if (bt.Has(blockName))
                    {
                        BlockId = bt[blockName];
                    }
                }
            }

            Manager gsm = doc.GraphicsManager;

            // now set the block sizes to something standard
            Point2d screenSize = (Point2d)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("SCREENSIZE");

            // set up the image sizes
            int width  = (int)screenSize.X;
            int height = (int)screenSize.Y;

            // now create an off screen device
            KernelDescriptor descriptor = new KernelDescriptor();

            descriptor.addRequirement(Autodesk.AutoCAD.UniqueString.Intern("3D Drawing"));
            GraphicsKernel kernal = Manager.AcquireGraphicsKernel(descriptor);

            using (Device offDevice = gsm.CreateAutoCADOffScreenDevice(kernal))
            {
                // now size the device
                offDevice.OnSize(new System.Drawing.Size(width, height));

                // now create the view
                using (View view = new View())
                {
                    // add the new view object to the device
                    offDevice.Add(view);

                    // update it
                    offDevice.Update();

                    // ok now create the model
                    using (Model model = gsm.CreateAutoCADModel(kernal))
                    {
                        using (Transaction nativeTrans = db.TransactionManager.StartTransaction())
                        {
                            using (BlockTable bt = nativeTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable)
                            {
                                // now iterate through our block tables
                                foreach (ObjectId id in bt)
                                {
                                    if (id == BlockId)
                                    {
                                        // open the btr for read
                                        using (BlockTableRecord btr = nativeTrans.GetObject(id, OpenMode.ForRead) as BlockTableRecord)
                                        {
                                            // add the btr to the view
                                            view.Add(btr, model);

                                            try
                                            {
                                                // get the extents of the btr
                                                Extents3d extents = new Extents3d();
                                                Vector3d  buffer  = new Vector3d(2, 2, 0);
                                                extents.AddBlockExtents(btr);

                                                _extMin = extents.MinPoint;
                                                _extMax = extents.MaxPoint;
                                                SetViewTo(view, db);

                                                // snap the image
                                                System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, width, height);

                                                blockBmp = view.GetSnapshot(rect);
                                            }
                                            catch
                                            {
                                            }
                                            // reset the view for the next iteration
                                            view.EraseAll();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    offDevice.EraseAll();
                }
            }
            return(blockBmp);
        }