Subscribe to this thread
Home - General / All posts - C# export script
bclement
273 post(s)
#01-Sep-22 00:41

//C#

using System;

using System.Collections.Generic;

class Script

{

 static Manifold.Context Manifold;

 static void Main()

 {

  Manifold.Application app = Manifold.Application;

  using (Manifold.Database db = app.GetDatabaseRoot())

  {

  using (Manifold.Table table = db.Search("mfd_root"))

  {

  string[] fields = new string[] { "Name""Type" };

  using (Manifold.Sequence sequence = table.SearchAll(fields))

  {

  while (sequence.Fetch())

 {

 string compName = sequence.GetValues()[0].Data.ToString(); 

 string compType = sequence.GetValues()[1].Data.ToString();

 if(compType == "table")

 {

// app.MessageBox(compName + " is a table.","");

 db.ExportFile(compName, @"C:\Users\ben.clement\Documents\Orion\WorkSpace\Export\"+compName+".csv");

 }

 }

 

  }

  }

  }

  app.MessageBox("Export complete!""Message");

 }

}

bclement
273 post(s)
#01-Sep-22 00:45

Just a quick and dirty rip-off of the API mostly. Used in this case to export all the tables in a version 9 project to .csv files. You can change the compType test to look for other component types. The available types are listed in the mfd_root table. You would need to change the extension type as well from .csv.

adamw


10,175 post(s)
#01-Sep-22 08:10

Nice!

Several small ideas for the community. :-)

  • Skip system tables (either check for 'mfd_root' and 'mfd_meta', or, better yet, check for names starting with 'mfd_', all of these names are reserved by Manifold).
  • Specify the folder to export to using a dialog (System.Windows.Forms.FolderBrowserDialog, the example code in the link is unbearably long though, it does so many things unrelated to folder selection...).
  • Convert the script into an add-in so that it can be used from any MAP file, including a read-only one (this is easiest: just save the script as a .CS file, it can then be put into the ~\extras folder).

Good script.

bclement
273 post(s)
#02-Sep-22 23:39

code

//C#

using System;

using System.Collections.Generic;

class Script

{

  static Manifold.Context Manifold;

  static void Main()

  {

    Manifold.Application app = Manifold.Application;

    using (Manifold.Database db = app.GetDatabaseRoot())

    {

      using (Manifold.Table table = db.Search("mfd_root"))

      {

        string[] fields = new string[] { "Name""Type" };

        using (Manifold.Sequence sequence = table.SearchAll(fields))

        {

          while (sequence.Fetch())

   {

            string compName = sequence.GetValues()[0].Data.ToString(); 

 string compType = sequence.GetValues()[1].Data.ToString();

 if(compName.StartsWith("mfd_"))

 {

//              app.MessageBox("Found root table " + compName + ".""Message");

 }

 else if(compType == "table")

 {

//   app.MessageBox(compName + " is a table.","");

   db.ExportFile(compName, @"C:\Users\ben.clement\Documents\Orion\WorkSpace\Export\Circuits\"+compName+".csv");

 }

   }

        }

      }

    }

  app.MessageBox("Export complete!""Message");

  }

}

Sloots

603 post(s)
online
#06-Sep-22 08:10

Put the attached file in the ~/extras folder. The script includes the system table skip implemented by bclement and asks for a folder where to store the output files.

Have fun,

Chris

Attachments:
Export tables to CSV.cs


http://www.mppng.nl/manifold/pointlabeler

yves61
356 post(s)
#06-Sep-22 12:20

How could this script with some changes to it export all tableNames, fieldNames and fieldTypes in a new table in M9?

bclement
273 post(s)
#06-Sep-22 15:34

I will give it a look-see. Should not be a big deal, just need to get to the column and type collections. Will also need to learn how to write a new table. Good learning opportunity.

yves61
356 post(s)
#06-Sep-22 16:37

@bclement: thank you for willing to look at this.

I don't know C# programming, but you may have a look at this script for inspiration if need be.

https://manifold.net/doc/mfd9/tools_-_add-ins.htm

bclement
273 post(s)
#09-Sep-22 23:16

//c#

//These statements load code libraries that make basic functions (in this case, arrays for instance) work.

//You will know to add more when you reference and object and which throws and error that asks if you are 

//missing a reference.

using System;

using System.Collections.Generic;

//This is the class that contains all the code. This is what you need to put all of the example code in to 

//make it run. The name "Script" is not important, its the container that counts.

class Script

{

  //This helps set up the Manifold environment (probably not stated exactly correctly).

  static Manifold.Context Manifold;

  //This is the entry point for the whole program. Every C# program has to have one.

  static void Main()

  {

    //Declare the Manifold Application object and name it app.

    Manifold.Application app = Manifold.Application;

 

    //Declare the Manifold Database object and name it db.

    //Also use the 'using' keyword for quick garbage disposal. Different reason for using the same

    //"using" statement at the top.

    using (Manifold.Database db = app.GetDatabaseRoot())

    {

      //Declare the Manifold Schema object and name it schema.

      Manifold.Schema schema = app.CreateSchema();

      //Create a string variable to hold the table name.

      string nameOfTableToAdd = "tableProperties";

 

      //Add TableName, ColumnName, and ColumnType column names to schema.

      schema.AddField("TableName""nvarchar");

      schema.AddField("ColumnName""nvarchar");

      schema.AddField("ColumnType""nvarchar");

      //Create a table calling Database Insert and name the table tableProperties.

      db.Insert(nameOfTableToAdd, "table", schema);

      //Declare another table (different to the mfd_root table we are going to read from) to write to.

      Manifold.Table propsTable = db.Search(nameOfTableToAdd);

      //Get a handle on the Manifold Root table and name it rootTable.

      //Using "using" again here is totally not necessary as the outer one will call garbage soon enough.

      //Just left them in here from the examples.

      using (Manifold.Table rootTable = db.Search("mfd_root"))

      {

        //Create a string array named fields with two fields, Name and Type to hold data from the root table.

        string[] fields = new string[] { "Name""Type" };

 

        //Get a sequence call it sequence and then make a call to SearchAll and dump the data into the fields array.

        using (Manifold.Sequence sequence = rootTable.SearchAll(fields))

        {

          //Call Fetch repeatedly. 

          while (sequence.Fetch())

   {

            //Get the first and second values from fetch and put them in compName and compType respectively. 

            string compName = sequence.GetValues()[0].Data.ToString(); 

 string compType = sequence.GetValues()[1].Data.ToString();

 

            //Check for the two Manifold tables at the root and skip them.

 if(compName.StartsWith("mfd_"))

 {

              //Message box for troubleshooting. Comment out when no longer needed.

              //app.MessageBox("Found root table " + compName + ".", "Message");

 }

            //Check to make sure the component is of type table. Other possible component types are in the mfd_root table.

            //If you wanted to look for a different component type, you would change it here.

 else if(compType == "table")

 {

   //Log compName for troubleshooting. Comment it out when no longer needed.

              app.Log(compName + " is a table.");

              //Search the table compName and put it's schema into the tableSchema variable.

              using (Manifold.Table tableSchema = db.Search(compName))

              {

                //Get a handle on a schema named schemaTwo to hold the table components from the GetSchema function.

                Manifold.Schema schemaTwo = tableSchema.GetSchema();

                //Call the fields function of the schema once for each row that exists.

                foreach (Manifold.Schema.Field field in schemaTwo.Fields)

                { 

                  //Log for troubleshooting. Comment out when no longer needed.

                  //app.Log(field.Name + " : " + field.Type);

                  //Set up a valueSet named values.

                  Manifold.ValueSet values = app.CreateValueSet();

                  //Start adding values to the valueSet values.

                  values.AddValue("TableName", compName);

                  values.AddValue("ColumnName", field.Name);

                  values.AddValue("ColumnType", field.Type);

                  //Add the values to the table.

                  long result = propsTable.Insert(values);

                }

              }

 }

   }

        }

      }

        //Message Box for end of process notification. Update as necessary.

  app.MessageBox("Table created!""Message");

    }

  }

}

yves61
356 post(s)
#10-Sep-22 14:23

@bclement

Thank you so much for this script! And well documented step by step ! Works like a charm.

When possible will dive in the API documentation to learn more from the examples.

bclement
273 post(s)
#10-Sep-22 20:59

Your welcome. It was my intent to document this line by line to help others. I am not very good at scripting myself and I know how hard it was to learn what to do in V8. So I am trying to help give a leg up as I learn V9.

bclement
273 post(s)
#06-Sep-22 15:10

Excellent work Chris! I had no idea how to go about that step. Now how hard would it be to ask the user for an export type with a dropdown, read in all components of that type from the root, and provide a checklist to the user for which components to export? Then it could just be named bulk export!

Manifold User Community Use Agreement Copyright (C) 2007-2021 Manifold Software Limited. All rights reserved.