Author Topic: How to modify the components of a BlockReference?  (Read 2929 times)

0 Members and 1 Guest are viewing this topic.

teslaxx

  • Guest
How to modify the components of a BlockReference?
« on: February 11, 2011, 07:06:46 AM »
1. Firstly, I want to apologize for the avalanches of the threads created by me. :)
2. Secondly, I want to ask you if you know where could I find Autocad .Net 2011 Developer Guide as a pdf. (this is not so important)
3. How can you modify the components of a BlockReference.
I have an XXX (a Civil Entity), which I explode it and I obtain a BlockReference.
I take the BlockReference and exploded it and I get the components (MTexts).
I modify the Contents attribute of the Mtext object, but I don't see any changes in the drawing.
So, any ideas? Maybe there is fundamental thing which I don't see.

Code: [Select]
[CommandMethod("app")]
        public static void app()
        {

            Document document = Application.DocumentManager.MdiActiveDocument;
            Database database = document.Database;
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
            String caleDirector = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + "NumeroteazaKilometrii\\";
            listAx = new List<Entity>();

            
            using (Transaction transaction = database.TransactionManager.StartTransaction())
            {
                Entity minor;

                DBObjectCollection dBObjectCollection = new DBObjectCollection();

                PromptSelectionResult promptSelectionResult = Helper.selectEntities("Minor", document, true);

                if (promptSelectionResult.Status == PromptStatus.OK)
                {
                    SelectionSet selectionSet = promptSelectionResult.Value;
                    minor = transaction.GetObject(selectionSet[0].ObjectId, OpenMode.ForWrite) as Entity;
                    if (minor == null) return;
                }
                else
                {
                    return;
                }


                
                if (minor is AlignmentMinorStationLabelGroup)
                {
                    minor.Explode(dBObjectCollection);

                    foreach (Entity elementAx in dBObjectCollection)
                    {
                        listAx.Add(elementAx);
                    }

                    Entity minor2 = listAx[0];
                    dBObjectCollection.Clear();
                    listAx.Clear();

                    BlockReference block = null;
                    if (minor2 is BlockReference)
                    {
                        block = minor2 as BlockReference;
                    }


                    if (block != null)
                    {
                        block.Explode(dBObjectCollection);
                        foreach (Entity e in dBObjectCollection)
                        {
                            if (e is MText)
                            {
                                MText mText = e as MText;
                                mText.Contents = "BlaBlaBla";
                            }
                        }
                    }
                }
                transaction.Commit();
            }
        }

n.yuan

  • Bull Frog
  • Posts: 348
Re: How to modify the components of a BlockReference?
« Reply #1 on: February 11, 2011, 10:43:40 AM »
Calling Entity.Explode() method appends the "exploded parts" of the entity (BlockReference, in your case) to the DBObjectCollection. The parts in the DBObjectCollection is not Database residing, nor the entity being exploded is erased (as opposed to "Explode" command in AutoCAD). It is up to your code to decide what to do with the generated "entity parts": you would eventually either dispose them, or add them to the database.

In your case, you did not add them into database, thus you cannot see any change in the drawing after running your code.

teslaxx

  • Guest
Re: How to modify the components of a BlockReference?
« Reply #2 on: February 14, 2011, 03:10:56 AM »
Ok, but how can I add those entity to my block? How can I delete some components from my block?

I can't seem to find some code to understand this thing. I found some code to insert new BlockReferences, but not to modify them.


I modified this line
MText mText = e as MText;
with
MText mText =  transaction.GetObject(e.ObjectId, OpenMode.ForRead); as MText;

But I receive "null ObjectId"

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to modify the components of a BlockReference?
« Reply #3 on: February 14, 2011, 09:15:17 PM »
Are you wanting to change the entities of a blockreference or the block definition itself?
To change the block definition you need the BlockTableRecord of the block.


You get a null ObjectId because it is not database resident.


teslaxx

  • Guest
Re: How to modify the components of a BlockReference?
« Reply #4 on: February 15, 2011, 02:41:49 AM »
I want to change the the entities of a blockreference (the components of the blockReference).

Quote
You get a null ObjectId because it is not database resident.

Seems logical :)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to modify the components of a BlockReference?
« Reply #5 on: February 15, 2011, 03:01:46 AM »
This is like using the explode in AutoCAD and changes the mtext value

Do you see how you take the blockreference then you must Erase it and add each object from the DBobjectCollection filled from explode?

If you want it to still be a block you would have to create a new block.
Code: [Select]
  [CommandMethod("ChangeBref")]
        public void ChangeBref()
        {         
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;

                BlockTableRecord btrMs = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite);

                PromptEntityResult per = ed.GetEntity("Select Block Reference");

                BlockReference bref = trx.GetObject(per.ObjectId, OpenMode.ForWrite) as BlockReference;

                DBObjectCollection brefEntites = new DBObjectCollection();

                bref.Explode(brefEntites);
                bref.Erase();
             
                foreach (Entity ent in brefEntites)
                {
                    MText mtxt = ent as MText;

                    if (mtxt != null)
                    {
                        mtxt.Contents = "Changed";
                    }
                    btrMs.AppendEntity(ent);
                    trx.AddNewlyCreatedDBObject(ent, true);

                }           

                trx.Commit();
            }
        }