Block creation and information extraction with vba

Block creation and information extraction with vba

StevGerrard
Contributor Contributor
358 Views
2 Replies
Message 1 of 3

Block creation and information extraction with vba

StevGerrard
Contributor
Contributor

Hi everyone,

I am a beginner in VBA on Autocad and I am having a bit of trouble finding the information I am looking for my project. Here's what I would like to do and I can't figure out how to do it. Starting from an Autocad drawing with all the polylines and other elements of different layers already created, I would like to be able to create blocks with hand-selected elements and name the block. I am sure that this is very simple, but I can't find any answer to it. Additionally, later on, I would like to be able to extract the block information to an Excel file. Thanks in advance for your valuable help!

0 Likes
359 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

Well, with VBA/AutoCAD COM API, creating a BLOCK DEFINITION (AcadBlock) from existing entities (in ModelSpace, let's assume) is not so easy: one cannot directly add an entity (AcadEntity, or any concrete one, such as AcadLine/AcadCircle...) to an AcadBlock.

 

It can only be done with AcadBlock.AddXxxxxx() methods, where you pass the required geometric information for the entity to be created. So, if you want to create the block definition based on existing entities, you need to collect the required geometric information from the entity: for line, you need to get its Start/EndPoint, for Polyline, you need to get its Vertex coordinates and bulges at each vertex... And then if the base/insertion point is different from 0,0, you need to transform the geometric data based on the actual base point of the block definition... It is certainly not a simple task for a VBA beginner.

 

Another approach, which is a bit if easier, is to use AcadDocument.WBlock() method to export a selected set of entities a to DWG file (a block drawing file), then insert it as AcadBlockReference (thus the AcadBlock is brought in the drawing). After inserting, you might want to change the AcadBlock's Origin property (the insertion/base point) if necessary.

 

With all said, I am not sure what is the purpose of you to firstly create a block definition based on existing entities (then you need to insert the block as block reference, right?) and then extract some kind of data from the block reference. Is it really necessary to do things that way? 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

StevGerrard
Contributor
Contributor

First of all, thank you for your reply. To be completely honest, I'm looking to automate the creation of blocks per room based on an architectural plan with all the elements already drawn. Additionally, I'd like to extract information from the blocks, which details each element within the block, including its length, area, etc., depending on the element. Lastly, I'd like to be able to click on the elements, interact with a dialog box that would allow me to change the color of the selected element. That's the overall idea of my project.

0 Likes