.NET

Reply
Contributor
ssdiesel
Posts: 20
Registered: ‎02-07-2012
Message 1 of 4 (206 Views)

Get matrix3d from Database

206 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;

Contributor
ssdiesel
Posts: 20
Registered: ‎02-07-2012
Message 2 of 4 (197 Views)

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? 

 

Moderator
Alexander.Rivilis
Posts: 1,398
Registered: ‎04-09-2008
Message 3 of 4 (180 Views)

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

Contributor
ssdiesel
Posts: 20
Registered: ‎02-07-2012
Message 4 of 4 (178 Views)

Re: Get matrix3d from Database

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

yeap. now i know the difference.

thx, again

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community