Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
[2020/11/14]
Browse files Browse the repository at this point in the history
* Now it is possible to change the ImageDefinition of an Image entity.
* Now it is possible to change the UnderlayDefinition of an Underlay entity.
* Now it is possible to change the ShapeStyle and Name of a Shape entity.
	You will need to make sure that the actual shape style file has a shape with the actual name.
	There are a few methods to help you with that in the ShapeStyle class but they only work with the SHP shape files.
* (fixed) Error creating the Underlay definition dictionaries when multiple entries of the same type PDF, DWF, or DGN where present.
  • Loading branch information
haplokuon committed Nov 14, 2020
1 parent 97d5bd7 commit 1a35445
Show file tree
Hide file tree
Showing 19 changed files with 1,160 additions and 319 deletions.
7 changes: 7 additions & 0 deletions doc/Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Change history

### [2020/11/14]
* Now it is possible to change the ImageDefinition of an Image entity.
* Now it is possible to change the UnderlayDefinition of an Underlay entity.
* Now it is possible to change the ShapeStyle and Name of a Shape entity.
You will need to make sure that the actual shape style file has a shape with the actual name.
There are a few methods to help you with that in the ShapeStyle class but they only work with the SHP shape files.
* (fixed) Error creating the Underlay definition dictionaries when multiple entries of the same type PDF, DWF, or DGN where present.

### [2020/11/09]
* Added some workarounds for invalid values in buggy DXF files.
Expand Down
8 changes: 8 additions & 0 deletions netDxf/Collections/AttributeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public AttributeCollection()
public AttributeCollection(IEnumerable<Attribute> attributes)
{
if (attributes == null)
{
throw new ArgumentNullException(nameof(attributes));
}
this.innerArray = new List<Attribute>(attributes);
}

Expand Down Expand Up @@ -132,12 +134,18 @@ public int IndexOf(Attribute item)
public Attribute AttributeWithTag(string tag)
{
if (string.IsNullOrEmpty(tag))
{
return null;
}
foreach (Attribute att in this.innerArray)
{
if (att.Definition != null)
{
if (string.Equals(tag, att.Tag, StringComparison.OrdinalIgnoreCase))
{
return att;
}
}
}

return null;
Expand Down
24 changes: 24 additions & 0 deletions netDxf/Collections/BlockRecords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ internal BlockRecords(DxfDocument document, string handle)
internal override Block Add(Block block, bool assignHandle)
{
if (block == null)
{
throw new ArgumentNullException(nameof(block));
}

Block add;
if (this.list.TryGetValue(block.Name, out add))
{
return add;
}

if (assignHandle || string.IsNullOrEmpty(block.Handle))
{
this.Owner.NumHandles = block.AssignHandle(this.Owner.NumHandles);
}

this.list.Add(block.Name, block);
this.references.Add(block.Name, new List<DxfObject>());
Expand All @@ -79,11 +85,15 @@ internal override Block Add(Block block, bool assignHandle)

//for new block definitions configure its entities
foreach (EntityObject entity in block.Entities)
{
this.Owner.AddEntityToDocument(entity, block, assignHandle);
}

//for new block definitions configure its attributes
foreach (AttributeDefinition attDef in block.AttributeDefinitions.Values)
{
this.Owner.AddAttributeDefinitionToDocument(attDef, assignHandle);
}

block.Record.Owner = this;

Expand Down Expand Up @@ -120,27 +130,39 @@ public override bool Remove(string name)
public override bool Remove(Block item)
{
if (item == null)
{
return false;
}

if (!this.Contains(item))
{
return false;
}

if (item.IsReserved)
{
return false;
}

if (this.references[item.Name].Count != 0)
{
return false;
}

// remove the block from the associated layer
this.Owner.Layers.References[item.Layer.Name].Remove(item);

// we will remove all entities from the block definition
foreach (EntityObject entity in item.Entities)
{
this.Owner.RemoveEntityFromDocument(entity);
}

// remove all attribute definitions from the associated layers
foreach (AttributeDefinition attDef in item.AttributeDefinitions.Values)
{
this.Owner.RemoveAttributeDefinitionFromDocument(attDef);
}

this.Owner.AddedObjects.Remove(item.Handle);
this.references.Remove(item.Name);
Expand Down Expand Up @@ -169,7 +191,9 @@ public override bool Remove(Block item)
private void Item_NameChanged(TableObject sender, TableObjectChangedEventArgs<string> e)
{
if (this.Contains(e.NewValue))
{
throw new ArgumentException("There is already another block with the same name.");
}

this.list.Remove(sender.Name);
this.list.Add(e.NewValue, (Block) sender);
Expand Down
108 changes: 103 additions & 5 deletions netDxf/DxfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,11 +1113,15 @@ internal void AddEntityToDocument(EntityObject entity, Block block, bool assignH
{
// null entities are not allowed
if (entity == null)
{
throw new ArgumentNullException(nameof(entity));
}

// assign a handle
if (assignHandle || string.IsNullOrEmpty(entity.Handle))
{
this.NumHandles = entity.AssignHandle(this.NumHandles);
}

// the entities that are part of a block do not belong to any of the entities lists but to the block definition.
switch (entity.Type)
Expand Down Expand Up @@ -1198,18 +1202,17 @@ internal void AddEntityToDocument(EntityObject entity, Block block, bool assignH
}
insert.AttributeAdded += this.Insert_AttributeAdded;
insert.AttributeRemoved += this.Insert_AttributeRemoved;
//insert.BlockChanged += this.Insert_BlockChanged;
break;
case EntityType.LwPolyline:
break;
case EntityType.Line:
break;
case EntityType.Shape:
Shape shape = (Shape)entity;
Shape shape = (Shape) entity;
shape.Style = this.shapeStyles.Add(shape.Style, assignHandle);
this.shapeStyles.References[shape.Style.Name].Add(shape);
//check if the shape style contains a shape with the stored name
if(!shape.Style.ContainsShapeName(shape.Name))
throw new ArgumentException("The shape style does not contain a shape with the stored name.", nameof(entity));
shape.StyleChanged += this.Shape_StyleChanged;
break;
case EntityType.Point:
break;
Expand Down Expand Up @@ -1239,12 +1242,14 @@ internal void AddEntityToDocument(EntityObject entity, Block block, bool assignH
Image image = (Image) entity;
image.Definition = this.imageDefs.Add(image.Definition, assignHandle);
this.imageDefs.References[image.Definition.Name].Add(image);

if (!image.Definition.Reactors.ContainsKey(image.Handle))
{
ImageDefinitionReactor reactor = new ImageDefinitionReactor(image.Handle);
this.NumHandles = reactor.AssignHandle(this.NumHandles);
image.Definition.Reactors.Add(image.Handle, reactor);
}
image.ImageDefinitionChanged += this.Image_ImageDefinitionChanged;
break;
case EntityType.MLine:
MLine mline = (MLine) entity;
Expand Down Expand Up @@ -1273,6 +1278,7 @@ internal void AddEntityToDocument(EntityObject entity, Block block, bool assignH
this.underlayPdfDefs.References[underlay.Definition.Name].Add(underlay);
break;
}
underlay.UnderlayDefinitionChanged += this.Underlay_UnderlayDefinitionChanged;
break;
case EntityType.Wipeout:
break;
Expand Down Expand Up @@ -1305,11 +1311,15 @@ internal void AddAttributeDefinitionToDocument(AttributeDefinition attDef, bool
{
// null entities are not allowed
if (attDef == null)
{
throw new ArgumentNullException(nameof(attDef));
}

// assign a handle
if (assignHandle || string.IsNullOrEmpty(attDef.Handle))
{
this.NumHandles = attDef.AssignHandle(this.NumHandles);
}

attDef.Style = this.textStyles.Add(attDef.Style, assignHandle);
this.textStyles.References[attDef.Style.Name].Add(attDef);
Expand Down Expand Up @@ -1395,6 +1405,7 @@ internal bool RemoveEntityFromDocument(EntityObject entity)
}
insert.AttributeAdded -= this.Insert_AttributeAdded;
insert.AttributeRemoved -= this.Insert_AttributeRemoved;
//insert.BlockChanged -= this.Insert_BlockChanged;
break;
case EntityType.LwPolyline:
break;
Expand All @@ -1403,6 +1414,7 @@ internal bool RemoveEntityFromDocument(EntityObject entity)
case EntityType.Shape:
Shape shape = (Shape)entity;
this.shapeStyles.References[shape.Style.Name].Remove(entity);
shape.StyleChanged -= this.Shape_StyleChanged;
break;
case EntityType.Point:
break;
Expand Down Expand Up @@ -1430,6 +1442,7 @@ internal bool RemoveEntityFromDocument(EntityObject entity)
Image image = (Image) entity;
this.imageDefs.References[image.Definition.Name].Remove(image);
image.Definition.Reactors.Remove(image.Handle);
image.ImageDefinitionChanged -= this.Image_ImageDefinitionChanged;
break;
case EntityType.MLine:
MLine mline = (MLine) entity;
Expand All @@ -1440,6 +1453,24 @@ internal bool RemoveEntityFromDocument(EntityObject entity)
break;
case EntityType.XLine:
break;
case EntityType.Underlay:
Underlay underlay = (Underlay) entity;
switch (underlay.Definition.Type)
{
case UnderlayType.DGN:
this.underlayDgnDefs.References[underlay.Definition.Name].Remove(underlay);
break;
case UnderlayType.DWF:
this.underlayDwfDefs.References[underlay.Definition.Name].Remove(underlay);
break;
case UnderlayType.PDF:
this.underlayPdfDefs.References[underlay.Definition.Name].Remove(underlay);
break;
}
underlay.UnderlayDefinitionChanged -= this.Underlay_UnderlayDefinitionChanged;
break;
case EntityType.Wipeout:
break;
case EntityType.Viewport:
Viewport viewport = (Viewport) entity;
// delete the viewport boundary entity in case there is one
Expand Down Expand Up @@ -1789,7 +1820,10 @@ private void Leader_DimStyleOverrideAdded(Leader sender, DimensionStyleOverrideC
case DimensionStyleOverrideType.DimArrow2:
Block block = (Block) e.Item.Value;
if (block == null)
return; // the block might be defined as null to indicate that the default arrowhead will be used
{
// the block might be defined as null to indicate that the default arrowhead will be used
return;
}
sender.StyleOverrides[e.Item.Type] = new DimensionStyleOverride(e.Item.Type, this.blocks.Add(block));
this.blocks.References[block.Name].Add(sender);
break;
Expand Down Expand Up @@ -1910,6 +1944,14 @@ private void Insert_AttributeRemoved(Insert sender, AttributeChangeEventArgs e)
e.Item.TextStyleChanged += this.Entity_TextStyleChanged;
}

//private void Insert_BlockChanged(Insert sender, TableObjectChangedEventArgs<Block> e)
//{
// this.blocks.References[e.OldValue.Name].Remove(sender);

// e.NewValue = this.blocks.Add(e.NewValue);
// this.blocks.References[e.NewValue.Name].Add(sender);
//}

private void Hatch_BoundaryPathAdded(Hatch sender, ObservableCollectionEventArgs<HatchBoundaryPath> e)
{
Layout layout = sender.Owner.Record.Layout;
Expand Down Expand Up @@ -1962,6 +2004,62 @@ private void Viewport_ClippingBoundaryRemoved(Viewport sender, EntityChangeEvent
this.RemoveEntity(e.Item);
}

private void Image_ImageDefinitionChanged(Image sender, TableObjectChangedEventArgs<ImageDefinition> e)
{
this.imageDefs.References[e.OldValue.Name].Remove(sender);

e.NewValue = this.imageDefs.Add(e.NewValue);
this.imageDefs.References[e.NewValue.Name].Add(sender);

if (!e.NewValue.Reactors.ContainsKey(sender.Handle))
{
ImageDefinitionReactor reactor = new ImageDefinitionReactor(sender.Handle);
this.NumHandles = reactor.AssignHandle(this.NumHandles);
e.NewValue.Reactors.Add(sender.Handle, reactor);
}
}

private void Underlay_UnderlayDefinitionChanged(Underlay sender, TableObjectChangedEventArgs<UnderlayDefinition> e)
{
switch (e.OldValue.Type)
{
case UnderlayType.DGN:
this.underlayDgnDefs.References[e.OldValue.Name].Remove(sender);
break;
case UnderlayType.DWF:
this.underlayDwfDefs.References[e.OldValue.Name].Remove(sender);
break;
case UnderlayType.PDF:
this.underlayPdfDefs.References[e.OldValue.Name].Remove(sender);
break;
}


switch (e.NewValue.Type)
{
case UnderlayType.DGN:
e.NewValue = this.underlayDgnDefs.Add((UnderlayDgnDefinition) e.NewValue);
this.underlayDgnDefs.References[e.NewValue.Name].Add(sender);
break;
case UnderlayType.DWF:
e.NewValue = this.underlayDwfDefs.Add((UnderlayDwfDefinition) e.NewValue);
this.underlayDwfDefs.References[e.NewValue.Name].Add(sender);
break;
case UnderlayType.PDF:
e.NewValue = this.underlayPdfDefs.Add((UnderlayPdfDefinition) e.NewValue);
this.underlayPdfDefs.References[e.NewValue.Name].Add(sender);
break;
}
}

private void Shape_StyleChanged(Shape sender, TableObjectChangedEventArgs<ShapeStyle> e)
{
this.shapeStyles.References[e.OldValue.Name].Remove(sender);

e.NewValue = this.shapeStyles.Add(e.NewValue);
this.shapeStyles.References[e.NewValue.Name].Add(sender);
}

#endregion

#region DxfObject events
Expand Down
Loading

0 comments on commit 1a35445

Please sign in to comment.