Adding Lines/Circles to a Block

Adding Lines/Circles to a Block

Anonymous
Not applicable
2,356 Views
15 Replies
Message 1 of 16

Adding Lines/Circles to a Block

Anonymous
Not applicable

I am unsure how to add new lines/circles to an already created block. I was able to remove lines/circles but when I try to add them they do not show up.

0 Likes
2,357 Views
15 Replies
Replies (15)
Message 2 of 16

tboehler
Collaborator
Collaborator

One of many ways:

Select the block, r-click, choose "Block Editor"  make your edits.   Close Block Editor, Be sure to save the changes.

0 Likes
Message 3 of 16

miguelmachadoecosta
Advocate
Advocate

You must append the entity to the BlockTableRecord. Afterwards you should regen in order to update block references.

0 Likes
Message 4 of 16

JBerns
Advisor
Advisor

@Anonymous,

 

You are trying to add these lines/circles programmatically? I presume this since you are asking in the AutoCAD .NET forum, not the AutoCAD General Forum.

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 5 of 16

cwr-pae
Mentor
Mentor

Use block editor (either right click and chose edit block, or type BEDIT and pick your block from the list) and make changes, then use BCLOSE to close the editor and choose to save the changes.

You can use the in place block editor (as above but pick edit in place our type REFEDIT) to add things already drawn outside the block to the block, using REFSET to add or remove objects from the block. Close refedit with REFCLOSE and chose save. While the editors are active the be ribbon should display the options and commands available.

 

@JBerns good catch, I didn't notice which forum this was in.

0 Likes
Message 6 of 16

Anonymous
Not applicable

I am in the .NET forum so I assumed it was clear I was writing a plugin. I am trying to use c# (But seeing as there is no c# forum .NET instructions work)

0 Likes
Message 7 of 16

Anonymous
Not applicable

I am in the .NET forum so I assumed it was clear I was writing a plugin. I am trying to use c# (But seeing as there is no c# forum .NET instructions work) But thanks

0 Likes
Message 8 of 16

Anonymous
Not applicable

I have tried regen after appending the entity and it isnt working. Ill attach my code here:

 

 

   // Open a transaction REQUIRES FOR ANY CHANGES
            using (var transaction = Active.Database.TransactionManager.StartTransaction()) {
                var options = new PromptSelectionOptions {
                    MessageForAdding = "Select AT LEAST TWO blocks to mark as elevator"
                };

                var result = Active.Editor.GetSelection(options);
                if (result.Status != PromptStatus.OK) {
                    Active.Editor.WriteMessage("\nNo block reference selected");
                    return;
                }

                // Must select at least two blocks
                if (result.Value.Count >= 2) {
                    var set = result.Value;

                    // Iterates through set of objects
                    foreach (var id in set.GetObjectIds()) {
                        // Get Block
                        var blockRef = (BlockReference)transaction.GetObject(id, OpenMode.ForWrite);
                        var tableRecord = (BlockTableRecord)transaction.GetObject(blockRef.BlockTableRecord, OpenMode.ForWrite);

                        // Get Blocks attributes and searched for 'P_TYPE'
                        var attCol = blockRef.AttributeCollection;
                        foreach (ObjectId attId in attCol) {
                            var attRef = (AttributeReference)transaction.GetObject(attId, OpenMode.ForWrite);

                            // Set P_TYPE to 10 (Elevator)
                            if (attRef.Tag != "P_TYPE") continue;
                            attRef.TextString = "10";
                            Active.Editor.WriteMessage("\nBlock changed to type Elevator");
                        }
                        
                        // Searched for lines and circles and removes them
                        foreach (var lineId in tableRecord) {
                            var obj = transaction.GetObject(lineId, OpenMode.ForWrite);
                            if (obj is Line || obj is Circle){
                                obj.Erase();
                            }
                        }
                   
                       
                        // TODO add new drawing to block
                        // Circle
                        Circle acCirc = new Circle();
                        acCirc.Center = new Point3d(0, 0, 0);
                        acCirc.Radius = 2;
                        tableRecord.AppendEntity(acCirc);
                        
                        
                       Active.Editor.Regen();
                        
                        
                        
                        
                        
                    }
                } else {
                    Active.Editor.WriteMessage("\nNot enough blocks selected");
                }
                transaction.Commit(); // Commits Change
                    
                Active.Editor.Regen();

            }
0 Likes
Message 9 of 16

Anonymous
Not applicable

I am in .NET so I assumed it was clear I am programming. Thank you though.

0 Likes
Message 10 of 16

norman.yuan
Mentor
Mentor

Not sure what exactly you want to achieve. But here is what you need to do:

 

after you append the circle to the block definition (BlockTableRecord) here:

tableRecord.AppendEntity(acCirc);

you also NEED TO ADD the newly created Circle to the transaction, following the above line of code:

 

transaction.AddNewlyCreatedDBObject(acCirc, true);

 

Then you only call Editor.Regen() AFTER the transaction is committed.

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 11 of 16

Anonymous
Not applicable

Thanks! That did it!

0 Likes
Message 12 of 16

Anonymous
Not applicable

Hey. So that has done it but I only want it to edit the shape of the currently selected block, when I do this it changes the Insert Blocks shape as well as any others of that type.

 

How do I only effect the one selected?

0 Likes
Message 13 of 16

Ed__Jobe
Mentor
Mentor

What you're asking goes against the concept of blocks. You have one definition of a block and then you can insert it multiple times. How would you do what you want in the AutoCAD gui? You would have to copy the block to a new block definition and then insert the copy. You need to do the same thing with code.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 14 of 16

Anonymous
Not applicable

I apologize for being such a novice. I have never used AutoCAD before but have been tasked to make a plugin for it for my coworkers.

0 Likes
Message 15 of 16

Anonymous
Not applicable

In AutoCAD what would I need to do to use a blockReference as a template but add on or remove to/from the drawing without effecting the original template blockReference or creating a new one.

 

I just want to use the blockReference as a template of sorts.

0 Likes
Message 16 of 16

_gile
Consultant
Consultant

@Anonymous  a écrit :

I apologize for being such a novice. I have never used AutoCAD before but have been tasked to make a plugin for it for my coworkers.


In my opinion, you cannot claim to program AutoCAD if you do not master at least the basics of the software, especially in this case, the notions of block definition (BlockTableRecord) and block reference (BlockReference). You should at least read this section of the AutoCAD .NET documentation and the related topics.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub