.NET

Reply
Member
Posts: 5
Registered: ‎07-16-2009
Message 1 of 6 (483 Views)

Autocad Get all elements

483 Views, 5 Replies
09-03-2012 01:10 AM

Hi guys,

How i can browse all objects into an autocad database ???

*Expert Elite*
Hallex
Posts: 1,558
Registered: ‎10-08-2008
Message 2 of 6 (472 Views)

Re: Autocad Get all elements

09-03-2012 03:19 AM in reply to: ltf123456

There are many ways to get all objects from db

Here is what you can to use, expand functionality

to your suit:

 

        [CommandMethod("gall")]
        public void GetAllFromDb()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            ObjectIdCollection ids = new ObjectIdCollection();
            using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                for (long i = 0; i < db.Handseed.Value; i++)
                {
                    ObjectId id = ObjectId.Null;
                    db.TryGetObjectId(new Handle(i), out id);
                    if (id != ObjectId.Null && !id.IsErased) ids.Add(id);
                }
                foreach (ObjectId id in ids)
                {
                    DBObject obj = tr.GetObject(id, OpenMode.ForRead, false) as DBObject;
                    ed.WriteMessage("\nType of:\t{0}\t{1}\n", obj.GetRXClass().DxfName,id);
                }
            }
        }

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Member
Posts: 5
Registered: ‎07-16-2009
Message 3 of 6 (471 Views)

Re: Autocad Get all elements

09-03-2012 03:22 AM in reply to: Hallex

thanks a lot, let me try it

Member
Posts: 5
Registered: ‎07-16-2009
Message 4 of 6 (457 Views)

Re: Autocad Get all elements

09-03-2012 07:17 AM in reply to: ltf123456

This code is working fine , no problem on that, but what i'm looking for is, how i can get all the objects user does, because i need to export all objects (lines, circles...etc) and in the xml, I will export all properties of those objects (air, length...etc)

 

 

thanks a lot of help

*Expert Elite*
Hallex
Posts: 1,558
Registered: ‎10-08-2008
Message 5 of 6 (455 Views)

Re: Autocad Get all elements

09-03-2012 07:39 AM in reply to: ltf123456

In this case better yet to use datextraction methods,

search for this on google or on "through the interface" web page:

http://through-the-interface.typepad.com/

there is an example in there

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
*Expert Elite*
Hallex
Posts: 1,558
Registered: ‎10-08-2008
Message 6 of 6 (448 Views)

Re: Autocad Get all elements

09-03-2012 09:03 AM in reply to: ltf123456

Here is my old code I've grabbed from Kean Walmsley

See if this helps

 

        #region "System"
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Data;
using System.Timers;
using System.Windows.Forms;
#endregion

#region "AutoCAD Imports"

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DataExtraction;
#endregion
//--------------------------------------------------//

       //' based on Kean Walmsley's example from there:
        //' http://through-the-interface.typepad.com/through_the_interface/2008/04/extracting-data.html
        [CommandMethod("extdata")]
        public void ExtractData()
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            double starttime = 0;
            double endtime = 0;

            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();

            dialog.CheckPathExists = true;
            dialog.CheckPathExists = true;
            dialog.DefaultExt = "dwg";
            dialog.DereferenceLinks = true;
            dialog.Multiselect = false;
            dialog.Filter = "Drawing (*.dwg)|*.dwg|All files (*.*)|*.*";
            dialog.Title = "Select drawing";
           dialog.FilterIndex = 1;


            if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            string fname = dialog.FileName;
            System.Timers.Timer _timer = new System.Timers.Timer();
          _timer.Interval = 1000;
          _timer.Start();
          starttime = DateTime.Now.TimeOfDay.TotalMilliseconds;


            try
            {
                //------------------------------------------------------------------------------------------------------
                // following datatable easy to write to the any data file (Excel, Access etc)

                System.Data.DataTable dataTable = extractLinesAndTexts(fname);

                 _timer.Stop();
                //------------------------------------------------------------------------------------------------------
                     endtime = DateTime.Now.TimeOfDay.TotalMilliseconds;
                //' write data to Lines.xml in the Debug folder
                dataTable.WriteXml("LinesAndTexts.xml");

                ed.WriteMessage("\nElapsed time: {0:f2} milliseconds" + "\n" + "Found {1} objects\n" , endtime - starttime, dataTable.Rows.Count);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString() + "\n" + ex.StackTrace);
            }
            //' Display the  file
            Process.Start("LinesAndTexts.xml", null);
        }

        public System.Data.DataTable extractLinesAndTexts(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.\n");
                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 = "LinesAndTexts";

                DataRow[] selrows = dataTable.Select("AcDxObjectTypeGlobalName Like '%Line' Or AcDxObjectTypeGlobalName Like '%Text' Or AcDxObjectTypeGlobalName Like '%MText'");

                blkTable = dataTable.Clone();

                foreach (DataRow dr in selrows)
                {
                    blkTable.ImportRow(dr);
                }
                //' commented code lines is just for populating a form listBox control:
                //Dim columns As String() = New String() {"AcDxObjectTypeName"}

                //Dim dvw As DataView = blkTable.DefaultView
                //distinctTable = dvw.ToTable(False, columns)

                //Dim bnames As New List(Of String)()
                //For Each dr As System.Data.DataRow In distinctTable.Rows
                //    Dim bname As String = dr(0).ToString()
                //    If Not bnames.Contains(bname) Then
                //        bnames.Add(bname)
                //    End If
                //Next

                //Me.lst.DataSource = bnames'' <-- to fill ListBox only

                //lst.SelectedIndex = -1'' <-- set selected items to nothing
            }

            return blkTable;

        }

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community