Collapse |
Copy Code
public void ReadFromFile (string textFile)
{
string line1, line2;
line1 = "0"; line2 = "0";
long position = 0;
theSourceFile = new FileInfo (textFile); StreamReader reader = null;
try
{
reader = theSourceFile.OpenText();
}
catch (FileNotFoundException e)
{
MessageBox.Show(e.FileName.ToString() + " cannot be found");
}
catch
{
MessageBox.Show("An error occured while opening the DXF file");
return;
}
do
{
if (line1 == "0" && line2 == "LINE")
LineModule(reader);
else if (line1 == "0" && line2 == "LWPOLYLINE")
PolylineModule(reader);
else if (line1 == "0" && line2 == "CIRCLE")
CircleModule(reader);
else if (line1 == "0" && line2 == "ARC")
ArcModule(reader);
GetLineCouple (reader, out line1, out line2);
}
while (line2 != "EOF");
reader.DiscardBufferedData(); theSourceFile = null;
reader.Close(); }
Let me explain a little
bit about the structure
of a
DXF file.
In
DXF files
(they can be opened in
Notepad) the data is
stored in two-line
(bi-line, dual,
coupling..what ever you
call it) structures. The
first line contains a
kind of an identifier
(or a Tag) and the
second line contains the
data value. So in order
to interpret a
dxf file we
must interperet lines
two-by-two.
Depending on this dual
structure we can
interpret all entities
that reside in a
DXF file.
But currently only LINE,
LWPOLYLINE, CIRCLE and
ARC entities can be
recognizable. For near
future I'm planning to
add BSPLINE.
I provided plenty of
comments in the
source code,
so please look in the
source code
itself for
explanation of the
modules.
DXF Viewer
Viewer part contains
much more coding than
the reader. As I
mentioned above
currently I can draw a
limited number of
shapes. I hope to expand
it in near future.
While I'm developing the
Viewer I got lot's of
ideas and knowledge from
the ucancode. It
varies from double
buffering techniques to
making a
GradientBackground.
In the beginning I said
that the viewer is a MDI
application. In this way
you can open multiple
DXF files
to view. After opening a
file, if you click on
the "Window" button in
the main menu you can
see a list of the opened
windows.
To make this add a menu
item to the main menu,
and then in its
properties find MdiList
and change its value to
"true".
I made the gradient
background with
overriding
OnPaintBackground event:
Collapse |
Copy Code
protected override void
OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaintBackground(e);
...
Graphics g = e.Graphics;
Rectangle rect = new Rectangle(this.pictureBox1.Location,
this.pictureBox1.Size);
System.Drawing.Drawing2D.LinearGradientBrush brush =
new System.Drawing.Drawing2D.LinearGradientBrush(
rect, Color.SteelBlue, Color.Black,
System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal);
if (this.WindowState != FormWindowState.Minimized)
{
e.Graphics.FillRectangle(brush, rect);
Draw(g); }
brush.Dispose();
}
Conclusion
The main purpose of this
project is to
demonstrate that the
dxf files
can be read with the
above mentioned
techniques. In fact, the
viewer part has a
secondary priority at
the beginning. Later on
I paid some attention to
it, but I cannot claim
that the viewer is
composed of the best
practices.
The compressed file for
the demo application the
executable file and two
sample
dxf files.
One of the dxf files is
the one that can be seen
in the screenshot. The
drawing seems to be 3-D
but it is not. it is
drawn in
2D...