TheSwamp

Code Red => .NET => Topic started by: latour_g on December 13, 2021, 02:48:39 PM

Title: Problem with transformation World to Plan
Post by: latour_g on December 13, 2021, 02:48:39 PM
Hi,

I have a command that trace all viewport in model and add the name of it at the lower left corner (see image attach for better explanation).
It's working fine except when viewport in presentation is Plan Current UCS instead of World.

The line are at the right place, only the text is not at the right place.

I have tried with :

I have attach a dwg file to test with. 

Here is the complete code but the problematic part is at the end where I set my mtext.  Thank you !

        public static void vpAreaAll()
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            bool lTileMode = doc.Database.TileMode;

            string StartLayout = LayoutManager.Current.CurrentLayout;

            using (doc.LockDocument())
            {
                using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
                {
                    DBDictionary layoutDict = (DBDictionary)tr.GetObject(doc.Database.LayoutDictionaryId, OpenMode.ForRead);

                    foreach (DictionaryEntry layoutEntry in layoutDict)
                    {
                        Layout layoutObj = (Layout)tr.GetObject((ObjectId)(layoutEntry.Value), OpenMode.ForWrite);

                        if (layoutObj.LayoutName == "Model") continue;
                        LayoutManager.Current.CurrentLayout = layoutObj.LayoutName;

                        ObjectIdCollection objs = layoutObj.GetViewports();
                        int nCnt = 0;
                        foreach (ObjectId obj in objs)
                        {
                            if (nCnt++ == 0) continue;
                            Viewport vp = (Viewport)tr.GetObject(obj, OpenMode.ForRead);
                            drawVPLimits(vp.ObjectId, layoutObj.LayoutName);
                        }
                    }
                    layoutDict.Dispose();
                    tr.Commit();
                }
            }
        }

        private static void drawVPLimits(ObjectId vpId, string cLayout)
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            using (doc.LockDocument())
            {
                using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
                {
                    Editor ed = doc.Editor;
                    Point3dCollection pnts = new Point3dCollection();

                    DBObject objT = tr.GetObject(vpId, OpenMode.ForRead);
                    Viewport vp = objT as Viewport;

                    if (vp == null) { tr.Commit(); return; }

                    Point3d pntC = new Point3d(vp.ViewTarget.X + vp.ViewCenter.X, vp.ViewTarget.Y + vp.ViewCenter.Y, 0);
                    Point3d pt3d = new Point3d(0, 0, 0);
                    Matrix3d matPS2MS;
                    matPS2MS = Matrix3d.PlaneToWorld(vp.ViewDirection);
                    matPS2MS = Matrix3d.Displacement(pt3d - Point3d.Origin) * matPS2MS;
                    matPS2MS = Matrix3d.Rotation(-vp.TwistAngle, vp.ViewDirection, pt3d) * matPS2MS;

                    LayoutManager.Current.CurrentLayout = cLayout;
                    pnts.Add(new Point3d(pntC.X - (vp.Width / 2) / vp.CustomScale, pntC.Y - (vp.Height / 2) / vp.CustomScale, 0).TransformBy(matPS2MS));
                    pnts.Add(new Point3d(pntC.X - (vp.Width / 2) / vp.CustomScale, pntC.Y + (vp.Height / 2) / vp.CustomScale, 0).TransformBy(matPS2MS));
                    pnts.Add(new Point3d(pntC.X + (vp.Width / 2) / vp.CustomScale, pntC.Y + (vp.Height / 2) / vp.CustomScale, 0).TransformBy(matPS2MS));
                    pnts.Add(new Point3d(pntC.X + (vp.Width / 2) / vp.CustomScale, pntC.Y - (vp.Height / 2) / vp.CustomScale, 0).TransformBy(matPS2MS));

                    doc.Database.TileMode = true;

                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(doc.Database.CurrentSpaceId, OpenMode.ForWrite);
                    bool bPouce = ((Int16)AcadApp.GetSystemVariable("LUNITS") == 4) ? true : false;
                    double nScale = (bPouce) ? 25.4 : 1;
                   
                    Polyline pl = new Polyline();
                    pl.SetDatabaseDefaults();
                    foreach (Point3d pnt in pnts) pl.AddVertexAt(pl.NumberOfVertices, new Point2d(pnt.X, pnt.Y), 0, 0, 0);
                    pl.Closed = true;
                    pl.ColorIndex = 256;
                    pl.Plinegen = true;
                    btr.AppendEntity(pl);
                    tr.AddNewlyCreatedDBObject(pl, true);

                    pntC = new Point3d(pnts[0].X + (3 / nScale) / vp.CustomScale, pnts[0].Y + (3 / nScale) / vp.CustomScale, 0);

                    // texte d'identification
                    MText txt = new MText();
                    txt.SetDatabaseDefaults();
                    txt.TextHeight = (5 / vp.CustomScale) * (1 / nScale);
                    txt.ColorIndex = 256;
                    //txt.Rotation = 0.0;
                    //txt.Rotation = (double)AcadApp.GetSystemVariable("SNAPANG");
                    txt.Annotative = AnnotativeStates.False;
                    txt.Contents = "\\fArial Black|b1|i0|c0|p34;" + cLayout;
                    txt.Attachment = AttachmentPoint.BottomLeft;
                    txt.Location = pntC.TransformBy(matPS2MS);
                    //txt.TransformBy(ed.CurrentUserCoordinateSystem);
                    btr.AppendEntity(txt);
                    tr.AddNewlyCreatedDBObject(txt, true);
                    //
                    tr.Commit();
                }
            }
        }
Title: Re: Problem with transformation World to Plan
Post by: LinhPham on January 19, 2022, 04:05:10 AM
I see you can draw the trace polyline, that mean you know the WCS coordinates of 4 corners and the directions of 4 edges.
So I think you can easily put the text to one corner of the polyline, without any more transformation.
pntC seems to be the center point of the polyline, in WCS. I think you put the text there, without any transformation, you will have the text position at center.
But not sure why you have the yellow text at bottom left though.
Title: Re: Problem with transformation World to Plan
Post by: latour_g on April 08, 2022, 04:18:36 PM
I didn't take the time to solve this problem because there was always something more important to do before that but today I took the time.

In the first image I sent, the text in red square should have shown like the image above. 
To solve the rotation, I used that : txt.Rotation = txt.Rotation + ((360 * (Math.PI / 180) - nTwist));

nTwist refer to VIEWTWIST on the viewport selected.

So everything is fine and thank you for your reply :-)