Skip to content

Proposed fixes for #672 #673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/ACadSharp/Entities/Dimension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using ACadSharp.Attributes;
using ACadSharp.Tables;
using ACadSharp.Tables.Collections;

using CSMath;
using CSUtilities.Extensions;
using System;
Expand Down Expand Up @@ -30,9 +32,9 @@ public abstract class Dimension : Entity
public BlockRecord Block
{
get { return this._block; }
set
{
this._block = this.updateTable(this._block, this.Document?.BlockRecords);
set {
this._block = value;
this.addToBlockRecordsTable(_block);
}
}

Expand Down Expand Up @@ -240,6 +242,7 @@ public override CadObject Clone()
Dimension clone = (Dimension)base.Clone();

clone.Style = (DimensionStyle)(this.Style.Clone());
clone.Block = (BlockRecord)this.Block?.Clone();

return clone;
}
Expand All @@ -249,7 +252,7 @@ internal override void AssignDocument(CadDocument doc)
base.AssignDocument(doc);

this._style = this.updateTable(this.Style, doc.DimensionStyles);
this._block = this.updateTable(this.Block, doc.BlockRecords);
this.addToBlockRecordsTable(this._block);

doc.DimensionStyles.OnRemove += this.tableOnRemove;
doc.BlockRecords.OnRemove += this.tableOnRemove;
Expand Down Expand Up @@ -280,5 +283,35 @@ protected override void tableOnRemove(object sender, CollectionChangedEventArgs
this._block = null;
}
}


private void addToBlockRecordsTable(BlockRecord block)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's a good practice to make changes to an object that is not managed by the library itself, I would suggest to keep the old method updateTable and add a guard that check that the block has the flag anonymous set.

If the naming of the block is an issue for any software, we can add a method that specifically changes the block name to a unique one for the current dimension, or a method in the CadDocument that updates the name for all anonymous blocks.

Copy link
Contributor Author

@mme1950 mme1950 May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the uniqueness of the name is essential for ACadSharp.
updateTable tries to find a BlockRecord with the same name and returns the existing one so that the new one is to be discarded. We have to ensure that the new one is kept - with a new name.

Example:
I cloned Dimension objects from various source DWGs to a new document. They accidenially had the same "*D..." names. Effectively I had more than one Dimension object that were completely different referencing the same BlockRecord.

{
if (this.Document == null) {
return;
}
if (block == null)
{
return;
}

if (block.Document != null)
{
return;
}
BlockRecordsTable blockRecords = this.Document.BlockRecords;
int num = 1;
foreach (BlockRecord blockRecord in blockRecords)
{
string name = blockRecord.Name;
if (name.StartsWith("*D"))
{
num = Convert.ToInt32(name.Substring(2)) + 1;
}
}

block.Name = $"*D{num}";
blockRecords.Add(block);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,7 @@ private void writeCommonDimensionData(Dimension dimension)
//H 3 DIMSTYLE(hard pointer)
this._writer.HandleReference(DwgReferenceType.HardPointer, dimension.Style);
//H 2 anonymous BLOCK(hard pointer)
//TODO: fix annotative dimensions
//this._writer.HandleReference(DwgReferenceType.HardPointer, dimension.Block);
this._writer.HandleReference(DwgReferenceType.HardPointer, null);
this._writer.HandleReference(DwgReferenceType.HardPointer, dimension.Block);
}

private void writeDimensionLinear(DimensionLinear dimension)
Expand Down