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:
-
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. -
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}");
}
}