Author Topic: How do I link a data extraction with an autocad table?  (Read 3112 times)

0 Members and 1 Guest are viewing this topic.

PFICAD

  • Guest
How do I link a data extraction with an autocad table?
« on: April 03, 2013, 08:14:31 AM »
Hi,

I use the Data Extraction API with VB.NET to extract attribute values from blocks in a drawing. The data extraction part works fine but finally I want to link the data  with a table inserted in the drawing the same way as eattext this do.

I found only code snippets to connect with a excel spreadsheet.

Thanks for help!


PFICAD

  • Guest
Re: How do I link a data extraction with an autocad table?
« Reply #2 on: April 03, 2013, 02:41:04 PM »
Hi fixo

Thanks for the hint. The sample from Kean connects to an excel spreadsheet. But I want to link a data extraction result with the autocad table. For the data extraction I used the informations I found here:

http://aucache.autodesk.com/au2008/sessions/3054/CP301-2L_Data-Extraction-API.pdf

I believe that the key to solve the problem is the correct value for the DataLink.ConnectionString property. But I can't find more information about the data extraction API.

PFICAD

  • Guest
Re: How do I link a data extraction with an autocad table?
« Reply #3 on: April 04, 2013, 04:19:26 AM »
I did some further research. If I use EATTEXT to create a table linked to the block attributes inserted in the drawing, the DataLink.ConnectionString is the full path to the DXE file I used for the data extraction. So far, so good. But now, I have the following problem: If I use the Data Extraction API to create a data extraction configuration programmatically, I can't save the configuration to a DXE file. There is only a function to load a configuration from file (DxExtractionSettings.FromFile). Has anybody an idea, how I can write the settings to a file?

fixo

  • Guest
Re: How do I link a data extraction with an autocad table?
« Reply #4 on: April 04, 2013, 07:55:26 AM »
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

Code: [Select]
        [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;

        }

PFICAD

  • Guest
Re: How do I link a data extraction with an autocad table?
« Reply #5 on: April 04, 2013, 10:11:21 AM »
I did nearly the same things as you in your code sample. If I link the exported xml file with the autocad table, the autocad table is not updated if a block attribute is changed. As far as I can see, to get a data link with automatic updates, it is necessary to use a dxe file.

PFICAD

  • Guest
Re: How do I link a data extraction with an autocad table?
« Reply #6 on: April 25, 2013, 03:57:19 AM »
Balaji Ramamoorthy from the AutoCAD DevBlog has created a code sample that exactly covers my question. The sample code could be found here:

http://adndevblog.typepad.com/autocad/2013/04/linking-attributes-and-table-using-dataextraction-api.html

fixo

  • Guest
Re: How do I link a data extraction with an autocad table?
« Reply #7 on: April 25, 2013, 09:05:24 AM »
Tested on my A2010
It's working like a sharm,
Thanks