Author Topic: how can i redefine a block  (Read 663 times)

0 Members and 1 Guest are viewing this topic.

Offline ajayvarma

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
how can i redefine a block
« on: March 30, 2012, 12:48:21 PM »
PLZZZ SOLVE IT
« Last Edit: April 03, 2012, 05:37:00 AM by ajayvarma »

Offline Patriiick

  • Administrator
  • *****
  • Posts: 130
  • Karma: +1/-0
  • Gender: Male
    • prefered language: VB
    • Prog expertise: Good
    • View Profile
  • Twitter: @Patriiick
Re: hOW CAN I READ .INI FILE AND TO DISPLAY .INI FILE LIST IN CHECK BOX
« Reply #1 on: March 30, 2012, 02:49:09 PM »
Would you please write in lower case your message subject, and give more explanations on what you want? (PLZZZ SOLVE IT is a bit rude)  :reprobateur:

Offline frits432

  • Newbie
  • *
  • Posts: 15
  • Karma: +1/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: how can i redefine a block
« Reply #2 on: April 12, 2012, 03:57:32 PM »
What kind of changed do you want to make ?
On of the simplest change to a block is appending an entity to the ModelSpace Block, like so:

Code: [Select]
[CommandMethod("drawline")]
    static public void drawline()
    {
      Database db = HostApplicationServices.WorkingDatabase;
      using (Transaction tr = db.TransactionManager.StartTransaction())
     
        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
        Line ln = new Line(new Point3d(10, 10, 0), new Point3d(20, 20, 0));
        ms.AppendEntity(ln);
        tr.AddNewlyCreatedDBObject(ln, true);
        tr.Commit();
      }
    }
Frits

Offline ajayvarma

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
how can i redefine a block
« Reply #3 on: April 16, 2012, 05:22:02 AM »
 redefine mean replacing the existing blocks

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Re: how can i redefine a block
« Reply #4 on: April 16, 2012, 08:34:48 PM »
See if this is working for you
Code: [Select]
        [CommandMethod("BlockReplaceTest", "breplace", CommandFlags.Session | CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]

        public void TestBlockReplaceByName()
        {
            // objects initializing
            Document doc = acadApp.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            try
            {
                using (doc.LockDocument())
                {
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        PromptStringOptions psto =
                            new PromptStringOptions("\nEnter a replacement block name: ");

                        psto.AllowSpaces = true;

                        psto.DefaultValue = "MyBlock";//old block

                        PromptResult stres;

                        stres = ed.GetString(psto);

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

                        string oldblock = stres.StringResult;

                        ed.WriteMessage("\nText Entered\t{0}", oldblock);

                        psto = new PromptStringOptions("\nEnter a block name to be replaced: ");

                        psto.AllowSpaces = true;

                        psto.DefaultValue = "NewBlock";//new block

                        stres = ed.GetString(psto);

                        if (stres.Status != PromptStatus.OK)

                            return;

                        string newblock = stres.StringResult;

                        BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                        if (!bt.Has(newblock)) return;

                        ObjectId newblkId = bt[newblock];

                        acadApp.SetSystemVariable("nomutt", 0);

                        TypedValue[] tvs = { new TypedValue(0, "insert"), new TypedValue(2, oldblock) };

                        SelectionFilter filt = new SelectionFilter(tvs);

                        PromptSelectionOptions pso = new PromptSelectionOptions();

                        pso.MessageForRemoval = "You must select the blocks only";

                        pso.MessageForAdding = "\nSelect replacement blocks: ";

                        ed.SelectionAdded += new SelectionAddedEventHandler(ed_SelectionAdded);

                        PromptSelectionResult res = ed.GetSelection(pso, filt);

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

                        BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                        SelectionSet sset = res.Value;

                        foreach (SelectedObject obj in sset)
                        {
                            Entity ent = (Entity)obj.ObjectId.GetObject(OpenMode.ForRead) as Entity;

                            BlockReference oldblk = ent as BlockReference;

                            Point3d ip = oldblk.Position;

                            Scale3d scl = oldblk.ScaleFactors;

                            double rot = oldblk.Rotation;

                            BlockReference newblk = new BlockReference(ip, newblkId);

                            newblk.SetPropertiesFrom(ent);

                            newblk.Rotation = rot;

                            newblk.ScaleFactors = scl;

                            btr.AppendEntity(newblk);

                            tr.AddNewlyCreatedDBObject(newblk, true);

                            ApplyAttributes(db, tr, newblk);

                            oldblk.UpgradeOpen();

                            oldblk.Erase();

                            oldblk.Dispose();

                        }

                        tr.Commit();
                    }

                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                acadApp.SetSystemVariable("nomutt", 1);

                ed.SelectionAdded -= ed_SelectionAdded;

            }

        }


        void ed_SelectionAdded(object sender, SelectionAddedEventArgs e)
        {
            ((Editor)sender).WriteMessage("\n\t{0} blocks to selection added", e.AddedObjects.Count);
        }


        public void ApplyAttributes(Database db, Transaction tr, BlockReference bref)
        {
            BlockTableRecord btrec = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;

            if (btrec.HasAttributeDefinitions)
            {
                Autodesk.AutoCAD.DatabaseServices.AttributeCollection atcoll = bref.AttributeCollection;

                foreach (ObjectId subid in btrec)
                {
                    Entity ent = (Entity)subid.GetObject(OpenMode.ForRead);

                    AttributeDefinition attDef = ent as AttributeDefinition;

                    if (attDef != null)
                    {

                        AttributeReference attRef = new AttributeReference();

                        attRef.SetDatabaseDefaults();//optional

                        attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);

                        attRef.Position = attDef.Position.TransformBy(bref.BlockTransform);

                        attRef.Justify = attDef.Justify;
                        //must be added all other properties for right position, eg. alignment modes etc
                        attRef.Tag = attDef.Tag;

                        attRef.AdjustAlignment(db);

                        atcoll.AppendAttribute(attRef);

                        tr.AddNewlyCreatedDBObject(attRef, true);

                    }
                }

            }
        }