Community
AutoCAD Forum
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Adding Unique IDs in and Searching for Blocks in DWG Files

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
vyshakhgnair_cvr
910 Views, 10 Replies

Adding Unique IDs in and Searching for Blocks in DWG Files

Hello everyone,

I am seeking assistance on how to add a unique ID to each block within a CAD file in DWG format using Autodesk software. Additionally, I would like to know if there is an API available to search for a specific unique ID within the DWG file and modify the color of the corresponding block.

I am specifically interested in understanding the steps or commands to accomplish these tasks within Autodesk. Any guidance or code examples you can provide would be greatly appreciated.

Thank you in advance for your support!

10 REPLIES 10
Message 2 of 11

It's not something you can assign, but the "handle" in any object's entity data is unique, and remains constant no matter what, unlike its "entity name" which may be different the next time you open the drawing.  Is that something you might be able to use?

 

The handle is a text string representing a hexadecimal number, so the string can include numerical characters as well as the letters A through F.  One way get it from an object is with this AutoLisp expression:

(cdr (assoc 5 (entget (car (entsel)))))

and select the object.

 

If that is saved to a variable:

(setq test (cdr (assoc 5 (entget (car (entsel))))))

then:

(handent test)

returns the entity name, so that would qualify as "search[ing] for a specific unique ID within the DWG file."  That can be used in object selection for command-based modifications you want to make, or its entity data can be manipulated in various ways, etc.

 

A routine could be written that would, for example, find all Block insertions, and write out their handles to a file, along with related information [Block name?  insertion point?  Layer?].  Then you could later pull the handle from that file to find that unique Block, to do with what you need.

 

If you're talking about something like using the same identifier for specific Blocks in different drawings [e.g. open up a bunch of drawings and change all the Blocks identified as "George" in some way], that couldn't be done directly with handles, because you can't assign those.  But in writing out to a file, the identifier could be associated with the handle in some way.  More detail about how you imagine keeping track of and making use of the identifiers would be helpful.

 

Also consider having an Attribute in your Block definitions, even an invisible one, that could hold your unique ID.  In that case you could assign ID's as you desire, including with the same assignment to Blocks in different drawings, or even to multiple Blocks in the same drawing.

Kent Cooper, AIA
Message 3 of 11

The unique identifier is the block name itself. Within the same dwg file, there cannot be blocks of the same name. But one block can be inserted into the drawing (create a block reference) as many times as you like. So if you need a unique block reference identifier, you can use Handle or add a mutable attribute (visible or not visible) to the block. Also, the attribute will help identify blocks in many files. But you will have to fill in the attributes with unique values yourself. Or you can create a program for this. Or use ready-made numbering programs (for example, this)


Plugins for AutoCAD
A>V>C>
AppStore | Facebook | Twitter | YouTube | Blog
Message 4 of 11

Dear Kent1Cooper,

Thank you for your detailed explanation and suggestions. I'm currently working on a web application that involves real-time data about a CAD model. The goal is to update the CAD model dynamically whenever a block is placed in its respective position as per the CAD model.

To achieve this, I plan to incorporate QR codes on each block. When a block is placed, the user will touch the respective block on the viewer, triggering a small window to open, allowing them to scan the QR code. The QR code contains two numbers that need to be mapped to the corresponding block. It would be helpful to insert a unique value(For customer reference) as an attribute for each block to facilitate this mapping process.

My challenge lies in determining the appropriate format and method for embedding this unique value as an attribute. Additionally, I need guidance on how to utilize an API to change the color of the specific block once the mapping is complete, indicating that it has been successfully placed.

I would greatly appreciate your insights on embedding the unique value as an attribute and any recommendations regarding the API integration for block coloring. I have already developed the viewer using the Autodesk viewer API.

Thank you for your assistance.

Message 5 of 11

Thank you for your response,

How can I insert the data manually into a block in AutoCAD? When I export the model where does that data get stored.? Is it stored with a dwg file or any extra file? I want to access that data using some program to link it with other data.
Message 6 of 11

A block's handle can be obtained and assigned to an attribute, with a prefix as part of the unique ID, using a lisp routine. The picture below shows the prefix"Z" added to the handle of the block ("Z" + handle "457") and since a handle is unique, the ID will be unique.  When the object needs to be found, the handle is easily obtained by eliminating the prefix "Z". The lisp routine sets the ID attribute as each block is placed. An ID attribute would be required for each block (type and instance) for it to work..

Washingtonn_0-1689700618873.png

 

I use the block for developing sloped pipe system elevations at various points along the pipe run.  The lisp also identifies the previous node's ID to allow slope calculations to be generated within Excel.  The data exported into  Excel

cal also include insertion point, color, layer, etc... for the block which can be modified and subsequently updated back into AutoCAD.

Message 7 of 11


@vyshakhgnair_cvr wrote:

....
My challenge lies in determining the appropriate format and method for embedding this unique value as an attribute. Additionally, I need guidance on how to utilize an API to change the color of the specific block once the mapping is complete, indicating that it has been successfully placed.
....


In AutoLisp terms, one easy way to assign a value to an Attribute in a specific Block reference is:

(setpropertyvalue TheBlockEntityName "TheAttributeTag" "TheValue")

 

If done immediately after placing the Block, TheBlockEntityName can be simply (entlast).  Or it can be done within the Inserting of the Block if that's done with an INSERT command -- read about the ATTREQ System Variable.

 

And a color can be assigned in several ways, for instance:

(command "_.chprop" TheBlockEntityName "" "_color" TheColor "")

or

(setpropertyvalue TheBlockEntityName "Color" TheColor)

[That assumes the elements in the Block that you want to show in that color are given ByBlock as a color override.]

Kent Cooper, AIA
Message 8 of 11

Thank you for the response.

In my case, I need to embed data that is unique and predefined to each block while designing the model so that can be done in AutoCAD but I don't know how to implement that part. I had tried using xData but when I export the dwg file. I'm not able to get that data when it is viewed in an Autodesk web viewer.
Message 9 of 11


@vyshakhgnair_cvr wrote:
... I need to embed data that is unique and predefined to each block .... to get that data when it is viewed in an Autodesk web viewer.

An Attribute still seems like the way to go, if you can count on the "unique" staying true to that -- it's quite valid to give an Attribute the same value in more than one Block insertion, so controlling uniqueness would need to be by other means.  Can a web viewer show you invisible Attributes, if that's the preferred way to assign the unique ID's?

 

That's where handles are appealing, because they will always be unique, but can a web viewer show you handles?

 

And does "predefined" mean the nature and content of the ID would be in that predefinition?  That would mean handles wouldn't work, since AutoCAD assigns those, and you would have no control over them.

Kent Cooper, AIA
Message 10 of 11

Provide an example or two of the information/data you are looking to add and where it comes from. If you were looking into xdata, the AutoCAD object would have needed to be defined.

Lisp routines can add such information as a "predefined" attribute to the block at the same time the block is added into the model providing that the block name and target attribute being updated are available to the routine. 

The block in my earlier post immediately updates the unique ID  attribute as the block is placed.

Message 11 of 11

I just added that unique values as the block name. Yeah handels and properties can be viewed on the web viewer.But now I'm trying to locate the values in that block by searching through the block names and then change its color.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta