Aspose.Cad .NET C# Block Attributes Read/Write

Hi All,

I am working with the Aspose.CAD library and am attempting to export and import attribute data from dwg files. I am running into a few issues while doing so, these being:

  1. Reading Attribute values.
    While traversing the object collections it is hard to find the actual values of the attribute values for the block objects. I ended up find them but they were stored in the DefaultString property.
    It is also worth noting there is an Attributes collection within the block itself that is always empty.

  2. Writing to attribute values
    When I attempt to write to the same DefaultString property it seems to occassionally cause the finale to corrupt after it is saved. I have a suspicion that it is because the DefaultString is a property typically used for the actual attribute definition and might be causing some conflicts.
    Secondly, when the update is successful, the attribute locations move around the drawing. This is fixed by opening the drawing and running a synchronize on the attributes(ATTSYNC) but it would be nice if this didn’t happen.
    Thirdly, when a single attribute is updated, all other attributes are removed from the properties panel of the block. This too is fixed with an ATTSYNC.

Please note, I have also tried an array of different blocks and attributes to see if I can get it to work and have experience the same issues across all of them.
I am hoping you can clarify if this is user error or a bug in the library.
My code is below:

private List<Dictionary<string, string>> ReadDwgFile(string filePath)
{
    List<Dictionary<string, string>> allBlockAttributes = new List<Dictionary<string, string>>();

    try
    {
        using (CadImage image = (CadImage)Image.Load(filePath, new LoadOptions { UnloadOnDispose = true }))
        {
            // Loop through the spaces
            foreach (DictionaryEntry blockEntry in image.BlockEntities)
            {
                // convert the spaces to entities
                CadBlockEntity block = blockEntry.Value as CadBlockEntity;

                // Loop through the entities in the space
                foreach (var entity in block.Entities)
                {
                    // Check if the entity is an insert
                    if (entity is CadInsertObject insert)
                    {
                        Dictionary<string, string> attributes = new Dictionary<string, string>();

                        // Traverse the child objects to find attributes
                        foreach (var obj in insert.ChildObjects)
                        {
                            var attprops = insert.Attributes;
                            if (obj is CadAttrib attObj)
                            {
                                var name = attObj.DefinitionTagString;
                                var value = attObj.DefaultText;
                                attributes[name] = value; // Add to dictionary
                            }
                        }

                        allBlockAttributes.Add(attributes);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show($"Error reading DWG file: {ex.Message}");
    }

    return allBlockAttributes;
}

private void UpdateAttributeValues(string filePath, string attributeName, string newValue)
{
    try
    {
        using (CadImage image = (CadImage)Image.Load(filePath))
        {
            bool attributeUpdated = false;

            foreach (DictionaryEntry blockEntry in image.BlockEntities)
            {
                CadBlockEntity block = blockEntry.Value as CadBlockEntity;

                foreach (var entity in block.Entities)
                {
                    if (entity is CadInsertObject insert)
                    {
                        foreach (var obj in insert.ChildObjects)
                        {
                            if (obj is CadAttrib attObj)
                            {
                                if (attObj.DefinitionTagString == attributeName)
                                {
                                    attObj.DefaultText = newValue; // Update the attribute value
                                    attributeUpdated = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            if (attributeUpdated)
            {
                //image.Save(filePath); // Save the updated DWG file
                image.Save();
                
                MessageBox.Show("Attribute values updated successfully.");
            }
            else
            {
                MessageBox.Show("Attribute not found.");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show($"Error updating DWG file: {ex.Message}");
    }
}

@LSElite,
Hello,
storing the attributes in files are not trivial. Could you please share the initial DWG file and specify (e.g., on the screenshot) which attributes you are going to update.

SimpleTests.zip (57.5 KB)

Hi Oleksii,

Please find attached the 2 simplified tests I have been trying.
So far the bugs have occurred with all attributes that I have attempted to interact with so you can pick any of them to try out.

test2.dwg is a block that I have made from scratch. both the block and the attributes have been made from scratch in a default autocad template with no extra property changes or anything. it is as basic of a test as I could possibly make.

test.dwg is a small section of another block that I have extracted to simplify the intended problem space.

Cheers,

Matt Mortimer

@LSElite,
Hi.
We can confirm issues with writing attributes into the DWG unfortunately, we will fix them in the scope of CADNET-9830.