Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        MakeExtentsBlock(BoundBlock3d ext, int colorIndex, Database db)
        {
            Point3d minPt = ext.GetMinimumPoint();
            Point3d maxPt = ext.GetMaximumPoint();

            double deltaX = Math.Abs(maxPt.X - minPt.X);
            double deltaY = Math.Abs(maxPt.Y - minPt.Y);
            double deltaZ = Math.Abs(maxPt.Z - minPt.Z);

            Point3d[] pts = new Point3d[8];

            pts[0] = minPt;
            pts[6] = maxPt;

            // make bottom face
            pts[1] = new Point3d(pts[0].X + deltaX, pts[0].Y, pts[0].Z);
            pts[2] = new Point3d(pts[1].X, pts[1].Y + deltaY, pts[1].Z);
            pts[3] = new Point3d(pts[0].X, pts[0].Y + deltaY, pts[0].Z);

            // project up by Z
            pts[4] = new Point3d(pts[0].X, pts[0].Y, pts[0].Z + deltaZ);
            pts[5] = new Point3d(pts[1].X, pts[1].Y, pts[1].Z + deltaZ);
            pts[7] = new Point3d(pts[3].X, pts[3].Y, pts[3].Z + deltaZ);

            Vector3d offset = minPt.GetAsVector();

            // move points so that they are centered at WCS origin
            // for block creation.  Express everything in WCS since
            // that is what Entity.Extents works in.
            for (int i = 0; i < pts.Length; i++)
            {
                pts[i] -= offset;
            }

            DBObjectCollection faceEnts = new DBObjectCollection();

            faceEnts.Add(new Face(pts[0], pts[1], pts[2], pts[3], true, true, true, true));  // bottom face
            faceEnts.Add(new Face(pts[4], pts[5], pts[6], pts[7], true, true, true, true));  // top face
            faceEnts.Add(new Face(pts[0], pts[1], pts[5], pts[4], true, true, true, true));  // front face
            faceEnts.Add(new Face(pts[1], pts[2], pts[6], pts[5], true, true, true, true));  // right side face
            faceEnts.Add(new Face(pts[2], pts[3], pts[7], pts[6], true, true, true, true));  // back side face
            faceEnts.Add(new Face(pts[3], pts[0], pts[4], pts[7], true, true, true, true));  // left side face

            CompBldrAnonBlkDef compBldr = new CompBldrAnonBlkDef(db);

            compBldr.Start();

            foreach (Entity ent in faceEnts)
            {
                compBldr.SetToDefaultProps(ent);
                compBldr.AddToDb(ent);
            }

            compBldr.Commit();

            BlockReference blkRef = new BlockReference(minPt, compBldr.BlockDefId);

            blkRef.ColorIndex = colorIndex;
            Utils.SymTbl.AddToCurrentSpaceAndClose(blkRef, compBldr.Database);
        }
Exemplo n.º 3
0
        public static void Zoom(this Database database, Point3d minPoint, Point3d maxPoint)
        {
            BoundBlock3d box = new BoundBlock3d();

            box.Set(minPoint, maxPoint);
            Zoom(database, box);
        }
Exemplo n.º 4
0
        public static void Zoom(this Database database, Extents3d ext)
        {
            BoundBlock3d box = new BoundBlock3d();

            box.Set(ext.MinPoint, ext.MaxPoint);
            Zoom(database, box);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 高度
        /// </summary>
        /// <param name="boundBlock3D"></param>
        /// <returns></returns>
        public static double Depth(this BoundBlock3d boundBlock3D)
        {
            double value = 0;

            if (boundBlock3D != null)
            {
                value = boundBlock3D.GetMaximumPoint().Z - boundBlock3D.GetMinimumPoint().Z;
            }
            return(value);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 宽度
        /// </summary>
        /// <param name="boundBlock3D"></param>
        /// <returns></returns>
        public static double Height(this BoundBlock3d boundBlock3D)
        {
            double value = 0;

            if (boundBlock3D != null)
            {
                value = boundBlock3D.GetMaximumPoint().Y - boundBlock3D.GetMinimumPoint().Y;
            }
            return(value);
        }
Exemplo n.º 7
0
        public static BoundBlock3d PixelToWorld(this Database database, Rectangle srcRectangle)
        {
            Point        bl               = new Point(srcRectangle.Left, srcRectangle.Bottom);
            Point        tr               = new Point(srcRectangle.Right, srcRectangle.Top);
            var          bottomLeft       = PixelToWorld(database, bl);
            var          topRight         = PixelToWorld(database, tr);
            BoundBlock3d destBoundBlock3D = new BoundBlock3d();

            destBoundBlock3D.Set(bottomLeft, topRight);
            return(destBoundBlock3D);
        }
Exemplo n.º 8
0
        public static Rectangle WorldToPixel(this Database database, BoundBlock3d srcBoundBlock3d)
        {
            Point3d   minPoint3d    = srcBoundBlock3d.GetMinimumPoint();
            Point3d   maxPoint3d    = srcBoundBlock3d.GetMaximumPoint();
            Point3d   tl3d          = new Point3d(minPoint3d.X, maxPoint3d.Y, 0);
            Point3d   br3d          = new Point3d(maxPoint3d.X, minPoint3d.Y, 0);
            Point     tl            = WorldToPixel(database, tl3d);
            Point     br            = WorldToPixel(database, br3d);
            Rectangle destRectangle = new Rectangle(tl.X, tl.Y, br.X - tl.X + 1, br.Y - tl.Y + 1);

            return(destRectangle);
        }
Exemplo n.º 9
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);
             }
         }
     }
 }
        // Box
        public Box BoxToSpeckle(BoundBlock3d bound, bool OrientToWorldXY = false)
        {
            try
            {
                Box box = null;

                var min = bound.GetMinimumPoint();
                var max = bound.GetMaximumPoint();

                // get dimension intervals
                var xSize = new Interval(min.X, max.X);
                var ySize = new Interval(min.Y, max.Y);
                var zSize = new Interval(min.Z, max.Z);

                // get box size info
                double area   = 2 * ((xSize.Length * ySize.Length) + (xSize.Length * zSize.Length) + (ySize.Length * zSize.Length));
                double volume = xSize.Length * ySize.Length * zSize.Length;

                if (OrientToWorldXY)
                {
                    var origin = new Point3d(0, 0, 0);
                    var normal = new Vector3d(0, 0, 1);
                    var plane  = PlaneToSpeckle(new Autodesk.AutoCAD.Geometry.Plane(origin, normal));
                    box = new Box(plane, xSize, ySize, zSize, ModelUnits)
                    {
                        area = area, volume = volume
                    };
                }
                else
                {
                    // get base plane
                    var corner = new Point3d(max.X, max.Y, min.Z);
                    var origin = new Point3d((corner.X + min.X) / 2, (corner.Y + min.Y) / 2, (corner.Z + min.Z) / 2);
                    var plane  = PlaneToSpeckle(new Autodesk.AutoCAD.Geometry.Plane(min, origin, corner));
                    box = new Box(plane, xSize, ySize, zSize, ModelUnits)
                    {
                        area = area, volume = volume
                    };
                }

                return(box);
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 11
0
        public static BoundBlock3d GetLayoutExtents(this LayoutHelperDevice helperDevice)
        {
            BoundBlock3d boundBlock3D = new BoundBlock3d();

            using (View pView = helperDevice.ActiveView)
            {
                // camera position in world coordinates
                Point3d pos        = pView.Position;
                double  halfWidth  = pView.FieldWidth / 2;
                double  halfHeight = pView.FieldHeight / 2;
                double  xMin       = pos.X - halfWidth;
                double  xMax       = pos.X + halfWidth;
                double  yMin       = pos.Y - halfHeight;
                double  yMax       = pos.Y + halfHeight;
                boundBlock3D.Set(new Point3d(xMin, yMin, 0), new Point3d(xMax, yMax, 0));
            }
            return(boundBlock3D);
        }
Exemplo n.º 12
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);
                         }
                     }
Exemplo n.º 13
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);
            }
        }
Exemplo n.º 14
0
 public static void Zoom(this Database database, BoundBlock3d box)
 {
     using (var vtr = (ViewportTableRecord)database.CurrentViewportTableRecordId.GetObject(OpenMode.ForWrite))
     {
         // using protocol extensions we handle PS and MS viewports in the same manner
         using (var vpd = new AbstractViewportData(vtr))
         {
             var view = vpd.GsView;
             // do actual zooming - change GS view
             // here protocol extension is used again, that provides some helpful functions
             using (var vpe = new AbstractViewPE(view))
             {
                 using (BoundBlock3d boundBlock3D = (BoundBlock3d)box.Clone())
                 {
                     boundBlock3D.TransformBy(view.ViewingMatrix);
                     vpe.ZoomExtents(boundBlock3D);
                 }
             }
             vpd.SetView(view);
         }
     }
     //ReSize();
 }
Exemplo n.º 15
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();
                    }
                }
            }
        }