Page 1 of 2
DXFExport .NET - how to use DC?
Posted: 29 Aug 2005, 07:13
by xander
In the following page:
http://www.cadsofttools.com/?PageName=DXFExportNET
you say that "...All you need here is to draw image using Windows GDI+ functions onto DXF Exporter’s DC...". Can you please give a small example of how to do this? How do I access the DC on the DXFExport object?
Looking at your sample code, it appears that you must create a MetaForm in order to do this, which in turn means you need to have a Form. I want to create DXF invisibly in code and export it to a file (actually a string to be precise) - does this mean I must create a Form invisibly before I create the MetaForm?
Thanks in advance for you assistance...
Posted: 31 Aug 2005, 00:42
by xander
Anybody out there...?
I suppose that what I really want to know is:
What is the quickest way to get from a GraphicsPath object to a DXF? And I want to do this in code only, the user shouldn't see anything.
Posted: 02 Sep 2005, 17:21
by support
Hi,
Everything that is connected to DC in GDI+ .NET is being implemented inside of Graphics object. For the created image this object is MetaForm.mfGraph.
It is not necessary to show created image in the window when creating EMF file or DXF through EMF. But the form's handle is necessary for creating Metafile.
Here goes an example of creating EMF without it's showing anywhere (but the window is necessary itself):
Code: Select all
if(dlgSave.ShowDialog() != DialogResult.OK) return;
if(dlgSave.FileName == "") return;
mf1 = new MetaForm(dlgSave.FileName, ActiveForm, 500, 500, EmfType.EmfPlusDual, false);
... //drawing somthing
mf1.mfGraph.Dispose();
mf1.Free();
Here goes an example of creating DXF file through the existed EMF file without its showing anywhere (the window's handle is necessary):
Code: Select all
if(dlgOpenEmf.ShowDialog() != DialogResult.OK) return;
if(dlgOpenEmf.FileName == "") return;
MetaForm.Splitting(dlgOpenEmf.FileName, Handle, false);
Here goes an example of creating DXF file directly without its showing anywhere (there is no window's handle necessary):
Code: Select all
if(dlgSaveDXF.ShowDialog() != DialogResult.OK) return;
if(dlgSaveDXF.FileName != "")
{
DXFExport vDXF = new DXFExport();
DXFData Data = new DXFData();
DXFLayer vLayer;
DXFPoint Pt = new DXFPoint();
//line types
vDXF.AddLType("_SOLID",new float[]{5, 0});
vDXF.AddLType("_DASH",new float[]{5,-2});
vDXF.AddLType("_DOT", new float[]{2,-2});
vDXF.AddLType("_DASHDOT", new float[]{5,-2,2,-2});
vDXF.AddLType("_DASHDOTDOT", new float[]{5,-2,2,-2,2,-2});
// {blocks}
vDXF.BeginBlock("1");
Data.point.X = 0;
Data.point.Y = -3;
Data.point1.X = 10;
Data.point1.Y = 0;
vDXF.AddRectangle(Data);
...
vDXF.SaveToFile(dlgSaveDXF.FileName);
Sergey.
please post questions to the forum or write to
support@cadsofttools.com
Posted: 05 Sep 2005, 05:44
by xander
Thanks for the reply. My first attempt at converting a GraphicsPath to DXF looked like the following -- except it doesn't work since the output from this method appears to be a binary EMF file(?). Is there anyway that I can avoid creating a file at all since I want to save the DXF to a database and do not want to go through the process of creating a temporary EMF file (even if it is a temporary file).
Code: Select all
string GraphicsPathToDxf(GraphicsPath aPath)
{
// create an invisible dummy form
Form form = new Form();
form.Visible = false;
form.Width = 500;
form.Height = 900;
// create the meta form to obtain Graphics object to draw on
MetaForm mf = new MetaForm(form, EmfType.EmfPlusDual, true);
mf.mfGraph.DrawPath(new Pen(Color.Black), aPath);
mf.mfGraph.Dispose();
// convert the meta form stream to a string
MemoryStream stream = mf.mfStream;
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
string dxf = reader.ReadToEnd();
return dxf;
}
The reason that I want this is that I have created my own GDI+ draw control that works with a scene graph. I can draw everything I want to this GDI+ draw control using the scene graph, but I need a DXF copy of it to store in the database.
In other words, how can I convert mf.mfStream (in the above code) into a DXF string without using a temporary file?
Posted: 06 Sep 2005, 18:01
by Evgeny
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by xander</i>
<br />Thanks for the reply. My first attempt at converting a GraphicsPath to DXF looked like the following -- except it doesn't work since the output from this method appears to be a binary EMF file(?). Is there anyway that I can avoid creating a file at all since I want to save the DXF to a database and do not want to go through the process of creating a temporary EMF file (even if it is a temporary file).
string GraphicsPathToDxf(GraphicsPath aPath)
{
// create an invisible dummy form
Form form = new Form();
form.Visible = false;
form.Width = 500;
form.Height = 900;
// create the meta form to obtain Graphics object to draw on
MetaForm mf = new MetaForm(form, EmfType.EmfPlusDual, true);
mf.mfGraph.DrawPath(new Pen(Color.Black), aPath);
mf.mfGraph.Dispose();
// convert the meta form stream to a string
MemoryStream stream = mf.mfStream;
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
string dxf = reader.ReadToEnd();
return dxf;
}
The reason that I want this is that I have created my own GDI+ draw control that works with a scene graph. I can draw everything I want to this GDI+ draw control using the scene graph, but I need a DXF copy of it to store in the database.
In other words, how can I convert mf.mfStream (in the above code) into a DXF string without using a temporary file?
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
// create an invisible dummy form
Form form = new Form();-
...
This temprorary window is not allowed here.
MetaForm mf = new MetaForm(form, EmfType.EmfPlusDual, true); -
Active window required, for instance main application window.
Posted: 07 Sep 2005, 01:31
by xander
Ok, I can replace the temporary invisible form with the ActiveForm, but how do I create the DXF string directly? You didn't really answer my question...
My question was: How can I convert mf.mfStream (in the above code) into a DXF string without using a temporary file? Or is that not possible?
Posted: 07 Sep 2005, 17:22
by Evgeny
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by xander</i>
<br />
My question was: How can I convert mf.mfStream (in the above code) into a DXF string without using a temporary file? Or is that not possible?
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
Current version requires a file on HDD in order to convert metafile to DXF file.
We will provide DXF Export .NET version which does not need a temporary file soon.
Posted: 12 Sep 2005, 03:03
by xander
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">We will provide DXF Export .NET version which does not need a temporary file soon.<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
Can you give me a time frame on this please?
Posted: 12 Sep 2005, 14:31
by Evgeny
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by xander</i>
<br /><blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">We will provide DXF Export .NET version which does not need a temporary file soon.<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
Can you give me a time frame on this please?
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
It's ready:
http://www.cadsofttools.com/download/DXFExportNET.zip
Posted: 14 Sep 2005, 01:58
by xander
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">
It's ready:
http://www.cadsofttools.com/download/DXFExportNET.zip
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">Guys, help me out here please! I downloaded the above file. What have you changed to allow me to get a DXF string from the DC without using a temporary file? The help file seems to be missing the MetaForm class altogether? I cannot find any information about this in the sample Form1.cs either? I'm totally lost as to what you've changed... Thanks in advance.
Posted: 14 Sep 2005, 14:37
by Evgeny
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by xander</i>
<br /><blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">
It's ready:
http://www.cadsofttools.com/download/DXFExportNET.zip
<blockquote id="quote"><font size="2" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">Guys, help me out here please! I downloaded the above file. What have you changed to allow me to get a DXF string from the DC without using a temporary file? The help file seems to be missing the MetaForm class altogether? I cannot find any information about this in the sample Form1.cs either? I'm totally lost as to what you've changed... Thanks in advance.
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
VB.NET demo updated today, please download.
http://www.cadsofttools.com/download/DXFExportNET.zip
VB.NET:
Code: Select all
mf1 = New MetaForm(ActiveForm, EmfType.EmfPlusDual, True)
... //draw
mf1.Splitting(Handle, dlgSaveDXF.FileName) //export as dxf
Posted: 15 Sep 2005, 01:29
by xander
Thanks for the reply, but it still does not work for me. As said above, I want to get the DXF string ***without using a file***. In your VB.NET sample above you save the DXF to a file. I do NOT want to save it to a file.
Please give me a translation of the following method that will return a DXF string WITHOUT using a file:
Code: Select all
string GraphicsPathToDxf(GraphicsPath aPath)
{
// create the meta form to obtain Graphics object to draw on
MetaForm mf = new MetaForm(ActiveForm, EmfType.EmfPlusDual, true);
mf.mfGraph.DrawPath(new Pen(Color.Black), aPath);
mf.mfGraph.Dispose();
// convert the meta form stream to a dxf string
MemoryStream stream = mf.mfStream;
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
string dxf = reader.ReadToEnd();
return dxf;
}
Posted: 16 Sep 2005, 11:32
by Evgeny
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by xander</i>
<br />Thanks for the reply, but it still does not work for me. As said above, I want to get the DXF string ***without using a file***. In your VB.NET sample above you save the DXF to a file. I do NOT want to save it to a file.
Please give me a translation of the following method that will return a DXF string WITHOUT using a file:
string GraphicsPathToDxf(GraphicsPath aPath)
{
// create the meta form to obtain Graphics object to draw on
MetaForm mf = new MetaForm(ActiveForm, EmfType.EmfPlusDual, true);
mf.mfGraph.DrawPath(new Pen(Color.Black), aPath);
mf.mfGraph.Dispose();
// convert the meta form stream to a dxf string
MemoryStream stream = mf.mfStream;
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
string dxf = reader.ReadToEnd();
return dxf;
}
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
Demo with DXF export to string will be today.
Posted: 16 Sep 2005, 14:27
by Evgeny
<blockquote id="quote"><font size="2" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by xander</i>
<br />Thanks for the reply, but it still does not work for me. As said above, I want to get the DXF string ***without using a file***. In your VB.NET sample above you save the DXF to a file. I do NOT want to save it to a file.
Please give me a translation of the following method that will return a DXF string WITHOUT using a file:
Code: Select all
string GraphicsPathToDxf(GraphicsPath aPath)
{
// create the meta form to obtain Graphics object to draw on
MetaForm mf = new MetaForm(ActiveForm, EmfType.EmfPlusDual, true);
mf.mfGraph.DrawPath(new Pen(Color.Black), aPath);
mf.mfGraph.Dispose();
// convert the meta form stream to a dxf string
MemoryStream stream = mf.mfStream;
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
string dxf = reader.ReadToEnd();
return dxf;
}
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
Export to DXF as string is ready.
http://www.cadsofttools.com/download/DXFExportNET.zip
additional methods and theirs usage:
VB.NET
'for save to Stream
Code: Select all
StringWriter st1 = new StringWriter()
mf1.SplitingToStream(Handle, st1)
'for save to Stream
Code: Select all
StringWriter st1 = new StringWriter()
vDXF.SaveToStream(st1)
C#:
//for save to Stream
Code: Select all
StringWriter st1 = new StringWriter();
mf1.SplitingToStream(Handle, st1);
//for save to Stream
Code: Select all
StringWriter st1 = new StringWriter();
vDXF.SaveToStream(st1);
Posted: 19 Sep 2005, 05:59
by xander
Thanks for the update, we have almost reached the point of success. Unfortunately there appears to be a problem with the MetaForm.mfGraph property that prevents you from drawing a GraphicsPath object. See the following two examples that illustrate the problem:
Code: Select all
public string Works()
{
Form form = new Form();
form.Width = 500;
form.Height = 500;
form.Show();
MetaForm mf = new MetaForm(form, EmfType.EmfPlusDual, true);
mf.mfGraph.DrawRectangle(new Pen(Color.Black, 1), new Rectangle(100, 400, 50, 50));
mf.mfGraph.Dispose();
StringWriter st1 = new StringWriter();
mf.SplitingToStream(form.Handle, st1);
form.Close();
return st1.ToString();
}
public string DoesNotWork()
{
Form form = new Form();
form.Width = 500;
form.Height = 500;
form.Show();
MetaForm mf = new MetaForm(form, EmfType.EmfPlusDual, true);
GraphicsPath path = new GraphicsPath();
path.AddRectangle(new Rectangle(100, 400, 50, 50));
mf.mfGraph.DrawPath(new Pen(Color.Black, 1), path);
mf.mfGraph.Dispose();
StringWriter st1 = new StringWriter();
mf.SplitingToStream(form.Handle, st1);
form.Close();
return st1.ToString();
}
Thanks in advance.