Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
//
...
foreach (ObjectId oi in ids)
{
Line ln = (Line) tr.GetObject(oi, OpenMode.ForWrite);
Matrix3d m = doc.Editor.CurrentCoordinateSystem.Inverse();
Point3d pt = ln.StartPoint.TransformBy(m);
//some coordinates calculations
...
}
Database db = new Database(false, true);
db.ReadDwgFile(filePath, System.IO.FileShare.Read, true, "");
[color=red] //i tried to use Viewport to get this matrix[/color]
using (Transaction tr = db.TransactionManager.StartTransaction())
{
UcsTable acUCSTbl;
UcsTableRecord acUCSTblRec;
acUCSTbl = tr.GetObject(db.UcsTableId, OpenMode.ForRead) as UcsTable;
if (!acUCSTbl.Has("ucsCoord"))
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("not Exist");
else
{
acUCSTblRec = tr.GetObject(acUCSTbl["ucsCoord"], OpenMode.ForRead) as UcsTableRecord;
ViewportTable vt = tr.GetObject(db.ViewportTableId, OpenMode.ForRead) as ViewportTable;
ViewportTableRecord vtr = tr.GetObject(vt["*Active"], OpenMode.ForWrite) as ViewportTableRecord;
//vtr.SetUcs(acUCSTblRec.ObjectId);
//multiEd.UpdateTiledViewportsFromDatabase();
//multiEd.UpdateScreen();
//ucsMatrix = multiEd.CurrentUserCoordinateSystem;
//Here am stuck........
CoordinateSystem3d cs = vtr.Ucs;
ucsMatrix = Matrix3d.PlaneToWorld(vtr.ViewDirection);
ucsMatrix = Matrix3d.Displacement(vtr.Target - Point3d.Origin)*ucsMatrix;
ucsMatrix = Matrix3d.Rotation(-vtr.ViewTwist, vtr.ViewDirection, vtr.Target) * ucsMatrix;
ucsMatrix = ucsMatrix.Inverse();
tr.Commit();
}
}
public static Matrix3d GetUcsMatrix(Database db)
{
Point3d toOrigin;
Vector3d toXAxis, toYAxis, toZAxis;
toOrigin = db.Ucsorg;
toXAxis = db.Ucsxdir;
toYAxis = db.Ucsydir;
toZAxis = toXAxis.CrossProduct(toYAxis);
return Matrix3d.AlignCoordinateSystem(
Point3d.Origin,
Vector3d.XAxis,
Vector3d.YAxis,
Vector3d.ZAxis,
toOrigin, toXAxis, toYAxis, toZAxis);
}
and after:Point3d pt = ln.StartPoint.TransformBy(GetUcsMatrix(db).Inverse());
But it doesnt works(
But it doesnt works(
Point3d pt = ln.StartPoint.TransformBy(GetUcsMatrix(db));
Database db = new Database(false, true);
db.ReadDwgFile("C:\\test.dwg", System.IO.FileShare.ReadWrite, true, "");
...
Matrix3d e2u = getUcsMatrix(db);
using (Transaction trLn = db.TransactionManager.StartTransaction())
{
try
{
BlockTable bt = (BlockTable)trLn.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord btr = (BlockTableRecord)trLn.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
foreach (ObjectId objId in btr)
{
Entity ent = (Entity)trLn.GetObject(objId, OpenMode.ForRead);
if (ent.GetType().Name == "Line")
{
Line ln = ent as Line;
//if (ln.Layer == "0")
{
geoPoint = ln.StartPoint.TransformBy(e2u);
sRiga = geoPoint.X.ToString() + "," + geoPoint.Y.ToString() + ",";
geoPoint = ln.EndPoint.TransformBy(e2u);
sRiga += geoPoint.X.ToString() + "," + geoPoint.Y.ToString() + ";";
StreamWriter sw = new StreamWriter(FO, true);
sw.WriteLine("COORDINATE;");
sw.WriteLine(sRiga);
sw.Close();
StreamWriter swFO = new StreamWriter(FOS, true);
swFO.WriteLine("COORDINATE;");
swFO.WriteLine(sRiga);
swFO.Close();
}
}
}
}
catch (SystemException ex)
{
MessageBox.Show(ex.Message);
}
trLn.Commit();
}
public static Matrix3d getUcsMatrix (Database db)
{
System.Diagnostics.Debug.Assert(db != null);
Point3d toOrigin;
Vector3d toXAxis, toYAxis, toZAxis;
toOrigin = db.Ucsorg;
toXAxis = db.Ucsxdir;
toYAxis = db.Ucsydir;
toZAxis = toXAxis.CrossProduct(toYAxis);
return Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, toOrigin, toXAxis, toYAxis, toZAxis).Inverse();
}
i get some coordinates and they're different when i use the code below:
TypedValue[] tv = new TypedValue[4] { new TypedValue((int)DxfCode.Start, "LINE"), new TypedValue(67, 0), new TypedValue(62, CL), new TypedValue(8, LL) };
SelectionFilter sflt = new SelectionFilter(tv);
PromptSelectionResult resSel = ed.SelectAll(sflt);
SelectionSet selSel = resSel.Value;
ObjectId[] ids = selSel.GetObjectIds();
foreach (ObjectId oi in ids)
{
Line ln = (Line)trLn.GetObject(oi, OpenMode.ForWrite);
Matrix3d ucs2wcs = doc.Editor.CurrentUserCoordinateSystem.Inverse();
Point3d geoPoint = ln.StartPoint.TransformBy(ucs2wcs);
....
I think don't care about dwg file, but in attached. I think don't care about dwg file, but in attached.
in first case we have:
COORDINATE;
1516200.2924,5031892.933,1516385.5636,5031892.8813;
COORDINATE;
1516354.1415,5032032.582,1516354.1834,5032182.7509;
COORDINATE;
1516210.8185,5032032.622,1516354.1415,5032032.582;
in second case we have:
COORDINATE;
387.0067,315.2043,530.3297,315.2043;
COORDINATE;
530.3297,315.2043,530.3297,465.3732;
COORDINATE;
376.5197,175.5124,561.7909,175.5124;
Matrix3d e2u = getUcsMatrix(db);
getUcsMatrix retun for me e2u = Matrix3d.Identity;