Author Topic: Arrange modelspace as in paperspace problem  (Read 1154 times)

0 Members and 1 Guest are viewing this topic.

midko

  • Mosquito
  • Posts: 3
Arrange modelspace as in paperspace problem
« on: July 19, 2021, 11:29:39 AM »
I have written a method that moves some xrefs around in model space replicating their arrangement in the paperspace layout.
Most of the times it works perfectly. However the sample files below the script fails. You can see below the expected result and the jumbled result.




Code: [Select]
       [CommandMethod("tecFixMs")]
        public void tecFixMs()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            LayoutManager layoutMan = LayoutManager.Current;

            using (Transaction Tx = db.TransactionManager.StartTransaction())
            {
                DBDictionary layoutDict = (DBDictionary)db.LayoutDictionaryId.GetObject(OpenMode.ForWrite);
                BlockTableRecord ms = (BlockTableRecord)Tx.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);

                foreach (DBDictionaryEntry entry in layoutDict)
                {
                    string layoutName = entry.Key;
                    if (layoutName == "Layout1")
                    {
                        ed.WriteMessage("---------------");
                        Point3d basept = new Point3d(0, 0, 0);
                        //Point3d lastbasept = new Point3d(0, 0, 0);
                        Layout layoutObj = (Layout)layoutMan.GetLayoutId(layoutName).GetObject(OpenMode.ForWrite);
                        ed.WriteMessage("{0} - Viewports: {1}\n", layoutObj.LayoutName, layoutObj.GetViewports().Count);
                        ObjectIdCollection initObj = layoutObj.GetViewports();

                        Vector3d oshift = new Vector3d(0, 0, 0);
                        Point3d lastvpcpt = new Point3d(0, 0, 0);
                        int counter = 0;
                        Point3d psbasept = new Point3d(0, 0, 0);
                        Point3d msbasept = new Point3d(0, 0, 0);

                        foreach (ObjectId vpId in initObj)
                        {
                            Viewport vp = (Viewport)vpId.GetObject(OpenMode.ForWrite);
                            ed.WriteMessage("VP # - {0} - center: {1} view center: {2}\n", vp.Number, vp.CenterPoint, vp.ViewCenter);

                            Polyline vpOutlineInMs = null;
                            if (vp.NonRectClipOn)
                            {
                                ObjectId vpClipId = vp.NonRectClipEntityId;
                                Entity vpBoundary = (Entity)Tx.GetObject(vpClipId, OpenMode.ForRead);
                                vpOutlineInMs = (Polyline)vpBoundary.Clone();
                            }
                            else
                            {
                                Extents3d vpExt = vp.GeometricExtents;
                                vpOutlineInMs = new Polyline(4);
                                vpOutlineInMs.AddVertexAt(0, new Point2d(vpExt.MinPoint.X, vpExt.MinPoint.Y), 0, 0, 0);
                                vpOutlineInMs.AddVertexAt(1, new Point2d(vpExt.MaxPoint.X, vpExt.MinPoint.Y), 0, 0, 0);
                                vpOutlineInMs.AddVertexAt(2, new Point2d(vpExt.MaxPoint.X, vpExt.MaxPoint.Y), 0, 0, 0);
                                vpOutlineInMs.AddVertexAt(3, new Point2d(vpExt.MinPoint.X, vpExt.MaxPoint.Y), 0, 0, 0);
                                vpOutlineInMs.Closed = true;
                            }

                            // ViewportExtensionMethods.cs  (c) 2007-2012  Tony Tanzillo
                            Point3d vpMScpt = new Point3d(vp.ViewCenter.X, vp.ViewCenter.Y, 0.0);
                            Point3d vpPScpt = vp.CenterPoint;

                            Matrix3d msToPs = Matrix3d.Displacement(new Vector3d(vp.CenterPoint.X - vpMScpt.X, vp.CenterPoint.Y - vpMScpt.Y, 0.0))
                                                    * Matrix3d.Scaling(vp.CustomScale, vpMScpt)
                                                    * Matrix3d.Rotation(vp.TwistAngle, Vector3d.ZAxis, Point3d.Origin)
                                                    * Matrix3d.WorldToPlane(new Plane(vp.ViewTarget, vp.ViewDirection));
                            vpOutlineInMs.TransformBy(msToPs.Inverse());

                            foreach (ObjectId entId in ms)
                            {
                                Entity ent = (Entity)Tx.GetObject(entId, OpenMode.ForWrite);
                                try
                                {
                                    Extents3d entExtents = ent.GeometricExtents;
                                    Point3d cpt = new Point3d(entExtents.MinPoint.X + (entExtents.MaxPoint.X - entExtents.MinPoint.X) / 2, entExtents.MinPoint.Y + (entExtents.MaxPoint.Y - entExtents.MinPoint.Y) / 2, 0);

                                    if (IsInside2D(vpOutlineInMs, cpt))
                                    {
                                        Matrix3d vptargettransform = new Matrix3d();
                                        if (counter == 0)
                                        {
                                            counter = 1;
                                            vptargettransform = Matrix3d.Displacement(vpMScpt.GetVectorTo(msbasept));
                                        }
                                        else
                                        {
                                            Matrix3d bpmat = Matrix3d.Displacement(psbasept.GetVectorTo(vpPScpt).TransformBy(Matrix3d.Scaling(1 / vp.CustomScale, vpPScpt)));
                                            msbasept = msbasept.TransformBy(bpmat);
                                            vptargettransform = Matrix3d.Displacement(vpMScpt.GetVectorTo(msbasept));
                                        }
                                        ent.TransformBy(vptargettransform);

                                        vp.ViewTarget = vp.ViewTarget.TransformBy(vptargettransform); ;
                                        //msbasept = vpMScpt;
                                        psbasept = vpPScpt;
                                    }
                                }
                                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                                {
                                }
                            }
                        }
                    }
                }
                Tx.Commit();
            }
        }