AutoCAD2012
VS2010
this is a test file... there 2 lines only..
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.
Here two different way how to get coordinates (with Editor & without, using only Database).