Ejemplo n.º 1
0
        public static void Transform(this Teigha.GraphicsSystem.View pView, Point2d startPoint, Point2d endPoint)
        {
            double dx = endPoint.X - startPoint.X;
            double dy = endPoint.Y - startPoint.Y;

            Transform(pView, dx, dy);
        }
Ejemplo n.º 2
0
        // helper function transforming parameters from screen to world coordinates
        public static void Transform(this Teigha.GraphicsSystem.View pView, double x, double y)
        {
            Vector3d vec = new Vector3d(-x, -y, 0.0);

            vec = vec.TransformBy((pView.ScreenMatrix * pView.ProjectionMatrix).Inverse());
            pView.Dolly(vec);
        }
Ejemplo n.º 3
0
 public void Form1_MouseWheel(object sender, MouseEventArgs e)
 {
     if (!IsMouseEventNeeded(e.X, e.Y))
     {
         return;
     }
     if (helperDevice == null)
     {
         return;
     }
     using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)
     {
         // camera position in world coordinates
         Point3d pos = pView.Position;
         // TransformBy() returns a transformed copy
         pos = pos.TransformBy(pView.WorldToDeviceMatrix);
         int vx = (int)pos.X;
         int vy = (int)pos.Y;
         vx = e.X - vx;
         vy = e.Y - vy;
         // we move point of view to the mouse location, to create an illusion of scrolling in/out there
         dolly(pView, -vx, -vy);
         // note that we essentially ignore delta value (sign is enough for illustrative purposes)
         pView.Zoom(e.Delta > 0 ? 1.0 / 0.9 : 0.9);
         dolly(pView, vx, vy);
         //
         Invalidate();
     }
 }
Ejemplo n.º 4
0
        // helper function transforming parameters from screen to world coordinates
        void dolly(Teigha.GraphicsSystem.View pView, int x, int y)
        {
            Vector3d vec = new Vector3d(-x, -y, 0.0);

            vec = vec.TransformBy((pView.ScreenMatrix * pView.ProjectionMatrix).Inverse());
            pView.Dolly(vec);
        }
Ejemplo n.º 5
0
 private void Form1_MouseDown(object sender, MouseEventArgs e)
 {
     if (e.Button == MouseButtons.Left)
     {
         using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)
         {
             ClearSelection();
             selRect = new RectFram(toEyeToWorld(e.X, e.Y));
             pView.Add(selRect);
             startSelPoint = new Point2d(e.X, e.Y);
             Invalidate();
             selRect.setValue(toEyeToWorld(e.X + _SelectRegion, e.Y + _SelectRegion));
             pView.Select(
                 new Point2dCollection(new Point2d[] { startSelPoint, new Point2d(e.X + _SelectRegion, e.Y + _SelectRegion) }),
                 new SR(selected, database.CurrentSpaceId),
                 startSelPoint.X < e.X
       ? Teigha.GraphicsSystem.SelectionMode.Window
       : Teigha.GraphicsSystem.SelectionMode.Crossing);
             pView.Erase(selRect);
             selRect = null;
             gripManager.updateSelection(selected);
             helperDevice.Invalidate();
             Invalidate();
         }
     }
     else if (e.Button == MouseButtons.Right)
     {
         _IsMoving     = true;
         startSelPoint = new Point2d(e.X, e.Y);
     }
 }
Ejemplo n.º 6
0
        bool get_layout_extents(Database db, Teigha.GraphicsSystem.View pView, ref BoundBlock3d bbox)
        {
            BlockTable       bt      = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
            BlockTableRecord pSpace  = (BlockTableRecord)bt[BlockTableRecord.PaperSpace].GetObject(OpenMode.ForRead);
            Layout           pLayout = (Layout)pSpace.LayoutId.GetObject(OpenMode.ForRead);
            Extents3d        ext     = new Extents3d();

            if (pLayout.GetViewports().Count > 0)
            {
                bool bOverall = true;
                foreach (ObjectId id in pLayout.GetViewports())
                {
                    if (bOverall)
                    {
                        bOverall = false;
                        continue;
                    }
                    Teigha.DatabaseServices.Viewport pVp = (Teigha.DatabaseServices.Viewport)id.GetObject(OpenMode.ForRead);
                }
                ext.TransformBy(pView.ViewingMatrix);
                bbox.Set(ext.MinPoint, ext.MaxPoint);
            }
            else
            {
                ext = pLayout.Extents;
            }
            bbox.Set(ext.MinPoint, ext.MaxPoint);
            return(ext.MinPoint != ext.MaxPoint);
        }
Ejemplo n.º 7
0
        void locateGripsAtInt(int x, int y, ref ExGripDataCollection aRes, ExGripDataCollection coll, Point3d ptFirst, Tolerance tol)
        {
            foreach (ExGripData exgrData in coll)
            {
                Point3d ptCurrent = exgrData.Point;
                if (aRes.Count == 0)
                {
                    // First grip is obtained by comparing
                    // grip point device position with cursor position.
                    using (Teigha.GraphicsSystem.View pView = m_pDevice.ActiveView)
                    {
                        Point3d ptDC = ptCurrent.TransformBy(pView.WorldToDeviceMatrix);

                        double dDeltaX = Math.Abs(x - ptDC.X);
                        double dDeltaY = Math.Abs(y - ptDC.Y);
                        bool   bOk     = (dDeltaX <= m_GRIPSIZE) && (dDeltaY <= m_GRIPSIZE);
                        if (bOk)
                        {
                            ptFirst = ptCurrent;
                            aRes.Add(exgrData);
                        }
                    }
                }
                else
                {
                    if (ptCurrent.IsEqualTo(ptFirst, tol))
                    {
                        aRes.Add(exgrData);
                    }
                }
            }
        }
Ejemplo n.º 8
0
 public Vector3d activeViewDirection()
 {
     using (Teigha.GraphicsSystem.View pView = m_pDevice.ActiveView)
     {
         Vector3d vec = pView.Position - pView.Target;
         return(vec.GetNormal());
     }
 }
Ejemplo n.º 9
0
 private Point ToClient(Point3d pt3d)
 {
     using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)
     {
         Point3d ptDvc = pt3d.TransformBy(pView.WorldToDeviceMatrix);
         return(new Point((int)ptDvc.X, (int)ptDvc.Y));
     }
 }
Ejemplo n.º 10
0
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            switch (mouseMode)
            {
            case Mode.Quiescent:
            {
                if (gripManager.OnMouseDown(e.X, e.Y))
                {
                    mouseMode = Mode.DragDrop;
                }
                else
                {
                    using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)
                    {
                        selRect = new RectFram(toEyeToWorld(e.X, e.Y));
                        pView.Add(selRect);
                        startSelPoint = new Point2d(e.X, e.Y);
                        Invalidate();
                        mouseMode = Mode.Selection;
                    }
                }
                break;
            }

            case Mode.Selection:
            {
                using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)
                {
                    pView.Select(new Point2dCollection(new Point2d[] { startSelPoint, new Point2d(e.X, e.Y) }),
                                 new SR(selected, database.CurrentSpaceId), startSelPoint.X < e.X ? Teigha.GraphicsSystem.SelectionMode.Window : Teigha.GraphicsSystem.SelectionMode.Crossing);
                    pView.Erase(selRect);
                    selRect = null;

                    gripManager.updateSelection(selected);
                    helperDevice.Invalidate();
                    Invalidate();
                }
                mouseMode = Mode.Quiescent;
                break;
            }

            case Mode.DragDrop:
            {
                using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)
                {
                    gripManager.DragFinal(toEyeToWorld(e.X, e.Y), true);
                    helperDevice.Model.Invalidate(InvalidationHint.kInvalidateAll);
                    Invalidate();
                }
                mouseMode = Mode.Quiescent;
                break;
            }

            default:
                break;
            }
        }
Ejemplo n.º 11
0
 private Point3d toEyeToWorld(int x, int y)
 {
     using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)
     {
         Point3d wcsPt = new Point3d(x, y, 0.0);
         wcsPt = wcsPt.TransformBy((pView.ScreenMatrix * pView.ProjectionMatrix).Inverse());
         wcsPt = new Point3d(wcsPt.X, wcsPt.Y, 0.0);
         using (AbstractViewPE pVpPE = new AbstractViewPE(pView))
         {
             return(wcsPt.TransformBy(pVpPE.EyeToWorld));
         }
     }
 }
Ejemplo n.º 12
0
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
            case Keys.Escape:
                switch (mouseMode)
                {
                case Mode.DragDrop:
                {
                    using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)
                    {
                        gripManager.DragFinal(new Point3d(), false);
                        helperDevice.Model.Invalidate(InvalidationHint.kInvalidateAll);
                        Invalidate();
                    }
                    break;
                }

                case Mode.Selection:
                    if (selRect != null)
                    {
                        using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)
                        {
                            pView.Erase(selRect);
                            selRect = null;
                        }
                    }
                    break;
                }
                mouseMode = Mode.Quiescent;

                foreach (ObjectId id in selected)
                {
                    gripManager.removeEntityGrips(id, true);
                }
                selected.Clear();
                if (helperDevice != null)
                {
                    helperDevice.Invalidate();
                }
                Invalidate();
                break;

            case Keys.Oemplus:
                break;

            case Keys.OemMinus:
                break;
            }
        }
Ejemplo n.º 13
0
 public static void ZoomToExtents(this Database database)
 {
     using (DBObject dbObj = GetActiveViewportId(database).GetObject(OpenMode.ForWrite))
     {
         // using protocol extensions we handle PS and MS viewports in the same manner
         using (AbstractViewportData viewportData = new AbstractViewportData(dbObj))
         {
             using (Teigha.GraphicsSystem.View view = viewportData.GsView)
             {
                 // do actual zooming - change GS view
                 using (AbstractViewPE viewPE = new AbstractViewPE(view))
                 {
                     BoundBlock3d boundBlock = new BoundBlock3d();
                     bool         bBboxValid = viewPE.GetViewExtents(boundBlock);
                     // paper space overall view
                     if (dbObj is Viewport && ((Viewport)dbObj).Number == 1)
                     {
                         if (!bBboxValid || !(boundBlock.GetMinimumPoint().X < boundBlock.GetMaximumPoint().X&& boundBlock.GetMinimumPoint().Y < boundBlock.GetMaximumPoint().Y))
                         {
                             bBboxValid = GetLayoutExtents(database, view, ref boundBlock);
                         }
                     }
                     else if (!bBboxValid) // model space viewport
                     {
                         bBboxValid = GetLayoutExtents(database, view, ref boundBlock);
                     }
                     if (!bBboxValid)
                     {
                         // set to somewhat reasonable (e.g. paper size)
                         if (database.Measurement == MeasurementValue.Metric)
                         {
                             boundBlock.Set(Point3d.Origin, new Point3d(297.0, 210.0, 0.0)); // set to papersize ISO A4 (portrait)
                         }
                         else
                         {
                             boundBlock.Set(Point3d.Origin, new Point3d(11.0, 8.5, 0.0)); // ANSI A (8.50 x 11.00) (landscape)
                         }
                         boundBlock.TransformBy(view.ViewingMatrix);
                     }
                     viewPE.ZoomExtents(boundBlock);
                     boundBlock.Dispose();
                 }
                 // save changes to database
                 viewportData.SetView(view);
             }
         }
     }
 }
Ejemplo n.º 14
0
        // the same as Editor.ActiveViewportId if ApplicationServices are available

        private void zoom_extents_handler(object sender, EventArgs e)
        {
            using (DBObject pVpObj = Aux.active_viewport_id(database).GetObject(OpenMode.ForWrite))
            {
                // using protocol extensions we handle PS and MS viewports in the same manner
                AbstractViewportData       pAVD  = new AbstractViewportData(pVpObj);
                Teigha.GraphicsSystem.View pView = pAVD.GsView;
                // do actual zooming - change GS view
                zoom_extents(pView, pVpObj);
                // save changes to database
                pAVD.SetView(pView);
                pAVD.Dispose();
                pVpObj.Dispose();
                Invalidate();
            }
        }
Ejemplo n.º 15
0
        public double activeViewUnitSize()
        {
            Vector3d v;

            using (Teigha.GraphicsSystem.View pView = m_pDevice.ActiveView)
            {
                Point2d ptDim; // getNumPixelsInUnitSquare
                {
                    Extents2d ext = pView.Viewport;
                    ptDim = new Point2d(Math.Abs(((double)ext.MaxPoint.X - ext.MinPoint.X) / pView.FieldWidth),
                                        Math.Abs(((double)ext.MaxPoint.Y - ext.MinPoint.Y) / pView.FieldHeight));
                }
                v = new Vector3d(GRIPSIZE / ptDim.X, 0, 0);
                v = v.TransformBy(pView.ViewingMatrix);
            }
            return(v.Length / GRIPSIZE);
        }
Ejemplo n.º 16
0
        public static void ZoomMap(Teigha.GraphicsSystem.View pView, Point2d centerPoint, double zoomFactor)
        {
            // camera position in world coordinates
            Point3d pos = pView.Position;

            // TransformBy() returns a transformed copy
            pos = pos.TransformBy(pView.WorldToDeviceMatrix);
            double vx = (int)pos.X;
            double vy = (int)pos.Y;

            vx = centerPoint.X - vx;
            vy = centerPoint.Y - vy;
            // we move point of view to the mouse location, to create an illusion of scrolling in/out there
            Transform(pView, -vx, -vy);
            // note that we essentially ignore delta value (sign is enough for illustrative purposes)
            pView.Zoom(zoomFactor);
            Transform(pView, vx, vy);
        }
Ejemplo n.º 17
0
 private void panel1_MouseMove(object sender, MouseEventArgs e)
 {
     if (!IsMouseEventNeeded(e.X, e.Y))
     {
         return;
     }
     if (_IsMoving)
     {
         using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)
         {
             int vx = e.X;
             int vy = e.Y;
             vx = (int)startSelPoint.X - vx;
             vy = (int)startSelPoint.Y - vy;
             // we move point of view to the mouse location, to create an illusion of scrolling in/out there
             dolly(pView, -vx, -vy);
             startSelPoint = new Point2d(e.X, e.Y);
             Invalidate();
         }
     }
 }
Ejemplo n.º 18
0
        private void ZoomWindow(Point3d pt1, Point3d pt2)
        {
            using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)
            {
                using (AbstractViewPE pVpPE = new AbstractViewPE(pView))
                {
                    pt1 = pt1.TransformBy(pVpPE.WorldToEye);
                    pt2 = pt2.TransformBy(pVpPE.WorldToEye);
                    Vector3d eyeVec = pt2 - pt1;

                    if (((eyeVec.X < -1E-10) || (eyeVec.X > 1E-10)) && ((eyeVec.Y < -1E-10) || (eyeVec.Y > 1E-10)))
                    {
                        Point3d newPos = pt1 + eyeVec / 2.0;
                        pView.Dolly(newPos.GetAsVector());
                        double wf = pView.FieldWidth / Math.Abs(eyeVec.X);
                        double hf = pView.FieldHeight / Math.Abs(eyeVec.Y);
                        pView.Zoom(wf < hf ? wf : hf);
                        Invalidate();
                    }
                }
            }
        }
Ejemplo n.º 19
0
 public static void ZoomToExtents(this Database database)
 {
     using (DBObject dbObj = GetActiveViewportId(database).GetObject(OpenMode.ForWrite))
     {
         // using protocol extensions we handle PS and MS viewports in the same manner
         using (AbstractViewportData viewportData = new AbstractViewportData(dbObj))
         {
             using (Teigha.GraphicsSystem.View view = viewportData.GsView)
             {
                 // do actual zooming - change GS view
                 using (AbstractViewPE viewPE = new AbstractViewPE(view))
                 {
                     BoundBlock3d boundBlock = new BoundBlock3d();
                     bool         bBboxValid = viewPE.GetViewExtents(boundBlock);
                     // paper space overall view
                     if (dbObj is Viewport viewport && viewport.Number == 1)
                     {
                         if (!bBboxValid || !(boundBlock.GetMinimumPoint().X < boundBlock.GetMaximumPoint().X&& boundBlock.GetMinimumPoint().Y < boundBlock.GetMaximumPoint().Y))
                         {
                             bBboxValid = GetLayoutExtents(database, view, ref boundBlock);
                         }
                     }
Ejemplo n.º 20
0
        void zoom_extents(Teigha.GraphicsSystem.View pView, DBObject pVpObj)
        {
            // here protocol extension is used again, that provides some helpful functions
            using (AbstractViewPE pVpPE = new AbstractViewPE(pView))
            {
                BoundBlock3d bbox       = new BoundBlock3d();
                bool         bBboxValid = pVpPE.GetViewExtents(bbox);

                // paper space overall view
                if (pVpObj is Teigha.DatabaseServices.Viewport && ((Teigha.DatabaseServices.Viewport)pVpObj).Number == 1)
                {
                    if (!bBboxValid || !(bbox.GetMinimumPoint().X < bbox.GetMaximumPoint().X&& bbox.GetMinimumPoint().Y < bbox.GetMaximumPoint().Y))
                    {
                        bBboxValid = get_layout_extents(database, pView, ref bbox);
                    }
                }
                else if (!bBboxValid) // model space viewport
                {
                    bBboxValid = get_layout_extents(database, pView, ref bbox);
                }

                if (!bBboxValid)
                {
                    // set to somewhat reasonable (e.g. paper size)
                    if (database.Measurement == MeasurementValue.Metric)
                    {
                        bbox.Set(Point3d.Origin, new Point3d(297.0, 210.0, 0.0)); // set to papersize ISO A4 (portrait)
                    }
                    else
                    {
                        bbox.Set(Point3d.Origin, new Point3d(11.0, 8.5, 0.0)); // ANSI A (8.50 x 11.00) (landscape)
                    }
                    bbox.TransformBy(pView.ViewingMatrix);
                }

                pVpPE.ZoomExtents(bbox);
            }
        }
Ejemplo n.º 21
0
        public void DragFinal(Point3d ptFinal, bool bOk)
        {
            if (bOk)
            {
                using (Teigha.GraphicsSystem.View pView = m_pDevice.ActiveView)
                {
                    foreach (ExGripDrag grDrag in m_aDrags)
                    {
                        grDrag.moveEntity(ptFinal);

                        pView.Erase(grDrag);
                        grDrag.notifyDragEnded();
                        updateEntityGrips(grDrag.entityId());
                        grDrag.uninit();
                    }
                }
            }
            else
            {
                foreach (ExGripDrag grDrag in m_aDrags)
                {
                    grDrag.notifyDragAborted();
                }
            }

            m_aDrags.Clear();

            if (bOk)
            {
                updateInvisibleGrips();
            }

            foreach (ExGripData exgrData in aActiveKeys)
            {
                exgrData.Status = GripData.DrawType.WarmGrip;
            }
            aActiveKeys = null;
        }
Ejemplo n.º 22
0
        /// <summary>
        /// 刷新图纸缓存
        /// </summary>
        public void RefreshCache()
        {
            if (_cadControl.Database == null || _cadControl.HelperDevice == null)
            {
                MessageBox.Show("请添加数据");
                return;
            }
            if (_cadControl.Database.TileMode)
            {
                try
                {
                    using (Teigha.GraphicsSystem.View pView = _cadControl.HelperDevice.ActiveView)
                    {
                        //if (pView.FieldWidth * 2 < (_cadControl.Database.Extmax.X - _cadControl.Database.Extmin.X))
                        {
                            using (var mode = _cadControl.HelperDevice.CreateModel())
                            {
                                mode.Invalidate(InvalidationHint.kInvalidateViewportCache);
                            }
                            //_cadControl.HelperDevice.Model.Invalidate(InvalidationHint.kInvalidateViewportCache);
                            //_cadControl.HelperDevice.Model.Invalidate(InvalidationHint.kInvalidateAll);
                            _cadControl.HelperDevice.Update();
                        }
                    }

                    //Invalidate();
                }
                catch (System.Runtime.InteropServices.SEHException e)
                {
                    MessageBox.Show(string.Format("刷新失败,请重试!,错误信息:{0}", e.ToString()));
                }
            }
            else
            {
                MessageBox.Show("请设置图纸为‘Model’模式!");
            }
        }
Ejemplo n.º 23
0
        public bool OnMouseDown(int x, int y)
        {
            endHover();

            ExGripDataCollection aKeys = new ExGripDataCollection();

            locateGripsAt(x, y, ref aKeys);
            if (aKeys.Count > 0)
            {
                //if ( bShift )  TODO
                {
                    bool bMakeHot = true;

                    foreach (KeyValuePair <ObjectId, ExGripDataExt> grData in m_gripDataDict)
                    {
                        foreach (ExGripData exgrData in grData.Value.DataArray)
                        {
                            if (GripData.DrawType.HotGrip == exgrData.Status)
                            {
                                bMakeHot = false;
                                break;
                            }
                        }

                        foreach (ExGripDataSubent grDatSub in grData.Value.DataSub)
                        {
                            foreach (ExGripData exgrData in grDatSub.SubData)
                            {
                                if (GripData.DrawType.HotGrip == exgrData.Status)
                                {
                                    bMakeHot = false;
                                    break;
                                }
                            }
                            if (bMakeHot == false)
                            {
                                break;
                            }
                        }

                        if (bMakeHot == false)
                        {
                            break;
                        }
                    }

                    if (bMakeHot)
                    {
                        foreach (ExGripData exgrData in aKeys)
                        {
                            exgrData.Status = GripData.DrawType.HotGrip;
                        }
                    }

                    aActiveKeys = new ExGripDataCollection();
                    locateGripsByStatus(GripData.DrawType.HotGrip, ref aActiveKeys);
                    if (aActiveKeys.Count == 0)
                    {
                        // Valid situation.
                        // If trigger grip performed entity modification and returned eGripHotToWarm
                        // then nothing is to be done cause entity modification will cause reactor to regen grips.
                        return(false);
                    }

                    foreach (ExGripData exgrData in aActiveKeys)
                    {
                        exgrData.Status = GripData.DrawType.DragImageGrip;
                    }

                    foreach (KeyValuePair <ObjectId, ExGripDataExt> grData in m_gripDataDict)
                    {
                        bool bActive = false;
                        foreach (ExGripData exgrData in grData.Value.DataArray)
                        {
                            if (GripData.DrawType.DragImageGrip == exgrData.Status)
                            {
                                bActive = true;
                                m_aDrags.Add(new ExGripDrag(grData.Key, this));
                                break;
                            }
                        }
                        foreach (ExGripDataSubent grDatSub in grData.Value.DataSub)
                        {
                            foreach (ExGripData exgrData in grDatSub.SubData)
                            {
                                if (GripData.DrawType.DragImageGrip == exgrData.Status)
                                {
                                    bActive = true;
                                    m_aDrags.Add(new ExGripDrag(exgrData.SubentPath, this));
                                    break;
                                }
                            }
                            if (bActive == true)
                            {
                                break;
                            }
                        }
                    }

                    using (Teigha.GraphicsSystem.View pView = m_pDevice.ActiveView)
                    {
                        foreach (ExGripDrag grDrag in m_aDrags)
                        {
                            grDrag.notifyDragStarted();
                            grDrag.cloneEntity();
                            pView.Add(grDrag);
                        }
                    }

                    m_ptBasePoint = aKeys[0].Point;
                    m_ptLastPoint = m_ptBasePoint;
                }
            }
            else
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 24
0
        void PrintPage(object sender, PrintPageEventArgs ev)
        {
            if (m_pPrinterDevice != null)
            {
                using (DBObject pVpObj = Aux.active_viewport_id(database).GetObject(OpenMode.ForWrite))
                {
                    using (AbstractViewportData pAV = new AbstractViewportData(pVpObj))
                    {
                        Teigha.GraphicsSystem.View pGSView = pAV.GsView;

                        PrintDocument prDoc = (PrintDocument)sender;

                        // Get printer paper info
                        Double dPrinterWidth  = ev.PageBounds.Width;
                        Double dPrinterHeight = ev.PageBounds.Height;
                        Double dLogPixelX     = ev.PageSettings.PrinterResolution.X; //dot per inch
                        Double dLogPixelY     = ev.PageSettings.PrinterResolution.Y; //dot per inch
                        Double kMmPerInch     = 25.4;
                        Double kMmPerHInch    = 0.254;
                        Double koeffX         = dLogPixelX / kMmPerInch;
                        Double koeffY         = dLogPixelY / kMmPerInch;

                        Layout  pLayout      = (Layout)m_pPrinterDevice.LayoutId.GetObject(OpenMode.ForRead);
                        Boolean bScaledToFit = pLayout.UseStandardScale && (StdScaleType.ScaleToFit == pLayout.StdScaleType);
                        Boolean bCentered    = pLayout.PlotCentered;
                        Boolean bMetric      = (pLayout.PlotPaperUnits != PlotPaperUnit.Inches);
                        Boolean bPrintLW     = pLayout.PrintLineweights || pLayout.ShowPlotStyles;

                        Point2d offsets = pLayout.PlotOrigin; // in mm

                        Extents2d ex2d          = pLayout.PlotPaperMargins;
                        Double    dLeftMargin   = ex2d.MinPoint.X; // in mm
                        Double    dRightMargin  = ex2d.MaxPoint.X; // in mm
                        Double    dTopMargin    = ex2d.MinPoint.Y; // in mm
                        Double    dBottomMargin = ex2d.MaxPoint.Y; // in mm
                        PlotType  plotType      = pLayout.PlotType;

                        PlotRotation plotRotation = pLayout.PlotRotation;
                        if (plotRotation == PlotRotation.Degrees090 || plotRotation == PlotRotation.Degrees270)
                        {
                            plotRotation = (plotRotation == PlotRotation.Degrees090) ? PlotRotation.Degrees270 : PlotRotation.Degrees090;
                        }

                        switch (plotRotation)
                        {
                        case PlotRotation.Degrees090:
                            Swap <Double>(dTopMargin, dRightMargin);
                            Swap <Double>(dBottomMargin, dLeftMargin);
                            Swap <Double>(dBottomMargin, dTopMargin);
                            Swap <Double>(dTopMargin, dRightMargin);
                            offsets = new Point2d(-offsets.X, -offsets.Y);
                            break;

                        case PlotRotation.Degrees180:
                            Swap <Double>(dRightMargin, dLeftMargin);
                            offsets = new Point2d(-offsets.X, -offsets.Y);
                            break;

                        case PlotRotation.Degrees270:
                            Swap <Double>(dTopMargin, dRightMargin);
                            Swap <Double>(dBottomMargin, dLeftMargin);
                            Swap <Double>(dBottomMargin, dTopMargin);
                            offsets = new Point2d(offsets.X, offsets.Y);
                            break;
                        }

                        // Get scale factor
                        double factor;
                        if (pLayout.UseStandardScale)
                        {
                            factor = pLayout.StdScale;
                        }
                        else
                        {
                            CustomScale scale = pLayout.CustomPrintScale;
                            factor = scale.Numerator / scale.Denominator;
                        }

                        // Calculate paper drawable area using margins from layout (in mm).
                        Double drx1 = (ev.MarginBounds.Left * kMmPerHInch + dLeftMargin);                  // in mm
                        Double drx2 = (ev.MarginBounds.Width * kMmPerHInch - dLeftMargin - dRightMargin);  // in mm
                        Double dry1 = (ev.MarginBounds.Top * kMmPerHInch + dTopMargin);                    // in mm
                        Double dry2 = (ev.MarginBounds.Height * kMmPerHInch - dTopMargin - dBottomMargin); // in mm

                        Boolean        bType           = (plotType == PlotType.Display || plotType == PlotType.Layout);
                        AbstractViewPE pAbstractViewPE = new AbstractViewPE(bType ? pViewDr : pGSView);

                        // set LineWeight scale factor for model space
                        if (bPrintLW && database.TileMode)
                        {
                            Teigha.GraphicsSystem.View pTo = m_pPrinterDevice.ViewAt(0);
                            pTo.LineweightToDcScale = Math.Max(dLogPixelX, dLogPixelY) / kMmPerInch * 0.01;
                        }

                        Point3d  viewTarget     = pAbstractViewPE.Target;
                        Point3d  viewportCenter = pAbstractViewPE.Target;      // in plotPaperUnits
                        Boolean  isPerspective  = pAbstractViewPE.IsPerspective;
                        Double   viewportH      = pAbstractViewPE.FieldHeight; // in plotPaperUnits
                        Double   viewportW      = pAbstractViewPE.FieldWidth;  // in plotPaperUnits
                        Vector3d viewDir        = pAbstractViewPE.Direction;   // in plotPaperUnits
                        Vector3d upV            = pAbstractViewPE.UpVector;    // in plotPaperUnits
                        Matrix3d eyeToWorld     = pAbstractViewPE.EyeToWorld;
                        Matrix3d WorldToeye     = pAbstractViewPE.WorldToEye;

                        Boolean isPlanView = viewDir.GetNormal().Equals(Vector3d.ZAxis);
                        Point3d oldTarget  = viewTarget;

                        Double fieldWidth = viewportW, fieldHeight = viewportH;

                        if (plotType == PlotType.Display)
                        {
                            viewTarget  = viewportCenter;
                            fieldWidth  = viewportW;
                            fieldHeight = viewportH;
                        }
                        else if (plotType == PlotType.Extents || (plotType == PlotType.Limits && !isPlanView))
                        {
                            BoundBlock3d extents = new BoundBlock3d();
                            if (pAbstractViewPE.GetViewExtents(extents)) // pIter also skip 'off layers'
                            {
                                extents.TransformBy(eyeToWorld);
                                viewTarget = (extents.GetMinimumPoint() + extents.GetMaximumPoint().GetAsVector()) / 2.0;
                                extents.TransformBy(WorldToeye);

                                fieldWidth  = Math.Abs(extents.GetMaximumPoint().X - extents.GetMinimumPoint().X);
                                fieldHeight = Math.Abs(extents.GetMaximumPoint().Y - extents.GetMinimumPoint().Y);
                            }
                        }
                        else if (plotType == PlotType.View)
                        {
                            viewTarget  = viewportCenter;
                            fieldWidth  = viewportW;
                            fieldHeight = viewportH;
                        }
                        else if (plotType == PlotType.Limits)
                        {
                            fieldWidth  = (drx2 - drx1) / factor; // drx in mm -> fieldWidth in mm
                            fieldHeight = (dry2 - dry1) / factor;

                            viewTarget = new Point3d(fieldWidth / 2.0 - offsets.X / factor, fieldHeight / 2.0 - offsets.Y / factor, 0); // in mm
                            if (!bMetric)
                            {
                                viewTarget  /= kMmPerInch; // must be in plotpaper units
                                fieldWidth  /= kMmPerInch;
                                fieldHeight /= kMmPerInch;
                            }

                            bCentered = bScaledToFit = false; // kLayout doesn't support pIter.
                        }

                        if (plotType != PlotType.View)
                        {
                            viewTarget = viewTarget.OrthoProject(new Plane(oldTarget, viewDir));
                        }

                        pGSView.SetView(viewTarget + viewDir, viewTarget, upV, fieldWidth, fieldHeight, isPerspective ? Teigha.GraphicsSystem.Projection.Perspective : Teigha.GraphicsSystem.Projection.Parallel);

                        if (!bMetric)
                        {
                            fieldWidth  *= kMmPerInch;
                            fieldHeight *= kMmPerInch;
                        }

                        if (bScaledToFit)
                        {
                            factor = Math.Min((drx2 - drx1) / fieldWidth, (dry2 - dry1) / fieldHeight);
                        }

                        if (bCentered) // Offset also can be incorectly saved.
                        {
                            offsets = new Point2d(((drx2 - drx1) - fieldWidth * factor) / 2.0,
                                                  ((dry2 - dry1) - fieldHeight * factor) / 2.0);

                            if (plotRotation == PlotRotation.Degrees090 || plotRotation == PlotRotation.Degrees180)
                            {
                                offsets = new Point2d(-offsets.X, -offsets.Y);
                            }
                        }

                        pGSView.Viewport = new Extents2d(0, 0, 1, 1);

                        // Calculate viewport rect in printer units
                        //int x1 = (int)((offsets.X + drx1) * koeffX);
                        //int x2 = (int)((offsets.X + drx2) * koeffX);
                        //int y1 = (int)((-offsets.Y + dry1) * koeffY);
                        //int y2 = (int)((-offsets.Y + dry2) * koeffY);
                        int x1 = (int)(drx1 * koeffX);
                        int x2 = (int)(drx2 * koeffX);
                        int y1 = (int)(dry1 * koeffY);
                        int y2 = (int)(dry2 * koeffY);

                        Rectangle viewportRect = new Rectangle(x1, y1, x2, y2);
                        m_pPrinterDevice.OnSize(viewportRect);
                        if (m_pPrinterDevice.UnderlyingDevice.Properties.Contains("WindowHDC"))
                        {
                            m_pPrinterDevice.UnderlyingDevice.Properties.AtPut("WindowHDC", new RxVariant((Int32)ev.Graphics.GetHdc()));
                        }
                        m_pPrinterDevice.Update();

                        pAbstractViewPE.Dispose();
                        pLayout.Dispose();
                    }
                }
            }
        }
Ejemplo n.º 25
0
        public void Print(Database db, Teigha.GraphicsSystem.View pView, bool bPreview)
        {
            database = db;
            pViewDr  = pView;
            ObjectId idLayout = database.CurrentSpaceId;

            using (BlockTableRecord btr = (BlockTableRecord)database.CurrentSpaceId.GetObject(OpenMode.ForRead))
            {
                using (Layout pLayout = (Layout)btr.LayoutId.GetObject(OpenMode.ForRead))
                {
                    PrintDocument prDoc = new PrintDocument();
                    prDoc.PrintPage += new PrintPageEventHandler(this.PrintPage);
                    prDoc.PrinterSettings.PrinterName = pLayout.PlotConfigurationName;

                    PageSettings pageSetting = prDoc.DefaultPageSettings;
                    PlotRotation rotation    = pLayout.PlotRotation;
                    if (rotation == PlotRotation.Degrees090 || rotation == PlotRotation.Degrees270)
                    {
                        pageSetting.Landscape = true;
                    }
                    else
                    {
                        pageSetting.Landscape = false;
                    }

                    Double    kMmPerInch  = 10 / 2.54;
                    Point2d   ptPaperSize = pLayout.PlotPaperSize;
                    PaperSize ps          = new PaperSize(pLayout.CanonicalMediaName, (int)(ptPaperSize.X * kMmPerInch), (int)(ptPaperSize.Y * kMmPerInch));
                    pageSetting.PaperSize = ps;

                    //default as OdaMfc
                    pageSetting.Margins.Left   = 0;
                    pageSetting.Margins.Right  = 0;
                    pageSetting.Margins.Top    = 0;
                    pageSetting.Margins.Bottom = 0;

                    prDoc.DefaultPageSettings = pageSetting;
                    if (prDoc.PrinterSettings.IsValid)
                    {
                        try
                        {                                                                 //WinGDI, WinOpenGL
                            using (GsModule gsModule = (GsModule)SystemObjects.DynamicLinker.LoadModule("WinGDI.txv", false, true))
                            {
                                using (Teigha.GraphicsSystem.Device graphichsDevice = gsModule.CreateDevice())
                                {
                                    using (ContextForDbDatabase ctx = new ContextForDbDatabase(database))
                                    {
                                        ctx.SetPlotGeneration(true);
                                        ctx.UseGsModel   = true;
                                        m_pPrinterDevice = LayoutHelperDevice.SetupActiveLayoutViews(graphichsDevice, ctx);
                                        m_pPrinterDevice.BackgroundColor = Color.FromArgb(0, 255, 255, 255);
                                        m_pPrinterDevice.SetLogicalPalette(Device.LightPalette);

                                        Aux.preparePlotstyles(database, ctx);
                                    }
                                }
                            }
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }

                        if (bPreview)
                        {
                            PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
                            printPreviewDialog.Document = prDoc;
                            printPreviewDialog.ShowDialog();
                        }
                        else
                        {
                            prDoc.Print();
                        }

                        m_pPrinterDevice = null;
                        database         = null;
                        pViewDr          = null;
                    }
                }
            }
        }