private void GridGenerator(XYZ end0, XYZ end1, out List <DetailGrid> dgList)
        {
            dgList = new List <DetailGrid>();

            List <double> xList = new List <double>();

            #region xList (Columns)
            xList.Add(end0.X);
            double colNum   = 5;
            double colSpace = Math.Abs(end0.X - end1.X) / colNum;

            double colInc = end0.X;

            for (int i = 1; i <= colNum - 1; i++)
            {
                colInc -= colSpace;
                xList.Add(colInc);
            }
            #endregion

            List <double> yList = new List <double>();
            #region yList (Rows)
            yList.Add(end0.Y);
            double rowNum   = 4;
            double rowSpace = Math.Abs(end0.Y - end1.Y) / rowNum;

            double rowInc = end0.Y;

            for (int i = 1; i <= rowNum - 1; i++)
            {
                rowInc += rowSpace;
                yList.Add(rowInc);
            }
            #endregion

            int    pointIndex = 0;
            int    yIndex     = 0;
            double y          = 0;
            foreach (double x in xList)
            {
                for (int i = 0; i < 4; i++)
                {
                    y = yList.ElementAt(i);
                    DetailGrid dg = new DetailGrid();
                    XYZ        np = new XYZ(x, y, 0);
                    pointIndex++;
                    dg.detailNum = pointIndex;
                    dg.point     = np;
                    dgList.Add(dg);
                }
            }
        }
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
        {
            UIApplication uiApp = commandData.Application;
            Document      doc   = commandData.Application.ActiveUIDocument.Document;

            XYZ startPoint = new XYZ(0, 0, 0);
            XYZ endPoint   = new XYZ(0, 0, 0);

            GetSheetOutLine(doc, out startPoint, out endPoint);

            List <DetailGrid> dgList = new List <DetailGrid>();

            GridGenerator(startPoint, endPoint, out dgList);

            List <Viewport> vpList = new List <Viewport>();

            GetAllViewsOnSheet(doc, out vpList);
            foreach (Viewport vp in vpList)
            {
                XYZ VpSp = new XYZ(0, 0, 0);
                XYZ VpEp = new XYZ(0, 0, 0);
                GetOutLine(doc, vp, out VpSp, out VpEp);

                //Get Detail Number
                Parameter dtNumPara = vp.get_Parameter(BuiltInParameter.VIEWER_DETAIL_NUMBER);
                int       vpDtNum   = Convert.ToInt32(dtNumPara.AsString());

                //Use Detail number to find detail location
                int        detailNum = vpDtNum;
                DetailGrid dg        = new DetailGrid();
                dg = dgList.ElementAt(detailNum - 1);

                //calculate new center point
                XYZ    cenPoint   = vp.GetBoxCenter();
                double xOffCenter = Math.Abs(cenPoint.X - VpSp.X);
                double yOffCenter = Math.Abs(cenPoint.Y - VpSp.Y);
                XYZ    newCenter  = new XYZ(dg.point.X - xOffCenter, dg.point.Y + yOffCenter, 0);

                //Set new detail location
                using (Transaction t = new Transaction(doc, "relocate View"))
                {
                    t.Start();
                    vp.SetBoxCenter(newCenter);
                    t.Commit();
                }
            }

            return(Result.Succeeded);
        }