.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get matrix3d from Database

3 REPLIES 3
Reply
Message 1 of 4
SergeyShevtsov
491 Views, 3 Replies

Get matrix3d from Database

Hello everyone.
i gets some coordinates when i use MdiActiveDocument:

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
    ...
}

 But in case when i dont use  MdiActiveDocument (i have only Database), i cant gets matrix m. Can i get this matrix "m" from db only? 

am doing the next:

Database db = new Database(false, true);
db.ReadDwgFile("C:\\test.dwg", System.IO.FileShare.ReadWrite, true, "");

...
Matrix3d m = 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(m);
                                    
                                    sRiga = geoPoint.X.ToString() + "," + geoPoint.Y.ToString() + ",";
                                    geoPoint = ln.EndPoint.TransformBy(m);
                                    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(); 
        }

 Maybe i have some errors in the code? 
i have a little idea, maybe its wrong that i opened Entity object "forRead" only?
But if i set OpenMode.ForWrite... i have error(
and with Database my code is not working(
i wrote 2 variants of code
- with Editor
- without Editor
u can compile them and test on drawing, and u will see that results are differents(
P.S.: am working only in a Model Space

in first case we have (That's the result of a successful transformation to my UCS):
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 (That's the result of a unsuccessful transformation to my UCS):
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;

3 REPLIES 3
Message 2 of 4

i think i know where is the problem:

when i use

Matrix3d m = getUcsMatrix(db);

getUcsMatrix retun for me m = Matrix3d.Identity;

thats why i cant trnslate coordinates.

But the other question why getUcsMatrix return wrong matrix? 

 

Message 3 of 4

I think you already knew that the difference between active UCS and named UCS.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 4 of 4

yeap. now i know the difference.

thx, again

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost