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 :
- TransformBy(matPS2MS);
- TransformBy(ed.CurrentUserCoordinateSystem);
- no transformation
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();
}
}
}