Author Topic: Move all attributes to the new positions  (Read 838 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Move all attributes to the new positions
« on: August 05, 2012, 07:05:58 AM »
Code: [Select]
        // move attributes to the new positions
        // run command ATTSYNC manually to display result
        // right after the command is finished
        [CommandMethod("DAR")]

        static public void MoveAttributeReferences()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            // Select a block reference

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect a block reference:");

            peo.SetRejectMessage("\nMust be block reference...");

            peo.AddAllowedClass(typeof(BlockReference), true);

            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status != PromptStatus.OK)

                return;

            // Select attribute reference

            PromptNestedEntityOptions pno = new PromptNestedEntityOptions("\nSelect an attribute ");

            PromptNestedEntityResult nres = ed.GetNestedEntity(pno);

            if (nres.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nAttribute nentsel failed");

                return;
            }
            Point3d spickPt = nres.PickedPoint;

            ed.WriteMessage("\nPicked point is {0}", spickPt);

            ObjectId sselId = nres.ObjectId;

            List<ObjectId> enIds = new List<ObjectId>(nres.GetContainers());

            // Reverse the "containers" list

            // Now append the selected entity

            enIds.Add(sselId);

            enIds.Reverse();

            // Retrieve the sub-entity path for this entity

            SubentityId subent = new SubentityId(SubentityType.Null, 0);

            FullSubentityPath path = new FullSubentityPath(enIds.ToArray(), subent);

            // Open the outermost container, relying on the open

            // transaction...


            Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)
            {

                // This time we just open the block reference

                BlockReference br = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForRead);

                // If the block reference has attribute references...

                if (br.AttributeCollection.Count == 0) return;

                BlockTableRecord owner;

                owner = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
                if (br.IsDynamicBlock)
                {
                    owner = (BlockTableRecord)tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead);
                }
                owner.UpgradeOpen();


                Entity attent = tr.GetObject(enIds[0], OpenMode.ForRead, false) as Entity;

                AttributeReference attref = attent as AttributeReference;

                Point3d pt = attref.Position;

                PromptPointOptions ppo = new PromptPointOptions("\nSpecify the new attribute position: ");

                ppo.BasePoint=pt;

                ppo.UseBasePoint = true;

                ppo.UseDashedLine = true;

                PromptPointResult pres = ed.GetPoint(ppo);

                if (pres.Status != PromptStatus.OK) return;

                Point3d dp = pres.Value;

                Vector3d vec = pt.GetVectorTo(dp);

                Matrix3d mat = Matrix3d.Displacement(vec);

                foreach (ObjectId id in owner)
                {
                    if (id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AttributeDefinition))))
                    {
                        AttributeDefinition attdef = (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);

                        attdef.UpgradeOpen();
                         // move attribute
                        attdef.TransformBy(mat);

                        attdef.DowngradeOpen();

                        //attdef.Erase();// debug only, commented

                    }
                }
         
                owner.DowngradeOpen();

                tr.Commit();

            }

        }

~'J'~