Skip to content

Commit

Permalink
viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Oct 14, 2023
1 parent d16424a commit de31d01
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 1 deletion.
11 changes: 11 additions & 0 deletions ACadSharp.Tests/IO/IOTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ public void EmptyDwgToDxf()
this.writeDxfFile(pathOut, doc, true);
}

[Theory]
[MemberData(nameof(DwgFilePaths))]
public void DwgToDwg(string test)
{
CadDocument doc = DwgReader.Read(test);

string file = Path.GetFileNameWithoutExtension(test);
string pathOut = Path.Combine(_samplesOutFolder, $"{file}_out.dwg");
this.writeDwgFile(pathOut, doc, true);
}

[Theory]
[MemberData(nameof(DwgFilePaths))]
public void DwgToDxf(string test)
Expand Down
2 changes: 1 addition & 1 deletion ACadSharp.Tests/IO/LocalSampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void ReadUserDwg(string test)

CadDocument doc = DwgReader.Read(test, this._dwgConfiguration, this.onNotification);

//return;
return;

string outPath = Path.Combine(Path.GetDirectoryName(test), $"{Path.GetFileNameWithoutExtension(test)}.out.dxf");
using (DxfWriter writer = new DxfWriter(outPath, doc, false))
Expand Down
155 changes: 155 additions & 0 deletions ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ private void writeEntity(Entity entity)
throw new NotImplementedException($"Vertex not implemented : {entity.GetType().FullName}");
}
break;
case Viewport viewport:
this.writeViewport(viewport);
break;
case XLine xline:
this.writeXLine(xline);
break;
Expand Down Expand Up @@ -1296,6 +1299,158 @@ private void writeVertex(Vertex vertex)
this._writer.Write3BitDouble(vertex.Location);
}

private void writeViewport(Viewport viewport)
{
//Center 3BD 10
this._writer.Write3BitDouble(viewport.Center);
//Width BD 40
this._writer.WriteBitDouble(viewport.Width);
//Height BD 41
this._writer.WriteBitDouble(viewport.Height);

//R2000 +:
if (this.R2000Plus)
{
//View Target 3BD 17
this._writer.Write3BitDouble(viewport.ViewTarget);
//View Direction 3BD 16
this._writer.Write3BitDouble(viewport.ViewDirection);
//View Twist Angle BD 51
this._writer.WriteBitDouble(viewport.TwistAngle);
//View Height BD 45
this._writer.WriteBitDouble(viewport.ViewHeight);
//Lens Length BD 42
this._writer.WriteBitDouble(viewport.LensLength);
//Front Clip Z BD 43
this._writer.WriteBitDouble(viewport.FrontClipPlane);
//Back Clip Z BD 44
this._writer.WriteBitDouble(viewport.BackClipPlane);
//Snap Angle BD 50
this._writer.WriteBitDouble(viewport.SnapAngle);
//View Center 2RD 12
this._writer.Write2RawDouble(viewport.ViewCenter);
//Snap Base 2RD 13
this._writer.Write2RawDouble(viewport.SnapBase);
//Snap Spacing 2RD 14
this._writer.Write2RawDouble(viewport.SnapSpacing);
//Grid Spacing 2RD 15
this._writer.Write2RawDouble(viewport.GridSpacing);
//Circle Zoom BS 72
this._writer.WriteBitShort(viewport.CircleZoomPercent);
}

//R2007 +:
if (this.R2007Plus)
{
//Grid Major BS 61
this._writer.WriteBitShort(viewport.MajorGridLineFrequency);
}

//R2000 +:
if (this.R2000Plus)
{
//Frozen Layer Count BL
this._writer.WriteBitLong(viewport.FrozenLayers.Count);
//Status Flags BL 90
this._writer.WriteBitLong((int)viewport.Status);
//Style Sheet TV 1
this._writer.WriteVariableText(string.Empty); //This is never used
//Render Mode RC 281
this._writer.WriteByte((byte)viewport.RenderMode);
//UCS at origin B 74
this._writer.WriteBit(viewport.DisplayUcsIcon);
//UCS per Viewport B 71
this._writer.WriteBit(viewport.UcsPerViewport);
//UCS Origin 3BD 110
this._writer.Write3BitDouble(viewport.UcsOrigin);
//UCS X Axis 3BD 111
this._writer.Write3BitDouble(viewport.UcsXAxis);
//UCS Y Axis 3BD 112
this._writer.Write3BitDouble(viewport.UcsYAxis);
//UCS Elevation BD 146
this._writer.WriteBitDouble(viewport.Elevation);
//UCS Ortho View Type BS 79
this._writer.WriteBitShort((short)viewport.UcsOrthographicType);
}

//R2004 +:
if (this.R2004Plus)
{
//ShadePlot Mode BS 170
this._writer.WriteBitShort((short)viewport.ShadePlotMode);
}

//R2007 +:
if (this.R2007Plus)
{
//Use def. lights B 292
this._writer.WriteBit(viewport.UseDefaultLighting);
//Def.lighting type RC 282
this._writer.WriteByte((byte)viewport.DefaultLightingType);
//Brightness BD 141
this._writer.WriteBitDouble(viewport.Brightness);
//Contrast BD 142
this._writer.WriteBitDouble(viewport.Constrast);
//Ambient light color CMC 63
this._writer.WriteCmColor(viewport.AmbientLightColor);
}

//R13 - R14 Only:
if (this.R13_14Only)
{
this._writer.HandleReference(DwgReferenceType.HardPointer, null);
}

//R2000 +:
if (this.R2000Plus)
{
foreach (var layer in viewport.FrozenLayers)
{
if (this.R2004Plus)
{
//H 341 Frozen Layer Handles(use count from above)
//(hard pointer until R2000, soft pointer from R2004 onwards)
this._writer.HandleReference(DwgReferenceType.SoftPointer, layer);
}
else
{
this._writer.HandleReference(DwgReferenceType.HardPointer, layer);
}
}

//H 340 Clip boundary handle(soft pointer)
this._writer.HandleReference(DwgReferenceType.HardPointer, viewport.Boundary);
}

//R2000:
if (this._version == ACadVersion.AC1015)
{
//H VIEWPORT ENT HEADER((hard pointer))
this._writer.HandleReference(DwgReferenceType.HardPointer, null);
}

//R2000 +:
if (this.R2000Plus)
{
//TODO: Implement viewport UCS
this._writer.HandleReference(DwgReferenceType.HardPointer, null);
this._writer.HandleReference(DwgReferenceType.HardPointer, null);
}

//R2007 +:
if (this.R2007Plus)
{
//H 332 Background(soft pointer)
this._writer.HandleReference(DwgReferenceType.SoftPointer, null);
//H 348 Visual Style(hard pointer)
this._writer.HandleReference(DwgReferenceType.HardPointer, null);
//H 333 Shadeplot ID(soft pointer)
this._writer.HandleReference(DwgReferenceType.SoftPointer, null);
//H 361 Sun(hard owner)
this._writer.HandleReference(DwgReferenceType.HardOwnership, null);
}
}

private void writeXLine(XLine xline)
{
//3 RD: a point on the construction line
Expand Down

0 comments on commit de31d01

Please sign in to comment.