Example #1
0
        /// <summary>
        ///     Method to find the object under the mleader arrow
        /// </summary>
        /// <param name="mLeader"></param>
        /// <returns></returns>
        private static ObjectId GetObjectUnderArrow(this MLeader mLeader)
        {
            var leaderInd = mLeader.GetLeaderIndexes();
            var objId     = ObjectId.Null;

            var acCurDoc = Application.DocumentManager.MdiActiveDocument;
            var acCurDb  = acCurDoc.Database;
            var acCurEd  = acCurDoc.Editor;

            try
            {
                if (leaderInd.Count != 0)
                {
                    var leaderLineInd = mLeader.GetLeaderLineIndexes((int)leaderInd[0]);
                    var arrowPt       = mLeader.GetFirstVertex((int)leaderLineInd[0]);

                    SelectionSet ss = null;

                    if (LayoutManager.Current.CurrentLayout == "Model")
                    {
                        arrowPt = arrowPt.Trans(acCurEd, CoordSystem.Wcs, CoordSystem.Ucs);

                        var p   = arrowPt;
                        var tol = 0.01;

                        var p1 = new Point3d(p.X - tol, p.Y - tol, p.Z - tol);
                        var p2 = new Point3d(p.X + tol, p.Y + tol, p.Z + tol);

                        var res = acCurEd.SelectCrossingWindow(p1, p2);

                        ss = res.Value;
                    }
                    else
                    {
                        var layoutName = LayoutManager.Current.CurrentLayout;

                        using (var acTrans = acCurDb.TransactionManager.StartTransaction())
                        {
                            var dbDict = (DBDictionary)acTrans.GetObject(acCurDb.LayoutDictionaryId, OpenMode.ForRead);

                            if (dbDict.Contains(layoutName))
                            {
                                var id = dbDict.GetAt(layoutName);

                                var curLayout = acTrans.GetObject(id, OpenMode.ForRead) as Layout;

                                if (curLayout != null)
                                {
                                    var vPorts = new List <Viewport>();

                                    //Get viewports from chosen layout
                                    using (
                                        var blkTblRec =
                                            acTrans.GetObject(curLayout.BlockTableRecordId, OpenMode.ForRead) as
                                            BlockTableRecord)
                                    {
                                        if (blkTblRec != null)
                                        {
                                            foreach (var lObjId in blkTblRec)
                                            {
                                                if (lObjId == acCurDb.PaperSpaceVportId)
                                                {
                                                    continue;
                                                }

                                                var acVp = acTrans.GetObject(lObjId, OpenMode.ForWrite) as Viewport;
                                                if (acVp != null && !acVp.IsErased)
                                                {
                                                    vPorts.Add(acVp);
                                                }
                                            }
                                        }
                                    }

                                    if (vPorts.Count > 0)
                                    {
                                        var insideVp   = false;
                                        var vpLocation = 0;

                                        for (var index = 0; index < vPorts.Count; index++)
                                        {
                                            var acVp = vPorts[index];
                                            if (arrowPt.IsInside(acVp.GeometricExtents))
                                            {
                                                insideVp   = true;
                                                vpLocation = index;
                                                break;
                                            }
                                        }

                                        if (insideVp)
                                        {
                                            var psVpPnts = new Point3dCollection();

                                            // now extract the viewport geometry
                                            vPorts[vpLocation].GetGripPoints(psVpPnts, new IntegerCollection(),
                                                                             new IntegerCollection());

                                            // let's assume a rectangular vport for now, make the cross-direction grips square
                                            var tmp = psVpPnts[2];
                                            psVpPnts[2] = psVpPnts[1];
                                            psVpPnts[1] = tmp;

                                            var xform = vPorts[vpLocation].Dcs2Wcs() * vPorts[vpLocation].Psdcs2Dcs();
                                            arrowPt = arrowPt.TransformBy(xform);

                                            // now switch to MS
                                            acCurEd.SwitchToModelSpace();
                                            // set the CVPort
                                            Application.SetSystemVariable("CVPORT", vPorts[vpLocation].Number);

                                            var p   = arrowPt;
                                            var tol = 0.01;
                                            var p1  = new Point3d(p.X - tol, p.Y - tol, p.Z - tol);
                                            var p2  = new Point3d(p.X + tol, p.Y + tol, p.Z + tol);

                                            var res = acCurEd.SelectCrossingWindow(p1, p2);

                                            if (res.Status != PromptStatus.OK)
                                            {
                                                acCurEd.WriteMessage(res.Status.ToString());
                                            }

                                            ss = res.Value;

                                            // now switch to MS
                                            acCurEd.SwitchToPaperSpace();
                                        }
                                    }
                                }
                            }
                            else
                            {
                                acTrans.Abort();
                            }

                            acTrans.Commit();
                        }
                    }

                    if (ss == null)
                    {
                        return(objId);
                    }

                    var objIds = new ObjectIdCollection();

                    foreach (SelectedObject obj in ss)
                    {
                        if (obj.ObjectId != _curLeaderId)
                        {
                            objIds.Add(obj.ObjectId);
                        }
                    }

                    if (objIds.Count > 0)
                    {
                        objId = objIds[objIds.Count - 1];
                    }
                }
            }
            catch (Exception)
            {
                //Ignored
            }

            return(objId);
        }