Exemple #1
0
        private void Init()
        {
            if (!init && !disposed && !Disposing && !IsDesigner)
            {
                extents = new Extents3d();

                Autodesk.AutoCAD.GraphicsSystem.Manager gsm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.GraphicsManager;

                Autodesk.AutoCAD.GraphicsSystem.KernelDescriptor descriptor = new Autodesk.AutoCAD.GraphicsSystem.KernelDescriptor();
                descriptor.addRequirement(Autodesk.AutoCAD.UniqueString.Intern("3D Drawing"));
                Autodesk.AutoCAD.GraphicsSystem.GraphicsKernel kernel = Autodesk.AutoCAD.GraphicsSystem.Manager.AcquireGraphicsKernel(descriptor);

                device = gsm.CreateAutoCADOffScreenDevice(kernel);
                device.DeviceRenderType = Autodesk.AutoCAD.GraphicsSystem.RendererType.Default;
                device.BackgroundColor  = BackColor;

                view             = new Autodesk.AutoCAD.GraphicsSystem.View();
                view.VisualStyle = new Autodesk.AutoCAD.GraphicsInterface.VisualStyle(Autodesk.AutoCAD.GraphicsInterface.VisualStyleType.Wireframe2D);
                model            = gsm.CreateAutoCADModel(kernel);

                device.Add(view);

                init = true;
            }
        }
Exemple #2
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (device != null)
                {
                    device.EraseAll();
                }
                if (view != null)
                {
                    view.EraseAll();
                    view.Dispose();
                    view = null;
                }
                if (model != null)
                {
                    model.Dispose();
                    model = null;
                }
                if (device != null)
                {
                    device.Dispose();
                    device = null;
                }

                init     = false;
                disposed = true;
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Exemple #3
0
        private void OutputImageMenuItem_Click(object sender, EventArgs e)
        {
            // pick the place where o output the image type
            Autodesk.AutoCAD.Windows.SaveFileDialog dialog = new Autodesk.AutoCAD.Windows.SaveFileDialog("RenderToImage", null, "jpg;png;tif;bmp", "BlockViewSnapshotBrowseDialog", Autodesk.AutoCAD.Windows.SaveFileDialog.SaveFileDialogFlags.AllowAnyExtension);
            // if all is ok?
            if (DialogResult.OK == dialog.ShowDialog())
            {
                // create an offscreen device
                using (Device device = new Device())
                {
                    // get the size of the GS view
                    Size size = mPreviewCtrl.ClientRectangle.Size;
                    // resize the device to this
                    device.OnSize(size);
                    device.BackgroundColor = Color.Black;
                    device.SetLogicalPalette(GSUtil.MyAcadColorMs);
                    device.OnRealizeBackgroundPalette();
                    device.OnRealizeForegroundPalette();

                    // make a copy of the gs view
                    using (Autodesk.AutoCAD.GraphicsSystem.View view = mPreviewCtrl.mpView.Clone(true, true))
                    {
                        try
                        {
                            // add it to the device
                            device.Add(view);
                            // now render the image to a bitmap

                            //using( System.Drawing.Bitmap bitmap = view.RenderToImage() )
                            //{
                            // now save it out!!
                            //bitmap.Save( dialog.Filename );
                            //}
                            using (Bitmap bitmap = view.GetSnapshot(mPreviewCtrl.ClientRectangle))
                            {
                                bitmap.Save(dialog.Filename);
                            }
                        }
                        finally
                        {
                            // do a clear up
                            device.EraseAll();
                        }
                    }
                }
            }
        }
        public static void SetViewTo(Autodesk.AutoCAD.GraphicsSystem.View view, Database db)
        {
            if (_extMax.Equals(Point3d.Origin) && _extMin.Equals(Point3d.Origin))
            {
                // just check we have valid extents
                if (db.Extmax.X < db.Extmin.X || db.Extmax.Y < db.Extmin.Y || db.Extmax.Z < db.Extmax.Z)
                {
                    db.Extmin = new Point3d(0, 0, 0);
                    db.Extmax = new Point3d(400, 400, 400);
                }
                // get the dwg extents
                _extMax = db.Extmax;
                _extMin = db.Extmin;
            }

            // now the active viewport info
            double   height = 0.0, width = 0.0, viewTwist = 0.0;
            Point3d  targetView = new Point3d();
            Vector3d viewDir    = new Vector3d();

            GetActiveViewPortInfo(ref height, ref width, ref targetView, ref viewDir, ref viewTwist, true);
            // from the data returned let's work out the viewmatrix
            viewDir = viewDir.GetNormal();

            Vector3d viewXDir = viewDir.GetPerpendicularVector().GetNormal();

            viewXDir = viewXDir.RotateBy(viewTwist, -viewDir);
            Vector3d viewYDir  = viewDir.CrossProduct(viewXDir);
            Point3d  boxCenter = _extMin + 0.5 * (_extMax - _extMin);
            Matrix3d viewMat;

            viewMat = Matrix3d.AlignCoordinateSystem(boxCenter, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
                                                     boxCenter, viewXDir, viewYDir, viewDir).Inverse();
            Extents3d wcsExtents  = new Extents3d(_extMin, _extMax);
            Extents3d viewExtents = wcsExtents;

            viewExtents.TransformBy(viewMat);
            double  xMax = System.Math.Abs(viewExtents.MaxPoint.X - viewExtents.MinPoint.X);
            double  yMax = System.Math.Abs(viewExtents.MaxPoint.Y - viewExtents.MinPoint.Y);
            Point3d eye  = boxCenter + viewDir;

            view.SetView(eye, boxCenter, viewYDir, xMax, yMax);

            view.Invalidate();
            view.Update();
        }
Exemple #5
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);
                    }
                }
            }
        }
Exemple #6
0
        private void Init()
        {
            if (!init && !disposed && !Disposing && !IsDesigner)
            {
                extents = new Extents3d();

                Autodesk.AutoCAD.GraphicsSystem.Manager gsm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.GraphicsManager;
            #if REBARPOS2015
                Autodesk.AutoCAD.GraphicsSystem.KernelDescriptor descriptor = new Autodesk.AutoCAD.GraphicsSystem.KernelDescriptor();
                descriptor.addRequirement(Autodesk.AutoCAD.UniqueString.Intern("3D Drawing"));
                Autodesk.AutoCAD.GraphicsSystem.GraphicsKernel kernel = Autodesk.AutoCAD.GraphicsSystem.Manager.AcquireGraphicsKernel(descriptor);

                device = gsm.CreateAutoCADDevice(kernel, this.Handle);
                device.DeviceRenderType = RendererType.Default;
                device.BackgroundColor = BackColor;

                view = new Autodesk.AutoCAD.GraphicsSystem.View();
                view.VisualStyle = new Autodesk.AutoCAD.GraphicsInterface.VisualStyle(Autodesk.AutoCAD.GraphicsInterface.VisualStyleType.Wireframe2D);

                model = gsm.CreateAutoCADModel(kernel);
            #elif REBARPOS2013
                device = gsm.CreateAutoCADDevice(this.Handle);
                device.DeviceRenderType = RendererType.Default;
                device.BackgroundColor = BackColor;

                view = new Autodesk.AutoCAD.GraphicsSystem.View();
                view.VisualStyle = new Autodesk.AutoCAD.GraphicsInterface.VisualStyle(Autodesk.AutoCAD.GraphicsInterface.VisualStyleType.Wireframe2D);

                model = gsm.CreateAutoCADModel();
            #elif REBARPOS2010
                device = gsm.CreateAutoCADDevice(this.Handle);
                device.DeviceRenderType = RendererType.Default;
                device.BackgroundColor = BackColor;

                view = new Autodesk.AutoCAD.GraphicsSystem.View();
                view.VisualStyle = new Autodesk.AutoCAD.GraphicsInterface.VisualStyle(Autodesk.AutoCAD.GraphicsInterface.VisualStyleType.Wireframe2D);

                model = gsm.CreateAutoCADModel();
            #endif

                device.Add(view);
                device.Update();

                init = true;
            }
        }
Exemple #7
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (device != null)
                {
                    device.EraseAll();
                }
                if (view != null)
                {
                    view.EraseAll();
                    view.Dispose();
                    view = null;
                }
                if (model != null)
                {
                    model.Dispose();
                    model = null;
                }
                if (device != null)
                {
                    device.Dispose();
                    device = null;
                }

                init = false;
                disposed = true;
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
        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);
            }
        }