Coordinates Table to Text File C#

Coordinates Table to Text File C#

k005
Advisor Advisor
760 Views
6 Replies
Message 1 of 7

Coordinates Table to Text File C#

k005
Advisor
Advisor

 

 

Hello

How can I save the coordinate list to a text file?

Thanks.

0 Likes
761 Views
6 Replies
Replies (6)
Message 2 of 7

fsztuczny
Advocate
Advocate

Add reference to the System.Windows.Forms

 

Zrzut ekranu 2021-04-17 172054.png

    [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"

Message 3 of 7

k005
Advisor
Advisor

@fsztuczny 

 

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

0 Likes
Message 4 of 7

fsztuczny
Advocate
Advocate

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() );
            }

 

0 Likes
Message 5 of 7

k005
Advisor
Advisor

@fsztuczny 

 

In that :

in some cases there is a table.
in some cases there is also DBTEXT.

 

0 Likes
Message 6 of 7

fsztuczny
Advocate
Advocate

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.

0 Likes
Message 7 of 7

k005
Advisor
Advisor

@fsztuczny 

 

Thank you so much. For your help

0 Likes