Add reference to the System.Windows.Forms
[CommandMethod( "tableToTextFile", CommandFlags.Modal )]
public void tableToTextFile() // This method can have any name
{
// Put your command code here
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
try
{
PromptEntityOptions peo = new PromptEntityOptions( "\nPick a table: " );
peo.SetRejectMessage( "\nThis is not a table!" );
peo.AddAllowedClass( typeof( Table ), true );
PromptEntityResult per = ed.GetEntity( peo );
if( per.Status != PromptStatus.OK )
{
return;
}
ObjectId tableId = per.ObjectId;
string filePath = null;
using( System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog() )
{
saveDialog.Title = "Save the text file as...";
saveDialog.DefaultExt = "*.txt";
saveDialog.Filter = "Text files|" + saveDialog.DefaultExt;
saveDialog.AddExtension = true;
saveDialog.CheckFileExists = false;
saveDialog.CheckPathExists = true;
saveDialog.OverwritePrompt = true;
saveDialog.FilterIndex = 0;
saveDialog.InitialDirectory = Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments );
System.Windows.Forms.DialogResult dr = saveDialog.ShowDialog();
if( dr != System.Windows.Forms.DialogResult.OK )
{
return;
} //
filePath = saveDialog.FileName;
} //using( System.Windows.Forms.SaveFileDialog openDialog = new System.Windows.Forms.SaveFileDialog() )
using( System.IO.StreamWriter sw = new System.IO.StreamWriter( filePath ) )
{
const string fileColumnSeparator = "\t" /*tabulator*/;
using( Transaction tr = db.TransactionManager.StartOpenCloseTransaction() )
{
Table tab = tr.GetObject( tableId, OpenMode.ForRead ) as Table;
for( int r = 0; r < tab.Rows.Count; r++ )
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for( int c = 0; c < tab.Columns.Count; c++ )
{
sb.Append( tab.Cells[r, c].TextString );
if( c < tab.Columns.Count - 1 )
{
sb.Append( fileColumnSeparator );
}
}
sw.WriteLine( sb.ToString() );
}
}
}
}
catch( System.Exception e )
{
ed.WriteMessage( e.ToString() );
}
}
You can also using built-in command: "_TABLEEXPORT"
The codes are working properly. saved the txt file.
Can I print only "Num. of .vert" "X", "Y" values without a table?
so the table is exploded
Sure. You can change the loop:
for( int r = 1; r < tab.Rows.Count; r++ )
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for( int c = 0; c < tab.Columns.Count; c++ )
{
sb.Append( tab.Cells[r, c].TextString );
if( c < tab.Columns.Count - 1 )
{
sb.Append( fileColumnSeparator );
}
}
sw.WriteLine( sb.ToString() );
}
Unfortunately, this is related to the multi-level sorting of the text insertion coordinates and taking into account the text dimensions. This is not to be done in a moment. May introduce potential errors in the order of the texts. Stay with the table.
Maybe someone else will find a simple solution.