示例#1
0
        /// <summary>
        /// Определение границ блока панели для вида формы или фасада
        /// </summary>
        private Extents3d getPanelExt(BlockReference blRef, bool isFacadeView)
        {
            Extents3d resVal      = new Extents3d();
            string    ignoreLayer = isFacadeView ? Settings.Default.LayerDimensionForm : Settings.Default.LayerDimensionFacade;

            var btr = blRef.DynamicBlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord;

            foreach (ObjectId idEnt in btr)
            {
                if (!idEnt.IsValidEx())
                {
                    continue;
                }
                var ent = idEnt.GetObject(OpenMode.ForRead, false, true) as Entity;
                if (ent.Layer.Equals(ignoreLayer, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }
                try
                {
                    var curExt = ent.GeometricExtents;
                    if (!IsEmptyExt(ref curExt))
                    {
                        resVal.AddExtents(curExt);
                    }
                }
                catch { }
            }
            resVal.TransformBy(blRef.BlockTransform);
            return(resVal);
        }
示例#2
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);
        }
示例#3
0
        static public void PaperZoomExtents2()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;
            Editor   ed  = doc.Editor;

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect a viewport: ");

            peo.SetRejectMessage("\nMust be a viewport...");
            peo.AddAllowedClass(typeof(Viewport), true);

            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status != PromptStatus.OK)
            {
                return;
            }

            using (Transaction Tx = db.TransactionManager.StartTransaction())
            {
                Viewport vp = Tx.GetObject(per.ObjectId, OpenMode.ForWrite) as Viewport;


                db.UpdateExt(true);

                double scrRatio = (vp.Width / vp.Height);

                Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(vp.ViewDirection);

                matWCS2DCS = Matrix3d.Displacement(vp.ViewTarget - Point3d.Origin) * matWCS2DCS;

                matWCS2DCS = Matrix3d.Rotation(-vp.TwistAngle,
                                               vp.ViewDirection,
                                               vp.ViewTarget)
                             * matWCS2DCS;

                matWCS2DCS = matWCS2DCS.Inverse();

                Extents3d extents = new Extents3d(db.Extmin, db.Extmax);
                extents.TransformBy(matWCS2DCS);

                double width = (extents.MaxPoint.X - extents.MinPoint.X);

                double height = (extents.MaxPoint.Y - extents.MinPoint.Y);

                Point2d center = new Point2d((extents.MaxPoint.X + extents.MinPoint.X) * 0.5,
                                             (extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5);

                if (width > (height * scrRatio))
                {
                    height = width / scrRatio;
                }

                vp.ViewHeight = height;
                vp.ViewCenter = center;

                Tx.Commit();
            }
        }
示例#4
0
        public static Extents3d EnlargeExtend(Extents3d ext, double scaleRatio)
        {
            Extents3d tmpExt     = new Extents3d(ext.MinPoint, ext.MaxPoint);
            Matrix3d  scalingMat = Matrix3d.Scaling(scaleRatio, GetCenterPoint(tmpExt));

            tmpExt.TransformBy(scalingMat);
            return(tmpExt);
        }
示例#5
0
 public ColorArea(BlockReference blRef, Album album, Matrix3d trans)
 {
     _idblRef = blRef.ObjectId;
     // Определение габаритов
     _bounds = blRef.GeometricExtentsСlean();
     _bounds.TransformBy(trans);
     _paint = album.GetPaint(blRef.Layer);
     _size  = (_bounds.MaxPoint.X - _bounds.MinPoint.X) * (_bounds.MaxPoint.Y - _bounds.MinPoint.Y);
 }
示例#6
0
        /// <summary>
        ///     Method to zoom a viewport to the extents of an input entity
        /// </summary>
        /// <param name="acCurDb"></param>
        /// <param name="acCurVp"></param>
        /// <param name="acEnt"></param>
        private void ZoomViewport(Database acCurDb, Viewport acCurVp, Extents3d ext)
        {
            // get the screen aspect ratio to calculate
            // the height and width
            // width/height
            var mScrRatio = acCurVp.Width / acCurVp.Height;
            var mMaxExt   = acCurDb.Extmax;
            var mMinExt   = acCurDb.Extmin;

            if (ext != null)
            {
                mMaxExt = ext.MaxPoint;
                mMinExt = ext.MinPoint;
            }

            var mExtents = new Extents3d();

            mExtents.Set(mMinExt, mMaxExt);

            // prepare Matrix for DCS to WCS transformation
            var matWcs2Dcs = Matrix3d.PlaneToWorld(acCurVp.ViewDirection);

            matWcs2Dcs = Matrix3d.Displacement(acCurVp.ViewTarget - Point3d.Origin) * matWcs2Dcs;
            matWcs2Dcs = Matrix3d.Rotation(-acCurVp.TwistAngle, acCurVp.ViewDirection, acCurVp.ViewTarget) * matWcs2Dcs;
            matWcs2Dcs = matWcs2Dcs.Inverse();

            // tranform the extents to the DCS
            // defined by the viewdir
            mExtents.TransformBy(matWcs2Dcs);

            // width of the extents in current view
            var mWidth = mExtents.MaxPoint.X - mExtents.MinPoint.X;

            // height of the extents in current view
            var mHeight = mExtents.MaxPoint.Y - mExtents.MinPoint.Y;

            // get the view center point
            var mCentPt = new Point2d(
                (mExtents.MaxPoint.X + mExtents.MinPoint.X) * 0.5,
                (mExtents.MaxPoint.Y + mExtents.MinPoint.Y) * 0.5);

            // check if the width 'fits' in current window,
            // if not then get the new height as
            // per the viewports aspect ratio
            if (mWidth > mHeight * mScrRatio)
            {
                mHeight = mWidth / mScrRatio;
            }

            // set the view height - adjusted by view Identifier
            acCurVp.ViewHeight = mHeight * 1.25;

            // set the view center
            acCurVp.ViewCenter = mCentPt;
        }
        public static void SetViewportToExtents(Database db, ViewportTableRecord viewportTableRec)
        {
            try
            {
                //lets update the database extents first
                //true gives the best fit but will take time
                db.UpdateExt(false); //Does not work corectly

                //get the screen aspect ratio to calculate the height and width
                double scrRatio = (viewportTableRec.Width / viewportTableRec.Height);

                //prepare Matrix for DCS to WCS transformation
                Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(viewportTableRec.ViewDirection);

                //for DCS target point is the origin
                matWCS2DCS = Matrix3d.Displacement(viewportTableRec.Target - Point3d.Origin) * matWCS2DCS;

                //WCS Xaxis is twisted by twist angle
                matWCS2DCS = Matrix3d.Rotation(-viewportTableRec.ViewTwist,
                                               viewportTableRec.ViewDirection,
                                               viewportTableRec.Target)
                             * matWCS2DCS;

                matWCS2DCS = matWCS2DCS.Inverse();

                //tranform the extents to the DCS defined by the viewdir
                Extents3d extents = new Extents3d(db.Extmin, db.Extmax);
                extents.TransformBy(matWCS2DCS);

                //width of the extents in current view
                double width = (extents.MaxPoint.X - extents.MinPoint.X);

                //height of the extents in current view
                double height = (extents.MaxPoint.Y - extents.MinPoint.Y);

                //get the view center point
                Point2d center = new Point2d((extents.MaxPoint.X + extents.MinPoint.X) * 0.5,
                                             (extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5);

                //check if the width 'fits' in current window
                //if not then get the new height as per the viewports aspect ratio
                if (width > (height * scrRatio))
                {
                    height = width / scrRatio;
                }

                viewportTableRec.Height      = height;
                viewportTableRec.Width       = height * scrRatio;
                viewportTableRec.CenterPoint = center;
            }
            catch (Exception ex)
            {
            }
        }
示例#8
0
文件: Error.cs 项目: vildar82/AcadLib
 public Error(string message, Extents3d ext, Matrix3d trans, [CanBeNull] Icon icon = null)
 {
     _msg      = PrepareMessage(message);
     _shortMsg = GetShortMsg(_msg);
     _extents  = ext;
     _extents.TransformBy(trans);
     _alreadyCalcExtents = true;
     HasEntity           = false;
     Icon    = icon ?? SystemIcons.Error;
     Trans   = trans;
     CanShow = true;
     DefineStatus();
 }
示例#9
0
 public static void Zoom([CanBeNull] this Editor ed, Extents3d ext)
 {
     if (ed == null)
     {
         return;
     }
     using var view = ed.GetCurrentView();
     ext.TransformBy(view.WorldToEye());
     view.Width       = ext.MaxPoint.X - ext.MinPoint.X;
     view.Height      = ext.MaxPoint.Y - ext.MinPoint.Y;
     view.CenterPoint = new Point2d(
         (ext.MaxPoint.X + ext.MinPoint.X) / 2.0,
         (ext.MaxPoint.Y + ext.MinPoint.Y) / 2.0);
     ed.SetCurrentView(view);
 }
示例#10
0
文件: Zoom.cs 项目: vildar82/AcadLib
        public static void Zoom(this Editor ed, Extents3d ext)
        {
            if (ed == null)
                return;

            using (ViewTableRecord view = ed.GetCurrentView())
            {
                ext.TransformBy(view.WorldToEye());
                view.Width = ext.MaxPoint.X - ext.MinPoint.X;
                view.Height = ext.MaxPoint.Y - ext.MinPoint.Y;
                view.CenterPoint = new Point2d(
                    (ext.MaxPoint.X + ext.MinPoint.X) / 2.0,
                    (ext.MaxPoint.Y + ext.MinPoint.Y) / 2.0);
                ed.SetCurrentView(view);
            }
        }
示例#11
0
        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();
        }
示例#12
0
        public static string ZoomFilesAndSave(string fileName)
        {
            string newFileName = "";

            using (Database db = new Database(false, false))
            {
                db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndReadShare, true, null);
                Database prevDb = HostApplicationServices.WorkingDatabase;
                HostApplicationServices.WorkingDatabase = db;
                db.UpdateExt(true);
                using (ViewportTable vTab = db.ViewportTableId.GetObject(OpenMode.ForRead) as ViewportTable)
                {
                    ObjectId acVptId = vTab["*Active"];
                    using (ViewportTableRecord vpTabRec = acVptId.GetObject(OpenMode.ForWrite) as ViewportTableRecord)
                    {
                        double   scrRatio   = (vpTabRec.Width / vpTabRec.Height);
                        Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(vpTabRec.ViewDirection);
                        matWCS2DCS = Matrix3d.Displacement(vpTabRec.Target - Point3d.Origin) * matWCS2DCS;
                        matWCS2DCS = Matrix3d.Rotation(-vpTabRec.ViewTwist,
                                                       vpTabRec.ViewDirection,
                                                       vpTabRec.Target)
                                     * matWCS2DCS;
                        matWCS2DCS = matWCS2DCS.Inverse();
                        Extents3d extents = new Extents3d(db.Extmin, db.Extmax);
                        extents.TransformBy(matWCS2DCS);
                        double  width  = (extents.MaxPoint.X - extents.MinPoint.X);
                        double  height = (extents.MaxPoint.Y - extents.MinPoint.Y);
                        Point2d center = new Point2d((extents.MaxPoint.X + extents.MinPoint.X) * 0.5,
                                                     (extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5);
                        if (width > (height * scrRatio))
                        {
                            height = width / scrRatio;
                        }
                        vpTabRec.Height      = height;
                        vpTabRec.Width       = height * scrRatio;
                        vpTabRec.CenterPoint = center;
                    }
                }

                HostApplicationServices.WorkingDatabase = prevDb;
                newFileName = fileName.Substring(0, fileName.Length - 4) + "z.dwg";
                db.SaveAs(newFileName, DwgVersion.Current);
            }

            return(newFileName);
        }
        /// <summary>
        /// Borrowed from http://www.theswamp.org/index.php?topic=46442.msg514605#msg514605
        /// </summary>
        public static void Zoom(this Editor ed, Extents3d ext)
        {
            if (ed == null)
            {
                throw new ArgumentNullException("ed");
            }

            using (ViewTableRecord view = ed.GetCurrentView())
            {
                ext.TransformBy(view.WorldToEye());
                view.Width       = ext.MaxPoint.X - ext.MinPoint.X;
                view.Height      = ext.MaxPoint.Y - ext.MinPoint.Y;
                view.CenterPoint = new Point2d(
                    (ext.MaxPoint.X + ext.MinPoint.X) / 2.0,
                    (ext.MaxPoint.Y + ext.MinPoint.Y) / 2.0);
                ed.SetCurrentView(view);
            }
        }
            private Extents3d?_getSymbolsBounds(IEnumerable <Entity> symbols)
            {
                if (symbols.Count() < 1)
                {
                    return(null);
                }
                Extents3d res = new Extents3d();

                symbols.ToList().ForEach(ent =>
                {
                    /*var clone = ent.GetTransformedCopy(_lineTarnsform.Inverse());
                     * if (clone.Bounds.HasValue)
                     *  res.AddExtents(ent.Bounds.Value);*/
                    if (ent is DBText)
                    {
                        var rectg = ((DBText)ent).GetTextBoxCorners();
                        if (rectg.HasValue)
                        {
                            Point3d lowerLeft  = rectg.Value.LowerLeft.TransformBy(_lineTarnsform.Inverse());
                            Point3d upperRight = rectg.Value.UpperRight.TransformBy(_lineTarnsform.Inverse());

                            ///////////////////////////////////////////////////////////!!!!!!!!!!//////////////////////////
                            if (lowerLeft.X > upperRight.X)
                            {
                                lowerLeft = new Point3d(upperRight.X - 1d, lowerLeft.Y, lowerLeft.Z);
                            }
                            ///////////////////////////////////////////////////////////!!!!!!!!!!//////////////////////////

                            try
                            {
                                Extents3d ext = new Extents3d(lowerLeft, upperRight);
                                res.AddExtents(ext);
                            }
                            catch { }
                        }
                    }
                });
                if (res == null)
                {
                    return(null);
                }
                res.TransformBy(_lineTarnsform);
                return(res);
            }
示例#15
0
        public static List <ObjectId> SelectInExtents([NotNull] this Editor ed, Extents3d ext)
        {
            using (ed.Document.LockDocument())
            {
                Debug.WriteLine($"SelectInExtents IsApplicationContext={Application.DocumentManager.IsApplicationContext}.");
                ed.Try(e => e.Document.Database.TileMode = true);
                ed.Try(e => e.Zoom(ext.Offset(10)));
                ext.TransformBy(ed.WCS2UCS());
                var minPt  = ext.MinPoint;
                var maxPt  = ext.MaxPoint;
                var selRes = ed.SelectCrossingWindow(minPt, maxPt);
                if (selRes.Status == PromptStatus.OK)
                {
                    return(selRes.Value.GetObjectIds().ToList());
                }

                throw new OperationCanceledException();
            }
        }
示例#16
0
        /// <summary>
        /// 根据对象的范围显示视图
        /// </summary>
        /// <param name="ed">命令行对象</param>
        /// <param name="entId">对象的Id</param>
        public static void ZoomObject(this Editor ed, ObjectId entId)
        {
            Database db = ed.Document.Database;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //获取实体对象
                Entity ent = trans.GetObject(entId, OpenMode.ForRead) as Entity;
                if (ent == null)
                {
                    return;
                }
                //根据实体的范围对视图进行缩放
                Extents3d ext = ent.GeometricExtents;
                ext.TransformBy(ed.CurrentUserCoordinateSystem.Inverse());
                ed.ZoomWindow(ext.MinPoint, ext.MaxPoint);
                trans.Commit();
            }
        }
示例#17
0
文件: Helper.cs 项目: VB6Hobbyst7/kex
 public void Zoom(Editor ed, Extents3d ext)
 {
     if (ed == null)
     {
         throw new ArgumentNullException("ed");
     }
     using (ViewTableRecord view = ed.GetCurrentView())
     {
         Matrix3d worldToEye = Matrix3d.WorldToPlane(view.ViewDirection) *
                               Matrix3d.Displacement(Point3d.Origin - view.Target) *
                               Matrix3d.Rotation(view.ViewTwist, view.ViewDirection, view.Target);
         ext.TransformBy(worldToEye);
         view.Width       = ext.MaxPoint.X - ext.MinPoint.X;
         view.Height      = ext.MaxPoint.Y - ext.MinPoint.Y;
         view.CenterPoint = new Point2d(
             (ext.MaxPoint.X + ext.MinPoint.X) / 2.0,
             (ext.MaxPoint.Y + ext.MinPoint.Y) / 2.0);
         ed.SetCurrentView(view);
     }
 }
示例#18
0
        private void ZoomObjects(ObjectIdCollection idCol)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;
            Editor   ed  = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
                using (ViewTableRecord view = ed.GetCurrentView())
                {
                    Matrix3d WCS2DCS = Matrix3d.PlaneToWorld(view.ViewDirection);
                    WCS2DCS = Matrix3d.Displacement(view.Target - Point3d.Origin) * WCS2DCS;
                    WCS2DCS = Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) * WCS2DCS;
                    WCS2DCS = WCS2DCS.Inverse();
                    Entity    ent = (Entity)tr.GetObject(idCol[0], OpenMode.ForRead);
                    Extents3d ext = ent.GeometricExtents;
                    ext.TransformBy(WCS2DCS);
                    for (int i = 1; i < idCol.Count; i++)
                    {
                        ent = (Entity)tr.GetObject(idCol[i], OpenMode.ForRead);
                        Extents3d tmp = ent.GeometricExtents;
                        tmp.TransformBy(WCS2DCS);
                        ext.AddExtents(tmp);
                    }
                    double ratio  = view.Width / view.Height;
                    double width  = ext.MaxPoint.X - ext.MinPoint.X;
                    double height = ext.MaxPoint.Y - ext.MinPoint.Y;
                    if (width > (height * ratio))
                    {
                        height = width / ratio;
                    }
                    Point2d center =
                        new Point2d((ext.MaxPoint.X + ext.MinPoint.X) / 2.0, (ext.MaxPoint.Y + ext.MinPoint.Y) / 2.0);
                    view.Height      = height;
                    view.Width       = width;
                    view.CenterPoint = center;
                    ed.SetCurrentView(view);
                    tr.Commit();
                }
        }
示例#19
0
        private void ZoomToExtentsofViewport(ViewportTableRecord vp)
        {
            Database db = vp.Database;
            // get the screen aspect ratio to calculate the height and width
            double aspect = (vp.Width / vp.Height);

            db.UpdateExt(true);
            Point3d minExt = db.Extmin;
            Point3d maxExt = db.Extmax;

            Extents3d extents = new Extents3d(minExt, maxExt);

            // prepare Matrix for DCS to WCS transformation
            Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(vp.ViewDirection);

            matWCS2DCS = Matrix3d.Displacement(vp.Target - Point3d.Origin) * matWCS2DCS;
            matWCS2DCS = Matrix3d.Rotation(-vp.ViewTwist, vp.ViewDirection, vp.Target) * matWCS2DCS;
            matWCS2DCS = matWCS2DCS.Inverse();

            // tranform the extents to the DCS defined by the viewdir
            extents.TransformBy(matWCS2DCS);

            double width  = (extents.MaxPoint.X - extents.MinPoint.X);
            double height = (extents.MaxPoint.Y - extents.MinPoint.Y);

            // get the view center point
            Point2d center = new Point2d(((extents.MaxPoint.X + extents.MinPoint.X) * 0.5), ((extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5));

            // check if the width 'fits' in current window,
            if (width > (height * aspect))
            {
                height = width / aspect;
            }

            // set the view height - adjusted by 1%
            vp.Height = height * 1.01;
            // set the view center
            vp.CenterPoint = center;
        }
示例#20
0
        // Zooms to given objects
        public static void ZoomToObjects(Database db, IEnumerable <ObjectId> ids)
        {
            Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            Extents3d outerext = new Extents3d();

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                try
                {
                    foreach (ObjectId id in ids)
                    {
                        Autodesk.AutoCAD.DatabaseServices.Entity ent = tr.GetObject(id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity;
                        Extents3d ext = ent.GeometricExtents;
                        outerext.AddExtents(ext);
                    }
                }
                catch
                {
                    ;
                }

                tr.Commit();
            }

            outerext.TransformBy(ed.CurrentUserCoordinateSystem.Inverse());
            Point2d min2d = new Point2d(outerext.MinPoint.X, outerext.MinPoint.Y);
            Point2d max2d = new Point2d(outerext.MaxPoint.X, outerext.MaxPoint.Y);

            ViewTableRecord view = new ViewTableRecord();

            view.CenterPoint = min2d + ((max2d - min2d) / 2.0);
            view.Height      = max2d.Y - min2d.Y;
            view.Width       = max2d.X - min2d.X;

            ed.SetCurrentView(view);
        }
示例#21
0
        public static Extents3d?GetRotatedBounds(this DBText text)
        {
            if (!text.Bounds.HasValue)
            {
                return(null);
            }
            DBText cloneText = (DBText)text.Clone();

            Matrix3d mat = Matrix3d.Identity;

            mat = mat.PreMultiplyBy(Matrix3d.Rotation(-text.Rotation, text.Normal, text.Position));

            cloneText.TransformBy(mat);
            cloneText.AdjustAlignment(Tools.GetAcadDatabase());
            if (!cloneText.Bounds.HasValue)
            {
                return(null);
            }

            Extents3d bounds = cloneText.Bounds.Value;

            bounds.TransformBy(mat.Inverse());
            return(bounds);
        }
示例#22
0
文件: Zoom.cs 项目: ef5tech/MyCAD
        public static void Use(Point3d pMin, Point3d pMax, Point3d pCenter, double dFactor)
        {
            Document acDoc   = Application.DocumentManager.MdiActiveDocument;
            Database acCurDB = acDoc.Database;

            int nCurVport = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"));

            if (acCurDB.TileMode == true) //model space
            {
                if (pMin.Equals(new Point3d()) == true && pMax.Equals(new Point3d()) == true)
                {
                    pMin = acCurDB.Extmin; //Get the extends of model space
                    pMax = acCurDB.Extmax;
                }
            }
            else
            {
                if (nCurVport == 1) //paper space
                {
                    if (pMin.Equals(new Point3d()) == true && pMax.Equals(new Point3d()) == true)
                    {
                        pMin = acCurDB.Pextmin; //Get the extends of paper space
                        pMax = acCurDB.Pextmax;
                    }
                }
                else
                {
                    if (pMin.Equals(new Point3d()) == true && pMax.Equals(new Point3d()) == true)
                    {
                        pMin = acCurDB.Extmin; //Get the extends of model space
                        pMax = acCurDB.Extmax;
                    }
                }
            }
            using (Transaction acTrans = acCurDB.TransactionManager.StartTransaction())
            {
                using (ViewTableRecord acView = acDoc.Editor.GetCurrentView())
                {
                    Extents3d eExtents;

                    Matrix3d matWCStoDCS;
                    matWCStoDCS = Matrix3d.PlaneToWorld(acView.ViewDirection);
                    matWCStoDCS = Matrix3d.Displacement(acView.Target - Point3d.Origin) * matWCStoDCS;
                    matWCStoDCS = Matrix3d.Rotation(-acView.ViewTwist, acView.ViewDirection, acView.Target) * matWCStoDCS;

                    if (pCenter.DistanceTo(Point3d.Origin) != 0)
                    {
                        pMin = new Point3d(pCenter.X - (acView.Width / 2), pCenter.Y - (acView.Height / 2), 0);
                        pMax = new Point3d((acView.Width / 2) + pCenter.X, (acView.Height / 2) + pCenter.Y, 0);
                    }
                    using (Line acLine = new Line(pMin, pMax))
                    {
                        eExtents = new Extents3d(acLine.Bounds.Value.MinPoint, acLine.Bounds.Value.MaxPoint);
                    }
                    double dViewRatio;
                    dViewRatio = (acView.Width / acView.Height);

                    matWCStoDCS = matWCStoDCS.Inverse();
                    eExtents.TransformBy(matWCStoDCS);

                    double  dWidth;
                    double  dHeight;
                    Point2d pNewCentPt;

                    if (pCenter.DistanceTo(Point3d.Origin) != 0)
                    {
                        dWidth  = acView.Width;
                        dHeight = acView.Height;

                        if (dFactor == 0)
                        {
                            pCenter = pCenter.TransformBy(matWCStoDCS);
                        }
                        pNewCentPt = new Point2d(pCenter.X, pCenter.Y);
                    }
                    else
                    {
                        dWidth  = eExtents.MaxPoint.X - eExtents.MinPoint.X;
                        dHeight = eExtents.MaxPoint.Y - eExtents.MinPoint.Y;

                        pNewCentPt = new Point2d((eExtents.MaxPoint.X + eExtents.MinPoint.X) * 0.5, (eExtents.MaxPoint.Y + eExtents.MinPoint.Y) * 0.5);
                    }
                    if (dWidth > (dHeight * dViewRatio))
                    {
                        dHeight = dWidth / dViewRatio;
                    }
                    if (dFactor != 0)
                    {
                        acView.Height = dHeight * dFactor;
                        acView.Width  = dWidth * dFactor;
                    }
                    acView.CenterPoint = pNewCentPt;
                    acDoc.Editor.SetCurrentView(acView);
                }
                acTrans.Commit();
            }
        }
        //------------------------------------------------------------------------//
        //
        public static void Zoom(Point3d pMin, Point3d pMax, Point3d pCenter, double dFactor)
        {
            // Get de current document and database
            Document acDoc   = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            int nCurVport = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"));

            // Get the extents of the current space no points
            // or only a center point is provided.
            //Check to see if Model space is current
            if (acCurDb.TileMode)
            {
                if (pMin.Equals(new Point3d()) && pMax.Equals(new Point3d()))
                {
                    pMin = acCurDb.Extmin;
                    pMax = acCurDb.Extmax;
                }
            }
            else
            {
                // Check to see if Paper space is current
                if (nCurVport == 1)
                {
                    // Get the extents of Papers space
                    if (pMin.Equals(new Point3d()) && pMax.Equals(new Point3d()))
                    {
                        pMin = acCurDb.Pextmin;
                        pMax = acCurDb.Pextmax;
                    }
                }
                else
                {
                    // Get the extents of Model space
                    if (pMin.Equals(new Point3d()) && pMax.Equals(new Point3d()))
                    {
                        pMin = acCurDb.Extmin;
                        pMax = acCurDb.Extmax;
                    }
                }
            }

            // Start transation
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Get the current view
                using (ViewTableRecord acView = acDoc.Editor.GetCurrentView())
                {
                    // Translate WCS coordinates to DCS
                    Matrix3d matWCS2DCS;
                    matWCS2DCS = Matrix3d.PlaneToWorld(acView.ViewDirection);
                    matWCS2DCS = Matrix3d.Displacement(acView.Target - Point3d.Origin) * matWCS2DCS;
                    matWCS2DCS = Matrix3d.Rotation(-acView.ViewTwist, acView.ViewDirection, acView.Target) * matWCS2DCS;

                    // If a center point specified, define the min and max
                    // point of extents for Center and Scale modes
                    if (!pCenter.DistanceTo(Point3d.Origin).Equals(0))
                    {
                        pMin = new Point3d(pCenter.X - (acView.Width / 2),
                                           pCenter.Y - (acView.Height / 2), 0);

                        pMax = new Point3d((acView.Width / 2) + pCenter.X,
                                           (acView.Height / 2) + pCenter.Y, 0);
                    }

                    // Create an extents object using a line
                    Line acLine = new Line(pMin, pMax);
                    if (acLine.Bounds != null)
                    {
                        Extents3d eExtents = new Extents3d(acLine.Bounds.Value.MinPoint, acLine.Bounds.Value.MaxPoint);

                        // Calculate the ratio between the width and height of the current View
                        double dViewRatio = (acView.Width / acView.Height);

                        // Transform teh extents of the view
                        matWCS2DCS = matWCS2DCS.Inverse();
                        eExtents.TransformBy(matWCS2DCS);

                        double  dWidth;
                        double  dHeight;
                        Point2d pNewCentPt;

                        // Check see if a center point was provided (Center an dScale modes)
                        if (!pCenter.DistanceTo(Point3d.Origin).Equals(0.0))
                        {
                            dWidth  = acView.Width;
                            dHeight = acView.Height;

                            if (dFactor.Equals(0.0))
                            {
                                pCenter = pCenter.TransformBy(matWCS2DCS);
                            }

                            pNewCentPt = new Point2d(pCenter.X, pCenter.Y);
                        }
                        else // Working in window, Extents and Limits mode
                        {
                            // Calculate the new width and height of current view
                            dWidth  = eExtents.MaxPoint.X - eExtents.MinPoint.X;
                            dHeight = eExtents.MaxPoint.Y - eExtents.MinPoint.Y;

                            //Get te center of the view
                            pNewCentPt = new Point2d(((eExtents.MaxPoint.X + eExtents.MinPoint.X) * 0.5),
                                                     ((eExtents.MaxPoint.Y + eExtents.MinPoint.Y) * 0.5));
                        }

                        // Check to see if the new width fits in current window
                        if (dWidth > (dHeight * dViewRatio))
                        {
                            dHeight = dWidth / dViewRatio;
                        }

                        // Resize and scale the view
                        if (!dFactor.Equals(0.0))
                        {
                            acView.Height = dHeight * dFactor;
                            acView.Width  = dWidth * dFactor;
                        }

                        // Set teh center of the view
                        acView.CenterPoint = pNewCentPt;

                        // Set the current view
                        acDoc.Editor.SetCurrentView(acView);
                    }
                }

                acTrans.Commit();
            }
        }
示例#24
0
        /// <summary>
        /// Определение границ блока панели для вида формы или фасада
        /// </summary>            
        private Extents3d getPanelExt(BlockReference blRef, bool isFacadeView)
        {
            Extents3d resVal = new Extents3d();
            string ignoreLayer = isFacadeView ? Settings.Default.LayerDimensionForm : Settings.Default.LayerDimensionFacade;

            var btr = blRef.DynamicBlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord;
            foreach (ObjectId idEnt in btr)
            {
                var ent = idEnt.GetObject(OpenMode.ForRead, false, true) as Entity;
                if (ent.Layer.Equals(ignoreLayer, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }
                try
                {
                    var curExt = ent.GeometricExtents;
                    if (!IsEmptyExt(ref curExt))
                    {
                        resVal.AddExtents(curExt);
                    }
                }
                catch { }
            }
            resVal.TransformBy(blRef.BlockTransform);
            return resVal;
        }
示例#25
0
        /// <summary>
        /// Change the current view to a model space view centered on the provided bounding box.
        /// Based on code from http://help.autodesk.com/view/ACD/2016/ENU/?guid=GUID-FAC1A5EB-2D9E-497B-8FD9-E11D2FF87B93.
        /// </summary>
        /// <param name="boundingRect">Bounding box to focus on in 2d world coordinates.</param>
        public static void FocusBoundingInModel(Rectangle boundingRect)
        {
            // Get the current document and database
            Document acDoc   = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // TODO: Consider other languages?
                LayoutManager.Current.CurrentLayout = "Model";

                // Get the current view
                using (ViewTableRecord acView = acDoc.Editor.GetCurrentView())
                {
                    // TODO: Pull this value from settings.
                    const double dFactor = 1.1;

                    // Translate WCS coordinates to DCS
                    Matrix3d matWcs2Dcs = Matrix3d.PlaneToWorld(acView.ViewDirection);
                    matWcs2Dcs = Matrix3d.Displacement(acView.Target - Point3d.Origin) * matWcs2Dcs;
                    matWcs2Dcs = Matrix3d.Rotation(-acView.ViewTwist,
                                                   acView.ViewDirection,
                                                   acView.Target) * matWcs2Dcs;

                    Point3d min = new Point3d(boundingRect.Left, boundingRect.Bottom, 0);
                    Point3d max = new Point3d(boundingRect.Right, boundingRect.Top, 0);

                    Extents3d eExtents = new Extents3d(min, max);

                    // Calculate the ratio between the width and height of the current view
                    double dViewRatio;
                    dViewRatio = acView.Width / acView.Height;

                    // Tranform the extents of the view
                    matWcs2Dcs = matWcs2Dcs.Inverse();
                    eExtents.TransformBy(matWcs2Dcs);

                    double  dWidth;
                    double  dHeight;
                    Point2d pNewCentPt;

                    // Calculate the new width and height of the current view
                    dWidth  = eExtents.MaxPoint.X - eExtents.MinPoint.X;
                    dHeight = eExtents.MaxPoint.Y - eExtents.MinPoint.Y;

                    // Get the center of the view
                    pNewCentPt = new Point2d(((eExtents.MaxPoint.X + eExtents.MinPoint.X) * 0.5),
                                             ((eExtents.MaxPoint.Y + eExtents.MinPoint.Y) * 0.5));

                    // Check to see if the new width fits in current window
                    // TODO: Does this work?
                    if (dWidth > (dHeight * dViewRatio))
                    {
                        dHeight = dWidth / dViewRatio;
                    }

                    // Resize and scale the view to provide a 10% border around object
                    acView.Height = dHeight * dFactor;
                    acView.Width  = dWidth * dFactor;

                    // Set the center of the view
                    acView.CenterPoint = pNewCentPt;

                    // Set the current view
                    acDoc.Editor.SetCurrentView(acView);
                }

                // Commit the changes
                acTrans.Commit();
            }
        }
示例#26
0
        public static void TXZoom(Point3d pMin, Point3d pMax, Point3d pCenter, double dFactor)
        {
            //  获得当前文档和数据库   Get the current document and database
            Document acDoc   = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            int nCurVport = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"));

            // Get the extents of the current space no points
            // or only a center point is provided
            // 检查当前是否是模型空间  Check to see if Model space is current
            if (acCurDb.TileMode == true)
            {
                if (pMin.Equals(new Point3d()) == true &&
                    pMax.Equals(new Point3d()) == true)
                {
                    pMin = acCurDb.Extmin;
                    pMax = acCurDb.Extmax;
                }
            }
            else
            {
                // 检查当前是否是图纸空间  Check to see if Paper space is current
                if (nCurVport == 1)
                {
                    // Get the extents of Paper space
                    if (pMin.Equals(new Point3d()) == true &&
                        pMax.Equals(new Point3d()) == true)
                    {
                        pMin = acCurDb.Pextmin;
                        pMax = acCurDb.Pextmax;
                    }
                }
                else
                {
                    // 获得模型空间的范围  Get the extents of Model space
                    if (pMin.Equals(new Point3d()) == true &&
                        pMax.Equals(new Point3d()) == true)
                    {
                        pMin = acCurDb.Extmin;
                        pMax = acCurDb.Extmax;
                    }
                }
            }

            // 启动一个事务  Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Get the current view
                using (ViewTableRecord acView = acDoc.Editor.GetCurrentView())
                {
                    Extents3d eExtents;

                    // Translate WCS coordinates to DCS
                    Matrix3d matWCS2DCS;
                    matWCS2DCS = Matrix3d.PlaneToWorld(acView.ViewDirection);
                    matWCS2DCS = Matrix3d.Displacement(acView.Target - Point3d.Origin) * matWCS2DCS;
                    matWCS2DCS = Matrix3d.Rotation(-acView.ViewTwist,
                                                   acView.ViewDirection,
                                                   acView.Target) * matWCS2DCS;

                    // If a center point is specified, define the min and max
                    // point of the extents
                    // for Center and Scale modes
                    if (pCenter.DistanceTo(Point3d.Origin) != 0)
                    {
                        pMin = new Point3d(pCenter.X - (acView.Width / 2),
                                           pCenter.Y - (acView.Height / 2), 0);

                        pMax = new Point3d((acView.Width / 2) + pCenter.X,
                                           (acView.Height / 2) + pCenter.Y, 0);
                    }

                    // 使用一个直线创建一个范围对象 译者注:此处可能有错误,因为直线只有GeometricExtents属性表示范围  Create an extents object using a line
                    using (Line acLine = new Line(pMin, pMax))
                    {
                        eExtents = new Extents3d(pMin,
                                                 pMax);
                    }

                    // 计算当前视图的宽度与高度的比率  Calculate the ratio between the width and height of the current view
                    double dViewRatio;
                    dViewRatio = (acView.Width / acView.Height);

                    // 转换视图的范围  Tranform the extents of the view
                    matWCS2DCS = matWCS2DCS.Inverse();
                    eExtents.TransformBy(matWCS2DCS);

                    double  dWidth;
                    double  dHeight;
                    Point2d pNewCentPt;

                    // 检查中心点是否已提供(中心和缩放模式)  Check to see if a center point was provided (Center and Scale modes)
                    if (pCenter.DistanceTo(Point3d.Origin) != 0)
                    {
                        dWidth  = acView.Width;
                        dHeight = acView.Height;

                        if (dFactor == 0)
                        {
                            pCenter = pCenter.TransformBy(matWCS2DCS);
                        }

                        pNewCentPt = new Point2d(pCenter.X, pCenter.Y);
                    }
                    else // 配合窗口、范围和界限模式计算当前视图新的宽度和高度  Working in Window, Extents and Limits mode
                    {
                        // Calculate the new width and height of the current view
                        dWidth  = eExtents.MaxPoint.X - eExtents.MinPoint.X;
                        dHeight = eExtents.MaxPoint.Y - eExtents.MinPoint.Y;

                        // 获得视图的中心点  Get the center of the view
                        pNewCentPt = new Point2d(((eExtents.MaxPoint.X + eExtents.MinPoint.X) * 0.5),
                                                 ((eExtents.MaxPoint.Y + eExtents.MinPoint.Y) * 0.5));
                    }

                    // 检查新的宽度是否适合当前窗口 Check to see if the new width fits in current window
                    if (dWidth > (dHeight * dViewRatio))
                    {
                        dHeight = dWidth / dViewRatio;
                    }

                    // 调整大小并缩放视图  Resize and scale the view
                    if (dFactor != 0)
                    {
                        acView.Height = dHeight * dFactor;
                        acView.Width  = dWidth * dFactor;
                    }

                    // Set the center of the view
                    acView.CenterPoint = pNewCentPt;

                    // Set the current view
                    acDoc.Editor.SetCurrentView(acView);
                }

                // Commit the changes
                acTrans.Commit();
            }
        }
示例#27
0
        /// <summary>
        /// Scan the solid from bottom, identify points and create
        /// a surface
        /// </summary>
        /// <param name="solidId">Solid to scan</param>
        /// <param name="tinSurfaceToCutId">Referecen surface</param>
        /// <param name="solidSurfaceName">Name of the new
        /// surface that will be created</param>
        /// <param name="densityOfPoints">Number of points per
        /// AutoCAD unit used on scan</param>
        /// <param name="simplifySurface">Whether o not simplify
        /// the surface at the end (using Civil 3D
        /// built-in operation)</param>
        private static void GenerateSurfaceByScan(
            ObjectId solidId,
            ObjectId tinSurfaceToCutId,
            string solidSurfaceName,
            int densityOfPoints,
            bool simplifySurface)
        {
            _db = Application.DocumentManager.MdiActiveDocument.Database;
            using (_trans = _db.TransactionManager.StartTransaction())
            {
                try
                {
                    // open entities
                    _solid = _trans.GetObject(solidId, OpenMode.ForRead)
                             as Solid3d;
                    if (!tinSurfaceToCutId.IsNull)
                    {
                        _surface =
                            _trans.GetObject(tinSurfaceToCutId, OpenMode.ForRead)
                            as TinSurface;
                    }

                    // extract the Brep of the solid
                    _brepSolid = new Brep(_solid);
                    _newPoints = new Point3dCollection();

                    // get the extend of the solid
                    Extents3d extends = _solid.GeometricExtents;
                    // and expand by 20% to increase accuracy
                    // on the solid edges/borders
                    extends.TransformBy(Matrix3d.Scaling(1.2,
                                                         _solid.MassProperties.Centroid));

                    // geometric line at the bottom (virtual datum)
                    // this line is the scan line
                    //
                    //  x--------------------------x
                    //  |  pt2        ^            pt3
                    //  |             direction
                    //  |             of scan progress
                    //  |
                    //  | <-scan line
                    //  |
                    //  x  pt1
                    //
                    Point3d scanLinePt1 = extends.MinPoint;
                    Point3d scanLinePt2 = new Point3d(
                        scanLinePt1.X, extends.MaxPoint.Y, scanLinePt1.Z);
                    LineSegment3d scanLine = new LineSegment3d(
                        scanLinePt1, scanLinePt2);
                    Point3d scanLinePt3 = new Point3d(
                        extends.MaxPoint.X, extends.MaxPoint.Y, scanLinePt1.Z);
                    _upperLimit = extends.MaxPoint.Z; // scan upper limit

                    int numberOfScanLines = ((int)
                                             Math.Round(scanLinePt2.DistanceTo(scanLinePt3),
                                                        MidpointRounding.ToEven)) * densityOfPoints;

                    ProgressMeter progressBar = new ProgressMeter();
                    progressBar.SetLimit(numberOfScanLines);
                    progressBar.Start("Scanning solid...");

                    for (int i = 0; i < numberOfScanLines; i++)
                    {
                        ProcessScanLine(scanLine, densityOfPoints, true);

                        // move the scan line over the scane direction
                        // get direction vector
                        Vector3d scanLineDisplacementDirection =
                            scanLinePt2.GetVectorTo(scanLinePt3);
                        // make unit vector
                        scanLineDisplacementDirection /=
                            scanLineDisplacementDirection.Length;
                        // adjust size
                        scanLineDisplacementDirection *=
                            (1.0 / densityOfPoints);
                        scanLine.TransformBy(Matrix3d.Displacement(
                                                 scanLineDisplacementDirection));

                        progressBar.MeterProgress();
                        Util.AvoidNotResponding();
                    }
                    progressBar.Stop();
                    scanLine.Dispose();

                    #region For testing only
#if DEBUG
                    BlockTableRecord mSpace = _trans.GetObject(_db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                    foreach (Point3d pt in _newPoints)
                    {
                        DBPoint p = new DBPoint(pt);
                        mSpace.AppendEntity(p);
                        _trans.AddNewlyCreatedDBObject(p, true);
                    }
#endif
                    #endregion

                    CreateSolidSurface(solidSurfaceName, simplifySurface);

                    _trans.Commit();
                }
                catch (System.Exception ex)
                {
                    Application.DocumentManager.MdiActiveDocument.
                    Editor.WriteMessage("Error: Operation aborted ({0})",
                                        ex.Message);
                    _trans.Abort();
                }
                finally
                {
                    // final cleanup
                    if (!_brepSolid.IsDisposed)
                    {
                        _brepSolid.Dispose();
                    }
                    if (!_newPoints.IsDisposed)
                    {
                        _newPoints.Dispose();
                    }
                    _solid     = null;
                    _surface   = null;
                    _brepSolid = null;
                    _newPoints = null;
                }
            }
        }
示例#28
0
        /// <summary>
        /// The internal Zoom() method (credit: AutoCAD .NET Developer's Guide).
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="center"></param>
        /// <param name="factor"></param>
        internal static void Zoom(Point3d min, Point3d max, Point3d center, double factor)
        {
            // Get the current document and database
            var document = Application.DocumentManager.MdiActiveDocument;
            var database = document.Database;

            int currentViewport = Convert.ToInt32(Application.GetSystemVariable("CVPORT"));

            // Get the extents of the current space no points
            // or only a center point is provided
            // Check to see if Model space is current
            if (database.TileMode)
            {
                if (min.Equals(new Point3d()) && max.Equals(new Point3d()))
                {
                    min = database.Extmin;
                    max = database.Extmax;
                }
            }
            else
            {
                // Check to see if Paper space is current
                if (currentViewport == 1)
                {
                    // Get the extents of Paper space
                    if (min.Equals(new Point3d()) && max.Equals(new Point3d()))
                    {
                        min = database.Pextmin;
                        max = database.Pextmax;
                    }
                }
                else
                {
                    // Get the extents of Model space
                    if (min.Equals(new Point3d()) && max.Equals(new Point3d()))
                    {
                        min = database.Extmin;
                        max = database.Extmax;
                    }
                }
            }

            // Start a transaction
            using (var trans = database.TransactionManager.StartTransaction())
            {
                // Get the current view
                using (var currentView = document.Editor.GetCurrentView())
                {
                    Extents3d extents;

                    // Translate WCS coordinates to DCS
                    var matWCS2DCS = Matrix3d.PlaneToWorld(currentView.ViewDirection);
                    matWCS2DCS = Matrix3d.Displacement(currentView.Target - Point3d.Origin) * matWCS2DCS;
                    matWCS2DCS = Matrix3d.Rotation(
                        angle: -currentView.ViewTwist,
                        axis: currentView.ViewDirection,
                        center: currentView.Target) * matWCS2DCS;

                    // If a center point is specified, define the min and max
                    // point of the extents
                    // for Center and Scale modes
                    if (center.DistanceTo(Point3d.Origin) != 0)
                    {
                        min = new Point3d(center.X - (currentView.Width / 2), center.Y - (currentView.Height / 2), 0);
                        max = new Point3d((currentView.Width / 2) + center.X, (currentView.Height / 2) + center.Y, 0);
                    }

                    // Create an extents object using a line
                    using (Line line = new Line(min, max))
                    {
                        extents = new Extents3d(line.Bounds.Value.MinPoint, line.Bounds.Value.MaxPoint);
                    }

                    // Calculate the ratio between the width and height of the current view
                    double viewRatio = currentView.Width / currentView.Height;

                    // Tranform the extents of the view
                    matWCS2DCS = matWCS2DCS.Inverse();
                    extents.TransformBy(matWCS2DCS);

                    double  width;
                    double  height;
                    Point2d newCenter;

                    // Check to see if a center point was provided (Center and Scale modes)
                    if (center.DistanceTo(Point3d.Origin) != 0)
                    {
                        width  = currentView.Width;
                        height = currentView.Height;

                        if (factor == 0)
                        {
                            center = center.TransformBy(matWCS2DCS);
                        }

                        newCenter = new Point2d(center.X, center.Y);
                    }
                    else // Working in Window, Extents and Limits mode
                    {
                        // Calculate the new width and height of the current view
                        width  = extents.MaxPoint.X - extents.MinPoint.X;
                        height = extents.MaxPoint.Y - extents.MinPoint.Y;

                        // Get the center of the view
                        newCenter = new Point2d(
                            ((extents.MaxPoint.X + extents.MinPoint.X) * 0.5),
                            ((extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5));
                    }

                    // Check to see if the new width fits in current window
                    if (width > (height * viewRatio))
                    {
                        height = width / viewRatio;
                    }

                    // Resize and scale the view
                    if (factor != 0)
                    {
                        currentView.Height = height * factor;
                        currentView.Width  = width * factor;
                    }

                    // Set the center of the view
                    currentView.CenterPoint = newCenter;

                    // Set the current view
                    document.Editor.SetCurrentView(currentView);
                }

                // Commit the changes
                trans.Commit();
            }
        }
示例#29
0
        /// <summary>
        /// Рекурсивное получение габаритного контейнера для выбранного примитива.
        /// </summary>
        /// <param name="en">Имя примитива</param>
        /// <param name="ext">Габаритный контейнер</param>
        /// <param name="mat">Матрица преобразования из системы координат блока в МСК.</param>
        void GetBlockExtents(Entity en, ref Extents3d ext, ref Matrix3d mat)
        {
            if (!IsLayerOn(en.LayerId))
            {
                return;
            }
            if (en is BlockReference)
            {
                BlockReference bref   = en as BlockReference;
                Matrix3d       matIns = mat * bref.BlockTransform;
                using (BlockTableRecord btr =
                           bref.BlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord)
                {
                    foreach (ObjectId id in btr)
                    {
                        using (DBObject obj = id.GetObject(OpenMode.ForRead) as DBObject)
                        {
                            Entity enCur = obj as Entity;
                            if (enCur == null || enCur.Visible != true)
                            {
                                continue;
                            }
                            // Пропускаем неконстантные и невидимые определения атрибутов
                            AttributeDefinition attDef = enCur as AttributeDefinition;
                            if (attDef != null && (!attDef.Constant || attDef.Invisible))
                            {
                                continue;
                            }
                            GetBlockExtents(enCur, ref ext, ref matIns);
                        }
                    }
                }

                // Отдельно обрабатываем атрибуты блока
                if (bref.AttributeCollection.Count > 0)
                {
                    foreach (ObjectId idAtt in bref.AttributeCollection)
                    {
                        using (AttributeReference attRef =
                                   idAtt.GetObject(OpenMode.ForRead) as AttributeReference)
                        {
                            if (!attRef.Invisible && attRef.Visible)
                            {
                                GetBlockExtents(attRef, ref ext, ref mat);
                            }
                        }
                    }
                }
            }
            else
            {
                if (mat.IsUniscaledOrtho())
                {
                    using (Entity enTr = en.GetTransformedCopy(mat))
                    {
                        if (enTr is Dimension)
                        {
                            (enTr as Dimension).RecomputeDimensionBlock(true);
                        }
                        if (enTr is Table)
                        {
                            (enTr as Table).RecomputeTableBlock(true);
                        }
                        if (IsEmptyExt(ref ext))
                        {
                            try
                            {
                                ext = enTr.GeometricExtents;
                            }
                            catch
                            {
                                // ignored
                            }
                        }
                        else
                        {
                            try
                            {
                                ext.AddExtents(enTr.GeometricExtents);
                            }
                            catch
                            {
                                // ignored
                            }
                        }

                        return;
                    }
                }
                else
                {
                    try
                    {
                        Extents3d curExt = en.GeometricExtents;
                        curExt.TransformBy(mat);
                        if (IsEmptyExt(ref ext))
                        {
                            ext = curExt;
                        }
                        else
                        {
                            ext.AddExtents(curExt);
                        }
                    }
                    catch
                    {
                    }

                    return;
                }
            }
        }
示例#30
0
文件: WxxTU.cs 项目: scope111/cad
        /// <summary>
        /// 用来操作当前视图的函数
        /// <param name="pMin"></param>
        /// <param name="pMax"></param>
        /// <param name="pCenter"></param>
        /// <param name="dFactor"></param>
        public static void Zoom(Point3d pMin, Point3d pMax, Point3d pCenter, double dFactor)
        {//获取当前文档及数据库
            Document acDoc     = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb   = acDoc.Database;
            int      nCurVport = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"));

            //没提供点或只提供了一个中心点时,获取当前空间的范围
            //检查当前空间是否为模型空间
            if (acCurDb.TileMode == true)
            {
                if (pMin.Equals(new Point3d()) == true &&
                    pMax.Equals(new Point3d()) == true)
                {
                    pMin = acCurDb.Extmin;
                    pMax = acCurDb.Extmax;
                }
            }
            else
            {
                //检查当前空间是否为图纸空间
                if (nCurVport == 1)
                {
                    //获取图纸空间范围
                    if (pMin.Equals(new Point3d()) == true &&
                        pMax.Equals(new Point3d()) == true)
                    {
                        pMin = acCurDb.Pextmin;
                        pMax = acCurDb.Pextmax;
                    }
                }
                else
                {
                    //获取模型空间范围
                    if (pMin.Equals(new Point3d()) == true &&
                        pMax.Equals(new Point3d()) == true)
                    {
                        pMin = acCurDb.Extmin;
                        pMax = acCurDb.Extmax;
                    }
                }
            }
            //启动事务
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //获取当前视图
                using (ViewTableRecord acView = acDoc.Editor.GetCurrentView())
                {
                    Extents3d eExtents;
                    //将WCS坐标变换为DCS坐标
                    Matrix3d matWCS2DCS;
                    matWCS2DCS = Matrix3d.PlaneToWorld(acView.ViewDirection);
                    matWCS2DCS = Matrix3d.Displacement(acView.Target - Point3d.Origin) * matWCS2DCS;
                    matWCS2DCS = Matrix3d.Rotation(-acView.ViewTwist,
                                                   acView.ViewDirection,
                                                   acView.Target) * matWCS2DCS;
                    //如果指定了中心点,就为中心模式和比例模式
                    //设置显示范围的最小点和最大点;
                    if (pCenter.DistanceTo(Point3d.Origin) != 0)
                    {
                        pMin = new Point3d(pCenter.X - (acView.Width / 2),
                                           pCenter.Y - (acView.Height / 2), 0);
                        pMax = new Point3d((acView.Width / 2) + pCenter.X,
                                           (acView.Height / 2) + pCenter.Y, 0);
                    }
                    //用直线创建范围对象;
                    using (Line acLine = new Line(pMin, pMax))
                    {
                        eExtents = new Extents3d(acLine.Bounds.Value.MinPoint,
                                                 acLine.Bounds.Value.MaxPoint);
                    }
                    //计算当前视图的宽高比
                    double dViewRatio;
                    dViewRatio = (acView.Width / acView.Height);
                    //变换视图范围
                    matWCS2DCS = matWCS2DCS.Inverse();
                    eExtents.TransformBy(matWCS2DCS);
                    double  dWidth;
                    double  dHeight;
                    Point2d pNewCentPt;
                    //检查是否提供了中心点(中心模式和比例模式)
                    if (pCenter.DistanceTo(Point3d.Origin) != 0)
                    {
                        dWidth  = acView.Width;
                        dHeight = acView.Height;
                        if (dFactor == 0)
                        {
                            pCenter = pCenter.TransformBy(matWCS2DCS);
                        }
                        pNewCentPt = new Point2d(pCenter.X, pCenter.Y);
                    }
                    else //窗口、范围和界限模式下
                    {
                        //计算当前视图的宽高新值;
                        dWidth  = eExtents.MaxPoint.X - eExtents.MinPoint.X;
                        dHeight = eExtents.MaxPoint.Y - eExtents.MinPoint.Y;
                        //获取视图中心点
                        pNewCentPt = new Point2d(((eExtents.MaxPoint.X + eExtents.MinPoint.X) * 0.5),
                                                 ((eExtents.MaxPoint.Y + eExtents.MinPoint.Y) * 0.5));
                    }
                    //检查宽度新值是否适于当前窗口
                    if (dWidth > (dHeight * dViewRatio))
                    {
                        dHeight = dWidth / dViewRatio;
                    }
                    //调整视图大小;
                    if (dFactor != 0)
                    {
                        acView.Height = dHeight * dFactor;
                        acView.Width  = dWidth * dFactor;
                    }
                    //设置视图中心;
                    acView.CenterPoint = pNewCentPt;
                    //更新当前视图;
                    acDoc.Editor.SetCurrentView(acView);
                }
                //提交更改;
                acTrans.Commit();
            }
        }
示例#31
0
        /// <summary>
        /// 视图缩放
        /// </summary>
        /// <param name="ed">命令行对象</param>
        /// <param name="ptMin">要显示区域的左下角点</param>
        /// <param name="ptMax">要显示区域的右上角点</param>
        /// <param name="ptCenter">要显示区域的中心点</param>
        /// <param name="factor">缩放比例</param>
        public static void Zoom(this Editor ed, Point3d ptMin, Point3d ptMax, Point3d ptCenter, double factor)
        {
            Extents3d extents;
            Document  doc    = ed.Document;
            Database  db     = doc.Database;
            int       cvport = Convert.ToInt32(Application.GetSystemVariable("CVPORT"));

            if (db.TileMode == true)
            {
                if (ptMin.Equals(Point3d.Origin) == true && ptMax.Equals(Point3d.Origin) == true)
                {
                    ptMin = db.Extmin;
                    ptMax = db.Extmax;
                }
            }
            else
            {
                if (cvport == 1)
                {
                    if (ptMin.Equals(Point3d.Origin) == true && ptMax.Equals(Point3d.Origin) == true)
                    {
                        ptMin = db.Pextmin;
                        ptMax = db.Pextmax;
                    }
                }
                else
                {
                    if (ptMin.Equals(Point3d.Origin) == true && ptMax.Equals(Point3d.Origin) == true)
                    {
                        ptMin = db.Extmin;
                        ptMax = db.Extmax;
                    }
                }
            }
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                ViewTableRecord view       = ed.GetCurrentView();
                Matrix3d        matWCS2DCS = view.Wcs2Dcs();
                if (ptCenter.DistanceTo(Point3d.Origin) != 0)
                {
                    ptMin = new Point3d(ptCenter.X - view.Width / 2, ptCenter.Y - view.Height / 2, 0);
                    ptMax = new Point3d(ptCenter.X + view.Width / 2, ptCenter.Y + view.Height / 2, 0);
                }
                using (Line line = new Line(ptMin, ptMax))
                {
                    extents = new Extents3d(line.GeometricExtents.MinPoint, line.GeometricExtents.MaxPoint);
                }
                double viewRatio = view.Width / view.Height;
                extents.TransformBy(matWCS2DCS);
                double  width, height;
                Point2d newCenter;
                if (ptCenter.DistanceTo(Point3d.Origin) != 0)
                {
                    width  = view.Width;
                    height = view.Height;
                    if (factor == 0)
                    {
                        ptCenter = ptCenter.TransformBy(matWCS2DCS);
                    }
                    newCenter = new Point2d(ptCenter.X, ptCenter.Y);
                }
                else
                {
                    width     = extents.MaxPoint.X - extents.MinPoint.X;
                    height    = extents.MaxPoint.Y - extents.MinPoint.Y;
                    newCenter = new Point2d((extents.MaxPoint.X + extents.MinPoint.X) * 0.5, (extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5);
                }
                if (width > height * viewRatio)
                {
                    height = width / viewRatio;
                }
                if (factor != 0)
                {
                    view.Height = height * factor;
                    view.Width  = width * factor;
                }
                view.CenterPoint = newCenter;
                ed.SetCurrentView(view);
                trans.Commit();
            }
        }
        public static void LoadProcedures()
        {
            _ListProcedures.Clear();

            #region Purge

            if (_DWGCollection.Purge)
            {
                _ListProcedures.Add(new Procedure(delegate(Database db, Transaction tr, DWGFileModel objDWG)
                {
                    // Create the list of objects to "purge"
                    ObjectIdCollection idsToPurge = ArCaUtils.GetObjIdNonGrafical(db);

                    // Call the Purge function to filter the list
                    db.Purge(idsToPurge);

                    if (idsToPurge.Count != 0)
                    {
                        // Erase each of the objects we've been
                        // allowed to
                        foreach (ObjectId id in idsToPurge)
                        {
                            DBObject obj = tr.GetObject(id, OpenMode.ForWrite);
                            obj.Erase();
                        }

                        //Was Changed
                        objDWG.IsChanged = true;
                    }
                }));
            }

            #endregion

            #region Lock View Ports

            if (_DWGCollection.LockViewPorts)
            {
                _ListProcedures.Add(new Procedure(delegate(Database db, Transaction tr, DWGFileModel objDWG)
                {
                    //Block Table
                    BlockTable btBlock = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    foreach (ObjectId btrId in btBlock)
                    {
                        BlockTableRecord ltr = tr.GetObject(btrId, OpenMode.ForWrite) as BlockTableRecord;
                        foreach (ObjectId acObjId in ltr)
                        {
                            if (acObjId.ObjectClass.DxfName == "VIEWPORT")
                            {
                                Viewport vp = tr.GetObject(acObjId, OpenMode.ForWrite) as Viewport;
                                vp.Locked   = true;
                                //Was Changed
                                objDWG.IsChanged = true;
                            }
                        }
                    }
                }));
            }

            if (_DWGCollection.ZoomExtents)
            {
                _ListProcedures.Add(new Procedure(delegate(Database db, Transaction tr, DWGFileModel objDWG)
                {
                    //Model
                    ViewportTable vpt = tr.GetObject(db.ViewportTableId, OpenMode.ForWrite) as ViewportTable;
                    foreach (ObjectId btrId in vpt)
                    {
                        ViewportTableRecord vptrec = tr.GetObject(btrId, OpenMode.ForWrite) as ViewportTableRecord;
                        SetViewportToExtents(db, vptrec);
                    }

                    //Paper Spaces
                    BlockTable btBlock = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    foreach (ObjectId btrId in btBlock)
                    {
                        BlockTableRecord btr = tr.GetObject(btrId, OpenMode.ForWrite) as BlockTableRecord;

                        if (!btr.Name.StartsWith("*Paper_Space"))
                        {
                            continue;
                        }

                        //Retrieve paper space viewport (viewport with the lowest handle)
                        Viewport paperVp  = null;
                        long lowestHandle = -1;
                        foreach (ObjectId acObjId in btr)
                        {
                            if (acObjId.ObjectClass.Name == "AcDbViewport")
                            {
                                Viewport vp = tr.GetObject(acObjId, OpenMode.ForRead) as Viewport;

                                if (lowestHandle < 0)
                                {
                                    lowestHandle = vp.Handle.Value;
                                    paperVp      = vp;
                                }
                                else if (vp.Handle.Value < lowestHandle)
                                {
                                    paperVp      = vp;
                                    lowestHandle = vp.Handle.Value;
                                }
                            }
                        }
                        if (paperVp == null)
                        {
                            return;
                        }
                        paperVp.UpgradeOpen();

                        double scrRatio = (paperVp.Width / paperVp.Height);

                        Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(paperVp.ViewDirection);

                        matWCS2DCS = Matrix3d.Displacement(paperVp.ViewTarget - Point3d.Origin) * matWCS2DCS;

                        matWCS2DCS = Matrix3d.Rotation(-paperVp.TwistAngle,
                                                       paperVp.ViewDirection,
                                                       paperVp.ViewTarget)
                                     * matWCS2DCS;

                        matWCS2DCS = matWCS2DCS.Inverse();

                        Extents3d extents = GetExtents(db, btr);

                        extents.TransformBy(matWCS2DCS);

                        double width = (extents.MaxPoint.X - extents.MinPoint.X);

                        double height = (extents.MaxPoint.Y - extents.MinPoint.Y);

                        Point2d center = new Point2d((extents.MaxPoint.X + extents.MinPoint.X) * 0.5,
                                                     (extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5);

                        if (width > (height * scrRatio))
                        {
                            height = width / scrRatio;
                        }

                        paperVp.ViewHeight = height;
                        paperVp.ViewCenter = center;

                        //Was Changed
                        objDWG.IsChanged = true;
                    }
                }));
            }

            #endregion
        }
示例#33
0
        // Zooms to given objects
        public static void ZoomToObjects(IEnumerable<ObjectId> ids)
        {
            Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            Extents3d outerext = new Extents3d();
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                try
                {
                    foreach (ObjectId id in ids)
                    {
                        Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
                        Extents3d ext = ent.GeometricExtents;
                        outerext.AddExtents(ext);
                    }
                }
                catch
                {
                    ;
                }
            }

            outerext.TransformBy(ed.CurrentUserCoordinateSystem.Inverse());
            Point2d min2d = new Point2d(outerext.MinPoint.X, outerext.MinPoint.Y);
            Point2d max2d = new Point2d(outerext.MaxPoint.X, outerext.MaxPoint.Y);

            ViewTableRecord view = new ViewTableRecord();

            view.CenterPoint = min2d + ((max2d - min2d) / 2.0);
            view.Height = max2d.Y - min2d.Y;
            view.Width = max2d.X - min2d.X;

            ed.SetCurrentView(view);
        }