Author Topic: Redefine a block Origin  (Read 1504 times)

0 Members and 1 Guest are viewing this topic.

Odoshi

  • Guest
Redefine a block Origin
« on: November 29, 2011, 09:03:30 AM »
Hi,

Is there a simple way to redefine a block definition's origin. I'd like to shift it over on the X axis and have all the references update. A snippet or psuedo-code would help greatly.

Thanks,

drizzt

  • Guest
Re: Redefine a block Origin
« Reply #1 on: November 29, 2011, 09:10:32 AM »
I think you can open the block file, move it to where you want it, save it. You can then re-insert the block. This may have undesirable results though, so I would test it.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Redefine a block Origin
« Reply #2 on: November 29, 2011, 09:56:35 AM »
This should get you there. But you are on your own for an xclipped blockref

Code: [Select]
        [CommandMethod("ChangeInsertionpoint")]
        public static void NewInsertionPoint()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            PromptEntityOptions peo = new PromptEntityOptions("Pick a blockreference to change the insertion point:");
            peo.SetRejectMessage("Must be a blockreference:");
            peo.AddAllowedClass (typeof(BlockReference),true);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;
            PromptPointResult ppo=ed.GetPoint("Pick the new insertion point:");
            if (ppo.Status != PromptStatus.OK) return;
            Matrix3d ucs=ed.CurrentUserCoordinateSystem;
            Point3d insPt = ppo.Value.TransformBy(ucs),newPt;
           

            using(Transaction tr = db.TransactionManager.StartTransaction())       
            {
                BlockReference br = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as BlockReference;
                ObjectId blockId=br.BlockTableRecord;
                BlockTableRecord btr = tr.GetObject(blockId, OpenMode.ForWrite) as BlockTableRecord;
                Matrix3d blockmatrix = br.BlockTransform;
                insPt= insPt.TransformBy(blockmatrix.Inverse());
                Vector3d v=btr.Origin-insPt;
                Matrix3d move=Matrix3d.Displacement(v);
               
                foreach (ObjectId id in btr)
                {
                    Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity;
                    ent.TransformBy(move);
                    ent.DowngradeOpen();
                }
                ObjectIdCollection ids = btr.GetBlockReferenceIds(false, true);
                foreach (ObjectId brefId in ids)
                {
                    BlockReference b = tr.GetObject(brefId, OpenMode.ForWrite) as BlockReference;
                    newPt = insPt.TransformBy(b.BlockTransform);
                    if (b.BlockTableRecord == blockId)
                    {   
                        b.TransformBy(Matrix3d.Displacement(newPt - b.Position));
                    }
                    else
                    {
                        BlockTableRecord nestedBref = tr.GetObject(b.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
                        foreach (ObjectId id in nestedBref)
                        {
                            if (id == brefId)
                            {
                                b.TransformBy(Matrix3d.Displacement(newPt - b.Position));
                                break;
                            }
                        }
                    }
                    AttributeCollection atts = b.AttributeCollection;
                    foreach (ObjectId id in atts)
                    {
                        AttributeReference at = tr.GetObject(id, OpenMode.ForWrite) as AttributeReference;
                        at.SetAttributeFromBlock(b.BlockTransform.Inverse());

                    }
                    //UpdateClip(db, b, v);
                }
              tr.Commit();       
            }
            ed.Regen();

        } // end NewInsertionPoint

Odoshi

  • Guest
Re: Redefine a block Origin
« Reply #3 on: November 29, 2011, 10:50:23 AM »
I should clarify. The block definition is in the Block Table, not an external drawing. I would like to modify that, if possible, and then have the references update. Like what the Block Editor does in AutoCAD.

Thanks,

Odoshi

  • Guest
Re: Redefine a block Origin
« Reply #4 on: November 29, 2011, 10:51:32 AM »
Thanks, Bryco. I think your example is close to what I am looking for.