.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Data Extraction Using .Net

21 REPLIES 21
SOLVED
Reply
Message 1 of 22
santoshr0114
9924 Views, 21 Replies

Data Extraction Using .Net

Hi Guys,

 

I need to extract data from AutoCad Drawing using C# or VB.Net.

 

I have a Title Block which is not inserted using attributes. It has some horizontal & vertical lines drawn  to look like a table and text added between them. See the attchment.

 

I need to extract data from that and display in Ms-Excel or in a datagridview.

 

Does anybody know how to do it ?????

It would be very helpful.

 

Thank You

Regards
Santosh
21 REPLIES 21
Message 2 of 22
HJohn1
in reply to: santoshr0114

Personally I think that using attributes is more efficient than using plain text.  You can more easilly create a routine to select the block containing the attributes and also since you would know what data type is expected on each attribute validate it before you extract it.  There are many sample around on how to create selection sets, extract attributes, etc.

Message 3 of 22
Hallex
in reply to: santoshr0114

Upload your drawing with this block (< A2010),

maybe I can help

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 22
AubelecBE
in reply to: Hallex

hi you can't extract plain text from a block. You have to use attribut.

 

Autocad have a subroutine for extract attribut. No need à code vb.net for doing this.

 

 

++

Message 5 of 22
Hallex
in reply to: AubelecBE

Sorry, but I can to do it easy,the same way as for constant attributes

using BlockTableRecord's ObjectIds of this BlockReference

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 6 of 22
santoshr0114
in reply to: santoshr0114

Hi,

 

Sorry for the delay in uploading the drawing file(.dwg).

 

 

First Task:

 

I need to extract all the text from the drawing using C# or VB.Net and display it in notepad or excel file.

 

Second Task:

 

I have to fetch only the text data from the left bottom corner(BOM) and the right bottom corner(Title Block) in the same format as it is displayed i autocad drawing and send it to excel.

 

Please let me know how to do it...

 

 

 

Regards
Santosh
Message 7 of 22
Hallex
in reply to: santoshr0114

In other words you want to retrive:

"AP-0298" and "Sample"

Or I'm wrong?

 

You wrote

>>I need to extract all the text from the drawing using C# or VB.Net and display it in notepad or excel file.

 

Just the text only or you need to get mtext as well?

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 22
santoshr0114
in reply to: santoshr0114

Hallex,

 

Yes...

Regards
Santosh
Message 9 of 22
Hallex
in reply to: santoshr0114

Something wrong with your drawing, there are too many invisible texts,

see complete code:

using System;
using System.IO;
using System.Text;
using System.Collections;

using System.Collections.Generic;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Interop;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;

using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in1.ExcelReport))]

namespace AutoCAD_CSharp_plug_in1
{
    class ExcelReport
    {
        [CommandMethod("xlrep")]
        public static void TextSelectToExcel()
        {
            string title = "";
            string jobno = "";
            List<string> alltexts = new List<string>();

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            string xlFileName = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("dwgprefix");
            string dwgname = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("dwgname");
            dwgname = dwgname.Substring(0, dwgname.Length - 4) + ".xlsx";
            xlFileName = Path.Combine(xlFileName, dwgname);

            Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 0);
            try
            {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptPointOptions ppo = new PromptPointOptions("\nSelect a first corner of table: ");
                PromptPointResult ppr = ed.GetPoint(ppo);
                if (ppr.Status != PromptStatus.OK) return;
                PromptCornerOptions pco = new PromptCornerOptions("\nOther corner: ", ppr.Value);
                PromptPointResult pcr = ed.GetCorner(pco);
                if (pcr.Status != PromptStatus.OK) return;
                Point3d p1 = ppr.Value;
                Point3d p2 = pcr.Value;
                if (p1.X == p2.X || p1.Y == p2.Y)
                {
                    ed.WriteMessage("\nInvalid coordinate specification");
                    return;
                }
                TypedValue[] tvs = new TypedValue[]
            {new TypedValue(0, "text")

            };
                SelectionFilter filter = new SelectionFilter(tvs);

                PromptSelectionResult result = ed.SelectCrossingWindow(p1, p2, filter);
                if (result.Status != PromptStatus.OK) return;
                Dictionary<Point3d, string> txtlist = new Dictionary<Point3d, string>();

                foreach (SelectedObject selobj in result.Value)
                {

                    DBObject obj = tr.GetObject(selobj.ObjectId, OpenMode.ForRead) as DBObject;
                    DBText txt = obj as DBText;
                    if (txt != null)
                    {
                        string strtext = txt.TextString;
                        Point3d pp = txt.Position;
                        if (!txtlist.ContainsKey(pp))
                            txtlist.Add(pp, strtext);
                    }

                }

                List<Point3d> ptlist = new List<Point3d>(txtlist.Keys);

                ptlist.Sort(delegate(Point3d pa, Point3d pb)
                { return (pa.Y.CompareTo(pb.Y)); });

                ptlist.Reverse();

                title = txtlist[ptlist[6]];
                jobno = txtlist[ptlist[5]];

                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(string.Format
                ("Title: {0}\nJob No.: {1}", title, jobno));

                //---------------------------------------------------------------------------//

                PromptSelectionResult result2 = ed.SelectAll(filter);

                if (result2.Status != PromptStatus.OK) return;



                foreach (SelectedObject selobj2 in result2.Value)
                {

                    DBObject obj = tr.GetObject(selobj2.ObjectId, OpenMode.ForRead) as DBObject;
                    DBText txt2 = obj as DBText;
                    if (txt2 != null)
                    {
                        string strtext = txt2.TextString;
                       if (strtext!=string.Empty)
                        alltexts.Add(strtext);
                    }

                }



            }

            //---------------------------------------------------------------------------//
            Excel.Application xlApp;
            Excel.Workbooks xlBooks;
            Excel.Workbook xlBook;
            Excel.Sheets xlSheets;
            Excel.Worksheet xlSheet;
            Excel.Range xlRange;
          
                xlApp = new Excel.Application();

                xlApp.Visible = false;

                xlApp.SheetsInNewWorkbook = 1;

                xlApp.Workbooks.Add(Type.Missing);

                xlBooks = (Excel.Workbooks)xlApp.Workbooks;
                xlBook = (Excel.Workbook)xlBooks[1];
                xlBook.Saved = true;
                xlApp.DisplayAlerts = false;


                xlSheets = (Excel.Sheets)xlBook.Worksheets;
                xlSheet = (Excel.Worksheet)xlSheets.get_Item(1);

                xlRange = (Excel.Range)xlSheet.Range["A1"];
                xlRange.ColumnWidth = 20;
                xlRange.NumberFormat = "@";
                int row = 1;
                xlRange.Cells[row, 1] = title;

                row = 1;

                xlRange = (Excel.Range)xlSheet.Range["B1"];
                xlRange.ColumnWidth = 20;
                xlRange.NumberFormat = "@";
                xlRange.Cells[row, 1] = jobno;

                xlRange = (Excel.Range)xlSheet.Range["A2"];
                int r = 1;
                foreach (string s in alltexts)
                {
                    xlRange.Cells[r, 1] = s;
                    r += 1;
                }
         
                //Save workbook
                xlBook.SaveAs(xlFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                xlApp.Workbooks.Close();
                xlApp.Quit();
                //or
                // xlApp.Windows[1].Close(false, Type.Missing, Type.Missing);
            }
            catch (System.Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
                    "\n{0}", ex.Message);
            }
            finally
            {
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Remove blank rows from Excel file manually");
            }
        }
    }
}

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 10 of 22
santoshr0114
in reply to: santoshr0114

Hallex,

 

Thank You.

 

I will try using this, but i think it asks for user to select some objects.

 

Let me try this now...

 

Regards
Santosh
Message 11 of 22
Hallex
in reply to: santoshr0114

Let me know after what I need to change in there

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 12 of 22
santoshr0114
in reply to: santoshr0114

Hallex,

 

I tried your code but everytime it asks for users selection. I can work more on your code and get it done my way.

 

I have figured out one more method where i can get all the text/string available in Entire Autocad Drawing, and also i am able to send it to excel file using OLEDB. I have done it in VB.Net. I will change the code to C# once i finish my task.

 

But the problem is:

  1. It is taking lot of time to finish the execution and sometimes it is also entering into inifinte loop.
  2. I am unable to link one text to another.(E.g: JOB NO is AP-0298 & PART-MARK is 09-401) Check the screenshot of the excel file.
  3. The text/string we are getting in excel is jumbled up and due to hidden text in drawing there are lot of empty rows in excel.

The solution i thought for linking the text with one another is.

Getting the Position (Co-ordinates) of the title (JOB NO) and assuming that X axis is same, we will give a specific range of Y co-ordinates that if a text/string exists in this range grab the text. and store it in an array/list.

 

Then we will have all the Heading/Title in one array and the values in one array. This is the idea for the Right Bottom table(Title Block)

 

For the Left bottom table(BOM). Search for the string Mark and increment only the Y Co-ordinate range and grab all the text untill the range gets bigger(As the last row and the first row height is big) And the same logic for other columns.

 

 

I have this idea but i am not sure how to do it.....

 

Regards
Santosh
Message 13 of 22
Hallex
in reply to: santoshr0114

It is very scrupulous operation because all of the texts

 have a different coordinates of Y in the row, so I can't do the correct sorting

of these texts according this Y value, sorry, it is impossible to me

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 14 of 22
santoshr0114
in reply to: Hallex

Hi Hallex,

 

Thank You for your reply.

 

I am really interested in doing this, as this is quite challenging. Is there any possible approach.

 

You can give me some hints, as this is my first try in autocad customization.

 

 

 

Regards
Santosh
Message 15 of 22
santoshr0114
in reply to: santoshr0114

Hi Hallex,

 

I have sucessfully copleted the task with a help of very senior person who has experience on Autocad customisation.

 

It was vary challenging.

 

Thank You for your time and responses

 

 

Regards
Santosh
Message 16 of 22
Hallex
in reply to: santoshr0114

Good to hear,

Cheers 🙂

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 17 of 22

Hi Santosh,

 

I am trying to extract the Text from the dwg files - we have 100's of files from which we have to extract the data and save them to an individual excel report.

But we are not able to get this through. i am new to this autocad or dwg stuff, i know only c#, VB . 

As you specified that you have the code for extracting the data from .dwg files right . Can you help me with that whole code which helps me a lot. you can send me the code via mail specified below if you can.

 

Please do the needful.

 

Thanks,

Sai

ajay.potti@gmail.com

Message 18 of 22
divyaro463
in reply to: santoshr0114

Hi Santhosh,

              Can you share the code, I want to achieve the same 

Thanks,

Divz

Message 19 of 22
jrozell
in reply to: santoshr0114

Ya could you share the code like the guy above me said? I'm trying to learn but tend to learn better by examples, thanks.

Message 20 of 22

Hi Santosh,

I also have same requirement , can you please help out here.

if you share source code sample how can I give drawing URL and read text that will be more appreciable.

My email is jatin.vyas@rigelnetworks.com

Awaiting your reply.

 

Thanks,

Jatin Vyas.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost