70002016 How can I get the bounding box of all the entities visible inside a viewport

Article 70002016
Type HowTo
Product Engine
Version 8
Date Added 7/1/2021 12:00:00 AM
Fixed 9.9002.0.6 (7/1/2021 12:00:00 AM)
Submitted by You Robin

Summary

How can I get the bounding box of all the entities visible inside a viewport?

Solution

You can use a code like:

        vdViewport ViewPort_BB = null;
        VectorDraw.Geometry.Box BBox = new VectorDraw.Geometry.Box();
        private void button3_Click(object sender, EventArgs e)
        {
          //  doc.Open(@".... the drawing here...");
            ViewPort_BB = doc.LayOuts[0].Entities[1] as vdViewport;

            this.Text = ""; // just for debugging
            BBox = new VectorDraw.Geometry.Box();
            if (ViewPort_BB != null)
            {
                doc.FreezeEntityDrawEvents.Push(false);
                doc.OnDrawFigure += new vdDocument.FigureDrawEventHandler(doc_OnDrawFigure_forViewport);
                doc.OnDrawAfterFigure += new vdDocument.FigureAfterDrawEventHandler(doc_OnDrawAfterFigure_forViewport);
                doc.ActiveLayOut = ViewPort_BB.OwnerLayout();
                doc.ZoomExtents();
                doc.Redraw(true); // you should have the Layout that the viewport is active
                doc.OnDrawAfterFigure -= new vdDocument.FigureAfterDrawEventHandler(doc_OnDrawAfterFigure_forViewport);
                doc.OnDrawFigure -= new vdDocument.FigureDrawEventHandler(doc_OnDrawFigure_forViewport);
                doc.FreezeEntityDrawEvents.Pop();
                doc.ZoomPrevious();
                doc.Redraw(true);
            }
            ViewPort_BB = null;
            this.Text += " " + BBox.ToString();// just for debugging
        }

        void doc_OnDrawAfterFigure_forViewport(object sender, VectorDraw.Render.vdRender render)
        {
            if (sender != null && sender is vdFigure)
            {
                vdFigure fig = sender as vdFigure;
                if (fig.Owner == doc.Model.Entities)
                {
                    this.Text += fig.ToString(); // just for debugging
                    //these are the objects visible inside the vdViewport, you can get its bounding box like:
                    BBox.AddBox(fig.BoundingBox);
                }
            }
        }

        void doc_OnDrawFigure_forViewport(object sender, VectorDraw.Render.vdRender render, ref bool cancel)
        {
            if (sender != null && sender is vdFigure)
            {
                vdFigure fig = sender as vdFigure;
                // draw only the specific ViewPort 
                if (!ReferenceEquals(fig,ViewPort_BB) && 
                       fig.Owner != doc.Model.Entities) cancel = true;
            }
        }
See the remarks in this code

Send comments on this topic.