Skip to content

Commit

Permalink
face 3d
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Jun 22, 2023
1 parent 7979ad2 commit 018df77
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 43 deletions.
5 changes: 5 additions & 0 deletions ACadSharp/DxfClassMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,10 @@ public void ClearCache()
{
_cache.Clear();
}

public override string ToString()
{
return $"DxfClassMap:{this.Name}";
}
}
}
6 changes: 0 additions & 6 deletions ACadSharp/DxfMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ public class DxfMap : DxfMapBase

public Dictionary<string, DxfClassMap> SubClasses { get; private set; } = new Dictionary<string, DxfClassMap>();

public static CadObject Build<T>()
where T : CadObject
{
throw new NotImplementedException();
}

/// <summary>
/// Creates a dxf map for an <see cref="Entity"/> or a <see cref="TableEntry"/>
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions ACadSharp/DxfMapBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ namespace ACadSharp
{
public abstract class DxfMapBase
{
/// <summary>
/// Name of the subclass map
/// </summary>
public string Name { get; set; }

/// <summary>
/// Properties linked to a dxf code
/// </summary>
public Dictionary<int, DxfProperty> DxfProperties { get; } = new Dictionary<int, DxfProperty>();

protected static void addClassProperties(DxfMapBase map, Type type)
Expand Down
14 changes: 14 additions & 0 deletions ACadSharp/DxfProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,19 @@ public DxfCode[] GetCollectionCodes()
{
return this._property.GetCustomAttribute<DxfCollectionCodeValueAttribute>()?.ValueCodes;
}

public override string ToString()
{
string str = string.Empty;

foreach (int code in this.DxfCodes)
{
str += $"{code}:";
}

str += this._property.Name;

return str;
}
}
}
3 changes: 3 additions & 0 deletions ACadSharp/Entities/AttributeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class AttributeDefinition : AttributeBase
/// <inheritdoc/>
public override string ObjectName => DxfFileToken.EntityAttributeDefinition;

/// <inheritdoc/>
public override string SubclassMarker => DxfSubclassMarker.AttributeDefinition;

/// <summary>
/// Prompt text for this attribute.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions ACadSharp/Entities/Circle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class Circle : Entity
/// <inheritdoc/>
public override string ObjectName => DxfFileToken.EntityCircle;

/// <inheritdoc/>
public override string SubclassMarker => DxfSubclassMarker.Circle;

/// <summary>
/// Specifies the three-dimensional normal unit vector for the object.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions ACadSharp/Entities/Ellipse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class Ellipse : Entity
/// <inheritdoc/>
public override string ObjectName => DxfFileToken.EntityEllipse;

/// <inheritdoc/>
public override string SubclassMarker => DxfSubclassMarker.Ellipse;

/// <summary>
/// Specifies the distance a 2D AutoCAD object is extruded above or below its elevation.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions ACadSharp/Entities/Face3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class Face3D : Entity
/// <inheritdoc/>
public override string ObjectName => DxfFileToken.Entity3DFace;

/// <inheritdoc/>
public override string SubclassMarker => DxfSubclassMarker.Face3d;

/// <summary>
/// First corner(in WCS)
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion ACadSharp/Entities/Insert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ public override CadObject Clone()

private void attributesOnAdd(object sender, CollectionChangedEventArgs e)
{
this.Block.Entities.Add(new AttributeDefinition(e.Item as AttributeEntity));
//TODO: Fix the relation between insert and block
this.Block?.Entities.Add(new AttributeDefinition(e.Item as AttributeEntity));
}
}
}
3 changes: 3 additions & 0 deletions ACadSharp/Entities/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class Line : Entity
/// <inheritdoc/>
public override string ObjectName => DxfFileToken.EntityLine;

/// <inheritdoc/>
public override string SubclassMarker => DxfSubclassMarker.Line;

/// <summary>
/// Specifies the distance a 2D AutoCAD object is extruded above or below its elevation.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions ACadSharp/Entities/TextEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ namespace ACadSharp.Entities
[DxfSubClass(DxfSubclassMarker.Text)]
public class TextEntity : Entity
{
/// <inheritdoc/>
public override ObjectType ObjectType => ObjectType.TEXT;

/// <inheritdoc/>
public override string ObjectName => DxfFileToken.EntityText;

/// <inheritdoc/>
public override string SubclassMarker => DxfSubclassMarker.Text;

/// <summary>
/// Specifies the distance a 2D AutoCAD object is extruded above or below its elevation.
/// </summary>
Expand Down
10 changes: 5 additions & 5 deletions ACadSharp/IO/DXF/DxfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,23 +383,23 @@ private IDxfStreamReader getReader()

tmpReader.Find(DxfFileToken.HeaderSection);

while (tmpReader.LastValueAsString != DxfFileToken.EndSection)
while (tmpReader.ValueAsString != DxfFileToken.EndSection)
{
if (tmpReader.LastValueAsString == "$ACADVER")
if (tmpReader.ValueAsString == "$ACADVER")
{
tmpReader.ReadNext();
var version = CadUtils.GetVersionFromName(tmpReader.LastValueAsString);
var version = CadUtils.GetVersionFromName(tmpReader.ValueAsString);
if (version >= ACadVersion.AC1021)
{
this._encoding = Encoding.UTF8;
break;
}
}
else if (tmpReader.LastValueAsString == "$DWGCODEPAGE")
else if (tmpReader.ValueAsString == "$DWGCODEPAGE")
{
tmpReader.ReadNext();

string encoding = tmpReader.LastValueAsString;
string encoding = tmpReader.ValueAsString;

CodePage code = CadUtils.GetCodePage(encoding.ToLower());
this._encoding = this.getListedEncoding((int)code);
Expand Down
141 changes: 117 additions & 24 deletions ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ACadSharp.IO.DXF
{
internal abstract class DxfSectionReaderBase
{
public delegate bool ReadEntityDelegate<T>(CadEntityTemplate<T> template, DxfClassMap map) where T : Entity, new();
public delegate bool ReadEntityDelegate<T>(CadEntityTemplate template, DxfMap map, string subclass = null) where T : Entity;

/// <summary>
/// Object reactors, list of handles
Expand Down Expand Up @@ -125,6 +125,10 @@ protected void readCommonCodes(CadTemplate template, out bool isExtendedData, Dx

switch (this._reader.Code)
{
//Handle
case 5:
template.CadObject.Handle = this._reader.ValueAsHandle;
break;
//Check with mapper
case 100:
if (map != null && !map.SubClasses.ContainsKey(this._reader.ValueAsString))
Expand All @@ -134,6 +138,10 @@ protected void readCommonCodes(CadTemplate template, out bool isExtendedData, Dx
case 102:
this.readDefinedGroups(template);
break;
//Soft - pointer ID / handle to owner BLOCK_RECORD object
case 330:
template.OwnerHandle = this._reader.ValueAsHandle;
break;
case 1001:
isExtendedData = true;
this.readExtendedData(template.EDataTemplateByAppName);
Expand All @@ -154,22 +162,21 @@ protected CadEntityTemplate readEntity()
template = new CadTextEntityTemplate(new AttributeEntity());
break;
case DxfFileToken.EntityAttributeDefinition:
template = new CadTextEntityTemplate(new AttributeDefinition());
break;
return this.readEntityCodes<AttributeDefinition>(new CadTextEntityTemplate(new AttributeDefinition()), readAttributeDefinition);
case DxfFileToken.EntityArc:
return this.readEntityCodes(new CadEntityTemplate<Arc>(), readArc);
return this.readEntityCodes<Arc>(new CadEntityTemplate<Arc>(), readArc);
case DxfFileToken.EntityCircle:
template = new CadEntityTemplate(new Circle());
break;
return this.readEntityCodes<Circle>(new CadEntityTemplate<Circle>(), readSubclassMap);
case DxfFileToken.EntityDimension:
template = new CadDimensionTemplate();
//return this.readEntityCodes<Dimension>(new CadDimensionTemplate(), readDimension);
break;
case DxfFileToken.Entity3DFace:
return this.readEntityCodes<Face3D>(new CadEntityTemplate<Face3D>(), readSubclassMap);
case DxfFileToken.EntityEllipse:
template = new CadEntityTemplate(new Ellipse());
break;
return this.readEntityCodes<Ellipse>(new CadEntityTemplate<Ellipse>(), readSubclassMap);
case DxfFileToken.EntityLine:
template = new CadEntityTemplate(new Line());
break;
return this.readEntityCodes<Line>(new CadEntityTemplate<Line>(), readSubclassMap);
case DxfFileToken.EntityLwPolyline:
template = new CadLwPolylineTemplate();
break;
Expand Down Expand Up @@ -207,8 +214,7 @@ protected CadEntityTemplate readEntity()
template = new CadVertexTemplate();
break;
case DxfFileToken.EntityViewport:
template = new CadViewportTemplate(new Viewport());
break;
return this.readEntityCodes<Viewport>(new CadViewportTemplate(), this.readViewport);
case DxfFileToken.EntityXline:
template = new CadEntityTemplate(new XLine());
break;
Expand Down Expand Up @@ -357,18 +363,16 @@ protected CadEntityTemplate readEntity()
return template;
}

protected CadEntityTemplate readEntityCodes<T>(CadEntityTemplate<T> template, ReadEntityDelegate<T> readEntity)
where T : Entity, new()
protected CadEntityTemplate readEntityCodes<T>(CadEntityTemplate template, ReadEntityDelegate<T> readEntity)
where T : Entity
{
this._reader.ReadNext();

DxfMap map = DxfMap.Create<T>();

Debug.Assert(template.CadObject.SubclassMarker != DxfSubclassMarker.Entity);

while (this._reader.DxfCode != DxfCode.Start)
{
if (!readEntity(template, map.SubClasses[template.CadObject.SubclassMarker]))
if (!readEntity(template, map))
{
this.readCommonEntityCodes(template, out bool isExtendedData, map);
if (isExtendedData)
Expand All @@ -384,23 +388,112 @@ protected CadEntityTemplate readEntityCodes<T>(CadEntityTemplate<T> template, Re

protected void readCommonEntityCodes(CadEntityTemplate template, out bool isExtendedData, DxfMap map = null)
{
DxfClassMap entityMap = map.SubClasses[DxfSubclassMarker.Entity];

isExtendedData = false;
if (!this.tryAssignCurrentValue(template.CadObject, entityMap))
switch (this._reader.Code)
{
case 6:
template.LineTypeName = this._reader.ValueAsString;
break;
case 8:
template.LayerName = this._reader.ValueAsString;
break;
case 347:
template.MaterialHandle = this._reader.ValueAsHandle;
break;
default:
if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Entity]))
{
this.readCommonCodes(template, out isExtendedData, map);
}
break;
}
}

private bool readArc(CadEntityTemplate template, DxfMap map, string subclass = null)
{
switch (this._reader.Code)
{
default:
if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Arc]))
{
return this.readSubclassMap(template, map, DxfSubclassMarker.Circle);
}
return true;
}
}

private bool readAttributeDefinition(CadEntityTemplate template, DxfMap map, string subclass = null)
{
DxfClassMap emap = map.SubClasses[template.CadObject.SubclassMarker];
CadTextEntityTemplate tmp = template as CadTextEntityTemplate;

switch (this._reader.Code)
{
this.readCommonCodes(template, out isExtendedData, map);
//TODO: Implement multiline attribute def codes
case 44:
case 46:
case 101:
return true;
default:
if (!this.tryAssignCurrentValue(template.CadObject, emap))
{
return this.readTextEntity(template, map, DxfSubclassMarker.Text);
}
return true;
}
}

private bool readTextEntity(CadEntityTemplate template, DxfMap map, string subclass = null)
{
string mapName = string.IsNullOrEmpty(subclass) ? template.CadObject.SubclassMarker : subclass;

switch (this._reader.Code)
{
default:
return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[mapName]);
}
}

private bool readDimension(CadEntityTemplate template, DxfMap map, string subclass = null)
{
DxfClassMap dimMap = map.SubClasses[DxfSubclassMarker.Dimension];

switch (this._reader.Code)
{
default:
return this.tryAssignCurrentValue(template.CadObject, dimMap);
}
}

private bool readViewport(CadEntityTemplate template, DxfMap map, string subclass = null)
{
CadViewportTemplate tmp = template as CadViewportTemplate;

switch (this._reader.Code)
{
//Undocumented
case 67:
case 68:
return true;
case 69:
tmp.ViewportId = this._reader.ValueAsShort;
return true;
case 348:
tmp.VisualStyleHandle = this._reader.ValueAsHandle;
return true;
default:
return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Viewport]);
}
}

private bool readArc(CadEntityTemplate<Arc> template, DxfClassMap map)
private bool readSubclassMap(CadEntityTemplate template, DxfMap map, string subclass = null)
{
Debug.Assert(map.Name == DxfSubclassMarker.Arc);
string mapName = string.IsNullOrEmpty(subclass) ? template.CadObject.SubclassMarker : subclass;

switch (this._reader.Code)
{
default:
return this.tryAssignCurrentValue(template.CadObject, map);
return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[mapName]);
}
}

Expand Down
Loading

0 comments on commit 018df77

Please sign in to comment.