Author Topic: Get position of all Objects in drawing  (Read 1823 times)

0 Members and 1 Guest are viewing this topic.

michiel

  • Guest
Get position of all Objects in drawing
« on: September 26, 2012, 09:22:30 AM »
Hi all!

Is it possible to get all the positions (Point3d) of the objects (blocks) placed in the drawing?
So I can compare it with the startposition that I have saved and see if an object has moved, or is there an other solution?

Thx in advance!

M.

BillZndl

  • Guest
Re: Get position of all Objects in drawing
« Reply #1 on: September 26, 2012, 10:54:15 AM »
Is it possible to get all the positions (Point3d) of the objects (blocks) placed in the drawing?

If you mean position of BlockReferences, sure.

This will list all blockreferences and positions in the active document,
however, you may want to add the block refs rotation also.
Code: [Select]
public class BlockNameList
{
public static void ListBlockRefsInDwg()
{           
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;           
Database db = HostApplicationServices.WorkingDatabase;           

using(DocumentLock doclck = doc.LockDocument())
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);

foreach (ObjectId bId in bt)
{
BlockTableRecord blkRec = (BlockTableRecord)trans.GetObject(bId, OpenMode.ForRead);

if (!blkRec.IsLayout && !blkRec.IsFromExternalReference && !blkRec.IsDependent)
{
                            ObjectIdCollection blkRefIds = blkRec.GetBlockReferenceIds(false, false);

                            foreach (ObjectId oid in blkRefIds)
                            {
                                BlockReference bref = (BlockReference)trans.GetObject(oid, OpenMode.ForRead);
                                ed.WriteMessage("\n BlockRef: " + bref.Name + " Origin = " + bref.Position);
                            }
}
}       
}
}
}
}

fixo

  • Guest
Re: Get position of all Objects in drawing
« Reply #2 on: September 26, 2012, 05:54:40 PM »
You can write block info to the file, then compare contents when you need it
Try this sample code, change xml file path to your suit, in this code I used
time as suffix in the file name

Code: [Select]
        //      get the real block name.        //
        static public string EffectiveName(Transaction tr, BlockReference bref)
        {
            BlockTableRecord btr = null;

            if ((bref.IsDynamicBlock) | (bref.Name.StartsWith("*U", StringComparison.InvariantCultureIgnoreCase)))
            {
                btr = tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
            }
            else
            {
                btr = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
            }
            return btr.Name;
        }

        [CommandMethod("xmlblocks", "xm", CommandFlags.Modal)]
        public void ComparePositions()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            List<object[]> blockdata = new List<object[]>();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptSelectionResult psr;
                SelectionFilter sf = new SelectionFilter(
                    new TypedValue[] { new TypedValue(0, "insert") });
             
                    psr = ed.SelectAll(sf);
                    foreach (SelectedObject sobj in psr.Value)
                    {
                        Entity ent = (Entity)tr.GetObject(sobj.ObjectId, OpenMode.ForRead, false);
                        BlockReference bref = ent as BlockReference;
                        if (bref != null)
                        {
                            object[] line = new object[5];
                            line[0] = EffectiveName(tr, bref);
                            line[1] = bref.Handle.ToString();
                            Point3d pt =  bref.Position;
                            line[2] =  pt.X;
                            line[3] = pt.Y;
                            line[4] = pt.Z;
                            blockdata.Add(line);
                        }
                    }
                    string dt = DateTime.Now.Day.ToString() + "_" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.ToString();
                    XmlSerializer serializer = new XmlSerializer(blockdata.GetType());

                    using (MemoryStream stream = new MemoryStream())
                    using (StreamWriter encodingWriter = new StreamWriter("blockdata-" + dt +".xml", true, Encoding.GetEncoding("UTF-8")))
                    {
                        // serialization
                        serializer.Serialize(encodingWriter, blockdata);
                        stream.Seek(0, SeekOrigin.Begin);

                    }

                // write saved data in the command line
                FileStream fstream = new FileStream("blockdata-" + dt + ".xml", FileMode.Open);
                XmlReader xmlreader = new XmlTextReader(fstream);
                while (xmlreader.Read())
                {
                    ed.WriteMessage("\n{0}",xmlreader.Value);
                }
                tr.Commit();
            }
        }

~'J'~