I didn't tried to create DXF file,
here is just code to select all blocks from Model
then data would be saved in xml file in the project/debug folder
See if this helps anyway, wait till XML file will be appears at the end
of command
[CommandMethod("exblocks", CommandFlags.Modal | CommandFlags.Session | CommandFlags.Transparent)]
public void ExtractBlocks()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
System.Windows.Forms.OpenFileDialog _dia = new System.Windows.Forms.OpenFileDialog();
_dia.CheckPathExists = true;
_dia.CheckPathExists = true;
_dia.DefaultExt = "dwg";
_dia.DereferenceLinks = true;
_dia.Multiselect = false;
_dia.Filter = "Drawing (*.dwg)|*.dwg|All files (*.*)|*.*";
_dia.Title = "Select drawing";
_dia.FilterIndex = 1;
if (_dia.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
return;
}
string fname = _dia.FileName;
try
{
//------------------------------------------------------------------------------------------------------''
// following datatable easy to write to the any data file (Excel, Access etc)
System.Data.DataTable dataTable = ExtractLines(fname);
//------------------------------------------------------------------------------------------------------''
// write data to Lines.xml in the Debug folder
dataTable.WriteXml("blocks.xml");
}
catch (System.Exception ex)
{
ed.WriteMessage("\n{0]\n{1}\n", ex.Message, ex.StackTrace);
}
// Display the file
Process.Start("blocks.xml", null);
}
public System.Data.DataTable ExtractLines(string fname)
{
System.Data.DataTable dataTable = new System.Data.DataTable();
System.Data.DataTable blkTable = new System.Data.DataTable();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
List<string> tags = new List<string>();
if (!System.IO.File.Exists(fname))
{
ed.WriteMessage("\nDrawing file does not exist.");
return null;
}
IDxExtractionSettings es = new DxExtractionSettings();
IDxDrawingDataExtractor de = es.DrawingDataExtractor;
de.Settings.ExtractFlags = ExtractFlags.None | ExtractFlags.ModelSpaceOnly;
//de.Settings.ExtractFlags = ExtractFlags.ExtractBlockOnly;
//'Or ExtractFlags.ModelSpaceOnly
IDxFileReference fr = new DxFileReference(Path.GetDirectoryName(fname), fname);
de.Settings.DrawingList.AddFile(fr);
// Scan the drawing for object types & their properties
de.DiscoverTypesAndProperties(Path.GetDirectoryName(fname));
List<IDxTypeDescriptor> types = de.DiscoveredTypesAndProperties;
// Select all the types and properties for extraction
// by adding them one-by-one to these two lists
List<string> selTypes = new List<string>();
List<string> selProps = new List<string>();
foreach (IDxTypeDescriptor type in types)
{
selTypes.Add(type.GlobalName);
foreach (IDxPropertyDescriptor pr in type.Properties)
{
if (!selProps.Contains(pr.GlobalName))
{
selProps.Add(pr.GlobalName);
}
}
}
de.Settings.SetSelectedTypesAndProperties(types, selTypes, selProps);
// Now perform the extraction itself
de.ExtractData(Path.GetDirectoryName(fname));
// Get the results of the extraction
dataTable = de.ExtractedData;
if (dataTable.Rows.Count > 0)
{
dataTable.TableName = "Blocks";
//DataRow[] selrows = dataTable.Select("AcDxObjectTypeName Like '%BlockReference*'");// not working
DataRow[] selrows = dataTable.Select("AcDxObjectTypeGlobalName Like 'BlockReference*'");
blkTable = dataTable.Clone();
foreach (DataRow dr in selrows)
{
blkTable.ImportRow(dr);
}
}
return blkTable;
}