• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Contributor
    ssdiesel
    Posts: 18
    Registered: ‎02-07-2012

    Get matrix3d from Database

    163 Views, 3 Replies
    02-07-2012 05:15 PM

    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;

    Please use plain text.
    Contributor
    ssdiesel
    Posts: 18
    Registered: ‎02-07-2012

    Re: Get matrix3d from Database

    02-07-2012 06:13 PM in reply to: ssdiesel

    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? 

     

    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,168
    Registered: ‎04-09-2008

    Re: Get matrix3d from Database

    02-08-2012 01:15 AM in reply to: ssdiesel

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


    Пожалуйста не забывайте про Утвердить в качестве решения!Утвердить в качестве решения и Give Kudos!Баллы
    Please remember to Accept Solution!Accept as Solution and Give Kudos!Kudos

    Please use plain text.
    Contributor
    ssdiesel
    Posts: 18
    Registered: ‎02-07-2012

    Re: Get matrix3d from Database

    02-08-2012 01:48 AM in reply to: Alexander.Rivilis

    yeap. now i know the difference.

    thx, again

    Please use plain text.